@lobb-js/core 0.27.0 → 0.29.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 +2 -1
- package/src/api/collections/CollectionService.ts +8 -0
- package/src/api/collections/collectionStore.ts +66 -0
- package/src/config/ConfigManager.ts +9 -3
- package/src/events/coreEvents/index.ts +23 -0
- package/src/extension/ExtensionSystem.ts +1 -0
- package/src/types/Extension.ts +2 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lobb-js/core",
|
|
3
3
|
"license": "UNLICENSED",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.29.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"zod-to-ts": "^1.2.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
+
"lucide-svelte": "latest",
|
|
40
41
|
"@types/lodash": "^4.17.0",
|
|
41
42
|
"@types/nunjucks": "^3.2.6",
|
|
42
43
|
"@types/pg": "^8.11.0",
|
|
@@ -59,6 +59,8 @@ export class CollectionService {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
await Lobb.instance.eventSystem.emit("core.service.preCreateOne", props);
|
|
63
|
+
|
|
62
64
|
return Lobb.instance.collectionStore.createOne(props as unknown as P<"createOne">) as Promise<T["output"]>;
|
|
63
65
|
}
|
|
64
66
|
|
|
@@ -73,6 +75,8 @@ export class CollectionService {
|
|
|
73
75
|
}
|
|
74
76
|
}
|
|
75
77
|
|
|
78
|
+
await Lobb.instance.eventSystem.emit("core.service.preUpdateOne", props);
|
|
79
|
+
|
|
76
80
|
return Lobb.instance.collectionStore.updateOne(props as unknown as P<"updateOne">) as Promise<T["output"]>;
|
|
77
81
|
}
|
|
78
82
|
|
|
@@ -87,6 +91,8 @@ export class CollectionService {
|
|
|
87
91
|
}
|
|
88
92
|
}
|
|
89
93
|
|
|
94
|
+
await Lobb.instance.eventSystem.emit("core.service.preDeleteOne", props);
|
|
95
|
+
|
|
90
96
|
return Lobb.instance.collectionStore.deleteOne(props as unknown as P<"deleteOne">) as Promise<T["output"]>;
|
|
91
97
|
}
|
|
92
98
|
|
|
@@ -129,6 +135,8 @@ export class CollectionService {
|
|
|
129
135
|
}
|
|
130
136
|
}
|
|
131
137
|
|
|
138
|
+
await Lobb.instance.eventSystem.emit("core.service.preDeleteMany", props);
|
|
139
|
+
|
|
132
140
|
return Lobb.instance.collectionStore.deleteMany(props as unknown as P<"deleteMany">) as Promise<T["output"]>;
|
|
133
141
|
}
|
|
134
142
|
|
|
@@ -41,6 +41,16 @@ export class CollectionStore {
|
|
|
41
41
|
context?: HonoContext;
|
|
42
42
|
client?: PoolClient;
|
|
43
43
|
}): Promise<ExposedManyServiceOutput> {
|
|
44
|
+
// Resolve children_of + parent_id into a regular filter before proceeding.
|
|
45
|
+
// This allows fetching related records (fk, m2m, polymorphic) without the caller
|
|
46
|
+
// needing to know the relation type or junction details.
|
|
47
|
+
if ((params as any)?.children_of !== undefined) {
|
|
48
|
+
const { children_of, parent_id, ...rest } = params as any;
|
|
49
|
+
const childFilter = await this.resolveChildrenOf(collectionName, children_of, parent_id);
|
|
50
|
+
if (childFilter === null) return { data: [], meta: { totalCount: 0 } };
|
|
51
|
+
params = { ...rest, filter: { ...rest.filter, ...childFilter } };
|
|
52
|
+
}
|
|
53
|
+
|
|
44
54
|
return await beginTransaction(
|
|
45
55
|
async (client) => {
|
|
46
56
|
const eventResult = await Lobb.instance.eventSystem.emit(
|
|
@@ -1222,6 +1232,62 @@ export class CollectionStore {
|
|
|
1222
1232
|
}
|
|
1223
1233
|
}
|
|
1224
1234
|
|
|
1235
|
+
private async resolveChildrenOf(
|
|
1236
|
+
collectionName: string,
|
|
1237
|
+
childrenOf: string,
|
|
1238
|
+
parentId: any,
|
|
1239
|
+
): Promise<Record<string, any> | null> {
|
|
1240
|
+
const children = Lobb.instance.configManager.getCollectionChildren(childrenOf);
|
|
1241
|
+
const child = children.find((c) => c.collection === collectionName);
|
|
1242
|
+
|
|
1243
|
+
if (!child) {
|
|
1244
|
+
throw new LobbError({
|
|
1245
|
+
code: "BAD_REQUEST",
|
|
1246
|
+
message: `"${collectionName}" is not a child of "${childrenOf}"`,
|
|
1247
|
+
});
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
if (child.type === "fk") {
|
|
1251
|
+
return { [child.field!]: parentId };
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
if (child.type === "m2m") {
|
|
1255
|
+
const junction = await this.findAll({
|
|
1256
|
+
collectionName: child.junction!,
|
|
1257
|
+
params: { filter: { [child.junction_field!]: parentId } },
|
|
1258
|
+
});
|
|
1259
|
+
const ids = junction.data.map((r: any) => r[child.target_field!]);
|
|
1260
|
+
if (ids.length === 0) return null;
|
|
1261
|
+
return { id: { $in: ids } };
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
if (child.type === "polymorphic") {
|
|
1265
|
+
const relations = Lobb.instance.configManager.config.relations ?? [];
|
|
1266
|
+
const polyRelation = relations.find(
|
|
1267
|
+
(r: any) =>
|
|
1268
|
+
r.type === "polymorphic" &&
|
|
1269
|
+
r.from.collection === collectionName &&
|
|
1270
|
+
Array.isArray(r.to) &&
|
|
1271
|
+
r.to.includes(childrenOf),
|
|
1272
|
+
) as any;
|
|
1273
|
+
if (!polyRelation) {
|
|
1274
|
+
throw new LobbError({
|
|
1275
|
+
code: "BAD_REQUEST",
|
|
1276
|
+
message: `No polymorphic relation found between "${collectionName}" and "${childrenOf}"`,
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
return {
|
|
1280
|
+
[polyRelation.from.collection_field]: childrenOf,
|
|
1281
|
+
[polyRelation.from.id_field]: parentId,
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
throw new LobbError({
|
|
1286
|
+
code: "BAD_REQUEST",
|
|
1287
|
+
message: `Unknown relation type: ${child.type}`,
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1225
1291
|
private async handleSingletonCollection(
|
|
1226
1292
|
collectionName: string,
|
|
1227
1293
|
context?: HonoContext,
|
|
@@ -425,10 +425,10 @@ export class ConfigManager {
|
|
|
425
425
|
return [];
|
|
426
426
|
}
|
|
427
427
|
|
|
428
|
-
public getCollectionChildren(collectionName: string): { type: string; collection: string; field?: string }[] {
|
|
428
|
+
public getCollectionChildren(collectionName: string): { type: string; collection: string; field?: string; junction?: string; junction_field?: string; target_field?: string }[] {
|
|
429
429
|
const allRelations = this.config.relations ?? [];
|
|
430
430
|
const allCollections = this.config.collections;
|
|
431
|
-
const children: { type: string; collection: string; field?: string }[] = [];
|
|
431
|
+
const children: { type: string; collection: string; field?: string; junction?: string; junction_field?: string; target_field?: string }[] = [];
|
|
432
432
|
const seenJunctions = new Set<string>();
|
|
433
433
|
|
|
434
434
|
for (const relation of allRelations) {
|
|
@@ -446,7 +446,13 @@ export class ConfigManager {
|
|
|
446
446
|
(r as RegularRelation).from.field !== reg.from.field,
|
|
447
447
|
) as RegularRelation | undefined;
|
|
448
448
|
if (otherSide) {
|
|
449
|
-
children.push({
|
|
449
|
+
children.push({
|
|
450
|
+
type: "m2m",
|
|
451
|
+
collection: otherSide.to.collection,
|
|
452
|
+
junction: childCollection,
|
|
453
|
+
junction_field: reg.from.field,
|
|
454
|
+
target_field: otherSide.from.field,
|
|
455
|
+
});
|
|
450
456
|
}
|
|
451
457
|
} else {
|
|
452
458
|
children.push({ type: "fk", collection: childCollection, field: reg.from.field });
|
|
@@ -123,6 +123,29 @@ export function getCoreEvents(): Event[] {
|
|
|
123
123
|
inputSchema: z.object({}),
|
|
124
124
|
outputSchema: z.object({}),
|
|
125
125
|
});
|
|
126
|
+
// service pre-events
|
|
127
|
+
events.push({
|
|
128
|
+
name: "core.service.preCreateOne",
|
|
129
|
+
inputSchema: z.object({}),
|
|
130
|
+
outputSchema: z.object({}),
|
|
131
|
+
});
|
|
132
|
+
events.push({
|
|
133
|
+
name: "core.service.preUpdateOne",
|
|
134
|
+
inputSchema: z.object({}),
|
|
135
|
+
outputSchema: z.object({}),
|
|
136
|
+
});
|
|
137
|
+
events.push({
|
|
138
|
+
name: "core.service.preDeleteOne",
|
|
139
|
+
inputSchema: z.object({}),
|
|
140
|
+
outputSchema: z.object({}),
|
|
141
|
+
});
|
|
142
|
+
events.push({
|
|
143
|
+
name: "core.service.preDeleteMany",
|
|
144
|
+
inputSchema: z.object({}),
|
|
145
|
+
outputSchema: z.object({}),
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// service override events
|
|
126
149
|
events.push({
|
|
127
150
|
name: "core.service.findAll.override",
|
|
128
151
|
inputSchema: z.object({}),
|
package/src/types/Extension.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { OpenAPIV3_1 } from "openapi-types";
|
|
|
5
5
|
import type { RelationsConfig } from "./config/relations.ts";
|
|
6
6
|
import type { Lobb } from "../Lobb.ts";
|
|
7
7
|
import type { Workflow } from "../workflows/WorkflowSystem.ts";
|
|
8
|
+
import * as Icons from "lucide-svelte";
|
|
8
9
|
|
|
9
10
|
interface OpenApiProperty {
|
|
10
11
|
paths: OpenAPIV3_1.Document["paths"];
|
|
@@ -17,6 +18,7 @@ export interface Dashboard {
|
|
|
17
18
|
|
|
18
19
|
interface ExtensionBase {
|
|
19
20
|
name: string;
|
|
21
|
+
icon?: keyof typeof Icons;
|
|
20
22
|
workflows?: Workflow[];
|
|
21
23
|
migrations?: Migrations;
|
|
22
24
|
init?: (lobb: Lobb) => Promise<void>;
|