@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.
package/dist/index.mjs CHANGED
@@ -1152,10 +1152,6 @@ const createHelpers = (db) => {
1152
1152
  debug$2(`Dropping updated foreign key ${updatedForeignKey.name} on ${table.name}`);
1153
1153
  dropForeignKey(tableBuilder, updatedForeignKey.object);
1154
1154
  }
1155
- for (const removedColumn of table.columns.removed) {
1156
- debug$2(`Dropping column ${removedColumn.name} on ${table.name}`);
1157
- dropColumn(tableBuilder, removedColumn);
1158
- }
1159
1155
  const isMySQL = db.config.connection.client === "mysql";
1160
1156
  const ignoreForeignKeyNames = isMySQL ? [
1161
1157
  ...table.foreignKeys.removed.map((fk) => fk.name),
@@ -1173,6 +1169,10 @@ const createHelpers = (db) => {
1173
1169
  dropIndex2(tableBuilder, updatedIndex.object);
1174
1170
  }
1175
1171
  }
1172
+ for (const removedColumn of table.columns.removed) {
1173
+ debug$2(`Dropping column ${removedColumn.name} on ${table.name}`);
1174
+ dropColumn(tableBuilder, removedColumn);
1175
+ }
1176
1176
  for (const updatedColumn of table.columns.updated) {
1177
1177
  debug$2(`Updating column ${updatedColumn.name} on ${table.name}`);
1178
1178
  const { object } = updatedColumn;
@@ -1182,14 +1182,6 @@ const createHelpers = (db) => {
1182
1182
  createColumn2(tableBuilder, object).alter();
1183
1183
  }
1184
1184
  }
