@e22m4u/js-repository 0.0.31

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 (183) hide show
  1. package/.c8rc +9 -0
  2. package/.commitlintrc +5 -0
  3. package/.editorconfig +13 -0
  4. package/.eslintignore +1 -0
  5. package/.eslintrc.cjs +27 -0
  6. package/.husky/commit-msg +4 -0
  7. package/.husky/pre-commit +9 -0
  8. package/.mocharc.cjs +7 -0
  9. package/.prettierrc +7 -0
  10. package/LICENSE +21 -0
  11. package/README.md +523 -0
  12. package/mocha.setup.js +10 -0
  13. package/package.json +57 -0
  14. package/src/adapter/adapter-loader.d.ts +16 -0
  15. package/src/adapter/adapter-loader.js +63 -0
  16. package/src/adapter/adapter-loader.spec.js +31 -0
  17. package/src/adapter/adapter-registry.d.ts +14 -0
  18. package/src/adapter/adapter-registry.js +36 -0
  19. package/src/adapter/adapter-registry.spec.js +36 -0
  20. package/src/adapter/adapter.d.ts +118 -0
  21. package/src/adapter/adapter.js +181 -0
  22. package/src/adapter/adapter.spec.js +144 -0
  23. package/src/adapter/builtin/memory-adapter.d.ts +118 -0
  24. package/src/adapter/builtin/memory-adapter.js +342 -0
  25. package/src/adapter/builtin/memory-adapter.spec.js +2925 -0
  26. package/src/adapter/decorator/data-sanitizing-decorator.d.ts +13 -0
  27. package/src/adapter/decorator/data-sanitizing-decorator.js +44 -0
  28. package/src/adapter/decorator/data-sanitizing-decorator.spec.js +59 -0
  29. package/src/adapter/decorator/data-validation-decorator.d.ts +13 -0
  30. package/src/adapter/decorator/data-validation-decorator.js +41 -0
  31. package/src/adapter/decorator/data-validation-decorator.spec.js +59 -0
  32. package/src/adapter/decorator/default-values-decorator.d.ts +13 -0
  33. package/src/adapter/decorator/default-values-decorator.js +57 -0
  34. package/src/adapter/decorator/default-values-decorator.spec.js +141 -0
  35. package/src/adapter/decorator/fields-filtering-decorator.d.ts +13 -0
  36. package/src/adapter/decorator/fields-filtering-decorator.js +72 -0
  37. package/src/adapter/decorator/fields-filtering-decorator.spec.js +119 -0
  38. package/src/adapter/decorator/inclusion-decorator.d.ts +13 -0
  39. package/src/adapter/decorator/inclusion-decorator.js +78 -0
  40. package/src/adapter/decorator/inclusion-decorator.spec.js +117 -0
  41. package/src/adapter/decorator/index.d.ts +5 -0
  42. package/src/adapter/decorator/index.js +5 -0
  43. package/src/adapter/index.d.ts +3 -0
  44. package/src/adapter/index.js +3 -0
  45. package/src/definition/datasource/datasource-definition-validator.d.ts +14 -0
  46. package/src/definition/datasource/datasource-definition-validator.js +33 -0
  47. package/src/definition/datasource/datasource-definition-validator.spec.js +63 -0
  48. package/src/definition/datasource/datasource-definition.d.ts +7 -0
  49. package/src/definition/datasource/index.d.ts +2 -0
  50. package/src/definition/datasource/index.js +1 -0
  51. package/src/definition/definition-registry.d.ts +50 -0
  52. package/src/definition/definition-registry.js +98 -0
  53. package/src/definition/definition-registry.spec.js +78 -0
  54. package/src/definition/index.d.ts +3 -0
  55. package/src/definition/index.js +3 -0
  56. package/src/definition/model/index.d.ts +7 -0
  57. package/src/definition/model/index.js +6 -0
  58. package/src/definition/model/model-data-sanitizer.d.ts +15 -0
  59. package/src/definition/model/model-data-sanitizer.js +33 -0
  60. package/src/definition/model/model-data-validator.d.ts +32 -0
  61. package/src/definition/model/model-data-validator.js +144 -0
  62. package/src/definition/model/model-data-validator.spec.js +1889 -0
  63. package/src/definition/model/model-definition-utils.d.ts +161 -0
  64. package/src/definition/model/model-definition-utils.js +371 -0
  65. package/src/definition/model/model-definition-utils.spec.js +1474 -0
  66. package/src/definition/model/model-definition-validator.d.ts +14 -0
  67. package/src/definition/model/model-definition-validator.js +83 -0
  68. package/src/definition/model/model-definition-validator.spec.js +143 -0
  69. package/src/definition/model/model-definition.d.ts +28 -0
  70. package/src/definition/model/properties/data-type.d.ts +11 -0
  71. package/src/definition/model/properties/data-type.js +11 -0
  72. package/src/definition/model/properties/default-values-definition-validator.d.ts +15 -0
  73. package/src/definition/model/properties/default-values-definition-validator.js +53 -0
  74. package/src/definition/model/properties/default-values-definition-validator.spec.js +136 -0
  75. package/src/definition/model/properties/index.d.ts +5 -0
  76. package/src/definition/model/properties/index.js +4 -0
  77. package/src/definition/model/properties/primary-keys-definition-validator.d.ts +15 -0
  78. package/src/definition/model/properties/primary-keys-definition-validator.js +55 -0
  79. package/src/definition/model/properties/primary-keys-definition-validator.spec.js +145 -0
  80. package/src/definition/model/properties/properties-definition-validator.d.ts +15 -0
  81. package/src/definition/model/properties/properties-definition-validator.js +194 -0
  82. package/src/definition/model/properties/properties-definition-validator.spec.js +373 -0
  83. package/src/definition/model/properties/property-definition.d.ts +20 -0
  84. package/src/definition/model/relations/index.d.ts +3 -0
  85. package/src/definition/model/relations/index.js +2 -0
  86. package/src/definition/model/relations/relation-definition.d.ts +254 -0
  87. package/src/definition/model/relations/relation-type.d.ts +9 -0
  88. package/src/definition/model/relations/relation-type.js +9 -0
  89. package/src/definition/model/relations/relations-definition-validator.d.ts +15 -0
  90. package/src/definition/model/relations/relations-definition-validator.js +449 -0
  91. package/src/definition/model/relations/relations-definition-validator.spec.js +772 -0
  92. package/src/errors/index.d.ts +3 -0
  93. package/src/errors/index.js +3 -0
  94. package/src/errors/invalid-argument-error.d.ts +6 -0
  95. package/src/errors/invalid-argument-error.js +6 -0
  96. package/src/errors/invalid-argument-error.spec.js +33 -0
  97. package/src/errors/invalid-operator-value-error.d.ts +13 -0
  98. package/src/errors/invalid-operator-value-error.js +24 -0
  99. package/src/errors/invalid-operator-value-error.spec.js +11 -0
  100. package/src/errors/not-implemented-error.d.ts +6 -0
  101. package/src/errors/not-implemented-error.js +6 -0
  102. package/src/errors/not-implemented-error.spec.js +33 -0
  103. package/src/filter/fields-clause-tool.d.ts +38 -0
  104. package/src/filter/fields-clause-tool.js +88 -0
  105. package/src/filter/fields-clause-tool.spec.js +133 -0
  106. package/src/filter/filter.d.ts +335 -0
  107. package/src/filter/include-clause-tool.d.ts +53 -0
  108. package/src/filter/include-clause-tool.js +364 -0
  109. package/src/filter/include-clause-tool.spec.js +653 -0
  110. package/src/filter/index.d.ts +7 -0
  111. package/src/filter/index.js +6 -0
  112. package/src/filter/operator-clause-tool.d.ts +223 -0
  113. package/src/filter/operator-clause-tool.js +515 -0
  114. package/src/filter/operator-clause-tool.spec.js +1064 -0
  115. package/src/filter/order-clause-tool.d.ts +32 -0
  116. package/src/filter/order-clause-tool.js +97 -0
  117. package/src/filter/order-clause-tool.spec.js +438 -0
  118. package/src/filter/slice-clause-tool.d.ts +30 -0
  119. package/src/filter/slice-clause-tool.js +65 -0
  120. package/src/filter/slice-clause-tool.spec.js +117 -0
  121. package/src/filter/where-clause-tool.d.ts +23 -0
  122. package/src/filter/where-clause-tool.js +165 -0
  123. package/src/filter/where-clause-tool.spec.js +280 -0
  124. package/src/index.d.ts +9 -0
  125. package/src/index.js +8 -0
  126. package/src/relations/belongs-to-resolver.d.ts +46 -0
  127. package/src/relations/belongs-to-resolver.js +242 -0
  128. package/src/relations/belongs-to-resolver.spec.js +1047 -0
  129. package/src/relations/has-many-resolver.d.ts +67 -0
  130. package/src/relations/has-many-resolver.js +317 -0
  131. package/src/relations/has-many-resolver.spec.js +2911 -0
  132. package/src/relations/has-one-resolver.d.ts +67 -0
  133. package/src/relations/has-one-resolver.js +311 -0
  134. package/src/relations/has-one-resolver.spec.js +2274 -0
  135. package/src/relations/index.d.ts +4 -0
  136. package/src/relations/index.js +4 -0
  137. package/src/relations/references-many-resolver.d.ts +27 -0
  138. package/src/relations/references-many-resolver.js +113 -0
  139. package/src/relations/references-many-resolver.spec.js +631 -0
  140. package/src/repository/index.d.ts +2 -0
  141. package/src/repository/index.js +2 -0
  142. package/src/repository/repository-registry.d.ts +29 -0
  143. package/src/repository/repository-registry.js +57 -0
  144. package/src/repository/repository-registry.spec.js +38 -0
  145. package/src/repository/repository.d.ts +164 -0
  146. package/src/repository/repository.js +207 -0
  147. package/src/repository/repository.spec.js +202 -0
  148. package/src/schema.d.ts +37 -0
  149. package/src/schema.js +41 -0
  150. package/src/types.d.ts +30 -0
  151. package/src/utils/capitalize.d.ts +6 -0
  152. package/src/utils/capitalize.js +10 -0
  153. package/src/utils/capitalize.spec.js +14 -0
  154. package/src/utils/clone-deep.d.ts +6 -0
  155. package/src/utils/clone-deep.js +61 -0
  156. package/src/utils/clone-deep.spec.js +28 -0
  157. package/src/utils/exclude-object-keys.d.ts +10 -0
  158. package/src/utils/exclude-object-keys.js +20 -0
  159. package/src/utils/exclude-object-keys.spec.js +49 -0
  160. package/src/utils/get-ctor-name.d.ts +6 -0
  161. package/src/utils/get-ctor-name.js +11 -0
  162. package/src/utils/get-ctor-name.spec.js +17 -0
  163. package/src/utils/get-value-by-path.d.ts +12 -0
  164. package/src/utils/get-value-by-path.js +23 -0
  165. package/src/utils/get-value-by-path.spec.js +36 -0
  166. package/src/utils/index.d.ts +10 -0
  167. package/src/utils/index.js +10 -0
  168. package/src/utils/is-ctor.d.ts +7 -0
  169. package/src/utils/is-ctor.js +10 -0
  170. package/src/utils/is-ctor.spec.js +26 -0
  171. package/src/utils/is-pure-object.d.ts +6 -0
  172. package/src/utils/is-pure-object.js +15 -0
  173. package/src/utils/is-pure-object.spec.js +25 -0
  174. package/src/utils/select-object-keys.d.ts +10 -0
  175. package/src/utils/select-object-keys.js +37 -0
  176. package/src/utils/select-object-keys.spec.js +40 -0
  177. package/src/utils/singularize.d.ts +6 -0
  178. package/src/utils/singularize.js +22 -0
  179. package/src/utils/singularize.spec.js +23 -0
  180. package/src/utils/string-to-regexp.d.ts +10 -0
  181. package/src/utils/string-to-regexp.js +22 -0
  182. package/src/utils/string-to-regexp.spec.js +35 -0
  183. package/tsconfig.json +9 -0
