@lobb-js/core 0.13.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/package.json +48 -0
- package/src/Lobb.ts +150 -0
- package/src/LobbError.ts +105 -0
- package/src/TypesGenerator.ts +11 -0
- package/src/api/WebServer.ts +126 -0
- package/src/api/collections/CollectionControllers.ts +485 -0
- package/src/api/collections/CollectionService.ts +162 -0
- package/src/api/collections/collectionRoutes.ts +105 -0
- package/src/api/collections/collectionStore.ts +647 -0
- package/src/api/collections/transactions.ts +166 -0
- package/src/api/collections/utils.ts +73 -0
- package/src/api/errorHandler.ts +73 -0
- package/src/api/events/index.ts +129 -0
- package/src/api/meta/route.ts +66 -0
- package/src/api/meta/service.ts +163 -0
- package/src/api/middlewares.ts +71 -0
- package/src/api/openApiRoute.ts +1017 -0
- package/src/api/schema/SchemaService.ts +71 -0
- package/src/api/schema/schemaRoutes.ts +13 -0
- package/src/config/ConfigManager.ts +252 -0
- package/src/config/validations.ts +49 -0
- package/src/coreCollections/collectionsCollection.ts +56 -0
- package/src/coreCollections/index.ts +14 -0
- package/src/coreCollections/migrationsCollection.ts +36 -0
- package/src/coreCollections/queryCollection.ts +26 -0
- package/src/coreCollections/workflowsCollection.ts +73 -0
- package/src/coreDbSetup/index.ts +72 -0
- package/src/coreMigrations/index.ts +3 -0
- package/src/database/DatabaseService.ts +44 -0
- package/src/database/DatabaseSyncManager.ts +173 -0
- package/src/database/MigrationsManager.ts +95 -0
- package/src/database/drivers/MongoDriver.ts +750 -0
- package/src/database/drivers/pgDriver/PGDriver.ts +655 -0
- package/src/database/drivers/pgDriver/QueryBuilder.ts +474 -0
- package/src/database/drivers/pgDriver/utils.ts +6 -0
- package/src/events/EventSystem.ts +191 -0
- package/src/events/coreEvents/index.ts +218 -0
- package/src/events/studioEvents/index.ts +32 -0
- package/src/extension/ExtensionSystem.ts +236 -0
- package/src/extension/dashboardRoute.ts +35 -0
- package/src/fields/ArrayField.ts +33 -0
- package/src/fields/BoolField.ts +34 -0
- package/src/fields/DateField.ts +13 -0
- package/src/fields/DateTimeField.ts +13 -0
- package/src/fields/DecimalField.ts +13 -0
- package/src/fields/FieldUtils.ts +56 -0
- package/src/fields/FloatField.ts +13 -0
- package/src/fields/IntegerField.ts +13 -0
- package/src/fields/LongField.ts +13 -0
- package/src/fields/ObjectField.ts +15 -0
- package/src/fields/StringField.ts +13 -0
- package/src/fields/TextField.ts +13 -0
- package/src/fields/TimeField.ts +13 -0
- package/src/index.ts +53 -0
- package/src/studio/Studio.ts +108 -0
- package/src/types/CollectionControllers.ts +15 -0
- package/src/types/DatabaseDriver.ts +115 -0
- package/src/types/Extension.ts +46 -0
- package/src/types/Field.ts +29 -0
- package/src/types/apiSchema.ts +12 -0
- package/src/types/collectionServiceSchema.ts +18 -0
- package/src/types/config/collectionFields.ts +85 -0
- package/src/types/config/collectionsConfig.ts +50 -0
- package/src/types/config/config.ts +66 -0
- package/src/types/config/relations.ts +17 -0
- package/src/types/filterSchema.ts +88 -0
- package/src/types/index.ts +38 -0
- package/src/types/migrations.ts +12 -0
- package/src/types/websockets.ts +34 -0
- package/src/types/workflows/processors.ts +1 -0
- package/src/utils/lockCollectionToObject.ts +204 -0
- package/src/utils/utils.ts +310 -0
- package/src/workflows/WorkflowSystem.ts +182 -0
- package/src/workflows/coreWorkflows/collectionsTable/index.ts +118 -0
- package/src/workflows/coreWorkflows/index.ts +18 -0
- package/src/workflows/coreWorkflows/processors/postOperationsWorkflows.ts +46 -0
- package/src/workflows/coreWorkflows/processors/preOperationsWorkflows.ts +27 -0
- package/src/workflows/coreWorkflows/processors/processorForDB.ts +13 -0
- package/src/workflows/coreWorkflows/processors/processors/processor.ts +23 -0
- package/src/workflows/coreWorkflows/processors/processors/processorsFunctions.ts +47 -0
- package/src/workflows/coreWorkflows/processors/utils.ts +102 -0
- package/src/workflows/coreWorkflows/processors/validator/validator.ts +19 -0
- package/src/workflows/coreWorkflows/processors/validator/validatorsFunction.ts +52 -0
- package/src/workflows/coreWorkflows/queryCoreWorkflows.ts +31 -0
- package/src/workflows/coreWorkflows/utilsCoreWorkflows.ts +40 -0
- package/src/workflows/coreWorkflows/workflowsCollection/workflowsCollectionWorkflows.ts +101 -0
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
import type { Context } from "hono";
|
|
2
|
+
import type { CollectionControllers } from "../../types/index.ts";
|
|
3
|
+
import type { ExposedManyServiceOutput } from "./CollectionService.ts";
|
|
4
|
+
|
|
5
|
+
import qs from "qs";
|
|
6
|
+
import { LobbError } from "../../LobbError.ts";
|
|
7
|
+
import { Lobb } from "../../Lobb.ts";
|
|
8
|
+
import { transactions } from "./transactions.ts";
|
|
9
|
+
|
|
10
|
+
export const collectionControllers: CollectionControllers = {
|
|
11
|
+
async createOne(c: Context) {
|
|
12
|
+
const collectionName = param(c, "collectionName");
|
|
13
|
+
const body = await Lobb.instance.utils.getRequestBody(c);
|
|
14
|
+
|
|
15
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.controllers.createOne.override")) {
|
|
16
|
+
const result = (await Lobb.instance.eventSystem.emit("core.controllers.createOne.override", {
|
|
17
|
+
collectionName: collectionName,
|
|
18
|
+
body: body,
|
|
19
|
+
context: c,
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
if (result) {
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// this needs to be prior to the body data check
|
|
28
|
+
(await Lobb.instance.eventSystem.emit("core.controllers.preCreateOne", {
|
|
29
|
+
collectionName: collectionName,
|
|
30
|
+
body: body,
|
|
31
|
+
context: c,
|
|
32
|
+
})) as ExposedManyServiceOutput;
|
|
33
|
+
|
|
34
|
+
if (!body.data) {
|
|
35
|
+
throw new LobbError({
|
|
36
|
+
code: "BAD_REQUEST",
|
|
37
|
+
message: "The request payload must include a (data) property",
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (Array.isArray(body.data)) {
|
|
42
|
+
throw new LobbError({
|
|
43
|
+
code: "BAD_REQUEST",
|
|
44
|
+
message:
|
|
45
|
+
"The (data) property must be an object, not an array. Use the /many endpoint to create multiple records.",
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const result = await Lobb.instance.collectionService.createOne({
|
|
50
|
+
collectionName,
|
|
51
|
+
context: c,
|
|
52
|
+
data: body.data,
|
|
53
|
+
triggeredBy: "API",
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return c.json(result, 201);
|
|
57
|
+
},
|
|
58
|
+
async findAll(c: Context) {
|
|
59
|
+
const collectionName = param(c, "collectionName");
|
|
60
|
+
|
|
61
|
+
if (Lobb.instance.configManager.isCollectionSingleton(collectionName)) {
|
|
62
|
+
throw new LobbError({
|
|
63
|
+
code: "BAD_REQUEST",
|
|
64
|
+
message: `This collection is a singleton. (findAll) is not supported.`,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let params;
|
|
69
|
+
if (c.req.method === "POST") {
|
|
70
|
+
params = await Lobb.instance.utils.getRequestBody(c);
|
|
71
|
+
} else {
|
|
72
|
+
params = parseUrlQuery(c.req.url);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
await Lobb.instance.eventSystem.emit(
|
|
76
|
+
"core.controllers.preFindAll",
|
|
77
|
+
{
|
|
78
|
+
collectionName: collectionName,
|
|
79
|
+
context: c,
|
|
80
|
+
},
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.controllers.findAll.override")) {
|
|
84
|
+
const result = (await Lobb.instance.eventSystem.emit("core.controllers.findAll.override", {
|
|
85
|
+
collectionName,
|
|
86
|
+
context: c,
|
|
87
|
+
}));
|
|
88
|
+
|
|
89
|
+
if (result) {
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let result = await Lobb.instance.collectionService.findAll({
|
|
95
|
+
collectionName,
|
|
96
|
+
context: c,
|
|
97
|
+
params,
|
|
98
|
+
triggeredBy: "API",
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const eventResult = await Lobb.instance.eventSystem.emit(
|
|
102
|
+
"core.controllers.findAll",
|
|
103
|
+
{
|
|
104
|
+
collectionName: collectionName,
|
|
105
|
+
response: result,
|
|
106
|
+
context: c,
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
if (eventResult) {
|
|
111
|
+
result = eventResult.response;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return c.json(result);
|
|
115
|
+
},
|
|
116
|
+
async findOne(c: Context) {
|
|
117
|
+
const collectionName = param(c, "collectionName");
|
|
118
|
+
const id = param(c, "id");
|
|
119
|
+
|
|
120
|
+
if (Lobb.instance.configManager.isCollectionSingleton(collectionName)) {
|
|
121
|
+
throw new LobbError({
|
|
122
|
+
code: "BAD_REQUEST",
|
|
123
|
+
message: `This collection is a singleton. (findOne) is not supported.`,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.controllers.findOne.override")) {
|
|
128
|
+
const result = (await Lobb.instance.eventSystem.emit("core.controllers.findOne.override", {
|
|
129
|
+
collectionName: collectionName,
|
|
130
|
+
id: id,
|
|
131
|
+
context: c,
|
|
132
|
+
}));
|
|
133
|
+
|
|
134
|
+
if (result) {
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const eventResult = await Lobb.instance.eventSystem.emit(
|
|
140
|
+
"core.controllers.preFindOne",
|
|
141
|
+
{
|
|
142
|
+
collectionName: collectionName,
|
|
143
|
+
id: id,
|
|
144
|
+
context: c,
|
|
145
|
+
},
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const result = await Lobb.instance.collectionService.findOne({
|
|
149
|
+
collectionName,
|
|
150
|
+
context: c,
|
|
151
|
+
id: id,
|
|
152
|
+
triggeredBy: "API",
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
if (!result.data) {
|
|
156
|
+
throw new LobbError({
|
|
157
|
+
code: "NOT_FOUND",
|
|
158
|
+
message: `The record is not found`,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return c.json(result);
|
|
163
|
+
},
|
|
164
|
+
async updateOne(c: Context) {
|
|
165
|
+
const collectionName = param(c, "collectionName");
|
|
166
|
+
const id = param(c, "id");
|
|
167
|
+
const body = await Lobb.instance.utils.getRequestBody(c);
|
|
168
|
+
|
|
169
|
+
if (Lobb.instance.configManager.isCollectionSingleton(collectionName)) {
|
|
170
|
+
throw new LobbError({
|
|
171
|
+
code: "BAD_REQUEST",
|
|
172
|
+
message:
|
|
173
|
+
`This collection is a singleton. (updateOne) is not supported.`,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (!body.data) {
|
|
178
|
+
throw new LobbError({
|
|
179
|
+
code: "BAD_REQUEST",
|
|
180
|
+
message: "The request payload must include a (data) property",
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (body.data.id) {
|
|
185
|
+
throw new LobbError({
|
|
186
|
+
code: "FORBIDDEN",
|
|
187
|
+
message: `Updating the ID proprety is not allowed.`,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.controllers.updateOne.override")) {
|
|
192
|
+
const result = (await Lobb.instance.eventSystem.emit("core.controllers.updateOne.override", {
|
|
193
|
+
collectionName,
|
|
194
|
+
id,
|
|
195
|
+
body,
|
|
196
|
+
context: c,
|
|
197
|
+
}));
|
|
198
|
+
|
|
199
|
+
if (result) {
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const result = await Lobb.instance.collectionService.updateOne({
|
|
205
|
+
collectionName,
|
|
206
|
+
context: c,
|
|
207
|
+
id,
|
|
208
|
+
data: body.data,
|
|
209
|
+
triggeredBy: "API",
|
|
210
|
+
});
|
|
211
|
+
return c.json(result, 200);
|
|
212
|
+
},
|
|
213
|
+
async deleteOne(c: Context) {
|
|
214
|
+
const id = param(c, "id");
|
|
215
|
+
const collectionName = param(c, "collectionName");
|
|
216
|
+
const force = c.req.query("force") !== undefined;
|
|
217
|
+
|
|
218
|
+
if (Lobb.instance.configManager.isCollectionSingleton(collectionName)) {
|
|
219
|
+
throw new LobbError({
|
|
220
|
+
code: "BAD_REQUEST",
|
|
221
|
+
message:
|
|
222
|
+
`This collection is a singleton. (deleteOne) is not supported.`,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.controllers.deleteOne.override")) {
|
|
227
|
+
const result = (await Lobb.instance.eventSystem.emit("core.controllers.deleteOne.override", {
|
|
228
|
+
collectionName,
|
|
229
|
+
id,
|
|
230
|
+
force,
|
|
231
|
+
context: c,
|
|
232
|
+
}));
|
|
233
|
+
|
|
234
|
+
if (result) {
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const result = await Lobb.instance.collectionService.deleteOne({
|
|
240
|
+
collectionName,
|
|
241
|
+
context: c,
|
|
242
|
+
id,
|
|
243
|
+
force,
|
|
244
|
+
triggeredBy: "API",
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
return c.json(result);
|
|
248
|
+
},
|
|
249
|
+
async deleteMany(c: Context) {
|
|
250
|
+
const collectionName = param(c, "collectionName");
|
|
251
|
+
const force = c.req.query("force") !== undefined;
|
|
252
|
+
const body = await Lobb.instance.utils.getRequestBody(c);
|
|
253
|
+
|
|
254
|
+
if (Lobb.instance.configManager.isCollectionSingleton(collectionName)) {
|
|
255
|
+
throw new LobbError({
|
|
256
|
+
code: "BAD_REQUEST",
|
|
257
|
+
message:
|
|
258
|
+
`This collection is a singleton. (deleteMany) is not supported.`,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
(await Lobb.instance.eventSystem.emit("core.controllers.preDeleteMany", {
|
|
263
|
+
collectionName: collectionName,
|
|
264
|
+
body: body,
|
|
265
|
+
context: c,
|
|
266
|
+
})) as ExposedManyServiceOutput;
|
|
267
|
+
|
|
268
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.controllers.deleteMany.override")) {
|
|
269
|
+
const result = (await Lobb.instance.eventSystem.emit("core.controllers.deleteMany.override", {
|
|
270
|
+
collectionName,
|
|
271
|
+
body,
|
|
272
|
+
force,
|
|
273
|
+
context: c,
|
|
274
|
+
}));
|
|
275
|
+
|
|
276
|
+
if (result) {
|
|
277
|
+
return result;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (!body.filter) {
|
|
282
|
+
throw new LobbError({
|
|
283
|
+
code: "BAD_REQUEST",
|
|
284
|
+
message: "You need to provide the filter object",
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const result = await Lobb.instance.collectionService.deleteMany({
|
|
289
|
+
collectionName,
|
|
290
|
+
context: c,
|
|
291
|
+
filter: body.filter,
|
|
292
|
+
force,
|
|
293
|
+
triggeredBy: "API",
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
return c.json(result);
|
|
297
|
+
},
|
|
298
|
+
async updateMany(c: Context) {
|
|
299
|
+
const collectionName = param(c, "collectionName");
|
|
300
|
+
const body = await Lobb.instance.utils.getRequestBody(c);
|
|
301
|
+
|
|
302
|
+
if (Lobb.instance.configManager.isCollectionSingleton(collectionName)) {
|
|
303
|
+
throw new LobbError({
|
|
304
|
+
code: "BAD_REQUEST",
|
|
305
|
+
message:
|
|
306
|
+
`This collection is a singleton. (updateMany) is not supported.`,
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (!body.filter) {
|
|
311
|
+
throw new LobbError({
|
|
312
|
+
code: "BAD_REQUEST",
|
|
313
|
+
message: "You need to provide the filter object",
|
|
314
|
+
});
|
|
315
|
+
} else if (!body.data) {
|
|
316
|
+
throw new LobbError({
|
|
317
|
+
code: "BAD_REQUEST",
|
|
318
|
+
message: "You need to provide the data object",
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.controllers.updateMany.override")) {
|
|
323
|
+
const result = (await Lobb.instance.eventSystem.emit("core.controllers.updateMany.override", {
|
|
324
|
+
collectionName,
|
|
325
|
+
body,
|
|
326
|
+
context: c,
|
|
327
|
+
}));
|
|
328
|
+
|
|
329
|
+
if (result) {
|
|
330
|
+
return result;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const result = await Lobb.instance.collectionService.updateMany({
|
|
335
|
+
collectionName,
|
|
336
|
+
context: c,
|
|
337
|
+
data: body.data,
|
|
338
|
+
filter: body.filter,
|
|
339
|
+
triggeredBy: "API",
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
return c.json(result);
|
|
343
|
+
},
|
|
344
|
+
async createMany(c: Context) {
|
|
345
|
+
const collectionName = param(c, "collectionName");
|
|
346
|
+
const body = await Lobb.instance.utils.getRequestBody(c);
|
|
347
|
+
|
|
348
|
+
if (Lobb.instance.configManager.isCollectionSingleton(collectionName)) {
|
|
349
|
+
throw new LobbError({
|
|
350
|
+
code: "BAD_REQUEST",
|
|
351
|
+
message:
|
|
352
|
+
`This collection is a singleton. (createMany) is not supported.`,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (!body.data) {
|
|
357
|
+
throw new LobbError({
|
|
358
|
+
code: "BAD_REQUEST",
|
|
359
|
+
message: "You need to provide the data object",
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (!Array.isArray(body.data)) {
|
|
364
|
+
throw new LobbError({
|
|
365
|
+
code: "BAD_REQUEST",
|
|
366
|
+
message: "The data object needs to be an array",
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.controllers.createMany.override")) {
|
|
371
|
+
const result = (await Lobb.instance.eventSystem.emit("core.controllers.createMany.override", {
|
|
372
|
+
collectionName,
|
|
373
|
+
body,
|
|
374
|
+
context: c,
|
|
375
|
+
}));
|
|
376
|
+
|
|
377
|
+
if (result) {
|
|
378
|
+
return result;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const result = await Lobb.instance.collectionService.createMany({
|
|
383
|
+
collectionName,
|
|
384
|
+
context: c,
|
|
385
|
+
data: body.data,
|
|
386
|
+
triggeredBy: "API",
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
return c.json(result);
|
|
390
|
+
},
|
|
391
|
+
async readSingleton(c: Context) {
|
|
392
|
+
const collectionName = param(c, "collectionName");
|
|
393
|
+
|
|
394
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.controllers.readSingleton.override")) {
|
|
395
|
+
const result = (await Lobb.instance.eventSystem.emit("core.controllers.readSingleton.override", {
|
|
396
|
+
collectionName,
|
|
397
|
+
context: c,
|
|
398
|
+
}));
|
|
399
|
+
|
|
400
|
+
if (result) {
|
|
401
|
+
return result;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const result = await Lobb.instance.collectionService.readSingleton({
|
|
406
|
+
collectionName,
|
|
407
|
+
context: c,
|
|
408
|
+
triggeredBy: "API",
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
return c.json(result);
|
|
412
|
+
},
|
|
413
|
+
async updateSingleton(c: Context) {
|
|
414
|
+
const collectionName = param(c, "collectionName");
|
|
415
|
+
const body = await Lobb.instance.utils.getRequestBody(c);
|
|
416
|
+
|
|
417
|
+
if (!body?.data) {
|
|
418
|
+
throw new LobbError({
|
|
419
|
+
code: "BAD_REQUEST",
|
|
420
|
+
message: "You need to provide the data object",
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.controllers.updateSingleton.override")) {
|
|
425
|
+
const result = (await Lobb.instance.eventSystem.emit("core.controllers.updateSingleton.override", {
|
|
426
|
+
collectionName,
|
|
427
|
+
body,
|
|
428
|
+
context: c,
|
|
429
|
+
}));
|
|
430
|
+
|
|
431
|
+
if (result) {
|
|
432
|
+
return result;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const result = await Lobb.instance.collectionService.updateSingleton({
|
|
437
|
+
collectionName,
|
|
438
|
+
context: c,
|
|
439
|
+
data: body.data,
|
|
440
|
+
triggeredBy: "API",
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
return c.json(result);
|
|
444
|
+
},
|
|
445
|
+
async transactions(c: Context) {
|
|
446
|
+
const rollback: boolean = c.req.query("rollback") !== undefined;
|
|
447
|
+
const body = await Lobb.instance.utils.getRequestBody(c);
|
|
448
|
+
|
|
449
|
+
if (!body) {
|
|
450
|
+
throw new LobbError({
|
|
451
|
+
code: "BAD_REQUEST",
|
|
452
|
+
message: "You need to provide the body",
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (!Array.isArray(body)) {
|
|
457
|
+
throw new LobbError({
|
|
458
|
+
code: "BAD_REQUEST",
|
|
459
|
+
message: "The body should be an array",
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const result = await transactions({
|
|
464
|
+
body,
|
|
465
|
+
rollback,
|
|
466
|
+
context: c,
|
|
467
|
+
triggeredBy: "API",
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
return c.json(result);
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
export function param(c: Context, paramName: string) {
|
|
475
|
+
return c.req.param(paramName) ?? c.get(paramName);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
export function parseUrlQuery(url: string) {
|
|
479
|
+
const arr = url.split("?");
|
|
480
|
+
if (arr[1]) {
|
|
481
|
+
const queryString = arr[1];
|
|
482
|
+
return qs.parse(queryString);
|
|
483
|
+
}
|
|
484
|
+
return {};
|
|
485
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { MassReturn } from "../../types/index.ts";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ExposedServiceOutput,
|
|
5
|
+
ExposedManyServiceOutput,
|
|
6
|
+
TriggeredBy,
|
|
7
|
+
CollectionStore,
|
|
8
|
+
} from "./collectionStore.ts";
|
|
9
|
+
import { Lobb } from "../../Lobb.ts";
|
|
10
|
+
|
|
11
|
+
export type { ExposedServiceOutput, ExposedManyServiceOutput, TriggeredBy };
|
|
12
|
+
|
|
13
|
+
export type ServiceOp<TInput = unknown, TOutput = unknown> = {
|
|
14
|
+
input: TInput;
|
|
15
|
+
output: TOutput;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type P<M extends keyof CollectionStore> = Parameters<CollectionStore[M]>[0];
|
|
19
|
+
type WithCollection<T> = T & { collectionName: string };
|
|
20
|
+
type Without<M extends keyof CollectionStore> = Omit<P<M>, "collectionName">;
|
|
21
|
+
|
|
22
|
+
export class CollectionService {
|
|
23
|
+
public async findAll<
|
|
24
|
+
T extends ServiceOp = ServiceOp<Without<"findAll">, ExposedManyServiceOutput>,
|
|
25
|
+
>(props: WithCollection<T["input"]>): Promise<T["output"]> {
|
|
26
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.service.findAll.override")) {
|
|
27
|
+
const result = (await Lobb.instance.eventSystem.emit("core.service.findAll.override", props));
|
|
28
|
+
|
|
29
|
+
if (result) {
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return Lobb.instance.collectionStore.findAll(props) as Promise<T["output"]>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public async findOne<
|
|
38
|
+
T extends ServiceOp = ServiceOp<Without<"findOne">, ExposedServiceOutput>,
|
|
39
|
+
>(props: WithCollection<T["input"]>): Promise<T["output"]> {
|
|
40
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.service.findOne.override")) {
|
|
41
|
+
const result = (await Lobb.instance.eventSystem.emit("core.service.findOne.override", props));
|
|
42
|
+
|
|
43
|
+
if (result) {
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return Lobb.instance.collectionStore.findOne(props as unknown as P<"findOne">) as Promise<T["output"]>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public async createOne<
|
|
52
|
+
T extends ServiceOp = ServiceOp<Without<"createOne">, ExposedServiceOutput>,
|
|
53
|
+
>(props: WithCollection<T["input"]>): Promise<T["output"]> {
|
|
54
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.service.createOne.override")) {
|
|
55
|
+
const result = (await Lobb.instance.eventSystem.emit("core.service.createOne.override", props));
|
|
56
|
+
|
|
57
|
+
if (result) {
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return Lobb.instance.collectionStore.createOne(props as unknown as P<"createOne">) as Promise<T["output"]>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public async updateOne<
|
|
66
|
+
T extends ServiceOp = ServiceOp<Without<"updateOne">, ExposedServiceOutput>,
|
|
67
|
+
>(props: WithCollection<T["input"]>): Promise<T["output"]> {
|
|
68
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.service.updateOne.override")) {
|
|
69
|
+
const result = (await Lobb.instance.eventSystem.emit("core.service.updateOne.override", props));
|
|
70
|
+
|
|
71
|
+
if (result) {
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return Lobb.instance.collectionStore.updateOne(props as unknown as P<"updateOne">) as Promise<T["output"]>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public async deleteOne<
|
|
80
|
+
T extends ServiceOp = ServiceOp<Without<"deleteOne">, ExposedServiceOutput>,
|
|
81
|
+
>(props: WithCollection<T["input"]>): Promise<T["output"]> {
|
|
82
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.service.deleteOne.override")) {
|
|
83
|
+
const result = (await Lobb.instance.eventSystem.emit("core.service.deleteOne.override", props));
|
|
84
|
+
|
|
85
|
+
if (result) {
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return Lobb.instance.collectionStore.deleteOne(props as unknown as P<"deleteOne">) as Promise<T["output"]>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public async readSingleton<
|
|
94
|
+
T extends ServiceOp = ServiceOp<Without<"readSingleton">, ExposedServiceOutput>,
|
|
95
|
+
>(props: WithCollection<T["input"]>): Promise<T["output"]> {
|
|
96
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.service.readSingleton.override")) {
|
|
97
|
+
const result = (await Lobb.instance.eventSystem.emit("core.service.readSingleton.override", props));
|
|
98
|
+
|
|
99
|
+
if (result) {
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return Lobb.instance.collectionStore.readSingleton(props as unknown as P<"readSingleton">) as Promise<T["output"]>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public async updateSingleton<
|
|
108
|
+
T extends ServiceOp = ServiceOp<Without<"updateSingleton">, ExposedServiceOutput>,
|
|
109
|
+
>(props: WithCollection<T["input"]>): Promise<T["output"]> {
|
|
110
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.service.updateSingleton.override")) {
|
|
111
|
+
const result = (await Lobb.instance.eventSystem.emit("core.service.updateSingleton.override", props));
|
|
112
|
+
|
|
113
|
+
if (result) {
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return Lobb.instance.collectionStore.updateSingleton(props as unknown as P<"updateSingleton">) as Promise<T["output"]>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public async deleteMany<
|
|
122
|
+
T extends ServiceOp = ServiceOp<Without<"deleteMany">, MassReturn>,
|
|
123
|
+
>(props: WithCollection<T["input"]>): Promise<T["output"]> {
|
|
124
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.service.deleteMany.override")) {
|
|
125
|
+
const result = (await Lobb.instance.eventSystem.emit("core.service.deleteMany.override", props));
|
|
126
|
+
|
|
127
|
+
if (result) {
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return Lobb.instance.collectionStore.deleteMany(props as unknown as P<"deleteMany">) as Promise<T["output"]>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public async updateMany<
|
|
136
|
+
T extends ServiceOp = ServiceOp<Without<"updateMany">, MassReturn>,
|
|
137
|
+
>(props: WithCollection<T["input"]>): Promise<T["output"]> {
|
|
138
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.service.updateMany.override")) {
|
|
139
|
+
const result = (await Lobb.instance.eventSystem.emit("core.service.updateMany.override", props));
|
|
140
|
+
|
|
141
|
+
if (result) {
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return Lobb.instance.collectionStore.updateMany(props as unknown as P<"updateMany">) as Promise<T["output"]>;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public async createMany<
|
|
150
|
+
T extends ServiceOp = ServiceOp<Without<"createMany">, MassReturn>,
|
|
151
|
+
>(props: WithCollection<T["input"]>): Promise<T["output"]> {
|
|
152
|
+
if (Lobb.instance.eventSystem.eventHasSubscribers("core.service.createMany.override")) {
|
|
153
|
+
const result = (await Lobb.instance.eventSystem.emit("core.service.createMany.override", props));
|
|
154
|
+
|
|
155
|
+
if (result) {
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return Lobb.instance.collectionStore.createMany(props as unknown as P<"createMany">) as Promise<T["output"]>;
|
|
161
|
+
}
|
|
162
|
+
}
|