@e22m4u/js-repository 0.0.40 → 0.0.42

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.
@@ -18,6 +18,11 @@ class TestAdapter extends Adapter {
18
18
  return Promise.resolve({});
19
19
  }
20
20
 
21
+ // eslint-disable-next-line no-unused-vars
22
+ patch(modelName, modelData, where = undefined) {
23
+ return Promise.resolve(1);
24
+ }
25
+
21
26
  // eslint-disable-next-line no-unused-vars
22
27
  patchById(modelName, id, modelData, filter = undefined) {
23
28
  return Promise.resolve({});
@@ -36,6 +36,12 @@ export class DefaultValuesDecorator extends Service {
36
36
  return replaceById.call(this, modelName, id, modelData, filter);
37
37
  };
38
38
 
39
+ const patch = adapter.patch;
40
+ adapter.patch = function (modelName, modelData, where) {
41
+ modelData = setDefaults(modelName, modelData, true);
42
+ return patch.call(this, modelName, modelData, where);
43
+ };
44
+
39
45
  const patchById = adapter.patchById;
40
46
  adapter.patchById = function (modelName, id, modelData, filter) {
41
47
  modelData = setDefaults(modelName, modelData, true);
@@ -29,6 +29,11 @@ class TestAdapter extends Adapter {
29
29
  return modelData;
30
30
  }
31
31
 
32
+ // eslint-disable-next-line no-unused-vars
33
+ patch(modelName, modelData, where = undefined) {
34
+ return Promise.resolve(modelData);
35
+ }
36
+
32
37
  // eslint-disable-next-line no-unused-vars
33
38
  async patchById(modelName, id, modelData, filter = undefined) {
34
39
  return modelData;
@@ -76,6 +81,47 @@ describe('DefaultValuesDecorator', function () {
76
81
  );
77
82
  });
78
83
 
84
+ describe('overrides the "patch" method and sets default values to input data', function () {
85
+ it('does not set default values to not existing properties of input data', async function () {
86
+ sandbox.on(U, 'setDefaultValuesToEmptyProperties');
87
+ const data = {};
88
+ const retval = await A.patch('model', data);
89
+ expect(retval).to.be.eql({});
90
+ expect(U.setDefaultValuesToEmptyProperties).to.be.called.once;
91
+ expect(U.setDefaultValuesToEmptyProperties).to.be.called.with.exactly(
92
+ 'model',
93
+ data,
94
+ true,
95
+ );
96
+ });
97
+
98
+ it('does set default values to input properties of null', async function () {
99
+ sandbox.on(U, 'setDefaultValuesToEmptyProperties');
100
+ const data = {prop: null};
101
+ const retval = await A.patch('model', data);
102
+ expect(retval).to.be.eql({prop: 'value'});
103
+ expect(U.setDefaultValuesToEmptyProperties).to.be.called.once;
104
+ expect(U.setDefaultValuesToEmptyProperties).to.be.called.with.exactly(
105
+ 'model',
106
+ data,
107
+ true,
108
+ );
109
+ });
110
+
111
+ it('does set default values to input properties of undefined', async function () {
112
+ sandbox.on(U, 'setDefaultValuesToEmptyProperties');
113
+ const data = {prop: undefined};
114
+ const retval = await A.patch('model', data);
115
+ expect(retval).to.be.eql({prop: 'value'});
116
+ expect(U.setDefaultValuesToEmptyProperties).to.be.called.once;
117
+ expect(U.setDefaultValuesToEmptyProperties).to.be.called.with.exactly(
118
+ 'model',
119
+ data,
120
+ true,
121
+ );
122
+ });
123
+ });
124
+
79
125
  describe('overrides the "patchById" method and sets default values to input data', function () {
80
126
  it('does not set default values to not existing properties of input data', async function () {
81
127
  sandbox.on(U, 'setDefaultValuesToEmptyProperties');
@@ -99,8 +99,8 @@ export declare type PolyBelongsToDefinition = {
99
99
  export declare type HasOneDefinition = {
100
100
  type: RelationType.HAS_ONE;
101
101
  model: string;
102
+ foreignKey: string;
102
103
  polymorphic?: false;
103
- foreignKey?: string;
104
104
  discriminator?: undefined;
105
105
  };
106
106
 
@@ -127,16 +127,7 @@ export declare type PolyHasOneDefinitionWithTargetRelationName = {
127
127
  /**
128
128
  * The polymorphic "hasOne" relation with target relation keys.
129
129
  *
130
- * @example Required options only.
131
- * ```
132
- * {
133
- * type: RelationType.HAS_ONE,
134
- * model: 'model',
135
- * polymorphic: true,
136
- * }
137
- * ```
138
- *
139
- * @example Verbose definition.
130
+ * @example
140
131
  * ```
141
132
  * {
142
133
  * type: RelationType.HAS_ONE,
@@ -151,8 +142,8 @@ export declare type PolyHasOneDefinitionWithTargetKeys = {
151
142
  type: RelationType.HAS_ONE;
152
143
  model: string;
153
144
  polymorphic: true;
154
- foreignKey?: string;
155
- discriminator?: string;
145
+ foreignKey: string;
146
+ discriminator: string;
156
147
  };
157
148
 
158
149
  /**
@@ -170,8 +161,8 @@ export declare type PolyHasOneDefinitionWithTargetKeys = {
170
161
  export declare type HasManyDefinition = {
171
162
  type: RelationType.HAS_MANY;
172
163
  model: string;
164
+ foreignKey: string;
173
165
  polymorphic?: false;
174
- foreignKey?: string;
175
166
  discriminator?: undefined;
176
167
  };
177
168
 
@@ -198,16 +189,7 @@ export declare type PolyHasManyDefinitionWithTargetRelationName = {
198
189
  /**
199
190
  * The polymorphic "hasMany" relation with target relation keys.
200
191
  *
201
- * @example Required options only.
202
- * ```
203
- * {
204
- * type: RelationType.HAS_MANY,
205
- * model: 'model',
206
- * polymorphic: true,
207
- * }
208
- * ```
209
- *
210
- * @example Verbose definition.
192
+ * @example
211
193
  * ```
212
194
  * {
213
195
  * type: RelationType.HAS_MANY,
@@ -222,8 +204,8 @@ export declare type PolyHasManyDefinitionWithTargetKeys = {
222
204
  type: RelationType.HAS_MANY;
223
205
  model: string;
224
206
  polymorphic: true;
225
- foreignKey?: string;
226
- discriminator?: string;
207
+ foreignKey: string;
208
+ discriminator: string;
227
209
  };
228
210
 
229
211
  /**
@@ -253,15 +253,15 @@ describe('RelationsDefinitionValidator', function () {
253
253
  const validate = v => {
254
254
  const foo = {
255
255
  type: RelationType.HAS_ONE,
256
- model: 'model',
257
- foreignKey: v,
256
+ model: v,
257
+ foreignKey: 'modelId',
258
258
  };
259
259
  return () => S.validate('model', {foo});
260
260
  };
261
261
  const error = v =>
262
262
  format(
263
263
  'The relation "foo" of the model "model" has the type "hasOne", ' +
264
- 'so it requires the option "foreignKey" to be a non-empty String, ' +
264
+ 'so it requires the option "model" to be a non-empty String, ' +
265
265
  'but %s given.',
266
266
  v,
267
267
  );
@@ -273,22 +273,22 @@ describe('RelationsDefinitionValidator', function () {
273
273
  expect(validate([])).to.throw(error('Array'));
274
274
  expect(validate(undefined)).to.throw(error('undefined'));
275
275
  expect(validate(null)).to.throw(error('null'));
276
- validate('modelId')();
276
+ validate('model')();
277
277
  });
278
278
 
279
- it('requires the option "foreignKey" to be a string', function () {
279
+ it('requires the option "foreignKey" to be a non-empty string', function () {
280
280
  const validate = v => {
281
281
  const foo = {
282
282
  type: RelationType.HAS_ONE,
283
- model: v,
284
- foreignKey: 'modelId',
283
+ model: 'model',
284
+ foreignKey: v,
285
285
  };
286
286
  return () => S.validate('model', {foo});
287
287
  };
288
288
  const error = v =>
289
289
  format(
290
290
  'The relation "foo" of the model "model" has the type "hasOne", ' +
291
- 'so it requires the option "model" to be a non-empty String, ' +
291
+ 'so it requires the option "foreignKey" to be a non-empty String, ' +
292
292
  'but %s given.',
293
293
  v,
294
294
  );
@@ -300,7 +300,7 @@ describe('RelationsDefinitionValidator', function () {
300
300
  expect(validate([])).to.throw(error('Array'));
301
301
  expect(validate(undefined)).to.throw(error('undefined'));
302
302
  expect(validate(null)).to.throw(error('null'));
303
- validate('model')();
303
+ validate('modelId')();
304
304
  });
305
305
 
306
306
  it('throws an error if the option "discriminator" is provided', function () {
@@ -479,15 +479,15 @@ describe('RelationsDefinitionValidator', function () {
479
479
  const validate = v => {
480
480
  const foo = {
481
481
  type: RelationType.HAS_MANY,
482
- model: 'model',
483
- foreignKey: v,
482
+ model: v,
483
+ foreignKey: 'modelId',
484
484
  };
485
485
  return () => S.validate('model', {foo});
486
486
  };
487
487
  const error = v =>
488
488
  format(
489
489
  'The relation "foo" of the model "model" has the type "hasMany", ' +
490
- 'so it requires the option "foreignKey" to be a non-empty String, ' +
490
+ 'so it requires the option "model" to be a non-empty String, ' +
491
491
  'but %s given.',
492
492
  v,
493
493
  );
@@ -499,22 +499,22 @@ describe('RelationsDefinitionValidator', function () {
499
499
  expect(validate([])).to.throw(error('Array'));
500
500
  expect(validate(undefined)).to.throw(error('undefined'));
501
501
  expect(validate(null)).to.throw(error('null'));
502
- validate('modelId')();
502
+ validate('model')();
503
503
  });
504
504
 
505
- it('requires the option "foreignKey" to be a string', function () {
505
+ it('requires the option "foreignKey" to be a non-empty string', function () {
506
506
  const validate = v => {
507
507
  const foo = {
508
508
  type: RelationType.HAS_MANY,
509
- model: v,
510
- foreignKey: 'modelId',
509
+ model: 'model',
510
+ foreignKey: v,
511
511
  };
512
512
  return () => S.validate('model', {foo});
513
513
  };
514
514
  const error = v =>
515
515
  format(
516
516
  'The relation "foo" of the model "model" has the type "hasMany", ' +
517
- 'so it requires the option "model" to be a non-empty String, ' +
517
+ 'so it requires the option "foreignKey" to be a non-empty String, ' +
518
518
  'but %s given.',
519
519
  v,
520
520
  );
@@ -526,7 +526,7 @@ describe('RelationsDefinitionValidator', function () {
526
526
  expect(validate([])).to.throw(error('Array'));
527
527
  expect(validate(undefined)).to.throw(error('undefined'));
528
528
  expect(validate(null)).to.throw(error('null'));
529
- validate('model')();
529
+ validate('modelId')();
530
530
  });
531
531
 
532
532
  it('throws an error if the option "discriminator" is provided', function () {
@@ -43,9 +43,9 @@ export declare type ItemFilterClause = Pick<FilterClause, 'fields' | 'include'>;
43
43
  * ```
44
44
  */
45
45
  export declare type WhereClause =
46
- | AndClause
47
- | OrClause
48
- | PropertiesClause;
46
+ & AndClause
47
+ & OrClause
48
+ & PropertiesClause;
49
49
 
50
50
  /**
51
51
  * Properties clause.
@@ -67,7 +67,8 @@ export type PropertiesClause = {
67
67
  | boolean
68
68
  | RegExp
69
69
  | null
70
- | undefined;
70
+ | undefined
71
+ | object;
71
72
  };
72
73
 
73
74
  /**
@@ -127,8 +127,12 @@ export class WhereClauseTool extends Service {
127
127
  */
128
128
  _test(example, value) {
129
129
  // Test null.
130
- if (example == null) {
131
- return value == null;
130
+ if (example === null) {
131
+ return value === null;
132
+ }
133
+ // Test undefined.
134
+ if (example === undefined) {
135
+ return value === undefined;
132
136
  }
133
137
  // Test RegExp.
134
138
  // noinspection ALL
@@ -266,11 +266,16 @@ describe('WhereClauseTool', function () {
266
266
  expect(result[0]).to.be.eql(OBJECTS[2]);
267
267
  });
268
268
 
269
- it('uses null to match an undefined and null value', function () {
269
+ it('does not use null to match an undefined value', function () {
270
270
  const result = S.filter(OBJECTS, {nickname: null});
271
- expect(result).to.have.length(2);
271
+ expect(result).to.have.length(1);
272
272
  expect(result[0]).to.be.eql(OBJECTS[2]);
273
- expect(result[1]).to.be.eql(OBJECTS[3]);
273
+ });
274
+
275
+ it('does not use undefined to match a null value', function () {
276
+ const result = S.filter(OBJECTS, {nickname: undefined});
277
+ expect(result).to.have.length(1);
278
+ expect(result[0]).to.be.eql(OBJECTS[3]);
274
279
  });
275
280
  });
276
281
 
@@ -77,6 +77,17 @@ export declare class Repository<
77
77
  filter?: ItemFilterClause,
78
78
  ): Promise<FlatData>;
79
79
 
80
+ /**
81
+ * Patch.
82
+ *
83
+ * @param data
84
+ * @param where
85
+ */
86
+ patch(
87
+ data: PartialWithoutId<IdName, Data>,
88
+ where?: WhereClause,
89
+ ): Promise<number>;
90
+
80
91
  /**
81
92
  * Patch by id.
82
93
  *
@@ -111,6 +111,18 @@ export class Repository extends Service {
111
111
  return this.replaceById(pkValue, data, filter);
112
112
  }
113
113
 
114
+ /**
115
+ * Patch.
116
+ *
117
+ * @param {object} data
118
+ * @param {object|undefined} where
119
+ * @returns {Promise<number>}
120
+ */
121
+ async patch(data, where = undefined) {
122
+ const adapter = await this.getAdapter();
123
+ return adapter.patch(this.modelName, data, where);
124
+ }
125
+
114
126
  /**
115
127
  * Patch by id.
116
128
  *
@@ -50,6 +50,32 @@ describe('Repository', function () {
50
50
  });
51
51
  });
52
52
 
53
+ describe('patch', function () {
54
+ it('patches all items', async function () {
55
+ const schema = new Schema();
56
+ schema.defineDatasource({name: 'datasource', adapter: 'memory'});
57
+ schema.defineModel({name: 'model', datasource: 'datasource'});
58
+ const rep = schema.getRepository('model');
59
+ await rep.create({foo: 'a1', bar: 'b1'});
60
+ await rep.create({foo: 'a2', bar: 'b2'});
61
+ await rep.create({foo: 'a3', bar: 'b3'});
62
+ const result = await rep.patch({foo: 'test'});
63
+ expect(result).to.be.eq(3);
64
+ });
65
+
66
+ it('patches found items by the "where" clause', async function () {
67
+ const schema = new Schema();
68
+ schema.defineDatasource({name: 'datasource', adapter: 'memory'});
69
+ schema.defineModel({name: 'model', datasource: 'datasource'});
70
+ const rep = schema.getRepository('model');
71
+ await rep.create({foo: 'a', bar: '1'});
72
+ await rep.create({foo: 'b', bar: '2'});
73
+ await rep.create({foo: 'c', bar: '2'});
74
+ const result = await rep.patch({foo: 'test'}, {bar: '2'});
75
+ expect(result).to.be.eq(2);
76
+ });
77
+ });
78
+
53
79
  describe('patchById', function () {
54
80
  it('patches an item by the given id', async function () {
55
81
  const schema = new Schema();