rystem.repository.client 0.0.2 → 0.0.5
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 +208 -0
- package/lib/engine/Repository.d.ts +20 -0
- package/lib/engine/Repository.js +116 -0
- package/lib/engine/batch/Batch.d.ts +4 -0
- package/lib/engine/batch/Batch.js +2 -0
- package/lib/engine/batch/BatchBuilder.d.ts +11 -0
- package/lib/engine/batch/BatchBuilder.js +43 -0
- package/lib/engine/batch/BatchElement.d.ts +5 -0
- package/lib/engine/batch/BatchElement.js +2 -0
- package/lib/engine/batch/BatchResult.d.ts +6 -0
- package/lib/engine/batch/BatchResult.js +2 -0
- package/lib/engine/batch/BatchResults.d.ts +4 -0
- package/lib/engine/batch/BatchResults.js +2 -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 +23 -0
- package/lib/engine/query/QueryBuilder.js +111 -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 +26 -0
- package/lib/engine/query/filter/WhereBuilder.js +152 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +7 -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 +4 -0
- package/lib/models/Entity.js +2 -0
- package/lib/models/RepositoryEndpoint.d.ts +10 -0
- package/lib/models/RepositoryEndpoint.js +14 -0
- package/lib/models/State.d.ts +9 -0
- package/lib/models/State.js +2 -0
- package/lib/servicecollection/RepositoryServices.d.ts +19 -0
- package/lib/servicecollection/RepositoryServices.js +80 -0
- package/lib/servicecollection/RepositorySettings.d.ts +14 -0
- package/lib/servicecollection/RepositorySettings.js +52 -0
- package/package.json +2 -9
package/README.md
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
### [What is Rystem?](https://github.com/KeyserDSoze/Rystem)
|
|
2
|
+
|
|
3
|
+
# Client for Repository Framework
|
|
4
|
+
|
|
5
|
+
## Services setup
|
|
6
|
+
You need to install through the RepositoryServices class
|
|
7
|
+
all the repository clients, with settings.
|
|
8
|
+
|
|
9
|
+
.Create() => set a default uri for all clients.
|
|
10
|
+
In settings you have:
|
|
11
|
+
- name: a value to retrieve the installed client.
|
|
12
|
+
- path: a path which concatened with default uri create the final url for your client.
|
|
13
|
+
- uri: override the default uri and the path
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
import { RepositoryServices } from "rystem.repository.client";
|
|
17
|
+
|
|
18
|
+
RepositoryServices
|
|
19
|
+
.Create("https://localhost:7058/api/")
|
|
20
|
+
.addRepository<IperUser, string>(x => {
|
|
21
|
+
x.name = "test";
|
|
22
|
+
x.path = "SuperUser";
|
|
23
|
+
})
|
|
24
|
+
.addRepository<SuperUser, string>(x => {
|
|
25
|
+
x.name = "test2"
|
|
26
|
+
x.uri = "https://localhost:9090/api/SuperUser/inmemory/"
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
You can also add Command or Query for your CQRS pattern with
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
RepositoryServices
|
|
34
|
+
.Create("https://localhost:7058/api/")
|
|
35
|
+
.addCommand<IperUser, string>(x => {
|
|
36
|
+
x.name = "test";
|
|
37
|
+
x.path = "SuperUser";
|
|
38
|
+
})
|
|
39
|
+
.addQuery<SuperUser, string>(x => {
|
|
40
|
+
x.name = "test2"
|
|
41
|
+
x.uri = "https://localhost:9090/api/SuperUser/inmemory/"
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Add Custom Headers to each request
|
|
46
|
+
For instance when you need to add a token from your authentication flow you can use this implementation.
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
RepositoryServices
|
|
50
|
+
.Create("http://localhost:5000/api/")
|
|
51
|
+
.addRepository<IperUser, string>(x => {
|
|
52
|
+
x.name = "test";
|
|
53
|
+
x.path = "SuperUser";
|
|
54
|
+
x.addHeadersEnricher((...args) => {
|
|
55
|
+
return {
|
|
56
|
+
"Authorization-UI": "Bearer dsjadjalsdjalsdjalsda"
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
x.addHeadersEnricher((endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any) => {
|
|
60
|
+
return {
|
|
61
|
+
"Authorization-UI2": "Bearer dsjadjalsdjalsdjalsda"
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Add Custom Error Handlers
|
|
68
|
+
For instance when you need to capture a not authorized error to request a new authentication flow before a new request.
|
|
69
|
+
Returning true you can retry automatically the request, with false the request chain will be stopped.
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
RepositoryServices
|
|
73
|
+
.Create("http://localhost:5000/api/")
|
|
74
|
+
.addRepository<IperUser, string>(x => {
|
|
75
|
+
x.name = "test";
|
|
76
|
+
x.path = "SuperUser";
|
|
77
|
+
x.addErrorHandler((endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any, err: any) => {
|
|
78
|
+
return (err as string).startsWith("big error");
|
|
79
|
+
});
|
|
80
|
+
})
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Usage
|
|
84
|
+
Always with RepositoryServices class you can retrieve with
|
|
85
|
+
the correct name the integration you setup during your startup.
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
const repository = RepositoryServices
|
|
89
|
+
.Repository<IperUser, string>("test");
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
You can also retrieve CQRS interfaces.
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
const command = RepositoryServices
|
|
96
|
+
.Command<IperUser, string>("test");
|
|
97
|
+
const query = RepositoryServices
|
|
98
|
+
.Query<IperUser, string>("test2");
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Methods
|
|
102
|
+
|
|
103
|
+
### Insert
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
let response: State<IperUser, string> =
|
|
107
|
+
await repository.insert(id, iperUser);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Update
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
let response: State<IperUser, string> =
|
|
114
|
+
await repository.update(id, iperUser);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Exist
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
let response: State<IperUser, string> =
|
|
121
|
+
await repository.exist(id);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Delete
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
let response: State<IperUser, string> =
|
|
128
|
+
await repository.delete(id);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Batch operations
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
const batcher = repository.batch();
|
|
135
|
+
batcher
|
|
136
|
+
.addInsert(id1, iperUser1)
|
|
137
|
+
.addUpdate(id2, iperUser2)
|
|
138
|
+
.addDelete(id3);
|
|
139
|
+
const batchResults: BatchResults<IperUser, string> =
|
|
140
|
+
await batcher.execute();
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Query operations
|
|
144
|
+
- get all elements
|
|
145
|
+
```
|
|
146
|
+
let queryResults = await repository.query().execute();
|
|
147
|
+
```
|
|
148
|
+
- filter as string
|
|
149
|
+
```
|
|
150
|
+
let id = "someId";
|
|
151
|
+
let queryResults = await repository
|
|
152
|
+
.query()
|
|
153
|
+
.filter(`x => x.id == "${id}"`)
|
|
154
|
+
.execute();
|
|
155
|
+
```
|
|
156
|
+
- build a filter (similar to the previous example with addiction of ordering by ascending all retrieved elements)
|
|
157
|
+
```
|
|
158
|
+
let queryResults = await repository
|
|
159
|
+
.query()
|
|
160
|
+
.where()
|
|
161
|
+
.select(x => x.id)
|
|
162
|
+
.equal(id)
|
|
163
|
+
.build()
|
|
164
|
+
.orderBy(x => x.name)
|
|
165
|
+
.execute();
|
|
166
|
+
```
|
|
167
|
+
- build a filter and count all elements
|
|
168
|
+
```
|
|
169
|
+
const count = await repository
|
|
170
|
+
.query()
|
|
171
|
+
.where()
|
|
172
|
+
.select(x => x.id)
|
|
173
|
+
.equal(id)
|
|
174
|
+
.build()
|
|
175
|
+
.count();
|
|
176
|
+
```
|
|
177
|
+
- build a filter and sum a column of all elements
|
|
178
|
+
```
|
|
179
|
+
const sum = await repository
|
|
180
|
+
.query()
|
|
181
|
+
.where()
|
|
182
|
+
.select(x => x.id)
|
|
183
|
+
.equal(id)
|
|
184
|
+
.build()
|
|
185
|
+
.sum(x => x.port);
|
|
186
|
+
```
|
|
187
|
+
- build a filter with greaterThanOrEqual for instance
|
|
188
|
+
```
|
|
189
|
+
const portGreaterThanZero = await repository
|
|
190
|
+
.query()
|
|
191
|
+
.where()
|
|
192
|
+
.openRoundBracket()
|
|
193
|
+
.select(x => x.port)
|
|
194
|
+
.greaterThanOrEqual(0)
|
|
195
|
+
.build()
|
|
196
|
+
.count();
|
|
197
|
+
```
|
|
198
|
+
- same of previous but ordered by a column
|
|
199
|
+
```
|
|
200
|
+
const portGreaterThanZeroOrderedByName = await repository
|
|
201
|
+
.query()
|
|
202
|
+
.where()
|
|
203
|
+
.select(x => x.port)
|
|
204
|
+
.greaterThanOrEqual(0)
|
|
205
|
+
.build()
|
|
206
|
+
.orderBy(x => x.name)
|
|
207
|
+
.execute();
|
|
208
|
+
```
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { IRepository } from "../interfaces/IRepository";
|
|
2
|
+
import { RepositoryEndpoint } from "../models/RepositoryEndpoint";
|
|
3
|
+
import { State } from "../models/State";
|
|
4
|
+
import { RepositorySettings } from "../servicecollection/RepositorySettings";
|
|
5
|
+
import { BatchBuilder } from "./batch/BatchBuilder";
|
|
6
|
+
import { QueryBuilder } from "./query/QueryBuilder";
|
|
7
|
+
export declare class Repository<T, TKey> implements IRepository<T, TKey> {
|
|
8
|
+
private baseUri;
|
|
9
|
+
private settings;
|
|
10
|
+
constructor(baseUri: string | null, settings: RepositorySettings);
|
|
11
|
+
makeRequest<TResponse>(endpoint: RepositoryEndpoint, path: string, method: string, body?: any | null, headers?: HeadersInit): Promise<TResponse>;
|
|
12
|
+
get(key: TKey): Promise<T>;
|
|
13
|
+
insert(key: TKey, value: T): Promise<State<T, TKey>>;
|
|
14
|
+
update(key: TKey, value: T): Promise<State<T, TKey>>;
|
|
15
|
+
exist(key: TKey): Promise<State<T, TKey>>;
|
|
16
|
+
delete(key: TKey): Promise<State<T, TKey>>;
|
|
17
|
+
batch(): BatchBuilder<T, TKey>;
|
|
18
|
+
query(): QueryBuilder<T, TKey>;
|
|
19
|
+
static predicateAsString<T>(predicate: (value: T) => any): string;
|
|
20
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Repository = void 0;
|
|
4
|
+
const RepositoryEndpoint_1 = require("../models/RepositoryEndpoint");
|
|
5
|
+
const BatchBuilder_1 = require("./batch/BatchBuilder");
|
|
6
|
+
const QueryBuilder_1 = require("./query/QueryBuilder");
|
|
7
|
+
class Repository {
|
|
8
|
+
baseUri;
|
|
9
|
+
settings;
|
|
10
|
+
constructor(baseUri, settings) {
|
|
11
|
+
if (settings.uri == null)
|
|
12
|
+
this.baseUri = `${baseUri}${settings.path}`;
|
|
13
|
+
else
|
|
14
|
+
this.baseUri = settings.uri;
|
|
15
|
+
this.settings = settings;
|
|
16
|
+
}
|
|
17
|
+
async makeRequest(endpoint, path, method, body = null, headers = {}) {
|
|
18
|
+
let retry = true;
|
|
19
|
+
let response = null;
|
|
20
|
+
const uri = `${this.baseUri}/${path}`;
|
|
21
|
+
while (retry) {
|
|
22
|
+
response = await fetch(uri, {
|
|
23
|
+
method: method,
|
|
24
|
+
headers: this.settings.enrichHeaders(endpoint, uri, method, headers, body),
|
|
25
|
+
body: body == null ? null : JSON.stringify(body)
|
|
26
|
+
})
|
|
27
|
+
.then(res => {
|
|
28
|
+
const json = res.json();
|
|
29
|
+
return json;
|
|
30
|
+
})
|
|
31
|
+
.catch((err) => {
|
|
32
|
+
retry = this.settings.manageError(endpoint, uri, method, headers, body, err);
|
|
33
|
+
return null;
|
|
34
|
+
})
|
|
35
|
+
.then(res => {
|
|
36
|
+
retry = false;
|
|
37
|
+
return res;
|
|
38
|
+
});
|
|
39
|
+
if (retry)
|
|
40
|
+
await new Promise(f => setTimeout(f, 200));
|
|
41
|
+
}
|
|
42
|
+
return response;
|
|
43
|
+
}
|
|
44
|
+
get(key) {
|
|
45
|
+
if (!this.settings.complexKey) {
|
|
46
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Get, `Get?key=${key}`, 'GET');
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Get, `Get`, 'POST', key, {
|
|
50
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
insert(key, value) {
|
|
55
|
+
if (!this.settings.complexKey) {
|
|
56
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Insert, `Insert?key=${key}`, 'POST', value, {
|
|
57
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Insert, `Insert`, 'POST', {
|
|
62
|
+
value: value,
|
|
63
|
+
key: key
|
|
64
|
+
}, {
|
|
65
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
update(key, value) {
|
|
70
|
+
if (!this.settings.complexKey) {
|
|
71
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Update, `Update?key=${key}`, 'POST', value, {
|
|
72
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Update, `Update`, 'POST', {
|
|
77
|
+
value: value,
|
|
78
|
+
key: key
|
|
79
|
+
}, {
|
|
80
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exist(key) {
|
|
85
|
+
if (!this.settings.complexKey) {
|
|
86
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Exist, `Exist?key=${key}`, 'GET');
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Exist, `Exist`, 'POST', key, {
|
|
90
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
delete(key) {
|
|
95
|
+
if (!this.settings.complexKey) {
|
|
96
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Delete, `Delete?key=${key}`, 'GET');
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
return this.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Delete, `Delete`, 'POST', key, {
|
|
100
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
batch() {
|
|
105
|
+
return new BatchBuilder_1.BatchBuilder(this);
|
|
106
|
+
}
|
|
107
|
+
query() {
|
|
108
|
+
return new QueryBuilder_1.QueryBuilder(this);
|
|
109
|
+
}
|
|
110
|
+
static predicateAsString(predicate) {
|
|
111
|
+
const splittedPredicate = predicate.toString().split('.');
|
|
112
|
+
const variableName = splittedPredicate.slice(-(splittedPredicate.length - 1)).join('.');
|
|
113
|
+
return `_rystem.${variableName}`;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.Repository = Repository;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BatchResults } from "./BatchResults";
|
|
2
|
+
import { Repository } from "../Repository";
|
|
3
|
+
export declare class BatchBuilder<T, TKey> {
|
|
4
|
+
private batch;
|
|
5
|
+
private repository;
|
|
6
|
+
constructor(repository: Repository<T, TKey>);
|
|
7
|
+
addInsert(key: TKey, value: T): BatchBuilder<T, TKey>;
|
|
8
|
+
addUpdate(key: TKey, value: T): BatchBuilder<T, TKey>;
|
|
9
|
+
addDelete(key: TKey): BatchBuilder<T, TKey>;
|
|
10
|
+
execute(): Promise<BatchResults<T, TKey>>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
v: []
|
|
11
|
+
};
|
|
12
|
+
this.repository = repository;
|
|
13
|
+
}
|
|
14
|
+
addInsert(key, value) {
|
|
15
|
+
this.batch.v.push({
|
|
16
|
+
c: 1,
|
|
17
|
+
k: key,
|
|
18
|
+
v: value
|
|
19
|
+
});
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
addUpdate(key, value) {
|
|
23
|
+
this.batch.v.push({
|
|
24
|
+
c: 2,
|
|
25
|
+
k: key,
|
|
26
|
+
v: value
|
|
27
|
+
});
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
addDelete(key) {
|
|
31
|
+
this.batch.v.push({
|
|
32
|
+
c: 4,
|
|
33
|
+
k: key
|
|
34
|
+
});
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
execute() {
|
|
38
|
+
return this.repository.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Batch, `Batch`, 'POST', this.batch, {
|
|
39
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.BatchBuilder = BatchBuilder;
|
|
@@ -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,23 @@
|
|
|
1
|
+
import { Entity } from "../../models/Entity";
|
|
2
|
+
import { Repository } from "../Repository";
|
|
3
|
+
import { WhereBuilder } from "./filter/WhereBuilder";
|
|
4
|
+
export declare class QueryBuilder<T, TKey> {
|
|
5
|
+
private filters;
|
|
6
|
+
private repository;
|
|
7
|
+
constructor(repository: Repository<T, TKey>);
|
|
8
|
+
where(): WhereBuilder<T, TKey>;
|
|
9
|
+
filter(predicate: string): QueryBuilder<T, TKey>;
|
|
10
|
+
top(value: number): QueryBuilder<T, TKey>;
|
|
11
|
+
skip(value: number): QueryBuilder<T, TKey>;
|
|
12
|
+
orderBy(predicate: (value: T) => any): QueryBuilder<T, TKey>;
|
|
13
|
+
orderByDescending(predicate: (value: T) => any): QueryBuilder<T, TKey>;
|
|
14
|
+
thenBy(predicate: (value: T) => any): QueryBuilder<T, TKey>;
|
|
15
|
+
thenByDescending(predicate: (value: T) => any): QueryBuilder<T, TKey>;
|
|
16
|
+
execute(): Promise<Array<Entity<T, TKey>>>;
|
|
17
|
+
count(): Promise<number>;
|
|
18
|
+
max(predicate: (value: T) => any): Promise<number>;
|
|
19
|
+
min(predicate: (value: T) => any): Promise<number>;
|
|
20
|
+
average(predicate: (value: T) => any): Promise<number>;
|
|
21
|
+
sum(predicate: (value: T) => any): Promise<number>;
|
|
22
|
+
private executeOperation;
|
|
23
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
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, {
|
|
71
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
count() {
|
|
75
|
+
return this.executeOperation("Count", "long");
|
|
76
|
+
}
|
|
77
|
+
max(predicate) {
|
|
78
|
+
this.filters.o.push({
|
|
79
|
+
q: FilterOperations_1.FilterOperations.Select,
|
|
80
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
81
|
+
});
|
|
82
|
+
return this.executeOperation("Max", "decimal");
|
|
83
|
+
}
|
|
84
|
+
min(predicate) {
|
|
85
|
+
this.filters.o.push({
|
|
86
|
+
q: FilterOperations_1.FilterOperations.Select,
|
|
87
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
88
|
+
});
|
|
89
|
+
return this.executeOperation("Min", "decimal");
|
|
90
|
+
}
|
|
91
|
+
average(predicate) {
|
|
92
|
+
this.filters.o.push({
|
|
93
|
+
q: FilterOperations_1.FilterOperations.Select,
|
|
94
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
95
|
+
});
|
|
96
|
+
return this.executeOperation("Average", "decimal");
|
|
97
|
+
}
|
|
98
|
+
sum(predicate) {
|
|
99
|
+
this.filters.o.push({
|
|
100
|
+
q: FilterOperations_1.FilterOperations.Select,
|
|
101
|
+
v: `_rystem => ${Repository_1.Repository.predicateAsString(predicate)}`
|
|
102
|
+
});
|
|
103
|
+
return this.executeOperation("Sum", "decimal");
|
|
104
|
+
}
|
|
105
|
+
executeOperation(operation, returnType) {
|
|
106
|
+
return this.repository.makeRequest(RepositoryEndpoint_1.RepositoryEndpoint.Operation, `Operation?op=${operation}&returnType=${returnType}`, 'POST', this.filters, {
|
|
107
|
+
'content-type': 'application/json;charset=UTF-8',
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.QueryBuilder = QueryBuilder;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Entity } from "../../../models/Entity";
|
|
2
|
+
import { QueryBuilder } from "../QueryBuilder";
|
|
3
|
+
export declare class WhereBuilder<T, TKey> {
|
|
4
|
+
private roundBracketsCount;
|
|
5
|
+
private queryBuilder;
|
|
6
|
+
private query;
|
|
7
|
+
constructor(query: QueryBuilder<T, TKey>);
|
|
8
|
+
select(predicate: (value: T) => any): WhereBuilder<T, TKey>;
|
|
9
|
+
private operator;
|
|
10
|
+
private valueAsString;
|
|
11
|
+
equal(value: any | null): WhereBuilder<T, TKey>;
|
|
12
|
+
notEqual(value: any | null): WhereBuilder<T, TKey>;
|
|
13
|
+
greaterThan(value: any | null): WhereBuilder<T, TKey>;
|
|
14
|
+
greaterThanOrEqual(value: any | null): WhereBuilder<T, TKey>;
|
|
15
|
+
lesserThan(value: any | null): WhereBuilder<T, TKey>;
|
|
16
|
+
lesserThanOrEqual(value: any | null): WhereBuilder<T, TKey>;
|
|
17
|
+
startsWith(value: any | null): WhereBuilder<T, TKey>;
|
|
18
|
+
endsWith(value: any | null): WhereBuilder<T, TKey>;
|
|
19
|
+
contains(value: any | null): WhereBuilder<T, TKey>;
|
|
20
|
+
and(): WhereBuilder<T, TKey>;
|
|
21
|
+
or(): WhereBuilder<T, TKey>;
|
|
22
|
+
openRoundBracket(): WhereBuilder<T, TKey>;
|
|
23
|
+
closeRoundBracket(): WhereBuilder<T, TKey>;
|
|
24
|
+
build(): QueryBuilder<T, TKey>;
|
|
25
|
+
execute(): Promise<Array<Entity<T, TKey>>>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WhereBuilder = void 0;
|
|
4
|
+
const Repository_1 = require("../../Repository");
|
|
5
|
+
class WhereBuilder {
|
|
6
|
+
roundBracketsCount;
|
|
7
|
+
queryBuilder;
|
|
8
|
+
query;
|
|
9
|
+
constructor(query) {
|
|
10
|
+
this.roundBracketsCount = 0;
|
|
11
|
+
this.queryBuilder = new Array();
|
|
12
|
+
this.queryBuilder.push("_rystem => ");
|
|
13
|
+
this.query = query;
|
|
14
|
+
}
|
|
15
|
+
select(predicate) {
|
|
16
|
+
this.queryBuilder.push(Repository_1.Repository.predicateAsString(predicate));
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
operator(operation, value) {
|
|
20
|
+
switch (operation) {
|
|
21
|
+
case Operators.Equal:
|
|
22
|
+
this.queryBuilder.push(" == ");
|
|
23
|
+
this.queryBuilder.push(this.valueAsString(value));
|
|
24
|
+
break;
|
|
25
|
+
case Operators.NotEqual:
|
|
26
|
+
this.queryBuilder.push(" != ");
|
|
27
|
+
this.queryBuilder.push(this.valueAsString(value));
|
|
28
|
+
break;
|
|
29
|
+
case Operators.GreaterThan:
|
|
30
|
+
this.queryBuilder.push(" > ");
|
|
31
|
+
this.queryBuilder.push(this.valueAsString(value));
|
|
32
|
+
break;
|
|
33
|
+
case Operators.GreaterThanOrEqual:
|
|
34
|
+
this.queryBuilder.push(" >= ");
|
|
35
|
+
this.queryBuilder.push(this.valueAsString(value));
|
|
36
|
+
break;
|
|
37
|
+
case Operators.LesserThan:
|
|
38
|
+
this.queryBuilder.push(" < ");
|
|
39
|
+
this.queryBuilder.push(this.valueAsString(value));
|
|
40
|
+
break;
|
|
41
|
+
case Operators.LesserThanOrEqual:
|
|
42
|
+
this.queryBuilder.push(" <= ");
|
|
43
|
+
this.queryBuilder.push(this.valueAsString(value));
|
|
44
|
+
break;
|
|
45
|
+
case Operators.Contains:
|
|
46
|
+
this.queryBuilder.push(".Contains(");
|
|
47
|
+
this.queryBuilder.push(this.valueAsString(value));
|
|
48
|
+
this.queryBuilder.push(")");
|
|
49
|
+
break;
|
|
50
|
+
case Operators.StartsWith:
|
|
51
|
+
this.queryBuilder.push(".StartsWith(");
|
|
52
|
+
this.queryBuilder.push(this.valueAsString(value));
|
|
53
|
+
this.queryBuilder.push(")");
|
|
54
|
+
break;
|
|
55
|
+
case Operators.EndsWith:
|
|
56
|
+
this.queryBuilder.push(".EndsWith(");
|
|
57
|
+
this.queryBuilder.push(this.valueAsString(value));
|
|
58
|
+
this.queryBuilder.push(")");
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
valueAsString(v) {
|
|
64
|
+
if (v != null) {
|
|
65
|
+
if (typeof v == 'number') {
|
|
66
|
+
return v.toString();
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
return `"${v.toString()}"`;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else
|
|
73
|
+
return "null";
|
|
74
|
+
}
|
|
75
|
+
equal(value) {
|
|
76
|
+
return this
|
|
77
|
+
.operator(Operators.Equal, value);
|
|
78
|
+
}
|
|
79
|
+
notEqual(value) {
|
|
80
|
+
return this
|
|
81
|
+
.operator(Operators.NotEqual, value);
|
|
82
|
+
}
|
|
83
|
+
greaterThan(value) {
|
|
84
|
+
return this
|
|
85
|
+
.operator(Operators.GreaterThan, value);
|
|
86
|
+
}
|
|
87
|
+
greaterThanOrEqual(value) {
|
|
88
|
+
return this
|
|
89
|
+
.operator(Operators.GreaterThanOrEqual, value);
|
|
90
|
+
}
|
|
91
|
+
lesserThan(value) {
|
|
92
|
+
return this
|
|
93
|
+
.operator(Operators.LesserThan, value);
|
|
94
|
+
}
|
|
95
|
+
lesserThanOrEqual(value) {
|
|
96
|
+
return this
|
|
97
|
+
.operator(Operators.LesserThanOrEqual, value);
|
|
98
|
+
}
|
|
99
|
+
startsWith(value) {
|
|
100
|
+
return this
|
|
101
|
+
.operator(Operators.StartsWith, value);
|
|
102
|
+
}
|
|
103
|
+
endsWith(value) {
|
|
104
|
+
return this
|
|
105
|
+
.operator(Operators.EndsWith, value);
|
|
106
|
+
}
|
|
107
|
+
contains(value) {
|
|
108
|
+
return this
|
|
109
|
+
.operator(Operators.Contains, value);
|
|
110
|
+
}
|
|
111
|
+
and() {
|
|
112
|
+
this.queryBuilder.push(" && ");
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
or() {
|
|
116
|
+
this.queryBuilder.push(" || ");
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
openRoundBracket() {
|
|
120
|
+
this.queryBuilder.push("(");
|
|
121
|
+
this.roundBracketsCount++;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
closeRoundBracket() {
|
|
125
|
+
this.queryBuilder.push(")");
|
|
126
|
+
this.roundBracketsCount--;
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
build() {
|
|
130
|
+
for (let i = 0; i < this.roundBracketsCount; i++) {
|
|
131
|
+
this.queryBuilder.push(")");
|
|
132
|
+
}
|
|
133
|
+
this.roundBracketsCount = 0;
|
|
134
|
+
return this.query.filter(this.queryBuilder.join(''));
|
|
135
|
+
}
|
|
136
|
+
execute() {
|
|
137
|
+
return this.build().execute();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
exports.WhereBuilder = WhereBuilder;
|
|
141
|
+
var Operators;
|
|
142
|
+
(function (Operators) {
|
|
143
|
+
Operators[Operators["Equal"] = 1] = "Equal";
|
|
144
|
+
Operators[Operators["NotEqual"] = 2] = "NotEqual";
|
|
145
|
+
Operators[Operators["GreaterThan"] = 3] = "GreaterThan";
|
|
146
|
+
Operators[Operators["GreaterThanOrEqual"] = 4] = "GreaterThanOrEqual";
|
|
147
|
+
Operators[Operators["LesserThan"] = 5] = "LesserThan";
|
|
148
|
+
Operators[Operators["LesserThanOrEqual"] = 6] = "LesserThanOrEqual";
|
|
149
|
+
Operators[Operators["Contains"] = 7] = "Contains";
|
|
150
|
+
Operators[Operators["StartsWith"] = 8] = "StartsWith";
|
|
151
|
+
Operators[Operators["EndsWith"] = 9] = "EndsWith";
|
|
152
|
+
})(Operators || (Operators = {}));
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { BatchResults } from "./engine/batch/BatchResults";
|
|
2
|
+
export type { ICommand } from "./interfaces/ICommand";
|
|
3
|
+
export type { IQuery } from "./interfaces/IQuery";
|
|
4
|
+
export type { IRepository } from "./interfaces/IRepository";
|
|
5
|
+
export type { Entity } from "./models/Entity";
|
|
6
|
+
export type { State } from "./models/State";
|
|
7
|
+
export { RepositoryEndpoint } from "./models/RepositoryEndpoint";
|
|
8
|
+
export { RepositoryServices } from "./servicecollection/RepositoryServices";
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RepositoryServices = exports.RepositoryEndpoint = void 0;
|
|
4
|
+
var RepositoryEndpoint_1 = require("./models/RepositoryEndpoint");
|
|
5
|
+
Object.defineProperty(exports, "RepositoryEndpoint", { enumerable: true, get: function () { return RepositoryEndpoint_1.RepositoryEndpoint; } });
|
|
6
|
+
var RepositoryServices_1 = require("./servicecollection/RepositoryServices");
|
|
7
|
+
Object.defineProperty(exports, "RepositoryServices", { enumerable: true, get: function () { return RepositoryServices_1.RepositoryServices; } });
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BatchBuilder } from "../engine/batch/BatchBuilder";
|
|
2
|
+
import { State } from "../models/State";
|
|
3
|
+
export interface ICommand<T, TKey> {
|
|
4
|
+
insert(key: TKey, value: T): Promise<State<T, TKey>>;
|
|
5
|
+
update(key: TKey, value: T): Promise<State<T, TKey>>;
|
|
6
|
+
delete(key: TKey): Promise<State<T, TKey>>;
|
|
7
|
+
batch(): BatchBuilder<T, TKey>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RepositoryEndpoint = void 0;
|
|
4
|
+
var RepositoryEndpoint;
|
|
5
|
+
(function (RepositoryEndpoint) {
|
|
6
|
+
RepositoryEndpoint[RepositoryEndpoint["Get"] = 1] = "Get";
|
|
7
|
+
RepositoryEndpoint[RepositoryEndpoint["Exist"] = 2] = "Exist";
|
|
8
|
+
RepositoryEndpoint[RepositoryEndpoint["Query"] = 3] = "Query";
|
|
9
|
+
RepositoryEndpoint[RepositoryEndpoint["Operation"] = 4] = "Operation";
|
|
10
|
+
RepositoryEndpoint[RepositoryEndpoint["Insert"] = 5] = "Insert";
|
|
11
|
+
RepositoryEndpoint[RepositoryEndpoint["Update"] = 6] = "Update";
|
|
12
|
+
RepositoryEndpoint[RepositoryEndpoint["Delete"] = 7] = "Delete";
|
|
13
|
+
RepositoryEndpoint[RepositoryEndpoint["Batch"] = 8] = "Batch";
|
|
14
|
+
})(RepositoryEndpoint || (exports.RepositoryEndpoint = RepositoryEndpoint = {}));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ICommand } from "../interfaces/ICommand";
|
|
2
|
+
import { IQuery } from "../interfaces/IQuery";
|
|
3
|
+
import { IRepository } from "../interfaces/IRepository";
|
|
4
|
+
import { RepositorySettings } from "./RepositorySettings";
|
|
5
|
+
export declare class RepositoryServices {
|
|
6
|
+
private static instance;
|
|
7
|
+
private repositories;
|
|
8
|
+
private baseUri;
|
|
9
|
+
private constructor();
|
|
10
|
+
static Repository<T, TKey>(name: string): IRepository<T, TKey>;
|
|
11
|
+
static Command<T, TKey>(name: string): ICommand<T, TKey>;
|
|
12
|
+
static Query<T, TKey>(name: string): IQuery<T, TKey>;
|
|
13
|
+
private static Instance;
|
|
14
|
+
static Create<T, TKey>(baseUri: string | null): RepositoryServices;
|
|
15
|
+
private AddRepository;
|
|
16
|
+
addRepository<T, TKey>(builder: (x: RepositorySettings) => void): this;
|
|
17
|
+
addCommand<T, TKey>(builder: (x: RepositorySettings) => void): this;
|
|
18
|
+
addQuery<T, TKey>(builder: (x: RepositorySettings) => void): this;
|
|
19
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RepositoryServices = void 0;
|
|
4
|
+
const Repository_1 = require("../engine/Repository");
|
|
5
|
+
const RepositorySettings_1 = require("./RepositorySettings");
|
|
6
|
+
class RepositoryServices {
|
|
7
|
+
static instance;
|
|
8
|
+
repositories;
|
|
9
|
+
baseUri;
|
|
10
|
+
constructor() {
|
|
11
|
+
this.repositories = new Map();
|
|
12
|
+
this.baseUri = null;
|
|
13
|
+
}
|
|
14
|
+
static Repository(name) {
|
|
15
|
+
return RepositoryServices.Instance(name, RepositoryType.Repository);
|
|
16
|
+
}
|
|
17
|
+
static Command(name) {
|
|
18
|
+
return RepositoryServices.Instance(name, RepositoryType.Command);
|
|
19
|
+
}
|
|
20
|
+
static Query(name) {
|
|
21
|
+
return RepositoryServices.Instance(name, RepositoryType.Query);
|
|
22
|
+
}
|
|
23
|
+
static Instance(name, type) {
|
|
24
|
+
if (!RepositoryServices.instance) {
|
|
25
|
+
RepositoryServices.instance = new RepositoryServices();
|
|
26
|
+
}
|
|
27
|
+
if (RepositoryServices.instance.repositories.has(name)) {
|
|
28
|
+
const wrapper = RepositoryServices.instance.repositories.get(name);
|
|
29
|
+
if (wrapper != undefined && wrapper.type == type) {
|
|
30
|
+
if (wrapper.type == RepositoryType.Repository)
|
|
31
|
+
return wrapper.repository;
|
|
32
|
+
else if (wrapper.type == RepositoryType.Command)
|
|
33
|
+
return wrapper.repository;
|
|
34
|
+
else
|
|
35
|
+
return wrapper.repository;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
throw new Error(`${type} with '${name}' has a wrong setup.`);
|
|
39
|
+
}
|
|
40
|
+
static Create(baseUri) {
|
|
41
|
+
if (!RepositoryServices.instance) {
|
|
42
|
+
RepositoryServices.instance = new RepositoryServices();
|
|
43
|
+
}
|
|
44
|
+
RepositoryServices.instance.baseUri = baseUri;
|
|
45
|
+
return RepositoryServices.instance;
|
|
46
|
+
}
|
|
47
|
+
AddRepository(builder, type) {
|
|
48
|
+
const settings = new RepositorySettings_1.RepositorySettings();
|
|
49
|
+
builder(settings);
|
|
50
|
+
if (settings.name == null || settings.name == "")
|
|
51
|
+
throw new Error("Repository needs a name during setup. Please provide in settings a name length greater than or equal to 1.");
|
|
52
|
+
const repository = new Repository_1.Repository(this.baseUri, settings);
|
|
53
|
+
this.repositories.set(settings.name, new RepositoryWrapper(repository, type));
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
addRepository(builder) {
|
|
57
|
+
return this.AddRepository(builder, RepositoryType.Repository);
|
|
58
|
+
}
|
|
59
|
+
addCommand(builder) {
|
|
60
|
+
return this.AddRepository(builder, RepositoryType.Command);
|
|
61
|
+
}
|
|
62
|
+
addQuery(builder) {
|
|
63
|
+
return this.AddRepository(builder, RepositoryType.Query);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.RepositoryServices = RepositoryServices;
|
|
67
|
+
var RepositoryType;
|
|
68
|
+
(function (RepositoryType) {
|
|
69
|
+
RepositoryType[RepositoryType["Repository"] = 1] = "Repository";
|
|
70
|
+
RepositoryType[RepositoryType["Command"] = 2] = "Command";
|
|
71
|
+
RepositoryType[RepositoryType["Query"] = 3] = "Query";
|
|
72
|
+
})(RepositoryType || (RepositoryType = {}));
|
|
73
|
+
class RepositoryWrapper {
|
|
74
|
+
repository;
|
|
75
|
+
type;
|
|
76
|
+
constructor(repository, type) {
|
|
77
|
+
this.repository = repository;
|
|
78
|
+
this.type = type;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RepositoryEndpoint } from "../models/RepositoryEndpoint";
|
|
2
|
+
export declare class RepositorySettings {
|
|
3
|
+
name: string;
|
|
4
|
+
uri: string | null;
|
|
5
|
+
path: string | null;
|
|
6
|
+
complexKey: boolean;
|
|
7
|
+
private headersEnrichers;
|
|
8
|
+
private errorsHandlers;
|
|
9
|
+
constructor();
|
|
10
|
+
addHeadersEnricher(enricher: (endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any) => HeadersInit): this;
|
|
11
|
+
addErrorHandler(handler: (endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any, err: any) => boolean): this;
|
|
12
|
+
enrichHeaders(endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit | undefined, body: any): HeadersInit;
|
|
13
|
+
manageError(endpoint: RepositoryEndpoint, uri: string, method: string, headers: HeadersInit, body: any, err: any): boolean;
|
|
14
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RepositorySettings = void 0;
|
|
4
|
+
class RepositorySettings {
|
|
5
|
+
name;
|
|
6
|
+
uri;
|
|
7
|
+
path;
|
|
8
|
+
complexKey;
|
|
9
|
+
headersEnrichers;
|
|
10
|
+
errorsHandlers;
|
|
11
|
+
constructor() {
|
|
12
|
+
this.name = "";
|
|
13
|
+
this.uri = null;
|
|
14
|
+
this.path = null;
|
|
15
|
+
this.complexKey = false;
|
|
16
|
+
this.headersEnrichers = new Array();
|
|
17
|
+
this.errorsHandlers = new Array();
|
|
18
|
+
}
|
|
19
|
+
addHeadersEnricher(enricher) {
|
|
20
|
+
this.headersEnrichers.push(enricher);
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
addErrorHandler(handler) {
|
|
24
|
+
this.errorsHandlers.push(handler);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
enrichHeaders(endpoint, uri, method, headers, body) {
|
|
28
|
+
const requestHeaders = new Headers();
|
|
29
|
+
const setHeaders = (currentHeaders) => {
|
|
30
|
+
let forcedHeaders = currentHeaders;
|
|
31
|
+
for (let header in currentHeaders) {
|
|
32
|
+
if (!requestHeaders.has(header)) {
|
|
33
|
+
requestHeaders.set(header, forcedHeaders[header]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
if (headers != undefined) {
|
|
38
|
+
setHeaders(headers);
|
|
39
|
+
}
|
|
40
|
+
for (let enricher of this.headersEnrichers) {
|
|
41
|
+
setHeaders(enricher(endpoint, uri, method, requestHeaders, body));
|
|
42
|
+
}
|
|
43
|
+
return requestHeaders;
|
|
44
|
+
}
|
|
45
|
+
manageError(endpoint, uri, method, headers, body, err) {
|
|
46
|
+
let retry = this.errorsHandlers.length > 0;
|
|
47
|
+
for (let handler of this.errorsHandlers)
|
|
48
|
+
retry &&= handler(endpoint, uri, method, headers, body, err);
|
|
49
|
+
return retry;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.RepositorySettings = RepositorySettings;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rystem.repository.client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"author": "Alessandro Rapiti <alessandro.rapiti44@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "It can simplify the integration with rystem repository server.",
|
|
@@ -8,12 +8,5 @@
|
|
|
8
8
|
"types": "./lib/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"/lib"
|
|
11
|
-
]
|
|
12
|
-
"devDependencies": {
|
|
13
|
-
"@types/node": "^14.17.2",
|
|
14
|
-
"nodemon": "^2.0.22",
|
|
15
|
-
"ts-node": "^8.8.1",
|
|
16
|
-
"tslint": "^6.1.0",
|
|
17
|
-
"typescript": "^4.1.2"
|
|
18
|
-
}
|
|
11
|
+
]
|
|
19
12
|
}
|