rystem.repository.client 0.1.1 → 0.2.1
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/lib/engine/Repository.d.ts +24 -0
- package/lib/engine/Repository.js +310 -0
- package/lib/engine/batch/Batch.d.ts +9 -0
- package/lib/engine/batch/Batch.js +17 -0
- package/lib/engine/batch/BatchBuilder.d.ts +13 -0
- package/lib/engine/batch/BatchBuilder.js +48 -0
- package/lib/engine/batch/BatchOperation.d.ts +12 -0
- package/lib/engine/batch/BatchOperation.js +16 -0
- package/lib/engine/batch/BatchResult.d.ts +13 -0
- package/lib/engine/batch/BatchResult.js +18 -0
- package/lib/engine/query/FilterOperationAsString.d.ts +5 -0
- package/lib/engine/query/FilterOperationAsString.js +2 -0
- package/lib/engine/query/FilterOperations.d.ts +11 -0
- package/lib/engine/query/FilterOperations.js +15 -0
- package/lib/engine/query/QueryBuilder.d.ts +25 -0
- package/lib/engine/query/QueryBuilder.js +116 -0
- package/lib/engine/query/SerializableFilter.d.ts +4 -0
- package/lib/engine/query/SerializableFilter.js +2 -0
- package/lib/engine/query/filter/WhereBuilder.d.ts +33 -0
- package/lib/engine/query/filter/WhereBuilder.js +170 -0
- package/lib/hooks/hooks.d.ts +6 -0
- package/lib/hooks/hooks.js +16 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +13 -0
- package/lib/interfaces/ICommand.d.ts +8 -0
- package/lib/interfaces/ICommand.js +2 -0
- package/lib/interfaces/IQuery.d.ts +7 -0
- package/lib/interfaces/IQuery.js +2 -0
- package/lib/interfaces/IRepository.d.ts +4 -0
- package/lib/interfaces/IRepository.js +2 -0
- package/lib/models/Entity.d.ts +11 -0
- package/lib/models/Entity.js +20 -0
- package/lib/models/RepositoryEndpoint.d.ts +12 -0
- package/lib/models/RepositoryEndpoint.js +16 -0
- package/lib/models/State.d.ts +20 -0
- package/lib/models/State.js +26 -0
- package/lib/servicecollection/RepositoryServices.d.ts +19 -0
- package/lib/servicecollection/RepositoryServices.js +80 -0
- package/lib/servicecollection/RepositorySettings.d.ts +21 -0
- package/lib/servicecollection/RepositorySettings.js +56 -0
- package/package.json +6 -3
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { CancellationToken } from "typescript";
|
|
2
|
+
import { IRepository } from "../interfaces/IRepository";
|
|
3
|
+
import { RepositoryEndpoint } from "../models/RepositoryEndpoint";
|
|
4
|
+
import { State } from "../models/State";
|
|
5
|
+
import { RepositorySettings } from "../servicecollection/RepositorySettings";
|
|
6
|
+
import { BatchBuilder } from "./batch/BatchBuilder";
|
|
7
|
+
import { QueryBuilder } from "./query/QueryBuilder";
|
|
8
|
+
export declare class Repository<T, TKey> implements IRepository<T, TKey> {
|
|
9
|
+
private baseUri;
|
|
10
|
+
private settings;
|
|
11
|
+
constructor(baseUri: string | null, settings: RepositorySettings);
|
|
12
|
+
private serializeRequestBody;
|
|
13
|
+
private transformResponse;
|
|
14
|
+
makeRequest<TResponse>(endpoint: RepositoryEndpoint, path: string, method: string, body?: any, bodyIsKey?: boolean, skipSerialization?: boolean, skipSerializationOnResponse?: boolean, headers?: HeadersInit): Promise<TResponse>;
|
|
15
|
+
makeRequestAsStream<TResponse>(endpoint: RepositoryEndpoint, path: string, method: string, reader: (entity: TResponse) => void, body?: any, bodyIsKey?: boolean, skipSerialization?: boolean, skipSerializationOnResponse?: boolean, headers?: HeadersInit, cancellationToken?: CancellationToken | null): Promise<Array<TResponse>>;
|
|
16
|
+
get(key: TKey): Promise<T>;
|
|
17
|
+
insert(key: TKey, value: T): Promise<State<T, TKey>>;
|
|
18
|
+
update(key: TKey, value: T): Promise<State<T, TKey>>;
|
|
19
|
+
exist(key: TKey): Promise<State<T, TKey>>;
|
|
20
|
+
delete(key: TKey): Promise<State<T, TKey>>;
|
|
21
|
+
batch(): BatchBuilder<T, TKey>;
|
|
22
|
+
query(): QueryBuilder<T, TKey>;
|
|
23
|
+
static predicateAsString<T>(predicate: (value: T) => any): string;
|
|
24
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Repository = void 0;
|
|
4
|
+
const Entity_1 = require("../models/Entity");
|
|
5
|
+
const RepositoryEndpoint_1 = require("../models/RepositoryEndpoint");
|
|
6
|
+
const State_1 = require("../models/State");
|
|
7
|
+
const BatchBuilder_1 = require("./batch/BatchBuilder");
|
|
8
|
+
const Batch_1 = require("./batch/Batch");
|
|
9
|
+
const BatchResult_1 = require("./batch/BatchResult");
|
|
10
|
+
const QueryBuilder_1 = require("./query/QueryBuilder");
|
|
11
|
+
class Repository {
|
|
12
|
+
baseUri;
|
|
13
|
+
settings;
|
|
14
|
+
constructor(baseUri, settings) {
|
|
15
|
+
if (settings.uri == null)
|
|
16
|
+
this.baseUri = `${baseUri}${settings.path}`;
|
|
17
|
+
else
|
|
18
|
+
this.baseUri = settings.uri;
|
|
19
|
+
this.settings = settings;
|
|
20
|
+
}
|
|
21
|
+
serializeRequestBody(body, isKey) {
|
|
22
|
+
const { transformer, keyTransformer } = this.settings;
|
|
23
|
+
if (!body)
|
|
24
|
+
return body;
|
|
25
|
+
// Entity<T, TKey> → SerializedEntity<T, TKey>
|
|
26
|
+
if ((0, Entity_1.isEntity)(body)) {
|
|
27
|
+
return {
|
|
28
|
+
v: transformer?.toPlain?.(body.value) ?? body.value,
|
|
29
|
+
k: keyTransformer?.toPlain?.(body.key) ?? body.key,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
// Batch<T, TKey> → SerializedBatch<T, TKey>
|
|
33
|
+
if ((0, Batch_1.isBatch)(body)) {
|
|
34
|
+
return {
|
|
35
|
+
v: body.values.map(op => ({
|
|
36
|
+
c: op.command,
|
|
37
|
+
k: op.key && keyTransformer?.toPlain ? keyTransformer.toPlain(op.key) : op.key,
|
|
38
|
+
v: op.value && transformer?.toPlain ? transformer.toPlain(op.value) : op.value,
|
|
39
|
+
})),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
// State<T, TKey> → SerializedState<T, TKey>
|
|
43
|
+
if ((0, State_1.isState)(body)) {
|
|
44
|
+
return {
|
|
45
|
+
i: body.isOk,
|
|
46
|
+
c: body.code,
|
|
47
|
+
m: body.message,
|
|
48
|
+
e: {
|
|
49
|
+
k: keyTransformer?.toPlain?.(body.entity.key) ?? body.entity.key,
|
|
50
|
+
v: transformer?.toPlain?.(body.entity.value) ?? body.entity.value,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
// Fallback — assume it's T
|
|
55
|
+
if (!isKey)
|
|
56
|
+
return transformer?.toPlain?.(body) ?? body;
|
|
57
|
+
else
|
|
58
|
+
return keyTransformer?.toPlain?.(body) ?? body;
|
|
59
|
+
}
|
|
60
|
+
transformResponse(res) {
|
|
61
|
+
const { transformer, keyTransformer } = this.settings;
|
|
62
|
+
if (!res)
|
|
63
|
+
return res;
|
|
64
|
+
// SerializedState<T, TKey> → State<T, TKey>
|
|
65
|
+
if ((0, State_1.isSerializedState)(res)) {
|
|
66
|
+
return {
|
|
67
|
+
isOk: res.i,
|
|
68
|
+
code: res.c,
|
|
69
|
+
message: res.m,
|
|
70
|
+
entity: res.e ? {
|
|
71
|
+
key: res.e.k && keyTransformer?.fromPlain ? keyTransformer.fromPlain(res.e.k) : res.e.k,
|
|
72
|
+
value: res.e.v && transformer?.fromPlain ? transformer.fromPlain(res.e.v) : res.e.v,
|
|
73
|
+
} : res.e,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
// SerializedEntity<T, TKey> → Entity<T, TKey>
|
|
77
|
+
if ((0, Entity_1.isSerializedEntity)(res)) {
|
|
78
|
+
return {
|
|
79
|
+
key: res.k && keyTransformer?.fromPlain ? keyTransformer.fromPlain(res.k) : res.k,
|
|
80
|
+
value: res.v && transformer?.fromPlain ? transformer.fromPlain(res.v) : res.v,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// SerializedEntity<T, TKey>[] → Entity<T, TKey>[]
|
|
84
|
+
if ((0, Entity_1.isSerializedEntityArray)(res)) {
|
|
85
|
+
return res.map(item => ({
|
|
86
|
+
key: item.k && keyTransformer?.fromPlain ? keyTransformer.fromPlain(item.k) : item.k,
|
|
87
|
+
value: item.v && transformer?.fromPlain ? transformer.fromPlain(item.v) : item.v,
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
// SerializedBatchResult<T, TKey>[] → BatchResult<T, TKey>[]
|
|
91
|
+
if (Array.isArray(res) && res.every(BatchResult_1.isSerializedBatchResult)) {
|
|
92
|
+
return res.map(item => ({
|
|
93
|
+
code: item.c,
|
|
94
|
+
key: item.k && keyTransformer?.fromPlain ? keyTransformer.fromPlain(item.k) : item.k,
|
|
95
|
+
state: item.s ? {
|
|
96
|
+
isOk: item.s.i,
|
|
97
|
+
code: item.s.c,
|
|
98
|
+
message: item.s.m,
|
|
99
|
+
entity: item.s.e ? {
|
|
100
|
+
key: item.s.e.k && keyTransformer?.fromPlain ? keyTransformer.fromPlain(item.s.e.k) : item.s.e.k,
|
|
101
|
+
value: item.s.e.v && transformer?.fromPlain ? transformer.fromPlain(item.s.e.v) : item.s.e.v,
|
|
102
|
+
} : item.s.e,
|
|
103
|
+
} : item.s,
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
// SerializedBatch<T, TKey> → Batch<T, TKey>
|
|
107
|
+
if ((0, Batch_1.isSerializedBatch)(res)) {
|
|
108
|
+
return {
|
|
109
|
+
values: res.v.map((op) => ({
|
|
110
|
+
command: op.c,
|
|
111
|
+
key: op.k && keyTransformer?.fromPlain ? keyTransformer.fromPlain(op.k) : op.k,
|
|
112
|
+
value: op.v && transformer?.fromPlain ? transformer.fromPlain(op.v) : op.v,
|
|
113
|
+
})),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// SerializedBatchResult<T, TKey> → BatchResult<T, TKey>
|
|
117
|
+
if ((0, BatchResult_1.isSerializedBatchResult)(res)) {
|
|
118
|
+
return {
|
|
119
|
+
code: res.c,
|
|
120
|
+
key: keyTransformer?.fromPlain?.(res.k) ?? res.k,
|
|
121
|
+
state: res.s ? {
|
|
122
|
+
isOk: res.s.i,
|
|
123
|
+
code: res.s.c,
|
|
124
|
+
message: res.s.m,
|
|
125
|
+
entity: res.s.e ? {
|
|
126
|
+
key: res.s.e.k && keyTransformer?.fromPlain ? keyTransformer.fromPlain(res.s.e.k) : res.s.e.k,
|
|
127
|
+
value: res.s.e.v && transformer?.fromPlain ? transformer.fromPlain(res.s.e.v) : res.s.e.v,
|
|
128
|
+
} : res.s.e,
|
|
129
|
+
} : res.s,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
// Fallback: T ← fromPlain
|
|
133
|
+
return transformer?.fromPlain?.(res) ?? res;
|
|
134
|
+
}
|
|
135
|
+
async makeRequest(endpoint, path, method, body = null, bodyIsKey = false, skipSerialization = false, skipSerializationOnResponse = false, headers = {}) {
|
|
136
|
+
let retry = true;
|
|
137
|
+
let response = null;
|
|
138
|
+
const uri = `${this.baseUri}/${path}`;
|
|
139
|
+
while (retry) {
|
|
140
|
+
response = await fetch(uri, {
|
|
141
|
+
method: method,
|
|
142
|
+
headers: this.settings.enrichHeaders(endpoint, uri, method, headers, body),
|
|
143
|
+
body: body == null ? null : JSON.stringify(skipSerialization ? body : this.serializeRequestBody(body, bodyIsKey))
|
|
144
|
+
})
|
|
145
|
+
.then(async (res) => {
|
|
146
|
+
const json = await res.json();
|
|
147
|
+
if (!res.ok)
|
|
148
|
+
throw json;
|
|
149
|
+
return json;
|
|
150
|
+
})
|
|
151
|
+
.catch((err) => {
|
|
152
|
+
retry = this.settings.manageError(endpoint, uri, method, headers, body, err);
|
|
153
|
+
return null;
|
|
154
|
+
})
|
|
155
|
+
.then(res => {
|
|
156
|
+
retry = false;
|
|
157
|
+
return skipSerializationOnResponse ? res : this.transformResponse(res);
|
|
158
|
+
});
|
|
159
|
+
if (retry)
|
|
160
|
+
await new Promise(f => setTimeout(f, 200));
|
|
161
|
+
}
|
|
162
|
+
return response;
|
|
163
|
+
}
|
|
164
|
+
async makeRequestAsStream(endpoint, path, method, reader, body = null, bodyIsKey = false, skipSerialization = false, skipSerializationOnResponse = false, headers = {}, cancellationToken = null) {
|
|
165
|
+
const uri = `${this.baseUri}/${path}/Stream`;
|
|
166
|
+
let response = [];
|
|
167
|
+
await fetch(uri, {
|
|
168
|
+
method: method,
|
|
169
|
+
headers: this.settings.enrichHeaders(endpoint, uri, method, headers, body),
|
|
170
|
+
body: body == null ? null : JSON.stringify(skipSerialization ? body : this.serializeRequestBody(body, bodyIsKey))
|
|
171
|
+
})
|
|
172
|
+
.then(async (res) => {
|
|
173
|
+
const bodyReader = res.body?.getReader();
|
|
174
|
+
const decoder = new JsonStreamDecoder();
|
|
175
|
+
if (bodyReader != undefined) {
|
|
176
|
+
while (true) {
|
|
177
|
+
const { done, value } = await bodyReader.read();
|
|
178
|
+
if (done)
|
|
179
|
+
break;
|
|
180
|
+
if (!value)
|
|
181
|
+
continue;
|
|
182
|
+
if (cancellationToken?.isCancellationRequested())
|
|
183
|
+
break;
|
|
184
|
+
decoder.decodeChunk(value, entity => {
|
|
185
|
+
const transformed = skipSerializationOnResponse ? entity : this.transformResponse(entity);
|
|
186
|
+
response.push(transformed);
|
|
187
|
+
reader(transformed);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
bodyReader.releaseLock();
|
|
191
|
+
}
|
|
192
|
+
})
|
|
193
|
+
.catch(async (err) => {
|
|
194
|
+
this.settings.manageError(endpoint, uri, method, headers, body, err);
|
|
195
|
+
return null;
|
|
196
|
+
});
|
|
197
|
+
return response;
|
|
198
|
+
}
|
|
199
|
+
get(key) {
|
|
200
|
+
if (!this.settings.complexKey) {
|
|
201
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Get, `Get?key=${key}`, 'GET');
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Get, `Get`, 'POST', key, true, false, false, {
|
|
205
|
+
'content-type': 'application/json;charset=UTF-8'
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
insert(key, value) {
|
|
210
|
+
if (!this.settings.complexKey) {
|
|
211
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Insert, `Insert?key=${key}`, 'POST', {
|
|
212
|
+
value: value,
|
|
213
|
+
key: key
|
|
214
|
+
}, false, false, false, {
|
|
215
|
+
'content-type': 'application/json;charset=UTF-8'
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Insert, `Insert`, 'POST', {
|
|
220
|
+
value: value,
|
|
221
|
+
key: key
|
|
222
|
+
}, false, false, false, {
|
|
223
|
+
'content-type': 'application/json;charset=UTF-8'
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
update(key, value) {
|
|
228
|
+
if (!this.settings.complexKey) {
|
|
229
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Update, `Update?key=${key}`, 'POST', {
|
|
230
|
+
value: value,
|
|
231
|
+
key: key
|
|
232
|
+
}, false, false, false, {
|
|
233
|
+
'content-type': 'application/json;charset=UTF-8'
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Update, `Update`, 'POST', {
|
|
238
|
+
value: value,
|
|
239
|
+
key: key
|
|
240
|
+
}, false, false, false, {
|
|
241
|
+
'content-type': 'application/json;charset=UTF-8'
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
exist(key) {
|
|
246
|
+
if (!this.settings.complexKey) {
|
|
247
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Exist, `Exist?key=${key}`, 'GET');
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Exist, `Exist`, 'POST', key, true, false, false, {
|
|
251
|
+
'content-type': 'application/json;charset=UTF-8'
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
delete(key) {
|
|
256
|
+
if (!this.settings.complexKey) {
|
|
257
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Delete, `Delete?key=${key}`, 'GET');
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Delete, `Delete`, 'POST', key, true, false, false, {
|
|
261
|
+
'content-type': 'application/json;charset=UTF-8'
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
batch() {
|
|
266
|
+
return new BatchBuilder_1.BatchBuilder(this);
|
|
267
|
+
}
|
|
268
|
+
query() {
|
|
269
|
+
return new QueryBuilder_1.QueryBuilder(this);
|
|
270
|
+
}
|
|
271
|
+
static predicateAsString(predicate) {
|
|
272
|
+
const splittedPredicate = predicate.toString().split('.');
|
|
273
|
+
const variableName = splittedPredicate.slice(-(splittedPredicate.length - 1)).join('.');
|
|
274
|
+
return `_rystem.${variableName}`;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
exports.Repository = Repository;
|
|
278
|
+
class JsonStreamDecoder {
|
|
279
|
+
level = 0;
|
|
280
|
+
partialItem = '';
|
|
281
|
+
static JTOKEN_START_OBJECT = '{';
|
|
282
|
+
static JTOKEN_END_OBJECT = '}';
|
|
283
|
+
decoder = new TextDecoder();
|
|
284
|
+
decodeChunk(value, decodedItemCallback) {
|
|
285
|
+
const chunk = this.decoder.decode(value);
|
|
286
|
+
let itemStart = 0;
|
|
287
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
288
|
+
if (chunk[i] === JsonStreamDecoder.JTOKEN_START_OBJECT) {
|
|
289
|
+
if (this.level === 0) {
|
|
290
|
+
itemStart = i;
|
|
291
|
+
}
|
|
292
|
+
this.level++;
|
|
293
|
+
}
|
|
294
|
+
if (chunk[i] === JsonStreamDecoder.JTOKEN_END_OBJECT) {
|
|
295
|
+
this.level--;
|
|
296
|
+
if (this.level === 0) {
|
|
297
|
+
let item = chunk.substring(itemStart, i + 1);
|
|
298
|
+
if (this.partialItem) {
|
|
299
|
+
item = this.partialItem + item;
|
|
300
|
+
this.partialItem = '';
|
|
301
|
+
}
|
|
302
|
+
decodedItemCallback(JSON.parse(item));
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (this.level !== 0) {
|
|
307
|
+
this.partialItem = chunk.substring(itemStart);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BatchOperation } from "./BatchOperation";
|
|
2
|
+
export type Batch<T, TKey> = {
|
|
3
|
+
values: Array<BatchOperation<T, TKey>>;
|
|
4
|
+
};
|
|
5
|
+
export type SerializedBatch<T, TKey> = {
|
|
6
|
+
v: Array<BatchOperation<T, TKey>>;
|
|
7
|
+
};
|
|
8
|
+
export declare function isBatch<T, TKey>(obj: any): obj is Batch<T, TKey>;
|
|
9
|
+
export declare function isSerializedBatch<T, TKey>(obj: any): obj is SerializedBatch<T, TKey>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBatch = isBatch;
|
|
4
|
+
exports.isSerializedBatch = isSerializedBatch;
|
|
5
|
+
const BatchOperation_1 = require("./BatchOperation");
|
|
6
|
+
function isBatch(obj) {
|
|
7
|
+
return (typeof obj === 'object' &&
|
|
8
|
+
obj !== null &&
|
|
9
|
+
Array.isArray(obj.values) &&
|
|
10
|
+
obj.values.every(BatchOperation_1.isBatchOperation));
|
|
11
|
+
}
|
|
12
|
+
function isSerializedBatch(obj) {
|
|
13
|
+
return (typeof obj === 'object' &&
|
|
14
|
+
obj !== null &&
|
|
15
|
+
Array.isArray(obj.v) &&
|
|
16
|
+
obj.v.every(BatchOperation_1.isSerializedBatchOperation));
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Repository } from "../Repository";
|
|
2
|
+
import { BatchResult } from "./BatchResult";
|
|
3
|
+
import { CancellationToken } from "typescript";
|
|
4
|
+
export declare class BatchBuilder<T, TKey> {
|
|
5
|
+
private batch;
|
|
6
|
+
private repository;
|
|
7
|
+
constructor(repository: Repository<T, TKey>);
|
|
8
|
+
addInsert(key: TKey, value: T): BatchBuilder<T, TKey>;
|
|
9
|
+
addUpdate(key: TKey, value: T): BatchBuilder<T, TKey>;
|
|
10
|
+
addDelete(key: TKey): BatchBuilder<T, TKey>;
|
|
11
|
+
execute(): Promise<Array<BatchResult<T, TKey>>>;
|
|
12
|
+
executeAsStream(entityReader: (entity: BatchResult<T, TKey>) => void, cancellationToken?: CancellationToken | null): Promise<Array<BatchResult<T, TKey>>>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BatchBuilder = void 0;
|
|
4
|
+
const RepositoryEndpoint_1 = require("../../models/RepositoryEndpoint");
|
|
5
|
+
class BatchBuilder {
|
|
6
|
+
batch;
|
|
7
|
+
repository;
|
|
8
|
+
constructor(repository) {
|
|
9
|
+
this.batch = {
|
|
10
|
+
values: []
|
|
11
|
+
};
|
|
12
|
+
this.repository = repository;
|
|
13
|
+
}
|
|
14
|
+
addInsert(key, value) {
|
|
15
|
+
this.batch.values.push({
|
|
16
|
+
command: 1,
|
|
17
|
+
key: key,
|
|
18
|
+
value: value
|
|
19
|
+
});
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
addUpdate(key, value) {
|
|
23
|
+
this.batch.values.push({
|
|
24
|
+
command: 2,
|
|
25
|
+
key: key,
|
|
26
|
+
value: value
|
|
27
|
+
});
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
addDelete(key) {
|
|
31
|
+
this.batch.values.push({
|
|
32
|
+
command: 4,
|
|
33
|
+
key: key
|
|
34
|
+
});
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
execute() {
|
|
38
|
+
return this.repository.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Batch, `Batch`, 'POST', this.batch, false, false, false, {
|
|
39
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
executeAsStream(entityReader, cancellationToken = null) {
|
|
43
|
+
return this.repository.makeRequestAsStream(RepositoryEndpoint_1.RepositoryEndpoint.BatchStream, `Batch`, 'POST', entityReader, this.batch, false, false, false, {
|
|
44
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
45
|
+
}, cancellationToken);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.BatchBuilder = BatchBuilder;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type BatchOperation<T, TKey> = {
|
|
2
|
+
command: 1 | 2 | 4;
|
|
3
|
+
key: TKey;
|
|
4
|
+
value: T | null;
|
|
5
|
+
};
|
|
6
|
+
export type SerializedBatchOperation<T, TKey> = {
|
|
7
|
+
c: 1 | 2 | 4;
|
|
8
|
+
k: TKey;
|
|
9
|
+
v: T | null;
|
|
10
|
+
};
|
|
11
|
+
export declare function isBatchOperation<T, TKey>(obj: any): obj is BatchOperation<T, TKey>;
|
|
12
|
+
export declare function isSerializedBatchOperation<T, TKey>(obj: any): obj is SerializedBatchOperation<T, TKey>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBatchOperation = isBatchOperation;
|
|
4
|
+
exports.isSerializedBatchOperation = isSerializedBatchOperation;
|
|
5
|
+
function isBatchOperation(obj) {
|
|
6
|
+
return (typeof obj === 'object' &&
|
|
7
|
+
obj !== null &&
|
|
8
|
+
(obj.command === 1 || obj.command === 2 || obj.command === 4) &&
|
|
9
|
+
'key' in obj);
|
|
10
|
+
}
|
|
11
|
+
function isSerializedBatchOperation(obj) {
|
|
12
|
+
return (typeof obj === 'object' &&
|
|
13
|
+
obj !== null &&
|
|
14
|
+
(obj.c === 1 || obj.c === 2 || obj.c === 4) &&
|
|
15
|
+
'k' in obj);
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { State, SerializedState } from "../../models/State";
|
|
2
|
+
export type BatchResult<T, TKey> = {
|
|
3
|
+
code: 1 | 2 | 4;
|
|
4
|
+
key: TKey;
|
|
5
|
+
state: State<T, TKey>;
|
|
6
|
+
};
|
|
7
|
+
export type SerializedBatchResult<T, TKey> = {
|
|
8
|
+
c: 1 | 2 | 4;
|
|
9
|
+
k: TKey;
|
|
10
|
+
s: SerializedState<T, TKey>;
|
|
11
|
+
};
|
|
12
|
+
export declare function isBatchResult<T, TKey>(obj: any): obj is BatchResult<T, TKey>;
|
|
13
|
+
export declare function isSerializedBatchResult<T, TKey>(obj: any): obj is SerializedBatchResult<T, TKey>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBatchResult = isBatchResult;
|
|
4
|
+
exports.isSerializedBatchResult = isSerializedBatchResult;
|
|
5
|
+
function isBatchResult(obj) {
|
|
6
|
+
return (typeof obj === 'object' &&
|
|
7
|
+
obj !== null &&
|
|
8
|
+
(obj.code === 1 || obj.code === 2 || obj.code === 4) &&
|
|
9
|
+
'key' in obj &&
|
|
10
|
+
'state' in obj);
|
|
11
|
+
}
|
|
12
|
+
function isSerializedBatchResult(obj) {
|
|
13
|
+
return (typeof obj === 'object' &&
|
|
14
|
+
obj !== null &&
|
|
15
|
+
(obj.c === 1 || obj.c === 2 || obj.c === 4) &&
|
|
16
|
+
'k' in obj &&
|
|
17
|
+
's' in obj);
|
|
18
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FilterOperations = void 0;
|
|
4
|
+
var FilterOperations;
|
|
5
|
+
(function (FilterOperations) {
|
|
6
|
+
FilterOperations[FilterOperations["Select"] = 1] = "Select";
|
|
7
|
+
FilterOperations[FilterOperations["Where"] = 2] = "Where";
|
|
8
|
+
FilterOperations[FilterOperations["Top"] = 4] = "Top";
|
|
9
|
+
FilterOperations[FilterOperations["Skip"] = 8] = "Skip";
|
|
10
|
+
FilterOperations[FilterOperations["OrderBy"] = 16] = "OrderBy";
|
|
11
|
+
FilterOperations[FilterOperations["OrderByDescending"] = 32] = "OrderByDescending";
|
|
12
|
+
FilterOperations[FilterOperations["ThenBy"] = 64] = "ThenBy";
|
|
13
|
+
FilterOperations[FilterOperations["ThenByDescending"] = 128] = "ThenByDescending";
|
|
14
|
+
FilterOperations[FilterOperations["GroupBy"] = 256] = "GroupBy";
|
|
15
|
+
})(FilterOperations || (exports.FilterOperations = FilterOperations = {}));
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { CancellationToken } from "typescript";
|
|
2
|
+
import { Entity } from "../../models/Entity";
|
|
3
|
+
import { Repository } from "../Repository";
|
|
4
|
+
import { WhereBuilder } from "./filter/WhereBuilder";
|
|
5
|
+
export declare class QueryBuilder<T, TKey> {
|
|
6
|
+
private filters;
|
|
7
|
+
private repository;
|
|
8
|
+
constructor(repository: Repository<T, TKey>);
|
|
9
|
+
where(): WhereBuilder<T, TKey>;
|
|
10
|
+
filter(predicate: string): this;
|
|
11
|
+
top(value: number): this;
|
|
12
|
+
skip(value: number): this;
|
|
13
|
+
orderBy(predicate: (value: T) => any): this;
|
|
14
|
+
orderByDescending(predicate: (value: T) => any): this;
|
|
15
|
+
thenBy(predicate: (value: T) => any): this;
|
|
16
|
+
thenByDescending(predicate: (value: T) => any): this;
|
|
17
|
+
execute(): Promise<Array<Entity<T, TKey>>>;
|
|
18
|
+
executeAsStream(entityReader: (entity: Entity<T, TKey>) => void, cancellationToken?: CancellationToken | null): Promise<Array<Entity<T, TKey>>>;
|
|
19
|
+
count(): Promise<number>;
|
|
20
|
+
max(predicate: (value: T) => any): Promise<number>;
|
|
21
|
+
min(predicate: (value: T) => any): Promise<number>;
|
|
22
|
+
average(predicate: (value: T) => any): Promise<number>;
|
|
23
|
+
sum(predicate: (value: T) => any): Promise<number>;
|
|
24
|
+
private executeOperation;
|
|
25
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueryBuilder = void 0;
|
|
4
|
+
const RepositoryEndpoint_1 = require("../../models/RepositoryEndpoint");
|
|
5
|
+
const Repository_1 = require("../Repository");
|
|
6
|
+
const WhereBuilder_1 = require("./filter/WhereBuilder");
|
|
7
|
+
const FilterOperations_1 = require("./FilterOperations");
|
|
8
|
+
class QueryBuilder {
|
|
9
|
+
filters;
|
|
10
|
+
repository;
|
|
11
|
+
constructor(repository) {
|
|
12
|
+
this.filters = {
|
|
13
|
+
o: []
|
|
14
|
+
};
|
|
15
|
+
this.repository = repository;
|
|
16
|
+
}
|
|
17
|
+
where() {
|
|
18
|
+
return new WhereBuilder_1.WhereBuilder(this);
|
|
19
|
+
}
|
|
20
|
+
filter(predicate) {
|
|
21
|
+
this.filters.o.push({
|
|
22
|
+
q: FilterOperations_1.FilterOperations.Where,
|
|
23
|
+
v: predicate
|
|
24
|
+
});
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
top(value) {
|
|
28
|
+
this.filters.o.push({
|
|
29
|
+
q: FilterOperations_1.FilterOperations.Top,
|
|
30
|
+
v: value.toString()
|
|
31
|
+
});
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
skip(value) {
|
|
35
|
+
this.filters.o.push({
|
|
36
|
+
q: FilterOperations_1.FilterOperations.Skip,
|
|
37
|
+
v: value.toString()
|
|
38
|
+
});
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
orderBy(predicate) {
|
|
42
|
+
this.filters.o.push({
|
|
43
|
+
q: FilterOperations_1.FilterOperations.OrderBy,
|
|
44
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
45
|
+
});
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
orderByDescending(predicate) {
|
|
49
|
+
this.filters.o.push({
|
|
50
|
+
q: FilterOperations_1.FilterOperations.OrderByDescending,
|
|
51
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
52
|
+
});
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
thenBy(predicate) {
|
|
56
|
+
this.filters.o.push({
|
|
57
|
+
q: FilterOperations_1.FilterOperations.ThenBy,
|
|
58
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
59
|
+
});
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
thenByDescending(predicate) {
|
|
63
|
+
this.filters.o.push({
|
|
64
|
+
q: FilterOperations_1.FilterOperations.ThenByDescending,
|
|
65
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
66
|
+
});
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
execute() {
|
|
70
|
+
return this.repository.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Query, `Query`, 'POST', this.filters, false, true, false, {
|
|
71
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
executeAsStream(entityReader, cancellationToken = null) {
|
|
75
|
+
return this.repository.makeRequestAsStream(RepositoryEndpoint_1.RepositoryEndpoint.QueryStream, `Query`, 'POST', entityReader, this.filters, false, true, false, {
|
|
76
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
77
|
+
}, cancellationToken);
|
|
78
|
+
}
|
|
79
|
+
count() {
|
|
80
|
+
return this.executeOperation("Count", "long");
|
|
81
|
+
}
|
|
82
|
+
max(predicate) {
|
|
83
|
+
this.filters.o.push({
|
|
84
|
+
q: FilterOperations_1.FilterOperations.Select,
|
|
85
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
86
|
+
});
|
|
87
|
+
return this.executeOperation("Max", "decimal");
|
|
88
|
+
}
|
|
89
|
+
min(predicate) {
|
|
90
|
+
this.filters.o.push({
|
|
91
|
+
q: FilterOperations_1.FilterOperations.Select,
|
|
92
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
93
|
+
});
|
|
94
|
+
return this.executeOperation("Min", "decimal");
|
|
95
|
+
}
|
|
96
|
+
average(predicate) {
|
|
97
|
+
this.filters.o.push({
|
|
98
|
+
q: FilterOperations_1.FilterOperations.Select,
|
|
99
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
100
|
+
});
|
|
101
|
+
return this.executeOperation("Average", "decimal");
|
|
102
|
+
}
|
|
103
|
+
sum(predicate) {
|
|
104
|
+
this.filters.o.push({
|
|
105
|
+
q: FilterOperations_1.FilterOperations.Select,
|
|
106
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
107
|
+
});
|
|
108
|
+
return this.executeOperation("Sum", "decimal");
|
|
109
|
+
}
|
|
110
|
+
executeOperation(operation, returnType) {
|
|
111
|
+
return this.repository.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Operation, `Operation?op=${operation}&returnType=${returnType}`, 'POST', this.filters, false, true, true, {
|
|
112
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.QueryBuilder = QueryBuilder;
|