@@ -0,0 +1,118 @@
1
+ import {Adapter} from '../adapter.js';
2
+ import {ModelId} from '../../types.js';
3
+ import {AnyObject} from '../../types.js';
4
+ import {ModelData} from '../../types.js';
5
+ import {Filter} from '../../filter/index.js';
6
+ import {ItemFilter} from '../../filter/index.js';
7
+ import {WhereClause} from '../../filter/index.js';
8
+ import {ServiceContainer} from '@e22m4u/js-service';
9
+
10
+ /**
11
+ * Memory adapter.
12
+ */
13
+ export declare class MemoryAdapter extends Adapter {
14
+ /**
15
+ * Settings.
16
+ */
17
+ get settings(): AnyObject | undefined;
18
+
19
+ /**
20
+ * Constructor.
21
+ *
22
+ * @param container
23
+ * @param settings
24
+ */
25
+ constructor(container?: ServiceContainer, settings?: AnyObject);
26
+
27
+ /**
28
+ * Create.
29
+ *
30
+ * @param modelName
31
+ * @param modelData
32
+ * @param filter
33
+ */
34
+ create(
35
+ modelName: string,
36
+ modelData: ModelData,
37
+ filter?: ItemFilter,
38
+ ): Promise<ModelData>;
39
+
40
+ /**
41
+ * Replace by id.
42
+ *
43
+ * @param modelName
44
+ * @param id
45
+ * @param modelData
46
+ * @param filter
47
+ */
48
+ replaceById(
49
+ modelName: string,
50
+ id: ModelId,
51
+ modelData: ModelData,
52
+ filter?: ItemFilter,
53
+ ): Promise<ModelData>;
54
+
55
+ /**
56
+ * Patch by id.
57
+ *
58
+ * @param modelName
59
+ * @param id
60
+ * @param modelData
61
+ * @param filter
62
+ */
63
+ patchById(
64
+ modelName: string,
65
+ id: ModelId,
66
+ modelData: ModelData,
67
+ filter?: ItemFilter,
68
+ ): Promise<ModelData>;
69
+
70
+ /**
71
+ * Find.
72
+ *
73
+ * @param modelName
74
+ * @param filter
75
+ */
76
+ find(modelName: string, filter?: Filter): Promise<ModelData[]>;
77
+
78
+ /**
79
+ * Find by id.
80
+ *
81
+ * @param modelName
82
+ * @param id
83
+ * @param filter
84
+ */
85
+ findById(modelName: string, id: ModelId, filter?: Filter): Promise<ModelData>;
86
+
87
+ /**
88
+ * Delete.
89
+ *
90
+ * @param modelName
91
+ * @param where
92
+ */
93
+ delete(modelName: string, where?: WhereClause): Promise<number>;
94
+
95
+ /**
96
+ * Delete by id.
97
+ *
98
+ * @param modelName
99
+ * @param id
100
+ */
101
+ deleteById(modelName: string, id: ModelId): Promise<boolean>;
102
+
103
+ /**
104
+ * Exists.
105
+ *
106
+ * @param modelName
107
+ * @param id
108
+ */
109
+ exists(modelName: string, id: ModelId): Promise<boolean>;
110
+
111
+ /**
112
+ * Count.
113
+ *
114
+ * @param modelName
115
+ * @param where
116
+ */
117
+ count(modelName: string, where?: WhereClause): Promise<number>;
118
+ }
@@ -0,0 +1,342 @@
1
+ import {Adapter} from '../adapter.js';
2
+ import {cloneDeep} from '../../utils/index.js';
3
+ import {capitalize} from '../../utils/index.js';
4
+ import {DataType} from '../../definition/index.js';
5
+ import {SliceClauseTool} from '../../filter/index.js';
6
+ import {WhereClauseTool} from '../../filter/index.js';
7
+ import {OrderClauseTool} from '../../filter/index.js';
8
+ import {InvalidArgumentError} from '../../errors/index.js';
9
+ import {ModelDefinitionUtils} from '../../definition/index.js';
10
+
11
+ /**
12
+ * Memory adapter.
13
+ */
14
+ export class MemoryAdapter extends Adapter {
15
+ /**
16
+ * Tables.
17
+ *
18
+ * @type {Map<string, Map<number, Record<string, any>>>}
19
+ */
20
+ _tables = new Map();
21
+
22
+ /**
23
+ * Last ids.
24
+ *
25
+ * @type {Map<string, number>}
26
+ */
27
+ _lastIds = new Map();
28
+
29
+ /**
30
+ * Get table or create.
31
+ *
32
+ * @param {string} modelName
33
+ * @returns {Map<number, object>}
34
+ */
35
+ _getTableOrCreate(modelName) {
36
+ const tableName =
37
+ this.getService(ModelDefinitionUtils).getTableNameByModelName(modelName);
38
+ let table = this._tables.get(tableName);
39
+ if (table) return table;
40
+ table = new Map();
41
+ this._tables.set(tableName, table);
42
+ return table;
43
+ }
44
+
45
+ /**
46
+ * Gen next id value.
47
+ *
48
+ * @param {string} modelName
49
+ * @param {string} propName
50
+ * @returns {number}
51
+ */
52
+ _genNextIdValue(modelName, propName) {
53
+ const propType = this.getService(
54
+ ModelDefinitionUtils,
55
+ ).getDataTypeByPropertyName(modelName, propName);
56
+ if (propType !== DataType.ANY && propType !== DataType.NUMBER)
57
+ throw new InvalidArgumentError(
58
+ 'The memory adapter able to generate only Number identifiers, ' +
59
+ 'but the primary key %v of the model %v is defined as %s. ' +
60
+ 'Do provide your own value for the %v property, or change the type ' +
61
+ 'in the primary key definition to a Number that will be ' +
62
+ 'generated automatically.',
63
+ propName,
64
+ modelName,
65
+ capitalize(propType),
66
+ propName,
67
+ );
68
+ const tableName =
69
+ this.getService(ModelDefinitionUtils).getTableNameByModelName(modelName);
70
+ const lastId = this._lastIds.get(tableName) ?? 0;
71
+ const nextId = lastId + 1;
72
+ this._lastIds.set(tableName, nextId);
73
+ const table = this._getTableOrCreate(modelName);
74
+ const existedIds = Array.from(table.keys());
75
+ if (existedIds.includes(nextId))
76
+ return this._genNextIdValue(modelName, propName);
77
+ return nextId;
78
+ }
79
+
80
+ /**
81
+ * Create
82
+ *
83
+ * @param {string} modelName
84
+ * @param {object} modelData
85
+ * @param {object|undefined} filter
86
+ * @returns {Promise<object>}
87
+ */
88
+ // eslint-disable-next-line no-unused-vars
89
+ async create(modelName, modelData, filter = undefined) {
90
+ const pkPropName =
91
+ this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
92
+ modelName,
93
+ );
94
+ let idValue = modelData[pkPropName];
95
+ if (idValue == null) idValue = this._genNextIdValue(modelName, pkPropName);
96
+
97
+ const table = this._getTableOrCreate(modelName);
98
+ if (table.has(idValue))
99
+ throw new InvalidArgumentError(
100
+ 'The value %v of the primary key %v already exists in the model %v.',
101
+ idValue,
102
+ pkPropName,
103
+ modelName,
104
+ );
105
+
106
+ modelData = cloneDeep(modelData);
107
+ modelData[pkPropName] = idValue;
108
+
109
+ const tableData = this.getService(
110
+ ModelDefinitionUtils,
111
+ ).convertPropertyNamesToColumnNames(modelName, modelData);
112
+ table.set(idValue, tableData);
113
+
114
+ return this.getService(
115
+ ModelDefinitionUtils,
116
+ ).convertColumnNamesToPropertyNames(modelName, tableData);
117
+ }
118
+
119
+ /**
120
+ * Replace by id.
121
+ *
122
+ * @param {string} modelName
123
+ * @param {string|number} id
124
+ * @param {object} modelData
125
+ * @param {object|undefined} filter
126
+ * @returns {Promise<object>}
127
+ */
128
+ // eslint-disable-next-line no-unused-vars
129
+ async replaceById(modelName, id, modelData, filter = undefined) {
130
+ const table = this._getTableOrCreate(modelName);
131
+ const isExists = table.has(id);
132
+ const pkPropName =
133
+ this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
134
+ modelName,
135
+ );
136
+ if (!isExists)
137
+ throw new InvalidArgumentError(
138
+ 'The value %v of the primary key %v does not exist in the model %v.',
139
+ id,
140
+ pkPropName,
141
+ modelName,
142
+ );
143
+
144
+ modelData = cloneDeep(modelData);
145
+ modelData[pkPropName] = id;
146
+
147
+ const tableData = this.getService(
148
+ ModelDefinitionUtils,
149
+ ).convertPropertyNamesToColumnNames(modelName, modelData);
150
+ table.set(id, tableData);
151
+
152
+ return this.getService(
153
+ ModelDefinitionUtils,
154
+ ).convertColumnNamesToPropertyNames(modelName, tableData);
155
+ }
156
+
157
+ /**
158
+ * Patch by id.
159
+ *
160
+ * @param {string} modelName
161
+ * @param {string|number} id
162
+ * @param {object} modelData
163
+ * @param {object|undefined} filter
164
+ * @returns {Promise<object>}
165
+ */
166
+ // eslint-disable-next-line no-unused-vars
167
+ async patchById(modelName, id, modelData, filter = undefined) {
168
+ const table = this._getTableOrCreate(modelName);
169
+ const existingTableData = table.get(id);
170
+ const pkPropName =
171
+ this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
172
+ modelName,
173
+ );
174
+ if (existingTableData == null)
175
+ throw new InvalidArgumentError(
176
+ 'The value %v of the primary key %v does not exist in the model %v.',
177
+ id,
178
+ pkPropName,
179
+ modelName,
180
+ );
181
+
182
+ modelData = cloneDeep(modelData);
183
+ delete modelData[pkPropName];
184
+
185
+ const existingModelData = this.getService(
186
+ ModelDefinitionUtils,
187
+ ).convertColumnNamesToPropertyNames(modelName, existingTableData);
188
+ const mergedModelData = Object.assign({}, existingModelData, modelData);
189
+ const mergedTableData = this.getService(
190
+ ModelDefinitionUtils,
191
+ ).convertPropertyNamesToColumnNames(modelName, mergedModelData);
192
+ table.set(id, mergedTableData);
193
+
194
+ return this.getService(
195
+ ModelDefinitionUtils,
196
+ ).convertColumnNamesToPropertyNames(modelName, mergedTableData);
197
+ }
198
+
199
+ /**
200
+ * Find.
201
+ *
202
+ * @param {string} modelName
203
+ * @param {object|undefined} filter
204
+ * @returns {Promise<object[]>}
205
+ */
206
+ async find(modelName, filter = undefined) {
207
+ const table = this._getTableOrCreate(modelName);
208
+ const tableItems = Array.from(table.values());
209
+ let modelItems = tableItems.map(tableItem =>
210
+ this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
211
+ modelName,
212
+ tableItem,
213
+ ),
214
+ );
215
+
216
+ if (filter && typeof filter === 'object') {
217
+ if (filter.where)
218
+ modelItems = this.getService(WhereClauseTool).filter(
219
+ modelItems,
220
+ filter.where,
221
+ );
222
+ if (filter.skip || filter.limit)
223
+ modelItems = this.getService(SliceClauseTool).slice(
224
+ modelItems,
225
+ filter.skip,
226
+ filter.limit,
227
+ );
228
+ if (filter.order)
229
+ this.getService(OrderClauseTool).sort(modelItems, filter.order);
230
+ }
231
+ return modelItems;
232
+ }
233
+
234
+ /**
235
+ * Find by id.
236
+ *
237
+ * @param {string} modelName
238
+ * @param {string|number} id
239
+ * @param {object|undefined} filter
240
+ * @returns {Promise<object>}
241
+ */
242
+ // eslint-disable-next-line no-unused-vars
243
+ async findById(modelName, id, filter = undefined) {
244
+ const table = this._getTableOrCreate(modelName);
245
+ const tableData = table.get(id);
246
+ const pkPropName =
247
+ this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
248
+ modelName,
249
+ );
250
+ if (!tableData)
251
+ throw new InvalidArgumentError(
252
+ 'The value %v of the primary key %v does not exist in the model %v.',
253
+ id,
254
+ pkPropName,
255
+ modelName,
256
+ );
257
+ return this.getService(
258
+ ModelDefinitionUtils,
259
+ ).convertColumnNamesToPropertyNames(modelName, tableData);
260
+ }
261
+
262
+ /**
263
+ * Delete.
264
+ *
265
+ * @param {string} modelName
266
+ * @param {object|undefined} where
267
+ * @returns {Promise<number>}
268
+ */
269
+ async delete(modelName, where = undefined) {
270
+ const table = this._getTableOrCreate(modelName);
271
+ const tableItems = Array.from(table.values());
272
+ if (!tableItems.length) return 0;
273
+ let modelItems = tableItems.map(tableItem =>
274
+ this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
275
+ modelName,
276
+ tableItem,
277
+ ),
278
+ );
279
+
280
+ if (where && typeof where === 'object')
281
+ modelItems = this.getService(WhereClauseTool).filter(modelItems, where);
282
+ const size = modelItems.length;
283
+
284
+ const idPropName =
285
+ this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
286
+ modelName,
287
+ );
288
+ modelItems.forEach(modelData => {
289
+ const idValue = modelData[idPropName];
290
+ table.delete(idValue);
291
+ });
292
+ return size;
293
+ }
294
+
295
+ /**
296
+ * Delete by id.
297
+ *
298
+ * @param {string} modelName
299
+ * @param {string|number} id
300
+ * @returns {Promise<boolean>}
301
+ */
302
+ async deleteById(modelName, id) {
303
+ const table = this._getTableOrCreate(modelName);
304
+ const isExists = table.has(id);
305
+ table.delete(id);
306
+ return isExists;
307
+ }
308
+
309
+ /**
310
+ * Exists.
311
+ *
312
+ * @param {string} modelName
313
+ * @param {string|number} id
314
+ * @returns {Promise<boolean>}
315
+ */
316
+ async exists(modelName, id) {
317
+ const table = this._getTableOrCreate(modelName);
318
+ return table.has(id);
319
+ }
320
+
321
+ /**
322
+ * Count.
323
+ *
324
+ * @param {string} modelName
325
+ * @param {object|undefined} where
326
+ * @returns {Promise<number>}
327
+ */
328
+ async count(modelName, where = undefined) {
329
+ const table = this._getTableOrCreate(modelName);
330
+ const tableItems = Array.from(table.values());
331
+ let modelItems = tableItems.map(tableItem =>
332
+ this.getService(ModelDefinitionUtils).convertColumnNamesToPropertyNames(
333
+ modelName,
334
+ tableItem,
335
+ ),
336
+ );
337
+
338
+ if (where && typeof where === 'object')
339
+ modelItems = this.getService(WhereClauseTool).filter(modelItems, where);
340
+ return modelItems.length;
341
+ }
342
+ }