@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.
- package/dist/entity-manager/index.d.ts.map +1 -1
- package/dist/index.js +43 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +43 -20
- package/dist/index.mjs.map +1 -1
- package/dist/metadata/metadata.d.ts.map +1 -1
- package/dist/metadata/relations.d.ts.map +1 -1
- package/dist/migrations/internal-migrations/5.0.0-02-document-id.d.ts.map +1 -1
- package/dist/query/helpers/populate/apply.d.ts.map +1 -1
- package/dist/types/index.d.ts +14 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +5 -5
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;
|
|
@@ -3436,7 +3447,11 @@ const oneToMany = async (input, ctx) => {
|
|
|
3436
3447
|
const { db, qb } = ctx;
|
|
3437
3448
|
const fromTargetRow = (rowOrRows) => fromRow(targetMeta, rowOrRows);
|
|
3438
3449
|
if ("joinColumn" in attribute && attribute.joinColumn) {
|
|
3439
|
-
const {
|
|
3450
|
+
const {
|
|
3451
|
+
name: joinColumnName,
|
|
3452
|
+
referencedColumn: referencedColumnName,
|
|
3453
|
+
on
|
|
3454
|
+
} = attribute.joinColumn;
|
|
3440
3455
|
const referencedValues = _.uniq(
|
|
3441
3456
|
results.map((r) => r[joinColumnName]).filter((value) => !_.isNil(value))
|
|
3442
3457
|
);
|
|
@@ -3446,7 +3461,10 @@ const oneToMany = async (input, ctx) => {
|
|
|
3446
3461
|
});
|
|
3447
3462
|
return;
|
|
3448
3463
|
}
|
|
3449
|
-
const rows = await db.entityManager.createQueryBuilder(targetMeta.uid).init(populateValue).addSelect(`${qb.alias}.${referencedColumnName}`).where({
|
|
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 });
|
|
3450
3468
|
const map2 = _.groupBy(referencedColumnName)(rows);
|
|
3451
3469
|
results.forEach((result) => {
|
|
3452
3470
|
result[attributeName] = fromTargetRow(map2[result[joinColumnName]] || []);
|
|
@@ -5253,8 +5271,10 @@ const processData = (metadata, data = {}, { withDefaults = false } = {}) => {
|
|
|
5253
5271
|
if ("joinColumn" in attribute && attribute.joinColumn && attribute.owner) {
|
|
5254
5272
|
const joinColumnName = attribute.joinColumn.name;
|
|
5255
5273
|
const attrValue = !isUndefined(data[attributeName]) ? data[attributeName] : data[joinColumnName];
|
|
5256
|
-
if (
|
|
5274
|
+
if (isNull(attrValue)) {
|
|
5257
5275
|
obj[joinColumnName] = attrValue;
|
|
5276
|
+
} else if (!isUndefined(attrValue)) {
|
|
5277
|
+
obj[joinColumnName] = toId(attrValue);
|
|
5258
5278
|
}
|
|
5259
5279
|
continue;
|
|
5260
5280
|
}
|
|
@@ -6142,15 +6162,15 @@ const getNextIdsToCreateDocumentId = async (db, knex2, {
|
|
|
6142
6162
|
};
|
|
6143
6163
|
const migrateDocumentIdsWithLocalizations = async (db, knex2, meta) => {
|
|
6144
6164
|
const singularName = meta.singularName.toLowerCase();
|
|
6145
|
-
const joinColumn =
|
|
6146
|
-
const inverseJoinColumn =
|
|
6165
|
+
const joinColumn = snakeCase(`${singularName}_id`);
|
|
6166
|
+
const inverseJoinColumn = snakeCase(`inv_${singularName}_id`);
|
|
6147
6167
|
let ids;
|
|
6148
6168
|
do {
|
|
6149
6169
|
ids = await getNextIdsToCreateDocumentId(db, knex2, {
|
|
6150
6170
|
joinColumn,
|
|
6151
6171
|
inverseJoinColumn,
|
|
6152
6172
|
tableName: meta.tableName,
|
|
6153
|
-
joinTableName:
|
|
6173
|
+
joinTableName: snakeCase(`${meta.tableName}_localizations_links`)
|
|
6154
6174
|
});
|
|
6155
6175
|
if (ids.length > 0) {
|
|
6156
6176
|
await knex2(meta.tableName).update({ document_id: createId() }).whereIn("id", ids);
|
|
@@ -6172,7 +6192,7 @@ const createDocumentIdColumn = async (knex2, tableName) => {
|
|
|
6172
6192
|
});
|
|
6173
6193
|
};
|
|
6174
6194
|
const hasLocalizationsJoinTable = async (knex2, tableName) => {
|
|
6175
|
-
const joinTableName =
|
|
6195
|
+
const joinTableName = snakeCase(`${tableName}_localizations_links`);
|
|
6176
6196
|
return knex2.schema.hasTable(joinTableName);
|
|
6177
6197
|
};
|
|
6178
6198
|
const createdDocumentId = {
|
|
@@ -6244,21 +6264,24 @@ const renameIndex = async (knex2, db, diff) => {
|
|
|
6244
6264
|
debug(`not renaming index ${full.indexName} because name hasn't changed`);
|
|
6245
6265
|
return;
|
|
6246
6266
|
}
|
|
6247
|
-
if (short.indexName.
|
|
6267
|
+
if (short.indexName.endsWith("fk") || full.indexName.endsWith("fk")) {
|
|
6248
6268
|
return;
|
|
6249
6269
|
}
|
|
6250
6270
|
debug(`renaming index from ${full.indexName} to ${short.indexName}`);
|
|
6251
6271
|
try {
|
|
6252
6272
|
await knex2.transaction(async (trx) => {
|
|
6253
6273
|
if (client === "mysql" || client === "mariadb") {
|
|
6254
|
-
await knex2.raw(
|
|
6255
|
-
|
|
6256
|
-
|
|
6274
|
+
await knex2.raw("ALTER TABLE ?? RENAME INDEX ?? TO ??", [
|
|
6275
|
+
full.tableName,
|
|
6276
|
+
full.indexName,
|
|
6277
|
+
short.indexName
|
|
6278
|
+
]).transacting(trx);
|
|
6257
6279
|
} else if (client === "pg" || client === "postgres") {
|
|
6258
|
-
await knex2.raw(
|
|
6259
|
-
} else if (
|
|
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}`);
|
|
6260
6283
|
} else {
|
|
6261
|
-
debug(
|
|
6284
|
+
debug(`No db client name matches, not renaming index ${full.indexName}`);
|
|
6262
6285
|
}
|
|
6263
6286
|
});
|
|
6264
6287
|
} catch (err) {
|