@strapi/database 4.5.0-alpha.0 → 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;
@@ -23,6 +23,9 @@ const createQueryBuilder = (uid, db, initialState = {}) => {
23
23
  offset: null,
24
24
  transaction: null,
25
25
  forUpdate: false,
26
+ onConflict: null,
27
+ merge: null,
28
+ ignore: false,
26
29
  orderBy: [],
27
30
  groupBy: [],
28
31
  increments: [],
@@ -69,6 +72,24 @@ const createQueryBuilder = (uid, db, initialState = {}) => {
69
72
  return this;
70
73
  },
71
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
+
72
93
  delete() {
73
94
  state.type = 'delete';
74
95
 
@@ -400,6 +421,14 @@ const createQueryBuilder = (uid, db, initialState = {}) => {
400
421
  state.decrements.forEach((decr) => qb.decrement(decr.column, decr.amount));
401
422
  }
402
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
+
403
432
  if (state.limit) {
404
433
  qb.limit(state.limit);
405
434
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/database",
3
- "version": "4.5.0-alpha.0",
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": "c9a98c4dbcf3c4f2a449f8d96e7cbe4cd9b1e0f5"
45
+ "gitHead": "ee98b9a9cbb6e0e07e781ff9e87eb170c72e50df"
46
46
  }