@strapi/database 5.0.0-rc.3 → 5.0.0-rc.30

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
@@ -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,
@@ -2557,7 +2562,8 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2557
2562
  column: {
2558
2563
  unsigned: true,
2559
2564
  defaultTo: null
2560
- }
2565
+ },
2566
+ columnName: orderColumnName
2561
2567
  };
2562
2568
  metadataSchema.indexes.push({
2563
2569
  name: identifiers.getOrderFkIndexName(joinTableName),
@@ -2572,7 +2578,8 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2572
2578
  column: {
2573
2579
  unsigned: true,
2574
2580
  defaultTo: null
2575
- }
2581
+ },
2582
+ columnName: inverseOrderColumnName
2576
2583
  };
2577
2584
  metadataSchema.indexes.push({
2578
2585
  name: identifiers.getOrderInverseFkIndexName(joinTableName),
@@ -2595,6 +2602,7 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2595
2602
  );
2596
2603
  }
2597
2604
  inverseAttribute.joinTable = {
2605
+ __internal__: true,
2598
2606
  name: joinTableName,
2599
2607
  joinColumn: joinTable.inverseJoinColumn,
2600
2608
  inverseJoinColumn: joinTable.joinColumn,
@@ -2680,6 +2688,9 @@ class Metadata extends Map {
2680
2688
  for (const meta of this.values()) {
2681
2689
  for (const [attributeName, attribute] of Object.entries(meta.attributes)) {
2682
2690
  try {
2691
+ if (attribute.unstable_virtual) {
2692
+ continue;
2693
+ }
2683
2694
  if (isRelationalAttribute(attribute)) {
2684
2695
  createRelation(attributeName, attribute, meta, this);
2685
2696
  continue;
@@ -2743,12 +2754,22 @@ class StringField extends Field {
2743
2754
  }
2744
2755
  class JSONField extends Field {
2745
2756
  toDB(value) {
2746
- return JSON.stringify(value);
2757
+ if (value == null) {
2758
+ return null;
2759
+ }
2760
+ if (typeof value === "object") {
2761
+ return JSON.stringify(value);
2762
+ }
2763
+ return value;
2747
2764
  }
2748
2765
  fromDB(value) {
2749
2766
  try {
2750
2767
  if (typeof value === "string") {
2751
- return JSON.parse(value);
2768
+ const parsedValue = JSON.parse(value);
2769
+ if (typeof parsedValue === "string") {
2770
+ return JSON.parse(parsedValue);
2771
+ }
2772
+ return parsedValue;
2752
2773
  }
2753
2774
  } catch (error) {
2754
2775
  return value;
@@ -3426,7 +3447,11 @@ const oneToMany = async (input, ctx) => {
3426
3447
  const { db, qb } = ctx;
3427
3448
  const fromTargetRow = (rowOrRows) => fromRow(targetMeta, rowOrRows);
3428
3449
  if ("joinColumn" in attribute && attribute.joinColumn) {
3429
- const { name: joinColumnName, referencedColumn: referencedColumnName } = attribute.joinColumn;
3450
+ const {
3451
+ name: joinColumnName,
3452
+ referencedColumn: referencedColumnName,
3453
+ on
3454
+ } = attribute.joinColumn;
3430
3455
  const referencedValues = _.uniq(
3431
3456
  results.map((r) => r[joinColumnName]).filter((value) => !_.isNil(value))
3432
3457
  );
@@ -3436,7 +3461,10 @@ const oneToMany = async (input, ctx) => {
3436
3461
  });
3437
3462
  return;
3438
3463
  }
3439
- 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 });
3440
3468
  const map2 = _.groupBy(referencedColumnName)(rows);
3441
3469
  results.forEach((result) => {
3442
3470
  result[attributeName] = fromTargetRow(map2[result[joinColumnName]] || []);
@@ -5243,8 +5271,10 @@ const processData = (metadata, data = {}, { withDefaults = false } = {}) => {
5243
5271
  if ("joinColumn" in attribute && attribute.joinColumn && attribute.owner) {
5244
5272
  const joinColumnName = attribute.joinColumn.name;
5245
5273
  const attrValue = !isUndefined(data[attributeName]) ? data[attributeName] : data[joinColumnName];
5246
- if (!isUndefined(attrValue)) {
5274
+ if (isNull(attrValue)) {
5247
5275
  obj[joinColumnName] = attrValue;
5276
+ } else if (!isUndefined(attrValue)) {
5277
+ obj[joinColumnName] = toId(attrValue);
5248
5278
  }
5249
5279
  continue;
5250
5280
  }
@@ -6132,15 +6162,15 @@ const getNextIdsToCreateDocumentId = async (db, knex2, {
6132
6162
  };
6133
6163
  const migrateDocumentIdsWithLocalizations = async (db, knex2, meta) => {
6134
6164
  const singularName = meta.singularName.toLowerCase();
6135
- const joinColumn = identifiers.getJoinColumnAttributeIdName(singularName);
6136
- const inverseJoinColumn = identifiers.getInverseJoinColumnAttributeIdName(singularName);
6165
+ const joinColumn = snakeCase(`${singularName}_id`);
6166
+ const inverseJoinColumn = snakeCase(`inv_${singularName}_id`);
6137
6167
  let ids;
6138
6168
  do {
6139
6169
  ids = await getNextIdsToCreateDocumentId(db, knex2, {
6140
6170
  joinColumn,
6141
6171
  inverseJoinColumn,
6142
6172
  tableName: meta.tableName,
6143
- joinTableName: identifiers.getJoinTableName(meta.tableName, `localizations`)
6173
+ joinTableName: snakeCase(`${meta.tableName}_localizations_links`)
6144
6174
  });
6145
6175
  if (ids.length > 0) {
6146
6176
  await knex2(meta.tableName).update({ document_id: createId() }).whereIn("id", ids);
@@ -6162,7 +6192,7 @@ const createDocumentIdColumn = async (knex2, tableName) => {
6162
6192
  });
6163
6193
  };
6164
6194
  const hasLocalizationsJoinTable = async (knex2, tableName) => {
6165
- const joinTableName = identifiers.getJoinTableName(tableName, "localizations");
6195
+ const joinTableName = snakeCase(`${tableName}_localizations_links`);
6166
6196
  return knex2.schema.hasTable(joinTableName);
6167
6197
  };
6168
6198
  const createdDocumentId = {
@@ -6234,21 +6264,24 @@ const renameIndex = async (knex2, db, diff) => {
6234
6264
  debug(`not renaming index ${full.indexName} because name hasn't changed`);
6235
6265
  return;
6236
6266
  }
6237
- 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")) {
6238
6268
  return;
6239
6269
  }
6240
6270
  debug(`renaming index from ${full.indexName} to ${short.indexName}`);
6241
6271
  try {
6242
6272
  await knex2.transaction(async (trx) => {
6243
6273
  if (client === "mysql" || client === "mariadb") {
6244
- await knex2.raw(
6245
- `ALTER TABLE \`${full.tableName}\` RENAME INDEX \`${full.indexName}\` TO \`${short.indexName}\``
6246
- ).transacting(trx);
6274
+ await knex2.raw("ALTER TABLE ?? RENAME INDEX ?? TO ??", [
6275
+ full.tableName,
6276
+ full.indexName,
6277
+ short.indexName
6278
+ ]).transacting(trx);
6247
6279
  } else if (client === "pg" || client === "postgres") {
6248
- await knex2.raw(`ALTER INDEX "${full.indexName}" RENAME TO "${short.indexName}"`).transacting(trx);
6249
- } 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}`);
6250
6283
  } else {
6251
- debug("No db client name matches, not creating index");
6284
+ debug(`No db client name matches, not renaming index ${full.indexName}`);
6252
6285
  }
6253
6286
  });
6254
6287
  } catch (err) {