@mikro-orm/sql 7.0.0-dev.207 → 7.0.0-dev.208

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.
@@ -16,7 +16,7 @@ export declare class MySqlSchemaHelper extends SchemaHelper {
16
16
  enableForeignKeysSQL(): string;
17
17
  finalizeTable(table: DatabaseTable, charset: string, collate?: string): string;
18
18
  getListTablesSQL(): string;
19
- getListViewsSQL(schemaName?: string): string;
19
+ getListViewsSQL(): string;
20
20
  loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string): Promise<void>;
21
21
  loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[]): Promise<void>;
22
22
  getAllIndexes(connection: AbstractSqlConnection, tables: Table[]): Promise<Dictionary<IndexDef[]>>;
@@ -33,11 +33,11 @@ export class MySqlSchemaHelper extends SchemaHelper {
33
33
  getListTablesSQL() {
34
34
  return `select table_name as table_name, nullif(table_schema, schema()) as schema_name, table_comment as table_comment from information_schema.tables where table_type = 'BASE TABLE' and table_schema = schema()`;
35
35
  }
36
- getListViewsSQL(schemaName) {
36
+ getListViewsSQL() {
37
37
  return `select table_name as view_name, nullif(table_schema, schema()) as schema_name, view_definition from information_schema.views where table_schema = schema()`;
38
38
  }
39
39
  async loadViews(schema, connection, schemaName) {
40
- const views = await connection.execute(this.getListViewsSQL(schemaName));
40
+ const views = await connection.execute(this.getListViewsSQL());
41
41
  for (const view of views) {
42
42
  // MySQL information_schema.views.view_definition requires SHOW VIEW privilege
43
43
  // and may return NULL. Use SHOW CREATE VIEW as fallback.
@@ -17,6 +17,7 @@ export declare class PostgreSqlSchemaHelper extends SchemaHelper {
17
17
  getSchemaBeginning(charset: string, disableForeignKeys?: boolean): string;
18
18
  getCreateDatabaseSQL(name: string): string;
19
19
  getListTablesSQL(): string;
20
+ private getIgnoredViewsCondition;
20
21
  getListViewsSQL(): string;
21
22
  loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection): Promise<void>;
22
23
  getListMaterializedViewsSQL(): string;
@@ -1,5 +1,7 @@
1
- import { EnumType, Type, Utils, DeferMode } from '@mikro-orm/core';
1
+ import { DeferMode, EnumType, Type, Utils } from '@mikro-orm/core';
2
2
  import { SchemaHelper } from '../../schema/SchemaHelper.js';
3
+ /** PostGIS system views that should be automatically ignored */
4
+ const POSTGIS_VIEWS = ['geography_columns', 'geometry_columns'];
3
5
  export class PostgreSqlSchemaHelper extends SchemaHelper {
4
6
  static DEFAULT_VALUES = {
5
7
  'now()': ['now()', 'current_timestamp'],
@@ -29,10 +31,14 @@ export class PostgreSqlSchemaHelper extends SchemaHelper {
29
31
  + `and table_name not in (select inhrelid::regclass::text from pg_inherits) `
30
32
  + `order by table_name`;
31
33
  }
34
+ getIgnoredViewsCondition() {
35
+ return POSTGIS_VIEWS.map(v => `table_name != '${v}'`).join(' and ');
36
+ }
32
37
  getListViewsSQL() {
33
38
  return `select table_name as view_name, table_schema as schema_name, view_definition `
34
39
  + `from information_schema.views `
35
40
  + `where ${this.getIgnoredNamespacesConditionSQL('table_schema')} `
41
+ + `and ${this.getIgnoredViewsCondition()} `
36
42
  + `order by table_name`;
37
43
  }
38
44
  async loadViews(schema, connection) {
@@ -13,6 +13,7 @@ export declare class SqliteSchemaHelper extends SchemaHelper {
13
13
  getListTablesSQL(): string;
14
14
  getAllTables(connection: AbstractSqlConnection, schemas?: string[]): Promise<Table[]>;
15
15
  getNamespaces(connection: AbstractSqlConnection): Promise<string[]>;
16
+ private getIgnoredViewsCondition;
16
17
  getListViewsSQL(): string;
17
18
  loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string): Promise<void>;
18
19
  getDropDatabaseSQL(name: string): string;
@@ -1,5 +1,19 @@
1
1
  import { Utils } from '@mikro-orm/core';
2
2
  import { SchemaHelper } from '../../schema/SchemaHelper.js';
3
+ /** SpatiaLite system views that should be automatically ignored */
4
+ const SPATIALITE_VIEWS = [
5
+ 'geometry_columns',
6
+ 'spatial_ref_sys',
7
+ 'views_geometry_columns',
8
+ 'virts_geometry_columns',
9
+ 'geom_cols_ref_sys',
10
+ 'spatial_ref_sys_aux',
11
+ 'vector_layers',
12
+ 'vector_layers_auth',
13
+ 'vector_layers_field_infos',
14
+ 'vector_layers_statistics',
15
+ 'ElementaryGeometries',
16
+ ];
3
17
  export class SqliteSchemaHelper extends SchemaHelper {
4
18
  disableForeignKeysSQL() {
5
19
  return 'pragma foreign_keys = off;';
@@ -43,8 +57,11 @@ export class SqliteSchemaHelper extends SchemaHelper {
43
57
  async getNamespaces(connection) {
44
58
  return this.getDatabaseList(connection);
45
59
  }
60
+ getIgnoredViewsCondition() {
61
+ return SPATIALITE_VIEWS.map(v => `name != '${v}'`).join(' and ');
62
+ }
46
63
  getListViewsSQL() {
47
- return `select name as view_name, sql as view_definition from sqlite_master where type = 'view' order by name`;
64
+ return `select name as view_name, sql as view_definition from sqlite_master where type = 'view' and ${this.getIgnoredViewsCondition()} order by name`;
48
65
  }
49
66
  async loadViews(schema, connection, schemaName) {
50
67
  const databases = await this.getDatabaseList(connection);
@@ -62,7 +79,7 @@ export class SqliteSchemaHelper extends SchemaHelper {
62
79
  const targetDbs = schemaName ? [schemaName] : databases;
63
80
  for (const dbName of targetDbs) {
64
81
  const prefix = this.getSchemaPrefix(dbName);
65
- const views = await connection.execute(`select name as view_name, sql as view_definition from ${prefix}sqlite_master where type = 'view' order by name`);
82
+ const views = await connection.execute(`select name as view_name, sql as view_definition from ${prefix}sqlite_master where type = 'view' and ${this.getIgnoredViewsCondition()} order by name`);
66
83
  for (const view of views) {
67
84
  schema.addView(view.view_name, dbName, this.extractViewDefinition(view.view_definition));
68
85
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/sql",
3
- "version": "7.0.0-dev.207",
3
+ "version": "7.0.0-dev.208",
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
  "type": "module",
6
6
  "exports": {
@@ -56,6 +56,6 @@
56
56
  "@mikro-orm/core": "^6.6.4"
57
57
  },
58
58
  "peerDependencies": {
59
- "@mikro-orm/core": "7.0.0-dev.207"
59
+ "@mikro-orm/core": "7.0.0-dev.208"
60
60
  }
61
61
  }
@@ -40,11 +40,12 @@ export declare class DatabaseSchema {
40
40
  hasNamespace(namespace: string): boolean;
41
41
  hasNativeEnum(name: string): boolean;
42
42
  getNamespaces(): string[];
43
- static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, schemas?: string[], takeTables?: (string | RegExp)[], skipTables?: (string | RegExp)[]): Promise<DatabaseSchema>;
43
+ static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, schemas?: string[], takeTables?: (string | RegExp)[], skipTables?: (string | RegExp)[], skipViews?: (string | RegExp)[]): Promise<DatabaseSchema>;
44
44
  static fromMetadata(metadata: EntityMetadata[], platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, em?: any): DatabaseSchema;
45
45
  private static getViewDefinition;
46
46
  private static getSchemaName;
47
47
  private static matchName;
48
+ private static isNameAllowed;
48
49
  private static isTableNameAllowed;
49
50
  private static shouldHaveColumn;
50
51
  toJSON(): Dictionary;
@@ -75,7 +75,7 @@ export class DatabaseSchema {
75
75
  getNamespaces() {
76
76
  return [...this.namespaces];
77
77
  }
78
- static async create(connection, platform, config, schemaName, schemas, takeTables, skipTables) {
78
+ static async create(connection, platform, config, schemaName, schemas, takeTables, skipTables, skipViews) {
79
79
  const schema = new DatabaseSchema(platform, schemaName ?? config.get('schema') ?? platform.getDefaultSchemaName());
80
80
  const allTables = await platform.getSchemaHelper().getAllTables(connection, schemas);
81
81
  const parts = config.get('migrations').tableName.split('.');
@@ -89,6 +89,10 @@ export class DatabaseSchema {
89
89
  if (platform.supportsMaterializedViews()) {
90
90
  await platform.getSchemaHelper().loadMaterializedViews(schema, connection, schemaName);
91
91
  }
92
+ // Filter out skipped views
93
+ if (skipViews && skipViews.length > 0) {
94
+ schema.views = schema.views.filter(v => this.isNameAllowed(v.name, skipViews));
95
+ }
92
96
  return schema;
93
97
  }
94
98
  static fromMetadata(metadata, platform, config, schemaName, em) {
@@ -193,9 +197,12 @@ export class DatabaseSchema {
193
197
  ? name.toLocaleLowerCase() === nameToMatch.toLocaleLowerCase()
194
198
  : nameToMatch.test(name);
195
199
  }
200
+ static isNameAllowed(name, skipNames) {
201
+ return !(skipNames?.some(pattern => this.matchName(name, pattern)) ?? false);
202
+ }
196
203
  static isTableNameAllowed(tableName, takeTables, skipTables) {
197
204
  return ((takeTables?.some(tableNameToMatch => this.matchName(tableName, tableNameToMatch)) ?? true) &&
198
- !(skipTables?.some(tableNameToMatch => this.matchName(tableName, tableNameToMatch)) ?? false));
205
+ this.isNameAllowed(tableName, skipTables));
199
206
  }
200
207
  static shouldHaveColumn(meta, prop, skipColumns) {
201
208
  if (prop.persist === false || (prop.columnTypes?.length ?? 0) === 0) {
@@ -24,7 +24,7 @@ export declare abstract class SchemaHelper {
24
24
  getListTablesSQL(): string;
25
25
  getAllTables(connection: AbstractSqlConnection, schemas?: string[]): Promise<Table[]>;
26
26
  getListViewsSQL(): string;
27
- loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection): Promise<void>;
27
+ loadViews(schema: DatabaseSchema, connection: AbstractSqlConnection, schemaName?: string): Promise<void>;
28
28
  getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column, schemaName?: string): string;
29
29
  getCreateIndexSQL(tableName: string, index: IndexDef): string;
30
30
  getDropIndexSQL(tableName: string, index: IndexDef): string;
@@ -67,6 +67,7 @@ export declare abstract class SchemaHelper {
67
67
  createForeignKeyConstraints?: boolean;
68
68
  ignoreSchema?: string[];
69
69
  skipTables?: (string | RegExp)[];
70
+ skipViews?: (string | RegExp)[];
70
71
  skipColumns?: Dictionary<(string | RegExp)[]>;
71
72
  managementDbName?: string;
72
73
  defaultUpdateRule?: "cascade" | "no action" | "set null" | "set default" | "restrict";
@@ -71,7 +71,7 @@ export class SchemaHelper {
71
71
  getListViewsSQL() {
72
72
  throw new Error('Not supported by given driver');
73
73
  }
74
- async loadViews(schema, connection) {
74
+ async loadViews(schema, connection, schemaName) {
75
75
  throw new Error('Not supported by given driver');
76
76
  }
77
77
  getRenameColumnSQL(tableName, oldColumnName, to, schemaName) {
@@ -9,6 +9,7 @@ export declare class SqlSchemaGenerator extends AbstractSchemaGenerator<Abstract
9
9
  createForeignKeyConstraints?: boolean;
10
10
  ignoreSchema?: string[];
11
11
  skipTables?: (string | RegExp)[];
12
+ skipViews?: (string | RegExp)[];
12
13
  skipColumns?: Dictionary<(string | RegExp)[]>;
13
14
  managementDbName?: string;
14
15
  defaultUpdateRule?: "cascade" | "no action" | "set null" | "set default" | "restrict";
@@ -139,7 +139,7 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
139
139
  await this.ensureDatabase();
140
140
  const metadata = this.getOrderedMetadata(options.schema).reverse();
141
141
  const schemas = this.getTargetSchema(options.schema).getNamespaces();
142
- const schema = await DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas);
142
+ const schema = await DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas, undefined, this.options.skipTables, this.options.skipViews);
143
143
  const ret = [];
144
144
  // Drop views first (views may depend on tables)
145
145
  // Drop in reverse dependency order (dependent views first)
@@ -219,7 +219,7 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
219
219
  options.dropTables ??= true;
220
220
  const toSchema = this.getTargetSchema(options.schema);
221
221
  const schemas = toSchema.getNamespaces();
222
- const fromSchema = options.fromSchema ?? (await DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas, undefined, this.options.skipTables));
222
+ const fromSchema = options.fromSchema ?? (await DatabaseSchema.create(this.connection, this.platform, this.config, options.schema, schemas, undefined, this.options.skipTables, this.options.skipViews));
223
223
  const wildcardSchemaTables = [...this.metadata.getAll().values()].filter(meta => meta.schema === '*').map(meta => meta.tableName);
224
224
  fromSchema.prune(options.schema, wildcardSchemaTables);
225
225
  toSchema.prune(options.schema, wildcardSchemaTables);