@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.
@@ -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"}
@@ -1,6 +1,6 @@
1
1
  import Field from './field';
2
2
  export default class JSONField extends Field {
3
- toDB(value: unknown): string;
3
+ toDB(value: unknown): {} | null;
4
4
  fromDB(value: unknown): any;
5
5
  }
6
6
  //# sourceMappingURL=json.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/fields/json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,SAAS,CAAC;AAE5B,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,KAAK;IAC1C,IAAI,CAAC,KAAK,EAAE,OAAO;IAInB,MAAM,CAAC,KAAK,EAAE,OAAO;CAWtB"}
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/fields/json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,SAAS,CAAC;AAE5B,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,KAAK;IAC1C,IAAI,CAAC,KAAK,EAAE,OAAO;IAYnB,MAAM,CAAC,KAAK,EAAE,OAAO;CAsBtB"}
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;
@@ -2775,12 +2786,22 @@ class StringField extends Field {
2775
2786
  }
2776
2787
  class JSONField extends Field {
2777
2788
  toDB(value) {
2778
- return JSON.stringify(value);
2789
+ if (value == null) {
2790
+ return null;
2791
+ }
2792
+ if (typeof value === "object") {
2793
+ return JSON.stringify(value);
2794
+ }
2795
+ return value;
2779
2796
  }
2780
2797
  fromDB(value) {
2781
2798
  try {
2782
2799
  if (typeof value === "string") {
2783
- return JSON.parse(value);
2800
+ const parsedValue = JSON.parse(value);
2801
+ if (typeof parsedValue === "string") {
2802
+ return JSON.parse(parsedValue);
2803
+ }
2804
+ return parsedValue;
2784
2805
  }
2785
2806
  } catch (error) {
2786
2807
  return value;
@@ -3458,7 +3479,11 @@ const oneToMany = async (input, ctx) => {
3458
3479
  const { db, qb } = ctx;
3459
3480
  const fromTargetRow = (rowOrRows) => fromRow(targetMeta, rowOrRows);
3460
3481
  if ("joinColumn" in attribute && attribute.joinColumn) {
3461
- const { name: joinColumnName, referencedColumn: referencedColumnName } = attribute.joinColumn;
3482
+ const {
3483
+ name: joinColumnName,
3484
+ referencedColumn: referencedColumnName,
3485
+ on
3486
+ } = attribute.joinColumn;
3462
3487
  const referencedValues = ___default.default.uniq(
3463
3488
  results.map((r) => r[joinColumnName]).filter((value) => !___default.default.isNil(value))
3464
3489
  );
@@ -3468,7 +3493,10 @@ const oneToMany = async (input, ctx) => {
3468
3493
  });
3469
3494
  return;
3470
3495
  }
3471
- 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 });
3472
3500
  const map = ___default.default.groupBy(referencedColumnName)(rows);
3473
3501
  results.forEach((result) => {
3474
3502
  result[attributeName] = fromTargetRow(map[result[joinColumnName]] || []);
@@ -5275,8 +5303,10 @@ const processData = (metadata, data = {}, { withDefaults = false } = {}) => {
5275
5303
  if ("joinColumn" in attribute && attribute.joinColumn && attribute.owner) {
5276
5304
  const joinColumnName = attribute.joinColumn.name;
5277
5305
  const attrValue = !_.isUndefined(data[attributeName]) ? data[attributeName] : data[joinColumnName];
5278
- if (!_.isUndefined(attrValue)) {
5306
+ if (_.isNull(attrValue)) {
5279
5307
  obj[joinColumnName] = attrValue;
5308
+ } else if (!_.isUndefined(attrValue)) {
5309
+ obj[joinColumnName] = toId(attrValue);
5280
5310
  }
5281
5311
  continue;
5282
5312
  }
@@ -6164,15 +6194,15 @@ const getNextIdsToCreateDocumentId = async (db, knex2, {
6164
6194
  };
6165
6195
  const migrateDocumentIdsWithLocalizations = async (db, knex2, meta) => {
6166
6196
  const singularName = meta.singularName.toLowerCase();
6167
- const joinColumn = identifiers.getJoinColumnAttributeIdName(singularName);
6168
- const inverseJoinColumn = identifiers.getInverseJoinColumnAttributeIdName(singularName);
6197
+ const joinColumn = _.snakeCase(`${singularName}_id`);
6198
+ const inverseJoinColumn = _.snakeCase(`inv_${singularName}_id`);
6169
6199
  let ids;
6170
6200
  do {
6171
6201
  ids = await getNextIdsToCreateDocumentId(db, knex2, {
6172
6202
  joinColumn,
6173
6203
  inverseJoinColumn,
6174
6204
  tableName: meta.tableName,
6175
- joinTableName: identifiers.getJoinTableName(meta.tableName, `localizations`)
6205
+ joinTableName: _.snakeCase(`${meta.tableName}_localizations_links`)
6176
6206
  });
6177
6207
  if (ids.length > 0) {
6178
6208
  await knex2(meta.tableName).update({ document_id: cuid2.createId() }).whereIn("id", ids);
@@ -6194,7 +6224,7 @@ const createDocumentIdColumn = async (knex2, tableName) => {
6194
6224
  });
6195
6225
  };
6196
6226
  const hasLocalizationsJoinTable = async (knex2, tableName) => {
6197
- const joinTableName = identifiers.getJoinTableName(tableName, "localizations");
6227
+ const joinTableName = _.snakeCase(`${tableName}_localizations_links`);
6198
6228
  return knex2.schema.hasTable(joinTableName);
6199
6229
  };
6200
6230
  const createdDocumentId = {
@@ -6266,21 +6296,24 @@ const renameIndex = async (knex2, db, diff) => {
6266
6296
  debug(`not renaming index ${full.indexName} because name hasn't changed`);
6267
6297
  return;
6268
6298
  }
6269
- 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")) {
6270
6300
  return;
6271
6301
  }
6272
6302
  debug(`renaming index from ${full.indexName} to ${short.indexName}`);
6273
6303
  try {
6274
6304
  await knex2.transaction(async (trx) => {
6275
6305
  if (client === "mysql" || client === "mariadb") {
6276
- await knex2.raw(
6277
- `ALTER TABLE \`${full.tableName}\` RENAME INDEX \`${full.indexName}\` TO \`${short.indexName}\``
6278
- ).transacting(trx);
6306
+ await knex2.raw("ALTER TABLE ?? RENAME INDEX ?? TO ??", [
6307
+ full.tableName,
6308
+ full.indexName,
6309
+ short.indexName
6310
+ ]).transacting(trx);
6279
6311
  } else if (client === "pg" || client === "postgres") {
6280
- await knex2.raw(`ALTER INDEX "${full.indexName}" RENAME TO "${short.indexName}"`).transacting(trx);
6281
- } 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}`);
6282
6315
  } else {
6283
- debug("No db client name matches, not creating index");
6316
+ debug(`No db client name matches, not renaming index ${full.indexName}`);
6284
6317
  }
6285
6318
  });
6286
6319
  } catch (err) {