@strapi/database 4.5.0-beta.0 → 4.5.1

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.
@@ -982,7 +982,7 @@ const createEntityManager = (db) => {
982
982
  }
983
983
 
984
984
  // Delete the previous relations for anyToOne relations
985
- if (isBidirectional(attribute) && isAnyToOne(attribute)) {
985
+ if (isAnyToOne(attribute)) {
986
986
  await deletePreviousAnyToOneRelations({
987
987
  id,
988
988
  attribute,
@@ -65,10 +65,8 @@ const deletePreviousAnyToOneRelations = async ({
65
65
  const { joinTable } = attribute;
66
66
  const { joinColumn, inverseJoinColumn } = joinTable;
67
67
 
68
- if (!(isBidirectional(attribute) && isAnyToOne(attribute))) {
69
- throw new Error(
70
- 'deletePreviousAnyToOneRelations can only be called for bidirectional anyToOne relations'
71
- );
68
+ if (!isAnyToOne(attribute)) {
69
+ throw new Error('deletePreviousAnyToOneRelations can only be called for anyToOne relations');
72
70
  }
73
71
  // handling manyToOne
74
72
  if (isManyToAny(attribute)) {
@@ -228,8 +226,7 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
228
226
  // https://github.com/knex/knex/issues/2504
229
227
  switch (strapi.db.dialect.client) {
230
228
  case 'mysql':
231
- await db
232
- .getConnection()
229
+ await db.connection
233
230
  .raw(
234
231
  `UPDATE
235
232
  ?? as a,
@@ -245,17 +242,16 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
245
242
  .transacting(trx);
246
243
  break;
247
244
  default:
248
- await db
249
- .getConnection()
245
+ await db.connection
250
246
  .raw(
251
247
  `UPDATE ?? as a
252
- SET ${update.join(', ')}
253
- FROM (
254
- SELECT ${select.join(', ')}
255
- FROM ??
256
- WHERE ${where.join(' OR ')}
257
- ) AS b
258
- WHERE b.id = a.id`,
248
+ SET ${update.join(', ')}
249
+ FROM (
250
+ SELECT ${select.join(', ')}
251
+ FROM ??
252
+ WHERE ${where.join(' OR ')}
253
+ ) AS b
254
+ WHERE b.id = a.id`,
259
255
  [joinTable.name, ...updateBinding, ...selectBinding, joinTable.name, ...whereBinding]
260
256
  )
261
257
  .transacting(trx);
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { LifecycleProvider } from './lifecycles';
2
2
  import { MigrationProvider } from './migrations';
3
- import { SchemaProvideer } from './schema';
3
+ import { SchemaProvider } from './schema';
4
4
 
5
5
  type LogicalOperators<T> = {
6
6
  $and?: WhereParams<T>[];
@@ -154,7 +154,7 @@ interface DatabaseConfig {
154
154
  models: ModelConfig[];
155
155
  }
156
156
  export interface Database {
157
- schema: SchemaProvideer;
157
+ schema: SchemaProvider;
158
158
  lifecycles: LifecycleProvider;
159
159
  migrations: MigrationProvider;
160
160
  entityManager: EntityManager;
@@ -9,6 +9,21 @@ class Metadata extends Map {
9
9
  add(meta) {
10
10
  return this.set(meta.uid, meta);
11
11
  }
12
+
13
+ /**
14
+ * Validate the DB metadata, throwing an error if a duplicate DB table name is detected
15
+ */
16
+ validate() {
17
+ const seenTables = new Map();
18
+ for (const meta of this.values()) {
19
+ if (seenTables.get(meta.tableName)) {
20
+ throw new Error(
21
+ `DB table "${meta.tableName}" already exists. Change the collectionName of the related content type.`
22
+ );
23
+ }
24
+ seenTables.set(meta.tableName, true);
25
+ }
26
+ }
12
27
  }
13
28
 
14
29
  // TODO: check if there isn't an attribute with an id already
@@ -81,6 +96,7 @@ const createMetadata = (models = []) => {
81
96
  meta.columnToAttribute = columnToAttribute;
82
97
  }
83
98
 
99
+ metadata.validate();
84
100
  return metadata;
85
101
  };
86
102
 
@@ -82,7 +82,13 @@ const processPopulate = (populate, ctx) => {
82
82
  continue;
83
83
  }
84
84
 
85
- // make sure id is present for future populate queries
85
+ // Make sure to query the join column value if needed,
86
+ // so that we can apply the populate later on
87
+ if (attribute.joinColumn) {
88
+ qb.addSelect(attribute.joinColumn.name);
89
+ }
90
+
91
+ // Make sure id is present for future populate queries
86
92
  if (_.has('id', meta.attributes)) {
87
93
  qb.addSelect('id');
88
94
  }
@@ -38,7 +38,7 @@ export interface Model {
38
38
  };
39
39
  }
40
40
 
41
- export interface SchemaProvideer {
41
+ export interface SchemaProvider {
42
42
  sync(): Promise<void>;
43
43
  syncSchema(): Promise<void>;
44
44
  reset(): Promise<void>;
@@ -46,4 +46,4 @@ export interface SchemaProvideer {
46
46
  drop(): Promise<void>;
47
47
  }
48
48
 
49
- export default function(db: Database): SchemaProvideer;
49
+ export default function(db: Database): SchemaProvider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/database",
3
- "version": "4.5.0-beta.0",
3
+ "version": "4.5.1",
4
4
  "description": "Strapi's database layer",
5
5
  "homepage": "https://strapi.io",
6
6
  "bugs": {
@@ -42,5 +42,5 @@
42
42
  "node": ">=14.19.1 <=18.x.x",
43
43
  "npm": ">=6.0.0"
44
44
  },
45
- "gitHead": "ee98b9a9cbb6e0e07e781ff9e87eb170c72e50df"
45
+ "gitHead": "8c20ea2b5c5a115b78454086ea270dcd59b06004"
46
46
  }