@strapi/database 4.14.4 → 4.14.6

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.
Files changed (242) hide show
  1. package/README.md +3 -0
  2. package/dist/connection.d.ts +3 -0
  3. package/dist/connection.d.ts.map +1 -0
  4. package/dist/dialects/dialect.d.ts +27 -0
  5. package/dist/dialects/dialect.d.ts.map +1 -0
  6. package/dist/dialects/index.d.ts +5 -0
  7. package/dist/dialects/index.d.ts.map +1 -0
  8. package/dist/dialects/mysql/constants.d.ts +3 -0
  9. package/dist/dialects/mysql/constants.d.ts.map +1 -0
  10. package/dist/dialects/mysql/database-inspector.d.ts +12 -0
  11. package/dist/dialects/mysql/database-inspector.d.ts.map +1 -0
  12. package/dist/dialects/mysql/index.d.ts +20 -0
  13. package/dist/dialects/mysql/index.d.ts.map +1 -0
  14. package/dist/dialects/mysql/schema-inspector.d.ts +13 -0
  15. package/dist/dialects/mysql/schema-inspector.d.ts.map +1 -0
  16. package/dist/dialects/postgresql/index.d.ts +14 -0
  17. package/dist/dialects/postgresql/index.d.ts.map +1 -0
  18. package/dist/dialects/postgresql/schema-inspector.d.ts +14 -0
  19. package/dist/dialects/postgresql/schema-inspector.d.ts.map +1 -0
  20. package/dist/dialects/sqlite/index.d.ts +19 -0
  21. package/dist/dialects/sqlite/index.d.ts.map +1 -0
  22. package/dist/dialects/sqlite/schema-inspector.d.ts +13 -0
  23. package/dist/dialects/sqlite/schema-inspector.d.ts.map +1 -0
  24. package/dist/entity-manager/entity-repository.d.ts +4 -0
  25. package/dist/entity-manager/entity-repository.d.ts.map +1 -0
  26. package/dist/entity-manager/index.d.ts +5 -0
  27. package/dist/entity-manager/index.d.ts.map +1 -0
  28. package/dist/entity-manager/morph-relations.d.ts +13 -0
  29. package/dist/entity-manager/morph-relations.d.ts.map +1 -0
  30. package/dist/entity-manager/regular-relations.d.ts +83 -0
  31. package/dist/entity-manager/regular-relations.d.ts.map +1 -0
  32. package/dist/entity-manager/relations/cloning/regular-relations.d.ts +17 -0
  33. package/dist/entity-manager/relations/cloning/regular-relations.d.ts.map +1 -0
  34. package/dist/entity-manager/relations-orderer.d.ts +73 -0
  35. package/dist/entity-manager/relations-orderer.d.ts.map +1 -0
  36. package/dist/entity-manager/types.d.ts +97 -0
  37. package/dist/entity-manager/types.d.ts.map +1 -0
  38. package/dist/errors/database.d.ts +5 -0
  39. package/dist/errors/database.d.ts.map +1 -0
  40. package/dist/errors/index.d.ts +8 -0
  41. package/dist/errors/index.d.ts.map +1 -0
  42. package/dist/errors/invalid-date.d.ts +5 -0
  43. package/dist/errors/invalid-date.d.ts.map +1 -0
  44. package/dist/errors/invalid-datetime.d.ts +5 -0
  45. package/dist/errors/invalid-datetime.d.ts.map +1 -0
  46. package/dist/errors/invalid-relation.d.ts +5 -0
  47. package/dist/errors/invalid-relation.d.ts.map +1 -0
  48. package/dist/errors/invalid-time.d.ts +5 -0
  49. package/dist/errors/invalid-time.d.ts.map +1 -0
  50. package/dist/errors/not-null.d.ts +7 -0
  51. package/dist/errors/not-null.d.ts.map +1 -0
  52. package/dist/fields/biginteger.d.ts +4 -0
  53. package/dist/fields/biginteger.d.ts.map +1 -0
  54. package/dist/fields/boolean.d.ts +6 -0
  55. package/dist/fields/boolean.d.ts.map +1 -0
  56. package/dist/fields/date.d.ts +6 -0
  57. package/dist/fields/date.d.ts.map +1 -0
  58. package/dist/fields/datetime.d.ts +6 -0
  59. package/dist/fields/datetime.d.ts.map +1 -0
  60. package/dist/fields/field.d.ts +7 -0
  61. package/dist/fields/field.d.ts.map +1 -0
  62. package/dist/fields/index.d.ts +4 -0
  63. package/dist/fields/index.d.ts.map +1 -0
  64. package/dist/fields/json.d.ts +6 -0
  65. package/dist/fields/json.d.ts.map +1 -0
  66. package/dist/fields/number.d.ts +6 -0
  67. package/dist/fields/number.d.ts.map +1 -0
  68. package/dist/fields/shared/parsers.d.ts +4 -0
  69. package/dist/fields/shared/parsers.d.ts.map +1 -0
  70. package/dist/fields/string.d.ts +6 -0
  71. package/dist/fields/string.d.ts.map +1 -0
  72. package/dist/fields/time.d.ts +6 -0
  73. package/dist/fields/time.d.ts.map +1 -0
  74. package/dist/fields/timestamp.d.ts +6 -0
  75. package/dist/fields/timestamp.d.ts.map +1 -0
  76. package/dist/index.d.ts +46 -0
  77. package/dist/index.d.ts.map +1 -0
  78. package/dist/index.js +6211 -0
  79. package/dist/index.js.map +1 -0
  80. package/dist/index.mjs +6179 -0
  81. package/dist/index.mjs.map +1 -0
  82. package/dist/lifecycles/index.d.ts +17 -0
  83. package/dist/lifecycles/index.d.ts.map +1 -0
  84. package/dist/lifecycles/subscribers/index.d.ts +5 -0
  85. package/dist/lifecycles/subscribers/index.d.ts.map +1 -0
  86. package/dist/lifecycles/subscribers/models-lifecycles.d.ts +6 -0
  87. package/dist/lifecycles/subscribers/models-lifecycles.d.ts.map +1 -0
  88. package/dist/lifecycles/subscribers/timestamps.d.ts +3 -0
  89. package/dist/lifecycles/subscribers/timestamps.d.ts.map +1 -0
  90. package/dist/lifecycles/types.d.ts +25 -0
  91. package/dist/lifecycles/types.d.ts.map +1 -0
  92. package/dist/metadata/index.d.ts +10 -0
  93. package/dist/metadata/index.d.ts.map +1 -0
  94. package/dist/metadata/metadata.d.ts +22 -0
  95. package/dist/metadata/metadata.d.ts.map +1 -0
  96. package/dist/metadata/relations.d.ts +16 -0
  97. package/dist/metadata/relations.d.ts.map +1 -0
  98. package/dist/migrations/index.d.ts +12 -0
  99. package/dist/migrations/index.d.ts.map +1 -0
  100. package/dist/migrations/storage.d.ts +15 -0
  101. package/dist/migrations/storage.d.ts.map +1 -0
  102. package/dist/query/helpers/index.d.ts +8 -0
  103. package/dist/query/helpers/index.d.ts.map +1 -0
  104. package/dist/query/helpers/join.d.ts +30 -0
  105. package/dist/query/helpers/join.d.ts.map +1 -0
  106. package/dist/query/helpers/order-by.d.ts +14 -0
  107. package/dist/query/helpers/order-by.d.ts.map +1 -0
  108. package/dist/query/helpers/populate/apply.d.ts +11 -0
  109. package/dist/query/helpers/populate/apply.d.ts.map +1 -0
  110. package/dist/query/helpers/populate/index.d.ts +4 -0
  111. package/dist/query/helpers/populate/index.d.ts.map +1 -0
  112. package/dist/query/helpers/populate/process.d.ts +24 -0
  113. package/dist/query/helpers/populate/process.d.ts.map +1 -0
  114. package/dist/query/helpers/search.d.ts +4 -0
  115. package/dist/query/helpers/search.d.ts.map +1 -0
  116. package/dist/query/helpers/streams/index.d.ts +2 -0
  117. package/dist/query/helpers/streams/index.d.ts.map +1 -0
  118. package/dist/query/helpers/streams/readable.d.ts +39 -0
  119. package/dist/query/helpers/streams/readable.d.ts.map +1 -0
  120. package/dist/query/helpers/transform.d.ts +8 -0
  121. package/dist/query/helpers/transform.d.ts.map +1 -0
  122. package/dist/query/helpers/where.d.ts +19 -0
  123. package/dist/query/helpers/where.d.ts.map +1 -0
  124. package/dist/query/index.d.ts +3 -0
  125. package/dist/query/index.d.ts.map +1 -0
  126. package/dist/query/query-builder.d.ts +79 -0
  127. package/dist/query/query-builder.d.ts.map +1 -0
  128. package/dist/query/types.d.ts +8 -0
  129. package/dist/query/types.d.ts.map +1 -0
  130. package/dist/schema/builder.d.ts +33 -0
  131. package/dist/schema/builder.d.ts.map +1 -0
  132. package/dist/schema/diff.d.ts +7 -0
  133. package/dist/schema/diff.d.ts.map +1 -0
  134. package/dist/schema/index.d.ts +20 -0
  135. package/dist/schema/index.d.ts.map +1 -0
  136. package/dist/schema/schema.d.ts +4 -0
  137. package/dist/schema/schema.d.ts.map +1 -0
  138. package/dist/schema/storage.d.ts +10 -0
  139. package/dist/schema/storage.d.ts.map +1 -0
  140. package/dist/schema/types.d.ts +103 -0
  141. package/dist/schema/types.d.ts.map +1 -0
  142. package/dist/transaction-context.d.ts +22 -0
  143. package/dist/transaction-context.d.ts.map +1 -0
  144. package/dist/types/index.d.ts +169 -0
  145. package/dist/types/index.d.ts.map +1 -0
  146. package/dist/utils/content-types.d.ts +13 -0
  147. package/dist/utils/content-types.d.ts.map +1 -0
  148. package/dist/utils/knex.d.ts +12 -0
  149. package/dist/utils/knex.d.ts.map +1 -0
  150. package/dist/utils/types.d.ts +10 -0
  151. package/dist/utils/types.d.ts.map +1 -0
  152. package/dist/validations/index.d.ts +6 -0
  153. package/dist/validations/index.d.ts.map +1 -0
  154. package/dist/validations/relations/bidirectional.d.ts +12 -0
  155. package/dist/validations/relations/bidirectional.d.ts.map +1 -0
  156. package/dist/validations/relations/index.d.ts +7 -0
  157. package/dist/validations/relations/index.d.ts.map +1 -0
  158. package/package.json +20 -8
  159. package/.eslintignore +0 -2
  160. package/.eslintrc.js +0 -4
  161. package/jest.config.js +0 -6
  162. package/lib/__tests__/index.test.js +0 -93
  163. package/lib/__tests__/lifecycles.test.js +0 -55
  164. package/lib/connection.js +0 -64
  165. package/lib/dialects/dialect.js +0 -63
  166. package/lib/dialects/index.js +0 -53
  167. package/lib/dialects/mysql/constants.js +0 -6
  168. package/lib/dialects/mysql/database-inspector.js +0 -37
  169. package/lib/dialects/mysql/index.js +0 -92
  170. package/lib/dialects/mysql/schema-inspector.js +0 -234
  171. package/lib/dialects/postgresql/index.js +0 -65
  172. package/lib/dialects/postgresql/schema-inspector.js +0 -283
  173. package/lib/dialects/sqlite/index.js +0 -87
  174. package/lib/dialects/sqlite/schema-inspector.js +0 -151
  175. package/lib/entity-manager/__tests__/relations-orderer.test.js +0 -186
  176. package/lib/entity-manager/__tests__/sort-connect-array.test.js +0 -79
  177. package/lib/entity-manager/entity-repository.js +0 -164
  178. package/lib/entity-manager/index.js +0 -1385
  179. package/lib/entity-manager/morph-relations.js +0 -63
  180. package/lib/entity-manager/regular-relations.js +0 -506
  181. package/lib/entity-manager/relations/cloning/regular-relations.js +0 -76
  182. package/lib/entity-manager/relations-orderer.js +0 -225
  183. package/lib/errors/database.js +0 -12
  184. package/lib/errors/index.js +0 -17
  185. package/lib/errors/invalid-date.js +0 -14
  186. package/lib/errors/invalid-datetime.js +0 -14
  187. package/lib/errors/invalid-relation.js +0 -14
  188. package/lib/errors/invalid-time.js +0 -14
  189. package/lib/errors/not-null.js +0 -15
  190. package/lib/fields/biginteger.js +0 -17
  191. package/lib/fields/boolean.js +0 -39
  192. package/lib/fields/date.js +0 -16
  193. package/lib/fields/datetime.js +0 -19
  194. package/lib/fields/field.js +0 -17
  195. package/lib/fields/index.d.ts +0 -9
  196. package/lib/fields/index.js +0 -50
  197. package/lib/fields/json.js +0 -21
  198. package/lib/fields/number.js +0 -23
  199. package/lib/fields/shared/parsers.js +0 -71
  200. package/lib/fields/string.js +0 -17
  201. package/lib/fields/time.js +0 -17
  202. package/lib/fields/timestamp.js +0 -19
  203. package/lib/index.d.ts +0 -198
  204. package/lib/index.js +0 -129
  205. package/lib/lifecycles/index.d.ts +0 -51
  206. package/lib/lifecycles/index.js +0 -90
  207. package/lib/lifecycles/subscribers/index.d.ts +0 -11
  208. package/lib/lifecycles/subscribers/models-lifecycles.js +0 -19
  209. package/lib/lifecycles/subscribers/timestamps.js +0 -65
  210. package/lib/metadata/index.js +0 -244
  211. package/lib/metadata/relations.js +0 -578
  212. package/lib/migrations/index.d.ts +0 -9
  213. package/lib/migrations/index.js +0 -75
  214. package/lib/migrations/storage.js +0 -44
  215. package/lib/query/helpers/index.js +0 -11
  216. package/lib/query/helpers/join.js +0 -96
  217. package/lib/query/helpers/order-by.js +0 -70
  218. package/lib/query/helpers/populate/apply.js +0 -664
  219. package/lib/query/helpers/populate/index.js +0 -9
  220. package/lib/query/helpers/populate/process.js +0 -102
  221. package/lib/query/helpers/search.js +0 -84
  222. package/lib/query/helpers/streams/index.js +0 -5
  223. package/lib/query/helpers/streams/readable.js +0 -174
  224. package/lib/query/helpers/transform.js +0 -84
  225. package/lib/query/helpers/where.js +0 -365
  226. package/lib/query/index.js +0 -7
  227. package/lib/query/query-builder.js +0 -514
  228. package/lib/schema/__tests__/schema-diff.test.js +0 -231
  229. package/lib/schema/builder.js +0 -386
  230. package/lib/schema/diff.js +0 -399
  231. package/lib/schema/index.d.ts +0 -49
  232. package/lib/schema/index.js +0 -94
  233. package/lib/schema/schema.js +0 -202
  234. package/lib/schema/storage.js +0 -76
  235. package/lib/transaction-context.js +0 -68
  236. package/lib/types/index.d.ts +0 -6
  237. package/lib/types/index.js +0 -35
  238. package/lib/utils/content-types.js +0 -40
  239. package/lib/utils/knex.js +0 -22
  240. package/lib/validations/index.js +0 -20
  241. package/lib/validations/relations/bidirectional.js +0 -89
  242. package/lib/validations/relations/index.js +0 -14
