@contember/schema-utils 1.3.0-alpha.7 → 1.3.0-beta.1

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.
Files changed (77) hide show
  1. package/dist/src/SchemaDatabaseMetadata.d.ts +40 -0
  2. package/dist/src/SchemaDatabaseMetadata.d.ts.map +1 -0
  3. package/dist/src/SchemaDatabaseMetadata.js +20 -0
  4. package/dist/src/SchemaDatabaseMetadata.js.map +1 -0
  5. package/dist/src/definition-generator/DefinitionCodeGenerator.d.ts.map +1 -1
  6. package/dist/src/definition-generator/DefinitionCodeGenerator.js +4 -12
  7. package/dist/src/definition-generator/DefinitionCodeGenerator.js.map +1 -1
  8. package/dist/src/index.d.ts +2 -1
  9. package/dist/src/index.d.ts.map +1 -1
  10. package/dist/src/index.js +3 -1
  11. package/dist/src/index.js.map +1 -1
  12. package/dist/src/lax/LaxSchemaConverter.js +2 -2
  13. package/dist/src/lax/schema.d.ts +6 -6
  14. package/dist/src/lax/schema.d.ts.map +1 -1
  15. package/dist/src/model/index.d.ts +0 -1
  16. package/dist/src/model/index.d.ts.map +1 -1
  17. package/dist/src/model/index.js +0 -1
  18. package/dist/src/model/index.js.map +1 -1
  19. package/dist/src/model/modelUtils.js +1 -1
  20. package/dist/src/model/modelUtils.js.map +1 -1
  21. package/dist/src/schemaNormalizer.d.ts.map +1 -1
  22. package/dist/src/schemaNormalizer.js +9 -3
  23. package/dist/src/schemaNormalizer.js.map +1 -1
  24. package/dist/src/tsconfig.tsbuildinfo +1 -1
  25. package/dist/src/type-schema/acl.js +2 -2
  26. package/dist/src/type-schema/acl.js.map +1 -1
  27. package/dist/src/type-schema/model.d.ts +4 -6
  28. package/dist/src/type-schema/model.d.ts.map +1 -1
  29. package/dist/src/type-schema/model.js +5 -6
  30. package/dist/src/type-schema/model.js.map +1 -1
  31. package/dist/src/utils/deepCompare.d.ts +9 -3
  32. package/dist/src/utils/deepCompare.d.ts.map +1 -1
  33. package/dist/src/utils/deepCompare.js +32 -4
  34. package/dist/src/utils/deepCompare.js.map +1 -1
  35. package/dist/src/utils/printJsValue.d.ts +2 -2
  36. package/dist/src/utils/printJsValue.d.ts.map +1 -1
  37. package/dist/src/validation/MembershipResolver.js +1 -1
  38. package/dist/src/validation/MembershipResolver.js.map +1 -1
  39. package/dist/src/validation/ModelValidator.d.ts.map +1 -1
  40. package/dist/src/validation/ModelValidator.js +2 -28
  41. package/dist/src/validation/ModelValidator.js.map +1 -1
  42. package/dist/src/validation/SchemaValidator.d.ts +1 -0
  43. package/dist/src/validation/SchemaValidator.d.ts.map +1 -1
  44. package/dist/src/validation/SchemaValidator.js.map +1 -1
  45. package/dist/src/validation/errors.d.ts +1 -1
  46. package/dist/src/validation/errors.d.ts.map +1 -1
  47. package/dist/tests/cases/unit/deepCompare.test.d.ts +2 -0
  48. package/dist/tests/cases/unit/deepCompare.test.d.ts.map +1 -0
  49. package/dist/tests/cases/unit/deepCompare.test.js +43 -0
  50. package/dist/tests/cases/unit/deepCompare.test.js.map +1 -0
  51. package/dist/tests/cases/unit/modelValidator.test.js +6 -72
  52. package/dist/tests/cases/unit/modelValidator.test.js.map +1 -1
  53. package/dist/tests/cases/unit/schemas/acl.js +4 -6
  54. package/dist/tests/cases/unit/schemas/acl.js.map +1 -1
  55. package/dist/tests/cases/unit/schemas/unique.js +2 -3
  56. package/dist/tests/cases/unit/schemas/unique.js.map +1 -1
  57. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  58. package/package.json +6 -8
  59. package/src/SchemaDatabaseMetadata.ts +66 -0
  60. package/src/definition-generator/DefinitionCodeGenerator.ts +5 -14
  61. package/src/index.ts +2 -1
  62. package/src/lax/LaxSchemaConverter.ts +2 -2
  63. package/src/model/index.ts +0 -1
  64. package/src/schemaNormalizer.ts +9 -2
  65. package/src/type-schema/acl.ts +2 -2
  66. package/src/type-schema/model.ts +20 -8
  67. package/src/utils/deepCompare.ts +37 -4
  68. package/src/validation/ModelValidator.ts +2 -26
  69. package/src/validation/SchemaValidator.ts +1 -0
  70. package/tests/cases/unit/__snapshots__/laxSchemaConverter.test.ts.snap +14 -14
  71. package/tests/cases/unit/deepCompare.test.ts +44 -0
  72. package/tests/cases/unit/modelValidator.test.ts +6 -73
  73. package/dist/src/model/NamingHelper.d.ts +0 -9
  74. package/dist/src/model/NamingHelper.d.ts.map +0 -1
  75. package/dist/src/model/NamingHelper.js +0 -44
  76. package/dist/src/model/NamingHelper.js.map +0 -1
  77. package/src/model/NamingHelper.ts +0 -41
