@strapi/database 4.4.3 → 4.5.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.
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash/fp');
4
+
5
+ const types = require('../../../types');
6
+
7
+ const getRootLevelPopulate = (meta) => {
8
+ const populate = {};
9
+
10
+ for (const attributeName of Object.keys(meta.attributes)) {
11
+ const attribute = meta.attributes[attributeName];
12
+ if (attribute.type === 'relation') {
13
+ populate[attributeName] = true;
14
+ }
15
+ }
16
+
17
+ return populate;
18
+ };
19
+
20
+ /**
21
+ * Converts and prepares the query for populate
22
+ *
23
+ * @param {boolean|string[]|object} populate populate param
24
+ * @param {object} ctx query context
25
+ * @param {object} ctx.db database instance
26
+ * @param {object} ctx.qb query builder instance
27
+ * @param {string} ctx.uid model uid
28
+ */
29
+ const processPopulate = (populate, ctx) => {
30
+ const { qb, db, uid } = ctx;
31
+ const meta = db.metadata.get(uid);
32
+
33
+ let populateMap = {};
34
+
35
+ if (populate === false || _.isNil(populate)) {
36
+ return null;
37
+ }
38
+
39
+ if (populate === true) {
40
+ populateMap = getRootLevelPopulate(meta);
41
+ } else if (Array.isArray(populate)) {
42
+ for (const key of populate) {
43
+ const [root, ...rest] = key.split('.');
44
+
45
+ if (rest.length > 0) {
46
+ const subPopulate = rest.join('.');
47
+
48
+ if (populateMap[root]) {
49
+ if (populateMap[root] === true) {
50
+ populateMap[root] = {
51
+ populate: [subPopulate],
52
+ };
53
+ } else {
54
+ populateMap[root].populate = [subPopulate].concat(populateMap[root].populate || []);
55
+ }
56
+ } else {
57
+ populateMap[root] = {
58
+ populate: [subPopulate],
59
+ };
60
+ }
61
+ } else {
62
+ populateMap[root] = populateMap[root] ? populateMap[root] : true;
63
+ }
64
+ }
65
+ } else {
66
+ populateMap = populate;
67
+ }
68
+
69
+ if (!_.isPlainObject(populateMap)) {
70
+ throw new Error('Populate must be an object');
71
+ }
72
+
73
+ const finalPopulate = {};
74
+ for (const key of Object.keys(populateMap)) {
75
+ const attribute = meta.attributes[key];
76
+
77
+ if (!attribute) {
78
+ continue;
79
+ }
80
+
81
+ if (!types.isRelation(attribute.type)) {
82
+ continue;
83
+ }
84
+
85
+ // make sure id is present for future populate queries
86
+ if (_.has('id', meta.attributes)) {
87
+ qb.addSelect('id');
88
+ }
89
+
90
+ finalPopulate[key] = populateMap[key];
91
+ }
92
+
93
+ return finalPopulate;
94
+ };
95
+
96
+ module.exports = processPopulate;
@@ -6,6 +6,7 @@ const types = require('../../types');
6
6
  const { createField } = require('../../fields');
7
7
  const { createJoin } = require('./join');
8
8
  const { toColumnName } = require('./transform');
9
+ const { isKnexQuery } = require('../../utils/knex');
9
10
 
10
11
  const GROUP_OPERATORS = ['$and', '$or'];
