@mikro-orm/postgresql 6.2.10-dev.8 → 6.2.10-dev.80

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.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.definePostgreSqlConfig = exports.PostgreSqlMikroORM = void 0;
3
+ exports.PostgreSqlMikroORM = void 0;
4
+ exports.definePostgreSqlConfig = definePostgreSqlConfig;
4
5
  const core_1 = require("@mikro-orm/core");
5
6
  const PostgreSqlDriver_1 = require("./PostgreSqlDriver");
6
7
  /**
@@ -26,4 +27,3 @@ exports.PostgreSqlMikroORM = PostgreSqlMikroORM;
26
27
  function definePostgreSqlConfig(options) {
27
28
  return (0, core_1.defineConfig)({ driver: PostgreSqlDriver_1.PostgreSqlDriver, ...options });
28
29
  }
29
- exports.definePostgreSqlConfig = definePostgreSqlConfig;
@@ -10,12 +10,6 @@ export declare class PostgreSqlPlatform extends AbstractSqlPlatform {
10
10
  usesCascadeStatement(): boolean;
11
11
  supportsNativeEnums(): boolean;
12
12
  supportsCustomPrimaryKeyNames(): boolean;
13
- /**
14
- * Postgres will complain if we try to batch update uniquely constrained property (moving the value from one entity to another).
15
- * This flag will result in postponing 1:1 updates (removing them from the batched query).
16
- * @see https://stackoverflow.com/questions/5403437/atomic-multi-row-update-with-a-unique-constraint
17
- */
18
- allowsUniqueBatchUpdates(): boolean;
19
13
  getCurrentTimestampSQL(length: number): string;
