@entity-access/entity-access 1.0.490 → 1.0.492

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 (58) hide show
  1. package/dist/common/CIMap.d.ts +16 -0
  2. package/dist/common/CIMap.d.ts.map +1 -0
  3. package/dist/common/CIMap.js +43 -0
  4. package/dist/common/CIMap.js.map +1 -0
  5. package/dist/common/IColumnSchema.d.ts +9 -0
  6. package/dist/common/IColumnSchema.d.ts.map +1 -1
  7. package/dist/drivers/base/BaseDriver.d.ts +0 -2
  8. package/dist/drivers/base/BaseDriver.d.ts.map +1 -1
  9. package/dist/drivers/base/BaseDriver.js.map +1 -1
  10. package/dist/drivers/base/ExistingSchema.d.ts +14 -0
  11. package/dist/drivers/base/ExistingSchema.d.ts.map +1 -0
  12. package/dist/drivers/base/ExistingSchema.js +34 -0
  13. package/dist/drivers/base/ExistingSchema.js.map +1 -0
  14. package/dist/drivers/postgres/PostgreSqlDriver.d.ts.map +1 -1
  15. package/dist/drivers/postgres/PostgreSqlDriver.js +0 -31
  16. package/dist/drivers/postgres/PostgreSqlDriver.js.map +1 -1
  17. package/dist/drivers/sql-server/SqlServerDriver.d.ts +0 -2
  18. package/dist/drivers/sql-server/SqlServerDriver.d.ts.map +1 -1
  19. package/dist/drivers/sql-server/SqlServerDriver.js +0 -41
  20. package/dist/drivers/sql-server/SqlServerDriver.js.map +1 -1
  21. package/dist/migrations/Migrations.d.ts +9 -2
  22. package/dist/migrations/Migrations.d.ts.map +1 -1
  23. package/dist/migrations/Migrations.js +54 -6
  24. package/dist/migrations/Migrations.js.map +1 -1
  25. package/dist/migrations/postgres/PostgresAutomaticMigrations.d.ts +2 -4
  26. package/dist/migrations/postgres/PostgresAutomaticMigrations.d.ts.map +1 -1
  27. package/dist/migrations/postgres/PostgresAutomaticMigrations.js +15 -39
  28. package/dist/migrations/postgres/PostgresAutomaticMigrations.js.map +1 -1
  29. package/dist/migrations/postgres/PostgresMigrations.d.ts +3 -0
  30. package/dist/migrations/postgres/PostgresMigrations.d.ts.map +1 -1
  31. package/dist/migrations/postgres/PostgresMigrations.js +68 -0
  32. package/dist/migrations/postgres/PostgresMigrations.js.map +1 -1
  33. package/dist/migrations/sql-server/SqlServerAutomaticMigrations.d.ts +2 -4
  34. package/dist/migrations/sql-server/SqlServerAutomaticMigrations.d.ts.map +1 -1
  35. package/dist/migrations/sql-server/SqlServerAutomaticMigrations.js +22 -45
  36. package/dist/migrations/sql-server/SqlServerAutomaticMigrations.js.map +1 -1
  37. package/dist/migrations/sql-server/SqlServerMigrations.d.ts +5 -0
  38. package/dist/migrations/sql-server/SqlServerMigrations.d.ts.map +1 -1
  39. package/dist/migrations/sql-server/SqlServerMigrations.js +77 -0
  40. package/dist/migrations/sql-server/SqlServerMigrations.js.map +1 -1
  41. package/dist/tsconfig.tsbuildinfo +1 -1
  42. package/package.json +1 -1
  43. package/src/common/CIMap.ts +50 -0
  44. package/src/common/IColumnSchema.ts +12 -0
  45. package/src/drivers/base/BaseDriver.ts +1 -3
  46. package/src/drivers/base/ExistingSchema.ts +60 -0
  47. package/src/drivers/postgres/PostgreSqlDriver.ts +1 -32
  48. package/src/drivers/sql-server/SqlServerDriver.ts +1 -42
  49. package/src/migrations/Migrations.ts +86 -14
  50. package/src/migrations/postgres/PostgresAutomaticMigrations.ts +22 -54
  51. package/src/migrations/postgres/PostgresMigrations.ts +78 -0
  52. package/src/migrations/sql-server/SqlServerAutomaticMigrations.ts +27 -58
  53. package/src/migrations/sql-server/SqlServerMigrations.ts +86 -0
  54. package/dist/migrations/ExistingSchema.d.ts +0 -9
  55. package/dist/migrations/ExistingSchema.d.ts.map +0 -1
  56. package/dist/migrations/ExistingSchema.js +0 -31
  57. package/dist/migrations/ExistingSchema.js.map +0 -1
  58. package/src/migrations/ExistingSchema.ts +0 -39