11
12
  const OPERATORS = [
@@ -206,12 +207,12 @@ const applyOperator = (qb, column, operator, value) => {
206
207
  }
207
208
 
208
209
  case '$in': {
209
- qb.whereIn(column, _.castArray(value));
210
+ qb.whereIn(column, isKnexQuery(value) ? value : _.castArray(value));
210
211
  break;
211
212
  }
212
213
 
213
214
  case '$notIn': {
214
- qb.whereNotIn(column, _.castArray(value));
215
+ qb.whereNotIn(column, isKnexQuery(value) ? value : _.castArray(value));
215
216
  break;
216
217
  }
217
218
 
@@ -4,33 +4,41 @@ const _ = require('lodash/fp');
4
4
 
5
5
  const helpers = require('./helpers');
6
6
 
7
- const createQueryBuilder = (uid, db) => {
7
+ const createQueryBuilder = (uid, db, initialState = {}) => {
8
8
  const meta = db.metadata.get(uid);
9
9
  const { tableName } = meta;
10
10
 
11
- const state = {
12
- type: 'select',
13
- select: [],
14
- count: null,
15
- max: null,
16
- first: false,
17
- data: null,
18
- where: [],
19
- joins: [],
20
- populate: null,
21
- limit: null,
22
- offset: null,
23
- transaction: null,
24
- forUpdate: false,
25
- orderBy: [],
26
- groupBy: [],
27
- };
11
+ const state = _.defaults(
12
+ {
13
+ type: 'select',
14
+ select: [],
15
+ count: null,
16
+ max: null,
17
+ first: false,
18
+ data: null,
19
+ where: [],
20
+ joins: [],
21
+ populate: null,
22
+ limit: null,
23
+ offset: null,
24
+ transaction: null,
25
+ forUpdate: false,
26
+ onConflict: null,
27
+ merge: null,
28
+ ignore: false,
29
+ orderBy: [],
30
+ groupBy: [],
31
+ increments: [],
32
+ decrements: [],
33
+ aliasCounter: 0,
34
+ },
35
+ initialState
36
+ );
28
37
 
29
- let counter = 0;
30
38
  const getAlias = () => {
31
- const alias = `t${counter}`;
39
+ const alias = `t${state.aliasCounter}`;
32
40
 
33
- counter += 1;
41
+ state.aliasCounter += 1;
34
42
 
35
43
  return alias;
36
44
  };
@@ -40,6 +48,10 @@ const createQueryBuilder = (uid, db) => {
40
48
  getAlias,
41
49
  state,
42
50
 
51
+ clone() {
52
+ return createQueryBuilder(uid, db, state);
53
+ },
54
+
43
55
  select(args) {
44
56
  state.type = 'select';
45
57
  state.select = _.uniq(_.castArray(args));
@@ -60,6 +72,24 @@ const createQueryBuilder = (uid, db) => {
60
72
  return this;
61
73
  },
62
74
 
75
+ onConflict(args) {
76
+ state.onConflict = args;
77
+
78
+ return this;
79
+ },
80
+
81
+ merge(args) {
82
+ state.merge = args;
83
+
84
+ return this;
85
+ },
86
+
87
+ ignore() {
88
+ state.ignore = true;
89
+
90
+ return this;
91
+ },
92
+
63
93
  delete() {
64
94
  state.type = 'delete';
65
95
 
@@ -77,6 +107,20 @@ const createQueryBuilder = (uid, db) => {
77
107
  return this;
78
108
  },
79
109
 
110
+ increment(column, amount = 1) {
111
+ state.type = 'update';
112
+ state.increments.push({ column, amount });
113
+
114
+ return this;
115
+ },
116
+
117
+ decrement(column, amount = 1) {
118
+ state.type = 'update';
119
+ state.decrements.push({ column, amount });
120
+
121
+ return this;
122
+ },
123
+
80
124
  count(count = 'id') {
81
125
  state.type = 'count';
82
126
  state.count = count;
@@ -195,7 +239,24 @@ const createQueryBuilder = (uid, db) => {
195
239
  },
196
240
 
197
241
  join(join) {
198
- state.joins.push(join);
242
+ if (!join.targetField) {
243
+ state.joins.push(join);
244
+ return this;
245
+ }
246
+
247
+ const model = db.metadata.get(uid);
248
+ const attribute = model.attributes[join.targetField];
249
+
250
+ helpers.createJoin(
251
+ { db, qb: this },
252
+ {
253
+ alias: this.alias,
254
+ refAlias: join.alias,
255
+ attributeName: join.targetField,
256
+ attribute,
257
+ }
258
+ );
259
+
199
260
  return this;
200
261
  },
201
262
 
@@ -325,7 +386,9 @@ const createQueryBuilder = (uid, db) => {
325
386
  break;
326
387
  }
327
388
  case 'update': {
328
- qb.update(state.data);
389
+ if (state.data) {
390
+ qb.update(state.data);
391
+ }
329
392
  break;
330
393
  }
331
394
  case 'delete': {
@@ -350,6 +413,22 @@ const createQueryBuilder = (uid, db) => {
350
413
  qb.forUpdate();
351
414
  }
352
415
 
416
+ if (!_.isEmpty(state.increments)) {
417
+ state.increments.forEach((incr) => qb.increment(incr.column, incr.amount));
418
+ }
419
+
420
+ if (!_.isEmpty(state.decrements)) {
421
+ state.decrements.forEach((decr) => qb.decrement(decr.column, decr.amount));
422
+ }
423
+
424
+ if (state.onConflict) {
425
+ if (state.merge) {
426
+ qb.onConflict(state.onConflict).merge(state.merge);
427
+ } else if (state.ignore) {
428
+ qb.onConflict(state.onConflict).ignore();
429
+ }
430
+ }
431
+
353
432
  if (state.limit) {
354
433
  qb.limit(state.limit);
355
434
  }
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
4
+ const { isKnexQuery } = require('../utils/knex');
5
+
6
+ let strapi;
7
+
8
+ describe('knex', () => {
9
+ beforeAll(async () => {
10
+ strapi = await createStrapiInstance();
11
+ });
12
+
13
+ afterAll(async () => {
14
+ await strapi.destroy();
15
+ });
16
+
17
+ describe('isKnexQuery', () => {
18
+ test('knex query: true', () => {
19
+ const res = isKnexQuery(strapi.db.connection('strapi_core_store_settings'));
20
+ expect(res).toBe(true);
21
+ });
22
+
23
+ test('knex raw: true', () => {
24
+ const res = isKnexQuery(strapi.db.connection.raw('SELECT * FROM strapi_core_store_settings'));
25
+ expect(res).toBe(true);
26
+ });
27
+
28
+ test.each([[''], [{}], [[]], [2], [new Date()]])('%s: false', (value) => {
29
+ const res = isKnexQuery(value);
30
+ expect(res).toBe(false);
31
+ });
32
+ });
33
+ });
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ const KnexBuilder = require('knex/lib/query/querybuilder');
4
+ const KnexRaw = require('knex/lib/raw');
5
+
6
+ const isKnexQuery = (value) => {
7
+ return value instanceof KnexBuilder || value instanceof KnexRaw;
8
+ };
9
+
10
+ module.exports = {
11
+ isKnexQuery,
12
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/database",
3
- "version": "4.4.3",
3
+ "version": "4.5.0-beta.0",
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": "f99314ead7f0fdf82b921b9d8f0282a91e952ca8"
45
+ "gitHead": "ee98b9a9cbb6e0e07e781ff9e87eb170c72e50df"
46
46
  }