@strapi/database 5.0.0-rc.2 → 5.0.0-rc.20

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
@@ -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,
@@ -2454,7 +2455,7 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2454
2455
  if (!targetMeta) {
2455
2456
  throw new Error(`Unknown target ${attribute.target}`);
2456
2457
  }
2457
- if ("joinTable" in attribute && attribute.joinTable) {
2458
+ if ("joinTable" in attribute && attribute.joinTable && !attribute.joinTable.__internal__) {
2458
2459
  return;
2459
2460
  }
2460
2461
  const joinTableName = identifiers.getJoinTableName(
@@ -2538,6 +2539,7 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2538
2539
  columnToAttribute: {}
2539
2540
  };
2540
2541
  const joinTable = {
2542
+ __internal__: true,
2541
2543
  name: joinTableName,
2542
2544
  joinColumn: {
2543
2545
  name: joinColumnName,
@@ -2557,7 +2559,8 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2557
2559
  column: {
2558
2560
  unsigned: true,
2559
2561
  defaultTo: null
2560
- }
2562
+ },
2563
+ columnName: orderColumnName
2561
2564
  };
2562
2565
  metadataSchema.indexes.push({
2563
2566
  name: identifiers.getOrderFkIndexName(joinTableName),
@@ -2572,7 +2575,8 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2572
2575
  column: {
2573
2576
  unsigned: true,
2574
2577
  defaultTo: null
2575
- }
2578
+ },
2579
+ columnName: inverseOrderColumnName
2576
2580
  };
2577
2581
  metadataSchema.indexes.push({
2578
2582
  name: identifiers.getOrderInverseFkIndexName(joinTableName),
@@ -2595,6 +2599,7 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
2595
2599
  );
2596
2600
  }
2597
2601
  inverseAttribute.joinTable = {
2602
+ __internal__: true,
2598
2603
  name: joinTableName,
2599
2604
  joinColumn: joinTable.inverseJoinColumn,
2600
2605
  inverseJoinColumn: joinTable.joinColumn,
@@ -2743,12 +2748,22 @@ class StringField extends Field {
2743
2748
  }
2744
2749
  class JSONField extends Field {
2745
2750
  toDB(value) {
2746
- return JSON.stringify(value);
2751
+ if (value == null) {
2752
+ return null;
2753
+ }
2754
+ if (typeof value === "object") {
2755
+ return JSON.stringify(value);
2756
+ }
2757
+ return value;
2747
2758
  }
2748
2759
  fromDB(value) {
2749
2760
  try {
2750
2761
  if (typeof value === "string") {
2751
- return JSON.parse(value);
2762
+ const parsedValue = JSON.parse(value);
2763
+ if (typeof parsedValue === "string") {
2764
+ return JSON.parse(parsedValue);
2765
+ }
2766
+ return parsedValue;
2752
2767
  }
2753
2768
  } catch (error) {
2754
2769
  return value;
@@ -3426,7 +3441,11 @@ const oneToMany = async (input, ctx) => {
3426
3441
  const { db, qb } = ctx;
3427
3442
  const fromTargetRow = (rowOrRows) => fromRow(targetMeta, rowOrRows);
3428
3443
  if ("joinColumn" in attribute && attribute.joinColumn) {
3429
- const { name: joinColumnName, referencedColumn: referencedColumnName } = attribute.joinColumn;
3444
+ const {
3445
+ name: joinColumnName,
3446
+ referencedColumn: referencedColumnName,
3447
+ on
3448
+ } = attribute.joinColumn;
3430
3449
  const referencedValues = _.uniq(
3431
3450
  results.map((r) => r[joinColumnName]).filter((value) => !_.isNil(value))
3432
3451
  );
@@ -3436,7 +3455,10 @@ const oneToMany = async (input, ctx) => {
3436
3455
  });
3437
3456
  return;
3438
3457
  }
3439
- const rows = await db.entityManager.createQueryBuilder(targetMeta.uid).init(populateValue).addSelect(`${qb.alias}.${referencedColumnName}`).where({ [referencedColumnName]: referencedValues }).execute({ mapResults: false });
3458
+ const rows = await db.entityManager.createQueryBuilder(targetMeta.uid).init(populateValue).addSelect(`${qb.alias}.${referencedColumnName}`).where({
3459
+ [referencedColumnName]: referencedValues,
3460
+ ...on && typeof on === "function" ? on({ populateValue, results }) : {}
3461
+ }).execute({ mapResults: false });
3440
3462
  const map2 = _.groupBy(referencedColumnName)(rows);
3441
3463
  results.forEach((result) => {
3442
3464
  result[attributeName] = fromTargetRow(map2[result[joinColumnName]] || []);
@@ -5243,8 +5265,10 @@ const processData = (metadata, data = {}, { withDefaults = false } = {}) => {
5243
5265
  if ("joinColumn" in attribute && attribute.joinColumn && attribute.owner) {
5244
5266
  const joinColumnName = attribute.joinColumn.name;
5245
5267
  const attrValue = !isUndefined(data[attributeName]) ? data[attributeName] : data[joinColumnName];
5246
- if (!isUndefined(attrValue)) {
5268
+ if (isNull(attrValue)) {
5247
5269
  obj[joinColumnName] = attrValue;
5270
+ } else if (!isUndefined(attrValue)) {
5271
+ obj[joinColumnName] = toId(attrValue);
5248
5272
  }
5249
5273
  continue;
5250
5274
  }
@@ -6132,15 +6156,15 @@ const getNextIdsToCreateDocumentId = async (db, knex2, {
6132
6156
  };
6133
6157
  const migrateDocumentIdsWithLocalizations = async (db, knex2, meta) => {
6134
6158
  const singularName = meta.singularName.toLowerCase();
6135
- const joinColumn = identifiers.getJoinColumnAttributeIdName(singularName);
6136
- const inverseJoinColumn = identifiers.getInverseJoinColumnAttributeIdName(singularName);
6159
+ const joinColumn = snakeCase(`${singularName}_id`);
6160
+ const inverseJoinColumn = snakeCase(`inv_${singularName}_id`);
6137
6161
  let ids;
6138
6162
  do {
6139
6163
  ids = await getNextIdsToCreateDocumentId(db, knex2, {
6140
6164
  joinColumn,
6141
6165
  inverseJoinColumn,
6142
6166
  tableName: meta.tableName,
6143
- joinTableName: identifiers.getJoinTableName(meta.tableName, `localizations`)
6167
+ joinTableName: snakeCase(`${meta.tableName}_localizations_links`)
6144
6168
  });
6145
6169
  if (ids.length > 0) {
6146
6170
  await knex2(meta.tableName).update({ document_id: createId() }).whereIn("id", ids);
@@ -6162,7 +6186,7 @@ const createDocumentIdColumn = async (knex2, tableName) => {
6162
6186
  });
6163
6187
  };
6164
6188
  const hasLocalizationsJoinTable = async (knex2, tableName) => {
6165
- const joinTableName = identifiers.getJoinTableName(tableName, "localizations");
6189
+ const joinTableName = snakeCase(`${tableName}_localizations_links`);
6166
6190
  return knex2.schema.hasTable(joinTableName);
6167
6191
  };
6168
6192
  const createdDocumentId = {