mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
removed generated and generationStrategy parameters from column options;
This commit is contained in:
parent
d4cb010875
commit
cd3efb406f
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,4 +6,3 @@ node_modules/
|
||||
ormconfig.json
|
||||
ormlogs.log
|
||||
npm-debug.log
|
||||
/src/connection/BaseConnectionOptions.ts
|
||||
|
||||
@ -62,6 +62,8 @@ Also now all
|
||||
* create and update dates in entities now use date with fractional seconds.
|
||||
* `@PrimaryGeneratedColumn` decorator now accept generation strategy as first argument (default is `increment`), instead of column type.
|
||||
Column type must be passed in options object, e.g. `@PrimaryGeneratedColumn({ type: "bigint"})`
|
||||
* `@PrimaryColumn` now does not accept `generated` parameter in options. Use `@Generated` or `@PrimaryGeneratedColumn`
|
||||
decorators instead
|
||||
* Logger interface has changed. Custom logger supply mechanism has changed
|
||||
* Now `logging` options in connection options is simple "true", or "all", or list of logging modes can be supplied
|
||||
* removed `driver` section in connection options. Define options right in the connection options section.
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
import {Column, Entity} from "../../../src/index";
|
||||
import {PrimaryColumn} from "../../../src/decorator/columns/PrimaryColumn";
|
||||
import {Generated} from "../../../src/decorator/Generated";
|
||||
|
||||
@Entity("sample01_post")
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn("integer", { generated: true })
|
||||
@PrimaryColumn("integer")
|
||||
@Generated()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -3,10 +3,12 @@ import {BasePost} from "./BasePost";
|
||||
import {PostAuthor} from "./PostAuthor";
|
||||
import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
|
||||
import {PrimaryColumn} from "../../../src/decorator/columns/PrimaryColumn";
|
||||
import {Generated} from "../../../src/decorator/Generated";
|
||||
|
||||
export class BaseObject extends BasePost {
|
||||
|
||||
@PrimaryColumn("double", { generated: true })
|
||||
@PrimaryColumn("double")
|
||||
@Generated()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import {ColumnOptions} from "../options/ColumnOptions";
|
||||
import {GeneratedOnlyForPrimaryError} from "../../error/GeneratedOnlyForPrimaryError";
|
||||
import {getMetadataArgsStorage} from "../../index";
|
||||
import {
|
||||
ColumnType,
|
||||
@ -105,10 +104,6 @@ export function Column(typeOrOptions?: ((type?: any) => Function)|ColumnType|(Co
|
||||
if (!options.type && type)
|
||||
options = Object.assign({ type: type } as ColumnOptions, options);
|
||||
|
||||
// check if auto increment is not set for simple column
|
||||
if (options.generated && options.type !== "uuid")
|
||||
throw new GeneratedOnlyForPrimaryError(object, propertyName);
|
||||
|
||||
// create and register a new column metadata
|
||||
const args: ColumnMetadataArgs = {
|
||||
target: object.constructor,
|
||||
|
||||
@ -49,9 +49,6 @@ export function PrimaryColumn(typeOrOptions?: ColumnType|ColumnOptions, options?
|
||||
if (!options.type && type)
|
||||
options = Object.assign({ type: type } as ColumnOptions, options);
|
||||
|
||||
if (options.generated)
|
||||
options.generationStrategy = options.type === "uuid" ? "uuid" : "increment";
|
||||
|
||||
// if we still don't have a type then we need to give error to user that type is required
|
||||
if (!options.type)
|
||||
throw new ColumnTypeUndefinedError(object, propertyName);
|
||||
|
||||
@ -3,6 +3,7 @@ import {ColumnMetadataArgs} from "../../metadata-args/ColumnMetadataArgs";
|
||||
import {PrimaryGeneratedColumnNumericOptions} from "../options/PrimaryGeneratedColumnNumericOptions";
|
||||
import {ColumnOptions} from "../options/ColumnOptions";
|
||||
import {PrimaryGeneratedColumnUUIDOptions} from "../options/PrimaryGeneratedColumnUUIDOptions";
|
||||
import {GeneratedMetadataArgs} from "../../metadata-args/GeneratedMetadataArgs";
|
||||
|
||||
/**
|
||||
* Column decorator is used to mark a specific class property as a table column.
|
||||
@ -33,15 +34,18 @@ export function PrimaryGeneratedColumn(strategy: "uuid", options?: PrimaryGenera
|
||||
export function PrimaryGeneratedColumn(strategyOrOptions?: "increment"|"uuid"|PrimaryGeneratedColumnNumericOptions|PrimaryGeneratedColumnUUIDOptions,
|
||||
maybeOptions?: PrimaryGeneratedColumnNumericOptions|PrimaryGeneratedColumnUUIDOptions): Function {
|
||||
const options: ColumnOptions = {};
|
||||
let strategy: "increment"|"uuid";
|
||||
|
||||
if (strategyOrOptions) {
|
||||
if (typeof strategyOrOptions === "string")
|
||||
options.generationStrategy = strategyOrOptions as "increment"|"uuid";
|
||||
strategy = strategyOrOptions as "increment"|"uuid";
|
||||
|
||||
if (strategyOrOptions instanceof Object)
|
||||
if (strategyOrOptions instanceof Object) {
|
||||
strategy = "increment";
|
||||
Object.assign(options, strategyOrOptions);
|
||||
}
|
||||
} else {
|
||||
options.generationStrategy = "increment";
|
||||
strategy = "increment";
|
||||
}
|
||||
|
||||
if (maybeOptions instanceof Object)
|
||||
@ -51,7 +55,7 @@ export function PrimaryGeneratedColumn(strategyOrOptions?: "increment"|"uuid"|Pr
|
||||
|
||||
// check if there is no type in column options then set the int type - by default for auto generated column
|
||||
if (!options.type) {
|
||||
if (options.generationStrategy === "increment") {
|
||||
if (strategy === "increment") {
|
||||
Object.assign(options, { type: Number} as ColumnOptions);
|
||||
} else {
|
||||
Object.assign(options, { type: "uuid"} as ColumnOptions);
|
||||
@ -59,16 +63,23 @@ export function PrimaryGeneratedColumn(strategyOrOptions?: "increment"|"uuid"|Pr
|
||||
}
|
||||
|
||||
// implicitly set a primary and generated to column options
|
||||
Object.assign(options, { primary: true, generated: true } as ColumnOptions);
|
||||
Object.assign(options, { primary: true } as ColumnOptions);
|
||||
|
||||
// create and register a new column metadata
|
||||
const args: ColumnMetadataArgs = {
|
||||
const columnArgs: ColumnMetadataArgs = {
|
||||
target: object.constructor,
|
||||
propertyName: propertyName,
|
||||
mode: "regular",
|
||||
options: options
|
||||
};
|
||||
getMetadataArgsStorage().columns.push(args);
|
||||
getMetadataArgsStorage().columns.push(columnArgs);
|
||||
|
||||
const generationArgs: GeneratedMetadataArgs = {
|
||||
target: object.constructor,
|
||||
propertyName: propertyName,
|
||||
strategy: strategy
|
||||
};
|
||||
getMetadataArgsStorage().generations.push(generationArgs);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -82,21 +82,4 @@ export interface ColumnOptions {
|
||||
*/
|
||||
isArray?: boolean; // todo: rename to array?: boolean
|
||||
|
||||
/**
|
||||
* Specifies if this column will use auto increment (sequence, generated identity).
|
||||
* Note that only one column in entity can be marked as generated, and it must be a primary column.
|
||||
*
|
||||
* @deprecated - use @Generated instead
|
||||
* todo: remove
|
||||
*/
|
||||
generated?: boolean;
|
||||
|
||||
/**
|
||||
* Specifies generation strategy if this column will use auto increment.
|
||||
*
|
||||
* @deprecated - use @Generated instead
|
||||
* todo: remove
|
||||
*/
|
||||
generationStrategy?: "uuid"|"increment";
|
||||
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ import {JoinTableMetadataArgs} from "../metadata-args/JoinTableMetadataArgs";
|
||||
import {JoinTableOptions} from "../decorator/options/JoinTableOptions";
|
||||
import {JoinTableMultipleColumnsOptions} from "../decorator/options/JoinTableMuplipleColumnsOptions";
|
||||
import {ColumnMode} from "../metadata-args/types/ColumnMode";
|
||||
import {GeneratedMetadataArgs} from "../metadata-args/GeneratedMetadataArgs";
|
||||
|
||||
/**
|
||||
* Transforms entity schema into metadata args storage.
|
||||
@ -52,7 +53,7 @@ export class EntitySchemaTransformer {
|
||||
if (columnSchema.treeLevel)
|
||||
mode = "treeLevel";
|
||||
|
||||
const column: ColumnMetadataArgs = {
|
||||
const columnAgrs: ColumnMetadataArgs = {
|
||||
target: schema.target || schema.name,
|
||||
mode: mode,
|
||||
propertyName: columnName,
|
||||
@ -69,13 +70,16 @@ export class EntitySchemaTransformer {
|
||||
scale: columnSchema.scale
|
||||
}
|
||||
};
|
||||
metadataArgsStorage.columns.push(columnAgrs);
|
||||
|
||||
if (columnSchema.generated) {
|
||||
column.options.generated = true;
|
||||
column.options.generationStrategy = typeof columnSchema.generated === "string" ? columnSchema.generated : "increment";
|
||||
const generationArgs: GeneratedMetadataArgs = {
|
||||
target: schema.target || schema.name,
|
||||
propertyName: columnName,
|
||||
strategy: typeof columnSchema.generated === "string" ? columnSchema.generated : "increment"
|
||||
};
|
||||
metadataArgsStorage.generations.push(generationArgs);
|
||||
}
|
||||
|
||||
metadataArgsStorage.columns.push(column);
|
||||
});
|
||||
|
||||
// add relation metadata args from the schema
|
||||
|
||||
@ -217,15 +217,17 @@ export class EntityMetadataBuilder {
|
||||
|
||||
entityMetadatas.forEach(entityMetadata => {
|
||||
entityMetadata.columns.forEach(column => {
|
||||
const generated = this.metadataArgsStorage.findGenerated(entityMetadata.target, column.propertyName);
|
||||
const target = column.embeddedMetadata ? column.embeddedMetadata.type : entityMetadata.target;
|
||||
const generated = this.metadataArgsStorage.findGenerated(target, column.propertyName);
|
||||
if (generated) {
|
||||
column.isGenerated = true;
|
||||
column.generationStrategy = generated.strategy;
|
||||
column.type = generated.strategy === "increment" ? Number : "uuid"; // TODO do not override column type, because it can be tinyint, bigint, etc. Fix later
|
||||
column.build(this.connection);
|
||||
this.computeEntityMetadata(entityMetadata);
|
||||
}
|
||||
});
|
||||
|
||||
entityMetadata.generatedColumns = entityMetadata.columns.filter(column => column.isGenerated);
|
||||
});
|
||||
|
||||
return entityMetadatas;
|
||||
|
||||
@ -6,7 +6,6 @@ import {Driver} from "../driver/Driver";
|
||||
import {DataTypeNotSupportedError} from "../error/DataTypeNotSupportedError";
|
||||
import {ColumnType} from "../driver/types/ColumnTypes";
|
||||
import {MongoDriver} from "../driver/mongodb/MongoDriver";
|
||||
import {MysqlDriver} from "../driver/mysql/MysqlDriver";
|
||||
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver";
|
||||
|
||||
/// todo: add check if there are multiple tables with the same name
|
||||
@ -78,11 +77,11 @@ export class EntityMetadataValidator {
|
||||
});
|
||||
}
|
||||
|
||||
if (driver instanceof MysqlDriver) {
|
||||
/* if (driver instanceof MysqlDriver) {
|
||||
const generatedColumns = entityMetadata.columns.filter(column => column.isGenerated && column.generationStrategy !== "uuid");
|
||||
if (generatedColumns.length > 1)
|
||||
throw new Error(`Error in ${entityMetadata.name} entity. There can be only one auto-increment column in MySql table.`);
|
||||
}
|
||||
}*/
|
||||
|
||||
if (driver instanceof SqlServerDriver) {
|
||||
const carsetColumns = entityMetadata.columns.filter(column => column.charset);
|
||||
|
||||
@ -216,10 +216,6 @@ export class ColumnMetadata {
|
||||
this.collation = options.args.options.collation;
|
||||
if (options.args.options.primary)
|
||||
this.isPrimary = options.args.options.primary;
|
||||
if (options.args.options.generated)
|
||||
this.isGenerated = options.args.options.generated;
|
||||
if (options.args.options.generationStrategy)
|
||||
this.generationStrategy = options.args.options.generationStrategy;
|
||||
if (options.args.options.unique)
|
||||
this.isUnique = options.args.options.unique;
|
||||
if (options.args.options.default === null) // to make sure default: null is the same as nullable: true
|
||||
|
||||
@ -3,11 +3,13 @@ import {PrimaryColumn} from "../../../../../src/decorator/columns/PrimaryColumn"
|
||||
import {Column} from "../../../../../src/decorator/columns/Column";
|
||||
import {Post} from "./Post";
|
||||
import {OneToMany} from "../../../../../src/decorator/relations/OneToMany";
|
||||
import {Generated} from "../../../../../src/decorator/Generated";
|
||||
|
||||
@Entity()
|
||||
export class Category {
|
||||
|
||||
@PrimaryColumn("int", {generated: true, name: "theId"})
|
||||
@PrimaryColumn("int", {name: "theId"})
|
||||
@Generated()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -3,11 +3,13 @@ import {PrimaryColumn} from "../../../../../src/decorator/columns/PrimaryColumn"
|
||||
import {Column} from "../../../../../src/decorator/columns/Column";
|
||||
import {ManyToOne} from "../../../../../src/decorator/relations/ManyToOne";
|
||||
import {Category} from "./Category";
|
||||
import {Generated} from "../../../../../src/decorator/Generated";
|
||||
|
||||
@Entity()
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn("int", {generated: true, name: "theId"})
|
||||
@PrimaryColumn("int", {name: "theId"})
|
||||
@Generated()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -3,12 +3,14 @@ import {PrimaryColumn} from "../../../../../src/decorator/columns/PrimaryColumn"
|
||||
import {Column} from "../../../../../src/decorator/columns/Column";
|
||||
import {Post} from "./Post";
|
||||
import {OneToMany} from "../../../../../src/decorator/relations/OneToMany";
|
||||
import {Generated} from "../../../../../src/decorator/Generated";
|
||||
|
||||
|
||||
@Entity()
|
||||
export class Category {
|
||||
|
||||
@PrimaryColumn("int", {generated: true})
|
||||
@PrimaryColumn("int")
|
||||
@Generated()
|
||||
categoryId: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -12,7 +12,7 @@ import {Counters} from "./entity/Counters";
|
||||
import {User} from "./entity/User";
|
||||
import {Subcounters} from "./entity/Subcounters";
|
||||
|
||||
describe("query builder > relation-id > many-to-many > embedded", () => {
|
||||
describe("query builder > relation-id > one-to-many > embedded", () => {
|
||||
|
||||
let connections: Connection[];
|
||||
before(async () => connections = await createTestingConnections({
|
||||
|
||||
@ -4,11 +4,13 @@ import {OneToOne} from "../../../../src/decorator/relations/OneToOne";
|
||||
import {Column} from "../../../../src/decorator/columns/Column";
|
||||
import {PrimaryColumn} from "../../../../src/decorator/columns/PrimaryColumn";
|
||||
import {Entity} from "../../../../src/decorator/entity/Entity";
|
||||
import {Generated} from "../../../../src/decorator/Generated";
|
||||
|
||||
@Entity()
|
||||
export class User {
|
||||
|
||||
@PrimaryColumn("int", { generated: true })
|
||||
@PrimaryColumn("int")
|
||||
@Generated()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -2,11 +2,13 @@ import {PrimaryColumn} from "../../../../src/decorator/columns/PrimaryColumn";
|
||||
import {Entity} from "../../../../src/decorator/entity/Entity";
|
||||
import {OneToOne} from "../../../../src/decorator/relations/OneToOne";
|
||||
import {User} from "./User";
|
||||
import {Generated} from "../../../../src/decorator/Generated";
|
||||
|
||||
@Entity()
|
||||
export class AccessToken {
|
||||
|
||||
@PrimaryColumn("int", { generated: true })
|
||||
@PrimaryColumn("int")
|
||||
@Generated()
|
||||
primaryKey: number;
|
||||
|
||||
@OneToOne(type => User, user => user.access_token, {
|
||||
|
||||
@ -4,13 +4,13 @@ import {OneToOne} from "../../../../src/decorator/relations/OneToOne";
|
||||
import {Column} from "../../../../src/decorator/columns/Column";
|
||||
import {PrimaryColumn} from "../../../../src/decorator/columns/PrimaryColumn";
|
||||
import {Entity} from "../../../../src/decorator/entity/Entity";
|
||||
import {Generated} from "../../../../src/decorator/Generated";
|
||||
|
||||
@Entity()
|
||||
export class User {
|
||||
|
||||
@PrimaryColumn("int", {
|
||||
generated: true
|
||||
})
|
||||
@PrimaryColumn("int")
|
||||
@Generated()
|
||||
primaryKey: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -4,11 +4,13 @@ import {Column} from "../../../../src/decorator/columns/Column";
|
||||
import {ManyToOne} from "../../../../src/decorator/relations/ManyToOne";
|
||||
import {Kollektion} from "./Kollektion";
|
||||
import {JoinColumn} from "../../../../src/decorator/relations/JoinColumn";
|
||||
import {Generated} from "../../../../src/decorator/Generated";
|
||||
|
||||
@Entity("artikel")
|
||||
export class Artikel {
|
||||
|
||||
@PrimaryColumn("int", { generated: true, name: "artikel_id" })
|
||||
@PrimaryColumn("int", { name: "artikel_id" })
|
||||
@Generated()
|
||||
id: number;
|
||||
|
||||
@Column({ name: "artikel_nummer" })
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
import {Entity} from "../../../../src/decorator/entity/Entity";
|
||||
import {PrimaryColumn} from "../../../../src/decorator/columns/PrimaryColumn";
|
||||
import {Column} from "../../../../src/decorator/columns/Column";
|
||||
import {Generated} from "../../../../src/decorator/Generated";
|
||||
|
||||
@Entity("kollektion")
|
||||
export class Kollektion {
|
||||
|
||||
@PrimaryColumn("int", { generated: true, name: "kollektion_id" })
|
||||
@PrimaryColumn("int", { name: "kollektion_id" })
|
||||
@Generated()
|
||||
id: number;
|
||||
|
||||
@Column({ name: "kollektion_name" })
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user