@@ -148,10 +148,22 @@ const viewSchemaCheck: Typesafe.Equals<Model.View, ReturnType<typeof viewSchemaI
148
148
  const viewSchema: Typesafe.Type<Model.View> = viewSchemaInner
149
149
 
150
150
 
151
- const indexesSchema = Typesafe.coalesce<Model.Indexes, Model.Indexes>(Typesafe.record(Typesafe.string, Typesafe.object({
152
- fields: Typesafe.array(Typesafe.string),
153
- name: Typesafe.string,
154
- })), { })
151
+ const indexLike: Typesafe.Type<{readonly fields: readonly string[]; readonly name?: string | undefined}> = Typesafe.intersection(
152
+ Typesafe.object({
153
+ fields: Typesafe.array(Typesafe.string),
154
+ }),
155
+ Typesafe.partial({
156
+ name: Typesafe.string,
157
+ }),
158
+ )
159
+
160
+ const indexesSchema = Typesafe.coalesce<Model.Indexes, Model.Indexes>(
161
+ Typesafe.preprocess(
162
+ Typesafe.array(indexLike),
163
+ it => it?.constructor === Object ? Object.values(it) : it,
164
+ ),
165
+ [],
166
+ )
155
167
 
156
168
  const entitySchema = Typesafe.intersection(
157
169
  Typesafe.object({
@@ -160,10 +172,10 @@ const entitySchema = Typesafe.intersection(
160
172
  primaryColumn: Typesafe.string,
161
173
  tableName: Typesafe.string,
162
174
  fields: Typesafe.record(Typesafe.string, fieldSchema),
163
- unique: Typesafe.record(Typesafe.string, Typesafe.object({
164
- fields: Typesafe.array(Typesafe.string),
165
- name: Typesafe.string,
166
- })),
175
+ unique: Typesafe.preprocess(
176
+ Typesafe.array(indexLike),
177
+ it => it?.constructor === Object ? Object.values(it) : it,
178
+ ),
167
179
  indexes: indexesSchema,
168
180
  eventLog: eventLogSchema,
169
181
  }),
@@ -1,5 +1,7 @@
1
+ type Path = (string | number)[]
2
+
1
3
  type CompareError = {
2
- path: (string | number)[]
4
+ path: Path
3
5
  message: string
4
6
  }
5
7
 
@@ -26,10 +28,41 @@ const getType = (val: any): ExtendedType => {
26
28
  return 'object'
27
29
  }
28
30
 
29
- export function deepCompare(a: any, b: any, path: (string | number)[]): CompareError[] {
31
+ type Comparator = (a: unknown, b: unknown) => CompareError[]
32
+
33
+ export const compareArraysIgnoreOrder = (a: unknown, b: unknown, path: Path) => {
34
+ if (!Array.isArray(a) || !Array.isArray(b)) {
35
+ return [{ path, message: 'Invalid type, expected array' }]
36
+ }
37
+ if (a.length !== b.length) {
38
+ return [{ path, message: `Array length: ${a.length} != ${b.length}` }]
39
+ }
40
+ const haystack = [...b]
41
+ for (const needle of a) {
42
+ const index = haystack.findIndex(it => {
43
+ const errors = deepCompare(needle, it, path)
44
+ return errors.length === 0
45
+ })
46
+ if (index < 0) {
47
+ return [{ path, message: `Array item: ${JSON.stringify(needle)} not found in ${JSON.stringify(haystack)}` }]
48
+ }
49
+ haystack.splice(index, 1)
50
+ }
51
+ if (haystack.length > 0) {
52
+ return [{ path, message: `Array items: ${JSON.stringify(haystack)} not found in ${JSON.stringify(a)}` }]
53
+ }
54
+ return []
55
+ }
56
+
57
+ export function deepCompare(a: any, b: any, path: Path = [], getCustomComparator?: (path: Path) => Comparator | null): CompareError[] {
30
58
  if (a === b) {
31
59
  return []
32
60
  }
61
+ const comparator = getCustomComparator?.(path)
62
+ if (comparator) {
63
+ return comparator(a, b)
64
+ }
65
+
33
66
  const aType = getType(a)
34
67
  const bType = getType(b)
35
68
  if (aType !== bType) {
@@ -53,7 +86,7 @@ export function deepCompare(a: any, b: any, path: (string | number)[]): CompareE
53
86
  }
54
87
  const errors: CompareError[] = []
55
88
  for (let i = 0; i < aLength; i++) {
56
- errors.push(...deepCompare(a[i], b[i], [...path, i]))
89
+ errors.push(...deepCompare(a[i], b[i], [...path, i], getCustomComparator))
57
90
  }
58
91
  return errors
59
92
  }
@@ -91,7 +124,7 @@ export function deepCompare(a: any, b: any, path: (string | number)[]): CompareE
91
124
  message: `Missing property: ${key} not in right`,
92
125
  })
93
126
  } else {
94
- errors.push(...deepCompare(a[key], b[key], [...path, key]))
127
+ errors.push(...deepCompare(a[key], b[key], [...path, key], getCustomComparator))
95
128
  }
