@mikro-orm/sql 7.0.7-dev.2 → 7.0.7-dev.21

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.
@@ -89,16 +89,9 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
89
89
  this.append(ret, fks, true);
90
90
  }
91
91
  }
92
- // Create views after tables (views may depend on tables)
93
- // Sort views by dependencies (views depending on other views come later)
94
92
  const sortedViews = this.sortViewsByDependencies(toSchema.getViews());
95
93
  for (const view of sortedViews) {
96
- if (view.materialized) {
97
- this.append(ret, this.helper.createMaterializedView(view.name, view.schema, view.definition, view.withData ?? true));
98
- }
99
- else {
100
- this.append(ret, this.helper.createView(view.name, view.schema, view.definition), true);
101
- }
94
+ this.appendViewCreation(ret, view);
102
95
  }
103
96
  return this.wrapSchema(ret, options);
104
97
  }
@@ -185,8 +178,8 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
185
178
  this.append(ret, this.helper.dropTableIfExists(meta.tableName, this.getSchemaName(meta, options)));
186
179
  }
187
180
  if (this.platform.supportsNativeEnums()) {
188
- for (const columnName of Object.keys(schema.getNativeEnums())) {
189
- const sql = this.helper.getDropNativeEnumSQL(columnName, options.schema ?? this.config.get('schema'));
181
+ for (const enumOptions of Object.values(schema.getNativeEnums())) {
182
+ const sql = this.helper.getDropNativeEnumSQL(enumOptions.name, this.getSchemaName(enumOptions, options));
190
183
  this.append(ret, sql);
191
184
  }
192
185
  }
@@ -338,27 +331,14 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
338
331
  this.append(ret, sql);
339
332
  }
340
333
  }
341
- // Create new views after all table changes are done
342
- // Sort views by dependencies (views depending on other views come later)
343
334
  const sortedNewViews = this.sortViewsByDependencies(Object.values(schemaDiff.newViews));
344
335
  for (const view of sortedNewViews) {
345
- if (view.materialized) {
346
- this.append(ret, this.helper.createMaterializedView(view.name, view.schema, view.definition, view.withData ?? true));
347
- }
348
- else {
349
- this.append(ret, this.helper.createView(view.name, view.schema, view.definition), true);
350
- }
336
+ this.appendViewCreation(ret, view);
351
337
  }
352
- // Recreate changed views (also sorted by dependencies)
353
338
  const changedViews = Object.values(schemaDiff.changedViews).map(v => v.to);
354
339
  const sortedChangedViews = this.sortViewsByDependencies(changedViews);
355
340
  for (const view of sortedChangedViews) {
356
- if (view.materialized) {
357
- this.append(ret, this.helper.createMaterializedView(view.name, view.schema, view.definition, view.withData ?? true));
358
- }
359
- else {
360
- this.append(ret, this.helper.createView(view.name, view.schema, view.definition), true);
361
- }
341
+ this.appendViewCreation(ret, view);
362
342
  }
363
343
  return this.wrapSchema(ret, options);
364
344
  }
@@ -511,6 +491,22 @@ export class SqlSchemaGenerator extends AbstractSchemaGenerator {
511
491
  // Sort and map back to views
512
492
  return calc.sort().map(index => indexToView.get(index));
513
493
  }
494
+ appendViewCreation(ret, view) {
495
+ if (view.materialized) {
496
+ this.append(ret, this.helper.createMaterializedView(view.name, view.schema, view.definition, view.withData ?? true));
497
+ // Skip indexes for WITH NO DATA views — they have no data to index yet.
498
+ // Indexes will be created on the next schema:update after REFRESH populates data.
499
+ if (view.withData !== false) {
500
+ const viewName = this.helper.getTableName(view.name, view.schema);
501
+ for (const index of view.indexes ?? []) {
502
+ this.append(ret, this.helper.getCreateIndexSQL(viewName, index));
503
+ }
504
+ }
505
+ }
506
+ else {
507
+ this.append(ret, this.helper.createView(view.name, view.schema, view.definition), true);
508
+ }
509
+ }
514
510
  escapeRegExp(string) {
515
511
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
516
512
  }
package/typings.d.ts CHANGED
@@ -142,6 +142,8 @@ export interface DatabaseView {
142
142
  materialized?: boolean;
143
143
  /** For materialized views, whether data was populated on creation. */
144
144
  withData?: boolean;
145
+ /** Indexes on the materialized view. Only materialized views support indexes. */
146
+ indexes?: IndexDef[];
145
147
  }
146
148
  export interface SchemaDifference {
147
149
  newNamespaces: Set<string>;
@@ -250,8 +252,9 @@ export type MapTableName<T extends {
250
252
  }, TOptions extends MikroKyselyPluginOptions = {}> = {
251
253
  [P in T as TOptions['tableNamingStrategy'] extends 'entity' ? P['name'] : PreferStringLiteral<NonNullable<P['tableName']>, P['name']>]: P;
252
254
  };
255
+ type ResolveTableNaming<TOptions extends MikroKyselyPluginOptions> = TOptions['tableNamingStrategy'] extends 'entity' ? 'entity' : 'underscore';
253
256
  export type MapValueAsTable<TMap extends Record<string, any>, TOptions extends MikroKyselyPluginOptions = {}> = {
254
- [K in keyof TMap as TransformName<K, TOptions['tableNamingStrategy'] extends 'entity' ? 'entity' : 'underscore'>]: InferKyselyTable<TMap[K], TOptions>;
257
+ [K in keyof TMap as TransformName<K, ResolveTableNaming<TOptions>>]: InferKyselyTable<TMap[K], TOptions>;
255
258
  };
256
259
  export type InferKyselyTable<TSchema extends EntitySchemaWithMeta, TOptions extends MikroKyselyPluginOptions = {}> = ExcludeNever<{
257
260
  -readonly [K in keyof InferEntityProperties<TSchema> as TransformColumnName<K, TOptions['columnNamingStrategy'] extends 'property' ? 'property' : 'underscore', MaybeReturnType<InferEntityProperties<TSchema>[K]>>]: InferColumnValue<MaybeReturnType<InferEntityProperties<TSchema>[K]>, TOptions['processOnCreateHooks'] extends true ? true : false>;
@@ -318,7 +321,9 @@ export type InferClassEntityDB<TEntities, TOptions extends MikroKyselyPluginOpti
318
321
  type ClassEntityDBMap<TEntities, TOptions extends MikroKyselyPluginOptions = {}> = {
319
322
  [T in TEntities as ClassEntityTableName<T, TOptions>]: ClassEntityColumns<T, TOptions>;
320
323
  };
321
- type ClassEntityTableName<T, TOptions extends MikroKyselyPluginOptions = {}> = T extends abstract new (...args: any[]) => infer Instance ? TransformName<InferEntityName<Instance>, TOptions['tableNamingStrategy'] extends 'entity' ? 'entity' : 'underscore'> : never;
324
+ type ClassEntityTableName<T, TOptions extends MikroKyselyPluginOptions = {}> = T extends {
325
+ '~entityName'?: infer Name extends string;
326
+ } ? TransformName<Name, ResolveTableNaming<TOptions>> : T extends abstract new (...args: any[]) => infer Instance ? TransformName<InferEntityName<Instance>, ResolveTableNaming<TOptions>> : never;
322
327
  type ClassEntityColumns<T, TOptions extends MikroKyselyPluginOptions = {}> = T extends abstract new (...args: any[]) => infer Instance ? {
323
328
  [K in keyof Instance as ClassEntityColumnName<K, Instance[K], TOptions>]: ClassEntityColumnValue<Instance[K]>;
324
329
  } : never;