@strapi/database 5.0.0-rc.9 → 5.0.1

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 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entity-manager/index.ts"],"names":[],"mappings":"AA0CA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAGnC,OAAO,EAAE,aAAa,EAAsB,MAAM,SAAS,CAAC;AAE5D,cAAc,SAAS,CAAC;AAuLxB,eAAO,MAAM,mBAAmB,OAAQ,QAAQ,KAAG,aA2kClD,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entity-manager/index.ts"],"names":[],"mappings":"AA0CA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAGnC,OAAO,EAAE,aAAa,EAAsB,MAAM,SAAS,CAAC;AAE5D,cAAc,SAAS,CAAC;AAyLxB,eAAO,MAAM,mBAAmB,OAAQ,QAAQ,KAAG,aA2kClD,CAAC"}
package/dist/index.js CHANGED
@@ -1184,10 +1184,6 @@ const createHelpers = (db) => {
1184
1184
  debug$2(`Dropping updated foreign key ${updatedForeignKey.name} on ${table.name}`);
1185
1185
  dropForeignKey(tableBuilder, updatedForeignKey.object);
1186
1186
  }
1187
- for (const removedColumn of table.columns.removed) {
1188
- debug$2(`Dropping column ${removedColumn.name} on ${table.name}`);
1189
- dropColumn(tableBuilder, removedColumn);
1190
- }
1191
1187
  const isMySQL = db.config.connection.client === "mysql";
1192
1188
  const ignoreForeignKeyNames = isMySQL ? [
1193
1189
  ...table.foreignKeys.removed.map((fk) => fk.name),
@@ -1205,6 +1201,10 @@ const createHelpers = (db) => {
1205
1201
  dropIndex2(tableBuilder, updatedIndex.object);
1206
1202
  }
1207
1203
  }
1204
+ for (const removedColumn of table.columns.removed) {
1205
+ debug$2(`Dropping column ${removedColumn.name} on ${table.name}`);
1206
+ dropColumn(tableBuilder, removedColumn);
1207
+ }
1208
1208
  for (const updatedColumn of table.columns.updated) {
1209
1209
  debug$2(`Updating column ${updatedColumn.name} on ${table.name}`);
1210
1210
  const { object } = updatedColumn;
@@ -1214,14 +1214,6 @@ const createHelpers = (db) => {
1214
1214
  createColumn2(tableBuilder, object).alter();
1215
1215
  }
1216
1216
  }