@@ -4,10 +4,10 @@ import { IColumn } from "../../decorators/IColumn.js";
4
4
  import { IForeignKeyConstraint } from "../../decorators/IForeignKeyConstraint.js";
5
5
  import { IIndex } from "../../decorators/IIndex.js";
6
6
  import { BaseConnection } from "../../drivers/base/BaseDriver.js";
7
+ import ExistingSchema from "../../drivers/base/ExistingSchema.js";
7
8
  import { SqlServerLiteral } from "../../drivers/sql-server/SqlServerLiteral.js";
8
9
  import EntityType from "../../entity-query/EntityType.js";
9
10
  import type EntityContext from "../../model/EntityContext.js";
10
- import ExistingSchema from "../ExistingSchema.js";
11
11
  import SqlServerMigrations from "./SqlServerMigrations.js";
12
12
 
13
13
  export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
@@ -23,20 +23,6 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
23
23
  END`);
24
24
  }
25
25
 
26
- async migrateTable(context: EntityContext, type: EntityType) {
27
-
28
-
29
- // create table if not exists...
30
- const nonKeyColumns = type.nonKeys;
31
- const keys = type.keys;
32
-
33
- const driver = context.connection;
34
-
35
- await this.createTable(driver, type, keys);
36
-
37
- await this.createColumns(driver, type, nonKeyColumns);
38
-
39
- }
40
26
 
41
27
  async createIndexForForeignKeys(context: EntityContext, type: EntityType, fkColumns: IColumn[]) {
42
28
  for (const iterator of fkColumns) {
@@ -48,61 +34,43 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
48
34
  columns: [{ name: iterator.quotedColumnName, descending: iterator.indexOrder !== "ascending"}],
49
35
  filter
50
36
  };
51
- await this.migrateIndex(context, indexDef, type);
37
+ await this.migrateIndexInternal(context, indexDef, type);
52
38
  }
53
39
  }
54
40
 
55
- async createColumns(driver: BaseConnection, type: EntityType, nonKeyColumns: IColumn[]) {
41
+ async createColumn(type: EntityType, iterator: IColumn) {
56
42
 
57
- const name = type.schema
58
- ? type.schema + "." + type.name
59
- : type.name;
60
-
61
- if (nonKeyColumns.length > 1) {
62
- nonKeyColumns.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
63
- }
43
+ const { quotedColumnName } = iterator;
64
44
 
65
- const columns = await ExistingSchema.getSchema(driver, type.schema || "dbo", type.name);
66
-
67
- const columnSet = new Set(columns.map((x) => x.name.toLowerCase()));
68
-
69
- for (const iterator of nonKeyColumns) {
70
- const { quotedColumnName, columnName } = iterator;
71
- if (columnSet.has(columnName.toLowerCase())) {
72
- continue;
73
- }
74
- let def = `ALTER TABLE ${name} ADD ${quotedColumnName} `;
45
+ const name = type.schema
46
+ ? type.schema + "." + type.name
47
+ : type.name;
75
48
 
76
- if (iterator.computed) {
77
- def += ` AS ${iterator.computed} ${iterator.stored ? "PERSISTED" : ""}`;
78
- await this.executeQuery(def + ";");
79
- continue;
80
- }
49
+ let def = `ALTER TABLE ${name} ADD ${quotedColumnName} `;
81
50
 
82
- def += this.getColumnDefinition(iterator);
83
- if (iterator.nullable === true) {
84
- def += " NULL ";
85
- } else {
86
- def += " NOT NULL ";
87
- }
88
- if (iterator.computed) {
89
- def += ` AS ${iterator.computed} ${iterator.stored ? "PERSISTED" : ""}`;
90
- }
91
- if (typeof iterator.default === "string") {
92
- def += " DEFAULT " + iterator.default;
93
- }
51
+ if (iterator.computed) {
52
+ def += ` AS ${iterator.computed} ${iterator.stored ? "PERSISTED" : ""}`;
94
53
  await this.executeQuery(def + ";");
54
+ return;
95
55
  }
96
56
 
97
- }
98
-
99
- async createTable(driver: BaseConnection, type: EntityType, keys: IColumn[]) {
57
+ def += this.getColumnDefinition(iterator);
58
+ if (iterator.nullable === true) {
59
+ def += " NULL ";
60
+ } else {
61
+ def += " NOT NULL ";
62
+ }
63
+ if (iterator.computed) {
64
+ def += ` AS ${iterator.computed} ${iterator.stored ? "PERSISTED" : ""}`;
65
+ }
66
+ if (typeof iterator.default === "string") {
67
+ def += " DEFAULT " + iterator.default;
68
+ }
69
+ await this.executeQuery(def + ";");
100
70
 
101
- const columns = await ExistingSchema.getSchema(driver, type.schema || "public", type.name);
71
+ }
102
72
 
103
- if (columns.length) {
104
- return;
105
- }
73
+ async createTable(type: EntityType, keys: IColumn[]) {
106
74
 
107
75
  const name = type.schema
108
76
  ? type.schema + "." + type.name
@@ -138,6 +106,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
138
106
  }
139
107
 
140
108
  async migrateIndex(context: EntityContext, index: IIndex, type: EntityType) {
109
+
141
110
  const driver = context.connection;
142
111
  const name = type.schema
143
112
  ? type.schema + "." + type.name
@@ -1,8 +1,94 @@
1
+ import CIMap from "../../common/CIMap.js";
1
2
  import { IColumn } from "../../decorators/IColumn.js";
3
+ import ExistingSchema from "../../drivers/base/ExistingSchema.js";
4
+ import EntityType from "../../entity-query/EntityType.js";
2
5
  import Migrations from "../Migrations.js";
3
6
 
4
7
  export default abstract class SqlServerMigrations extends Migrations {
5
8
 
9
+ protected schemaCache = new CIMap<ExistingSchema>();
10
+
11
+ async getExistingSchema(type: EntityType) {
12
+
13
+ const schema = type.schema || "dbo";
14
+
15
+ let text = `
16
+ SELECT
17
+ COLUMN_NAME as [name],
18
+ CASE DATA_TYPE
19
+ WHEN 'bit' THEN 'Boolean'
20
+ WHEN 'int' Then 'Int'
21
+ WHEN 'bigint' THEN 'BigInt'
22
+ WHEN 'date' then 'DateTime'
23
+ WHEN 'datetime' then 'DateTime'
24
+ WHEN 'datetime2' then 'DateTime'
25
+ WHEN 'real' then 'Float'
26
+ WHEN 'double' then 'Double'
27
+ WHEN 'decimal' then 'Decimal'
28
+ WHEN 'identity' then 'UUID'
29
+ WHEN 'varbinary' then 'ByteArray'
30
+ WHEN 'geometry' then 'Geometry'
31
+ ELSE 'Char'
32
+ END as [dataType],
33
+ CASE WHEN IS_NULLABLE = 'YES' THEN 1 ELSE 0 END as [nullable],
34
+ CHARACTER_MAXIMUM_LENGTH as [length],
35
+ CASE
36
+ WHEN COLUMN_DEFAULT = 'getutcdate()' then '() => Sql.date.now()'
37
+ WHEN COLUMN_DEFAULT = '(getutcdate())' then '() => Sql.date.now()'
38
+ WHEN COLUMN_DEFAULT = '(newid())' then '() => Sql.crypto.randomUUID()'
39
+ WHEN (COLUMN_DEFAULT = '(0)' OR COLUMN_DEFAULT = '((0))')
40
+ AND DATA_TYPE = 'bit' THEN '() => false'
41
+ WHEN (COLUMN_DEFAULT = '(1)' OR COLUMN_DEFAULT = '((1))')
42
+ AND DATA_TYPE = 'bit' THEN '() => true'
43
+ WHEN COLUMN_DEFAULT is NULL THEN ''
44
+ ELSE '() => ' + COLUMN_DEFAULT
45
+ END as [default],
46
+ ColumnProperty(OBJECT_ID(TABLE_SCHEMA+'.'+TABLE_NAME),COLUMN_NAME,'IsComputed') as [computed],
47
+ TABLE_NAME as [ownerName],
48
+ 'table' as [ownerType]
49
+ FROM INFORMATION_SCHEMA.COLUMNS
50
+ WHERE TABLE_SCHEMA = $1
51
+ `;
52
+ let r = await this.executeQuery({ text, values: [schema] });
53
+ const columns = r.rows ?? [];
54
+
55
+ text = `
56
+ SELECT
57
+ ind.name as [name]
58
+ FROM
59
+ sys.indexes ind
60
+ INNER JOIN
61
+ sys.tables t ON ind.object_id = t.object_id
62
+ INNER JOIN
63
+ sys.schemas s ON t.schema_id = s.schema_id
64
+ WHERE
65
+ s.name = $1`;
66
+ r = await this.executeQuery({ text, values: [schema] });
67
+ const indexes = r.rows ?? [];
68
+
69
+ text = `
70
+ SELECT
71
+ CONSTRAINT_NAME as [name]
72
+ FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
73
+ WHERE
74
+ CONSTRAINT_TYPE <> 'Foreign Key'
75
+ AND TABLE_SCHEMA=$1`;
76
+ r = await this.executeQuery({ text, values: [schema] });
77
+ const constraints = r.rows ?? [];
78
+
79
+ text = `
80
+ SELECT
81
+ CONSTRAINT_NAME as [name]
82
+ FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
83
+ WHERE
84
+ CONSTRAINT_TYPE = 'Foreign Key'
85
+ AND TABLE_SCHEMA=$1`;
86
+ r = await this.executeQuery({ text, values: [schema] });
87
+ const foreignKeys = r.rows ?? [];
88
+
89
+ return new ExistingSchema(true, { columns, indexes, constraints, foreignKeys });
90
+ }
91
+
6
92
  protected getColumnDefinition(iterator: IColumn) {
7
93
  if (iterator.dataType === "Decimal") {
8
94
  if (iterator.precision && iterator.scale) {
@@ -1,9 +0,0 @@
1
- import IColumnSchema from "../common/IColumnSchema.js";
2
- import type { BaseConnection } from "../drivers/base/BaseDriver.js";
3
- export default class ExistingSchema {
4
- static getSchema(connection: BaseConnection, schema: string, table: string, caseInsensitive?: boolean): Promise<IColumnSchema[]>;
5
- private static cache;
6
- tables: Map<string, IColumnSchema[]>;
7
- constructor(columns: IColumnSchema[], caseInsensitive?: boolean);
8
- }
9
- //# sourceMappingURL=ExistingSchema.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ExistingSchema.d.ts","sourceRoot":"","sources":["../../src/migrations/ExistingSchema.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,4BAA4B,CAAC;AACvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAEpE,MAAM,CAAC,OAAO,OAAO,cAAc;WAGX,SAAS,CAAC,UAAU,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,UAAQ;IAahH,OAAO,CAAC,MAAM,CAAC,KAAK,CAAqC;IAElD,MAAM,+BAAqC;gBAEtC,OAAO,EAAE,aAAa,EAAE,EAAE,eAAe,UAAQ;CAehE"}
@@ -1,31 +0,0 @@
1
- export default class ExistingSchema {
2
- static async getSchema(connection, schema, table, caseInsensitive = false) {
3
- let s = this.cache.get(schema);
4
- if (!s) {
5
- const columns = await connection.getColumnSchema(schema);
6
- s = new ExistingSchema(columns, caseInsensitive);
7
- this.cache.set(schema, s);
8
- }
9
- if (caseInsensitive) {
10
- table = table.toLowerCase();
11
- }
12
- return s.tables.get(table) ?? [];
13
- }
14
- static cache = new Map();
15
- tables = new Map();
16
- constructor(columns, caseInsensitive = false) {
17
- for (const c of columns) {
18
- let tableName = c.ownerName;
19
- if (caseInsensitive) {
20
- tableName = tableName.toLowerCase();
21
- }
22
- let table = this.tables.get(tableName);
23
- if (!table) {
24
- table = [];
25
- this.tables.set(tableName, table);
26
- }
27
- table.push(c);
28
- }
29
- }
30
- }
31
- //# sourceMappingURL=ExistingSchema.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ExistingSchema.js","sourceRoot":"","sources":["../../src/migrations/ExistingSchema.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,OAAO,OAAO,cAAc;IAGxB,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,UAA0B,EAAE,MAAc,EAAE,KAAa,EAAE,eAAe,GAAG,KAAK;QAC5G,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC/B,IAAI,CAAC,CAAC,EAAE,CAAC;YACL,MAAM,OAAO,GAAG,MAAM,UAAU,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;YACzD,CAAC,GAAG,IAAI,cAAc,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;YACjD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;QAC9B,CAAC;QACD,IAAI,eAAe,EAAE,CAAC;YAClB,KAAK,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC;QAChC,CAAC;QACD,OAAO,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IACrC,CAAC;IAEO,MAAM,CAAC,KAAK,GAAG,IAAI,GAAG,EAA0B,CAAC;IAElD,MAAM,GAAG,IAAI,GAAG,EAA0B,CAAC;IAElD,YAAY,OAAwB,EAAE,eAAe,GAAG,KAAK;QACzD,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;YACtB,IAAI,SAAS,GAAG,CAAC,CAAC,SAAS,CAAC;YAC5B,IAAI,eAAe,EAAE,CAAC;gBAClB,SAAS,GAAG,SAAS,CAAC,WAAW,EAAE,CAAC;YACxC,CAAC;YACD,IAAI,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YACvC,IAAI,CAAC,KAAK,EAAE,CAAC;gBACT,KAAK,GAAG,EAAE,CAAC;gBACX,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;YACtC,CAAC;YACD,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC;IACL,CAAC"}
@@ -1,39 +0,0 @@
1
- import IColumnSchema from "../common/IColumnSchema.js";
2
- import type { BaseConnection } from "../drivers/base/BaseDriver.js";
3
-
4
- export default class ExistingSchema {
5
-
6
-
7
- public static async getSchema(connection: BaseConnection, schema: string, table: string, caseInsensitive = false) {
8
- let s = this.cache.get(schema);
9
- if (!s) {
10
- const columns = await connection.getColumnSchema(schema);
11
- s = new ExistingSchema(columns, caseInsensitive);
12
- this.cache.set(schema, s);
13
- }
14
- if (caseInsensitive) {
15
- table = table.toLowerCase();
16
- }
17
- return s.tables.get(table) ?? [];
18
- }
19
-
20
- private static cache = new Map<string, ExistingSchema>();
21
-
22
- public tables = new Map<string,IColumnSchema[]>();
23
-
24
- constructor(columns: IColumnSchema[], caseInsensitive = false) {
25
- for (const c of columns) {
26
- let tableName = c.ownerName;
27
- if (caseInsensitive) {
28
- tableName = tableName.toLowerCase();
29
- }
30
- let table = this.tables.get(tableName);
31
- if (!table) {
32
- table = [];
33
- this.tables.set(tableName, table);
34
- }
35
- table.push(c);
36
- }
37
- }
38
-
39
- }