@strapi/database 4.4.0-alpha.0 → 4.4.0-beta.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.
@@ -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;
@@ -34,7 +34,7 @@ const processData = (metadata, data = {}, { withDefaults = false } = {}) => {
34
34
 
35
35
  const obj = {};
36
36
 
37
- for (const attributeName in attributes) {
37
+ for (const attributeName of Object.keys(attributes)) {
38
38
  const attribute = attributes[attributeName];
39
39
 
40
40
  if (types.isScalar(attribute.type)) {
@@ -333,7 +333,7 @@ const createEntityManager = (db) => {
333
333
  async attachRelations(uid, id, data) {
334
334
  const { attributes } = db.metadata.get(uid);
335
335
 
336
- for (const attributeName in attributes) {
336
+ for (const attributeName of Object.keys(attributes)) {
337
337
  const attribute = attributes[attributeName];
338
338
 
339
339
  const isValidLink = _.has(attributeName, data) && !_.isNil(data[attributeName]);
@@ -487,7 +487,7 @@ const createEntityManager = (db) => {
487
487
  async updateRelations(uid, id, data) {
488
488
  const { attributes } = db.metadata.get(uid);
489
489
 
490
- for (const attributeName in attributes) {
490
+ for (const attributeName of Object.keys(attributes)) {
491
491
  const attribute = attributes[attributeName];
492
492
 
493
493
  if (attribute.type !== 'relation' || !_.has(attributeName, data)) {
@@ -667,7 +667,7 @@ const createEntityManager = (db) => {
667
667
  async deleteRelations(uid, id) {
668
668
  const { attributes } = db.metadata.get(uid);
669
669
 
670
- for (const attributeName in attributes) {
670
+ for (const attributeName of Object.keys(attributes)) {
671
671
  const attribute = attributes[attributeName];
672
672
 
673
673
  if (attribute.type !== 'relation') {
@@ -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
@@ -54,7 +54,7 @@ const createLifecyclesProvider = (db) => {
54
54
  * @param {Map<any, any>} states
55
55
  */
56
56
  async run(action, uid, properties, states = new Map()) {
57
- for (let i = 0; i < subscribers.length; i++) {
57
+ for (let i = 0; i < subscribers.length; i += 1) {
58
58
  const subscriber = subscribers[i];
59
59
  if (typeof subscriber === 'function') {
60
60
  const state = states.get(subscriber) || {};
@@ -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: {
@@ -71,7 +71,7 @@ const applyJoin = (qb, join) => {
71
71
  inner.on(`${rootTable}.${rootColumn}`, `${alias}.${referencedColumn}`);
72
72
 
73
73
  if (on) {
74
- for (const key in on) {
74
+ for (const key of Object.keys(on)) {
75
75
  inner.onVal(`${alias}.${key}`, on[key]);
76
76
  }
77
77
  }
@@ -8,7 +8,7 @@ const { fromRow } = require('./transform');
8
8
  const getRootLevelPopulate = (meta) => {
9
9
  const populate = {};
10
10
 
11
- for (const attributeName in meta.attributes) {
11
+ for (const attributeName of Object.keys(meta.attributes)) {
12
12
  const attribute = meta.attributes[attributeName];
13
13
  if (attribute.type === 'relation') {
14
14
  populate[attributeName] = true;
@@ -72,7 +72,7 @@ const processPopulate = (populate, ctx) => {
72
72
  }
73
73
 
74
74
  const finalPopulate = {};
75
- for (const key in populateMap) {
75
+ for (const key of Object.keys(populateMap)) {
76
76
  const attribute = meta.attributes[key];
77
77
 
78
78
  if (!attribute) {
@@ -119,7 +119,7 @@ const applyPopulate = async (results, populate, ctx) => {
119
119
  return results;
120
120
  }
121
121
 
122
- for (const key in populate) {
122
+ for (const key of Object.keys(populate)) {
123
123
  const attribute = meta.attributes[key];
124
124
  const targetMeta = db.metadata.get(attribute.target);
125
125
 
@@ -540,7 +540,7 @@ const applyPopulate = async (results, populate, ctx) => {
540
540
  }, {});
541
541
 
542
542
  const map = {};
543
- for (const type in idsByType) {
543
+ for (const type of Object.keys(idsByType)) {
544
544
  const ids = idsByType[type];
545
545
 
546
546
  // type was removed but still in morph relation
@@ -604,7 +604,7 @@ const applyPopulate = async (results, populate, ctx) => {
604
604
  }, {});
605
605
 
606
606
  const map = {};
607
- for (const type in idsByType) {
607
+ for (const type of Object.keys(idsByType)) {
608
608
  const ids = idsByType[type];
609
609
 
610
610
  // type was removed but still in morph relation
@@ -53,7 +53,7 @@ const toRow = (meta, data = {}) => {
53
53
 
54
54
  const { attributes } = meta;
55
55
 
56
- for (const key in data) {
56
+ for (const key of Object.keys(data)) {
57
57
  const attribute = attributes[key];
58
58
 
59
59
  if (!attribute || attribute.columnName === key) {
@@ -76,7 +76,7 @@ const processAttributeWhere = (attribute, where, operator = '$eq') => {
76
76
 
77
77
  const filters = {};
78
78
 
79
- for (const key in where) {
79
+ for (const key of Object.keys(where)) {
80
80
  const value = where[key];
81
81
 
82
82
  if (!isOperator(key)) {
@@ -119,7 +119,7 @@ const processWhere = (where, ctx) => {
119
119
  const filters = {};
120
120
 
121
121
  // for each key in where
122
- for (const key in where) {
122
+ for (const key of Object.keys(where)) {
123
123
  const value = where[key];
124
124
 
125
125
  // if operator $and $or then loop over them
@@ -27,7 +27,13 @@ const createQueryBuilder = (uid, db) => {
27
27
  };
28
28
 
29
29
  let counter = 0;
30
- const getAlias = () => `t${counter++}`;
30
+ const getAlias = () => {
31
+ const alias = `t${counter}`;
32
+
33
+ counter += 1;
34
+
35
+ return alias;
36
+ };
31
37
 
32
38
  return {
33
39
  alias: getAlias(),
@@ -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) {
@@ -25,7 +25,7 @@ const createTable = (meta) => {
25
25
  columns: [],
26
26
  };
27
27
 
28
- for (const key in meta.attributes) {
28
+ for (const key of Object.keys(meta.attributes)) {
29
29
  const attribute = meta.attributes[key];
30
30
 
31
31
  if (types.isRelation(attribute.type)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/database",
3
- "version": "4.4.0-alpha.0",
3
+ "version": "4.4.0-beta.0",
4
4
  "description": "Strapi's database layer",
5
5
  "homepage": "https://strapi.io",
6
6
  "bugs": {
@@ -39,8 +39,8 @@
39
39
  "umzug": "3.1.1"
40
40
  },
41
41
  "engines": {
42
- "node": ">=14.19.1 <=16.x.x",
42
+ "node": ">=14.19.1 <=18.x.x",
43
43
  "npm": ">=6.0.0"
44
44
  },
45
- "gitHead": "fc78298ae4f9b247d636beda568734d5f8ed7b3e"
45
+ "gitHead": "cae16f7f259fa4473a55e8fea57839cda98f34ae"
46
46
  }