@contember/client-content-generator 2.2.0-alpha.17 → 2.2.0-alpha.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/generate.cjs +2 -54
- package/dist/development/generate.cjs.map +1 -1
- package/dist/development/generate.js +2 -37
- package/dist/development/generate.js.map +1 -1
- package/dist/development/src/ContemberClientGenerator.cjs +4 -5
- package/dist/development/src/ContemberClientGenerator.cjs.map +1 -1
- package/dist/development/src/ContemberClientGenerator.js +4 -5
- package/dist/development/src/ContemberClientGenerator.js.map +1 -1
- package/dist/development/src/EntityTypeSchemaGenerator.cjs +40 -16
- package/dist/development/src/EntityTypeSchemaGenerator.cjs.map +1 -1
- package/dist/development/src/EntityTypeSchemaGenerator.js +41 -17
- package/dist/development/src/EntityTypeSchemaGenerator.js.map +1 -1
- package/dist/development/src/GenerateContentClient.cjs +72 -0
- package/dist/development/src/GenerateContentClient.cjs.map +1 -0
- package/dist/development/src/GenerateContentClient.js +55 -0
- package/dist/development/src/GenerateContentClient.js.map +1 -0
- package/dist/development/src/NameSchemaGenerator.cjs +12 -2
- package/dist/development/src/NameSchemaGenerator.cjs.map +1 -1
- package/dist/development/src/NameSchemaGenerator.js +12 -2
- package/dist/development/src/NameSchemaGenerator.js.map +1 -1
- package/dist/development/src/utils.cjs +21 -0
- package/dist/development/src/utils.cjs.map +1 -1
- package/dist/development/src/utils.js +22 -1
- package/dist/development/src/utils.js.map +1 -1
- package/dist/production/generate.cjs +2 -54
- package/dist/production/generate.cjs.map +1 -1
- package/dist/production/generate.js +2 -37
- package/dist/production/generate.js.map +1 -1
- package/dist/production/src/ContemberClientGenerator.cjs +4 -5
- package/dist/production/src/ContemberClientGenerator.cjs.map +1 -1
- package/dist/production/src/ContemberClientGenerator.js +4 -5
- package/dist/production/src/ContemberClientGenerator.js.map +1 -1
- package/dist/production/src/EntityTypeSchemaGenerator.cjs +40 -16
- package/dist/production/src/EntityTypeSchemaGenerator.cjs.map +1 -1
- package/dist/production/src/EntityTypeSchemaGenerator.js +41 -17
- package/dist/production/src/EntityTypeSchemaGenerator.js.map +1 -1
- package/dist/production/src/GenerateContentClient.cjs +72 -0
- package/dist/production/src/GenerateContentClient.cjs.map +1 -0
- package/dist/production/src/GenerateContentClient.js +55 -0
- package/dist/production/src/GenerateContentClient.js.map +1 -0
- package/dist/production/src/NameSchemaGenerator.cjs +12 -2
- package/dist/production/src/NameSchemaGenerator.cjs.map +1 -1
- package/dist/production/src/NameSchemaGenerator.js +12 -2
- package/dist/production/src/NameSchemaGenerator.js.map +1 -1
- package/dist/production/src/utils.cjs +21 -0
- package/dist/production/src/utils.cjs.map +1 -1
- package/dist/production/src/utils.js +22 -1
- package/dist/production/src/utils.js.map +1 -1
- package/dist/types/ContemberClientGenerator.d.ts +2 -1
- package/dist/types/ContemberClientGenerator.d.ts.map +1 -1
- package/dist/types/EntityTypeSchemaGenerator.d.ts +2 -1
- package/dist/types/EntityTypeSchemaGenerator.d.ts.map +1 -1
- package/dist/types/GenerateContentClient.d.ts +14 -0
- package/dist/types/GenerateContentClient.d.ts.map +1 -0
- package/dist/types/NameSchemaGenerator.d.ts +2 -1
- package/dist/types/NameSchemaGenerator.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/utils.d.ts +13 -0
- package/dist/types/utils.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/ContemberClientGenerator.ts +5 -5
- package/src/EntityTypeSchemaGenerator.ts +43 -16
- package/src/GenerateContentClient.ts +72 -0
- package/src/NameSchemaGenerator.ts +12 -2
- package/src/generate.ts +2 -41
- package/src/index.ts +1 -0
- package/src/tsconfig.json +1 -0
- package/src/utils.ts +29 -0
- package/tests/__snapshots__/generateEnittyTypes.test.ts.snap +559 -1
- package/tests/generateEnittyTypes.test.ts +40 -0
- package/tests/schemas.ts +93 -20
- package/tests/typecheckGeneratedClient.test.ts +53 -0
|
@@ -1,60 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
const
|
|
4
|
-
const path = require("node:path");
|
|
5
|
-
const ContemberClientGenerator = require("./src/ContemberClientGenerator.cjs");
|
|
6
|
-
function _interopNamespaceDefault(e) {
|
|
7
|
-
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
8
|
-
if (e) {
|
|
9
|
-
for (const k in e) {
|
|
10
|
-
if (k !== "default") {
|
|
11
|
-
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
12
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: () => e[k]
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
n.default = e;
|
|
20
|
-
return Object.freeze(n);
|
|
21
|
-
}
|
|
22
|
-
const fs__namespace = /* @__PURE__ */ _interopNamespaceDefault(fs);
|
|
3
|
+
const GenerateContentClient = require("./src/GenerateContentClient.cjs");
|
|
23
4
|
(async () => {
|
|
24
|
-
|
|
25
|
-
const outDir = process.argv[3];
|
|
26
|
-
if (!schemaPath || !outDir) {
|
|
27
|
-
console.error(`Usage:
|
|
28
|
-
|
|
29
|
-
From file:
|
|
30
|
-
yarn contember-client-generator <schema.json> <out-dir>
|
|
31
|
-
|
|
32
|
-
From stdin:
|
|
33
|
-
yarn run --silent contember project:print-schema --format=schema | yarn contember-client-generator - <out-dir>
|
|
34
|
-
`);
|
|
35
|
-
process.exit(1);
|
|
36
|
-
}
|
|
37
|
-
const sourceData = await (async () => {
|
|
38
|
-
if (schemaPath === "-") {
|
|
39
|
-
if (process.stdin.isTTY) {
|
|
40
|
-
throw new Error("Cannot read from stdin in TTY mode");
|
|
41
|
-
}
|
|
42
|
-
const buffer = [];
|
|
43
|
-
for await (const chunk of process.stdin) {
|
|
44
|
-
buffer.push(chunk);
|
|
45
|
-
}
|
|
46
|
-
return Buffer.concat(buffer).toString("utf8");
|
|
47
|
-
}
|
|
48
|
-
return await fs__namespace.readFile(path.resolve(process.cwd(), process.argv[2]), "utf8");
|
|
49
|
-
})();
|
|
50
|
-
const source = JSON.parse(sourceData);
|
|
51
|
-
const dir = path.resolve(process.cwd(), process.argv[3]);
|
|
52
|
-
const generator = new ContemberClientGenerator.ContemberClientGenerator();
|
|
53
|
-
const result = generator.generate(source.model);
|
|
54
|
-
await fs__namespace.mkdir(dir, { recursive: true });
|
|
55
|
-
for (const [name, content] of Object.entries(result)) {
|
|
56
|
-
await fs__namespace.writeFile(path.join(dir, name), content, "utf8");
|
|
57
|
-
}
|
|
5
|
+
await new GenerateContentClient.GenerateContentClient().run(process.argv.slice(2));
|
|
58
6
|
})().catch((e) => {
|
|
59
7
|
console.error(e);
|
|
60
8
|
process.exit(1);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate.cjs","sources":["../../../packages/client-content-generator/src/generate.ts"],"sourcesContent":["#!/usr/bin/env node\nimport
|
|
1
|
+
{"version":3,"file":"generate.cjs","sources":["../../../packages/client-content-generator/src/generate.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { GenerateContentClient } from './GenerateContentClient.js'\n;(async () => {\n\tawait new GenerateContentClient().run(process.argv.slice(2))\n})().catch(e => {\n\tconsole.error(e)\n\tprocess.exit(1)\n})\n"],"names":["GenerateContentClient"],"mappings":";;;CAEE,YAAY;AACb,QAAM,IAAIA,sBAAAA,wBAAwB,IAAI,QAAQ,KAAK,MAAM,CAAC,CAAC;AAC5D,KAAK,MAAM,CAAA,MAAK;AACf,UAAQ,MAAM,CAAC;AACf,UAAQ,KAAK,CAAC;AACf,CAAC;"}
|
|
@@ -1,42 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
import { resolve, join } from "node:path";
|
|
4
|
-
import { ContemberClientGenerator } from "./src/ContemberClientGenerator.js";
|
|
2
|
+
import { GenerateContentClient } from "./src/GenerateContentClient.js";
|
|
5
3
|
(async () => {
|
|
6
|
-
|
|
7
|
-
const outDir = process.argv[3];
|
|
8
|
-
if (!schemaPath || !outDir) {
|
|
9
|
-
console.error(`Usage:
|
|
10
|
-
|
|
11
|
-
From file:
|
|
12
|
-
yarn contember-client-generator <schema.json> <out-dir>
|
|
13
|
-
|
|
14
|
-
From stdin:
|
|
15
|
-
yarn run --silent contember project:print-schema --format=schema | yarn contember-client-generator - <out-dir>
|
|
16
|
-
`);
|
|
17
|
-
process.exit(1);
|
|
18
|
-
}
|
|
19
|
-
const sourceData = await (async () => {
|
|
20
|
-
if (schemaPath === "-") {
|
|
21
|
-
if (process.stdin.isTTY) {
|
|
22
|
-
throw new Error("Cannot read from stdin in TTY mode");
|
|
23
|
-
}
|
|
24
|
-
const buffer = [];
|
|
25
|
-
for await (const chunk of process.stdin) {
|
|
26
|
-
buffer.push(chunk);
|
|
27
|
-
}
|
|
28
|
-
return Buffer.concat(buffer).toString("utf8");
|
|
29
|
-
}
|
|
30
|
-
return await fs.readFile(resolve(process.cwd(), process.argv[2]), "utf8");
|
|
31
|
-
})();
|
|
32
|
-
const source = JSON.parse(sourceData);
|
|
33
|
-
const dir = resolve(process.cwd(), process.argv[3]);
|
|
34
|
-
const generator = new ContemberClientGenerator();
|
|
35
|
-
const result = generator.generate(source.model);
|
|
36
|
-
await fs.mkdir(dir, { recursive: true });
|
|
37
|
-
for (const [name, content] of Object.entries(result)) {
|
|
38
|
-
await fs.writeFile(join(dir, name), content, "utf8");
|
|
39
|
-
}
|
|
4
|
+
await new GenerateContentClient().run(process.argv.slice(2));
|
|
40
5
|
})().catch((e) => {
|
|
41
6
|
console.error(e);
|
|
42
7
|
process.exit(1);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate.js","sources":["../../../packages/client-content-generator/src/generate.ts"],"sourcesContent":["#!/usr/bin/env node\nimport
|
|
1
|
+
{"version":3,"file":"generate.js","sources":["../../../packages/client-content-generator/src/generate.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { GenerateContentClient } from './GenerateContentClient.js'\n;(async () => {\n\tawait new GenerateContentClient().run(process.argv.slice(2))\n})().catch(e => {\n\tconsole.error(e)\n\tprocess.exit(1)\n})\n"],"names":[],"mappings":";;CAEE,YAAY;AACb,QAAM,IAAI,wBAAwB,IAAI,QAAQ,KAAK,MAAM,CAAC,CAAC;AAC5D,KAAK,MAAM,CAAA,MAAK;AACf,UAAQ,MAAM,CAAC;AACf,UAAQ,KAAK,CAAC;AACf,CAAC;"}
|
|
@@ -9,10 +9,10 @@ class ContemberClientGenerator {
|
|
|
9
9
|
this.enumTypeSchemaGenerator = enumTypeSchemaGenerator;
|
|
10
10
|
this.entityTypeSchemaGenerator = entityTypeSchemaGenerator;
|
|
11
11
|
}
|
|
12
|
-
generate(model) {
|
|
13
|
-
const nameSchema = this.nameSchemaGenerator.generate(model);
|
|
12
|
+
generate(model, options) {
|
|
13
|
+
const nameSchema = this.nameSchemaGenerator.generate(model, options);
|
|
14
14
|
const enumTypeSchema = this.enumTypeSchemaGenerator.generate(model);
|
|
15
|
-
const entityTypeSchema = this.entityTypeSchemaGenerator.generate(model);
|
|
15
|
+
const entityTypeSchema = this.entityTypeSchemaGenerator.generate(model, options);
|
|
16
16
|
const namesCode = `import type { SchemaNames, SchemaEntityNames } from '@contember/client-content'
|
|
17
17
|
import type { ContemberClientEntities } from './entities'
|
|
18
18
|
import type { ContemberClientEnums } from './enums'
|
|
@@ -20,8 +20,7 @@ export const ContemberClientNames = {
|
|
|
20
20
|
entities: ${JSON.stringify(nameSchema.entities, null, " ").replaceAll("\n", "\n ")} satisfies {[K in keyof ContemberClientEntities]: SchemaEntityNames<K>},
|
|
21
21
|
enums: ${JSON.stringify(nameSchema.enums, null, " ").replaceAll("\n", "\n ")} satisfies {[K in keyof ContemberClientEnums]: readonly ContemberClientEnums[K][]},
|
|
22
22
|
} satisfies SchemaNames`;
|
|
23
|
-
const indexCode = `
|
|
24
|
-
import { ContemberClientNames } from './names'
|
|
23
|
+
const indexCode = `import { ContemberClientNames } from './names'
|
|
25
24
|
import type { ContemberClientSchema } from './entities'
|
|
26
25
|
import { ContentQueryBuilder, TypedContentQueryBuilder, TypedEntitySelection } from '@contember/client-content'
|
|
27
26
|
export * from './names'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContemberClientGenerator.cjs","sources":["../../../../packages/client-content-generator/src/ContemberClientGenerator.ts"],"sourcesContent":["import { Model } from '@contember/schema'\nimport { EnumTypeSchemaGenerator } from './EnumTypeSchemaGenerator.js'\nimport { EntityTypeSchemaGenerator } from './EntityTypeSchemaGenerator.js'\nimport { NameSchemaGenerator } from './NameSchemaGenerator.js'\n\nexport class ContemberClientGenerator {\n\tconstructor(\n\t\tprivate readonly nameSchemaGenerator: NameSchemaGenerator = new NameSchemaGenerator(),\n\t\tprivate readonly enumTypeSchemaGenerator: EnumTypeSchemaGenerator = new EnumTypeSchemaGenerator(),\n\t\tprivate readonly entityTypeSchemaGenerator: EntityTypeSchemaGenerator = new EntityTypeSchemaGenerator(),\n\t) {\n\t}\n\n\tgenerate(model: Model.Schema): Record<string, string> {\n\t\tconst nameSchema = this.nameSchemaGenerator.generate(model)\n\t\tconst enumTypeSchema = this.enumTypeSchemaGenerator.generate(model)\n\t\tconst entityTypeSchema = this.entityTypeSchemaGenerator.generate(model)\n\n\t\tconst namesCode = `import type { SchemaNames, SchemaEntityNames } from '@contember/client-content'\nimport type { ContemberClientEntities } from './entities'\nimport type { ContemberClientEnums } from './enums'\nexport const ContemberClientNames = {\n\tentities: ${\n\t\t\tJSON.stringify(nameSchema.entities, null, '\\t').replaceAll('\\n', '\\n\\t')\n\t\t} satisfies {[K in keyof ContemberClientEntities]: SchemaEntityNames<K>},\n\tenums: ${\n\t\t\tJSON.stringify(nameSchema.enums, null, '\\t').replaceAll('\\n', '\\n\\t')\n\t\t} satisfies {[K in keyof ContemberClientEnums]: readonly ContemberClientEnums[K][]},\n} satisfies SchemaNames`\n\n\t\tconst indexCode =
|
|
1
|
+
{"version":3,"file":"ContemberClientGenerator.cjs","sources":["../../../../packages/client-content-generator/src/ContemberClientGenerator.ts"],"sourcesContent":["import { Model } from '@contember/schema'\nimport { EnumTypeSchemaGenerator } from './EnumTypeSchemaGenerator.js'\nimport { EntityTypeSchemaGenerator } from './EntityTypeSchemaGenerator.js'\nimport { NameSchemaGenerator } from './NameSchemaGenerator.js'\nimport { GenerateOptions } from './utils.js'\n\nexport class ContemberClientGenerator {\n\tconstructor(\n\t\tprivate readonly nameSchemaGenerator: NameSchemaGenerator = new NameSchemaGenerator(),\n\t\tprivate readonly enumTypeSchemaGenerator: EnumTypeSchemaGenerator = new EnumTypeSchemaGenerator(),\n\t\tprivate readonly entityTypeSchemaGenerator: EntityTypeSchemaGenerator = new EntityTypeSchemaGenerator(),\n\t) {\n\t}\n\n\tgenerate(model: Model.Schema, options?: GenerateOptions): Record<string, string> {\n\t\tconst nameSchema = this.nameSchemaGenerator.generate(model, options)\n\t\tconst enumTypeSchema = this.enumTypeSchemaGenerator.generate(model)\n\t\tconst entityTypeSchema = this.entityTypeSchemaGenerator.generate(model, options)\n\n\t\tconst namesCode = `import type { SchemaNames, SchemaEntityNames } from '@contember/client-content'\nimport type { ContemberClientEntities } from './entities'\nimport type { ContemberClientEnums } from './enums'\nexport const ContemberClientNames = {\n\tentities: ${\n\t\t\tJSON.stringify(nameSchema.entities, null, '\\t').replaceAll('\\n', '\\n\\t')\n\t\t} satisfies {[K in keyof ContemberClientEntities]: SchemaEntityNames<K>},\n\tenums: ${\n\t\t\tJSON.stringify(nameSchema.enums, null, '\\t').replaceAll('\\n', '\\n\\t')\n\t\t} satisfies {[K in keyof ContemberClientEnums]: readonly ContemberClientEnums[K][]},\n} satisfies SchemaNames`\n\n\t\tconst indexCode = `import { ContemberClientNames } from './names'\nimport type { ContemberClientSchema } from './entities'\nimport { ContentQueryBuilder, TypedContentQueryBuilder, TypedEntitySelection } from '@contember/client-content'\nexport * from './names'\nexport * from './enums'\nexport * from './entities'\n\nexport const queryBuilder = new ContentQueryBuilder(ContemberClientNames) as unknown as TypedContentQueryBuilder<ContemberClientSchema>\n\nexport type FragmentOf<EntityName extends keyof ContemberClientSchema['entities'] & string, Data = unknown> =\nTypedEntitySelection<ContemberClientSchema, EntityName, ContemberClientSchema['entities'][EntityName], Data>\n\nexport type FragmentType<T extends TypedEntitySelection<any, any, any, any> = any> =\nT extends TypedEntitySelection<any, any, any, infer TFields>\n\t? TFields\n\t: never\n`\n\t\treturn {\n\t\t\t'names.ts': namesCode,\n\t\t\t'enums.ts': enumTypeSchema,\n\t\t\t'entities.ts': entityTypeSchema,\n\t\t\t'index.ts': indexCode,\n\t\t}\n\t}\n}\n"],"names":["NameSchemaGenerator","EnumTypeSchemaGenerator","EntityTypeSchemaGenerator"],"mappings":";;;;;AAMO,MAAM,yBAAyB;AAAA,EACrC,YACkB,sBAA2C,IAAIA,oBAAAA,uBAC/C,0BAAmD,IAAIC,wBAAAA,2BACvD,4BAAuD,IAAIC,0BAAAA,6BAC3E;AAHgB,SAAA,sBAAA;AACA,SAAA,0BAAA;AACA,SAAA,4BAAA;AAAA,EAElB;AAAA,EAEA,SAAS,OAAqB,SAAmD;AAChF,UAAM,aAAa,KAAK,oBAAoB,SAAS,OAAO,OAAO;AACnE,UAAM,iBAAiB,KAAK,wBAAwB,SAAS,KAAK;AAClE,UAAM,mBAAmB,KAAK,0BAA0B,SAAS,OAAO,OAAO;AAE/E,UAAM,YAAY;AAAA;AAAA;AAAA;AAAA,aAKjB,KAAK,UAAU,WAAW,UAAU,MAAM,GAAI,EAAE,WAAW,MAAM,KAAM,CACxE;AAAA,UAEC,KAAK,UAAU,WAAW,OAAO,MAAM,GAAI,EAAE,WAAW,MAAM,KAAM,CACrE;AAAA;AAGA,UAAM,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiBlB,WAAO;AAAA,MACN,YAAY;AAAA,MACZ,YAAY;AAAA,MACZ,eAAe;AAAA,MACf,YAAY;AAAA,IAAA;AAAA,EAEd;AACD;;"}
|
|
@@ -7,10 +7,10 @@ class ContemberClientGenerator {
|
|
|
7
7
|
this.enumTypeSchemaGenerator = enumTypeSchemaGenerator;
|
|
8
8
|
this.entityTypeSchemaGenerator = entityTypeSchemaGenerator;
|
|
9
9
|
}
|
|
10
|
-
generate(model) {
|
|
11
|
-
const nameSchema = this.nameSchemaGenerator.generate(model);
|
|
10
|
+
generate(model, options) {
|
|
11
|
+
const nameSchema = this.nameSchemaGenerator.generate(model, options);
|
|
12
12
|
const enumTypeSchema = this.enumTypeSchemaGenerator.generate(model);
|
|
13
|
-
const entityTypeSchema = this.entityTypeSchemaGenerator.generate(model);
|
|
13
|
+
const entityTypeSchema = this.entityTypeSchemaGenerator.generate(model, options);
|
|
14
14
|
const namesCode = `import type { SchemaNames, SchemaEntityNames } from '@contember/client-content'
|
|
15
15
|
import type { ContemberClientEntities } from './entities'
|
|
16
16
|
import type { ContemberClientEnums } from './enums'
|
|
@@ -18,8 +18,7 @@ export const ContemberClientNames = {
|
|
|
18
18
|
entities: ${JSON.stringify(nameSchema.entities, null, " ").replaceAll("\n", "\n ")} satisfies {[K in keyof ContemberClientEntities]: SchemaEntityNames<K>},
|
|
19
19
|
enums: ${JSON.stringify(nameSchema.enums, null, " ").replaceAll("\n", "\n ")} satisfies {[K in keyof ContemberClientEnums]: readonly ContemberClientEnums[K][]},
|
|
20
20
|
} satisfies SchemaNames`;
|
|
21
|
-
const indexCode = `
|
|
22
|
-
import { ContemberClientNames } from './names'
|
|
21
|
+
const indexCode = `import { ContemberClientNames } from './names'
|
|
23
22
|
import type { ContemberClientSchema } from './entities'
|
|
24
23
|
import { ContentQueryBuilder, TypedContentQueryBuilder, TypedEntitySelection } from '@contember/client-content'
|
|
25
24
|
export * from './names'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContemberClientGenerator.js","sources":["../../../../packages/client-content-generator/src/ContemberClientGenerator.ts"],"sourcesContent":["import { Model } from '@contember/schema'\nimport { EnumTypeSchemaGenerator } from './EnumTypeSchemaGenerator.js'\nimport { EntityTypeSchemaGenerator } from './EntityTypeSchemaGenerator.js'\nimport { NameSchemaGenerator } from './NameSchemaGenerator.js'\n\nexport class ContemberClientGenerator {\n\tconstructor(\n\t\tprivate readonly nameSchemaGenerator: NameSchemaGenerator = new NameSchemaGenerator(),\n\t\tprivate readonly enumTypeSchemaGenerator: EnumTypeSchemaGenerator = new EnumTypeSchemaGenerator(),\n\t\tprivate readonly entityTypeSchemaGenerator: EntityTypeSchemaGenerator = new EntityTypeSchemaGenerator(),\n\t) {\n\t}\n\n\tgenerate(model: Model.Schema): Record<string, string> {\n\t\tconst nameSchema = this.nameSchemaGenerator.generate(model)\n\t\tconst enumTypeSchema = this.enumTypeSchemaGenerator.generate(model)\n\t\tconst entityTypeSchema = this.entityTypeSchemaGenerator.generate(model)\n\n\t\tconst namesCode = `import type { SchemaNames, SchemaEntityNames } from '@contember/client-content'\nimport type { ContemberClientEntities } from './entities'\nimport type { ContemberClientEnums } from './enums'\nexport const ContemberClientNames = {\n\tentities: ${\n\t\t\tJSON.stringify(nameSchema.entities, null, '\\t').replaceAll('\\n', '\\n\\t')\n\t\t} satisfies {[K in keyof ContemberClientEntities]: SchemaEntityNames<K>},\n\tenums: ${\n\t\t\tJSON.stringify(nameSchema.enums, null, '\\t').replaceAll('\\n', '\\n\\t')\n\t\t} satisfies {[K in keyof ContemberClientEnums]: readonly ContemberClientEnums[K][]},\n} satisfies SchemaNames`\n\n\t\tconst indexCode =
|
|
1
|
+
{"version":3,"file":"ContemberClientGenerator.js","sources":["../../../../packages/client-content-generator/src/ContemberClientGenerator.ts"],"sourcesContent":["import { Model } from '@contember/schema'\nimport { EnumTypeSchemaGenerator } from './EnumTypeSchemaGenerator.js'\nimport { EntityTypeSchemaGenerator } from './EntityTypeSchemaGenerator.js'\nimport { NameSchemaGenerator } from './NameSchemaGenerator.js'\nimport { GenerateOptions } from './utils.js'\n\nexport class ContemberClientGenerator {\n\tconstructor(\n\t\tprivate readonly nameSchemaGenerator: NameSchemaGenerator = new NameSchemaGenerator(),\n\t\tprivate readonly enumTypeSchemaGenerator: EnumTypeSchemaGenerator = new EnumTypeSchemaGenerator(),\n\t\tprivate readonly entityTypeSchemaGenerator: EntityTypeSchemaGenerator = new EntityTypeSchemaGenerator(),\n\t) {\n\t}\n\n\tgenerate(model: Model.Schema, options?: GenerateOptions): Record<string, string> {\n\t\tconst nameSchema = this.nameSchemaGenerator.generate(model, options)\n\t\tconst enumTypeSchema = this.enumTypeSchemaGenerator.generate(model)\n\t\tconst entityTypeSchema = this.entityTypeSchemaGenerator.generate(model, options)\n\n\t\tconst namesCode = `import type { SchemaNames, SchemaEntityNames } from '@contember/client-content'\nimport type { ContemberClientEntities } from './entities'\nimport type { ContemberClientEnums } from './enums'\nexport const ContemberClientNames = {\n\tentities: ${\n\t\t\tJSON.stringify(nameSchema.entities, null, '\\t').replaceAll('\\n', '\\n\\t')\n\t\t} satisfies {[K in keyof ContemberClientEntities]: SchemaEntityNames<K>},\n\tenums: ${\n\t\t\tJSON.stringify(nameSchema.enums, null, '\\t').replaceAll('\\n', '\\n\\t')\n\t\t} satisfies {[K in keyof ContemberClientEnums]: readonly ContemberClientEnums[K][]},\n} satisfies SchemaNames`\n\n\t\tconst indexCode = `import { ContemberClientNames } from './names'\nimport type { ContemberClientSchema } from './entities'\nimport { ContentQueryBuilder, TypedContentQueryBuilder, TypedEntitySelection } from '@contember/client-content'\nexport * from './names'\nexport * from './enums'\nexport * from './entities'\n\nexport const queryBuilder = new ContentQueryBuilder(ContemberClientNames) as unknown as TypedContentQueryBuilder<ContemberClientSchema>\n\nexport type FragmentOf<EntityName extends keyof ContemberClientSchema['entities'] & string, Data = unknown> =\nTypedEntitySelection<ContemberClientSchema, EntityName, ContemberClientSchema['entities'][EntityName], Data>\n\nexport type FragmentType<T extends TypedEntitySelection<any, any, any, any> = any> =\nT extends TypedEntitySelection<any, any, any, infer TFields>\n\t? TFields\n\t: never\n`\n\t\treturn {\n\t\t\t'names.ts': namesCode,\n\t\t\t'enums.ts': enumTypeSchema,\n\t\t\t'entities.ts': entityTypeSchema,\n\t\t\t'index.ts': indexCode,\n\t\t}\n\t}\n}\n"],"names":[],"mappings":";;;AAMO,MAAM,yBAAyB;AAAA,EACrC,YACkB,sBAA2C,IAAI,uBAC/C,0BAAmD,IAAI,2BACvD,4BAAuD,IAAI,6BAC3E;AAHgB,SAAA,sBAAA;AACA,SAAA,0BAAA;AACA,SAAA,4BAAA;AAAA,EAElB;AAAA,EAEA,SAAS,OAAqB,SAAmD;AAChF,UAAM,aAAa,KAAK,oBAAoB,SAAS,OAAO,OAAO;AACnE,UAAM,iBAAiB,KAAK,wBAAwB,SAAS,KAAK;AAClE,UAAM,mBAAmB,KAAK,0BAA0B,SAAS,OAAO,OAAO;AAE/E,UAAM,YAAY;AAAA;AAAA;AAAA;AAAA,aAKjB,KAAK,UAAU,WAAW,UAAU,MAAM,GAAI,EAAE,WAAW,MAAM,KAAM,CACxE;AAAA,UAEC,KAAK,UAAU,WAAW,OAAO,MAAM,GAAI,EAAE,WAAW,MAAM,KAAM,CACrE;AAAA;AAGA,UAAM,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiBlB,WAAO;AAAA,MACN,YAAY;AAAA,MACZ,YAAY;AAAA,MACZ,eAAe;AAAA,MACf,YAAY;AAAA,IAAA;AAAA,EAEd;AACD;"}
|
|
@@ -4,7 +4,7 @@ const schema = require("@contember/schema");
|
|
|
4
4
|
const schemaUtils = require("@contember/schema-utils");
|
|
5
5
|
const utils = require("./utils.cjs");
|
|
6
6
|
class EntityTypeSchemaGenerator {
|
|
7
|
-
generate(model) {
|
|
7
|
+
generate(model, options) {
|
|
8
8
|
let code = "";
|
|
9
9
|
for (const enumName of Object.keys(model.enums)) {
|
|
10
10
|
code += `import type { ${utils.getEnumTypeName(enumName)} } from './enums'
|
|
@@ -17,13 +17,15 @@ export type JSONObject = { readonly [K in string]?: JSONValue }
|
|
|
17
17
|
export type JSONArray = readonly JSONValue[]
|
|
18
18
|
|
|
19
19
|
`;
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
const entities = Object.values(model.entities).filter((entity) => !utils.isEntityExcluded(entity, options));
|
|
21
|
+
for (const entity of entities) {
|
|
22
|
+
code += this.generateTypeEntityCode(model, entity, options);
|
|
22
23
|
}
|
|
23
24
|
code += "\n";
|
|
24
25
|
code += `export type ContemberClientEntities = {
|
|
25
26
|
`;
|
|
26
|
-
for (const entity of
|
|
27
|
+
for (const entity of entities) {
|
|
28
|
+
code += utils.formatDocComment(" ", { deprecationReason: entity.deprecationReason });
|
|
27
29
|
code += ` ${entity.name}: ${entity.name}
|
|
28
30
|
`;
|
|
29
31
|
}
|
|
@@ -34,25 +36,38 @@ export type JSONArray = readonly JSONValue[]
|
|
|
34
36
|
code += "}\n";
|
|
35
37
|
return code;
|
|
36
38
|
}
|
|
37
|
-
generateTypeEntityCode(model, entity) {
|
|
38
|
-
let code =
|
|
39
|
+
generateTypeEntityCode(model, entity, options) {
|
|
40
|
+
let code = utils.formatDocComment("", entity);
|
|
41
|
+
code += `export type ${entity.name} <OverRelation extends string | never = never> = {
|
|
39
42
|
`;
|
|
40
43
|
code += " name: '" + entity.name + "'\n";
|
|
41
44
|
code += " unique:\n";
|
|
42
|
-
code += this.formatUniqueFields(model, entity);
|
|
45
|
+
code += this.formatUniqueFields(model, entity, options);
|
|
43
46
|
let columnsCode = "";
|
|
44
47
|
let hasOneCode = "";
|
|
45
48
|
let hasManyCode = "";
|
|
46
49
|
schemaUtils.acceptEveryFieldVisitor(model, entity, {
|
|
47
50
|
visitHasMany: (ctx) => {
|
|
51
|
+
if (utils.isRelationExcluded(ctx.relation, ctx.targetEntity, options)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
hasManyCode += utils.formatDocComment(" ", ctx.relation);
|
|
48
55
|
hasManyCode += ` ${ctx.relation.name}: ${ctx.targetEntity.name}${ctx.targetRelation?.type === schema.Model.RelationType.ManyHasOne ? `<'${ctx.targetRelation.name}'>` : ""}
|
|
49
56
|
`;
|
|
50
57
|
},
|
|
51
58
|
visitHasOne: (ctx) => {
|
|
59
|
+
if (utils.isRelationExcluded(ctx.relation, ctx.targetEntity, options)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
hasOneCode += utils.formatDocComment(" ", ctx.relation);
|
|
52
63
|
hasOneCode += ` ${ctx.relation.name}: ${ctx.targetEntity.name}
|
|
53
64
|
`;
|
|
54
65
|
},
|
|
55
66
|
visitColumn: (ctx) => {
|
|
67
|
+
if (utils.isColumnExcluded(ctx.column, options)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
columnsCode += utils.formatDocComment(" ", ctx.column);
|
|
56
71
|
columnsCode += ` ${ctx.column.name}: ${columnToTsType(ctx.column)}${ctx.column.nullable ? ` | null` : ""}
|
|
57
72
|
`;
|
|
58
73
|
}
|
|
@@ -67,19 +82,19 @@ export type JSONArray = readonly JSONValue[]
|
|
|
67
82
|
code += hasManyCode;
|
|
68
83
|
code += " }\n";
|
|
69
84
|
code += " hasManyBy: {\n";
|
|
70
|
-
code += this.formatReducedFields(model, entity);
|
|
85
|
+
code += this.formatReducedFields(model, entity, options);
|
|
71
86
|
code += " }\n";
|
|
72
87
|
code += "}\n";
|
|
73
88
|
return code;
|
|
74
89
|
}
|
|
75
|
-
formatReducedFields(model, entity) {
|
|
90
|
+
formatReducedFields(model, entity, options) {
|
|
76
91
|
let code = "";
|
|
77
92
|
schemaUtils.acceptEveryFieldVisitor(model, entity, {
|
|
78
93
|
visitOneHasMany: ({ entity: entity2, relation, targetEntity, targetRelation }) => {
|
|
79
|
-
if (!targetRelation) {
|
|
94
|
+
if (!targetRelation || utils.isRelationExcluded(relation, targetEntity, options) || utils.isRelationExcluded(targetRelation, entity2, options)) {
|
|
80
95
|
return;
|
|
81
96
|
}
|
|
82
|
-
const uniqueConstraints = getFieldsForUniqueWhere(model, targetEntity);
|
|
97
|
+
const uniqueConstraints = getFieldsForUniqueWhere(model, targetEntity, options);
|
|
83
98
|
const composedUnique = uniqueConstraints.filter((fields) => fields.length === 2).filter((fields) => fields.includes(targetRelation.name)).map((fields) => fields.filter((it) => it !== targetRelation.name)).map((fields) => fields[0]);
|
|
84
99
|
const singleUnique = uniqueConstraints.filter((fields) => fields.length === 1 && fields[0] !== targetEntity.primary).map((fields) => fields[0]).filter((it) => it !== targetRelation.name);
|
|
85
100
|
[...composedUnique, ...singleUnique].forEach((fieldName) => {
|
|
@@ -88,6 +103,7 @@ export type JSONArray = readonly JSONValue[]
|
|
|
88
103
|
};
|
|
89
104
|
const name = `${relation.name}By${capitalizeFirstLetter(fieldName)}`;
|
|
90
105
|
const targetUnique = targetEntity.fields[fieldName];
|
|
106
|
+
code += utils.formatDocComment(" ", { deprecationReason: relation.deprecationReason });
|
|
91
107
|
code += ` ${name}: { entity: ${targetEntity.name}; by: {${fieldName}: ${uniqueType(model, targetEntity, targetUnique)}} }
|
|
92
108
|
`;
|
|
93
109
|
});
|
|
@@ -107,12 +123,12 @@ export type JSONArray = readonly JSONValue[]
|
|
|
107
123
|
});
|
|
108
124
|
return code;
|
|
109
125
|
}
|
|
110
|
-
formatUniqueFields(model, entity) {
|
|
111
|
-
const fields = getFieldsForUniqueWhere(model, entity);
|
|
126
|
+
formatUniqueFields(model, entity, options) {
|
|
127
|
+
const fields = getFieldsForUniqueWhere(model, entity, options);
|
|
112
128
|
let code = "";
|
|
113
129
|
for (const field of fields) {
|
|
114
130
|
code += " | Omit<{ ";
|
|
115
|
-
code += field.map((it) => `${it}: ${uniqueType(model, entity, entity.fields[it])}`).join("
|
|
131
|
+
code += field.map((it) => `${it}: ${uniqueType(model, entity, entity.fields[it])}`).join("; ");
|
|
116
132
|
code += "}, OverRelation>\n";
|
|
117
133
|
}
|
|
118
134
|
return code;
|
|
@@ -161,7 +177,15 @@ const columnToTsType = (column) => {
|
|
|
161
177
|
})();
|
|
162
178
|
return column.list ? `readonly ${baseType}[]` : baseType;
|
|
163
179
|
};
|
|
164
|
-
const getFieldsForUniqueWhere = (schema2, entity) => {
|
|
180
|
+
const getFieldsForUniqueWhere = (schema2, entity, options) => {
|
|
181
|
+
const excludedFields = new Set(
|
|
182
|
+
Object.values(entity.fields).filter(
|
|
183
|
+
(field) => schemaUtils.acceptFieldVisitor(schema2, entity, field, {
|
|
184
|
+
visitColumn: ({ column }) => utils.isColumnExcluded(column, options),
|
|
185
|
+
visitRelation: ({ relation, targetEntity }) => utils.isRelationExcluded(relation, targetEntity, options)
|
|
186
|
+
})
|
|
187
|
+
).map((field) => field.name)
|
|
188
|
+
);
|
|
165
189
|
const relations = Object.values(
|
|
166
190
|
schemaUtils.acceptEveryFieldVisitor(schema2, entity, {
|
|
167
191
|
visitColumn: () => void 0,
|
|
@@ -173,7 +197,7 @@ const getFieldsForUniqueWhere = (schema2, entity) => {
|
|
|
173
197
|
visitOneHasOneOwning: ({ relation }) => [relation.name]
|
|
174
198
|
})
|
|
175
199
|
).filter((it) => !!it);
|
|
176
|
-
return [[entity.primary], ...Object.values(entity.unique).map((it) => it.fields), ...relations];
|
|
200
|
+
return [[entity.primary], ...Object.values(entity.unique).map((it) => it.fields), ...relations].filter((fields) => fields.every((field) => !excludedFields.has(field)));
|
|
177
201
|
};
|
|
178
202
|
exports.EntityTypeSchemaGenerator = EntityTypeSchemaGenerator;
|
|
179
203
|
//# sourceMappingURL=EntityTypeSchemaGenerator.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EntityTypeSchemaGenerator.cjs","sources":["../../../../packages/client-content-generator/src/EntityTypeSchemaGenerator.ts"],"sourcesContent":["import { Model } from '@contember/schema'\nimport { acceptEveryFieldVisitor, acceptFieldVisitor, jsonSchemaToTsType } from '@contember/schema-utils'\n\nimport { getEnumTypeName } from './utils.js'\n\nexport class EntityTypeSchemaGenerator {\n\tgenerate(model: Model.Schema): string {\n\t\tlet code = ''\n\t\tfor (const enumName of Object.keys(model.enums)) {\n\t\t\tcode += `import type { ${getEnumTypeName(enumName)} } from './enums'\\n`\n\t\t}\n\n\t\tcode += `\nexport type JSONPrimitive = string | number | boolean | null\nexport type JSONValue = JSONPrimitive | JSONObject | JSONArray\nexport type JSONObject = { readonly [K in string]?: JSONValue }\nexport type JSONArray = readonly JSONValue[]\n\n`\n\n\t\tfor (const entity of Object.values(model.entities)) {\n\t\t\tcode += this.generateTypeEntityCode(model, entity)\n\t\t}\n\t\tcode += '\\n'\n\t\tcode += `export type ContemberClientEntities = {\\n`\n\t\tfor (const entity of Object.values(model.entities)) {\n\t\t\tcode += `\\t${entity.name}: ${entity.name}\\n`\n\t\t}\n\t\tcode += '}\\n\\n'\n\t\tcode += `export type ContemberClientSchema = {\\n`\n\t\tcode += '\\tentities: ContemberClientEntities\\n'\n\t\tcode += '}\\n'\n\t\treturn code\n\t}\n\n\tprivate generateTypeEntityCode(model: Model.Schema, entity: Model.Entity): string {\n\t\tlet code = `export type ${entity.name} <OverRelation extends string | never = never> = {\\n`\n\t\tcode += \"\\tname: '\" + entity.name + \"'\\n\"\n\t\tcode += '\\tunique:\\n'\n\t\tcode += this.formatUniqueFields(model, entity)\n\t\tlet columnsCode = ''\n\t\tlet hasOneCode = ''\n\t\tlet hasManyCode = ''\n\t\tacceptEveryFieldVisitor(model, entity, {\n\t\t\tvisitHasMany: ctx => {\n\t\t\t\thasManyCode += `\\t\\t${ctx.relation.name}: ${ctx.targetEntity.name}${\n\t\t\t\t\tctx.targetRelation?.type === Model.RelationType.ManyHasOne ? `<'${ctx.targetRelation.name}'>` : ''\n\t\t\t\t}\\n`\n\t\t\t},\n\t\t\tvisitHasOne: ctx => {\n\t\t\t\thasOneCode += `\\t\\t${ctx.relation.name}: ${ctx.targetEntity.name}\\n`\n\t\t\t},\n\t\t\tvisitColumn: ctx => {\n\t\t\t\tcolumnsCode += `\\t\\t${ctx.column.name}: ${columnToTsType(ctx.column)}${ctx.column.nullable ? ` | null` : ''}\\n`\n\t\t\t},\n\t\t})\n\n\t\tcode += '\\tcolumns: {\\n'\n\t\tcode += columnsCode\n\t\tcode += '\\t}\\n'\n\t\tcode += '\\thasOne: {\\n'\n\t\tcode += hasOneCode\n\t\tcode += '\\t}\\n'\n\t\tcode += '\\thasMany: {\\n'\n\t\tcode += hasManyCode\n\t\tcode += '\\t}\\n'\n\t\tcode += '\\thasManyBy: {\\n'\n\t\tcode += this.formatReducedFields(model, entity)\n\t\tcode += '\\t}\\n'\n\t\tcode += '}\\n'\n\t\treturn code\n\t}\n\n\tprivate formatReducedFields(model: Model.Schema, entity: Model.Entity): string {\n\t\tlet code = ''\n\t\tacceptEveryFieldVisitor(model, entity, {\n\t\t\tvisitOneHasMany: ({ entity, relation, targetEntity, targetRelation }) => {\n\t\t\t\tif (!targetRelation) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tconst uniqueConstraints = getFieldsForUniqueWhere(model, targetEntity)\n\t\t\t\tconst composedUnique = uniqueConstraints\n\t\t\t\t\t.filter(fields => fields.length === 2) // todo support all uniques\n\t\t\t\t\t.filter(fields => fields.includes(targetRelation.name))\n\t\t\t\t\t.map(fields => fields.filter(it => it !== targetRelation.name))\n\t\t\t\t\t.map(fields => fields[0])\n\t\t\t\tconst singleUnique = uniqueConstraints\n\t\t\t\t\t.filter(fields => fields.length === 1 && fields[0] !== targetEntity.primary)\n\t\t\t\t\t.map(fields => fields[0])\n\t\t\t\t\t.filter(it => it !== targetRelation.name)\n\t\t\t\t;[...composedUnique, ...singleUnique].forEach(fieldName => {\n\t\t\t\t\tconst capitalizeFirstLetter = (value: string) => {\n\t\t\t\t\t\treturn value.charAt(0).toUpperCase() + value.slice(1)\n\t\t\t\t\t}\n\t\t\t\t\tconst name = `${relation.name}By${capitalizeFirstLetter(fieldName)}`\n\n\t\t\t\t\tconst targetUnique = targetEntity.fields[fieldName]\n\n\t\t\t\t\tcode += `\\t\\t${name}: { entity: ${targetEntity.name}; by: {${fieldName}: ${uniqueType(model, targetEntity, targetUnique)}} }\\n`\n\t\t\t\t})\n\t\t\t},\n\t\t\tvisitColumn: () => {\n\t\t\t},\n\t\t\tvisitManyHasManyInverse: () => {\n\t\t\t},\n\t\t\tvisitManyHasManyOwning: () => {\n\t\t\t},\n\t\t\tvisitManyHasOne: () => {\n\t\t\t},\n\t\t\tvisitOneHasOneInverse: () => {\n\t\t\t},\n\t\t\tvisitOneHasOneOwning: () => {\n\t\t\t},\n\t\t})\n\t\treturn code\n\t}\n\n\tprivate formatUniqueFields(model: Model.Schema, entity: Model.Entity): string {\n\t\tconst fields = getFieldsForUniqueWhere(model, entity)\n\t\tlet code = ''\n\t\tfor (const field of fields) {\n\t\t\tcode += '\\t\\t| Omit<{ '\n\t\t\tcode += field.map(it => `${it}: ${uniqueType(model, entity, entity.fields[it])}`).join(', ')\n\t\t\tcode += '}, OverRelation>\\n'\n\t\t}\n\t\treturn code\n\t}\n}\n\nconst uniqueType = (model: Model.Schema, entity: Model.Entity, field: Model.AnyField): string => {\n\treturn acceptFieldVisitor(model, entity, field, {\n\t\tvisitColumn: ctx => {\n\t\t\treturn columnToTsType(ctx.column)\n\t\t},\n\t\tvisitRelation: ctx => {\n\t\t\treturn ctx.targetEntity.name + `['unique']`\n\t\t},\n\t})\n}\n\nconst columnToTsType = (column: Model.AnyColumn): string => {\n\tconst baseType = (() => {\n\t\tswitch (column.type) {\n\t\t\tcase Model.ColumnType.Enum:\n\t\t\t\treturn getEnumTypeName(column.columnType)\n\t\t\tcase Model.ColumnType.String:\n\t\t\t\treturn 'string'\n\t\t\tcase Model.ColumnType.Int:\n\t\t\t\treturn 'number'\n\t\t\tcase Model.ColumnType.Double:\n\t\t\t\treturn 'number'\n\t\t\tcase Model.ColumnType.Numeric:\n\t\t\t\treturn 'string'\n\t\t\tcase Model.ColumnType.Bool:\n\t\t\t\treturn 'boolean'\n\t\t\tcase Model.ColumnType.DateTime:\n\t\t\t\treturn 'string'\n\t\t\tcase Model.ColumnType.Time:\n\t\t\t\treturn 'string'\n\t\t\tcase Model.ColumnType.Date:\n\t\t\t\treturn 'string'\n\t\t\tcase Model.ColumnType.Json:\n\t\t\t\t// A jsonColumn with an attached JSON Schema gets a derived TypeScript type; without a\n\t\t\t\t// schema (or for any construct the schema cannot faithfully express) it stays `JSONValue`.\n\t\t\t\treturn column.schema !== undefined ? jsonSchemaToTsType(column.schema) : 'JSONValue'\n\t\t\tcase Model.ColumnType.Uuid:\n\t\t\t\treturn 'string'\n\t\t\tdefault:\n\t\t\t\t;((_: never) => {\n\t\t\t\t\tthrow new Error(`Unknown type ${_}`)\n\t\t\t\t})(column.type)\n\t\t}\n\t})()\n\treturn column.list ? `readonly ${baseType}[]` : baseType\n}\n\nconst getFieldsForUniqueWhere = (schema: Model.Schema, entity: Model.Entity): readonly (readonly string[])[] => {\n\tconst relations = Object.values(\n\t\tacceptEveryFieldVisitor<undefined | [string]>(schema, entity, {\n\t\t\tvisitColumn: () => undefined,\n\t\t\tvisitManyHasManyInverse: () => undefined,\n\t\t\tvisitManyHasManyOwning: () => undefined,\n\t\t\tvisitOneHasMany: ({ relation }) => [relation.name],\n\t\t\tvisitManyHasOne: () => undefined,\n\t\t\tvisitOneHasOneInverse: ({ relation }) => [relation.name],\n\t\t\tvisitOneHasOneOwning: ({ relation }) => [relation.name],\n\t\t}),\n\t).filter((it): it is [string] => !!it)\n\n\treturn [[entity.primary], ...Object.values(entity.unique).map(it => it.fields), ...relations]\n}\n"],"names":["getEnumTypeName","acceptEveryFieldVisitor","Model","entity","acceptFieldVisitor","jsonSchemaToTsType","schema"],"mappings":";;;;;AAKO,MAAM,0BAA0B;AAAA,EACtC,SAAS,OAA6B;AACrC,QAAI,OAAO;AACX,eAAW,YAAY,OAAO,KAAK,MAAM,KAAK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AAAA,MAAM,WAAW;AACrB,eAAO;AAAA,MACR,KAAKA,OAAAA,MAAM,WAAW;AACrB,eAAO;AAAA,MACR,KAAKA,OAAAA,MAAM,WAAW;AACrB,eAAO;AAAA,MACR,KAAKA,OAAAA,MAAM,WAAW;AAGrB,eAAO,OAAO,WAAW,SAAYG,YAAAA,mBAAmB,OAAO,MAAM,IAAI;AAAA,MAC1E,KAAKH,OAAAA,MAAM,WAAW;AACrB,eAAO;AAAA,MACR;AACE,SAAC,CAAC,MAAa;AACf,gBAAM,IAAI,MAAM,gBAAgB,CAAC,EAAE;AAAA,QACpC,GAAG,OAAO,IAAI;AAAA,IAAA;AAAA,EAEjB,GAAA;AACA,SAAO,OAAO,OAAO,YAAY,QAAQ,OAAO;AACjD;AAEA,MAAM,0BAA0B,CAACI,SAAsB,WAAyD;AAC/G,QAAM,YAAY,OAAO;AAAA,IACxBL,YAAAA,wBAA8CK,SAAQ,QAAQ;AAAA,MAC7D,aAAa,MAAM;AAAA,MACnB,yBAAyB,MAAM;AAAA,MAC/B,wBAAwB,MAAM;AAAA,MAC9B,iBAAiB,CAAC,EAAE,eAAe,CAAC,SAAS,IAAI;AAAA,MACjD,iBAAiB,MAAM;AAAA,MACvB,uBAAuB,CAAC,EAAE,eAAe,CAAC,SAAS,IAAI;AAAA,MACvD,sBAAsB,CAAC,EAAE,eAAe,CAAC,SAAS,IAAI;AAAA,IAAA,CACtD;AAAA,EAAA,EACA,OAAO,CAAC,OAAuB,CAAC,CAAC,EAAE;AAErC,SAAO,CAAC,CAAC,OAAO,OAAO,GAAG,GAAG,OAAO,OAAO,OAAO,MAAM,EAAE,IAAI,CAAA,OAAM,GAAG,MAAM,GAAG,GAAG,SAAS;AAC7F;;"}
|
|
1
|
+
{"version":3,"file":"EntityTypeSchemaGenerator.cjs","sources":["../../../../packages/client-content-generator/src/EntityTypeSchemaGenerator.ts"],"sourcesContent":["import { Model } from '@contember/schema'\nimport { acceptEveryFieldVisitor, acceptFieldVisitor, jsonSchemaToTsType } from '@contember/schema-utils'\n\nimport { formatDocComment, GenerateOptions, getEnumTypeName, isColumnExcluded, isEntityExcluded, isRelationExcluded } from './utils.js'\n\nexport class EntityTypeSchemaGenerator {\n\tgenerate(model: Model.Schema, options?: GenerateOptions): string {\n\t\tlet code = ''\n\t\tfor (const enumName of Object.keys(model.enums)) {\n\t\t\tcode += `import type { ${getEnumTypeName(enumName)} } from './enums'\\n`\n\t\t}\n\n\t\tcode += `\nexport type JSONPrimitive = string | number | boolean | null\nexport type JSONValue = JSONPrimitive | JSONObject | JSONArray\nexport type JSONObject = { readonly [K in string]?: JSONValue }\nexport type JSONArray = readonly JSONValue[]\n\n`\n\n\t\tconst entities = Object.values(model.entities).filter(entity => !isEntityExcluded(entity, options))\n\t\tfor (const entity of entities) {\n\t\t\tcode += this.generateTypeEntityCode(model, entity, options)\n\t\t}\n\t\tcode += '\\n'\n\t\tcode += `export type ContemberClientEntities = {\\n`\n\t\tfor (const entity of entities) {\n\t\t\tcode += formatDocComment('\\t', { deprecationReason: entity.deprecationReason })\n\t\t\tcode += `\\t${entity.name}: ${entity.name}\\n`\n\t\t}\n\t\tcode += '}\\n\\n'\n\t\tcode += `export type ContemberClientSchema = {\\n`\n\t\tcode += '\\tentities: ContemberClientEntities\\n'\n\t\tcode += '}\\n'\n\t\treturn code\n\t}\n\n\tprivate generateTypeEntityCode(model: Model.Schema, entity: Model.Entity, options?: GenerateOptions): string {\n\t\tlet code = formatDocComment('', entity)\n\t\tcode += `export type ${entity.name} <OverRelation extends string | never = never> = {\\n`\n\t\tcode += \"\\tname: '\" + entity.name + \"'\\n\"\n\t\tcode += '\\tunique:\\n'\n\t\tcode += this.formatUniqueFields(model, entity, options)\n\t\tlet columnsCode = ''\n\t\tlet hasOneCode = ''\n\t\tlet hasManyCode = ''\n\t\tacceptEveryFieldVisitor(model, entity, {\n\t\t\tvisitHasMany: ctx => {\n\t\t\t\tif (isRelationExcluded(ctx.relation, ctx.targetEntity, options)) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\thasManyCode += formatDocComment('\\t\\t', ctx.relation)\n\t\t\t\thasManyCode += `\\t\\t${ctx.relation.name}: ${ctx.targetEntity.name}${\n\t\t\t\t\tctx.targetRelation?.type === Model.RelationType.ManyHasOne ? `<'${ctx.targetRelation.name}'>` : ''\n\t\t\t\t}\\n`\n\t\t\t},\n\t\t\tvisitHasOne: ctx => {\n\t\t\t\tif (isRelationExcluded(ctx.relation, ctx.targetEntity, options)) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\thasOneCode += formatDocComment('\\t\\t', ctx.relation)\n\t\t\t\thasOneCode += `\\t\\t${ctx.relation.name}: ${ctx.targetEntity.name}\\n`\n\t\t\t},\n\t\t\tvisitColumn: ctx => {\n\t\t\t\tif (isColumnExcluded(ctx.column, options)) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tcolumnsCode += formatDocComment('\\t\\t', ctx.column)\n\t\t\t\tcolumnsCode += `\\t\\t${ctx.column.name}: ${columnToTsType(ctx.column)}${ctx.column.nullable ? ` | null` : ''}\\n`\n\t\t\t},\n\t\t})\n\n\t\tcode += '\\tcolumns: {\\n'\n\t\tcode += columnsCode\n\t\tcode += '\\t}\\n'\n\t\tcode += '\\thasOne: {\\n'\n\t\tcode += hasOneCode\n\t\tcode += '\\t}\\n'\n\t\tcode += '\\thasMany: {\\n'\n\t\tcode += hasManyCode\n\t\tcode += '\\t}\\n'\n\t\tcode += '\\thasManyBy: {\\n'\n\t\tcode += this.formatReducedFields(model, entity, options)\n\t\tcode += '\\t}\\n'\n\t\tcode += '}\\n'\n\t\treturn code\n\t}\n\n\tprivate formatReducedFields(model: Model.Schema, entity: Model.Entity, options?: GenerateOptions): string {\n\t\tlet code = ''\n\t\tacceptEveryFieldVisitor(model, entity, {\n\t\t\tvisitOneHasMany: ({ entity, relation, targetEntity, targetRelation }) => {\n\t\t\t\tif (!targetRelation || isRelationExcluded(relation, targetEntity, options) || isRelationExcluded(targetRelation, entity, options)) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\tconst uniqueConstraints = getFieldsForUniqueWhere(model, targetEntity, options)\n\t\t\t\tconst composedUnique = uniqueConstraints\n\t\t\t\t\t.filter(fields => fields.length === 2) // todo support all uniques\n\t\t\t\t\t.filter(fields => fields.includes(targetRelation.name))\n\t\t\t\t\t.map(fields => fields.filter(it => it !== targetRelation.name))\n\t\t\t\t\t.map(fields => fields[0])\n\t\t\t\tconst singleUnique = uniqueConstraints\n\t\t\t\t\t.filter(fields => fields.length === 1 && fields[0] !== targetEntity.primary)\n\t\t\t\t\t.map(fields => fields[0])\n\t\t\t\t\t.filter(it => it !== targetRelation.name)\n\t\t\t\t;[...composedUnique, ...singleUnique].forEach(fieldName => {\n\t\t\t\t\tconst capitalizeFirstLetter = (value: string) => {\n\t\t\t\t\t\treturn value.charAt(0).toUpperCase() + value.slice(1)\n\t\t\t\t\t}\n\t\t\t\t\tconst name = `${relation.name}By${capitalizeFirstLetter(fieldName)}`\n\n\t\t\t\t\tconst targetUnique = targetEntity.fields[fieldName]\n\n\t\t\t\t\tcode += formatDocComment('\\t\\t', { deprecationReason: relation.deprecationReason })\n\t\t\t\t\tcode += `\\t\\t${name}: { entity: ${targetEntity.name}; by: {${fieldName}: ${uniqueType(model, targetEntity, targetUnique)}} }\\n`\n\t\t\t\t})\n\t\t\t},\n\t\t\tvisitColumn: () => {\n\t\t\t},\n\t\t\tvisitManyHasManyInverse: () => {\n\t\t\t},\n\t\t\tvisitManyHasManyOwning: () => {\n\t\t\t},\n\t\t\tvisitManyHasOne: () => {\n\t\t\t},\n\t\t\tvisitOneHasOneInverse: () => {\n\t\t\t},\n\t\t\tvisitOneHasOneOwning: () => {\n\t\t\t},\n\t\t})\n\t\treturn code\n\t}\n\n\tprivate formatUniqueFields(model: Model.Schema, entity: Model.Entity, options?: GenerateOptions): string {\n\t\tconst fields = getFieldsForUniqueWhere(model, entity, options)\n\t\tlet code = ''\n\t\tfor (const field of fields) {\n\t\t\tcode += '\\t\\t| Omit<{ '\n\t\t\tcode += field.map(it => `${it}: ${uniqueType(model, entity, entity.fields[it])}`).join('; ')\n\t\t\tcode += '}, OverRelation>\\n'\n\t\t}\n\t\treturn code\n\t}\n}\n\nconst uniqueType = (model: Model.Schema, entity: Model.Entity, field: Model.AnyField): string => {\n\treturn acceptFieldVisitor(model, entity, field, {\n\t\tvisitColumn: ctx => {\n\t\t\treturn columnToTsType(ctx.column)\n\t\t},\n\t\tvisitRelation: ctx => {\n\t\t\treturn ctx.targetEntity.name + `['unique']`\n\t\t},\n\t})\n}\n\nconst columnToTsType = (column: Model.AnyColumn): string => {\n\tconst baseType = (() => {\n\t\tswitch (column.type) {\n\t\t\tcase Model.ColumnType.Enum:\n\t\t\t\treturn getEnumTypeName(column.columnType)\n\t\t\tcase Model.ColumnType.String:\n\t\t\t\treturn 'string'\n\t\t\tcase Model.ColumnType.Int:\n\t\t\t\treturn 'number'\n\t\t\tcase Model.ColumnType.Double:\n\t\t\t\treturn 'number'\n\t\t\tcase Model.ColumnType.Numeric:\n\t\t\t\treturn 'string'\n\t\t\tcase Model.ColumnType.Bool:\n\t\t\t\treturn 'boolean'\n\t\t\tcase Model.ColumnType.DateTime:\n\t\t\t\treturn 'string'\n\t\t\tcase Model.ColumnType.Time:\n\t\t\t\treturn 'string'\n\t\t\tcase Model.ColumnType.Date:\n\t\t\t\treturn 'string'\n\t\t\tcase Model.ColumnType.Json:\n\t\t\t\t// A jsonColumn with an attached JSON Schema gets a derived TypeScript type; without a\n\t\t\t\t// schema (or for any construct the schema cannot faithfully express) it stays `JSONValue`.\n\t\t\t\treturn column.schema !== undefined ? jsonSchemaToTsType(column.schema) : 'JSONValue'\n\t\t\tcase Model.ColumnType.Uuid:\n\t\t\t\treturn 'string'\n\t\t\tdefault:\n\t\t\t\t;((_: never) => {\n\t\t\t\t\tthrow new Error(`Unknown type ${_}`)\n\t\t\t\t})(column.type)\n\t\t}\n\t})()\n\treturn column.list ? `readonly ${baseType}[]` : baseType\n}\n\nconst getFieldsForUniqueWhere = (schema: Model.Schema, entity: Model.Entity, options?: GenerateOptions): readonly (readonly string[])[] => {\n\tconst excludedFields = new Set(\n\t\tObject.values(entity.fields)\n\t\t\t.filter(field =>\n\t\t\t\tacceptFieldVisitor(schema, entity, field, {\n\t\t\t\t\tvisitColumn: ({ column }) => isColumnExcluded(column, options),\n\t\t\t\t\tvisitRelation: ({ relation, targetEntity }) => isRelationExcluded(relation, targetEntity, options),\n\t\t\t\t})\n\t\t\t)\n\t\t\t.map(field => field.name),\n\t)\n\tconst relations = Object.values(\n\t\tacceptEveryFieldVisitor<undefined | [string]>(schema, entity, {\n\t\t\tvisitColumn: () => undefined,\n\t\t\tvisitManyHasManyInverse: () => undefined,\n\t\t\tvisitManyHasManyOwning: () => undefined,\n\t\t\tvisitOneHasMany: ({ relation }) => [relation.name],\n\t\t\tvisitManyHasOne: () => undefined,\n\t\t\tvisitOneHasOneInverse: ({ relation }) => [relation.name],\n\t\t\tvisitOneHasOneOwning: ({ relation }) => [relation.name],\n\t\t}),\n\t).filter((it): it is [string] => !!it)\n\n\treturn [[entity.primary], ...Object.values(entity.unique).map(it => it.fields), ...relations]\n\t\t.filter(fields => fields.every(field => !excludedFields.has(field)))\n}\n"],"names":["getEnumTypeName","isEntityExcluded","formatDocComment","acceptEveryFieldVisitor","isRelationExcluded","Model","isColumnExcluded","entity","acceptFieldVisitor","jsonSchemaToTsType","schema"],"mappings":";;;;;AAKO,MAAM,0BAA0B;AAAA,EACtC,SAAS,OAAqB,SAAmC;AAChE,QAAI,OAAO;AACX,eAAW,YAAY,OAAO,KAAK,MAAM,KAAK,GAAG;AAChD,cAAQ,iBAAiBA,MAAAA,gBAAgB,QAAQ,CAAC;AAAA;AAAA,IACnD;AAEA,YAAQ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAQR,UAAM,WAAW,OAAO,OAAO,MAAM,QAAQ,EAAE,OAAO,CAAA,WAAU,CAACC,MAA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iBAAiB,QAAQ,OAAO;AAAA,QAC7D,eAAe,CAAC,EAAE,UAAU,mBAAmBF,yBAAmB,UAAU,cAAc,OAAO;AAAA,MAAA,CACjG;AAAA,IAAA,EAED,IAAI,CAAA,UAAS,MAAM,IAAI;AAAA,EAAA;AAE1B,QAAM,YAAY,OAAO;AAAA,IACxBD,YAAAA,wBAA8CO,SAAQ,QAAQ;AAAA,MAC7D,aAAa,MAAM;AAAA,MACnB,yBAAyB,MAAM;AAAA,MAC/B,wBAAwB,MAAM;AAAA,MAC9B,iBAAiB,CAAC,EAAE,eAAe,CAAC,SAAS,IAAI;AAAA,MACjD,iBAAiB,MAAM;AAAA,MACvB,uBAAuB,CAAC,EAAE,eAAe,CAAC,SAAS,IAAI;AAAA,MACvD,sBAAsB,CAAC,EAAE,eAAe,CAAC,SAAS,IAAI;AAAA,IAAA,CACtD;AAAA,EAAA,EACA,OAAO,CAAC,OAAuB,CAAC,CAAC,EAAE;AAErC,SAAO,CAAC,CAAC,OAAO,OAAO,GAAG,GAAG,OAAO,OAAO,OAAO,MAAM,EAAE,IAAI,CAAA,OAAM,GAAG,MAAM,GAAG,GAAG,SAAS,EAC1F,OAAO,CAAA,WAAU,OAAO,MAAM,CAAA,UAAS,CAAC,eAAe,IAAI,KAAK,CAAC,CAAC;AACrE;;"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Model } from "@contember/schema";
|
|
2
2
|
import { acceptEveryFieldVisitor, jsonSchemaToTsType, acceptFieldVisitor } from "@contember/schema-utils";
|
|
3
|
-
import { getEnumTypeName } from "./utils.js";
|
|
3
|
+
import { getEnumTypeName, isEntityExcluded, formatDocComment, isColumnExcluded, isRelationExcluded } from "./utils.js";
|
|
4
4
|
class EntityTypeSchemaGenerator {
|
|
5
|
-
generate(model) {
|
|
5
|
+
generate(model, options) {
|
|
6
6
|
let code = "";
|
|
7
7
|
for (const enumName of Object.keys(model.enums)) {
|
|
8
8
|
code += `import type { ${getEnumTypeName(enumName)} } from './enums'
|
|
@@ -15,13 +15,15 @@ export type JSONObject = { readonly [K in string]?: JSONValue }
|
|
|
15
15
|
export type JSONArray = readonly JSONValue[]
|
|
16
16
|
|
|
17
17
|
`;
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const entities = Object.values(model.entities).filter((entity) => !isEntityExcluded(entity, options));
|
|
19
|
+
for (const entity of entities) {
|
|
20
|
+
code += this.generateTypeEntityCode(model, entity, options);
|
|
20
21
|
}
|
|
21
22
|
code += "\n";
|
|
22
23
|
code += `export type ContemberClientEntities = {
|
|
23
24
|
`;
|
|
24
|
-
for (const entity of
|
|
25
|
+
for (const entity of entities) {
|
|
26
|
+
code += formatDocComment(" ", { deprecationReason: entity.deprecationReason });
|
|
25
27
|
code += ` ${entity.name}: ${entity.name}
|
|
26
28
|
`;
|
|
27
29
|
}
|
|
@@ -32,25 +34,38 @@ export type JSONArray = readonly JSONValue[]
|
|
|
32
34
|
code += "}\n";
|
|
33
35
|
return code;
|
|
34
36
|
}
|
|
35
|
-
generateTypeEntityCode(model, entity) {
|
|
36
|
-
let code =
|
|
37
|
+
generateTypeEntityCode(model, entity, options) {
|
|
38
|
+
let code = formatDocComment("", entity);
|
|
39
|
+
code += `export type ${entity.name} <OverRelation extends string | never = never> = {
|
|
37
40
|
`;
|
|
38
41
|
code += " name: '" + entity.name + "'\n";
|
|
39
42
|
code += " unique:\n";
|
|
40
|
-
code += this.formatUniqueFields(model, entity);
|
|
43
|
+
code += this.formatUniqueFields(model, entity, options);
|
|
41
44
|
let columnsCode = "";
|
|
42
45
|
let hasOneCode = "";
|
|
43
46
|
let hasManyCode = "";
|
|
44
47
|
acceptEveryFieldVisitor(model, entity, {
|
|
45
48
|
visitHasMany: (ctx) => {
|
|
49
|
+
if (isRelationExcluded(ctx.relation, ctx.targetEntity, options)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
hasManyCode += formatDocComment(" ", ctx.relation);
|
|
46
53
|
hasManyCode += ` ${ctx.relation.name}: ${ctx.targetEntity.name}${ctx.targetRelation?.type === Model.RelationType.ManyHasOne ? `<'${ctx.targetRelation.name}'>` : ""}
|
|
47
54
|
`;
|
|
48
55
|
},
|
|
49
56
|
visitHasOne: (ctx) => {
|
|
57
|
+
if (isRelationExcluded(ctx.relation, ctx.targetEntity, options)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
hasOneCode += formatDocComment(" ", ctx.relation);
|
|
50
61
|
hasOneCode += ` ${ctx.relation.name}: ${ctx.targetEntity.name}
|
|
51
62
|
`;
|
|
52
63
|
},
|
|
53
64
|
visitColumn: (ctx) => {
|
|
65
|
+
if (isColumnExcluded(ctx.column, options)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
columnsCode += formatDocComment(" ", ctx.column);
|
|
54
69
|
columnsCode += ` ${ctx.column.name}: ${columnToTsType(ctx.column)}${ctx.column.nullable ? ` | null` : ""}
|
|
55
70
|
`;
|
|
56
71
|
}
|
|
@@ -65,19 +80,19 @@ export type JSONArray = readonly JSONValue[]
|
|
|
65
80
|
code += hasManyCode;
|
|
66
81
|
code += " }\n";
|
|
67
82
|
code += " hasManyBy: {\n";
|
|
68
|
-
code += this.formatReducedFields(model, entity);
|
|
83
|
+
code += this.formatReducedFields(model, entity, options);
|
|
69
84
|
code += " }\n";
|
|
70
85
|
code += "}\n";
|
|
71
86
|
return code;
|
|
72
87
|
}
|
|
73
|
-
formatReducedFields(model, entity) {
|
|
88
|
+
formatReducedFields(model, entity, options) {
|
|
74
89
|
let code = "";
|
|
75
90
|
acceptEveryFieldVisitor(model, entity, {
|
|
76
91
|
visitOneHasMany: ({ entity: entity2, relation, targetEntity, targetRelation }) => {
|
|
77
|
-
if (!targetRelation) {
|
|
92
|
+
if (!targetRelation || isRelationExcluded(relation, targetEntity, options) || isRelationExcluded(targetRelation, entity2, options)) {
|
|
78
93
|
return;
|
|
79
94
|
}
|
|
80
|
-
const uniqueConstraints = getFieldsForUniqueWhere(model, targetEntity);
|
|
95
|
+
const uniqueConstraints = getFieldsForUniqueWhere(model, targetEntity, options);
|
|
81
96
|
const composedUnique = uniqueConstraints.filter((fields) => fields.length === 2).filter((fields) => fields.includes(targetRelation.name)).map((fields) => fields.filter((it) => it !== targetRelation.name)).map((fields) => fields[0]);
|
|
82
97
|
const singleUnique = uniqueConstraints.filter((fields) => fields.length === 1 && fields[0] !== targetEntity.primary).map((fields) => fields[0]).filter((it) => it !== targetRelation.name);
|
|
83
98
|
[...composedUnique, ...singleUnique].forEach((fieldName) => {
|
|
@@ -86,6 +101,7 @@ export type JSONArray = readonly JSONValue[]
|
|
|
86
101
|
};
|
|
87
102
|
const name = `${relation.name}By${capitalizeFirstLetter(fieldName)}`;
|
|
88
103
|
const targetUnique = targetEntity.fields[fieldName];
|
|
104
|
+
code += formatDocComment(" ", { deprecationReason: relation.deprecationReason });
|
|
89
105
|
code += ` ${name}: { entity: ${targetEntity.name}; by: {${fieldName}: ${uniqueType(model, targetEntity, targetUnique)}} }
|
|
90
106
|
`;
|
|
91
107
|
});
|
|
@@ -105,12 +121,12 @@ export type JSONArray = readonly JSONValue[]
|
|
|
105
121
|
});
|
|
106
122
|
return code;
|
|
107
123
|
}
|
|
108
|
-
formatUniqueFields(model, entity) {
|
|
109
|
-
const fields = getFieldsForUniqueWhere(model, entity);
|
|
124
|
+
formatUniqueFields(model, entity, options) {
|
|
125
|
+
const fields = getFieldsForUniqueWhere(model, entity, options);
|
|
110
126
|
let code = "";
|
|
111
127
|
for (const field of fields) {
|
|
112
128
|
code += " | Omit<{ ";
|
|
113
|
-
code += field.map((it) => `${it}: ${uniqueType(model, entity, entity.fields[it])}`).join("
|
|
129
|
+
code += field.map((it) => `${it}: ${uniqueType(model, entity, entity.fields[it])}`).join("; ");
|
|
114
130
|
code += "}, OverRelation>\n";
|
|
115
131
|
}
|
|
116
132
|
return code;
|
|
@@ -159,7 +175,15 @@ const columnToTsType = (column) => {
|
|
|
159
175
|
})();
|
|
160
176
|
return column.list ? `readonly ${baseType}[]` : baseType;
|
|
161
177
|
};
|
|
162
|
-
const getFieldsForUniqueWhere = (schema, entity) => {
|
|
178
|
+
const getFieldsForUniqueWhere = (schema, entity, options) => {
|
|
179
|
+
const excludedFields = new Set(
|
|
180
|
+
Object.values(entity.fields).filter(
|
|
181
|
+
(field) => acceptFieldVisitor(schema, entity, field, {
|
|
182
|
+
visitColumn: ({ column }) => isColumnExcluded(column, options),
|
|
183
|
+
visitRelation: ({ relation, targetEntity }) => isRelationExcluded(relation, targetEntity, options)
|
|
184
|
+
})
|
|
185
|
+
).map((field) => field.name)
|
|
186
|
+
);
|
|
163
187
|
const relations = Object.values(
|
|
164
188
|
acceptEveryFieldVisitor(schema, entity, {
|
|
165
189
|
visitColumn: () => void 0,
|
|
@@ -171,7 +195,7 @@ const getFieldsForUniqueWhere = (schema, entity) => {
|
|
|
171
195
|
visitOneHasOneOwning: ({ relation }) => [relation.name]
|
|
172
196
|
})
|
|
173
197
|
).filter((it) => !!it);
|
|
174
|
-
return [[entity.primary], ...Object.values(entity.unique).map((it) => it.fields), ...relations];
|
|
198
|
+
return [[entity.primary], ...Object.values(entity.unique).map((it) => it.fields), ...relations].filter((fields) => fields.every((field) => !excludedFields.has(field)));
|
|
175
199
|
};
|
|
176
200
|
export {
|
|
177
201
|
EntityTypeSchemaGenerator
|