1217
- for (const updatedForeignKey of table.foreignKeys.updated) {
1218
- debug$2(`Recreating updated foreign key ${updatedForeignKey.name} on ${table.name}`);
1219
- createForeignKey(tableBuilder, updatedForeignKey.object);
1220
- }
1221
- for (const updatedIndex of table.indexes.updated) {
1222
- debug$2(`Recreating updated index ${updatedIndex.name} on ${table.name}`);
1223
- createIndex(tableBuilder, updatedIndex.object);
1224
- }
1225
1217
  for (const addedColumn of table.columns.added) {
1226
1218
  debug$2(`Creating column ${addedColumn.name} on ${table.name}`);
1227
1219
  if (addedColumn.type === "increments" && !db.dialect.canAddIncrements()) {
@@ -1231,6 +1223,14 @@ const createHelpers = (db) => {
1231
1223
  createColumn2(tableBuilder, addedColumn);
1232
1224
  }
1233
1225
  }
1226
+ for (const updatedForeignKey of table.foreignKeys.updated) {
1227
+ debug$2(`Recreating updated foreign key ${updatedForeignKey.name} on ${table.name}`);
1228
+ createForeignKey(tableBuilder, updatedForeignKey.object);
1229
+ }
1230
+ for (const updatedIndex of table.indexes.updated) {
1231
+ debug$2(`Recreating updated index ${updatedIndex.name} on ${table.name}`);
1232
+ createIndex(tableBuilder, updatedIndex.object);
1233
+ }
1234
1234
  for (const addedForeignKey of table.foreignKeys.added) {
1235
1235
  debug$2(`Creating foreign keys ${addedForeignKey.name} on ${table.name}`);
1236
1236
  createForeignKey(tableBuilder, addedForeignKey);
@@ -2293,7 +2293,7 @@ const createOneToOne = (attributeName, attribute, meta, metadata) => {
2293
2293
  }
2294
2294
  };
2295
2295
  const createOneToMany = (attributeName, attribute, meta, metadata) => {
2296
- if (!isBidirectional(attribute)) {
2296
+ if (shouldUseJoinTable(attribute) && !isBidirectional(attribute)) {
2297
2297
  createJoinTable(metadata, {
2298
2298
  attribute,
2299
2299
  attributeName,
@@ -2322,7 +2322,7 @@ const createManyToOne = (attributeName, attribute, meta, metadata) => {
2322
2322
  }
2323
2323
  };
2324
2324
  const createManyToMany = (attributeName, attribute, meta, metadata) => {
2325
- if (!isBidirectional(attribute) || isOwner(attribute)) {
2325
+ if (shouldUseJoinTable(attribute) && (!isBidirectional(attribute) || isOwner(attribute))) {
2326
2326
  createJoinTable(metadata, {
2327
2327
  attribute,
2328
2328
  attributeName,
@@ -2347,7 +2347,7 @@ const createMorphToOne = (attributeName, attribute) => {
2347
2347
  });
2348
2348
  };
2349
2349
  const createMorphToMany = (attributeName, attribute, meta, metadata) => {
2350
- if ("joinTable" in attribute && attribute.joinTable) {
2350
+ if ("joinTable" in attribute && attribute.joinTable && !attribute.joinTable.__internal__) {
2351
2351
  return;
2352
2352
  }
2353
2353
  const joinTableName = identifiers.getMorphTableName(meta.tableName, attributeName);
@@ -2417,6 +2417,7 @@ const createMorphToMany = (attributeName, attribute, meta, metadata) => {
2417
2417
  columnToAttribute: {}
2418
2418
  });
2419
2419
  const joinTable = {
2420
+ __internal__: true,
2420
2421
  name: joinTableName,
2421
2422
  joinColumn: {
2422
2423
  name: joinColumnName,
@@ -2482,11 +2483,14 @@ const createJoinColumn = (metadata, { attribute, attributeName }) => {
2482
2483
  }
2483
2484
  };
2484
2485
  const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2486
+ if (!shouldUseJoinTable(attribute)) {
2487
+ throw new Error("Attempted to create join table when useJoinTable is false");
2488
+ }
2485
2489
  const targetMeta = metadata.get(attribute.target);
2486
2490
  if (!targetMeta) {
2487
2491
  throw new Error(`Unknown target ${attribute.target}`);
2488
2492
  }
2489
- if ("joinTable" in attribute && attribute.joinTable) {
2493
+ if ("joinTable" in attribute && attribute.joinTable && !attribute.joinTable.__internal__) {
2490
2494
  return;
2491
2495
  }
2492
2496
  const joinTableName = identifiers.getJoinTableName(
@@ -2570,6 +2574,7 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2570
2574
  columnToAttribute: {}
2571
2575
  };
2572
2576
  const joinTable = {
2577
+ __internal__: true,
2573
2578
  name: joinTableName,
2574
2579
  joinColumn: {
2575
2580
  name: joinColumnName,
@@ -2629,6 +2634,7 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2629
2634
  );
2630
2635
  }
2631
2636
  inverseAttribute.joinTable = {
2637
+ __internal__: true,
2632
2638
  name: joinTableName,
2633
2639
  joinColumn: joinTable.inverseJoinColumn,
2634
2640
  inverseJoinColumn: joinTable.joinColumn,
@@ -2714,6 +2720,9 @@ class Metadata extends Map {
2714
2720
  for (const meta of this.values()) {
2715
2721
  for (const [attributeName, attribute] of Object.entries(meta.attributes)) {
2716
2722
  try {
2723
+ if (attribute.unstable_virtual) {
2724
+ continue;
2725
+ }
2717
2726
  if (isRelationalAttribute(attribute)) {
2718
2727
  createRelation(attributeName, attribute, meta, this);
2719
2728
  continue;
@@ -3470,7 +3479,11 @@ const oneToMany = async (input, ctx) => {
3470
3479
  const { db, qb } = ctx;
3471
3480
  const fromTargetRow = (rowOrRows) => fromRow(targetMeta, rowOrRows);
3472
3481
  if ("joinColumn" in attribute && attribute.joinColumn) {
3473
- const { name: joinColumnName, referencedColumn: referencedColumnName } = attribute.joinColumn;
3482
+ const {
3483
+ name: joinColumnName,
3484
+ referencedColumn: referencedColumnName,
3485
+ on
3486
+ } = attribute.joinColumn;
3474
3487
  const referencedValues = ___default.default.uniq(
3475
3488
  results.map((r) => r[joinColumnName]).filter((value) => !___default.default.isNil(value))
3476
3489
  );
@@ -3480,7 +3493,10 @@ const oneToMany = async (input, ctx) => {
3480
3493
  });
3481
3494
  return;
3482
3495
  }
3483
- const rows = await db.entityManager.createQueryBuilder(targetMeta.uid).init(populateValue).addSelect(`${qb.alias}.${referencedColumnName}`).where({ [referencedColumnName]: referencedValues }).execute({ mapResults: false });
3496
+ const rows = await db.entityManager.createQueryBuilder(targetMeta.uid).init(populateValue).addSelect(`${qb.alias}.${referencedColumnName}`).where({
3497
+ [referencedColumnName]: referencedValues,
3498
+ ...on && typeof on === "function" ? on({ populateValue, results }) : {}
3499
+ }).execute({ mapResults: false });
3484
3500
  const map = ___default.default.groupBy(referencedColumnName)(rows);
3485
3501
  results.forEach((result) => {
3486
3502
  result[attributeName] = fromTargetRow(map[result[joinColumnName]] || []);
@@ -5287,8 +5303,10 @@ const processData = (metadata, data = {}, { withDefaults = false } = {}) => {
5287
5303
  if ("joinColumn" in attribute && attribute.joinColumn && attribute.owner) {
5288
5304
  const joinColumnName = attribute.joinColumn.name;
5289
5305
  const attrValue = !_.isUndefined(data[attributeName]) ? data[attributeName] : data[joinColumnName];
5290
- if (!_.isUndefined(attrValue)) {
5306
+ if (_.isNull(attrValue)) {
5291
5307
  obj[joinColumnName] = attrValue;
5308
+ } else if (!_.isUndefined(attrValue)) {
5309
+ obj[joinColumnName] = toId(attrValue);
5292
5310
  }
5293
5311
  continue;
5294
5312
  }
@@ -6176,15 +6194,15 @@ const getNextIdsToCreateDocumentId = async (db, knex2, {
6176
6194
  };
6177
6195
  const migrateDocumentIdsWithLocalizations = async (db, knex2, meta) => {
6178
6196
  const singularName = meta.singularName.toLowerCase();
6179
- const joinColumn = identifiers.getJoinColumnAttributeIdName(singularName);
6180
- const inverseJoinColumn = identifiers.getInverseJoinColumnAttributeIdName(singularName);
6197
+ const joinColumn = _.snakeCase(`${singularName}_id`);
6198
+ const inverseJoinColumn = _.snakeCase(`inv_${singularName}_id`);
6181
6199
  let ids;
6182
6200
  do {
6183
6201
  ids = await getNextIdsToCreateDocumentId(db, knex2, {
6184
6202
  joinColumn,
6185
6203
  inverseJoinColumn,
6186
6204
  tableName: meta.tableName,
6187
- joinTableName: identifiers.getJoinTableName(meta.tableName, `localizations`)
6205
+ joinTableName: _.snakeCase(`${meta.tableName}_localizations_links`)
6188
6206
  });
6189
6207
  if (ids.length > 0) {
6190
6208
  await knex2(meta.tableName).update({ document_id: cuid2.createId() }).whereIn("id", ids);
@@ -6206,7 +6224,7 @@ const createDocumentIdColumn = async (knex2, tableName) => {
6206
6224
  });
6207
6225
  };
6208
6226
  const hasLocalizationsJoinTable = async (knex2, tableName) => {
6209
- const joinTableName = identifiers.getJoinTableName(tableName, "localizations");
6227
+ const joinTableName = _.snakeCase(`${tableName}_localizations_links`);
6210
6228
  return knex2.schema.hasTable(joinTableName);
6211
6229
  };
6212
6230
  const createdDocumentId = {
@@ -6278,21 +6296,24 @@ const renameIndex = async (knex2, db, diff) => {
6278
6296
  debug(`not renaming index ${full.indexName} because name hasn't changed`);
6279
6297
  return;
6280
6298
  }
6281
- if (short.indexName.includes("_lnk_") || full.indexName.includes("_lnk_") || short.indexName.endsWith("fk") || full.indexName.endsWith("fk")) {
6299
+ if (short.indexName.endsWith("fk") || full.indexName.endsWith("fk")) {
6282
6300
  return;
6283
6301
  }
6284
6302
  debug(`renaming index from ${full.indexName} to ${short.indexName}`);
6285
6303
  try {
6286
6304
  await knex2.transaction(async (trx) => {
6287
6305
  if (client === "mysql" || client === "mariadb") {
6288
- await knex2.raw(
6289
- `ALTER TABLE \`${full.tableName}\` RENAME INDEX \`${full.indexName}\` TO \`${short.indexName}\``
6290
- ).transacting(trx);
6306
+ await knex2.raw("ALTER TABLE ?? RENAME INDEX ?? TO ??", [
6307
+ full.tableName,
6308
+ full.indexName,
6309
+ short.indexName
6310
+ ]).transacting(trx);
6291
6311
  } else if (client === "pg" || client === "postgres") {
6292
- await knex2.raw(`ALTER INDEX "${full.indexName}" RENAME TO "${short.indexName}"`).transacting(trx);
6293
- } else if (client === "sqlite" || client === "better") {
6312
+ await knex2.raw("ALTER INDEX ?? RENAME TO ??", [full.indexName, short.indexName]).transacting(trx);
6313
+ } else if (["sqlite", "sqlite3", "better-sqlite3"].includes(client)) {
6314
+ debug(`SQLite does not support index renaming, not renaming index ${full.indexName}`);
6294
6315
  } else {
6295
- debug("No db client name matches, not creating index");
6316
+ debug(`No db client name matches, not renaming index ${full.indexName}`);
6296
6317
  }
6297
6318
  });
6298
6319
  } catch (err) {