96
129
  }
97
130
  for (const key of bKeys) {
@@ -54,15 +54,10 @@ export class ModelValidator {
54
54
  }
55
55
 
56
56
  private validateUniqueConstraints(uniqueConstraints: Model.Entity['unique'], fields: Set<string>, errors: ErrorBuilder): void {
57
- for (const [constraintName, constraint] of Object.entries(uniqueConstraints)) {
58
- const uniqueErrors = errors.for(constraintName)
59
- if (constraint.name !== constraintName) {
60
- uniqueErrors.add('MODEL_NAME_MISMATCH', `Constraint name ${constraint.name} does not match the name in a map "${constraintName}"`)
61
- continue
62
- }
57
+ for (const constraint of uniqueConstraints) {
63
58
  for (const field of constraint.fields) {
64
59
  if (!fields.has(field)) {
65
- uniqueErrors.add('MODEL_UNDEFINED_FIELD', `Referenced field ${field} in a constraint does not exists`)
60
+ errors.add('MODEL_UNDEFINED_FIELD', `Referenced field ${field} in a constraint does not exists`)
66
61
  }
67
62
  }
68
63
  }
@@ -266,25 +261,6 @@ export class ModelValidator {
266
261
  visitManyHasOne: () => { },
267
262
  })
268
263
  }
269
- for (const entity of entities) {
270
- const entityErrorBuilder = errorBuilder.for(entity.name)
271
- for (const index of Object.values(entity.indexes)) {
272
- const description = `index name ${index.name} of entity ${entity.name}`
273
- if (relationNames[index.name]) {
274
- entityErrorBuilder.add('MODEL_NAME_COLLISION', `${description} collides with ${relationNames[index.name]}`)
275
- } else {
276
- relationNames[index.name] = description
277
- }
278
- }
279
- for (const unique of Object.values(entity.unique)) {
280
- const description = `unique index name ${unique.name} of entity ${entity.name}`
281
- if (relationNames[unique.name]) {
282
- entityErrorBuilder.add('MODEL_NAME_COLLISION', `${description} collides with ${relationNames[unique.name]}`)
283
- } else {
284
- relationNames[unique.name] = description
285
- }
286
- }
287
- }
288
264
  }