1185
- for (const updatedForeignKey of table.foreignKeys.updated) {
1186
- debug$2(`Recreating updated foreign key ${updatedForeignKey.name} on ${table.name}`);
1187
- createForeignKey(tableBuilder, updatedForeignKey.object);
1188
- }
1189
- for (const updatedIndex of table.indexes.updated) {
1190
- debug$2(`Recreating updated index ${updatedIndex.name} on ${table.name}`);
1191
- createIndex(tableBuilder, updatedIndex.object);
1192
- }
1193
1185
  for (const addedColumn of table.columns.added) {
1194
1186
  debug$2(`Creating column ${addedColumn.name} on ${table.name}`);
1195
1187
  if (addedColumn.type === "increments" && !db.dialect.canAddIncrements()) {
@@ -1199,6 +1191,14 @@ const createHelpers = (db) => {
1199
1191
  createColumn2(tableBuilder, addedColumn);
1200
1192
  }
1201
1193
  }
1194
+ for (const updatedForeignKey of table.foreignKeys.updated) {
1195
+ debug$2(`Recreating updated foreign key ${updatedForeignKey.name} on ${table.name}`);
1196
+ createForeignKey(tableBuilder, updatedForeignKey.object);
1197
+ }
1198
+ for (const updatedIndex of table.indexes.updated) {
1199
+ debug$2(`Recreating updated index ${updatedIndex.name} on ${table.name}`);
1200
+ createIndex(tableBuilder, updatedIndex.object);
1201
+ }
1202
1202
  for (const addedForeignKey of table.foreignKeys.added) {
1203
1203
  debug$2(`Creating foreign keys ${addedForeignKey.name} on ${table.name}`);
1204
1204
  createForeignKey(tableBuilder, addedForeignKey);
@@ -2261,7 +2261,7 @@ const createOneToOne = (attributeName, attribute, meta, metadata) => {
2261
2261
  }
2262
2262
  };
2263
2263
  const createOneToMany = (attributeName, attribute, meta, metadata) => {
2264
- if (!isBidirectional(attribute)) {
2264
+ if (shouldUseJoinTable(attribute) && !isBidirectional(attribute)) {
2265
2265
  createJoinTable(metadata, {
2266
2266
  attribute,
2267
2267
  attributeName,
@@ -2290,7 +2290,7 @@ const createManyToOne = (attributeName, attribute, meta, metadata) => {
2290
2290
  }
2291
2291
  };
2292
2292
  const createManyToMany = (attributeName, attribute, meta, metadata) => {
2293
- if (!isBidirectional(attribute) || isOwner(attribute)) {
2293
+ if (shouldUseJoinTable(attribute) && (!isBidirectional(attribute) || isOwner(attribute))) {
2294
2294
  createJoinTable(metadata, {
2295
2295
  attribute,
2296
2296
  attributeName,
@@ -2315,7 +2315,7 @@ const createMorphToOne = (attributeName, attribute) => {
2315
2315
  });
2316
2316
  };
2317
2317
  const createMorphToMany = (attributeName, attribute, meta, metadata) => {
2318
- if ("joinTable" in attribute && attribute.joinTable) {
2318
+ if ("joinTable" in attribute && attribute.joinTable && !attribute.joinTable.__internal__) {
2319
2319
  return;
2320
2320
  }
2321
2321
  const joinTableName = identifiers.getMorphTableName(meta.tableName, attributeName);
@@ -2385,6 +2385,7 @@ const createMorphToMany = (attributeName, attribute, meta, metadata) => {
2385
2385
  columnToAttribute: {}
2386
2386
  });
2387
2387
  const joinTable = {
2388
+ __internal__: true,
2388
2389
  name: joinTableName,
2389
2390
  joinColumn: {
2390
2391
  name: joinColumnName,
@@ -2450,11 +2451,14 @@ const createJoinColumn = (metadata, { attribute, attributeName }) => {
2450
2451
  }
2451
2452
  };
2452
2453
  const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2454
+ if (!shouldUseJoinTable(attribute)) {
2455
+ throw new Error("Attempted to create join table when useJoinTable is false");
2456
+ }
2453
2457
  const targetMeta = metadata.get(attribute.target);
2454
2458
  if (!targetMeta) {
2455
2459
  throw new Error(`Unknown target ${attribute.target}`);
2456
2460
  }
2457
- if ("joinTable" in attribute && attribute.joinTable) {
2461
+ if ("joinTable" in attribute && attribute.joinTable && !attribute.joinTable.__internal__) {
2458
2462
  return;
2459
2463
  }
2460
2464
  const joinTableName = identifiers.getJoinTableName(
@@ -2538,6 +2542,7 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2538
2542
  columnToAttribute: {}
2539
2543
  };
2540
2544
  const joinTable = {
2545
+ __internal__: true,
2541
2546
  name: joinTableName,
2542
2547
  joinColumn: {
2543
2548
  name: joinColumnName,
@@ -2597,6 +2602,7 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2597
2602
  );
2598
2603
  }
2599
2604
  inverseAttribute.joinTable = {
2605
+ __internal__: true,
2600
2606
  name: joinTableName,
2601
2607
  joinColumn: joinTable.inverseJoinColumn,
2602
2608
  inverseJoinColumn: joinTable.joinColumn,
@@ -2682,6 +2688,9 @@ class Metadata extends Map {
2682
2688
  for (const meta of this.values()) {
2683
2689
  for (const [attributeName, attribute] of Object.entries(meta.attributes)) {
2684
2690
  try {
2691
+ if (attribute.unstable_virtual) {
2692
+ continue;
2693
+ }
2685
2694
  if (isRelationalAttribute(attribute)) {
2686
2695
  createRelation(attributeName, attribute, meta, this);
2687
2696
  continue;
@@ -3438,7 +3447,11 @@ const oneToMany = async (input, ctx) => {
3438
3447
  const { db, qb } = ctx;
3439
3448
  const fromTargetRow = (rowOrRows) => fromRow(targetMeta, rowOrRows);
3440
3449
  if ("joinColumn" in attribute && attribute.joinColumn) {
3441
- const { name: joinColumnName, referencedColumn: referencedColumnName } = attribute.joinColumn;
3450
+ const {
3451
+ name: joinColumnName,
3452
+ referencedColumn: referencedColumnName,
3453
+ on
3454
+ } = attribute.joinColumn;
3442
3455
  const referencedValues = _.uniq(
3443
3456
  results.map((r) => r[joinColumnName]).filter((value) => !_.isNil(value))
3444
3457
  );
@@ -3448,7 +3461,10 @@ const oneToMany = async (input, ctx) => {
3448
3461
  });
3449
3462
  return;
3450
3463
  }
3451
- const rows = await db.entityManager.createQueryBuilder(targetMeta.uid).init(populateValue).addSelect(`${qb.alias}.${referencedColumnName}`).where({ [referencedColumnName]: referencedValues }).execute({ mapResults: false });
3464
+ const rows = await db.entityManager.createQueryBuilder(targetMeta.uid).init(populateValue).addSelect(`${qb.alias}.${referencedColumnName}`).where({
3465
+ [referencedColumnName]: referencedValues,
3466
+ ...on && typeof on === "function" ? on({ populateValue, results }) : {}
3467
+ }).execute({ mapResults: false });
3452
3468
  const map2 = _.groupBy(referencedColumnName)(rows);
3453
3469
  results.forEach((result) => {
3454
3470
  result[attributeName] = fromTargetRow(map2[result[joinColumnName]] || []);
@@ -5255,8 +5271,10 @@ const processData = (metadata, data = {}, { withDefaults = false } = {}) => {
5255
5271
  if ("joinColumn" in attribute && attribute.joinColumn && attribute.owner) {
5256
5272
  const joinColumnName = attribute.joinColumn.name;
5257
5273
  const attrValue = !isUndefined(data[attributeName]) ? data[attributeName] : data[joinColumnName];
5258
- if (!isUndefined(attrValue)) {
5274
+ if (isNull(attrValue)) {
5259
5275
  obj[joinColumnName] = attrValue;
5276
+ } else if (!isUndefined(attrValue)) {
5277
+ obj[joinColumnName] = toId(attrValue);
5260
5278
  }
5261
5279
  continue;
5262
5280
  }
@@ -6144,15 +6162,15 @@ const getNextIdsToCreateDocumentId = async (db, knex2, {
6144
6162
  };
6145
6163
  const migrateDocumentIdsWithLocalizations = async (db, knex2, meta) => {
6146
6164
  const singularName = meta.singularName.toLowerCase();
6147
- const joinColumn = identifiers.getJoinColumnAttributeIdName(singularName);
6148
- const inverseJoinColumn = identifiers.getInverseJoinColumnAttributeIdName(singularName);
6165
+ const joinColumn = snakeCase(`${singularName}_id`);
6166
+ const inverseJoinColumn = snakeCase(`inv_${singularName}_id`);
6149
6167
  let ids;
6150
6168
  do {
6151
6169
  ids = await getNextIdsToCreateDocumentId(db, knex2, {
6152
6170
  joinColumn,
6153
6171
  inverseJoinColumn,
6154
6172
  tableName: meta.tableName,
6155
- joinTableName: identifiers.getJoinTableName(meta.tableName, `localizations`)
6173
+ joinTableName: snakeCase(`${meta.tableName}_localizations_links`)
6156
6174
  });
6157
6175
  if (ids.length > 0) {
6158
6176
  await knex2(meta.tableName).update({ document_id: createId() }).whereIn("id", ids);
@@ -6174,7 +6192,7 @@ const createDocumentIdColumn = async (knex2, tableName) => {
6174
6192
  });
6175
6193
  };
6176
6194
  const hasLocalizationsJoinTable = async (knex2, tableName) => {
6177
- const joinTableName = identifiers.getJoinTableName(tableName, "localizations");
6195
+ const joinTableName = snakeCase(`${tableName}_localizations_links`);
6178
6196
  return knex2.schema.hasTable(joinTableName);
6179
6197
  };
6180
6198
  const createdDocumentId = {
@@ -6246,21 +6264,24 @@ const renameIndex = async (knex2, db, diff) => {
6246
6264
  debug(`not renaming index ${full.indexName} because name hasn't changed`);
6247
6265
  return;
6248
6266
  }
6249
- if (short.indexName.includes("_lnk_") || full.indexName.includes("_lnk_") || short.indexName.endsWith("fk") || full.indexName.endsWith("fk")) {
6267
+ if (short.indexName.endsWith("fk") || full.indexName.endsWith("fk")) {
6250
6268
  return;
6251
6269
  }
6252
6270
  debug(`renaming index from ${full.indexName} to ${short.indexName}`);
6253
6271
  try {
6254
6272
  await knex2.transaction(async (trx) => {
6255
6273
  if (client === "mysql" || client === "mariadb") {
6256
- await knex2.raw(
6257
- `ALTER TABLE \`${full.tableName}\` RENAME INDEX \`${full.indexName}\` TO \`${short.indexName}\``
6258
- ).transacting(trx);
6274
+ await knex2.raw("ALTER TABLE ?? RENAME INDEX ?? TO ??", [
6275
+ full.tableName,
6276
+ full.indexName,
6277
+ short.indexName
6278
+ ]).transacting(trx);
6259
6279
  } else if (client === "pg" || client === "postgres") {
6260
- await knex2.raw(`ALTER INDEX "${full.indexName}" RENAME TO "${short.indexName}"`).transacting(trx);
6261
- } else if (client === "sqlite" || client === "better") {
6280
+ await knex2.raw("ALTER INDEX ?? RENAME TO ??", [full.indexName, short.indexName]).transacting(trx);
6281
+ } else if (["sqlite", "sqlite3", "better-sqlite3"].includes(client)) {
6282
+ debug(`SQLite does not support index renaming, not renaming index ${full.indexName}`);
6262
6283
  } else {
6263
- debug("No db client name matches, not creating index");
6284
+ debug(`No db client name matches, not renaming index ${full.indexName}`);
6264
6285
  }
6265
6286
  });
6266
6287
  } catch (err) {