@e22m4u/js-repository 0.6.4 → 0.7.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.
- package/README.md +6 -532
- package/dist/cjs/index.cjs +141 -1156
- package/package.json +7 -7
- package/src/adapter/adapter.d.ts +2 -4
- package/src/adapter/adapter.js +2 -6
- package/src/adapter/adapter.spec.js +3 -15
- package/src/adapter/builtin/memory-adapter.js +41 -12
- package/src/adapter/builtin/memory-adapter.spec.js +47 -0
- package/src/adapter/decorator/index.d.ts +0 -2
- package/src/adapter/decorator/index.js +0 -2
- package/src/definition/model/index.d.ts +0 -2
- package/src/definition/model/index.js +0 -2
- package/src/definition/model/properties/index.d.ts +0 -2
- package/src/definition/model/properties/index.js +0 -2
- package/src/definition/model/properties/properties-definition-validator.js +0 -169
- package/src/definition/model/properties/properties-definition-validator.spec.js +0 -162
- package/src/definition/model/properties/property-definition.d.ts +1 -5
- package/src/definition/model/properties/property-uniqueness-validator.js +2 -2
- package/src/filter/filter-clause.d.ts +3 -4
- package/src/filter/where-clause-tool.js +2 -2
- package/src/utils/index.d.ts +1 -5
- package/src/utils/index.js +1 -5
- package/src/utils/is-plain-object.d.ts +6 -0
- package/src/utils/{is-pure-object.js → is-plain-object.js} +2 -2
- package/src/utils/is-plain-object.spec.js +25 -0
- package/src/utils/model-name-to-model-key.js +0 -4
- package/src/utils/model-name-to-model-key.spec.js +0 -23
- package/tsconfig.json +2 -2
- package/src/adapter/decorator/data-transformation-decorator.d.ts +0 -14
- package/src/adapter/decorator/data-transformation-decorator.js +0 -54
- package/src/adapter/decorator/data-transformation-decorator.spec.js +0 -193
- package/src/adapter/decorator/data-validation-decorator.d.ts +0 -14
- package/src/adapter/decorator/data-validation-decorator.js +0 -54
- package/src/adapter/decorator/data-validation-decorator.spec.js +0 -105
- package/src/definition/model/model-data-transformer.d.ts +0 -16
- package/src/definition/model/model-data-transformer.js +0 -170
- package/src/definition/model/model-data-transformer.spec.js +0 -2312
- package/src/definition/model/model-data-validator.d.ts +0 -16
- package/src/definition/model/model-data-validator.js +0 -318
- package/src/definition/model/model-data-validator.spec.js +0 -4528
- package/src/definition/model/properties/property-transformer/builtin/index.d.ts +0 -3
- package/src/definition/model/properties/property-transformer/builtin/index.js +0 -3
- package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.d.ts +0 -6
- package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.js +0 -19
- package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.spec.js +0 -39
- package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.d.ts +0 -6
- package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.js +0 -19
- package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.spec.js +0 -39
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.d.ts +0 -6
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.js +0 -19
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.spec.js +0 -39
- package/src/definition/model/properties/property-transformer/index.d.ts +0 -2
- package/src/definition/model/properties/property-transformer/index.js +0 -2
- package/src/definition/model/properties/property-transformer/property-transformer-registry.d.ts +0 -29
- package/src/definition/model/properties/property-transformer/property-transformer-registry.js +0 -76
- package/src/definition/model/properties/property-transformer/property-transformer-registry.spec.js +0 -133
- package/src/definition/model/properties/property-transformer/property-transformer.d.ts +0 -27
- package/src/definition/model/properties/property-transformer/property-transformer.js +0 -1
- package/src/definition/model/properties/property-validator/builtin/index.d.ts +0 -3
- package/src/definition/model/properties/property-validator/builtin/index.js +0 -3
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.d.ts +0 -6
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.js +0 -28
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.spec.js +0 -100
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.d.ts +0 -6
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.js +0 -28
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.spec.js +0 -100
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.d.ts +0 -6
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.js +0 -30
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.spec.js +0 -102
- package/src/definition/model/properties/property-validator/index.d.ts +0 -2
- package/src/definition/model/properties/property-validator/index.js +0 -2
- package/src/definition/model/properties/property-validator/property-validator-registry.d.ts +0 -29
- package/src/definition/model/properties/property-validator/property-validator-registry.js +0 -76
- package/src/definition/model/properties/property-validator/property-validator-registry.spec.js +0 -132
- package/src/definition/model/properties/property-validator/property-validator.d.ts +0 -25
- package/src/definition/model/properties/property-validator/property-validator.js +0 -1
- package/src/utils/get-ctor-name.d.ts +0 -6
- package/src/utils/get-ctor-name.js +0 -11
- package/src/utils/get-ctor-name.spec.js +0 -17
- package/src/utils/get-decorator-target-type.d.ts +0 -27
- package/src/utils/get-decorator-target-type.js +0 -63
- package/src/utils/get-decorator-target-type.spec.js +0 -80
- package/src/utils/is-ctor.d.ts +0 -8
- package/src/utils/is-ctor.js +0 -11
- package/src/utils/is-ctor.spec.js +0 -26
- package/src/utils/is-pure-object.d.ts +0 -6
- package/src/utils/is-pure-object.spec.js +0 -25
- package/src/utils/transform-promise.d.ts +0 -13
- package/src/utils/transform-promise.js +0 -15
- package/src/utils/transform-promise.spec.js +0 -19
|
@@ -1,2312 +0,0 @@
|
|
|
1
|
-
import {expect} from 'chai';
|
|
2
|
-
import {format} from '@e22m4u/js-format';
|
|
3
|
-
import {DataType} from './properties/index.js';
|
|
4
|
-
import {DatabaseSchema} from '../../database-schema.js';
|
|
5
|
-
import {EmptyValuesService} from '@e22m4u/js-empty-values';
|
|
6
|
-
import {DefinitionRegistry} from '../definition-registry.js';
|
|
7
|
-
import {ModelDataTransformer} from './model-data-transformer.js';
|
|
8
|
-
import {PropertyTransformerRegistry} from './properties/index.js';
|
|
9
|
-
|
|
10
|
-
describe('ModelDataTransformer', function () {
|
|
11
|
-
describe('transform', function () {
|
|
12
|
-
it('throws an error if the given model name is not defined', function () {
|
|
13
|
-
const dbs = new DatabaseSchema();
|
|
14
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
15
|
-
const throwable = () => T.transform('model', {});
|
|
16
|
-
expect(throwable).to.throw('The model "model" is not defined.');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('throws an error if the given model data is not a pure object', function () {
|
|
20
|
-
const dbs = new DatabaseSchema();
|
|
21
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
22
|
-
const throwable = v => () => T.transform('model', v);
|
|
23
|
-
const error = v =>
|
|
24
|
-
format(
|
|
25
|
-
'The data of the model "model" should be an Object, but %s was given.',
|
|
26
|
-
v,
|
|
27
|
-
);
|
|
28
|
-
expect(throwable('str')).to.throw(error('"str"'));
|
|
29
|
-
expect(throwable('')).to.throw(error('""'));
|
|
30
|
-
expect(throwable(10)).to.throw(error('10'));
|
|
31
|
-
expect(throwable(0)).to.throw(error('0'));
|
|
32
|
-
expect(throwable(true)).to.throw(error('true'));
|
|
33
|
-
expect(throwable(false)).to.throw(error('false'));
|
|
34
|
-
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
35
|
-
expect(throwable(null)).to.throw(error('null'));
|
|
36
|
-
expect(throwable([])).to.throw(error('Array'));
|
|
37
|
-
expect(throwable(new Date())).to.throw(error('Date'));
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('does nothing with the given model if no transformers are set', function () {
|
|
41
|
-
const dbs = new DatabaseSchema();
|
|
42
|
-
dbs.defineModel({
|
|
43
|
-
name: 'model',
|
|
44
|
-
properties: {
|
|
45
|
-
foo: DataType.STRING,
|
|
46
|
-
bar: {
|
|
47
|
-
type: DataType.STRING,
|
|
48
|
-
default: 'test',
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
53
|
-
const modelData = {baz: 'qux'};
|
|
54
|
-
const res = T.transform('model', modelData);
|
|
55
|
-
expect(res).to.be.eql(modelData);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('the option "transform" requires a non-empty String, a Function, an Array or an Object', function () {
|
|
59
|
-
const dbs = new DatabaseSchema();
|
|
60
|
-
dbs
|
|
61
|
-
.getService(PropertyTransformerRegistry)
|
|
62
|
-
.addTransformer('myTransformer', () => 'transformed');
|
|
63
|
-
dbs.defineModel({
|
|
64
|
-
name: 'model',
|
|
65
|
-
properties: {
|
|
66
|
-
foo: {
|
|
67
|
-
type: DataType.ANY,
|
|
68
|
-
transform: undefined,
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
73
|
-
const throwable = v => () => {
|
|
74
|
-
const models = dbs.getService(DefinitionRegistry)['_models'];
|
|
75
|
-
models.model.properties.foo.transform = v;
|
|
76
|
-
T.transform('model', {foo: 'bar'});
|
|
77
|
-
};
|
|
78
|
-
const error = v =>
|
|
79
|
-
format(
|
|
80
|
-
'The provided option "transform" for the property "foo" in the model "model" ' +
|
|
81
|
-
'should be either a transformer name, a transformer function, an array ' +
|
|
82
|
-
'of transformer names or functions, or an object mapping transformer ' +
|
|
83
|
-
'names to their arguments, but %s was given.',
|
|
84
|
-
v,
|
|
85
|
-
);
|
|
86
|
-
expect(throwable('')).to.throw(error('""'));
|
|
87
|
-
expect(throwable(10)).to.throw(error('10'));
|
|
88
|
-
expect(throwable(0)).to.throw(error('0'));
|
|
89
|
-
expect(throwable(true)).to.throw(error('true'));
|
|
90
|
-
expect(throwable(false)).to.throw(error('false'));
|
|
91
|
-
throwable('myTransformer')();
|
|
92
|
-
throwable(() => 10)();
|
|
93
|
-
throwable(() => Promise.resolve('bar'))();
|
|
94
|
-
throwable(['myTransformer'])();
|
|
95
|
-
throwable([() => Promise.resolve('bar')])();
|
|
96
|
-
throwable([])();
|
|
97
|
-
throwable({myTransformer: true})();
|
|
98
|
-
throwable({})();
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
describe('when the option "transform" is a String', function () {
|
|
102
|
-
it('should not transform the non-provided property', function () {
|
|
103
|
-
let calls = 0;
|
|
104
|
-
const dbs = new DatabaseSchema();
|
|
105
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
106
|
-
reg.addTransformer('myTransformer', () => {
|
|
107
|
-
calls++;
|
|
108
|
-
throw new Error('Should not to be called.');
|
|
109
|
-
});
|
|
110
|
-
dbs.defineModel({
|
|
111
|
-
name: 'model',
|
|
112
|
-
properties: {
|
|
113
|
-
foo: {
|
|
114
|
-
type: DataType.ANY,
|
|
115
|
-
transform: 'myTransformer',
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
});
|
|
119
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
120
|
-
const res = T.transform('model', {});
|
|
121
|
-
expect(res).to.be.eql({});
|
|
122
|
-
expect(calls).to.be.eq(0);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it('should not transform non-provided properties when the option "isPartial" is true', function () {
|
|
126
|
-
let calls = 0;
|
|
127
|
-
const dbs = new DatabaseSchema();
|
|
128
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
129
|
-
reg.addTransformer('myTransformer', value => {
|
|
130
|
-
calls++;
|
|
131
|
-
return String(value);
|
|
132
|
-
});
|
|
133
|
-
dbs.defineModel({
|
|
134
|
-
name: 'model',
|
|
135
|
-
properties: {
|
|
136
|
-
foo: {
|
|
137
|
-
type: DataType.ANY,
|
|
138
|
-
transform: 'myTransformer',
|
|
139
|
-
},
|
|
140
|
-
bar: {
|
|
141
|
-
type: DataType.ANY,
|
|
142
|
-
transform: 'myTransformer',
|
|
143
|
-
},
|
|
144
|
-
},
|
|
145
|
-
});
|
|
146
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
147
|
-
const res = T.transform('model', {}, true);
|
|
148
|
-
expect(res).to.be.eql({});
|
|
149
|
-
expect(calls).to.be.eq(0);
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('should not transform undefined and null values', function () {
|
|
153
|
-
let calls = 0;
|
|
154
|
-
const dbs = new DatabaseSchema();
|
|
155
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
156
|
-
reg.addTransformer('myTransformer', () => {
|
|
157
|
-
calls++;
|
|
158
|
-
throw new Error('Should not to be called.');
|
|
159
|
-
});
|
|
160
|
-
dbs.defineModel({
|
|
161
|
-
name: 'model',
|
|
162
|
-
properties: {
|
|
163
|
-
foo: {
|
|
164
|
-
type: DataType.ANY,
|
|
165
|
-
transform: 'myTransformer',
|
|
166
|
-
},
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
170
|
-
const res1 = T.transform('model', {foo: undefined});
|
|
171
|
-
const res2 = T.transform('model', {foo: null});
|
|
172
|
-
expect(res1).to.be.eql({foo: undefined});
|
|
173
|
-
expect(res2).to.be.eql({foo: null});
|
|
174
|
-
expect(calls).to.be.eq(0);
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it('should not transform the empty value', function () {
|
|
178
|
-
let calls = 0;
|
|
179
|
-
const dbs = new DatabaseSchema();
|
|
180
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
181
|
-
reg.addTransformer('myTransformer', () => {
|
|
182
|
-
calls++;
|
|
183
|
-
throw new Error('Should not to be called.');
|
|
184
|
-
});
|
|
185
|
-
dbs.defineModel({
|
|
186
|
-
name: 'model',
|
|
187
|
-
properties: {
|
|
188
|
-
foo: {
|
|
189
|
-
type: DataType.ANY,
|
|
190
|
-
transform: 'myTransformer',
|
|
191
|
-
},
|
|
192
|
-
},
|
|
193
|
-
});
|
|
194
|
-
dbs.getService(EmptyValuesService).setEmptyValuesOf(DataType.ANY, [10]);
|
|
195
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
196
|
-
const res = T.transform('model', {foo: 10});
|
|
197
|
-
expect(res).to.be.eql({foo: 10});
|
|
198
|
-
expect(calls).to.be.eq(0);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it('should transform the property by the transformer', function () {
|
|
202
|
-
let calls = 0;
|
|
203
|
-
const dbs = new DatabaseSchema();
|
|
204
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
205
|
-
reg.addTransformer('myTransformer', value => {
|
|
206
|
-
calls++;
|
|
207
|
-
return String(value);
|
|
208
|
-
});
|
|
209
|
-
dbs.defineModel({
|
|
210
|
-
name: 'model',
|
|
211
|
-
properties: {
|
|
212
|
-
foo: {
|
|
213
|
-
type: DataType.ANY,
|
|
214
|
-
transform: 'myTransformer',
|
|
215
|
-
},
|
|
216
|
-
},
|
|
217
|
-
});
|
|
218
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
219
|
-
const res = T.transform('model', {foo: 10});
|
|
220
|
-
expect(res).to.be.eql({foo: '10'});
|
|
221
|
-
expect(calls).to.be.eq(1);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it('should transform properties by transformers', function () {
|
|
225
|
-
let calls = 0;
|
|
226
|
-
const dbs = new DatabaseSchema();
|
|
227
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
228
|
-
reg.addTransformer('myTransformer', value => {
|
|
229
|
-
calls++;
|
|
230
|
-
return String(value);
|
|
231
|
-
});
|
|
232
|
-
dbs.defineModel({
|
|
233
|
-
name: 'model',
|
|
234
|
-
properties: {
|
|
235
|
-
foo: {
|
|
236
|
-
type: DataType.ANY,
|
|
237
|
-
transform: ['myTransformer'],
|
|
238
|
-
},
|
|
239
|
-
bar: {
|
|
240
|
-
type: DataType.ANY,
|
|
241
|
-
transform: ['myTransformer'],
|
|
242
|
-
},
|
|
243
|
-
},
|
|
244
|
-
});
|
|
245
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
246
|
-
const res = T.transform('model', {foo: 1, bar: 2});
|
|
247
|
-
expect(res).to.be.eql({foo: '1', bar: '2'});
|
|
248
|
-
expect(calls).to.be.eq(2);
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
it('should pass arguments to the transformer', function () {
|
|
252
|
-
let calls = 0;
|
|
253
|
-
const dbs = new DatabaseSchema();
|
|
254
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
255
|
-
reg.addTransformer('myTransformer', (value, options, context) => {
|
|
256
|
-
calls++;
|
|
257
|
-
expect(value).to.be.eq(10);
|
|
258
|
-
expect(options).to.be.undefined;
|
|
259
|
-
expect(context).to.be.eql({
|
|
260
|
-
transformerName: 'myTransformer',
|
|
261
|
-
modelName: 'model',
|
|
262
|
-
propName: 'foo',
|
|
263
|
-
});
|
|
264
|
-
return String(value);
|
|
265
|
-
});
|
|
266
|
-
dbs.defineModel({
|
|
267
|
-
name: 'model',
|
|
268
|
-
properties: {
|
|
269
|
-
foo: {
|
|
270
|
-
type: DataType.ANY,
|
|
271
|
-
transform: 'myTransformer',
|
|
272
|
-
},
|
|
273
|
-
},
|
|
274
|
-
});
|
|
275
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
276
|
-
const res = T.transform('model', {foo: 10});
|
|
277
|
-
expect(res).to.be.eql({foo: '10'});
|
|
278
|
-
expect(calls).to.be.eq(1);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
it('should transform the property by the asynchronous transformer', async function () {
|
|
282
|
-
let calls = 0;
|
|
283
|
-
const dbs = new DatabaseSchema();
|
|
284
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
285
|
-
reg.addTransformer('myTransformer', value => {
|
|
286
|
-
calls++;
|
|
287
|
-
return Promise.resolve(String(value));
|
|
288
|
-
});
|
|
289
|
-
dbs.defineModel({
|
|
290
|
-
name: 'model',
|
|
291
|
-
properties: {
|
|
292
|
-
foo: {
|
|
293
|
-
type: DataType.ANY,
|
|
294
|
-
transform: 'myTransformer',
|
|
295
|
-
},
|
|
296
|
-
},
|
|
297
|
-
});
|
|
298
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
299
|
-
const promise = T.transform('model', {foo: 10});
|
|
300
|
-
expect(promise).to.be.instanceof(Promise);
|
|
301
|
-
const res = await promise;
|
|
302
|
-
expect(res).to.be.eql({foo: '10'});
|
|
303
|
-
expect(calls).to.be.eq(1);
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
it('should transform properties by the asynchronous transformer', async function () {
|
|
307
|
-
let calls = 0;
|
|
308
|
-
const dbs = new DatabaseSchema();
|
|
309
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
310
|
-
reg.addTransformer('myTransformer', value => {
|
|
311
|
-
calls++;
|
|
312
|
-
return Promise.resolve(String(value));
|
|
313
|
-
});
|
|
314
|
-
dbs.defineModel({
|
|
315
|
-
name: 'model',
|
|
316
|
-
properties: {
|
|
317
|
-
foo: {
|
|
318
|
-
type: DataType.ANY,
|
|
319
|
-
transform: 'myTransformer',
|
|
320
|
-
},
|
|
321
|
-
bar: {
|
|
322
|
-
type: DataType.ANY,
|
|
323
|
-
transform: 'myTransformer',
|
|
324
|
-
},
|
|
325
|
-
},
|
|
326
|
-
});
|
|
327
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
328
|
-
const promise = T.transform('model', {foo: 10, bar: 20});
|
|
329
|
-
expect(promise).to.be.instanceof(Promise);
|
|
330
|
-
const res = await promise;
|
|
331
|
-
expect(res).to.be.eql({foo: '10', bar: '20'});
|
|
332
|
-
expect(calls).to.be.eq(2);
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
it('should throw an error from the transformer', async function () {
|
|
336
|
-
let calls = 0;
|
|
337
|
-
const dbs = new DatabaseSchema();
|
|
338
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
339
|
-
reg.addTransformer('myTransformer', () => {
|
|
340
|
-
calls++;
|
|
341
|
-
throw new Error('My error');
|
|
342
|
-
});
|
|
343
|
-
dbs.defineModel({
|
|
344
|
-
name: 'model',
|
|
345
|
-
properties: {
|
|
346
|
-
foo: {
|
|
347
|
-
type: DataType.ANY,
|
|
348
|
-
transform: 'myTransformer',
|
|
349
|
-
},
|
|
350
|
-
},
|
|
351
|
-
});
|
|
352
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
353
|
-
const throwable = () => T.transform('model', {foo: 10});
|
|
354
|
-
expect(throwable).to.throw('My error');
|
|
355
|
-
expect(calls).to.be.eq(1);
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
it('should throw an error from the asynchronous transformer', async function () {
|
|
359
|
-
let calls = 0;
|
|
360
|
-
const dbs = new DatabaseSchema();
|
|
361
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
362
|
-
reg.addTransformer('myTransformer', async () => {
|
|
363
|
-
calls++;
|
|
364
|
-
throw new Error('My error');
|
|
365
|
-
});
|
|
366
|
-
dbs.defineModel({
|
|
367
|
-
name: 'model',
|
|
368
|
-
properties: {
|
|
369
|
-
foo: {
|
|
370
|
-
type: DataType.ANY,
|
|
371
|
-
transform: 'myTransformer',
|
|
372
|
-
},
|
|
373
|
-
},
|
|
374
|
-
});
|
|
375
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
376
|
-
const promise = T.transform('model', {foo: 10});
|
|
377
|
-
await expect(promise).to.rejectedWith('My error');
|
|
378
|
-
expect(calls).to.be.eq(1);
|
|
379
|
-
});
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
describe('when the option "transform" is a Function', function () {
|
|
383
|
-
describe('named transformers', function () {
|
|
384
|
-
it('should not transform the non-provided property', function () {
|
|
385
|
-
let calls = 0;
|
|
386
|
-
const dbs = new DatabaseSchema();
|
|
387
|
-
const myTransformer = function () {
|
|
388
|
-
calls++;
|
|
389
|
-
throw new Error('Should not to be called.');
|
|
390
|
-
};
|
|
391
|
-
dbs.defineModel({
|
|
392
|
-
name: 'model',
|
|
393
|
-
properties: {
|
|
394
|
-
foo: {
|
|
395
|
-
type: DataType.ANY,
|
|
396
|
-
transform: myTransformer,
|
|
397
|
-
},
|
|
398
|
-
},
|
|
399
|
-
});
|
|
400
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
401
|
-
const res = T.transform('model', {});
|
|
402
|
-
expect(res).to.be.eql({});
|
|
403
|
-
expect(calls).to.be.eq(0);
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
it('should not transform non-provided properties when the option "isPartial" is true', function () {
|
|
407
|
-
let calls = 0;
|
|
408
|
-
const dbs = new DatabaseSchema();
|
|
409
|
-
const myTransformer = function () {
|
|
410
|
-
calls++;
|
|
411
|
-
throw new Error('Should not to be called.');
|
|
412
|
-
};
|
|
413
|
-
dbs.defineModel({
|
|
414
|
-
name: 'model',
|
|
415
|
-
properties: {
|
|
416
|
-
foo: {
|
|
417
|
-
type: DataType.ANY,
|
|
418
|
-
transform: myTransformer,
|
|
419
|
-
},
|
|
420
|
-
bar: {
|
|
421
|
-
type: DataType.ANY,
|
|
422
|
-
transform: myTransformer,
|
|
423
|
-
},
|
|
424
|
-
},
|
|
425
|
-
});
|
|
426
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
427
|
-
const res = T.transform('model', {}, true);
|
|
428
|
-
expect(res).to.be.eql({});
|
|
429
|
-
expect(calls).to.be.eq(0);
|
|
430
|
-
});
|
|
431
|
-
|
|
432
|
-
it('should not transform undefined and null values', function () {
|
|
433
|
-
let calls = 0;
|
|
434
|
-
const dbs = new DatabaseSchema();
|
|
435
|
-
const myTransformer = function () {
|
|
436
|
-
calls++;
|
|
437
|
-
throw new Error('Should not to be called.');
|
|
438
|
-
};
|
|
439
|
-
dbs.defineModel({
|
|
440
|
-
name: 'model',
|
|
441
|
-
properties: {
|
|
442
|
-
foo: {
|
|
443
|
-
type: DataType.ANY,
|
|
444
|
-
transform: myTransformer,
|
|
445
|
-
},
|
|
446
|
-
},
|
|
447
|
-
});
|
|
448
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
449
|
-
const res1 = T.transform('model', {foo: undefined});
|
|
450
|
-
const res2 = T.transform('model', {foo: null});
|
|
451
|
-
expect(res1).to.be.eql({foo: undefined});
|
|
452
|
-
expect(res2).to.be.eql({foo: null});
|
|
453
|
-
expect(calls).to.be.eq(0);
|
|
454
|
-
});
|
|
455
|
-
|
|
456
|
-
it('should not transform the empty value', function () {
|
|
457
|
-
let calls = 0;
|
|
458
|
-
const dbs = new DatabaseSchema();
|
|
459
|
-
const myTransformer = function () {
|
|
460
|
-
calls++;
|
|
461
|
-
throw new Error('Should not to be called.');
|
|
462
|
-
};
|
|
463
|
-
dbs.defineModel({
|
|
464
|
-
name: 'model',
|
|
465
|
-
properties: {
|
|
466
|
-
foo: {
|
|
467
|
-
type: DataType.ANY,
|
|
468
|
-
transform: myTransformer,
|
|
469
|
-
},
|
|
470
|
-
},
|
|
471
|
-
});
|
|
472
|
-
dbs
|
|
473
|
-
.getService(EmptyValuesService)
|
|
474
|
-
.setEmptyValuesOf(DataType.ANY, [10]);
|
|
475
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
476
|
-
const res = T.transform('model', {foo: 10});
|
|
477
|
-
expect(res).to.be.eql({foo: 10});
|
|
478
|
-
expect(calls).to.be.eq(0);
|
|
479
|
-
});
|
|
480
|
-
|
|
481
|
-
it('should transform the property by the transformer', function () {
|
|
482
|
-
let calls = 0;
|
|
483
|
-
const dbs = new DatabaseSchema();
|
|
484
|
-
const myTransformer = function (value) {
|
|
485
|
-
calls++;
|
|
486
|
-
return String(value);
|
|
487
|
-
};
|
|
488
|
-
dbs.defineModel({
|
|
489
|
-
name: 'model',
|
|
490
|
-
properties: {
|
|
491
|
-
foo: {
|
|
492
|
-
type: DataType.ANY,
|
|
493
|
-
transform: myTransformer,
|
|
494
|
-
},
|
|
495
|
-
},
|
|
496
|
-
});
|
|
497
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
498
|
-
const res = T.transform('model', {foo: 10});
|
|
499
|
-
expect(res).to.be.eql({foo: '10'});
|
|
500
|
-
expect(calls).to.be.eq(1);
|
|
501
|
-
});
|
|
502
|
-
|
|
503
|
-
it('should transform properties by transformers', function () {
|
|
504
|
-
let calls = 0;
|
|
505
|
-
const dbs = new DatabaseSchema();
|
|
506
|
-
const myTransformer = function (value) {
|
|
507
|
-
calls++;
|
|
508
|
-
return String(value);
|
|
509
|
-
};
|
|
510
|
-
dbs.defineModel({
|
|
511
|
-
name: 'model',
|
|
512
|
-
properties: {
|
|
513
|
-
foo: {
|
|
514
|
-
type: DataType.ANY,
|
|
515
|
-
transform: myTransformer,
|
|
516
|
-
},
|
|
517
|
-
bar: {
|
|
518
|
-
type: DataType.ANY,
|
|
519
|
-
transform: myTransformer,
|
|
520
|
-
},
|
|
521
|
-
},
|
|
522
|
-
});
|
|
523
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
524
|
-
const res = T.transform('model', {foo: 1, bar: 2});
|
|
525
|
-
expect(res).to.be.eql({foo: '1', bar: '2'});
|
|
526
|
-
expect(calls).to.be.eq(2);
|
|
527
|
-
});
|
|
528
|
-
|
|
529
|
-
it('should pass arguments to the transformer', function () {
|
|
530
|
-
let calls = 0;
|
|
531
|
-
const dbs = new DatabaseSchema();
|
|
532
|
-
const myTransformer = function (value, options, context) {
|
|
533
|
-
calls++;
|
|
534
|
-
expect(value).to.be.eq(10);
|
|
535
|
-
expect(options).to.be.undefined;
|
|
536
|
-
expect(context).to.be.eql({
|
|
537
|
-
transformerName: 'myTransformer',
|
|
538
|
-
modelName: 'model',
|
|
539
|
-
propName: 'foo',
|
|
540
|
-
});
|
|
541
|
-
return String(value);
|
|
542
|
-
};
|
|
543
|
-
dbs.defineModel({
|
|
544
|
-
name: 'model',
|
|
545
|
-
properties: {
|
|
546
|
-
foo: {
|
|
547
|
-
type: DataType.ANY,
|
|
548
|
-
transform: myTransformer,
|
|
549
|
-
},
|
|
550
|
-
},
|
|
551
|
-
});
|
|
552
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
553
|
-
const res = T.transform('model', {foo: 10});
|
|
554
|
-
expect(res).to.be.eql({foo: '10'});
|
|
555
|
-
expect(calls).to.be.eq(1);
|
|
556
|
-
});
|
|
557
|
-
|
|
558
|
-
it('should transform the property by the asynchronous transformer', async function () {
|
|
559
|
-
let calls = 0;
|
|
560
|
-
const dbs = new DatabaseSchema();
|
|
561
|
-
const myTransformer = function (value) {
|
|
562
|
-
calls++;
|
|
563
|
-
return Promise.resolve(String(value));
|
|
564
|
-
};
|
|
565
|
-
dbs.defineModel({
|
|
566
|
-
name: 'model',
|
|
567
|
-
properties: {
|
|
568
|
-
foo: {
|
|
569
|
-
type: DataType.ANY,
|
|
570
|
-
transform: myTransformer,
|
|
571
|
-
},
|
|
572
|
-
},
|
|
573
|
-
});
|
|
574
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
575
|
-
const promise = T.transform('model', {foo: 10});
|
|
576
|
-
expect(promise).to.be.instanceof(Promise);
|
|
577
|
-
const res = await promise;
|
|
578
|
-
expect(res).to.be.eql({foo: '10'});
|
|
579
|
-
expect(calls).to.be.eq(1);
|
|
580
|
-
});
|
|
581
|
-
|
|
582
|
-
it('should transform properties by the asynchronous transformer', async function () {
|
|
583
|
-
let calls = 0;
|
|
584
|
-
const dbs = new DatabaseSchema();
|
|
585
|
-
const myTransformer = function (value) {
|
|
586
|
-
calls++;
|
|
587
|
-
return Promise.resolve(String(value));
|
|
588
|
-
};
|
|
589
|
-
dbs.defineModel({
|
|
590
|
-
name: 'model',
|
|
591
|
-
properties: {
|
|
592
|
-
foo: {
|
|
593
|
-
type: DataType.ANY,
|
|
594
|
-
transform: myTransformer,
|
|
595
|
-
},
|
|
596
|
-
bar: {
|
|
597
|
-
type: DataType.ANY,
|
|
598
|
-
transform: myTransformer,
|
|
599
|
-
},
|
|
600
|
-
},
|
|
601
|
-
});
|
|
602
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
603
|
-
const promise = T.transform('model', {foo: 10, bar: 20});
|
|
604
|
-
expect(promise).to.be.instanceof(Promise);
|
|
605
|
-
const res = await promise;
|
|
606
|
-
expect(res).to.be.eql({foo: '10', bar: '20'});
|
|
607
|
-
expect(calls).to.be.eq(2);
|
|
608
|
-
});
|
|
609
|
-
|
|
610
|
-
it('should throw an error from the transformer', async function () {
|
|
611
|
-
let calls = 0;
|
|
612
|
-
const dbs = new DatabaseSchema();
|
|
613
|
-
const myTransformer = function () {
|
|
614
|
-
calls++;
|
|
615
|
-
throw new Error('My error');
|
|
616
|
-
};
|
|
617
|
-
dbs.defineModel({
|
|
618
|
-
name: 'model',
|
|
619
|
-
properties: {
|
|
620
|
-
foo: {
|
|
621
|
-
type: DataType.ANY,
|
|
622
|
-
transform: myTransformer,
|
|
623
|
-
},
|
|
624
|
-
},
|
|
625
|
-
});
|
|
626
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
627
|
-
const throwable = () => T.transform('model', {foo: 10});
|
|
628
|
-
expect(throwable).to.throw('My error');
|
|
629
|
-
expect(calls).to.be.eq(1);
|
|
630
|
-
});
|
|
631
|
-
|
|
632
|
-
it('should throw an error from the asynchronous transformer', async function () {
|
|
633
|
-
let calls = 0;
|
|
634
|
-
const dbs = new DatabaseSchema();
|
|
635
|
-
const myTransformer = async function () {
|
|
636
|
-
calls++;
|
|
637
|
-
throw new Error('My error');
|
|
638
|
-
};
|
|
639
|
-
dbs.defineModel({
|
|
640
|
-
name: 'model',
|
|
641
|
-
properties: {
|
|
642
|
-
foo: {
|
|
643
|
-
type: DataType.ANY,
|
|
644
|
-
transform: myTransformer,
|
|
645
|
-
},
|
|
646
|
-
},
|
|
647
|
-
});
|
|
648
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
649
|
-
const promise = T.transform('model', {foo: 10});
|
|
650
|
-
await expect(promise).to.rejectedWith('My error');
|
|
651
|
-
expect(calls).to.be.eq(1);
|
|
652
|
-
});
|
|
653
|
-
});
|
|
654
|
-
|
|
655
|
-
describe('anonymous transformers', function () {
|
|
656
|
-
it('should not transform the non-provided property', function () {
|
|
657
|
-
let calls = 0;
|
|
658
|
-
const dbs = new DatabaseSchema();
|
|
659
|
-
dbs.defineModel({
|
|
660
|
-
name: 'model',
|
|
661
|
-
properties: {
|
|
662
|
-
foo: {
|
|
663
|
-
type: DataType.ANY,
|
|
664
|
-
transform() {
|
|
665
|
-
calls++;
|
|
666
|
-
throw new Error('Should not to be called.');
|
|
667
|
-
},
|
|
668
|
-
},
|
|
669
|
-
},
|
|
670
|
-
});
|
|
671
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
672
|
-
const res = T.transform('model', {});
|
|
673
|
-
expect(res).to.be.eql({});
|
|
674
|
-
expect(calls).to.be.eq(0);
|
|
675
|
-
});
|
|
676
|
-
|
|
677
|
-
it('should not transform non-provided properties when the option "isPartial" is true', function () {
|
|
678
|
-
let calls = 0;
|
|
679
|
-
const dbs = new DatabaseSchema();
|
|
680
|
-
dbs.defineModel({
|
|
681
|
-
name: 'model',
|
|
682
|
-
properties: {
|
|
683
|
-
foo: {
|
|
684
|
-
type: DataType.ANY,
|
|
685
|
-
transform() {
|
|
686
|
-
calls++;
|
|
687
|
-
throw new Error('Should not to be called.');
|
|
688
|
-
},
|
|
689
|
-
},
|
|
690
|
-
bar: {
|
|
691
|
-
type: DataType.ANY,
|
|
692
|
-
transform() {
|
|
693
|
-
calls++;
|
|
694
|
-
throw new Error('Should not to be called.');
|
|
695
|
-
},
|
|
696
|
-
},
|
|
697
|
-
},
|
|
698
|
-
});
|
|
699
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
700
|
-
const res = T.transform('model', {}, true);
|
|
701
|
-
expect(res).to.be.eql({});
|
|
702
|
-
expect(calls).to.be.eq(0);
|
|
703
|
-
});
|
|
704
|
-
|
|
705
|
-
it('should not transform undefined and null values', function () {
|
|
706
|
-
let calls = 0;
|
|
707
|
-
const dbs = new DatabaseSchema();
|
|
708
|
-
dbs.defineModel({
|
|
709
|
-
name: 'model',
|
|
710
|
-
properties: {
|
|
711
|
-
foo: {
|
|
712
|
-
type: DataType.ANY,
|
|
713
|
-
transform() {
|
|
714
|
-
calls++;
|
|
715
|
-
throw new Error('Should not to be called.');
|
|
716
|
-
},
|
|
717
|
-
},
|
|
718
|
-
},
|
|
719
|
-
});
|
|
720
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
721
|
-
const res1 = T.transform('model', {foo: undefined});
|
|
722
|
-
const res2 = T.transform('model', {foo: null});
|
|
723
|
-
expect(res1).to.be.eql({foo: undefined});
|
|
724
|
-
expect(res2).to.be.eql({foo: null});
|
|
725
|
-
expect(calls).to.be.eq(0);
|
|
726
|
-
});
|
|
727
|
-
|
|
728
|
-
it('should not transform the empty value', function () {
|
|
729
|
-
let calls = 0;
|
|
730
|
-
const dbs = new DatabaseSchema();
|
|
731
|
-
dbs.defineModel({
|
|
732
|
-
name: 'model',
|
|
733
|
-
properties: {
|
|
734
|
-
foo: {
|
|
735
|
-
type: DataType.ANY,
|
|
736
|
-
transform() {
|
|
737
|
-
calls++;
|
|
738
|
-
throw new Error('Should not to be called.');
|
|
739
|
-
},
|
|
740
|
-
},
|
|
741
|
-
},
|
|
742
|
-
});
|
|
743
|
-
dbs
|
|
744
|
-
.getService(EmptyValuesService)
|
|
745
|
-
.setEmptyValuesOf(DataType.ANY, [10]);
|
|
746
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
747
|
-
const res = T.transform('model', {foo: 10});
|
|
748
|
-
expect(res).to.be.eql({foo: 10});
|
|
749
|
-
expect(calls).to.be.eq(0);
|
|
750
|
-
});
|
|
751
|
-
|
|
752
|
-
it('should transform the property by the transformer', function () {
|
|
753
|
-
let calls = 0;
|
|
754
|
-
const dbs = new DatabaseSchema();
|
|
755
|
-
dbs.defineModel({
|
|
756
|
-
name: 'model',
|
|
757
|
-
properties: {
|
|
758
|
-
foo: {
|
|
759
|
-
type: DataType.ANY,
|
|
760
|
-
transform(value) {
|
|
761
|
-
calls++;
|
|
762
|
-
return String(value);
|
|
763
|
-
},
|
|
764
|
-
},
|
|
765
|
-
},
|
|
766
|
-
});
|
|
767
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
768
|
-
const res = T.transform('model', {foo: 10});
|
|
769
|
-
expect(res).to.be.eql({foo: '10'});
|
|
770
|
-
expect(calls).to.be.eq(1);
|
|
771
|
-
});
|
|
772
|
-
|
|
773
|
-
it('should transform properties by transformers', function () {
|
|
774
|
-
let calls = 0;
|
|
775
|
-
const dbs = new DatabaseSchema();
|
|
776
|
-
dbs.defineModel({
|
|
777
|
-
name: 'model',
|
|
778
|
-
properties: {
|
|
779
|
-
foo: {
|
|
780
|
-
type: DataType.ANY,
|
|
781
|
-
transform(value) {
|
|
782
|
-
calls++;
|
|
783
|
-
return String(value);
|
|
784
|
-
},
|
|
785
|
-
},
|
|
786
|
-
bar: {
|
|
787
|
-
type: DataType.ANY,
|
|
788
|
-
transform(value) {
|
|
789
|
-
calls++;
|
|
790
|
-
return String(value);
|
|
791
|
-
},
|
|
792
|
-
},
|
|
793
|
-
},
|
|
794
|
-
});
|
|
795
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
796
|
-
const res = T.transform('model', {foo: 1, bar: 2});
|
|
797
|
-
expect(res).to.be.eql({foo: '1', bar: '2'});
|
|
798
|
-
expect(calls).to.be.eq(2);
|
|
799
|
-
});
|
|
800
|
-
|
|
801
|
-
it('should pass arguments to the transformer', function () {
|
|
802
|
-
let calls = 0;
|
|
803
|
-
const dbs = new DatabaseSchema();
|
|
804
|
-
dbs.defineModel({
|
|
805
|
-
name: 'model',
|
|
806
|
-
properties: {
|
|
807
|
-
foo: {
|
|
808
|
-
type: DataType.ANY,
|
|
809
|
-
transform(value, options, context) {
|
|
810
|
-
calls++;
|
|
811
|
-
expect(value).to.be.eq(10);
|
|
812
|
-
expect(options).to.be.undefined;
|
|
813
|
-
expect(context).to.be.eql({
|
|
814
|
-
transformerName: undefined,
|
|
815
|
-
modelName: 'model',
|
|
816
|
-
propName: 'foo',
|
|
817
|
-
});
|
|
818
|
-
return String(value);
|
|
819
|
-
},
|
|
820
|
-
},
|
|
821
|
-
},
|
|
822
|
-
});
|
|
823
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
824
|
-
const res = T.transform('model', {foo: 10});
|
|
825
|
-
expect(res).to.be.eql({foo: '10'});
|
|
826
|
-
expect(calls).to.be.eq(1);
|
|
827
|
-
});
|
|
828
|
-
|
|
829
|
-
it('should transform the property by the asynchronous transformer', async function () {
|
|
830
|
-
let calls = 0;
|
|
831
|
-
const dbs = new DatabaseSchema();
|
|
832
|
-
dbs.defineModel({
|
|
833
|
-
name: 'model',
|
|
834
|
-
properties: {
|
|
835
|
-
foo: {
|
|
836
|
-
type: DataType.ANY,
|
|
837
|
-
transform(value) {
|
|
838
|
-
calls++;
|
|
839
|
-
return Promise.resolve(String(value));
|
|
840
|
-
},
|
|
841
|
-
},
|
|
842
|
-
},
|
|
843
|
-
});
|
|
844
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
845
|
-
const promise = T.transform('model', {foo: 10});
|
|
846
|
-
expect(promise).to.be.instanceof(Promise);
|
|
847
|
-
const res = await promise;
|
|
848
|
-
expect(res).to.be.eql({foo: '10'});
|
|
849
|
-
expect(calls).to.be.eq(1);
|
|
850
|
-
});
|
|
851
|
-
|
|
852
|
-
it('should transform properties by the asynchronous transformer', async function () {
|
|
853
|
-
let calls = 0;
|
|
854
|
-
const dbs = new DatabaseSchema();
|
|
855
|
-
dbs.defineModel({
|
|
856
|
-
name: 'model',
|
|
857
|
-
properties: {
|
|
858
|
-
foo: {
|
|
859
|
-
type: DataType.ANY,
|
|
860
|
-
transform(value) {
|
|
861
|
-
calls++;
|
|
862
|
-
return Promise.resolve(String(value));
|
|
863
|
-
},
|
|
864
|
-
},
|
|
865
|
-
bar: {
|
|
866
|
-
type: DataType.ANY,
|
|
867
|
-
transform(value) {
|
|
868
|
-
calls++;
|
|
869
|
-
return Promise.resolve(String(value));
|
|
870
|
-
},
|
|
871
|
-
},
|
|
872
|
-
},
|
|
873
|
-
});
|
|
874
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
875
|
-
const promise = T.transform('model', {foo: 10, bar: 20});
|
|
876
|
-
expect(promise).to.be.instanceof(Promise);
|
|
877
|
-
const res = await promise;
|
|
878
|
-
expect(res).to.be.eql({foo: '10', bar: '20'});
|
|
879
|
-
expect(calls).to.be.eq(2);
|
|
880
|
-
});
|
|
881
|
-
|
|
882
|
-
it('should throw an error from the transformer', async function () {
|
|
883
|
-
let calls = 0;
|
|
884
|
-
const dbs = new DatabaseSchema();
|
|
885
|
-
dbs.defineModel({
|
|
886
|
-
name: 'model',
|
|
887
|
-
properties: {
|
|
888
|
-
foo: {
|
|
889
|
-
type: DataType.ANY,
|
|
890
|
-
transform() {
|
|
891
|
-
calls++;
|
|
892
|
-
throw new Error('My error');
|
|
893
|
-
},
|
|
894
|
-
},
|
|
895
|
-
},
|
|
896
|
-
});
|
|
897
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
898
|
-
const throwable = () => T.transform('model', {foo: 10});
|
|
899
|
-
expect(throwable).to.throw('My error');
|
|
900
|
-
expect(calls).to.be.eq(1);
|
|
901
|
-
});
|
|
902
|
-
|
|
903
|
-
it('should throw an error from the asynchronous transformer', async function () {
|
|
904
|
-
let calls = 0;
|
|
905
|
-
const dbs = new DatabaseSchema();
|
|
906
|
-
dbs.defineModel({
|
|
907
|
-
name: 'model',
|
|
908
|
-
properties: {
|
|
909
|
-
foo: {
|
|
910
|
-
type: DataType.ANY,
|
|
911
|
-
async transform() {
|
|
912
|
-
calls++;
|
|
913
|
-
throw new Error('My error');
|
|
914
|
-
},
|
|
915
|
-
},
|
|
916
|
-
},
|
|
917
|
-
});
|
|
918
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
919
|
-
const promise = T.transform('model', {foo: 10});
|
|
920
|
-
await expect(promise).to.rejectedWith('My error');
|
|
921
|
-
expect(calls).to.be.eq(1);
|
|
922
|
-
});
|
|
923
|
-
});
|
|
924
|
-
});
|
|
925
|
-
|
|
926
|
-
describe('when the option "transform" is an Array', function () {
|
|
927
|
-
describe('when an Array element is a String', function () {
|
|
928
|
-
it('should not transform the non-provided property', function () {
|
|
929
|
-
let calls = 0;
|
|
930
|
-
const dbs = new DatabaseSchema();
|
|
931
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
932
|
-
reg.addTransformer('myTransformer', () => {
|
|
933
|
-
calls++;
|
|
934
|
-
throw new Error('Should not to be called.');
|
|
935
|
-
});
|
|
936
|
-
dbs.defineModel({
|
|
937
|
-
name: 'model',
|
|
938
|
-
properties: {
|
|
939
|
-
foo: {
|
|
940
|
-
type: DataType.ANY,
|
|
941
|
-
transform: ['myTransformer'],
|
|
942
|
-
},
|
|
943
|
-
},
|
|
944
|
-
});
|
|
945
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
946
|
-
const res = T.transform('model', {});
|
|
947
|
-
expect(res).to.be.eql({});
|
|
948
|
-
expect(calls).to.be.eq(0);
|
|
949
|
-
});
|
|
950
|
-
|
|
951
|
-
it('should not transform non-provided properties when the option "isPartial" is true', function () {
|
|
952
|
-
let calls = 0;
|
|
953
|
-
const dbs = new DatabaseSchema();
|
|
954
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
955
|
-
reg.addTransformer('myTransformer', () => {
|
|
956
|
-
calls++;
|
|
957
|
-
throw new Error('Should not to be called.');
|
|
958
|
-
});
|
|
959
|
-
dbs.defineModel({
|
|
960
|
-
name: 'model',
|
|
961
|
-
properties: {
|
|
962
|
-
foo: {
|
|
963
|
-
type: DataType.ANY,
|
|
964
|
-
transform: ['myTransformer'],
|
|
965
|
-
},
|
|
966
|
-
bar: {
|
|
967
|
-
type: DataType.ANY,
|
|
968
|
-
transform: ['myTransformer'],
|
|
969
|
-
},
|
|
970
|
-
},
|
|
971
|
-
});
|
|
972
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
973
|
-
const res = T.transform('model', {}, true);
|
|
974
|
-
expect(res).to.be.eql({});
|
|
975
|
-
expect(calls).to.be.eq(0);
|
|
976
|
-
});
|
|
977
|
-
|
|
978
|
-
it('should not transform undefined and null values', function () {
|
|
979
|
-
let calls = 0;
|
|
980
|
-
const dbs = new DatabaseSchema();
|
|
981
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
982
|
-
reg.addTransformer('myTransformer', () => {
|
|
983
|
-
calls++;
|
|
984
|
-
throw new Error('Should not to be called.');
|
|
985
|
-
});
|
|
986
|
-
dbs.defineModel({
|
|
987
|
-
name: 'model',
|
|
988
|
-
properties: {
|
|
989
|
-
foo: {
|
|
990
|
-
type: DataType.ANY,
|
|
991
|
-
transform: ['myTransformer'],
|
|
992
|
-
},
|
|
993
|
-
},
|
|
994
|
-
});
|
|
995
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
996
|
-
const res1 = T.transform('model', {foo: undefined});
|
|
997
|
-
const res2 = T.transform('model', {foo: null});
|
|
998
|
-
expect(res1).to.be.eql({foo: undefined});
|
|
999
|
-
expect(res2).to.be.eql({foo: null});
|
|
1000
|
-
expect(calls).to.be.eq(0);
|
|
1001
|
-
});
|
|
1002
|
-
|
|
1003
|
-
it('should not transform the empty value', function () {
|
|
1004
|
-
let calls = 0;
|
|
1005
|
-
const dbs = new DatabaseSchema();
|
|
1006
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1007
|
-
reg.addTransformer('myTransformer', () => {
|
|
1008
|
-
calls++;
|
|
1009
|
-
throw new Error('Should not to be called.');
|
|
1010
|
-
});
|
|
1011
|
-
dbs.defineModel({
|
|
1012
|
-
name: 'model',
|
|
1013
|
-
properties: {
|
|
1014
|
-
foo: {
|
|
1015
|
-
type: DataType.ANY,
|
|
1016
|
-
transform: ['myTransformer'],
|
|
1017
|
-
},
|
|
1018
|
-
},
|
|
1019
|
-
});
|
|
1020
|
-
dbs
|
|
1021
|
-
.getService(EmptyValuesService)
|
|
1022
|
-
.setEmptyValuesOf(DataType.ANY, [10]);
|
|
1023
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1024
|
-
const res = T.transform('model', {foo: 10});
|
|
1025
|
-
expect(res).to.be.eql({foo: 10});
|
|
1026
|
-
expect(calls).to.be.eq(0);
|
|
1027
|
-
});
|
|
1028
|
-
|
|
1029
|
-
it('should transform the property by the transformer', function () {
|
|
1030
|
-
let calls = 0;
|
|
1031
|
-
const dbs = new DatabaseSchema();
|
|
1032
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1033
|
-
reg.addTransformer('myTransformer', value => {
|
|
1034
|
-
calls++;
|
|
1035
|
-
return String(value);
|
|
1036
|
-
});
|
|
1037
|
-
dbs.defineModel({
|
|
1038
|
-
name: 'model',
|
|
1039
|
-
properties: {
|
|
1040
|
-
foo: {
|
|
1041
|
-
type: DataType.ANY,
|
|
1042
|
-
transform: ['myTransformer'],
|
|
1043
|
-
},
|
|
1044
|
-
},
|
|
1045
|
-
});
|
|
1046
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1047
|
-
const res = T.transform('model', {foo: 10});
|
|
1048
|
-
expect(res).to.be.eql({foo: '10'});
|
|
1049
|
-
expect(calls).to.be.eq(1);
|
|
1050
|
-
});
|
|
1051
|
-
|
|
1052
|
-
it('should transform properties by transformers', function () {
|
|
1053
|
-
let calls = 0;
|
|
1054
|
-
const dbs = new DatabaseSchema();
|
|
1055
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1056
|
-
reg.addTransformer('myTransformer', value => {
|
|
1057
|
-
calls++;
|
|
1058
|
-
return String(value);
|
|
1059
|
-
});
|
|
1060
|
-
dbs.defineModel({
|
|
1061
|
-
name: 'model',
|
|
1062
|
-
properties: {
|
|
1063
|
-
foo: {
|
|
1064
|
-
type: DataType.ANY,
|
|
1065
|
-
transform: ['myTransformer'],
|
|
1066
|
-
},
|
|
1067
|
-
bar: {
|
|
1068
|
-
type: DataType.ANY,
|
|
1069
|
-
transform: ['myTransformer'],
|
|
1070
|
-
},
|
|
1071
|
-
},
|
|
1072
|
-
});
|
|
1073
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1074
|
-
const res = T.transform('model', {foo: 1, bar: 2});
|
|
1075
|
-
expect(res).to.be.eql({foo: '1', bar: '2'});
|
|
1076
|
-
expect(calls).to.be.eq(2);
|
|
1077
|
-
});
|
|
1078
|
-
|
|
1079
|
-
it('should pass arguments to the transformer', function () {
|
|
1080
|
-
let calls = 0;
|
|
1081
|
-
const dbs = new DatabaseSchema();
|
|
1082
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1083
|
-
reg.addTransformer('myTransformer', (value, options, context) => {
|
|
1084
|
-
calls++;
|
|
1085
|
-
expect(value).to.be.eq(10);
|
|
1086
|
-
expect(options).to.be.undefined;
|
|
1087
|
-
expect(context).to.be.eql({
|
|
1088
|
-
transformerName: 'myTransformer',
|
|
1089
|
-
modelName: 'model',
|
|
1090
|
-
propName: 'foo',
|
|
1091
|
-
});
|
|
1092
|
-
return String(value);
|
|
1093
|
-
});
|
|
1094
|
-
dbs.defineModel({
|
|
1095
|
-
name: 'model',
|
|
1096
|
-
properties: {
|
|
1097
|
-
foo: {
|
|
1098
|
-
type: DataType.ANY,
|
|
1099
|
-
transform: ['myTransformer'],
|
|
1100
|
-
},
|
|
1101
|
-
},
|
|
1102
|
-
});
|
|
1103
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1104
|
-
const res = T.transform('model', {foo: 10});
|
|
1105
|
-
expect(res).to.be.eql({foo: '10'});
|
|
1106
|
-
expect(calls).to.be.eq(1);
|
|
1107
|
-
});
|
|
1108
|
-
|
|
1109
|
-
it('should transform the property by the asynchronous transformer', async function () {
|
|
1110
|
-
let calls = 0;
|
|
1111
|
-
const dbs = new DatabaseSchema();
|
|
1112
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1113
|
-
reg.addTransformer('myTransformer', value => {
|
|
1114
|
-
calls++;
|
|
1115
|
-
return Promise.resolve(String(value));
|
|
1116
|
-
});
|
|
1117
|
-
dbs.defineModel({
|
|
1118
|
-
name: 'model',
|
|
1119
|
-
properties: {
|
|
1120
|
-
foo: {
|
|
1121
|
-
type: DataType.ANY,
|
|
1122
|
-
transform: ['myTransformer'],
|
|
1123
|
-
},
|
|
1124
|
-
},
|
|
1125
|
-
});
|
|
1126
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1127
|
-
const promise = T.transform('model', {foo: 10});
|
|
1128
|
-
expect(promise).to.be.instanceof(Promise);
|
|
1129
|
-
const res = await promise;
|
|
1130
|
-
expect(res).to.be.eql({foo: '10'});
|
|
1131
|
-
expect(calls).to.be.eq(1);
|
|
1132
|
-
});
|
|
1133
|
-
|
|
1134
|
-
it('should transform properties by the asynchronous transformer', async function () {
|
|
1135
|
-
let calls = 0;
|
|
1136
|
-
const dbs = new DatabaseSchema();
|
|
1137
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1138
|
-
reg.addTransformer('myTransformer', value => {
|
|
1139
|
-
calls++;
|
|
1140
|
-
return Promise.resolve(String(value));
|
|
1141
|
-
});
|
|
1142
|
-
dbs.defineModel({
|
|
1143
|
-
name: 'model',
|
|
1144
|
-
properties: {
|
|
1145
|
-
foo: {
|
|
1146
|
-
type: DataType.ANY,
|
|
1147
|
-
transform: ['myTransformer'],
|
|
1148
|
-
},
|
|
1149
|
-
bar: {
|
|
1150
|
-
type: DataType.ANY,
|
|
1151
|
-
transform: ['myTransformer'],
|
|
1152
|
-
},
|
|
1153
|
-
},
|
|
1154
|
-
});
|
|
1155
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1156
|
-
const promise = T.transform('model', {foo: 10, bar: 20});
|
|
1157
|
-
expect(promise).to.be.instanceof(Promise);
|
|
1158
|
-
const res = await promise;
|
|
1159
|
-
expect(res).to.be.eql({foo: '10', bar: '20'});
|
|
1160
|
-
expect(calls).to.be.eq(2);
|
|
1161
|
-
});
|
|
1162
|
-
|
|
1163
|
-
it('should throw an error from the transformer', async function () {
|
|
1164
|
-
let calls = 0;
|
|
1165
|
-
const dbs = new DatabaseSchema();
|
|
1166
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1167
|
-
reg.addTransformer('myTransformer', () => {
|
|
1168
|
-
calls++;
|
|
1169
|
-
throw new Error('My error');
|
|
1170
|
-
});
|
|
1171
|
-
dbs.defineModel({
|
|
1172
|
-
name: 'model',
|
|
1173
|
-
properties: {
|
|
1174
|
-
foo: {
|
|
1175
|
-
type: DataType.ANY,
|
|
1176
|
-
transform: ['myTransformer'],
|
|
1177
|
-
},
|
|
1178
|
-
},
|
|
1179
|
-
});
|
|
1180
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1181
|
-
const throwable = () => T.transform('model', {foo: 10});
|
|
1182
|
-
expect(throwable).to.throw('My error');
|
|
1183
|
-
expect(calls).to.be.eq(1);
|
|
1184
|
-
});
|
|
1185
|
-
|
|
1186
|
-
it('should throw an error from the asynchronous transformer', async function () {
|
|
1187
|
-
let calls = 0;
|
|
1188
|
-
const dbs = new DatabaseSchema();
|
|
1189
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1190
|
-
reg.addTransformer('myTransformer', async () => {
|
|
1191
|
-
calls++;
|
|
1192
|
-
throw new Error('My error');
|
|
1193
|
-
});
|
|
1194
|
-
dbs.defineModel({
|
|
1195
|
-
name: 'model',
|
|
1196
|
-
properties: {
|
|
1197
|
-
foo: {
|
|
1198
|
-
type: DataType.ANY,
|
|
1199
|
-
transform: ['myTransformer'],
|
|
1200
|
-
},
|
|
1201
|
-
},
|
|
1202
|
-
});
|
|
1203
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1204
|
-
const promise = T.transform('model', {foo: 10});
|
|
1205
|
-
await expect(promise).to.rejectedWith('My error');
|
|
1206
|
-
expect(calls).to.be.eq(1);
|
|
1207
|
-
});
|
|
1208
|
-
|
|
1209
|
-
it('should transform the property by transformers with the correct order', function () {
|
|
1210
|
-
const order = [];
|
|
1211
|
-
const dbs = new DatabaseSchema();
|
|
1212
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1213
|
-
reg.addTransformer('myTransformer1', value => {
|
|
1214
|
-
order.push('myTransformer1');
|
|
1215
|
-
return String(value);
|
|
1216
|
-
});
|
|
1217
|
-
reg.addTransformer('myTransformer2', value => {
|
|
1218
|
-
order.push('myTransformer2');
|
|
1219
|
-
return value.toUpperCase();
|
|
1220
|
-
});
|
|
1221
|
-
dbs.defineModel({
|
|
1222
|
-
name: 'model',
|
|
1223
|
-
properties: {
|
|
1224
|
-
foo: {
|
|
1225
|
-
type: DataType.ANY,
|
|
1226
|
-
transform: ['myTransformer1', 'myTransformer2'],
|
|
1227
|
-
},
|
|
1228
|
-
},
|
|
1229
|
-
});
|
|
1230
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1231
|
-
const res = T.transform('model', {foo: true});
|
|
1232
|
-
expect(res).to.be.eql({foo: 'TRUE'});
|
|
1233
|
-
expect(order).to.be.eql(['myTransformer1', 'myTransformer2']);
|
|
1234
|
-
});
|
|
1235
|
-
|
|
1236
|
-
it('should transform the property by asynchronous transformers with the correct order', async function () {
|
|
1237
|
-
const order = [];
|
|
1238
|
-
const dbs = new DatabaseSchema();
|
|
1239
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1240
|
-
reg.addTransformer('myTransformer1', async value => {
|
|
1241
|
-
order.push('myTransformer1');
|
|
1242
|
-
return String(value);
|
|
1243
|
-
});
|
|
1244
|
-
reg.addTransformer('myTransformer2', async value => {
|
|
1245
|
-
order.push('myTransformer2');
|
|
1246
|
-
return value.toUpperCase();
|
|
1247
|
-
});
|
|
1248
|
-
dbs.defineModel({
|
|
1249
|
-
name: 'model',
|
|
1250
|
-
properties: {
|
|
1251
|
-
foo: {
|
|
1252
|
-
type: DataType.ANY,
|
|
1253
|
-
transform: ['myTransformer1', 'myTransformer2'],
|
|
1254
|
-
},
|
|
1255
|
-
},
|
|
1256
|
-
});
|
|
1257
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1258
|
-
const res = await T.transform('model', {foo: true});
|
|
1259
|
-
expect(res).to.be.eql({foo: 'TRUE'});
|
|
1260
|
-
expect(order).to.be.eql(['myTransformer1', 'myTransformer2']);
|
|
1261
|
-
});
|
|
1262
|
-
});
|
|
1263
|
-
|
|
1264
|
-
describe('when an Array element is a Function', function () {
|
|
1265
|
-
describe('named transformers', function () {
|
|
1266
|
-
it('should not transform the non-provided property', function () {
|
|
1267
|
-
let calls = 0;
|
|
1268
|
-
const dbs = new DatabaseSchema();
|
|
1269
|
-
const myTransformer = function () {
|
|
1270
|
-
calls++;
|
|
1271
|
-
throw new Error('Should not to be called.');
|
|
1272
|
-
};
|
|
1273
|
-
dbs.defineModel({
|
|
1274
|
-
name: 'model',
|
|
1275
|
-
properties: {
|
|
1276
|
-
foo: {
|
|
1277
|
-
type: DataType.ANY,
|
|
1278
|
-
transform: [myTransformer],
|
|
1279
|
-
},
|
|
1280
|
-
},
|
|
1281
|
-
});
|
|
1282
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1283
|
-
const res = T.transform('model', {});
|
|
1284
|
-
expect(res).to.be.eql({});
|
|
1285
|
-
expect(calls).to.be.eq(0);
|
|
1286
|
-
});
|
|
1287
|
-
|
|
1288
|
-
it('should not transform non-provided properties when the option "isPartial" is true', function () {
|
|
1289
|
-
let calls = 0;
|
|
1290
|
-
const dbs = new DatabaseSchema();
|
|
1291
|
-
const myTransformer = function () {
|
|
1292
|
-
calls++;
|
|
1293
|
-
throw new Error('Should not to be called.');
|
|
1294
|
-
};
|
|
1295
|
-
dbs.defineModel({
|
|
1296
|
-
name: 'model',
|
|
1297
|
-
properties: {
|
|
1298
|
-
foo: {
|
|
1299
|
-
type: DataType.ANY,
|
|
1300
|
-
transform: [myTransformer],
|
|
1301
|
-
},
|
|
1302
|
-
bar: {
|
|
1303
|
-
type: DataType.ANY,
|
|
1304
|
-
transform: [myTransformer],
|
|
1305
|
-
},
|
|
1306
|
-
},
|
|
1307
|
-
});
|
|
1308
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1309
|
-
const res = T.transform('model', {}, true);
|
|
1310
|
-
expect(res).to.be.eql({});
|
|
1311
|
-
expect(calls).to.be.eq(0);
|
|
1312
|
-
});
|
|
1313
|
-
|
|
1314
|
-
it('should not transform undefined and null values', function () {
|
|
1315
|
-
let calls = 0;
|
|
1316
|
-
const dbs = new DatabaseSchema();
|
|
1317
|
-
const myTransformer = function () {
|
|
1318
|
-
calls++;
|
|
1319
|
-
throw new Error('Should not to be called.');
|
|
1320
|
-
};
|
|
1321
|
-
dbs.defineModel({
|
|
1322
|
-
name: 'model',
|
|
1323
|
-
properties: {
|
|
1324
|
-
foo: {
|
|
1325
|
-
type: DataType.ANY,
|
|
1326
|
-
transform: [myTransformer],
|
|
1327
|
-
},
|
|
1328
|
-
},
|
|
1329
|
-
});
|
|
1330
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1331
|
-
const res1 = T.transform('model', {foo: undefined});
|
|
1332
|
-
const res2 = T.transform('model', {foo: null});
|
|
1333
|
-
expect(res1).to.be.eql({foo: undefined});
|
|
1334
|
-
expect(res2).to.be.eql({foo: null});
|
|
1335
|
-
expect(calls).to.be.eq(0);
|
|
1336
|
-
});
|
|
1337
|
-
|
|
1338
|
-
it('should not transform the empty value', function () {
|
|
1339
|
-
let calls = 0;
|
|
1340
|
-
const dbs = new DatabaseSchema();
|
|
1341
|
-
const myTransformer = function () {
|
|
1342
|
-
calls++;
|
|
1343
|
-
throw new Error('Should not to be called.');
|
|
1344
|
-
};
|
|
1345
|
-
dbs.defineModel({
|
|
1346
|
-
name: 'model',
|
|
1347
|
-
properties: {
|
|
1348
|
-
foo: {
|
|
1349
|
-
type: DataType.ANY,
|
|
1350
|
-
transform: [myTransformer],
|
|
1351
|
-
},
|
|
1352
|
-
},
|
|
1353
|
-
});
|
|
1354
|
-
dbs
|
|
1355
|
-
.getService(EmptyValuesService)
|
|
1356
|
-
.setEmptyValuesOf(DataType.ANY, [10]);
|
|
1357
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1358
|
-
const res = T.transform('model', {foo: 10});
|
|
1359
|
-
expect(res).to.be.eql({foo: 10});
|
|
1360
|
-
expect(calls).to.be.eq(0);
|
|
1361
|
-
});
|
|
1362
|
-
|
|
1363
|
-
it('should transform the property by the transformer', function () {
|
|
1364
|
-
let calls = 0;
|
|
1365
|
-
const dbs = new DatabaseSchema();
|
|
1366
|
-
const myTransformer = function (value) {
|
|
1367
|
-
calls++;
|
|
1368
|
-
return String(value);
|
|
1369
|
-
};
|
|
1370
|
-
dbs.defineModel({
|
|
1371
|
-
name: 'model',
|
|
1372
|
-
properties: {
|
|
1373
|
-
foo: {
|
|
1374
|
-
type: DataType.ANY,
|
|
1375
|
-
transform: [myTransformer],
|
|
1376
|
-
},
|
|
1377
|
-
},
|
|
1378
|
-
});
|
|
1379
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1380
|
-
const res = T.transform('model', {foo: 10});
|
|
1381
|
-
expect(res).to.be.eql({foo: '10'});
|
|
1382
|
-
expect(calls).to.be.eq(1);
|
|
1383
|
-
});
|
|
1384
|
-
|
|
1385
|
-
it('should transform properties by transformers', function () {
|
|
1386
|
-
let calls = 0;
|
|
1387
|
-
const dbs = new DatabaseSchema();
|
|
1388
|
-
const myTransformer = function (value) {
|
|
1389
|
-
calls++;
|
|
1390
|
-
return String(value);
|
|
1391
|
-
};
|
|
1392
|
-
dbs.defineModel({
|
|
1393
|
-
name: 'model',
|
|
1394
|
-
properties: {
|
|
1395
|
-
foo: {
|
|
1396
|
-
type: DataType.ANY,
|
|
1397
|
-
transform: [myTransformer],
|
|
1398
|
-
},
|
|
1399
|
-
bar: {
|
|
1400
|
-
type: DataType.ANY,
|
|
1401
|
-
transform: [myTransformer],
|
|
1402
|
-
},
|
|
1403
|
-
},
|
|
1404
|
-
});
|
|
1405
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1406
|
-
const res = T.transform('model', {foo: 1, bar: 2});
|
|
1407
|
-
expect(res).to.be.eql({foo: '1', bar: '2'});
|
|
1408
|
-
expect(calls).to.be.eq(2);
|
|
1409
|
-
});
|
|
1410
|
-
|
|
1411
|
-
it('should pass arguments to the transformer', function () {
|
|
1412
|
-
let calls = 0;
|
|
1413
|
-
const dbs = new DatabaseSchema();
|
|
1414
|
-
const myTransformer = function (value, options, context) {
|
|
1415
|
-
calls++;
|
|
1416
|
-
expect(value).to.be.eq(10);
|
|
1417
|
-
expect(options).to.be.undefined;
|
|
1418
|
-
expect(context).to.be.eql({
|
|
1419
|
-
transformerName: 'myTransformer',
|
|
1420
|
-
modelName: 'model',
|
|
1421
|
-
propName: 'foo',
|
|
1422
|
-
});
|
|
1423
|
-
return String(value);
|
|
1424
|
-
};
|
|
1425
|
-
dbs.defineModel({
|
|
1426
|
-
name: 'model',
|
|
1427
|
-
properties: {
|
|
1428
|
-
foo: {
|
|
1429
|
-
type: DataType.ANY,
|
|
1430
|
-
transform: [myTransformer],
|
|
1431
|
-
},
|
|
1432
|
-
},
|
|
1433
|
-
});
|
|
1434
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1435
|
-
const res = T.transform('model', {foo: 10});
|
|
1436
|
-
expect(res).to.be.eql({foo: '10'});
|
|
1437
|
-
expect(calls).to.be.eq(1);
|
|
1438
|
-
});
|
|
1439
|
-
|
|
1440
|
-
it('should transform the property by the asynchronous transformer', async function () {
|
|
1441
|
-
let calls = 0;
|
|
1442
|
-
const dbs = new DatabaseSchema();
|
|
1443
|
-
const myTransformer = function (value) {
|
|
1444
|
-
calls++;
|
|
1445
|
-
return Promise.resolve(String(value));
|
|
1446
|
-
};
|
|
1447
|
-
dbs.defineModel({
|
|
1448
|
-
name: 'model',
|
|
1449
|
-
properties: {
|
|
1450
|
-
foo: {
|
|
1451
|
-
type: DataType.ANY,
|
|
1452
|
-
transform: [myTransformer],
|
|
1453
|
-
},
|
|
1454
|
-
},
|
|
1455
|
-
});
|
|
1456
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1457
|
-
const promise = T.transform('model', {foo: 10});
|
|
1458
|
-
expect(promise).to.be.instanceof(Promise);
|
|
1459
|
-
const res = await promise;
|
|
1460
|
-
expect(res).to.be.eql({foo: '10'});
|
|
1461
|
-
expect(calls).to.be.eq(1);
|
|
1462
|
-
});
|
|
1463
|
-
|
|
1464
|
-
it('should transform properties by the asynchronous transformer', async function () {
|
|
1465
|
-
let calls = 0;
|
|
1466
|
-
const dbs = new DatabaseSchema();
|
|
1467
|
-
const myTransformer = function (value) {
|
|
1468
|
-
calls++;
|
|
1469
|
-
return Promise.resolve(String(value));
|
|
1470
|
-
};
|
|
1471
|
-
dbs.defineModel({
|
|
1472
|
-
name: 'model',
|
|
1473
|
-
properties: {
|
|
1474
|
-
foo: {
|
|
1475
|
-
type: DataType.ANY,
|
|
1476
|
-
transform: [myTransformer],
|
|
1477
|
-
},
|
|
1478
|
-
bar: {
|
|
1479
|
-
type: DataType.ANY,
|
|
1480
|
-
transform: [myTransformer],
|
|
1481
|
-
},
|
|
1482
|
-
},
|
|
1483
|
-
});
|
|
1484
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1485
|
-
const promise = T.transform('model', {foo: 10, bar: 20});
|
|
1486
|
-
expect(promise).to.be.instanceof(Promise);
|
|
1487
|
-
const res = await promise;
|
|
1488
|
-
expect(res).to.be.eql({foo: '10', bar: '20'});
|
|
1489
|
-
expect(calls).to.be.eq(2);
|
|
1490
|
-
});
|
|
1491
|
-
|
|
1492
|
-
it('should throw an error from the transformer', async function () {
|
|
1493
|
-
let calls = 0;
|
|
1494
|
-
const dbs = new DatabaseSchema();
|
|
1495
|
-
const myTransformer = function () {
|
|
1496
|
-
calls++;
|
|
1497
|
-
throw new Error('My error');
|
|
1498
|
-
};
|
|
1499
|
-
dbs.defineModel({
|
|
1500
|
-
name: 'model',
|
|
1501
|
-
properties: {
|
|
1502
|
-
foo: {
|
|
1503
|
-
type: DataType.ANY,
|
|
1504
|
-
transform: [myTransformer],
|
|
1505
|
-
},
|
|
1506
|
-
},
|
|
1507
|
-
});
|
|
1508
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1509
|
-
const throwable = () => T.transform('model', {foo: 10});
|
|
1510
|
-
expect(throwable).to.throw('My error');
|
|
1511
|
-
expect(calls).to.be.eq(1);
|
|
1512
|
-
});
|
|
1513
|
-
|
|
1514
|
-
it('should throw an error from the asynchronous transformer', async function () {
|
|
1515
|
-
let calls = 0;
|
|
1516
|
-
const dbs = new DatabaseSchema();
|
|
1517
|
-
const myTransformer = async function () {
|
|
1518
|
-
calls++;
|
|
1519
|
-
throw new Error('My error');
|
|
1520
|
-
};
|
|
1521
|
-
dbs.defineModel({
|
|
1522
|
-
name: 'model',
|
|
1523
|
-
properties: {
|
|
1524
|
-
foo: {
|
|
1525
|
-
type: DataType.ANY,
|
|
1526
|
-
transform: [myTransformer],
|
|
1527
|
-
},
|
|
1528
|
-
},
|
|
1529
|
-
});
|
|
1530
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1531
|
-
const promise = T.transform('model', {foo: 10});
|
|
1532
|
-
await expect(promise).to.rejectedWith('My error');
|
|
1533
|
-
expect(calls).to.be.eq(1);
|
|
1534
|
-
});
|
|
1535
|
-
|
|
1536
|
-
it('should transform the property by transformers with the correct order', function () {
|
|
1537
|
-
const order = [];
|
|
1538
|
-
const dbs = new DatabaseSchema();
|
|
1539
|
-
const myTransformer1 = function (value) {
|
|
1540
|
-
order.push('myTransformer1');
|
|
1541
|
-
return String(value);
|
|
1542
|
-
};
|
|
1543
|
-
const myTransformer2 = function (value) {
|
|
1544
|
-
order.push('myTransformer2');
|
|
1545
|
-
return value.toUpperCase();
|
|
1546
|
-
};
|
|
1547
|
-
dbs.defineModel({
|
|
1548
|
-
name: 'model',
|
|
1549
|
-
properties: {
|
|
1550
|
-
foo: {
|
|
1551
|
-
type: DataType.ANY,
|
|
1552
|
-
transform: [myTransformer1, myTransformer2],
|
|
1553
|
-
},
|
|
1554
|
-
},
|
|
1555
|
-
});
|
|
1556
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1557
|
-
const res = T.transform('model', {foo: true});
|
|
1558
|
-
expect(res).to.be.eql({foo: 'TRUE'});
|
|
1559
|
-
expect(order).to.be.eql(['myTransformer1', 'myTransformer2']);
|
|
1560
|
-
});
|
|
1561
|
-
|
|
1562
|
-
it('should transform the property by asynchronous transformers with the correct order', async function () {
|
|
1563
|
-
const order = [];
|
|
1564
|
-
const dbs = new DatabaseSchema();
|
|
1565
|
-
const myTransformer1 = async function (value) {
|
|
1566
|
-
order.push('myTransformer1');
|
|
1567
|
-
return String(value);
|
|
1568
|
-
};
|
|
1569
|
-
const myTransformer2 = async function (value) {
|
|
1570
|
-
order.push('myTransformer2');
|
|
1571
|
-
return value.toUpperCase();
|
|
1572
|
-
};
|
|
1573
|
-
dbs.defineModel({
|
|
1574
|
-
name: 'model',
|
|
1575
|
-
properties: {
|
|
1576
|
-
foo: {
|
|
1577
|
-
type: DataType.ANY,
|
|
1578
|
-
transform: [myTransformer1, myTransformer2],
|
|
1579
|
-
},
|
|
1580
|
-
},
|
|
1581
|
-
});
|
|
1582
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1583
|
-
const res = await T.transform('model', {foo: true});
|
|
1584
|
-
expect(res).to.be.eql({foo: 'TRUE'});
|
|
1585
|
-
expect(order).to.be.eql(['myTransformer1', 'myTransformer2']);
|
|
1586
|
-
});
|
|
1587
|
-
});
|
|
1588
|
-
|
|
1589
|
-
describe('anonymous transformers', function () {
|
|
1590
|
-
it('should not transform the non-provided property', function () {
|
|
1591
|
-
let calls = 0;
|
|
1592
|
-
const dbs = new DatabaseSchema();
|
|
1593
|
-
dbs.defineModel({
|
|
1594
|
-
name: 'model',
|
|
1595
|
-
properties: {
|
|
1596
|
-
foo: {
|
|
1597
|
-
type: DataType.ANY,
|
|
1598
|
-
transform: [
|
|
1599
|
-
() => {
|
|
1600
|
-
calls++;
|
|
1601
|
-
throw new Error('Should not to be called.');
|
|
1602
|
-
},
|
|
1603
|
-
],
|
|
1604
|
-
},
|
|
1605
|
-
},
|
|
1606
|
-
});
|
|
1607
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1608
|
-
const res = T.transform('model', {});
|
|
1609
|
-
expect(res).to.be.eql({});
|
|
1610
|
-
expect(calls).to.be.eq(0);
|
|
1611
|
-
});
|
|
1612
|
-
|
|
1613
|
-
it('should not transform non-provided properties when the option "isPartial" is true', function () {
|
|
1614
|
-
let calls = 0;
|
|
1615
|
-
const dbs = new DatabaseSchema();
|
|
1616
|
-
dbs.defineModel({
|
|
1617
|
-
name: 'model',
|
|
1618
|
-
properties: {
|
|
1619
|
-
foo: {
|
|
1620
|
-
type: DataType.ANY,
|
|
1621
|
-
transform: [
|
|
1622
|
-
() => {
|
|
1623
|
-
calls++;
|
|
1624
|
-
throw new Error('Should not to be called.');
|
|
1625
|
-
},
|
|
1626
|
-
],
|
|
1627
|
-
},
|
|
1628
|
-
bar: {
|
|
1629
|
-
type: DataType.ANY,
|
|
1630
|
-
transform: [
|
|
1631
|
-
() => {
|
|
1632
|
-
calls++;
|
|
1633
|
-
throw new Error('Should not to be called.');
|
|
1634
|
-
},
|
|
1635
|
-
],
|
|
1636
|
-
},
|
|
1637
|
-
},
|
|
1638
|
-
});
|
|
1639
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1640
|
-
const res = T.transform('model', {}, true);
|
|
1641
|
-
expect(res).to.be.eql({});
|
|
1642
|
-
expect(calls).to.be.eq(0);
|
|
1643
|
-
});
|
|
1644
|
-
|
|
1645
|
-
it('should not transform undefined and null values', function () {
|
|
1646
|
-
let calls = 0;
|
|
1647
|
-
const dbs = new DatabaseSchema();
|
|
1648
|
-
dbs.defineModel({
|
|
1649
|
-
name: 'model',
|
|
1650
|
-
properties: {
|
|
1651
|
-
foo: {
|
|
1652
|
-
type: DataType.ANY,
|
|
1653
|
-
transform: [
|
|
1654
|
-
() => {
|
|
1655
|
-
calls++;
|
|
1656
|
-
throw new Error('Should not to be called.');
|
|
1657
|
-
},
|
|
1658
|
-
],
|
|
1659
|
-
},
|
|
1660
|
-
},
|
|
1661
|
-
});
|
|
1662
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1663
|
-
const res1 = T.transform('model', {foo: undefined});
|
|
1664
|
-
const res2 = T.transform('model', {foo: null});
|
|
1665
|
-
expect(res1).to.be.eql({foo: undefined});
|
|
1666
|
-
expect(res2).to.be.eql({foo: null});
|
|
1667
|
-
expect(calls).to.be.eq(0);
|
|
1668
|
-
});
|
|
1669
|
-
|
|
1670
|
-
it('should not transform the empty value', function () {
|
|
1671
|
-
let calls = 0;
|
|
1672
|
-
const dbs = new DatabaseSchema();
|
|
1673
|
-
dbs.defineModel({
|
|
1674
|
-
name: 'model',
|
|
1675
|
-
properties: {
|
|
1676
|
-
foo: {
|
|
1677
|
-
type: DataType.ANY,
|
|
1678
|
-
transform: [
|
|
1679
|
-
() => {
|
|
1680
|
-
calls++;
|
|
1681
|
-
throw new Error('Should not to be called.');
|
|
1682
|
-
},
|
|
1683
|
-
],
|
|
1684
|
-
},
|
|
1685
|
-
},
|
|
1686
|
-
});
|
|
1687
|
-
dbs
|
|
1688
|
-
.getService(EmptyValuesService)
|
|
1689
|
-
.setEmptyValuesOf(DataType.ANY, [10]);
|
|
1690
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1691
|
-
const res = T.transform('model', {foo: 10});
|
|
1692
|
-
expect(res).to.be.eql({foo: 10});
|
|
1693
|
-
expect(calls).to.be.eq(0);
|
|
1694
|
-
});
|
|
1695
|
-
|
|
1696
|
-
it('should transform the property by the transformer', function () {
|
|
1697
|
-
let calls = 0;
|
|
1698
|
-
const dbs = new DatabaseSchema();
|
|
1699
|
-
dbs.defineModel({
|
|
1700
|
-
name: 'model',
|
|
1701
|
-
properties: {
|
|
1702
|
-
foo: {
|
|
1703
|
-
type: DataType.ANY,
|
|
1704
|
-
transform: [
|
|
1705
|
-
value => {
|
|
1706
|
-
calls++;
|
|
1707
|
-
return String(value);
|
|
1708
|
-
},
|
|
1709
|
-
],
|
|
1710
|
-
},
|
|
1711
|
-
},
|
|
1712
|
-
});
|
|
1713
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1714
|
-
const res = T.transform('model', {foo: 10});
|
|
1715
|
-
expect(res).to.be.eql({foo: '10'});
|
|
1716
|
-
expect(calls).to.be.eq(1);
|
|
1717
|
-
});
|
|
1718
|
-
|
|
1719
|
-
it('should transform properties by transformers', function () {
|
|
1720
|
-
let calls = 0;
|
|
1721
|
-
const dbs = new DatabaseSchema();
|
|
1722
|
-
dbs.defineModel({
|
|
1723
|
-
name: 'model',
|
|
1724
|
-
properties: {
|
|
1725
|
-
foo: {
|
|
1726
|
-
type: DataType.ANY,
|
|
1727
|
-
transform: [
|
|
1728
|
-
value => {
|
|
1729
|
-
calls++;
|
|
1730
|
-
return String(value);
|
|
1731
|
-
},
|
|
1732
|
-
],
|
|
1733
|
-
},
|
|
1734
|
-
bar: {
|
|
1735
|
-
type: DataType.ANY,
|
|
1736
|
-
transform: [
|
|
1737
|
-
value => {
|
|
1738
|
-
calls++;
|
|
1739
|
-
return String(value);
|
|
1740
|
-
},
|
|
1741
|
-
],
|
|
1742
|
-
},
|
|
1743
|
-
},
|
|
1744
|
-
});
|
|
1745
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1746
|
-
const res = T.transform('model', {foo: 1, bar: 2});
|
|
1747
|
-
expect(res).to.be.eql({foo: '1', bar: '2'});
|
|
1748
|
-
expect(calls).to.be.eq(2);
|
|
1749
|
-
});
|
|
1750
|
-
|
|
1751
|
-
it('should pass arguments to the transformer', function () {
|
|
1752
|
-
let calls = 0;
|
|
1753
|
-
const dbs = new DatabaseSchema();
|
|
1754
|
-
dbs.defineModel({
|
|
1755
|
-
name: 'model',
|
|
1756
|
-
properties: {
|
|
1757
|
-
foo: {
|
|
1758
|
-
type: DataType.ANY,
|
|
1759
|
-
transform: [
|
|
1760
|
-
(value, options, context) => {
|
|
1761
|
-
calls++;
|
|
1762
|
-
expect(value).to.be.eq(10);
|
|
1763
|
-
expect(options).to.be.undefined;
|
|
1764
|
-
expect(context).to.be.eql({
|
|
1765
|
-
transformerName: undefined,
|
|
1766
|
-
modelName: 'model',
|
|
1767
|
-
propName: 'foo',
|
|
1768
|
-
});
|
|
1769
|
-
return String(value);
|
|
1770
|
-
},
|
|
1771
|
-
],
|
|
1772
|
-
},
|
|
1773
|
-
},
|
|
1774
|
-
});
|
|
1775
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1776
|
-
const res = T.transform('model', {foo: 10});
|
|
1777
|
-
expect(res).to.be.eql({foo: '10'});
|
|
1778
|
-
expect(calls).to.be.eq(1);
|
|
1779
|
-
});
|
|
1780
|
-
|
|
1781
|
-
it('should transform the property by the asynchronous transformer', async function () {
|
|
1782
|
-
let calls = 0;
|
|
1783
|
-
const dbs = new DatabaseSchema();
|
|
1784
|
-
dbs.defineModel({
|
|
1785
|
-
name: 'model',
|
|
1786
|
-
properties: {
|
|
1787
|
-
foo: {
|
|
1788
|
-
type: DataType.ANY,
|
|
1789
|
-
transform: [
|
|
1790
|
-
value => {
|
|
1791
|
-
calls++;
|
|
1792
|
-
return Promise.resolve(String(value));
|
|
1793
|
-
},
|
|
1794
|
-
],
|
|
1795
|
-
},
|
|
1796
|
-
},
|
|
1797
|
-
});
|
|
1798
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1799
|
-
const promise = T.transform('model', {foo: 10});
|
|
1800
|
-
expect(promise).to.be.instanceof(Promise);
|
|
1801
|
-
const res = await promise;
|
|
1802
|
-
expect(res).to.be.eql({foo: '10'});
|
|
1803
|
-
expect(calls).to.be.eq(1);
|
|
1804
|
-
});
|
|
1805
|
-
|
|
1806
|
-
it('should transform properties by the asynchronous transformer', async function () {
|
|
1807
|
-
let calls = 0;
|
|
1808
|
-
const dbs = new DatabaseSchema();
|
|
1809
|
-
dbs.defineModel({
|
|
1810
|
-
name: 'model',
|
|
1811
|
-
properties: {
|
|
1812
|
-
foo: {
|
|
1813
|
-
type: DataType.ANY,
|
|
1814
|
-
transform: [
|
|
1815
|
-
value => {
|
|
1816
|
-
calls++;
|
|
1817
|
-
return Promise.resolve(String(value));
|
|
1818
|
-
},
|
|
1819
|
-
],
|
|
1820
|
-
},
|
|
1821
|
-
bar: {
|
|
1822
|
-
type: DataType.ANY,
|
|
1823
|
-
transform: [
|
|
1824
|
-
value => {
|
|
1825
|
-
calls++;
|
|
1826
|
-
return Promise.resolve(String(value));
|
|
1827
|
-
},
|
|
1828
|
-
],
|
|
1829
|
-
},
|
|
1830
|
-
},
|
|
1831
|
-
});
|
|
1832
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1833
|
-
const promise = T.transform('model', {foo: 10, bar: 20});
|
|
1834
|
-
expect(promise).to.be.instanceof(Promise);
|
|
1835
|
-
const res = await promise;
|
|
1836
|
-
expect(res).to.be.eql({foo: '10', bar: '20'});
|
|
1837
|
-
expect(calls).to.be.eq(2);
|
|
1838
|
-
});
|
|
1839
|
-
|
|
1840
|
-
it('should throw an error from the transformer', async function () {
|
|
1841
|
-
let calls = 0;
|
|
1842
|
-
const dbs = new DatabaseSchema();
|
|
1843
|
-
dbs.defineModel({
|
|
1844
|
-
name: 'model',
|
|
1845
|
-
properties: {
|
|
1846
|
-
foo: {
|
|
1847
|
-
type: DataType.ANY,
|
|
1848
|
-
transform: [
|
|
1849
|
-
() => {
|
|
1850
|
-
calls++;
|
|
1851
|
-
throw new Error('My error');
|
|
1852
|
-
},
|
|
1853
|
-
],
|
|
1854
|
-
},
|
|
1855
|
-
},
|
|
1856
|
-
});
|
|
1857
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1858
|
-
const throwable = () => T.transform('model', {foo: 10});
|
|
1859
|
-
expect(throwable).to.throw('My error');
|
|
1860
|
-
expect(calls).to.be.eq(1);
|
|
1861
|
-
});
|
|
1862
|
-
|
|
1863
|
-
it('should throw an error from the asynchronous transformer', async function () {
|
|
1864
|
-
let calls = 0;
|
|
1865
|
-
const dbs = new DatabaseSchema();
|
|
1866
|
-
dbs.defineModel({
|
|
1867
|
-
name: 'model',
|
|
1868
|
-
properties: {
|
|
1869
|
-
foo: {
|
|
1870
|
-
type: DataType.ANY,
|
|
1871
|
-
transform: [
|
|
1872
|
-
async () => {
|
|
1873
|
-
calls++;
|
|
1874
|
-
throw new Error('My error');
|
|
1875
|
-
},
|
|
1876
|
-
],
|
|
1877
|
-
},
|
|
1878
|
-
},
|
|
1879
|
-
});
|
|
1880
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1881
|
-
const promise = T.transform('model', {foo: 10});
|
|
1882
|
-
await expect(promise).to.rejectedWith('My error');
|
|
1883
|
-
expect(calls).to.be.eq(1);
|
|
1884
|
-
});
|
|
1885
|
-
|
|
1886
|
-
it('should transform the property by transformers with the correct order', function () {
|
|
1887
|
-
const order = [];
|
|
1888
|
-
const dbs = new DatabaseSchema();
|
|
1889
|
-
dbs.defineModel({
|
|
1890
|
-
name: 'model',
|
|
1891
|
-
properties: {
|
|
1892
|
-
foo: {
|
|
1893
|
-
type: DataType.ANY,
|
|
1894
|
-
transform: [
|
|
1895
|
-
value => {
|
|
1896
|
-
order.push('myTransformer1');
|
|
1897
|
-
return String(value);
|
|
1898
|
-
},
|
|
1899
|
-
value => {
|
|
1900
|
-
order.push('myTransformer2');
|
|
1901
|
-
return value.toUpperCase();
|
|
1902
|
-
},
|
|
1903
|
-
],
|
|
1904
|
-
},
|
|
1905
|
-
},
|
|
1906
|
-
});
|
|
1907
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1908
|
-
const res = T.transform('model', {foo: true});
|
|
1909
|
-
expect(res).to.be.eql({foo: 'TRUE'});
|
|
1910
|
-
expect(order).to.be.eql(['myTransformer1', 'myTransformer2']);
|
|
1911
|
-
});
|
|
1912
|
-
|
|
1913
|
-
it('should transform the property by asynchronous transformers with the correct order', async function () {
|
|
1914
|
-
const order = [];
|
|
1915
|
-
const dbs = new DatabaseSchema();
|
|
1916
|
-
dbs.defineModel({
|
|
1917
|
-
name: 'model',
|
|
1918
|
-
properties: {
|
|
1919
|
-
foo: {
|
|
1920
|
-
type: DataType.ANY,
|
|
1921
|
-
transform: [
|
|
1922
|
-
async value => {
|
|
1923
|
-
order.push('myTransformer1');
|
|
1924
|
-
return String(value);
|
|
1925
|
-
},
|
|
1926
|
-
async value => {
|
|
1927
|
-
order.push('myTransformer2');
|
|
1928
|
-
return value.toUpperCase();
|
|
1929
|
-
},
|
|
1930
|
-
],
|
|
1931
|
-
},
|
|
1932
|
-
},
|
|
1933
|
-
});
|
|
1934
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1935
|
-
const res = await T.transform('model', {foo: true});
|
|
1936
|
-
expect(res).to.be.eql({foo: 'TRUE'});
|
|
1937
|
-
expect(order).to.be.eql(['myTransformer1', 'myTransformer2']);
|
|
1938
|
-
});
|
|
1939
|
-
});
|
|
1940
|
-
});
|
|
1941
|
-
});
|
|
1942
|
-
|
|
1943
|
-
describe('when the option "transform" is an Object', function () {
|
|
1944
|
-
it('should not transform the non-provided property', function () {
|
|
1945
|
-
let calls = 0;
|
|
1946
|
-
const dbs = new DatabaseSchema();
|
|
1947
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1948
|
-
reg.addTransformer('myTransformer', () => {
|
|
1949
|
-
calls++;
|
|
1950
|
-
throw new Error('Should not to be called.');
|
|
1951
|
-
});
|
|
1952
|
-
dbs.defineModel({
|
|
1953
|
-
name: 'model',
|
|
1954
|
-
properties: {
|
|
1955
|
-
foo: {
|
|
1956
|
-
type: DataType.ANY,
|
|
1957
|
-
transform: {
|
|
1958
|
-
myTransformer: true,
|
|
1959
|
-
},
|
|
1960
|
-
},
|
|
1961
|
-
},
|
|
1962
|
-
});
|
|
1963
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1964
|
-
const res = T.transform('model', {});
|
|
1965
|
-
expect(res).to.be.eql({});
|
|
1966
|
-
expect(calls).to.be.eq(0);
|
|
1967
|
-
});
|
|
1968
|
-
|
|
1969
|
-
it('should not transform non-provided properties when the option "isPartial" is true', function () {
|
|
1970
|
-
let calls = 0;
|
|
1971
|
-
const dbs = new DatabaseSchema();
|
|
1972
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
1973
|
-
reg.addTransformer('myTransformer', () => {
|
|
1974
|
-
calls++;
|
|
1975
|
-
throw new Error('Should not to be called.');
|
|
1976
|
-
});
|
|
1977
|
-
dbs.defineModel({
|
|
1978
|
-
name: 'model',
|
|
1979
|
-
properties: {
|
|
1980
|
-
foo: {
|
|
1981
|
-
type: DataType.ANY,
|
|
1982
|
-
transform: {
|
|
1983
|
-
myTransformer: true,
|
|
1984
|
-
},
|
|
1985
|
-
},
|
|
1986
|
-
bar: {
|
|
1987
|
-
type: DataType.ANY,
|
|
1988
|
-
transform: {
|
|
1989
|
-
myTransformer: true,
|
|
1990
|
-
},
|
|
1991
|
-
},
|
|
1992
|
-
},
|
|
1993
|
-
});
|
|
1994
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
1995
|
-
const res = T.transform('model', {}, true);
|
|
1996
|
-
expect(res).to.be.eql({});
|
|
1997
|
-
expect(calls).to.be.eq(0);
|
|
1998
|
-
});
|
|
1999
|
-
|
|
2000
|
-
it('should not transform undefined and null values', function () {
|
|
2001
|
-
let calls = 0;
|
|
2002
|
-
const dbs = new DatabaseSchema();
|
|
2003
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2004
|
-
reg.addTransformer('myTransformer', () => {
|
|
2005
|
-
calls++;
|
|
2006
|
-
throw new Error('Should not to be called.');
|
|
2007
|
-
});
|
|
2008
|
-
dbs.defineModel({
|
|
2009
|
-
name: 'model',
|
|
2010
|
-
properties: {
|
|
2011
|
-
foo: {
|
|
2012
|
-
type: DataType.ANY,
|
|
2013
|
-
transform: {
|
|
2014
|
-
myTransformer: true,
|
|
2015
|
-
},
|
|
2016
|
-
},
|
|
2017
|
-
},
|
|
2018
|
-
});
|
|
2019
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2020
|
-
const res1 = T.transform('model', {foo: undefined});
|
|
2021
|
-
const res2 = T.transform('model', {foo: null});
|
|
2022
|
-
expect(res1).to.be.eql({foo: undefined});
|
|
2023
|
-
expect(res2).to.be.eql({foo: null});
|
|
2024
|
-
expect(calls).to.be.eq(0);
|
|
2025
|
-
});
|
|
2026
|
-
|
|
2027
|
-
it('should not transform the empty value', function () {
|
|
2028
|
-
let calls = 0;
|
|
2029
|
-
const dbs = new DatabaseSchema();
|
|
2030
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2031
|
-
reg.addTransformer('myTransformer', () => {
|
|
2032
|
-
calls++;
|
|
2033
|
-
throw new Error('Should not to be called.');
|
|
2034
|
-
});
|
|
2035
|
-
dbs.defineModel({
|
|
2036
|
-
name: 'model',
|
|
2037
|
-
properties: {
|
|
2038
|
-
foo: {
|
|
2039
|
-
type: DataType.ANY,
|
|
2040
|
-
transform: {
|
|
2041
|
-
myTransformer: true,
|
|
2042
|
-
},
|
|
2043
|
-
},
|
|
2044
|
-
},
|
|
2045
|
-
});
|
|
2046
|
-
dbs.getService(EmptyValuesService).setEmptyValuesOf(DataType.ANY, [10]);
|
|
2047
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2048
|
-
const res = T.transform('model', {foo: 10});
|
|
2049
|
-
expect(res).to.be.eql({foo: 10});
|
|
2050
|
-
expect(calls).to.be.eq(0);
|
|
2051
|
-
});
|
|
2052
|
-
|
|
2053
|
-
it('should transform the property by the transformer', function () {
|
|
2054
|
-
let calls = 0;
|
|
2055
|
-
const dbs = new DatabaseSchema();
|
|
2056
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2057
|
-
reg.addTransformer('myTransformer', value => {
|
|
2058
|
-
calls++;
|
|
2059
|
-
return String(value);
|
|
2060
|
-
});
|
|
2061
|
-
dbs.defineModel({
|
|
2062
|
-
name: 'model',
|
|
2063
|
-
properties: {
|
|
2064
|
-
foo: {
|
|
2065
|
-
type: DataType.ANY,
|
|
2066
|
-
transform: {
|
|
2067
|
-
myTransformer: true,
|
|
2068
|
-
},
|
|
2069
|
-
},
|
|
2070
|
-
},
|
|
2071
|
-
});
|
|
2072
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2073
|
-
const res = T.transform('model', {foo: 10});
|
|
2074
|
-
expect(res).to.be.eql({foo: '10'});
|
|
2075
|
-
expect(calls).to.be.eq(1);
|
|
2076
|
-
});
|
|
2077
|
-
|
|
2078
|
-
it('should transform properties by transformers', function () {
|
|
2079
|
-
let calls = 0;
|
|
2080
|
-
const dbs = new DatabaseSchema();
|
|
2081
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2082
|
-
reg.addTransformer('myTransformer', value => {
|
|
2083
|
-
calls++;
|
|
2084
|
-
return String(value);
|
|
2085
|
-
});
|
|
2086
|
-
dbs.defineModel({
|
|
2087
|
-
name: 'model',
|
|
2088
|
-
properties: {
|
|
2089
|
-
foo: {
|
|
2090
|
-
type: DataType.ANY,
|
|
2091
|
-
transform: {
|
|
2092
|
-
myTransformer: true,
|
|
2093
|
-
},
|
|
2094
|
-
},
|
|
2095
|
-
bar: {
|
|
2096
|
-
type: DataType.ANY,
|
|
2097
|
-
transform: {
|
|
2098
|
-
myTransformer: true,
|
|
2099
|
-
},
|
|
2100
|
-
},
|
|
2101
|
-
},
|
|
2102
|
-
});
|
|
2103
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2104
|
-
const res = T.transform('model', {foo: 1, bar: 2});
|
|
2105
|
-
expect(res).to.be.eql({foo: '1', bar: '2'});
|
|
2106
|
-
expect(calls).to.be.eq(2);
|
|
2107
|
-
});
|
|
2108
|
-
|
|
2109
|
-
it('should pass arguments to the transformer', function () {
|
|
2110
|
-
let calls = 0;
|
|
2111
|
-
const dbs = new DatabaseSchema();
|
|
2112
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2113
|
-
reg.addTransformer('myTransformer', (value, options, context) => {
|
|
2114
|
-
calls++;
|
|
2115
|
-
expect(value).to.be.eq(10);
|
|
2116
|
-
expect(options).to.be.eq('test');
|
|
2117
|
-
expect(context).to.be.eql({
|
|
2118
|
-
transformerName: 'myTransformer',
|
|
2119
|
-
modelName: 'model',
|
|
2120
|
-
propName: 'foo',
|
|
2121
|
-
});
|
|
2122
|
-
return String(value);
|
|
2123
|
-
});
|
|
2124
|
-
dbs.defineModel({
|
|
2125
|
-
name: 'model',
|
|
2126
|
-
properties: {
|
|
2127
|
-
foo: {
|
|
2128
|
-
type: DataType.ANY,
|
|
2129
|
-
transform: {
|
|
2130
|
-
myTransformer: 'test',
|
|
2131
|
-
},
|
|
2132
|
-
},
|
|
2133
|
-
},
|
|
2134
|
-
});
|
|
2135
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2136
|
-
const res = T.transform('model', {foo: 10});
|
|
2137
|
-
expect(res).to.be.eql({foo: '10'});
|
|
2138
|
-
expect(calls).to.be.eq(1);
|
|
2139
|
-
});
|
|
2140
|
-
|
|
2141
|
-
it('should transform the property by the asynchronous transformer', async function () {
|
|
2142
|
-
let calls = 0;
|
|
2143
|
-
const dbs = new DatabaseSchema();
|
|
2144
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2145
|
-
reg.addTransformer('myTransformer', value => {
|
|
2146
|
-
calls++;
|
|
2147
|
-
return Promise.resolve(String(value));
|
|
2148
|
-
});
|
|
2149
|
-
dbs.defineModel({
|
|
2150
|
-
name: 'model',
|
|
2151
|
-
properties: {
|
|
2152
|
-
foo: {
|
|
2153
|
-
type: DataType.ANY,
|
|
2154
|
-
transform: {
|
|
2155
|
-
myTransformer: true,
|
|
2156
|
-
},
|
|
2157
|
-
},
|
|
2158
|
-
},
|
|
2159
|
-
});
|
|
2160
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2161
|
-
const promise = T.transform('model', {foo: 10});
|
|
2162
|
-
expect(promise).to.be.instanceof(Promise);
|
|
2163
|
-
const res = await promise;
|
|
2164
|
-
expect(res).to.be.eql({foo: '10'});
|
|
2165
|
-
expect(calls).to.be.eq(1);
|
|
2166
|
-
});
|
|
2167
|
-
|
|
2168
|
-
it('should transform properties by the asynchronous transformer', async function () {
|
|
2169
|
-
let calls = 0;
|
|
2170
|
-
const dbs = new DatabaseSchema();
|
|
2171
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2172
|
-
reg.addTransformer('myTransformer', value => {
|
|
2173
|
-
calls++;
|
|
2174
|
-
return Promise.resolve(String(value));
|
|
2175
|
-
});
|
|
2176
|
-
dbs.defineModel({
|
|
2177
|
-
name: 'model',
|
|
2178
|
-
properties: {
|
|
2179
|
-
foo: {
|
|
2180
|
-
type: DataType.ANY,
|
|
2181
|
-
transform: {
|
|
2182
|
-
myTransformer: true,
|
|
2183
|
-
},
|
|
2184
|
-
},
|
|
2185
|
-
bar: {
|
|
2186
|
-
type: DataType.ANY,
|
|
2187
|
-
transform: {
|
|
2188
|
-
myTransformer: true,
|
|
2189
|
-
},
|
|
2190
|
-
},
|
|
2191
|
-
},
|
|
2192
|
-
});
|
|
2193
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2194
|
-
const promise = T.transform('model', {foo: 10, bar: 20});
|
|
2195
|
-
expect(promise).to.be.instanceof(Promise);
|
|
2196
|
-
const res = await promise;
|
|
2197
|
-
expect(res).to.be.eql({foo: '10', bar: '20'});
|
|
2198
|
-
expect(calls).to.be.eq(2);
|
|
2199
|
-
});
|
|
2200
|
-
|
|
2201
|
-
it('should throw an error from the transformer', async function () {
|
|
2202
|
-
let calls = 0;
|
|
2203
|
-
const dbs = new DatabaseSchema();
|
|
2204
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2205
|
-
reg.addTransformer('myTransformer', () => {
|
|
2206
|
-
calls++;
|
|
2207
|
-
throw new Error('My error');
|
|
2208
|
-
});
|
|
2209
|
-
dbs.defineModel({
|
|
2210
|
-
name: 'model',
|
|
2211
|
-
properties: {
|
|
2212
|
-
foo: {
|
|
2213
|
-
type: DataType.ANY,
|
|
2214
|
-
transform: {
|
|
2215
|
-
myTransformer: true,
|
|
2216
|
-
},
|
|
2217
|
-
},
|
|
2218
|
-
},
|
|
2219
|
-
});
|
|
2220
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2221
|
-
const throwable = () => T.transform('model', {foo: 10});
|
|
2222
|
-
expect(throwable).to.throw('My error');
|
|
2223
|
-
expect(calls).to.be.eq(1);
|
|
2224
|
-
});
|
|
2225
|
-
|
|
2226
|
-
it('should throw an error from the asynchronous transformer', async function () {
|
|
2227
|
-
let calls = 0;
|
|
2228
|
-
const dbs = new DatabaseSchema();
|
|
2229
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2230
|
-
reg.addTransformer('myTransformer', async () => {
|
|
2231
|
-
calls++;
|
|
2232
|
-
throw new Error('My error');
|
|
2233
|
-
});
|
|
2234
|
-
dbs.defineModel({
|
|
2235
|
-
name: 'model',
|
|
2236
|
-
properties: {
|
|
2237
|
-
foo: {
|
|
2238
|
-
type: DataType.ANY,
|
|
2239
|
-
transform: {
|
|
2240
|
-
myTransformer: true,
|
|
2241
|
-
},
|
|
2242
|
-
},
|
|
2243
|
-
},
|
|
2244
|
-
});
|
|
2245
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2246
|
-
const promise = T.transform('model', {foo: 10});
|
|
2247
|
-
await expect(promise).to.rejectedWith('My error');
|
|
2248
|
-
expect(calls).to.be.eq(1);
|
|
2249
|
-
});
|
|
2250
|
-
|
|
2251
|
-
it('should transform the property by transformers with the correct order', function () {
|
|
2252
|
-
const order = [];
|
|
2253
|
-
const dbs = new DatabaseSchema();
|
|
2254
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2255
|
-
reg.addTransformer('myTransformer1', value => {
|
|
2256
|
-
order.push('myTransformer1');
|
|
2257
|
-
return String(value);
|
|
2258
|
-
});
|
|
2259
|
-
reg.addTransformer('myTransformer2', value => {
|
|
2260
|
-
order.push('myTransformer2');
|
|
2261
|
-
return value.toUpperCase();
|
|
2262
|
-
});
|
|
2263
|
-
dbs.defineModel({
|
|
2264
|
-
name: 'model',
|
|
2265
|
-
properties: {
|
|
2266
|
-
foo: {
|
|
2267
|
-
type: DataType.ANY,
|
|
2268
|
-
transform: {
|
|
2269
|
-
myTransformer1: true,
|
|
2270
|
-
myTransformer2: true,
|
|
2271
|
-
},
|
|
2272
|
-
},
|
|
2273
|
-
},
|
|
2274
|
-
});
|
|
2275
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2276
|
-
const res = T.transform('model', {foo: true});
|
|
2277
|
-
expect(res).to.be.eql({foo: 'TRUE'});
|
|
2278
|
-
expect(order).to.be.eql(['myTransformer1', 'myTransformer2']);
|
|
2279
|
-
});
|
|
2280
|
-
|
|
2281
|
-
it('should transform the property by asynchronous transformers with the correct order', async function () {
|
|
2282
|
-
const order = [];
|
|
2283
|
-
const dbs = new DatabaseSchema();
|
|
2284
|
-
const reg = dbs.getService(PropertyTransformerRegistry);
|
|
2285
|
-
reg.addTransformer('myTransformer1', async value => {
|
|
2286
|
-
order.push('myTransformer1');
|
|
2287
|
-
return String(value);
|
|
2288
|
-
});
|
|
2289
|
-
reg.addTransformer('myTransformer2', async value => {
|
|
2290
|
-
order.push('myTransformer2');
|
|
2291
|
-
return value.toUpperCase();
|
|
2292
|
-
});
|
|
2293
|
-
dbs.defineModel({
|
|
2294
|
-
name: 'model',
|
|
2295
|
-
properties: {
|
|
2296
|
-
foo: {
|
|
2297
|
-
type: DataType.ANY,
|
|
2298
|
-
transform: {
|
|
2299
|
-
myTransformer1: true,
|
|
2300
|
-
myTransformer2: true,
|
|
2301
|
-
},
|
|
2302
|
-
},
|
|
2303
|
-
},
|
|
2304
|
-
});
|
|
2305
|
-
const T = dbs.getService(ModelDataTransformer);
|
|
2306
|
-
const res = await T.transform('model', {foo: true});
|
|
2307
|
-
expect(res).to.be.eql({foo: 'TRUE'});
|
|
2308
|
-
expect(order).to.be.eql(['myTransformer1', 'myTransformer2']);
|
|
2309
|
-
});
|
|
2310
|
-
});
|
|
2311
|
-
});
|
|
2312
|
-
});
|