@opra/sqb 1.0.0-alpha.2 → 1.0.0-alpha.4
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/cjs/sqb-entity-service.js +35 -33
- package/esm/sqb-entity-service.js +35 -33
- package/package.json +4 -4
- package/types/sqb-entity-service.d.ts +6 -6
|
@@ -19,7 +19,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
19
19
|
*/
|
|
20
20
|
constructor(dataType, options) {
|
|
21
21
|
super();
|
|
22
|
-
this.
|
|
22
|
+
this._inputCodecs = {};
|
|
23
|
+
this._outputCodecs = {};
|
|
23
24
|
this._dataType_ = dataType;
|
|
24
25
|
this.db = options?.db;
|
|
25
26
|
this.$resourceName = options?.resourceName;
|
|
@@ -74,34 +75,34 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
74
75
|
throw new Error('resourceName is not defined');
|
|
75
76
|
}
|
|
76
77
|
/**
|
|
77
|
-
* Retrieves the
|
|
78
|
+
* Retrieves the codec for the specified operation.
|
|
78
79
|
*
|
|
79
80
|
* @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
|
|
80
81
|
*/
|
|
81
|
-
|
|
82
|
-
let
|
|
83
|
-
if (
|
|
84
|
-
return
|
|
85
|
-
const options = { projection: '*' };
|
|
82
|
+
getInputCodec(operation) {
|
|
83
|
+
let validator = this._inputCodecs[operation];
|
|
84
|
+
if (validator)
|
|
85
|
+
return validator;
|
|
86
|
+
const options = { projection: '*', ignoreReadonlyFields: true };
|
|
86
87
|
if (operation === 'update')
|
|
87
88
|
options.partial = 'deep';
|
|
88
89
|
const dataType = this.dataType;
|
|
89
|
-
|
|
90
|
-
this.
|
|
91
|
-
return
|
|
90
|
+
validator = dataType.generateCodec('decode', options);
|
|
91
|
+
this._inputCodecs[operation] = validator;
|
|
92
|
+
return validator;
|
|
92
93
|
}
|
|
93
94
|
/**
|
|
94
|
-
* Retrieves the
|
|
95
|
+
* Retrieves the codec.
|
|
95
96
|
*/
|
|
96
|
-
|
|
97
|
-
let
|
|
98
|
-
if (
|
|
99
|
-
return
|
|
100
|
-
const options = { projection: '*', partial: 'deep' };
|
|
97
|
+
getOutputCodec(operation) {
|
|
98
|
+
let validator = this._outputCodecs[operation];
|
|
99
|
+
if (validator)
|
|
100
|
+
return validator;
|
|
101
|
+
const options = { projection: '*', partial: 'deep', ignoreWriteonlyFields: true };
|
|
101
102
|
const dataType = this.dataType;
|
|
102
|
-
|
|
103
|
-
this.
|
|
104
|
-
return
|
|
103
|
+
validator = dataType.generateCodec('decode', options);
|
|
104
|
+
this._outputCodecs[operation] = validator;
|
|
105
|
+
return validator;
|
|
105
106
|
}
|
|
106
107
|
/**
|
|
107
108
|
* Insert a new record into database
|
|
@@ -113,11 +114,12 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
113
114
|
* @protected
|
|
114
115
|
*/
|
|
115
116
|
async _create(input, options) {
|
|
116
|
-
const
|
|
117
|
-
const
|
|
117
|
+
const inputCodec = this.getInputCodec('create');
|
|
118
|
+
const outputCodec = this.getOutputCodec('create');
|
|
119
|
+
const data = inputCodec(input);
|
|
118
120
|
const out = await this._dbCreate(data, options);
|
|
119
121
|
if (out)
|
|
120
|
-
return out;
|
|
122
|
+
return outputCodec(out);
|
|
121
123
|
throw new common_1.InternalServerError(`Unknown error while creating document for "${this.getResourceName()}"`);
|
|
122
124
|
}
|
|
123
125
|
/**
|
|
@@ -181,7 +183,7 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
181
183
|
* @protected
|
|
182
184
|
*/
|
|
183
185
|
async _findById(id, options) {
|
|
184
|
-
const decode = this.
|
|
186
|
+
const decode = this.getOutputCodec('find');
|
|
185
187
|
const out = await this._dbFindById(id, options);
|
|
186
188
|
return out ? decode(out) : undefined;
|
|
187
189
|
}
|
|
@@ -193,7 +195,7 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
193
195
|
* @protected
|
|
194
196
|
*/
|
|
195
197
|
async _findOne(options) {
|
|
196
|
-
const decode = this.
|
|
198
|
+
const decode = this.getOutputCodec('find');
|
|
197
199
|
const out = await this._dbFindOne(options);
|
|
198
200
|
return out ? decode(out) : undefined;
|
|
199
201
|
}
|
|
@@ -205,7 +207,7 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
205
207
|
* @protected
|
|
206
208
|
*/
|
|
207
209
|
async _findMany(options) {
|
|
208
|
-
const decode = this.
|
|
210
|
+
const decode = this.getOutputCodec('find');
|
|
209
211
|
const out = await this._dbFindMany(options);
|
|
210
212
|
if (out?.length) {
|
|
211
213
|
return out.map(x => decode(x));
|
|
@@ -223,12 +225,12 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
223
225
|
* @protected
|
|
224
226
|
*/
|
|
225
227
|
async _update(id, input, options) {
|
|
226
|
-
const
|
|
227
|
-
const
|
|
228
|
-
const data = encode(input);
|
|
228
|
+
const inputCodec = this.getInputCodec('update');
|
|
229
|
+
const data = inputCodec(input);
|
|
229
230
|
const out = await this._dbUpdate(id, data, options);
|
|
231
|
+
const outputCodec = this.getOutputCodec('update');
|
|
230
232
|
if (out)
|
|
231
|
-
return
|
|
233
|
+
return outputCodec(out);
|
|
232
234
|
}
|
|
233
235
|
/**
|
|
234
236
|
* Updates a record in the collection with the specified ID and returns updated record count
|
|
@@ -240,8 +242,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
240
242
|
* @protected
|
|
241
243
|
*/
|
|
242
244
|
async _updateOnly(id, input, options) {
|
|
243
|
-
const
|
|
244
|
-
const data =
|
|
245
|
+
const inputCodec = this.getInputCodec('create');
|
|
246
|
+
const data = inputCodec(input);
|
|
245
247
|
return await this._dbUpdateOnly(id, data, options);
|
|
246
248
|
}
|
|
247
249
|
/**
|
|
@@ -253,8 +255,8 @@ class SqbEntityService extends core_1.ServiceBase {
|
|
|
253
255
|
* @protected
|
|
254
256
|
*/
|
|
255
257
|
async _updateMany(input, options) {
|
|
256
|
-
const
|
|
257
|
-
const data =
|
|
258
|
+
const inputCodec = this.getInputCodec('update');
|
|
259
|
+
const data = inputCodec(input);
|
|
258
260
|
return await this._dbUpdateMany(data, options);
|
|
259
261
|
}
|
|
260
262
|
/**
|
|
@@ -16,7 +16,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
16
16
|
*/
|
|
17
17
|
constructor(dataType, options) {
|
|
18
18
|
super();
|
|
19
|
-
this.
|
|
19
|
+
this._inputCodecs = {};
|
|
20
|
+
this._outputCodecs = {};
|
|
20
21
|
this._dataType_ = dataType;
|
|
21
22
|
this.db = options?.db;
|
|
22
23
|
this.$resourceName = options?.resourceName;
|
|
@@ -71,34 +72,34 @@ export class SqbEntityService extends ServiceBase {
|
|
|
71
72
|
throw new Error('resourceName is not defined');
|
|
72
73
|
}
|
|
73
74
|
/**
|
|
74
|
-
* Retrieves the
|
|
75
|
+
* Retrieves the codec for the specified operation.
|
|
75
76
|
*
|
|
76
77
|
* @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
|
|
77
78
|
*/
|
|
78
|
-
|
|
79
|
-
let
|
|
80
|
-
if (
|
|
81
|
-
return
|
|
82
|
-
const options = { projection: '*' };
|
|
79
|
+
getInputCodec(operation) {
|
|
80
|
+
let validator = this._inputCodecs[operation];
|
|
81
|
+
if (validator)
|
|
82
|
+
return validator;
|
|
83
|
+
const options = { projection: '*', ignoreReadonlyFields: true };
|
|
83
84
|
if (operation === 'update')
|
|
84
85
|
options.partial = 'deep';
|
|
85
86
|
const dataType = this.dataType;
|
|
86
|
-
|
|
87
|
-
this.
|
|
88
|
-
return
|
|
87
|
+
validator = dataType.generateCodec('decode', options);
|
|
88
|
+
this._inputCodecs[operation] = validator;
|
|
89
|
+
return validator;
|
|
89
90
|
}
|
|
90
91
|
/**
|
|
91
|
-
* Retrieves the
|
|
92
|
+
* Retrieves the codec.
|
|
92
93
|
*/
|
|
93
|
-
|
|
94
|
-
let
|
|
95
|
-
if (
|
|
96
|
-
return
|
|
97
|
-
const options = { projection: '*', partial: 'deep' };
|
|
94
|
+
getOutputCodec(operation) {
|
|
95
|
+
let validator = this._outputCodecs[operation];
|
|
96
|
+
if (validator)
|
|
97
|
+
return validator;
|
|
98
|
+
const options = { projection: '*', partial: 'deep', ignoreWriteonlyFields: true };
|
|
98
99
|
const dataType = this.dataType;
|
|
99
|
-
|
|
100
|
-
this.
|
|
101
|
-
return
|
|
100
|
+
validator = dataType.generateCodec('decode', options);
|
|
101
|
+
this._outputCodecs[operation] = validator;
|
|
102
|
+
return validator;
|
|
102
103
|
}
|
|
103
104
|
/**
|
|
104
105
|
* Insert a new record into database
|
|
@@ -110,11 +111,12 @@ export class SqbEntityService extends ServiceBase {
|
|
|
110
111
|
* @protected
|
|
111
112
|
*/
|
|
112
113
|
async _create(input, options) {
|
|
113
|
-
const
|
|
114
|
-
const
|
|
114
|
+
const inputCodec = this.getInputCodec('create');
|
|
115
|
+
const outputCodec = this.getOutputCodec('create');
|
|
116
|
+
const data = inputCodec(input);
|
|
115
117
|
const out = await this._dbCreate(data, options);
|
|
116
118
|
if (out)
|
|
117
|
-
return out;
|
|
119
|
+
return outputCodec(out);
|
|
118
120
|
throw new InternalServerError(`Unknown error while creating document for "${this.getResourceName()}"`);
|
|
119
121
|
}
|
|
120
122
|
/**
|
|
@@ -178,7 +180,7 @@ export class SqbEntityService extends ServiceBase {
|
|
|
178
180
|
* @protected
|
|
179
181
|
*/
|
|
180
182
|
async _findById(id, options) {
|
|
181
|
-
const decode = this.
|
|
183
|
+
const decode = this.getOutputCodec('find');
|
|
182
184
|
const out = await this._dbFindById(id, options);
|
|
183
185
|
return out ? decode(out) : undefined;
|
|
184
186
|
}
|
|
@@ -190,7 +192,7 @@ export class SqbEntityService extends ServiceBase {
|
|
|
190
192
|
* @protected
|
|
191
193
|
*/
|
|
192
194
|
async _findOne(options) {
|
|
193
|
-
const decode = this.
|
|
195
|
+
const decode = this.getOutputCodec('find');
|
|
194
196
|
const out = await this._dbFindOne(options);
|
|
195
197
|
return out ? decode(out) : undefined;
|
|
196
198
|
}
|
|
@@ -202,7 +204,7 @@ export class SqbEntityService extends ServiceBase {
|
|
|
202
204
|
* @protected
|
|
203
205
|
*/
|
|
204
206
|
async _findMany(options) {
|
|
205
|
-
const decode = this.
|
|
207
|
+
const decode = this.getOutputCodec('find');
|
|
206
208
|
const out = await this._dbFindMany(options);
|
|
207
209
|
if (out?.length) {
|
|
208
210
|
return out.map(x => decode(x));
|
|
@@ -220,12 +222,12 @@ export class SqbEntityService extends ServiceBase {
|
|
|
220
222
|
* @protected
|
|
221
223
|
*/
|
|
222
224
|
async _update(id, input, options) {
|
|
223
|
-
const
|
|
224
|
-
const
|
|
225
|
-
const data = encode(input);
|
|
225
|
+
const inputCodec = this.getInputCodec('update');
|
|
226
|
+
const data = inputCodec(input);
|
|
226
227
|
const out = await this._dbUpdate(id, data, options);
|
|
228
|
+
const outputCodec = this.getOutputCodec('update');
|
|
227
229
|
if (out)
|
|
228
|
-
return
|
|
230
|
+
return outputCodec(out);
|
|
229
231
|
}
|
|
230
232
|
/**
|
|
231
233
|
* Updates a record in the collection with the specified ID and returns updated record count
|
|
@@ -237,8 +239,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
237
239
|
* @protected
|
|
238
240
|
*/
|
|
239
241
|
async _updateOnly(id, input, options) {
|
|
240
|
-
const
|
|
241
|
-
const data =
|
|
242
|
+
const inputCodec = this.getInputCodec('create');
|
|
243
|
+
const data = inputCodec(input);
|
|
242
244
|
return await this._dbUpdateOnly(id, data, options);
|
|
243
245
|
}
|
|
244
246
|
/**
|
|
@@ -250,8 +252,8 @@ export class SqbEntityService extends ServiceBase {
|
|
|
250
252
|
* @protected
|
|
251
253
|
*/
|
|
252
254
|
async _updateMany(input, options) {
|
|
253
|
-
const
|
|
254
|
-
const data =
|
|
255
|
+
const inputCodec = this.getInputCodec('update');
|
|
256
|
+
const data = inputCodec(input);
|
|
255
257
|
return await this._dbUpdateMany(data, options);
|
|
256
258
|
}
|
|
257
259
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/sqb",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.4",
|
|
4
4
|
"description": "Opra SQB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"postbuild": "cp README.md package.json ../../LICENSE ../../build/sqb && cp ../../package.cjs.json ../../build/sqb/cjs/package.json",
|
|
19
19
|
"lint": "eslint . --max-warnings=0",
|
|
20
20
|
"format": "prettier . --write --log-level=warn",
|
|
21
|
-
"test": "jest",
|
|
22
|
-
"cover": "jest --collect-coverage",
|
|
21
|
+
"test": "jest --passWithNoTests",
|
|
22
|
+
"cover": "jest --passWithNoTests --collect-coverage",
|
|
23
23
|
"clean": "npm run clean:src && npm run clean:test && npm run clean:dist && npm run clean:cover",
|
|
24
24
|
"clean:src": "ts-cleanup -s src --all",
|
|
25
25
|
"clean:test": "ts-cleanup -s test --all",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"ts-gems": "^3.4.0"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@opra/core": "^1.0.0-alpha.
|
|
38
|
+
"@opra/core": "^1.0.0-alpha.4",
|
|
39
39
|
"@sqb/connect": ">= 4.10.6"
|
|
40
40
|
},
|
|
41
41
|
"type": "module",
|
|
@@ -111,8 +111,8 @@ export declare class SqbEntityService<T extends object = object> extends Service
|
|
|
111
111
|
protected _dataType: ComplexType;
|
|
112
112
|
protected _dataTypeClass?: Type;
|
|
113
113
|
protected _entityMetadata?: EntityMetadata;
|
|
114
|
-
protected
|
|
115
|
-
protected
|
|
114
|
+
protected _inputCodecs: Record<string, IsObject.Validator<T>>;
|
|
115
|
+
protected _outputCodecs: Record<string, IsObject.Validator<T>>;
|
|
116
116
|
/**
|
|
117
117
|
* Represents a SqbClient or SqbConnection object
|
|
118
118
|
*/
|
|
@@ -178,15 +178,15 @@ export declare class SqbEntityService<T extends object = object> extends Service
|
|
|
178
178
|
*/
|
|
179
179
|
getResourceName(): string;
|
|
180
180
|
/**
|
|
181
|
-
* Retrieves the
|
|
181
|
+
* Retrieves the codec for the specified operation.
|
|
182
182
|
*
|
|
183
183
|
* @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
|
|
184
184
|
*/
|
|
185
|
-
|
|
185
|
+
getInputCodec(operation: string): IsObject.Validator<T>;
|
|
186
186
|
/**
|
|
187
|
-
* Retrieves the
|
|
187
|
+
* Retrieves the codec.
|
|
188
188
|
*/
|
|
189
|
-
|
|
189
|
+
getOutputCodec(operation: string): IsObject.Validator<T>;
|
|
190
190
|
/**
|
|
191
191
|
* Insert a new record into database
|
|
192
192
|
*
|