289
265
  }
290
266
 
@@ -7,6 +7,7 @@ import { ActionsValidator } from './ActionsValidator'
7
7
 
8
8
  export interface SchemaValidatorSkippedErrors {
9
9
  code: ValidationErrorCode
10
+ skipUntil?: string
10
11
  path?: string
11
12
  }
12
13
 
@@ -45,12 +45,12 @@ exports[`lax schema converter basic 1`] = `
45
45
  "type": "String",
46
46
  },
47
47
  },
48
- "indexes": {},
48
+ "indexes": [],
49
49
  "name": "Article",
50
50
  "primary": "id",
51
51
  "primaryColumn": "id",
52
52
  "tableName": "article",
53
- "unique": {},
53
+ "unique": [],
54
54
  },
55
55
  },
56
56
  "enums": {
@@ -117,12 +117,12 @@ exports[`lax schema converter many-has-many 1`] = `
117
117
  "type": "String",
118
118
  },
119
119
  },
120
- "indexes": {},
120
+ "indexes": [],
121
121
  "name": "Article",
122
122
  "primary": "id",
123
123
  "primaryColumn": "id",
124
124
  "tableName": "article",
125
- "unique": {},
125
+ "unique": [],
126
126
  },
127
127
  "Tag": {
128
128
  "eventLog": {
@@ -143,12 +143,12 @@ exports[`lax schema converter many-has-many 1`] = `
143
143
  "type": "Uuid",
144
144
  },
145
145
  },
146
- "indexes": {},
146
+ "indexes": [],
147
147
  "name": "Tag",
148
148
  "primary": "id",
149
149
  "primaryColumn": "id",
150
150
  "tableName": "tag",
151
- "unique": {},
151
+ "unique": [],
152
152
  },
153
153
  },
154
154
  "enums": {},
@@ -200,12 +200,12 @@ exports[`lax schema converter one-has-many 1`] = `
200
200
  "type": "String",
201
201
  },
202
202
  },
203
- "indexes": {},
203
+ "indexes": [],
204
204
  "name": "Article",
205
205
  "primary": "id",
206
206
  "primaryColumn": "id",
207
207
  "tableName": "article",
208
- "unique": {},
208
+ "unique": [],
209
209
  },
210
210
  "Category": {
211
211
  "eventLog": {
@@ -226,12 +226,12 @@ exports[`lax schema converter one-has-many 1`] = `
226
226
  "type": "Uuid",
227
227
  },
228
228
  },
229
- "indexes": {},
229
+ "indexes": [],
230
230
  "name": "Category",
231
231
  "primary": "id",
232
232
  "primaryColumn": "id",
233
233
  "tableName": "category",
234
- "unique": {},
234
+ "unique": [],
235
235
  },
236
236
  },
237
237
  "enums": {},
@@ -283,12 +283,12 @@ exports[`lax schema converter one-has-one 1`] = `
283
283
  "type": "String",
284
284
  },
285
285
  },
286
- "indexes": {},
286
+ "indexes": [],
287
287
  "name": "Article",
288
288
  "primary": "id",
289
289
  "primaryColumn": "id",
290
290
  "tableName": "article",
291
- "unique": {},
291
+ "unique": [],
292
292
  },
