@strapi/database 4.25.19 → 4.25.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.
@@ -2,6 +2,7 @@ import type { Database } from '..';
2
2
  import type { Schema } from '../schema';
3
3
  export interface SchemaInspector {
4
4
  getSchema(): Promise<Schema>;
5
+ getTables(): Promise<string[]>;
5
6
  }
6
7
  export default class Dialect {
7
8
  db: Database;
@@ -10,6 +11,7 @@ export default class Dialect {
10
11
  constructor(db: Database, client: string);
11
12
  configure(): void;
12
13
  initialize(): void;
14
+ getTables(): void;
13
15
  getSqlType(type: unknown): unknown;
14
16
  canAlterConstraints(): boolean;
15
17
  usesForeignKeys(): boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"dialect.d.ts","sourceRoot":"","sources":["../../src/dialects/dialect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AACnC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAExC,MAAM,WAAW,eAAe;IAC9B,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CAC9B;AAED,MAAM,CAAC,OAAO,OAAO,OAAO;IAC1B,EAAE,EAAE,QAAQ,CAAC;IAEb,eAAe,EAAE,eAAe,CAAyB;IAEzD,MAAM,EAAE,MAAM,CAAC;gBAEH,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM;IAKxC,SAAS;IAET,UAAU;IAEV,UAAU,CAAC,IAAI,EAAE,OAAO;IAIxB,mBAAmB;IAInB,eAAe;IAIf,YAAY;IAIZ,gBAAgB;IAIhB,uBAAuB;IAIvB,gBAAgB,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO;IAKtC,iBAAiB;IAIjB,eAAe;IAIrB,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE;IAQlD,gBAAgB;CAGjB"}
1
+ {"version":3,"file":"dialect.d.ts","sourceRoot":"","sources":["../../src/dialects/dialect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AACnC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAExC,MAAM,WAAW,eAAe;IAC9B,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7B,SAAS,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;CAChC;AAED,MAAM,CAAC,OAAO,OAAO,OAAO;IAC1B,EAAE,EAAE,QAAQ,CAAC;IAEb,eAAe,EAAE,eAAe,CAAyB;IAEzD,MAAM,EAAE,MAAM,CAAC;gBAEH,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM;IAKxC,SAAS;IAET,UAAU;IAEV,SAAS;IAIT,UAAU,CAAC,IAAI,EAAE,OAAO;IAIxB,mBAAmB;IAInB,eAAe;IAIf,YAAY;IAIZ,gBAAgB;IAIhB,uBAAuB;IAIvB,gBAAgB,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO;IAKtC,iBAAiB;IAIjB,eAAe;IAIrB,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE;IAQlD,gBAAgB;CAGjB"}
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ import { MigrationProvider } from './migrations';
7
7
  import { LifecycleProvider } from './lifecycles';
8
8
  import * as errors from './errors';
9
9
  import { Callback, TransactionObject } from './transaction-context';
10
+ import { type RepairManager } from './repairs';
10
11
  import { Model } from './types';
11
12
  export { isKnexQuery } from './utils/knex';
12
13
  interface Settings {
@@ -14,10 +15,12 @@ interface Settings {
14
15
  runMigrations?: boolean;
15
16
  [key: string]: unknown;
16
17
  }
18
+ export type Logger = Record<'info' | 'warn' | 'error' | 'debug', (message: string | Record<string, unknown>) => void>;
17
19
  export interface DatabaseConfig {
18
20
  connection: Knex.Config;
19
21
  settings: Settings;
20
22
  models: Model[];
23
+ logger?: Logger;
21
24
  }
22
25
  declare class Database {
23
26
  connection: Knex;
@@ -28,6 +31,7 @@ declare class Database {
28
31
  migrations: MigrationProvider;
29
32
  lifecycles: LifecycleProvider;
30
33
  entityManager: EntityManager;
34
+ repair: RepairManager;
31
35
  static transformContentTypes: (contentTypes: import("./utils/content-types").ContentType[]) => Model[];
32
36
  static init(config: DatabaseConfig): Promise<Database>;
33
37
  constructor(config: DatabaseConfig);
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAEjC,OAAO,EAAE,OAAO,EAAc,MAAM,YAAY,CAAC;AACjD,OAAO,EAAwB,cAAc,EAAE,MAAM,UAAU,CAAC;AAChE,OAAO,EAAkB,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAuB,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAA4B,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC3E,OAAO,EAA4B,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAE3E,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAkB,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAKpF,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,UAAU,QAAQ;IAChB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,cAAc;IAC7B,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC;IACxB,QAAQ,EAAE,QAAQ,CAAC;IACnB,MAAM,EAAE,KAAK,E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IA6BlC,KAAK,CAAC,GAAG,EAAE,MAAM;IAQjB,aAAa;IAIb,WAAW,IAAI,OAAO,CAAC,iBAAiB,CAAC;IACzC,WAAW,CAAC,SAAS,SAAS,QAAQ,EAAE,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;IA4CrF,aAAa,IAAI,MAAM,GAAG,SAAS;IAInC,aAAa,IAAI,IAAI;IACrB,aAAa,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC,YAAY;IAOpD,mBAAmB,CAAC,GAAG,mBAAkB;IAKzC,YAAY,CAAC,GAAG,EAAE,MAAM;IAIlB,OAAO;CAId;AAED,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC"}
package/dist/index.js CHANGED
@@ -62,6 +62,9 @@ class Dialect {
62
62
  }
63
63
  initialize() {
64
64
  }
65
+ getTables() {
66
+ throw new Error("getTables not implemented for this dialect");
67
+ }
65
68
  getSqlType(type) {
66
69
  return type;
67
70
  }
@@ -1017,7 +1020,7 @@ const getDialect = (db) => {
1017
1020
  const dialect = new constructor(db, dialectName);
1018
1021
  return dialect;
1019
1022
  };
1020
- const debug$1 = createDebug__default.default("strapi::database");
1023
+ const debug$2 = createDebug__default.default("strapi::database");
1021
1024
  const createSchemaBuilder = (db) => {
1022
1025
  const helpers2 = createHelpers(db);
1023
1026
  return {
@@ -1043,12 +1046,12 @@ const createSchemaBuilder = (db) => {
1043
1046
  */
1044
1047
  async createTables(tables, trx) {
1045
1048
  for (const table of tables) {
1046
- debug$1(`Creating table: ${table.name}`);
1049
+ debug$2(`Creating table: ${table.name}`);
1047
1050
  const schemaBuilder = this.getSchemaBuilder(trx);
1048
1051
  await helpers2.createTable(schemaBuilder, table);
1049
1052
  }
1050
1053
  for (const table of tables) {
1051
- debug$1(`Creating table foreign keys: ${table.name}`);
1054
+ debug$2(`Creating table foreign keys: ${table.name}`);
1052
1055
  const schemaBuilder = this.getSchemaBuilder(trx);
1053
1056
  await helpers2.createTableForeignKeys(schemaBuilder, table);
1054
1057
  }
@@ -1079,18 +1082,18 @@ const createSchemaBuilder = (db) => {
1079
1082
  await this.createTables(schemaDiff.tables.added, trx);
1080
1083
  if (forceMigration) {
1081
1084
  for (const table of schemaDiff.tables.removed) {
1082
- debug$1(`Removing table foreign keys: ${table.name}`);
1085
+ debug$2(`Removing table foreign keys: ${table.name}`);
1083
1086
  const schemaBuilder = this.getSchemaBuilder(trx);
1084
1087
  await helpers2.dropTableForeignKeys(schemaBuilder, table);
1085
1088
  }
1086
1089
  for (const table of schemaDiff.tables.removed) {
1087
- debug$1(`Removing table: ${table.name}`);
1090
+ debug$2(`Removing table: ${table.name}`);
1088
1091
  const schemaBuilder = this.getSchemaBuilder(trx);
1089
1092
  await helpers2.dropTable(schemaBuilder, table);
1090
1093
  }
1091
1094
  }
1092
1095
  for (const table of schemaDiff.tables.updated) {
1093
- debug$1(`Updating table: ${table.name}`);
1096
+ debug$2(`Updating table: ${table.name}`);
1094
1097
  const schemaBuilder = this.getSchemaBuilder(trx);
1095
1098
  await helpers2.alterTable(schemaBuilder, table);
1096
1099
  }
@@ -1186,27 +1189,27 @@ const createHelpers = (db) => {
1186
1189
  const alterTable = async (schemaBuilder, table) => {
1187
1190
  await schemaBuilder.alterTable(table.name, (tableBuilder) => {
1188
1191
  for (const removedIndex of table.indexes.removed) {
1189
- debug$1(`Dropping index ${removedIndex.name}`);
1192
+ debug$2(`Dropping index ${removedIndex.name}`);
1190
1193
  dropIndex(tableBuilder, removedIndex);
1191
1194
  }
1192
1195
  for (const updateddIndex of table.indexes.updated) {
1193
- debug$1(`Dropping updated index ${updateddIndex.name}`);
1196
+ debug$2(`Dropping updated index ${updateddIndex.name}`);
1194
1197
  dropIndex(tableBuilder, updateddIndex.object);
1195
1198
  }
1196
1199
  for (const removedForeignKey of table.foreignKeys.removed) {
1197
- debug$1(`Dropping foreign key ${removedForeignKey.name}`);
1200
+ debug$2(`Dropping foreign key ${removedForeignKey.name}`);
1198
1201
  dropForeignKey(tableBuilder, removedForeignKey);
1199
1202
  }
1200
1203
  for (const updatedForeignKey of table.foreignKeys.updated) {
1201
- debug$1(`Dropping updated foreign key ${updatedForeignKey.name}`);
1204
+ debug$2(`Dropping updated foreign key ${updatedForeignKey.name}`);
1202
1205
  dropForeignKey(tableBuilder, updatedForeignKey.object);
1203
1206
  }
1204
1207
  for (const removedColumn of table.columns.removed) {
1205
- debug$1(`Dropping column ${removedColumn.name}`);
1208
+ debug$2(`Dropping column ${removedColumn.name}`);
1206
1209
  dropColumn(tableBuilder, removedColumn);
1207
1210
  }
1208
1211
  for (const updatedColumn of table.columns.updated) {
1209
- debug$1(`Updating column ${updatedColumn.name}`);
1212
+ debug$2(`Updating column ${updatedColumn.name}`);
1210
1213
  const { object } = updatedColumn;
1211
1214
  if (object.type === "increments") {
1212
1215
  createColumn2(tableBuilder, { ...object, type: "integer" }).alter();
@@ -1215,15 +1218,15 @@ const createHelpers = (db) => {
1215
1218
  }
1216
1219
  }
1217
1220
  for (const updatedForeignKey of table.foreignKeys.updated) {
1218
- debug$1(`Recreating updated foreign key ${updatedForeignKey.name}`);
1221
+ debug$2(`Recreating updated foreign key ${updatedForeignKey.name}`);
1219
1222
  createForeignKey(tableBuilder, updatedForeignKey.object);
1220
1223
  }
1221
1224
  for (const updatedIndex of table.indexes.updated) {
1222
- debug$1(`Recreating updated index ${updatedIndex.name}`);
1225
+ debug$2(`Recreating updated index ${updatedIndex.name}`);
1223
1226
  createIndex(tableBuilder, updatedIndex.object);
1224
1227
  }
1225
1228
  for (const addedColumn of table.columns.added) {
1226
- debug$1(`Creating column ${addedColumn.name}`);
1229
+ debug$2(`Creating column ${addedColumn.name}`);
1227
1230
  if (addedColumn.type === "increments" && !db.dialect.canAddIncrements()) {
1228
1231
  tableBuilder.integer(addedColumn.name).unsigned();
1229
1232
  tableBuilder.primary([addedColumn.name]);
@@ -1232,11 +1235,11 @@ const createHelpers = (db) => {
1232
1235
  }
1233
1236
  }
1234
1237
  for (const addedForeignKey of table.foreignKeys.added) {
1235
- debug$1(`Creating foreign keys ${addedForeignKey.name}`);
1238
+ debug$2(`Creating foreign keys ${addedForeignKey.name}`);
1236
1239
  createForeignKey(tableBuilder, addedForeignKey);
1237
1240
  }
1238
1241
  for (const addedIndex of table.indexes.added) {
1239
- debug$1(`Creating index ${addedIndex.name}`);
1242
+ debug$2(`Creating index ${addedIndex.name}`);
1240
1243
  createIndex(tableBuilder, addedIndex);
1241
1244
  }
1242
1245
  });
@@ -1822,7 +1825,7 @@ const metadataToSchema = (metadata) => {
1822
1825
  });
1823
1826
  return schema;
1824
1827
  };
1825
- const debug = createDebug__default.default("strapi::database");
1828
+ const debug$1 = createDebug__default.default("strapi::database");
1826
1829
  const createSchemaProvider = (db) => {
1827
1830
  const schema = metadataToSchema(db.metadata);
1828
1831
  return {
@@ -1833,7 +1836,7 @@ const createSchemaProvider = (db) => {
1833
1836
  * Drops the database schema
1834
1837
  */
1835
1838
  async drop() {
1836
- debug("Dropping database schema");
1839
+ debug$1("Dropping database schema");
1837
1840
  const DBSchema = await db.dialect.schemaInspector.getSchema();
1838
1841
  await this.builder.dropSchema(DBSchema);
1839
1842
  },
@@ -1841,47 +1844,49 @@ const createSchemaProvider = (db) => {
1841
1844
  * Creates the database schema
1842
1845
  */
1843
1846
  async create() {
1844
- debug("Created database schema");
1847
+ debug$1("Created database schema");
1845
1848
  await this.builder.createSchema(schema);
1846
1849
  },
1847
1850
  /**
1848
1851
  * Resets the database schema
1849
1852
  */
1850
1853
  async reset() {
1851
- debug("Resetting database schema");
1854
+ debug$1("Resetting database schema");
1852
1855
  await this.drop();
1853
1856
  await this.create();
1854
1857
  },
1855
1858
  async syncSchema() {
1856
- debug("Synchronizing database schema");
1859
+ debug$1("Synchronizing database schema");
1857
1860
  const DBSchema = await db.dialect.schemaInspector.getSchema();
1858
1861
  const { status, diff } = await this.schemaDiff.diff(DBSchema, schema);
1859
1862
  if (status === "CHANGED") {
1860
1863
  await this.builder.updateSchema(diff);
1861
1864
  }
1862
1865
  await this.schemaStorage.add(schema);
1866
+ return status;
1863
1867
  },
1864
1868
  // TODO: support options to migrate softly or forcefully
1865
1869
  // TODO: support option to disable auto migration & run a CLI command instead to avoid doing it at startup
1866
1870
  // TODO: Allow keeping extra indexes / extra tables / extra columns (globally or on a per table basis)
1867
1871
  async sync() {
1868
1872
  if (await db.migrations.shouldRun()) {
1869
- debug("Found migrations to run");
1873
+ debug$1("Found migrations to run");
1870
1874
  await db.migrations.up();
1871
1875
  return this.syncSchema();
1872
1876
  }
1873
1877
  const oldSchema = await this.schemaStorage.read();
1874
1878
  if (!oldSchema) {
1875
- debug("Schema not persisted yet");
1879
+ debug$1("Schema not persisted yet");
1876
1880
  return this.syncSchema();
1877
1881
  }
1878
1882
  const { hash: oldHash } = oldSchema;
1879
1883
  const hash = await this.schemaStorage.hashSchema(schema);
1880
1884
  if (oldHash !== hash) {
1881
- debug("Schema changed");
1885
+ debug$1("Schema changed");
1882
1886
  return this.syncSchema();
1883
1887
  }
1884
- debug("Schema unchanged");
1888
+ debug$1("Schema unchanged");
1889
+ return "UNCHANGED";
1885
1890
  }
1886
1891
  };
1887
1892
  };
@@ -5989,6 +5994,7 @@ const createLifecyclesProvider = (db) => {
5989
5994
  timestampsLifecyclesSubscriber,
5990
5995
  modelsLifecyclesSubscriber
5991
5996
  ];
5997
+ let isLifecycleHooksDisabled = false;
5992
5998
  return {
5993
5999
  subscribe(subscriber) {
5994
6000
  assert.strict(
@@ -5998,6 +6004,12 @@ const createLifecyclesProvider = (db) => {
5998
6004
  subscribers.push(subscriber);
5999
6005
  return () => subscribers.splice(subscribers.indexOf(subscriber), 1);
6000
6006
  },
6007
+ disable() {
6008
+ isLifecycleHooksDisabled = true;
6009
+ },
6010
+ enable() {
6011
+ isLifecycleHooksDisabled = false;
6012
+ },
6001
6013
  clear() {
6002
6014
  subscribers = [];
6003
6015
  },
@@ -6017,6 +6029,8 @@ const createLifecyclesProvider = (db) => {
6017
6029
  * @param {Map<any, any>} states
6018
6030
  */
6019
6031
  async run(action, uid, properties, states = /* @__PURE__ */ new Map()) {
6032
+ if (isLifecycleHooksDisabled)
6033
+ return states;
6020
6034
  for (let i = 0; i < subscribers.length; i += 1) {
6021
6035
  const subscriber = subscribers[i];
6022
6036
  if (typeof subscriber === "function") {
@@ -6082,6 +6096,61 @@ const createConnection = (config) => {
6082
6096
  }
6083
6097
  return knex__default.default(knexConfig);
6084
6098
  };
6099
+ const debug = createDebug__default.default("strapi::database");
6100
+ const isMorphRelationWithPivot = (attribute, pivot) => {
6101
+ return attribute.type === "relation" && "relation" in attribute && "joinTable" in attribute && "name" in attribute.joinTable && "pivotColumns" in attribute.joinTable && attribute.joinTable.pivotColumns.includes(pivot);
6102
+ };
6103
+ const filterMorphRelationalAttributes = (attributes, pivot) => {
6104
+ return Object.values(attributes).filter(
6105
+ (attribute) => isMorphRelationWithPivot(attribute, pivot)
6106
+ );
6107
+ };
6108
+ const removeOrphanMorphType = async (db, { pivot }) => {
6109
+ debug(`Removing orphaned morph type: ${JSON.stringify(pivot)}`);
6110
+ const mdValues = db.metadata.values();
6111
+ for (const model of mdValues) {
6112
+ const attributes = filterMorphRelationalAttributes(model.attributes || {}, pivot);
6113
+ for (const attribute of attributes) {
6114
+ const joinTableName = attribute.joinTable.name;
6115
+ const morphTypes = await db.connection(joinTableName).distinct(pivot).pluck(pivot);
6116
+ for (const morphType of morphTypes) {
6117
+ const deleteComponentType = await (async () => {
6118
+ try {
6119
+ return !db.metadata.get(morphType);
6120
+ } catch {
6121
+ debug(`Metadata for morph type "${morphType}" in table "${joinTableName}" not found`);
6122
+ return true;
6123
+ }
6124
+ })();
6125
+ if (deleteComponentType) {
6126
+ debug(`Removing invalid morph type "${morphType}" from table "${joinTableName}".`);
6127
+ try {
6128
+ await db.connection(joinTableName).where(pivot, morphType).del();
6129
+ } catch (error) {
6130
+ const errorMessage = error instanceof Error ? error.message : String(error);
6131
+ debug(
6132
+ `Failed to remove invalid morph type "${morphType}" from table "${joinTableName}": ${errorMessage}`
6133
+ );
6134
+ }
6135
+ }
6136
+ }
6137
+ }
6138
+ }
6139
+ };
6140
+ const asyncCurry = (fn) => {
6141
+ const curried = (...args) => {
6142
+ if (args.length >= fn.length) {
6143
+ return fn(...args);
6144
+ }
6145
+ return (...moreArgs) => curried(...args, ...moreArgs);
6146
+ };
6147
+ return curried;
6148
+ };
6149
+ const createRepairManager = (db) => {
6150
+ return {
6151
+ removeOrphanMorphType: asyncCurry(removeOrphanMorphType)(db)
6152
+ };
6153
+ };
6085
6154
  const transformAttribute = (attribute) => {
6086
6155
  switch (attribute.type) {
6087
6156
  case "media": {
@@ -6179,6 +6248,7 @@ class Database {
6179
6248
  migrations;
6180
6249
  lifecycles;
6181
6250
  entityManager;
6251
+ repair;
6182
6252
  static transformContentTypes = transformContentTypes;
6183
6253
  static async init(config) {
6184
6254
  const db = new Database(config);
@@ -6203,6 +6273,7 @@ class Database {
6203
6273
  this.migrations = createMigrationsProvider(this);
6204
6274
  this.lifecycles = createLifecyclesProvider(this);
6205
6275
  this.entityManager = createEntityManager(this);
6276
+ this.repair = createRepairManager(this);
6206
6277
  }
6207
6278
  query(uid) {
6208
6279
  if (!this.metadata.has(uid)) {