-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(entity-generator): added support for generated columns (#5250)
- Loading branch information
Showing
6 changed files
with
592 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
tests/features/entity-generator/GeneratedColumns.mysql.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { MikroORM, Utils } from '@mikro-orm/mysql'; | ||
import { EntityGenerator } from '@mikro-orm/entity-generator'; | ||
|
||
let orm: MikroORM; | ||
|
||
const schemaName = 'generated_columns_example'; | ||
const schema = ` | ||
CREATE TABLE IF NOT EXISTS \`allowed_ages_at_creation\` | ||
( | ||
\`age\` TINYINT UNSIGNED NOT NULL, | ||
PRIMARY KEY (\`age\`) | ||
) | ||
ENGINE = InnoDB; | ||
CREATE TABLE IF NOT EXISTS \`users\` | ||
( | ||
\`id\` INT UNSIGNED NOT NULL AUTO_INCREMENT, | ||
\`first_name\` VARCHAR(100) NOT NULL, | ||
\`last_name\` VARCHAR(100) NOT NULL, | ||
\`full_name\` VARCHAR(200) GENERATED ALWAYS AS (CONCAT(\`first_name\`, ' ', \`last_name\`)) VIRTUAL NOT NULL, | ||
\`created_at\` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
\`date_of_birth\` DATE NOT NULL, | ||
\`age_at_creation\` TINYINT UNSIGNED GENERATED ALWAYS AS (TIMESTAMPDIFF(YEAR, \`date_of_birth\`, \`created_at\`)) STORED NULL, | ||
PRIMARY KEY (\`id\`), | ||
INDEX \`fk_users_allowed_ages_at_creation_idx\` (\`age_at_creation\` ASC) VISIBLE, | ||
CONSTRAINT \`fk_users_allowed_ages_at_creation\` | ||
FOREIGN KEY (\`age_at_creation\`) | ||
REFERENCES \`allowed_ages_at_creation\` (\`age\`) | ||
ON DELETE CASCADE | ||
ON UPDATE RESTRICT | ||
) | ||
ENGINE = InnoDB; | ||
`; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: schemaName, | ||
port: 3308, | ||
discovery: { warnWhenNoEntities: false }, | ||
extensions: [EntityGenerator], | ||
multipleStatements: true, | ||
ensureDatabase: false, | ||
}); | ||
|
||
if (await orm.schema.ensureDatabase({ create: true })) { | ||
await orm.schema.execute(schema); | ||
} | ||
|
||
await orm.close(true); | ||
}); | ||
|
||
beforeEach(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: schemaName, | ||
port: 3308, | ||
discovery: { warnWhenNoEntities: false }, | ||
extensions: [EntityGenerator], | ||
multipleStatements: true, | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
describe(schemaName, () => { | ||
describe.each([true, false])('entitySchema=%s', entitySchema => { | ||
beforeEach(() => { | ||
orm.config.get('entityGenerator').entitySchema = entitySchema; | ||
}); | ||
|
||
test('generates from db', async () => { | ||
const dump = await orm.entityGenerator.generate(); | ||
expect(dump).toMatchSnapshot('dump'); | ||
}); | ||
|
||
test('as functions from extensions', async () => { | ||
orm.config.get('entityGenerator').onInitialMetadata = metadata => { | ||
const usersMeta = metadata.find(meta => meta.className === 'Users')!; | ||
Object.entries(usersMeta.properties).forEach(([propName, propOptions]) => { | ||
if (typeof propOptions.generated === 'string') { | ||
propOptions.generated = Utils.createFunction(new Map(), `return () => ${JSON.stringify(propOptions.generated)}`); | ||
} | ||
}); | ||
}; | ||
const dump = await orm.entityGenerator.generate(); | ||
expect(dump).toMatchSnapshot('dump'); | ||
}); | ||
}); | ||
}); |
84 changes: 84 additions & 0 deletions
84
tests/features/entity-generator/GeneratedColumns.postgresql.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { MikroORM, Utils } from '@mikro-orm/postgresql'; | ||
import { EntityGenerator } from '@mikro-orm/entity-generator'; | ||
|
||
let orm: MikroORM; | ||
|
||
const schemaName = 'generated_columns_example'; | ||
const schema = ` | ||
CREATE TABLE IF NOT EXISTS "allowed_ages_at_creation" | ||
( | ||
"age" SMALLINT, | ||
PRIMARY KEY ("age") | ||
); | ||
CREATE TABLE IF NOT EXISTS "users" | ||
( | ||
"id" SERIAL PRIMARY KEY, | ||
"first_name" VARCHAR(100) NOT NULL, | ||
"last_name" VARCHAR(100) NOT NULL, | ||
"full_name" VARCHAR(200) GENERATED ALWAYS AS ("first_name" || ' ' || "last_name") STORED NOT NULL, | ||
"created_at" TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"date_of_birth" DATE NOT NULL, | ||
"age_at_creation" SMALLINT GENERATED ALWAYS AS (EXTRACT(YEAR FROM "created_at") - EXTRACT(YEAR FROM "date_of_birth")) STORED NULL, | ||
CONSTRAINT "fk_users_allowed_ages_at_creation" | ||
FOREIGN KEY ("age_at_creation") | ||
REFERENCES "allowed_ages_at_creation" ("age") | ||
ON DELETE CASCADE | ||
ON UPDATE RESTRICT | ||
); | ||
`; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: schemaName, | ||
discovery: { warnWhenNoEntities: false }, | ||
extensions: [EntityGenerator], | ||
multipleStatements: true, | ||
ensureDatabase: false, | ||
}); | ||
|
||
if (await orm.schema.ensureDatabase({ create: true })) { | ||
await orm.schema.execute(schema); | ||
} | ||
|
||
await orm.close(true); | ||
}); | ||
|
||
beforeEach(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: schemaName, | ||
discovery: { warnWhenNoEntities: false }, | ||
extensions: [EntityGenerator], | ||
multipleStatements: true, | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
describe(schemaName, () => { | ||
describe.each([true, false])('entitySchema=%s', entitySchema => { | ||
beforeEach(() => { | ||
orm.config.get('entityGenerator').entitySchema = entitySchema; | ||
}); | ||
|
||
test('generates from db', async () => { | ||
const dump = await orm.entityGenerator.generate(); | ||
expect(dump).toMatchSnapshot('dump'); | ||
}); | ||
|
||
test('as functions from extensions', async () => { | ||
orm.config.get('entityGenerator').onInitialMetadata = metadata => { | ||
const usersMeta = metadata.find(meta => meta.className === 'Users')!; | ||
Object.entries(usersMeta.properties).forEach(([propName, propOptions]) => { | ||
if (typeof propOptions.generated === 'string') { | ||
propOptions.generated = Utils.createFunction(new Map(), `return () => ${JSON.stringify(propOptions.generated)}`); | ||
} | ||
}); | ||
}; | ||
const dump = await orm.entityGenerator.generate(); | ||
expect(dump).toMatchSnapshot('dump'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.