20
14
  getDateTimeTypeDeclarationSQL(column: {
21
15
  length?: number;
@@ -61,6 +55,12 @@ export declare class PostgreSqlPlatform extends AbstractSqlPlatform {
61
55
  supportsMultipleStatements(): boolean;
62
56
  marshallArray(values: string[]): string;
63
57
  unmarshallArray(value: string): string[];
58
+ getVarcharTypeDeclarationSQL(column: {
59
+ length?: number;
60
+ }): string;
61
+ getCharTypeDeclarationSQL(column: {
62
+ length?: number;
63
+ }): string;
64
64
  getBlobDeclarationSQL(): string;
65
65
  getJsonDeclarationSQL(): string;
66
66
  getSearchJsonPropertyKey(path: string[], type: string | undefined | Type, aliased: boolean, value?: unknown): string;
@@ -27,14 +27,6 @@ class PostgreSqlPlatform extends knex_1.AbstractSqlPlatform {
27
27
  supportsCustomPrimaryKeyNames() {
28
28
  return true;
29
29
  }
30
- /**
31
- * Postgres will complain if we try to batch update uniquely constrained property (moving the value from one entity to another).
32
- * This flag will result in postponing 1:1 updates (removing them from the batched query).
33
- * @see https://stackoverflow.com/questions/5403437/atomic-multi-row-update-with-a-unique-constraint
34
- */
35
- allowsUniqueBatchUpdates() {
36
- return false;
37
- }
38
30
  getCurrentTimestampSQL(length) {
39
31
  return `current_timestamp(${length})`;
40
32
  }
@@ -156,6 +148,18 @@ class PostgreSqlPlatform extends knex_1.AbstractSqlPlatform {
156
148
  /* istanbul ignore next */
157
149
  return value.substring(1, value.length - 1).split(',').map(v => v === `""` ? '' : v);
158
150
  }
151
+ getVarcharTypeDeclarationSQL(column) {
152
+ if (column.length === -1) {
153
+ return 'varchar';
154
+ }
155
+ return super.getVarcharTypeDeclarationSQL(column);
156
+ }
157
+ getCharTypeDeclarationSQL(column) {
158
+ if (column.length === -1) {
159
+ return 'char';
160
+ }
161
+ return super.getCharTypeDeclarationSQL(column);
162
+ }
159
163
  getBlobDeclarationSQL() {
160
164
  return 'bytea';
161
165
  }
@@ -242,6 +246,7 @@ class PostgreSqlPlatform extends knex_1.AbstractSqlPlatform {
242
246
  'bytea': 'blob',
243
247
  'jsonb': 'json',
244
248
  'character varying': 'varchar',
249
+ 'bpchar': 'character',
245
250
  };
246
251
  return super.getDefaultMappedType(map[normalizedType] ?? type);
247
252
  }
@@ -51,4 +51,5 @@ export declare class PostgreSqlSchemaHelper extends SchemaHelper {
51
51
  getChecks(connection: AbstractSqlConnection, tableName: string, schemaName: string, columns?: Column[]): Promise<CheckDef[]>;
52
52
  getColumns(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<Column[]>;
53
53
  getIndexes(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<IndexDef[]>;
54
+ inferLengthFromColumnType(type: string): number | undefined;
54
55
  }
@@ -26,6 +26,7 @@ class PostgreSqlSchemaHelper extends knex_1.SchemaHelper {
26
26
  + `from information_schema.tables `
27
27
  + `where ${this.getIgnoredNamespacesConditionSQL('table_schema')} `
28
28
  + `and table_name != 'geometry_columns' and table_name != 'spatial_ref_sys' and table_type != 'VIEW' `
29
+ + `and table_name not in (select inhrelid::regclass::text from pg_inherits) `
29
30
  + `order by table_name`;
30
31
  }
31
32
  async getNamespaces(connection) {
@@ -105,16 +106,17 @@ class PostgreSqlSchemaHelper extends knex_1.SchemaHelper {
105
106
  is_nullable,
106
107
  udt_name,
107
108
  coalesce(datetime_precision, character_maximum_length) length,
109
+ atttypmod custom_length,
108
110
  numeric_precision,
109
111
  numeric_scale,
110
112
  data_type,
111
113
  is_identity,
112
114
  identity_generation,
113
115
  generation_expression,
114
- (select pg_catalog.col_description(c.oid, cols.ordinal_position::int)
115
- from pg_catalog.pg_class c
116
- where c.oid = (select ('"' || cols.table_schema || '"."' || cols.table_name || '"')::regclass::oid) and c.relname = cols.table_name) as column_comment
116
+ pg_catalog.col_description(pgc.oid, cols.ordinal_position::int) column_comment
117
117
  from information_schema.columns cols
118
+ join pg_class pgc on cols.table_name = pgc.relname
119
+ join pg_attribute pga on pgc.oid = pga.attrelid and cols.column_name = pga.attname
118
120
  where (${tables.map(t => `(table_schema = ${this.platform.quoteValue(t.schema_name)} and table_name = ${this.platform.quoteValue(t.table_name)})`).join(' or ')})
119
121
  order by ordinal_position`;
120
122
  const allColumns = await connection.execute(sql);
@@ -128,17 +130,21 @@ class PostgreSqlSchemaHelper extends knex_1.SchemaHelper {
128
130
  let type = col.data_type.toLowerCase() === 'array'
129
131
  ? col.udt_name.replace(/^_(.*)$/, '$1[]')
130
132
  : col.udt_name;
133
+ if (type === 'vector' && col.length == null && col.custom_length != null) {
134
+ col.length = col.custom_length;
135
+ }
131
136
  if (col.length != null && !type.endsWith(`(${col.length})`)) {
132
137
  type += `(${col.length})`;
133
138
  }
134
139
  if (type === 'numeric' && col.numeric_precision != null && col.numeric_scale != null) {
135
140
  type += `(${col.numeric_precision},${col.numeric_scale})`;
136
141
  }
142
+ const length = this.inferLengthFromColumnType(type) === -1 ? -1 : col.length;
137
143
  const column = {
138
144
  name: col.column_name,
139
145
  type,
140
146
  mappedType,
141
- length: col.length,
147
+ length,
142
148
  precision: col.numeric_precision,
143
149
  scale: col.numeric_scale,
144
150
  nullable: col.is_nullable === 'YES',
@@ -178,7 +184,8 @@ class PostgreSqlSchemaHelper extends knex_1.SchemaHelper {
178
184
  async getAllForeignKeys(connection, tables) {
179
185
  const sql = `select nsp1.nspname schema_name, cls1.relname table_name, nsp2.nspname referenced_schema_name,
180
186
  cls2.relname referenced_table_name, a.attname column_name, af.attname referenced_column_name, conname constraint_name,
181
- confupdtype update_rule, confdeltype delete_rule, array_position(con.conkey,a.attnum) as ord, condeferrable, condeferred
187
+ confupdtype update_rule, confdeltype delete_rule, array_position(con.conkey,a.attnum) as ord, condeferrable, condeferred,
188
+ pg_get_constraintdef(con.oid) as constraint_def
182
189
  from pg_attribute a
183
190
  join pg_constraint con on con.conrelid = a.attrelid AND a.attnum = ANY (con.conkey)
184
191
  join pg_attribute af on af.attnum = con.confkey[array_position(con.conkey,a.attnum)] AND af.attrelid = con.confrelid
@@ -191,7 +198,11 @@ class PostgreSqlSchemaHelper extends knex_1.SchemaHelper {
191
198
  order by nsp1.nspname, cls1.relname, constraint_name, ord`;
192
199
  const allFks = await connection.execute(sql);
193
200
  const ret = {};
194
- function mapReferencialIntegrity(value) {
201
+ function mapReferentialIntegrity(value, def) {
202
+ const match = ['n', 'd'].includes(value) && def.match(/ON DELETE (SET (NULL|DEFAULT) \(.*?\))/);
203
+ if (match) {
204
+ return match[1];
205
+ }
195
206
  switch (value) {
196
207
  case 'r': return 'RESTRICT';
197
208
  case 'c': return 'CASCADE';
@@ -202,8 +213,8 @@ class PostgreSqlSchemaHelper extends knex_1.SchemaHelper {
202
213
  }
203
214
  }
204
215
  for (const fk of allFks) {
205
- fk.update_rule = mapReferencialIntegrity(fk.update_rule);
206
- fk.delete_rule = mapReferencialIntegrity(fk.delete_rule);
216
+ fk.update_rule = mapReferentialIntegrity(fk.update_rule, fk.constraint_def);
217
+ fk.delete_rule = mapReferentialIntegrity(fk.delete_rule, fk.constraint_def);
207
218
  if (fk.condeferrable) {
208
219
  fk.defer_mode = fk.condeferred ? core_1.DeferMode.INITIALLY_DEFERRED : core_1.DeferMode.INITIALLY_IMMEDIATE;
209
220
  }
@@ -268,7 +279,7 @@ class PostgreSqlSchemaHelper extends knex_1.SchemaHelper {
268
279
  const m1 = item.definition?.match(/check \(\(\("?(\w+)"?\)::/i) || item.definition?.match(/check \(\("?(\w+)"? = /i);
269
280
  const m2 = item.definition?.match(/\(array\[(.*)]\)/i) || item.definition?.match(/ = (.*)\)/i);
270
281
  if (item.columnName && m1 && m2) {
271
- const m3 = m2[1].match(/('[^']+'::text)/g);
282
+ const m3 = m2[1].match(/('[^']*'::text)/g);
272
283
  let items;
273
284
  /* istanbul ignore else */
274
285
  if (m3) {
@@ -277,7 +288,7 @@ class PostgreSqlSchemaHelper extends knex_1.SchemaHelper {
277
288
  else {
278
289
  items = m2[1].split(',').map((item) => item.trim().match(/^\(?'(.*)'/)?.[1]);
279
290
  }
280
- items = items.filter(Boolean);
291
+ items = items.filter(item => item !== undefined);
281
292
  if (items.length > 0) {
282
293
  o[item.columnName] = items;
283
294
  found.push(index);
@@ -494,5 +505,27 @@ class PostgreSqlSchemaHelper extends knex_1.SchemaHelper {
494
505
  const res = await this.getAllIndexes(connection, [{ table_name: tableName, schema_name: schemaName }]);
495
506
  return res[tableName];
496
507
  }
508
+ inferLengthFromColumnType(type) {
509
+ const match = type.match(/^(\w+(?:\s+\w+)*)\s*(?:\(\s*(\d+)\s*\)|$)/);
510
+ if (!match) {
511
+ return;
512
+ }
513
+ if (!match[2]) {
514
+ switch (match[1]) {
515
+ case 'character varying':
516
+ case 'varchar':
517
+ case 'bpchar':
518
+ case 'char':
519
+ case 'character':
520
+ return -1;
521
+ case 'interval':
522
+ case 'time':
523
+ case 'timestamptz':
524
+ return this.platform.getDefaultDateTimeLength();
525
+ }
526
+ return;
527
+ }
528
+ return +match[2];
529
+ }
497
530
  }
498
531
  exports.PostgreSqlSchemaHelper = PostgreSqlSchemaHelper;
package/index.mjs CHANGED
@@ -33,6 +33,7 @@ export const ChangeSet = mod.ChangeSet;
33
33
  export const ChangeSetComputer = mod.ChangeSetComputer;
34
34
  export const ChangeSetPersister = mod.ChangeSetPersister;
35
35
  export const ChangeSetType = mod.ChangeSetType;
36
+ export const CharacterType = mod.CharacterType;
36
37
  export const Check = mod.Check;
37
38
  export const CheckConstraintViolationException = mod.CheckConstraintViolationException;
38
39
  export const Collection = mod.Collection;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/postgresql",
3
- "version": "6.2.10-dev.8",
3
+ "version": "6.2.10-dev.80",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -58,8 +58,8 @@
58
58
  "access": "public"
59
59
  },
60
60
  "dependencies": {
61
- "@mikro-orm/knex": "6.2.10-dev.8",
62
- "pg": "8.11.5",
61
+ "@mikro-orm/knex": "6.2.10-dev.80",
62
+ "pg": "8.12.0",
63
63
  "postgres-array": "3.0.2",
64
64
  "postgres-date": "2.1.0",
65
65
  "postgres-interval": "4.0.2"
@@ -68,6 +68,6 @@
68
68
  "@mikro-orm/core": "^6.2.9"
69
69
  },
70
70
  "peerDependencies": {
71
- "@mikro-orm/core": "6.2.10-dev.8"
71
+ "@mikro-orm/core": "6.2.10-dev.80"
72
72
  }
73
73
  }