@strapi/database 5.0.0-rc.9 → 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 +39 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +39 -18
- 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,
|
|
@@ -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 {
|
|
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({
|
|
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 (
|
|
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 =
|
|
6148
|
-
const inverseJoinColumn =
|
|
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:
|
|
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 =
|
|
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.
|
|
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
|
-
|
|
6258
|
-
|
|
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(
|
|
6261
|
-
} 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}`);
|
|
6262
6283
|
} else {
|
|
6263
|
-
debug(
|
|
6284
|
+
debug(`No db client name matches, not renaming index ${full.indexName}`);
|
|
6264
6285
|
}
|
|
6265
6286
|
});
|
|
6266
6287
|
} catch (err) {
|