@strapi/database 5.0.0-rc.8 → 5.0.0

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
@@ -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,
@@ -2589,7 +2594,8 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2589
2594
  column: {
2590
2595
  unsigned: true,
2591
2596
  defaultTo: null
2592
- }
2597
+ },
2598
+ columnName: orderColumnName
2593
2599
  };
2594
2600
  metadataSchema.indexes.push({
2595
2601
  name: identifiers.getOrderFkIndexName(joinTableName),
@@ -2604,7 +2610,8 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2604
2610
  column: {
2605
2611
  unsigned: true,
2606
2612
  defaultTo: null
2607
- }
2613
+ },
2614
+ columnName: inverseOrderColumnName
2608
2615
  };
2609
2616
  metadataSchema.indexes.push({
2610
2617
  name: identifiers.getOrderInverseFkIndexName(joinTableName),
@@ -2627,6 +2634,7 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2627
2634
  );
2628
2635
  }
2629
2636
  inverseAttribute.joinTable = {
2637
+ __internal__: true,
2630
2638
  name: joinTableName,
2631
2639
  joinColumn: joinTable.inverseJoinColumn,
2632
2640
  inverseJoinColumn: joinTable.joinColumn,
@@ -2712,6 +2720,9 @@ class Metadata extends Map {
2712
2720
  for (const meta of this.values()) {
2713
2721
  for (const [attributeName, attribute] of Object.entries(meta.attributes)) {
2714
2722
  try {
2723
+ if (attribute.unstable_virtual) {
2724
+ continue;
2725
+ }
2715
2726
  if (isRelationalAttribute(attribute)) {
2716
2727
  createRelation(attributeName, attribute, meta, this);
2717
2728
  continue;
@@ -3468,7 +3479,11 @@ const oneToMany = async (input, ctx) => {
3468
3479
  const { db, qb } = ctx;
3469
3480
  const fromTargetRow = (rowOrRows) => fromRow(targetMeta, rowOrRows);
3470
3481
  if ("joinColumn" in attribute && attribute.joinColumn) {
3471
- const { name: joinColumnName, referencedColumn: referencedColumnName } = attribute.joinColumn;
3482
+ const {
3483
+ name: joinColumnName,
3484
+ referencedColumn: referencedColumnName,
3485
+ on
3486
+ } = attribute.joinColumn;
3472
3487
  const referencedValues = ___default.default.uniq(
3473
3488
  results.map((r) => r[joinColumnName]).filter((value) => !___default.default.isNil(value))
3474
3489
  );
@@ -3478,7 +3493,10 @@ const oneToMany = async (input, ctx) => {
3478
3493
  });
3479
3494
  return;
3480
3495
  }
3481
- 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 });
3482
3500
  const map = ___default.default.groupBy(referencedColumnName)(rows);
3483
3501
  results.forEach((result) => {
3484
3502
  result[attributeName] = fromTargetRow(map[result[joinColumnName]] || []);
@@ -5285,8 +5303,10 @@ const processData = (metadata, data = {}, { withDefaults = false } = {}) => {
5285
5303
  if ("joinColumn" in attribute && attribute.joinColumn && attribute.owner) {
5286
5304
  const joinColumnName = attribute.joinColumn.name;
5287
5305
  const attrValue = !_.isUndefined(data[attributeName]) ? data[attributeName] : data[joinColumnName];
5288
- if (!_.isUndefined(attrValue)) {
5306
+ if (_.isNull(attrValue)) {
5289
5307
  obj[joinColumnName] = attrValue;
5308
+ } else if (!_.isUndefined(attrValue)) {
5309
+ obj[joinColumnName] = toId(attrValue);
5290
5310
  }
5291
5311
  continue;
5292
5312
  }
@@ -6174,15 +6194,15 @@ const getNextIdsToCreateDocumentId = async (db, knex2, {
6174
6194
  };
6175
6195
  const migrateDocumentIdsWithLocalizations = async (db, knex2, meta) => {
6176
6196
  const singularName = meta.singularName.toLowerCase();
6177
- const joinColumn = identifiers.getJoinColumnAttributeIdName(singularName);
6178
- const inverseJoinColumn = identifiers.getInverseJoinColumnAttributeIdName(singularName);
6197
+ const joinColumn = _.snakeCase(`${singularName}_id`);
6198
+ const inverseJoinColumn = _.snakeCase(`inv_${singularName}_id`);
6179
6199
  let ids;
6180
6200
  do {
6181
6201
  ids = await getNextIdsToCreateDocumentId(db, knex2, {
6182
6202
  joinColumn,
6183
6203
  inverseJoinColumn,
6184
6204
  tableName: meta.tableName,
6185
- joinTableName: identifiers.getJoinTableName(meta.tableName, `localizations`)
6205
+ joinTableName: _.snakeCase(`${meta.tableName}_localizations_links`)
6186
6206
  });
6187
6207
  if (ids.length > 0) {
6188
6208
  await knex2(meta.tableName).update({ document_id: cuid2.createId() }).whereIn("id", ids);
@@ -6204,7 +6224,7 @@ const createDocumentIdColumn = async (knex2, tableName) => {
6204
6224
  });
6205
6225
  };
6206
6226
  const hasLocalizationsJoinTable = async (knex2, tableName) => {
6207
- const joinTableName = identifiers.getJoinTableName(tableName, "localizations");
6227
+ const joinTableName = _.snakeCase(`${tableName}_localizations_links`);
6208
6228
  return knex2.schema.hasTable(joinTableName);
6209
6229
  };
6210
6230
  const createdDocumentId = {
@@ -6276,21 +6296,24 @@ const renameIndex = async (knex2, db, diff) => {
6276
6296
  debug(`not renaming index ${full.indexName} because name hasn't changed`);
6277
6297
  return;
6278
6298
  }
6279
- 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")) {
6280
6300
  return;
6281
6301
  }
6282
6302
  debug(`renaming index from ${full.indexName} to ${short.indexName}`);
6283
6303
  try {
6284
6304
  await knex2.transaction(async (trx) => {
6285
6305
  if (client === "mysql" || client === "mariadb") {
6286
- await knex2.raw(
6287
- `ALTER TABLE \`${full.tableName}\` RENAME INDEX \`${full.indexName}\` TO \`${short.indexName}\``
6288
- ).transacting(trx);
6306
+ await knex2.raw("ALTER TABLE ?? RENAME INDEX ?? TO ??", [
6307
+ full.tableName,
6308
+ full.indexName,
6309
+ short.indexName
6310
+ ]).transacting(trx);
6289
6311
  } else if (client === "pg" || client === "postgres") {
6290
- await knex2.raw(`ALTER INDEX "${full.indexName}" RENAME TO "${short.indexName}"`).transacting(trx);
6291
- } 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}`);
6292
6315
  } else {
6293
- debug("No db client name matches, not creating index");
6316
+ debug(`No db client name matches, not renaming index ${full.indexName}`);
6294
6317
  }
6295
6318
  });
6296
6319
  } catch (err) {