293
293
  "Content": {
294
294
  "eventLog": {
@@ -310,12 +310,12 @@ exports[`lax schema converter one-has-one 1`] = `
310
310
  "type": "Uuid",
311
311
  },
312
312
  },
313
- "indexes": {},
313
+ "indexes": [],
314
314
  "name": "Content",
315
315
  "primary": "id",
316
316
  "primaryColumn": "id",
317
317
  "tableName": "content",
318
- "unique": {},
318
+ "unique": [],
319
319
  },
320
320
  },
321
321
  "enums": {},
@@ -0,0 +1,44 @@
1
+ import { assert, describe, test } from 'vitest'
2
+ import { compareArraysIgnoreOrder } from '../../../src'
3
+
4
+ describe('deep compare', () => {
5
+ test('compare array ignore order - success', () => {
6
+ const actual = compareArraysIgnoreOrder([
7
+ { foo: 'bar' },
8
+ { lorem: 'ipsum' },
9
+ ], [
10
+ { lorem: 'ipsum' },
11
+ { foo: 'bar' },
12
+ ], [])
13
+ assert.deepStrictEqual(actual, [])
14
+ })
15
+
16
+ test('compare array ignore order - missing in A', () => {
17
+ const actual = compareArraysIgnoreOrder([
18
+ { lorem: 'ipsum' },
19
+ { lorem: 'ipsum' },
20
+ ], [
21
+ { lorem: 'ipsum' },
22
+ { foo: 'bar' },
23
+ ], [])
24
+ assert.deepStrictEqual(actual, [{
25
+ path: [],
26
+ message: 'Array item: {"lorem":"ipsum"} not found in [{"foo":"bar"}]',
27
+ }])
28
+ })
29
+
30
+ test('compare array ignore order - missing in B', () => {
31
+ const actual = compareArraysIgnoreOrder([
32
+ { foo: 'bar' },
33
+ { lorem: 'ipsum' },
34
+ ], [
35
+ { lorem: 'ipsum' },
36
+ { lorem: 'ipsum' },
37
+ ], [])
38
+ assert.deepStrictEqual(actual, [{
39
+ path: [],
40
+ message: 'Array item: {"foo":"bar"} not found in [{"lorem":"ipsum"},{"lorem":"ipsum"}]',
41
+ }],
42
+ )
43
+ })
44
+ })
@@ -4,73 +4,6 @@ import { ModelValidator } from '../../../src'
4
4
 
5
5
 
6
6
 
7
- test('index name collision', () => {
8
- const model: Model.Schema = {
9
- enums: {},
10
- entities: {
11
- Foo: {
12
- fields: {
13
- id: {
14
- columnName: 'id',
15
- name: 'id',
16
- nullable: false,
17
- type: Model.ColumnType.Uuid,
18
- columnType: 'uuid',
19
- },
20
- },
21
- name: 'Foo',
22
- primary: 'id',
23
- primaryColumn: 'id',
24
- tableName: 'foo',
25
- unique: {},
26
- eventLog: {
27
- enabled: true,
28
- },
29
- indexes: {
30
- test: { fields: ['id'], name: 'test' },
31
- },
32
- },
33
- Bar: {
34
- fields: {
35
- id: {
36
- columnName: 'id',
37
- name: 'id',
38
- nullable: false,
39
- type: Model.ColumnType.Uuid,
40
- columnType: 'uuid',
41
- },
42
- },
43
- name: 'Bar',
44
- primary: 'id',
45
- primaryColumn: 'id',
46
- tableName: 'bar',
47
- unique: {
48
- test: { fields: ['id'], name: 'test' },
49
- },
50
- eventLog: {
51
- enabled: true,
52
- },
53
- indexes: {
54
- foo: { fields: ['id'], name: 'foo' },
55
- },
56
- },
57
- },
58
- }
59
- const validator = new ModelValidator(model)
60
- assert.deepStrictEqual(validator.validate(), [
61
- {
62
- code: 'MODEL_NAME_COLLISION',
63
- message: 'index name foo of entity Bar collides with table name foo of entity Foo',
64
- path: ['entities', 'Bar'],
65
- },
66
- {
67
- code: 'MODEL_NAME_COLLISION',
68
- message: 'unique index name test of entity Bar collides with index name test of entity Foo',
69
- path: ['entities', 'Bar'],
70
- },
71
- ])
72
- })
73
-
74
7
 
75
8
  test('"meta" collision', () => {
76
9
  const model: Model.Schema = {
@@ -90,9 +23,9 @@ test('"meta" collision', () => {
90
23
  primary: 'id',
91
24
  primaryColumn: 'id',
92
25
  tableName: 'foo',
93
- unique: {},
26
+ unique: [],
94
27
  eventLog: { enabled: true },
95
- indexes: {},
28
+ indexes: [],
96
29
  },
97
30
  FooMeta: {
98
31
  fields: {
@@ -108,9 +41,9 @@ test('"meta" collision', () => {
108
41
  primary: 'id',
109
42
  primaryColumn: 'id',
110
43
  tableName: 'foo_meta',
111
- unique: {},
44
+ unique: [],
112
45
  eventLog: { enabled: true },
113
- indexes: {},
46
+ indexes: [],
114
47
  },
115
48
  BarMeta: {
116
49
  fields: {
@@ -126,9 +59,9 @@ test('"meta" collision', () => {
126
59
  primary: 'id',
127
60
  primaryColumn: 'id',
128
61
  tableName: 'bar',
129
- unique: {},
62
+ unique: [],
130
63
  eventLog: { enabled: true },
131
- indexes: {},
64
+ indexes: [],
132
65
  },
133
66
  },
134
67
  }
@@ -1,9 +0,0 @@
1
- export declare class NamingHelper {
2
- static createForeignKeyIndexName(tableName: string, column: string): string;
3
- static createJunctionTablePrimaryConstraintName(tableName: string): string;
4
- static createForeignKeyName(fromTable: string, fromColumn: string, toTable: string, toColumn: string): string;
5
- static createIndexName: (entityName: string, fields: readonly string[]) => string;
6
- static createUniqueConstraintName: (entityName: string, fields: readonly string[]) => string;
7
- private static createUniqueSuffix;
8
- }
9
- //# sourceMappingURL=NamingHelper.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"NamingHelper.d.ts","sourceRoot":"","sources":["../../../src/model/NamingHelper.ts"],"names":[],"mappings":"AAIA,qBAAa,YAAY;WACV,yBAAyB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;WAK3D,wCAAwC,CAAC,SAAS,EAAE,MAAM;WAK1D,oBAAoB,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM;IAOpH,OAAc,eAAe,eAAgB,MAAM,UAAU,SAAS,MAAM,EAAE,KAAG,MAAM,CAKtF;IAED,OAAc,0BAA0B,eAAgB,MAAM,UAAU,SAAS,MAAM,EAAE,KAAG,MAAM,CAKjG;IAED,OAAO,CAAC,MAAM,CAAC,kBAAkB,CAGhC;CACD"}
@@ -1,44 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- var _a;
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.NamingHelper = void 0;
8
- const sha256_1 = __importDefault(require("crypto-js/sha256"));
9
- const enc_hex_1 = __importDefault(require("crypto-js/enc-hex"));
10
- class NamingHelper {
11
- static createForeignKeyIndexName(tableName, column) {
12
- // format matches https://github.com/salsita/node-pg-migrate/blob/9331f6fda98795e3f3733461f9b71345168b99d8/src/operations/indexes.ts#L21
13
- return `${tableName}_${column}_index`;
14
- }
15
- static createJunctionTablePrimaryConstraintName(tableName) {
16
- // format matches https://github.com/salsita/node-pg-migrate/blob/9331f6fda98795e3f3733461f9b71345168b99d8/src/operations/tables.ts#L204
17
- return `${tableName}_pkey`;
18
- }
19
- static createForeignKeyName(fromTable, fromColumn, toTable, toColumn) {
20
- return 'fk_'
21
- + fromTable + '_'
22
- + fromColumn + '_'
23
- + this.createUniqueSuffix([fromTable, fromColumn, toTable, toColumn]);
24
- }
25
- }
26
- exports.NamingHelper = NamingHelper;
27
- _a = NamingHelper;
28
- NamingHelper.createIndexName = (entityName, fields) => {
29
- return 'idx_'
30
- + entityName + '_'
31
- + fields.join('_') + '_'
32
- + _a.createUniqueSuffix([entityName, ...fields]);
33
- };
34
- NamingHelper.createUniqueConstraintName = (entityName, fields) => {
35
- return 'unique_'
36
- + entityName + '_'
37
- + fields.join('_') + '_'
38
- + _a.createUniqueSuffix([entityName, ...fields]);
39
- };
40
- NamingHelper.createUniqueSuffix = (values) => {
41
- const uniqueSuffix = (0, sha256_1.default)(JSON.stringify(values)).toString(enc_hex_1.default);
42
- return uniqueSuffix.slice(0, 6);
43
- };
44
- //# sourceMappingURL=NamingHelper.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"NamingHelper.js","sourceRoot":"","sources":["../../../src/model/NamingHelper.ts"],"names":[],"mappings":";;;;;;;AAAA,8DAAqC;AACrC,gEAAsC;AAGtC,MAAa,YAAY;IACjB,MAAM,CAAC,yBAAyB,CAAC,SAAiB,EAAE,MAAc;QACxE,wIAAwI;QACxI,OAAO,GAAG,SAAS,IAAI,MAAM,QAAQ,CAAA;IACtC,CAAC;IAEM,MAAM,CAAC,wCAAwC,CAAC,SAAiB;QACvE,wIAAwI;QACxI,OAAO,GAAG,SAAS,OAAO,CAAA;IAC3B,CAAC;IAEM,MAAM,CAAC,oBAAoB,CAAC,SAAiB,EAAE,UAAkB,EAAE,OAAe,EAAE,QAAgB;QAC1G,OAAO,KAAK;cACT,SAAS,GAAG,GAAG;cACf,UAAU,GAAG,GAAG;cAChB,IAAI,CAAC,kBAAkB,CAAC,CAAC,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAA;IACvE,CAAC;;AAhBF,oCAoCC;;AAlBc,4BAAe,GAAG,CAAC,UAAkB,EAAE,MAAyB,EAAU,EAAE;IACzF,OAAO,MAAM;UACV,UAAU,GAAG,GAAG;UAChB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG;UACtB,EAAI,CAAC,kBAAkB,CAAC,CAAC,UAAU,EAAE,GAAG,MAAM,CAAC,CAAC,CAAA;AACpD,CAAC,CAAA;AAEa,uCAA0B,GAAG,CAAC,UAAkB,EAAE,MAAyB,EAAU,EAAE;IACpG,OAAO,SAAS;UACb,UAAU,GAAG,GAAG;UAChB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG;UACtB,EAAI,CAAC,kBAAkB,CAAC,CAAC,UAAU,EAAE,GAAG,MAAM,CAAC,CAAC,CAAA;AACpD,CAAC,CAAA;AAEc,+BAAkB,GAAG,CAAC,MAAgB,EAAU,EAAE;IAChE,MAAM,YAAY,GAAG,IAAA,gBAAM,EAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,iBAAM,CAAC,CAAA;IACpE,OAAO,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AAChC,CAAC,CAAA"}
@@ -1,41 +0,0 @@
1
- import Sha256 from 'crypto-js/sha256'
2
- import EncHex from 'crypto-js/enc-hex'
3
-
4
-
5
- export class NamingHelper {
6
- public static createForeignKeyIndexName(tableName: string, column: string) {
7
- // format matches https://github.com/salsita/node-pg-migrate/blob/9331f6fda98795e3f3733461f9b71345168b99d8/src/operations/indexes.ts#L21
8
- return `${tableName}_${column}_index`
9
- }
10
-
11
- public static createJunctionTablePrimaryConstraintName(tableName: string) {
12
- // format matches https://github.com/salsita/node-pg-migrate/blob/9331f6fda98795e3f3733461f9b71345168b99d8/src/operations/tables.ts#L204
13
- return `${tableName}_pkey`
14
- }
15
-
16
- public static createForeignKeyName(fromTable: string, fromColumn: string, toTable: string, toColumn: string): string {
17
- return 'fk_'
18
- + fromTable + '_'
19
- + fromColumn + '_'
20
- + this.createUniqueSuffix([fromTable, fromColumn, toTable, toColumn])
21
- }
22
-
23
- public static createIndexName = (entityName: string, fields: readonly string[]): string => {
24
- return 'idx_'
25
- + entityName + '_'
26
- + fields.join('_') + '_'
27
- + this.createUniqueSuffix([entityName, ...fields])
28
- }
29
-
30
- public static createUniqueConstraintName = (entityName: string, fields: readonly string[]): string => {
31
- return 'unique_'
32
- + entityName + '_'
33
- + fields.join('_') + '_'
34
- + this.createUniqueSuffix([entityName, ...fields])
35
- }
36
-
37
- private static createUniqueSuffix = (values: string[]): string => {
38
- const uniqueSuffix = Sha256(JSON.stringify(values)).toString(EncHex)
39
- return uniqueSuffix.slice(0, 6)
40
- }
41
- }