@@ -1,102 +0,0 @@
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 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
92
- if (_.has('id', meta.attributes)) {
93
- qb.addSelect('id');
94
- }
95
-
96
- finalPopulate[key] = populateMap[key];
97
- }
98
-
99
- return finalPopulate;
100
- };
101
-
102
- module.exports = processPopulate;
@@ -1,84 +0,0 @@
1
- 'use strict';
2
-
3
- const _ = require('lodash/fp');
4
-
5
- const types = require('../../types');
6
- const { toColumnName } = require('./transform');
7
-
8
- const applySearch = (knex, query, ctx) => {
9
- const { qb, uid, db } = ctx;
10
- const meta = db.metadata.get(uid);
11
-
12
- const { attributes } = meta;
13
-
14
- const searchColumns = ['id'];
15
-
16
- const stringColumns = Object.keys(attributes).filter((attributeName) => {
17
- const attribute = attributes[attributeName];
18
- return types.isString(attribute.type) && attribute.searchable !== false;
19
- });
20
-
21
- searchColumns.push(...stringColumns);
22
-
23
- if (!_.isNaN(_.toNumber(query))) {
24
- const numberColumns = Object.keys(attributes).filter((attributeName) => {
25
- const attribute = attributes[attributeName];
26
- return types.isNumber(attribute.type) && attribute.searchable !== false;
27
- });
28
-
29
- searchColumns.push(...numberColumns);
30
- }
31
-
32
- switch (db.dialect.client) {
33
- case 'postgres': {
34
- searchColumns.forEach((attr) => {
35
- const columnName = toColumnName(meta, attr);
36
- return knex.orWhereRaw(`??::text ILIKE ?`, [
37
- qb.aliasColumn(columnName),
38
- `%${escapeQuery(query, '*%\\')}%`,
39
- ]);
40
- });
41
-
42
- break;
43
- }
44
- case 'sqlite': {
45
- searchColumns.forEach((attr) => {
46
- const columnName = toColumnName(meta, attr);
47
- return knex.orWhereRaw(`?? LIKE ? ESCAPE '\\'`, [
48
- qb.aliasColumn(columnName),
49
- `%${escapeQuery(query, '*%\\')}%`,
50
- ]);
51
- });
52
- break;
53
- }
54
- case 'mysql': {
55
- searchColumns.forEach((attr) => {
56
- const columnName = toColumnName(meta, attr);
57
- return knex.orWhereRaw(`?? LIKE ?`, [
58
- qb.aliasColumn(columnName),
59
- `%${escapeQuery(query, '*%\\')}%`,
60
- ]);
61
- });
62
- break;
63
- }
64
- default: {
65
- // do nothing
66
- }
67
- }
68
- };
69
-
70
- const escapeQuery = (query, charsToEscape, escapeChar = '\\') => {
71
- return query
72
- .split('')
73
- .reduce(
74
- (escapedQuery, char) =>
75
- charsToEscape.includes(char)
76
- ? `${escapedQuery}${escapeChar}${char}`
77
- : `${escapedQuery}${char}`,
78
- ''
79
- );
80
- };
81
-
82
- module.exports = {
83
- applySearch,
84
- };
@@ -1,5 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = {
4
- ReadableQuery: require('./readable'),
5
- };
@@ -1,174 +0,0 @@
1
- 'use strict';
2
-
3
- const { Readable } = require('stream');
4
- const { isFinite } = require('lodash/fp');
5
-
6
- const { applyPopulate } = require('../populate');
7
- const { fromRow } = require('../transform');
8
-
9
- const knexQueryDone = Symbol('knexQueryDone');
10
- const knexPerformingQuery = Symbol('knexPerformingQuery');
11
-
12
- class ReadableStrapiQuery extends Readable {
13
- /**
14
- * @param {object} options
15
- * @param {ReturnType<typeof import('../../query-builder')>} options.qb The strapi query builder instance
16
- * @param {string} options.uid The model uid
17
- * @param {import('../../../index').Database} options.db The Database instance
18
- * @param {boolean} [options.mapResults] The maximum number of entities to fetch per query
19
- * @param {number} [options.batchSize] The maximum number of entities to fetch per query
20
- */
21
- constructor({ qb, db, uid, mapResults = true, batchSize = 500 }) {
22
- super({ objectMode: true, highWaterMark: batchSize });
23
-
24
- // Extract offset & limit from the query-builder's state
25
- const { offset, limit } = qb.state;
26
-
27
- // Original offset value
28
- this._offset = isFinite(offset) ? offset : 0;
29
-
30
- // Max amount of entities to fetch, force null as undefined value
31
- this._limit = isFinite(limit) ? limit : null;
32
-
33
- // Total amount of entities fetched
34
- this._fetched = 0;
35
-
36
- /**
37
- * Original query
38
- * @type {import('knex').Knex}
39
- */
40
- this._query = qb.getKnexQuery();
41
-
42
- // Query Builder instance
43
- this._qb = qb;
44
-
45
- // Database related properties
46
- this._db = db;
47
- this._uid = uid;
48
- this._meta = db.metadata.get(uid);
49
-
50
- // Stream params
51
- this._batchSize = batchSize;
52
- this._mapResults = mapResults;
53
-
54
- // States
55
- this[knexPerformingQuery] = false;
56
- }
57
-
58
- _destroy(err, cb) {
59
- // If the stream is destroyed while a query is being made, then wait for a
60
- // kQueryDone event to be emitted before actually destroying the stream
61
- if (this[knexPerformingQuery]) {
62
- this.once(knexQueryDone, (er) => cb(err || er));
63
- } else {
64
- cb(err);
65
- }
66
- }
67
-
68
- /**
69
- * Custom ._read() implementation
70
- *
71
- * NOTE: Here "size" means the number of entities to be read from the database.
72
- * Not the actual byte size, as it would means that we need to return partial entities.
73
- *
74
- * @param {number} size
75
- */
76
- async _read(size) {
77
- const query = this._query;
78
-
79
- // Remove the original offset & limit properties from the query
80
- // Theoretically, they would be replaced by calling them again, but this is just to be sure
81
- query.clear('limit').clear('offset');
82
-
83
- // Define the maximum read size based on the limit and the requested size
84
- // NOTE: size is equal to _batchSize by default. Since we want to allow customizing it on
85
- // the fly, we need to use its value instead of batchSize when computing the maxReadSize value
86
- const maxReadSize =
87
- // if no limit is defined in the query, use the given size,
88
- // otherwise, use the smallest value between the two
89
- this._limit === null ? size : Math.min(size, this._limit);
90
-
91
- // Compute the limit for the next query
92
- const limit =
93
- // If a limit is defined
94
- this._limit !== null &&
95
- // And reading `maxReadSize` would fetch too many entities (> _limit)
96
- this._fetched + maxReadSize > this._limit
97
- ? // Then adjust the limit so that it only get the remaining entities
98
- this._limit - this._fetched
99
- : // Else, use the max read size
100
- maxReadSize;
101
-
102
- // If we don't have anything left to read (_limit === _fetched),
103
- // don't bother making the query and end the stream by pushing null
104
- if (limit <= 0) {
105
- this.push(null);
106
- return;
107
- }
108
-
109
- // Compute the offset (base offset + number of entities already fetched)
110
- const offset = this._offset + this._fetched;
111
-
112
- // Update the query with the new values (offset + limit)
113
- query.offset(offset).limit(limit);
114
-
115
- // Lock the ._destroy()
116
- this[knexPerformingQuery] = true;
117
-
118
- let results;
119
- let count;
120
- let err;
121
-
122
- try {
123
- // Execute the query and store the results & count
124
- results = await query;
125
-
126
- const { populate } = this._qb.state;
127
-
128
- // Apply populate if needed
129
- if (populate) {
130
- await applyPopulate(results, populate, { qb: this._qb, uid: this._uid, db: this._db });
131
- }
132
-
133
- // Map results if asked to
134
- if (this._mapResults) {
135
- results = fromRow(this._meta, results);
136
- }
137
-
138
- count = results.length;
139
- } catch (e) {
140
- err = e;
141
- }
142
-
143
- // Unlock the ._destroy()
144
- this[knexPerformingQuery] = false;
145
-
146
- // Tell ._destroy() that it's now safe to close the db connection
147
- if (this.destroyed) {
148
- this.emit(knexQueryDone);
149
- return;
150
- }
151
-
152
- // If there is an error, destroy with the given error
153
- if (err) {
154
- this.destroy(err);
155
- return;
156
- }
157
-
158
- // Update the amount of fetched entities
159
- this._fetched += count;
160
-
161
- // While there is at least one value to unpack
162
- for (const result of results) {
163
- this.push(result);
164
- }
165
-
166
- // If the amount of fetched entities is smaller than the
167
- // maximum read size, Then push null to close the stream
168
- if (this._fetched === this._limit || count < this._batchSize) {
169
- this.push(null);
170
- }
171
- }
172
- }
173
-
174
- module.exports = ReadableStrapiQuery;
@@ -1,84 +0,0 @@
1
- 'use strict';
2
-
3
- const _ = require('lodash/fp');
4
-
5
- const types = require('../../types');
6
- const { createField } = require('../../fields');
7
-
8
- const fromRow = (meta, row) => {
9
- if (Array.isArray(row)) {
10
- return row.map((singleRow) => fromRow(meta, singleRow));
11
- }
12
-
13
- const { attributes } = meta;
14
-
15
- if (_.isNil(row)) {
16
- return null;
17
- }
18
-
19
- const obj = {};
20
-
21
- for (const column in row) {
22
- if (!_.has(column, meta.columnToAttribute)) {
23
- continue;
24
- }
25
-
26
- const attributeName = meta.columnToAttribute[column];
27
- const attribute = attributes[attributeName];
28
-
29
- if (types.isScalar(attribute.type)) {
30
- const field = createField(attribute);
31
-
32
- const val = row[column] === null ? null : field.fromDB(row[column]);
33
-
34
- obj[attributeName] = val;
35
- }
36
-
37
- if (types.isRelation(attribute.type)) {
38
- obj[attributeName] = row[column];
39
- }
40
- }
41
-
42
- return obj;
43
- };
44
-
45
- const toRow = (meta, data = {}) => {
46
- if (_.isNil(data)) {
47
- return data;
48
- }
49
-
50
- if (_.isArray(data)) {
51
- return data.map((datum) => toRow(meta, datum));
52
- }
53
-
54
- const { attributes } = meta;
55
-
56
- for (const key of Object.keys(data)) {
57
- const attribute = attributes[key];
58
-
59
- if (!attribute || attribute.columnName === key) {
60
- continue;
61
- }
62
-
63
- data[attribute.columnName] = data[key];
64
- delete data[key];
65
- }
66
-
67
- return data;
68
- };
69
-
70
- const toColumnName = (meta, name) => {
71
- const attribute = meta.attributes[name];
72
-
73
- if (!attribute) {
74
- return name;
75
- }
76
-
77
- return attribute.columnName || name;
78
- };
79
-
80
- module.exports = {
81
- toRow,
82
- fromRow,
83
- toColumnName,
84
- };