@strapi/database 4.3.5 → 4.3.8

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.
@@ -44,6 +44,10 @@ class Dialect {
44
44
 
45
45
  throw new Error(error.message);
46
46
  }
47
+
48
+ canAddIncrements() {
49
+ return true;
50
+ }
47
51
  }
48
52
 
49
53
  module.exports = {
@@ -24,6 +24,10 @@ class SqliteDialect extends Dialect {
24
24
  fse.ensureDirSync(dbDir);
25
25
  }
26
26
 
27
+ useReturning() {
28
+ return true;
29
+ }
30
+
27
31
  async initialize() {
28
32
  await this.db.connection.raw('pragma foreign_keys = on');
29
33
  }
@@ -68,6 +72,10 @@ class SqliteDialect extends Dialect {
68
72
  }
69
73
  }
70
74
  }
75
+
76
+ canAddIncrements() {
77
+ return false;
78
+ }
71
79
  }
72
80
 
73
81
  module.exports = SqliteDialect;
@@ -776,7 +776,6 @@ const createEntityManager = (db) => {
776
776
  }
777
777
  },
778
778
 
779
- // TODO: support multiple relations at once with the populate syntax
780
779
  // TODO: add lifecycle events
781
780
  async populate(uid, entity, populate) {
782
781
  const entry = await this.findOne(uid, {
@@ -788,30 +787,37 @@ const createEntityManager = (db) => {
788
787
  return { ...entity, ...entry };
789
788
  },
790
789
 
791
- // TODO: support multiple relations at once with the populate syntax
792
790
  // TODO: add lifecycle events
793
- async load(uid, entity, field, params) {
791
+ async load(uid, entity, fields, params) {
794
792
  const { attributes } = db.metadata.get(uid);
795
793
 
796
- const attribute = attributes[field];
794
+ const fieldsArr = _.castArray(fields);
795
+ fieldsArr.forEach((field) => {
796
+ const attribute = attributes[field];
797
797
 
798
- if (!attribute || attribute.type !== 'relation') {
799
- throw new Error('Invalid load. Expected a relational attribute');
800
- }
798
+ if (!attribute || attribute.type !== 'relation') {
799
+ throw new Error(`Invalid load. Expected ${field} to be a relational attribute`);
800
+ }
801
+ });
801
802
 
802
803
  const entry = await this.findOne(uid, {
803
804
  select: ['id'],
804
805
  where: { id: entity.id },
805
- populate: {
806
- [field]: params || true,
807
- },
806
+ populate: fieldsArr.reduce((acc, field) => {
807
+ acc[field] = params || true;
808
+ return acc;
809
+ }, {}),
808
810
  });
809
811
 
810
812
  if (!entry) {
811
813
  return null;
812
814
  }
813
815
 
814
- return entry[field];
816
+ if (Array.isArray(fields)) {
817
+ return _.pick(fields, entry);
818
+ }
819
+
820
+ return entry[fields];
815
821
  },
816
822
 
817
823
  // cascading
@@ -206,6 +206,9 @@ const createMorphToMany = (attributeName, attribute, meta, metadata) => {
206
206
  uid: joinTableName,
207
207
  tableName: joinTableName,
208
208
  attributes: {
209
+ id: {
210
+ type: 'increments',
211
+ },
209
212
  [joinColumnName]: {
210
213
  type: 'integer',
211
214
  column: {
@@ -404,6 +407,9 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
404
407
  uid: joinTableName,
405
408
  tableName: joinTableName,
406
409
  attributes: {
410
+ id: {
411
+ type: 'increments',
412
+ },
407
413
  [joinColumnName]: {
408
414
  type: 'integer',
409
415
  column: {
@@ -290,13 +290,16 @@ const createHelpers = (db) => {
290
290
  }
291
291
 
292
292
  // Update existing columns / foreign keys / indexes
293
-
294
293
  for (const updatedColumn of table.columns.updated) {
295
294
  debug(`Updating column ${updatedColumn.name}`);
296
295
 
297
296
  const { object } = updatedColumn;
298
297
 
299
- createColumn(tableBuilder, object).alter();
298
+ if (object.type === 'increments') {
299
+ createColumn(tableBuilder, { ...object, type: 'integer' }).alter();
300
+ } else {
301
+ createColumn(tableBuilder, object).alter();
302
+ }
300
303
  }
301
304
 
302
305
  for (const updatedForeignKey of table.foreignKeys.updated) {
@@ -311,7 +314,13 @@ const createHelpers = (db) => {
311
314
 
312
315
  for (const addedColumn of table.columns.added) {
313
316
  debug(`Creating column ${addedColumn.name}`);
314
- createColumn(tableBuilder, addedColumn);
317
+
318
+ if (addedColumn.type === 'increments' && !db.dialect.canAddIncrements()) {
319
+ tableBuilder.integer(addedColumn.name).unsigned();
320
+ tableBuilder.primary(addedColumn.name);
321
+ } else {
322
+ createColumn(tableBuilder, addedColumn);
323
+ }
315
324
  }
316
325
 
317
326
  for (const addedForeignKey of table.foreignKeys.added) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/database",
3
- "version": "4.3.5",
3
+ "version": "4.3.8",
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 <=16.x.x",
43
43
  "npm": ">=6.0.0"
44
44
  },
45
- "gitHead": "a52b0535513ff35b4ef46c9daf7be48f2ba71737"
45
+ "gitHead": "c44f21d5ba95d3f2fbba239cf241081dddd54dd7"
46
46
  }