@stndrds/schema 0.1.0-alpha.14
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/dist/chunk-3ABIOLKO.mjs +4624 -0
- package/dist/chunk-3IRX2BDY.mjs +3970 -0
- package/dist/chunk-4OTLPWSP.mjs +4448 -0
- package/dist/chunk-54ODQBUN.mjs +3867 -0
- package/dist/chunk-6HIA3FR2.mjs +4230 -0
- package/dist/chunk-6P3XHNLD.mjs +4171 -0
- package/dist/chunk-7LJECNAS.mjs +4167 -0
- package/dist/chunk-A2WNLUKV.mjs +4475 -0
- package/dist/chunk-AA3234RM.mjs +4475 -0
- package/dist/chunk-ADNXDLLD.mjs +3136 -0
- package/dist/chunk-B4DCDQUI.mjs +4695 -0
- package/dist/chunk-B4PSXY6I.mjs +4527 -0
- package/dist/chunk-BJHJBZCM.mjs +4228 -0
- package/dist/chunk-C3ZWTO6G.mjs +4474 -0
- package/dist/chunk-CLOGNKDD.mjs +4040 -0
- package/dist/chunk-EW4V63PA.mjs +3053 -0
- package/dist/chunk-FR2LCO6R.mjs +2485 -0
- package/dist/chunk-GJSF2RKL.mjs +2786 -0
- package/dist/chunk-GO5NPP2X.mjs +4726 -0
- package/dist/chunk-H3ZYJUDS.mjs +3050 -0
- package/dist/chunk-IDDLSFSK.mjs +4230 -0
- package/dist/chunk-JRADUJQ7.mjs +4726 -0
- package/dist/chunk-NEDR6N7R.mjs +3118 -0
- package/dist/chunk-NESPRMDF.mjs +2482 -0
- package/dist/chunk-NWFGRJBW.mjs +4612 -0
- package/dist/chunk-NXJ57GSN.mjs +2918 -0
- package/dist/chunk-O4C5FCTS.mjs +3961 -0
- package/dist/chunk-O4PRB27Q.mjs +2513 -0
- package/dist/chunk-OJDFNUCR.mjs +4463 -0
- package/dist/chunk-OSEKVBHC.mjs +4627 -0
- package/dist/chunk-QX4U6U6K.mjs +2484 -0
- package/dist/chunk-RMDQ6LKV.mjs +3046 -0
- package/dist/chunk-SBXKDATH.mjs +2673 -0
- package/dist/chunk-SKMP3AP3.mjs +2918 -0
- package/dist/chunk-STMS7WZD.mjs +2623 -0
- package/dist/chunk-T3T6PTFG.mjs +3057 -0
- package/dist/chunk-TPW72RAH.mjs +4624 -0
- package/dist/chunk-TUQEGNGV.mjs +2534 -0
- package/dist/chunk-TVKFS3YN.mjs +2449 -0
- package/dist/chunk-TZTH3BHG.mjs +3110 -0
- package/dist/chunk-W6MU3XPG.mjs +4612 -0
- package/dist/chunk-WL7Z3YFR.mjs +4603 -0
- package/dist/chunk-XNVVYHH3.mjs +4602 -0
- package/dist/chunk-XTA3WY64.mjs +2918 -0
- package/dist/chunk-YV7DEZFZ.mjs +2528 -0
- package/dist/chunk-ZLJPJND6.mjs +4474 -0
- package/dist/chunk-ZNZXTCEX.mjs +4234 -0
- package/dist/index.d.mts +1222 -0
- package/dist/index.d.ts +1222 -0
- package/dist/index.js +5320 -0
- package/dist/index.mjs +635 -0
- package/dist/runtime-2iKarXtl.d.mts +1924 -0
- package/dist/runtime-2iKarXtl.d.ts +1924 -0
- package/dist/runtime-4l2ddHZ7.d.mts +2242 -0
- package/dist/runtime-4l2ddHZ7.d.ts +2243 -0
- package/dist/runtime-8hKS6zAv.d.mts +2226 -0
- package/dist/runtime-8hKS6zAv.d.ts +2226 -0
- package/dist/runtime-B6yjtoR3.d.mts +4142 -0
- package/dist/runtime-B6yjtoR3.d.ts +4142 -0
- package/dist/runtime-BGCShrZB.d.mts +2001 -0
- package/dist/runtime-BGCShrZB.d.ts +2001 -0
- package/dist/runtime-BRW4NGAk.d.mts +1668 -0
- package/dist/runtime-BRW4NGAk.d.ts +1668 -0
- package/dist/runtime-BnfBr314.d.mts +1683 -0
- package/dist/runtime-BnfBr314.d.ts +1683 -0
- package/dist/runtime-BrqhiJUF.d.mts +4151 -0
- package/dist/runtime-BrqhiJUF.d.ts +4151 -0
- package/dist/runtime-Bvj8c-BE.d.mts +4277 -0
- package/dist/runtime-Bvj8c-BE.d.ts +4277 -0
- package/dist/runtime-C03qT4qU.d.mts +4267 -0
- package/dist/runtime-C03qT4qU.d.ts +4267 -0
- package/dist/runtime-CaCZ7mSf.d.mts +2974 -0
- package/dist/runtime-CaCZ7mSf.d.ts +2974 -0
- package/dist/runtime-CaIzNX0Z.d.mts +4091 -0
- package/dist/runtime-CaIzNX0Z.d.ts +4091 -0
- package/dist/runtime-ChPTgmLP.d.mts +4320 -0
- package/dist/runtime-ChPTgmLP.d.ts +4320 -0
- package/dist/runtime-CnImZ1Vv.d.mts +4267 -0
- package/dist/runtime-CnImZ1Vv.d.ts +4267 -0
- package/dist/runtime-Cp-B26Nj.d.mts +2890 -0
- package/dist/runtime-Cp-B26Nj.d.ts +2890 -0
- package/dist/runtime-Cq8jfk3c.d.mts +4277 -0
- package/dist/runtime-Cq8jfk3c.d.ts +4277 -0
- package/dist/runtime-CqDFXhhP.d.mts +1685 -0
- package/dist/runtime-CqDFXhhP.d.ts +1685 -0
- package/dist/runtime-CqtpZLdL.d.mts +4280 -0
- package/dist/runtime-CqtpZLdL.d.ts +4280 -0
- package/dist/runtime-D-4DblaZ.d.mts +4147 -0
- package/dist/runtime-D-4DblaZ.d.ts +4147 -0
- package/dist/runtime-D59sSqNl.d.mts +4146 -0
- package/dist/runtime-D59sSqNl.d.ts +4146 -0
- package/dist/runtime-DEfPk3wT.d.mts +1840 -0
- package/dist/runtime-DEfPk3wT.d.ts +1840 -0
- package/dist/runtime-DGV-7vES.d.mts +1685 -0
- package/dist/runtime-DGV-7vES.d.ts +1685 -0
- package/dist/runtime-DNacghqc.d.mts +3971 -0
- package/dist/runtime-DNacghqc.d.ts +3971 -0
- package/dist/runtime-DPLsUHYK.d.mts +3977 -0
- package/dist/runtime-DPLsUHYK.d.ts +3977 -0
- package/dist/runtime-DTxCw60F.d.mts +2973 -0
- package/dist/runtime-DTxCw60F.d.ts +2973 -0
- package/dist/runtime-DUXJ-zNS.d.mts +2788 -0
- package/dist/runtime-DUXJ-zNS.d.ts +2788 -0
- package/dist/runtime-DbljInhV.d.mts +2237 -0
- package/dist/runtime-DbljInhV.d.ts +2237 -0
- package/dist/runtime-DcH3y6Yu.d.mts +4272 -0
- package/dist/runtime-DcH3y6Yu.d.ts +4272 -0
- package/dist/runtime-DdBFz85g.d.mts +4229 -0
- package/dist/runtime-DdBFz85g.d.ts +4229 -0
- package/dist/runtime-DjMWhjkm.d.mts +4321 -0
- package/dist/runtime-DjMWhjkm.d.ts +4321 -0
- package/dist/runtime-Djz3fOBE.d.mts +2078 -0
- package/dist/runtime-Djz3fOBE.d.ts +2078 -0
- package/dist/runtime-Dli8g3Aq.d.mts +2082 -0
- package/dist/runtime-Dli8g3Aq.d.ts +2082 -0
- package/dist/runtime-a8J378im.d.mts +4123 -0
- package/dist/runtime-a8J378im.d.ts +4123 -0
- package/dist/runtime-dp3rb3Lf.d.mts +2968 -0
- package/dist/runtime-dp3rb3Lf.d.ts +2968 -0
- package/dist/runtime-iWXlSN_H.d.mts +1683 -0
- package/dist/runtime-iWXlSN_H.d.ts +1683 -0
- package/dist/runtime-yMql0npY.d.mts +4321 -0
- package/dist/runtime-yMql0npY.d.ts +4321 -0
- package/dist/runtime.d.mts +3 -0
- package/dist/runtime.d.ts +3 -0
- package/dist/runtime.js +3626 -0
- package/dist/runtime.mjs +48 -0
- package/package.json +58 -0
|
@@ -0,0 +1,3136 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
function generateId() {
|
|
3
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
4
|
+
return crypto.randomUUID();
|
|
5
|
+
}
|
|
6
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
7
|
+
const r = Math.random() * 16 | 0;
|
|
8
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
9
|
+
return v.toString(16);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
function generatePrefixedId(prefix) {
|
|
13
|
+
return `${prefix}_${generateId()}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/runtime/mock-adapter.ts
|
|
17
|
+
function createMockObjectsRepository(stores) {
|
|
18
|
+
return {
|
|
19
|
+
findById(id) {
|
|
20
|
+
return Promise.resolve(stores.objects.get(id) ?? null);
|
|
21
|
+
},
|
|
22
|
+
findByName(tenantId, name) {
|
|
23
|
+
for (const obj of stores.objects.values()) {
|
|
24
|
+
if (obj.tenantId === tenantId && obj.name === name) {
|
|
25
|
+
return Promise.resolve(obj);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return Promise.resolve(null);
|
|
29
|
+
},
|
|
30
|
+
findSystemByName(name) {
|
|
31
|
+
for (const obj of stores.objects.values()) {
|
|
32
|
+
if (obj.system && obj.name === name) {
|
|
33
|
+
return Promise.resolve(obj);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return Promise.resolve(null);
|
|
37
|
+
},
|
|
38
|
+
create(data) {
|
|
39
|
+
const obj = {
|
|
40
|
+
id: generateId(),
|
|
41
|
+
tenantId: data.tenantId,
|
|
42
|
+
name: data.name,
|
|
43
|
+
label: data.label,
|
|
44
|
+
description: data.description,
|
|
45
|
+
icon: data.icon,
|
|
46
|
+
system: data.system ?? false,
|
|
47
|
+
metadata: data.metadata,
|
|
48
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
49
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
50
|
+
};
|
|
51
|
+
stores.objects.set(obj.id, obj);
|
|
52
|
+
return Promise.resolve(obj);
|
|
53
|
+
},
|
|
54
|
+
update(id, data) {
|
|
55
|
+
const existing = stores.objects.get(id);
|
|
56
|
+
if (!existing) {
|
|
57
|
+
return Promise.reject(new Error(`Object ${id} not found`));
|
|
58
|
+
}
|
|
59
|
+
const updated = {
|
|
60
|
+
...existing,
|
|
61
|
+
...data,
|
|
62
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
63
|
+
};
|
|
64
|
+
stores.objects.set(id, updated);
|
|
65
|
+
return Promise.resolve(updated);
|
|
66
|
+
},
|
|
67
|
+
delete(id) {
|
|
68
|
+
stores.objects.delete(id);
|
|
69
|
+
return Promise.resolve();
|
|
70
|
+
},
|
|
71
|
+
list(tenantId) {
|
|
72
|
+
const results = Array.from(stores.objects.values()).filter(
|
|
73
|
+
(obj) => obj.tenantId === tenantId
|
|
74
|
+
);
|
|
75
|
+
return Promise.resolve(results);
|
|
76
|
+
},
|
|
77
|
+
upsert(data) {
|
|
78
|
+
for (const obj of stores.objects.values()) {
|
|
79
|
+
if (obj.system && obj.name === data.name) {
|
|
80
|
+
const updated = {
|
|
81
|
+
...obj,
|
|
82
|
+
label: data.label,
|
|
83
|
+
description: data.description,
|
|
84
|
+
icon: data.icon,
|
|
85
|
+
metadata: data.metadata,
|
|
86
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
87
|
+
};
|
|
88
|
+
stores.objects.set(obj.id, updated);
|
|
89
|
+
return Promise.resolve(updated);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const newObj = {
|
|
93
|
+
id: generateId(),
|
|
94
|
+
tenantId: data.tenantId,
|
|
95
|
+
name: data.name,
|
|
96
|
+
label: data.label,
|
|
97
|
+
description: data.description,
|
|
98
|
+
icon: data.icon,
|
|
99
|
+
system: data.system,
|
|
100
|
+
metadata: data.metadata,
|
|
101
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
102
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
103
|
+
};
|
|
104
|
+
stores.objects.set(newObj.id, newObj);
|
|
105
|
+
return Promise.resolve(newObj);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function createMockAttributesRepository(stores) {
|
|
110
|
+
return {
|
|
111
|
+
findById(id) {
|
|
112
|
+
return Promise.resolve(stores.attributes.get(id) ?? null);
|
|
113
|
+
},
|
|
114
|
+
findByObjectId(objectId) {
|
|
115
|
+
const results = Array.from(stores.attributes.values()).filter(
|
|
116
|
+
(attr) => attr.objectId === objectId
|
|
117
|
+
);
|
|
118
|
+
return Promise.resolve(results);
|
|
119
|
+
},
|
|
120
|
+
create(data) {
|
|
121
|
+
const attr = {
|
|
122
|
+
id: generateId(),
|
|
123
|
+
objectId: data.objectId,
|
|
124
|
+
name: data.name,
|
|
125
|
+
label: data.label,
|
|
126
|
+
type: data.type,
|
|
127
|
+
description: data.description,
|
|
128
|
+
icon: data.icon,
|
|
129
|
+
system: data.system ?? false,
|
|
130
|
+
required: data.required ?? false,
|
|
131
|
+
unique: data.unique ?? false,
|
|
132
|
+
order: data.order ?? 0,
|
|
133
|
+
hidden: data.hidden,
|
|
134
|
+
archived: data.archived,
|
|
135
|
+
deprecated: data.deprecated,
|
|
136
|
+
defaultValue: data.defaultValue,
|
|
137
|
+
config: data.config ?? {},
|
|
138
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
139
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
140
|
+
};
|
|
141
|
+
stores.attributes.set(attr.id, attr);
|
|
142
|
+
return Promise.resolve(attr);
|
|
143
|
+
},
|
|
144
|
+
update(id, data) {
|
|
145
|
+
const existing = stores.attributes.get(id);
|
|
146
|
+
if (!existing) {
|
|
147
|
+
return Promise.reject(new Error(`Attribute ${id} not found`));
|
|
148
|
+
}
|
|
149
|
+
const updated = {
|
|
150
|
+
...existing,
|
|
151
|
+
...data,
|
|
152
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
153
|
+
};
|
|
154
|
+
stores.attributes.set(id, updated);
|
|
155
|
+
return Promise.resolve(updated);
|
|
156
|
+
},
|
|
157
|
+
delete(id) {
|
|
158
|
+
stores.attributes.delete(id);
|
|
159
|
+
return Promise.resolve();
|
|
160
|
+
},
|
|
161
|
+
deleteByNames(objectId, excludeNames) {
|
|
162
|
+
let count = 0;
|
|
163
|
+
for (const [id, attr] of stores.attributes.entries()) {
|
|
164
|
+
if (attr.objectId === objectId && !excludeNames.includes(attr.name)) {
|
|
165
|
+
stores.attributes.delete(id);
|
|
166
|
+
count++;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return Promise.resolve(count);
|
|
170
|
+
},
|
|
171
|
+
upsert(data) {
|
|
172
|
+
for (const attr of stores.attributes.values()) {
|
|
173
|
+
if (attr.objectId === data.objectId && attr.name === data.name) {
|
|
174
|
+
const updated = {
|
|
175
|
+
...attr,
|
|
176
|
+
label: data.label ?? attr.label,
|
|
177
|
+
type: data.type ?? attr.type,
|
|
178
|
+
description: data.description ?? attr.description,
|
|
179
|
+
icon: data.icon ?? attr.icon,
|
|
180
|
+
system: data.system ?? attr.system,
|
|
181
|
+
required: data.required ?? attr.required,
|
|
182
|
+
unique: data.unique ?? attr.unique,
|
|
183
|
+
order: data.order ?? attr.order,
|
|
184
|
+
hidden: data.hidden ?? attr.hidden,
|
|
185
|
+
archived: data.archived ?? attr.archived,
|
|
186
|
+
deprecated: data.deprecated ?? attr.deprecated,
|
|
187
|
+
defaultValue: data.defaultValue ?? attr.defaultValue,
|
|
188
|
+
config: data.config ?? attr.config,
|
|
189
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
190
|
+
};
|
|
191
|
+
stores.attributes.set(attr.id, updated);
|
|
192
|
+
return Promise.resolve(updated);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return this.create(data);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function createMockUserProfilesRepository(stores) {
|
|
200
|
+
return {
|
|
201
|
+
findById(id) {
|
|
202
|
+
return Promise.resolve(stores.userProfiles.get(id) ?? null);
|
|
203
|
+
},
|
|
204
|
+
findByAuthId(authId) {
|
|
205
|
+
for (const profile of stores.userProfiles.values()) {
|
|
206
|
+
if (profile.authId === authId) {
|
|
207
|
+
return Promise.resolve(profile);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return Promise.resolve(null);
|
|
211
|
+
},
|
|
212
|
+
findByEmail(tenantId, email) {
|
|
213
|
+
for (const profile of stores.userProfiles.values()) {
|
|
214
|
+
if (profile.tenantId === tenantId && profile.email === email) {
|
|
215
|
+
return Promise.resolve(profile);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return Promise.resolve(null);
|
|
219
|
+
},
|
|
220
|
+
create(data) {
|
|
221
|
+
const profile = {
|
|
222
|
+
id: generateId(),
|
|
223
|
+
tenantId: data.tenantId,
|
|
224
|
+
authId: data.authId,
|
|
225
|
+
email: data.email,
|
|
226
|
+
firstName: data.firstName,
|
|
227
|
+
lastName: data.lastName,
|
|
228
|
+
avatarUrl: data.avatarUrl,
|
|
229
|
+
role: data.role ?? "member",
|
|
230
|
+
status: data.status ?? "active",
|
|
231
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
232
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
233
|
+
};
|
|
234
|
+
stores.userProfiles.set(profile.id, profile);
|
|
235
|
+
return Promise.resolve(profile);
|
|
236
|
+
},
|
|
237
|
+
update(id, data) {
|
|
238
|
+
const existing = stores.userProfiles.get(id);
|
|
239
|
+
if (!existing) {
|
|
240
|
+
return Promise.reject(new Error(`UserProfile ${id} not found`));
|
|
241
|
+
}
|
|
242
|
+
const updated = {
|
|
243
|
+
...existing,
|
|
244
|
+
...data,
|
|
245
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
246
|
+
};
|
|
247
|
+
stores.userProfiles.set(id, updated);
|
|
248
|
+
return Promise.resolve(updated);
|
|
249
|
+
},
|
|
250
|
+
delete(id) {
|
|
251
|
+
stores.userProfiles.delete(id);
|
|
252
|
+
return Promise.resolve();
|
|
253
|
+
},
|
|
254
|
+
list(tenantId, options) {
|
|
255
|
+
let results = Array.from(stores.userProfiles.values()).filter((p) => p.tenantId === tenantId);
|
|
256
|
+
if (options?.limit) {
|
|
257
|
+
results = results.slice(options.offset ?? 0, (options.offset ?? 0) + options.limit);
|
|
258
|
+
}
|
|
259
|
+
return Promise.resolve(results);
|
|
260
|
+
},
|
|
261
|
+
updateLastLogin(id) {
|
|
262
|
+
const profile = stores.userProfiles.get(id);
|
|
263
|
+
if (profile) {
|
|
264
|
+
profile.lastLoginAt = /* @__PURE__ */ new Date();
|
|
265
|
+
stores.userProfiles.set(id, profile);
|
|
266
|
+
}
|
|
267
|
+
return Promise.resolve();
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
function createMockFilesRepository(stores) {
|
|
272
|
+
return {
|
|
273
|
+
findById(id) {
|
|
274
|
+
const file2 = stores.files.get(id);
|
|
275
|
+
if (file2?.deletedAt) return Promise.resolve(null);
|
|
276
|
+
return Promise.resolve(file2 ?? null);
|
|
277
|
+
},
|
|
278
|
+
create(data) {
|
|
279
|
+
const file2 = {
|
|
280
|
+
id: generateId(),
|
|
281
|
+
tenantId: data.tenantId,
|
|
282
|
+
name: data.name,
|
|
283
|
+
originalName: data.originalName,
|
|
284
|
+
mimeType: data.mimeType,
|
|
285
|
+
size: data.size,
|
|
286
|
+
storageProvider: data.storageProvider,
|
|
287
|
+
storagePath: data.storagePath,
|
|
288
|
+
storageBucket: data.storageBucket,
|
|
289
|
+
url: data.url,
|
|
290
|
+
uploadedBy: data.uploadedBy,
|
|
291
|
+
folderPath: data.folderPath,
|
|
292
|
+
tags: data.tags,
|
|
293
|
+
visibility: data.visibility ?? "private",
|
|
294
|
+
allowedUsers: data.allowedUsers,
|
|
295
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
296
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
297
|
+
};
|
|
298
|
+
stores.files.set(file2.id, file2);
|
|
299
|
+
return Promise.resolve(file2);
|
|
300
|
+
},
|
|
301
|
+
update(id, data) {
|
|
302
|
+
const existing = stores.files.get(id);
|
|
303
|
+
if (!existing) {
|
|
304
|
+
return Promise.reject(new Error(`File ${id} not found`));
|
|
305
|
+
}
|
|
306
|
+
const updated = {
|
|
307
|
+
...existing,
|
|
308
|
+
...data,
|
|
309
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
310
|
+
};
|
|
311
|
+
stores.files.set(id, updated);
|
|
312
|
+
return Promise.resolve(updated);
|
|
313
|
+
},
|
|
314
|
+
delete(id) {
|
|
315
|
+
const file2 = stores.files.get(id);
|
|
316
|
+
if (file2) {
|
|
317
|
+
file2.deletedAt = /* @__PURE__ */ new Date();
|
|
318
|
+
stores.files.set(id, file2);
|
|
319
|
+
}
|
|
320
|
+
return Promise.resolve();
|
|
321
|
+
},
|
|
322
|
+
hardDelete(id) {
|
|
323
|
+
stores.files.delete(id);
|
|
324
|
+
return Promise.resolve();
|
|
325
|
+
},
|
|
326
|
+
list(tenantId, options) {
|
|
327
|
+
let results = Array.from(stores.files.values()).filter(
|
|
328
|
+
(f) => f.tenantId === tenantId && !f.deletedAt
|
|
329
|
+
);
|
|
330
|
+
if (options?.mimeType) {
|
|
331
|
+
results = results.filter((f) => f.mimeType === options.mimeType);
|
|
332
|
+
}
|
|
333
|
+
if (options?.limit) {
|
|
334
|
+
results = results.slice(options.offset ?? 0, (options.offset ?? 0) + options.limit);
|
|
335
|
+
}
|
|
336
|
+
return Promise.resolve(results);
|
|
337
|
+
},
|
|
338
|
+
findByFolder(tenantId, folderPath) {
|
|
339
|
+
const results = Array.from(stores.files.values()).filter(
|
|
340
|
+
(f) => f.tenantId === tenantId && f.folderPath === folderPath && !f.deletedAt
|
|
341
|
+
);
|
|
342
|
+
return Promise.resolve(results);
|
|
343
|
+
},
|
|
344
|
+
findByUploader(uploadedBy) {
|
|
345
|
+
const results = Array.from(stores.files.values()).filter(
|
|
346
|
+
(f) => f.uploadedBy === uploadedBy && !f.deletedAt
|
|
347
|
+
);
|
|
348
|
+
return Promise.resolve(results);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function createMockObjectRecordsRepository(stores) {
|
|
353
|
+
return {
|
|
354
|
+
findById(id) {
|
|
355
|
+
const internal = stores.objectRecords.get(id);
|
|
356
|
+
if (!internal) return Promise.resolve(null);
|
|
357
|
+
const { tenantId: _, ...record } = internal;
|
|
358
|
+
return Promise.resolve(record);
|
|
359
|
+
},
|
|
360
|
+
create(data) {
|
|
361
|
+
const internal = {
|
|
362
|
+
id: generateId(),
|
|
363
|
+
tenantId: data.tenantId,
|
|
364
|
+
objectId: data.objectId,
|
|
365
|
+
completionStatus: data.completionStatus,
|
|
366
|
+
values: data.data,
|
|
367
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
368
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
369
|
+
};
|
|
370
|
+
stores.objectRecords.set(internal.id, internal);
|
|
371
|
+
const { tenantId: _, ...record } = internal;
|
|
372
|
+
return Promise.resolve(record);
|
|
373
|
+
},
|
|
374
|
+
update(id, data) {
|
|
375
|
+
const existing = stores.objectRecords.get(id);
|
|
376
|
+
if (!existing) {
|
|
377
|
+
return Promise.reject(new Error(`ObjectRecord ${id} not found`));
|
|
378
|
+
}
|
|
379
|
+
const { __completionStatus, ...valueData } = data;
|
|
380
|
+
const updated = {
|
|
381
|
+
...existing,
|
|
382
|
+
completionStatus: __completionStatus ?? existing.completionStatus,
|
|
383
|
+
values: { ...existing.values, ...valueData },
|
|
384
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
385
|
+
};
|
|
386
|
+
stores.objectRecords.set(id, updated);
|
|
387
|
+
const { tenantId: _, ...record } = updated;
|
|
388
|
+
return Promise.resolve(record);
|
|
389
|
+
},
|
|
390
|
+
delete(id) {
|
|
391
|
+
stores.objectRecords.delete(id);
|
|
392
|
+
return Promise.resolve();
|
|
393
|
+
},
|
|
394
|
+
list(tenantId, objectId, options) {
|
|
395
|
+
let results = Array.from(stores.objectRecords.values()).filter(
|
|
396
|
+
(r) => r.tenantId === tenantId && r.objectId === objectId
|
|
397
|
+
);
|
|
398
|
+
const total = results.length;
|
|
399
|
+
if (options?.limit) {
|
|
400
|
+
results = results.slice(options.offset ?? 0, (options.offset ?? 0) + options.limit);
|
|
401
|
+
}
|
|
402
|
+
const records = results.map(({ tenantId: _, ...r }) => r);
|
|
403
|
+
return Promise.resolve({ records, total });
|
|
404
|
+
},
|
|
405
|
+
search(tenantId, objectId, query, options) {
|
|
406
|
+
const lowerQuery = query.toLowerCase();
|
|
407
|
+
let results = Array.from(stores.objectRecords.values()).filter((r) => {
|
|
408
|
+
if (r.tenantId !== tenantId || r.objectId !== objectId) return false;
|
|
409
|
+
return Object.values(r.values).some(
|
|
410
|
+
(val) => String(val).toLowerCase().includes(lowerQuery)
|
|
411
|
+
);
|
|
412
|
+
});
|
|
413
|
+
const total = results.length;
|
|
414
|
+
if (options?.limit) {
|
|
415
|
+
results = results.slice(options.offset ?? 0, (options.offset ?? 0) + options.limit);
|
|
416
|
+
}
|
|
417
|
+
const records = results.map(({ tenantId: _, ...r }) => r);
|
|
418
|
+
return Promise.resolve({ records, total });
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
function createMockAdapter() {
|
|
423
|
+
const stores = {
|
|
424
|
+
objects: /* @__PURE__ */ new Map(),
|
|
425
|
+
attributes: /* @__PURE__ */ new Map(),
|
|
426
|
+
userProfiles: /* @__PURE__ */ new Map(),
|
|
427
|
+
files: /* @__PURE__ */ new Map(),
|
|
428
|
+
objectRecords: /* @__PURE__ */ new Map()
|
|
429
|
+
};
|
|
430
|
+
const adapter = {
|
|
431
|
+
objects: createMockObjectsRepository(stores),
|
|
432
|
+
attributes: createMockAttributesRepository(stores),
|
|
433
|
+
userProfiles: createMockUserProfilesRepository(stores),
|
|
434
|
+
files: createMockFilesRepository(stores),
|
|
435
|
+
objectRecords: createMockObjectRecordsRepository(stores),
|
|
436
|
+
async transaction(callback) {
|
|
437
|
+
return await callback(adapter);
|
|
438
|
+
},
|
|
439
|
+
// Expose stores for testing inspection
|
|
440
|
+
_stores: stores,
|
|
441
|
+
// Reset all stores (useful between tests)
|
|
442
|
+
reset() {
|
|
443
|
+
stores.objects.clear();
|
|
444
|
+
stores.attributes.clear();
|
|
445
|
+
stores.userProfiles.clear();
|
|
446
|
+
stores.files.clear();
|
|
447
|
+
stores.objectRecords.clear();
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
return adapter;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// src/runtime/services/file.service.ts
|
|
454
|
+
var FileService = class {
|
|
455
|
+
constructor(adapter, tenantId) {
|
|
456
|
+
this.adapter = adapter;
|
|
457
|
+
this.tenantId = tenantId;
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Create a new file record (after upload to storage)
|
|
461
|
+
*
|
|
462
|
+
* @param data - File metadata
|
|
463
|
+
* @returns Created file record
|
|
464
|
+
*
|
|
465
|
+
* @example
|
|
466
|
+
* ```typescript
|
|
467
|
+
* const service = new FileService(adapter, "tenant-123");
|
|
468
|
+
*
|
|
469
|
+
* // After uploading to S3
|
|
470
|
+
* const file = await service.createFile({
|
|
471
|
+
* tenantId: "tenant-123",
|
|
472
|
+
* name: "contract-2025.pdf",
|
|
473
|
+
* originalName: "Contract Acme Corp 2025.pdf",
|
|
474
|
+
* mimeType: "application/pdf",
|
|
475
|
+
* size: 2458624,
|
|
476
|
+
* storageProvider: "s3",
|
|
477
|
+
* storagePath: "tenants/123/files/2025/contract.pdf",
|
|
478
|
+
* storageBucket: "my-app-files",
|
|
479
|
+
* url: "https://cdn.example.com/files/file-123",
|
|
480
|
+
* uploadedBy: "profile-456",
|
|
481
|
+
* visibility: "private"
|
|
482
|
+
* });
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
async createFile(data) {
|
|
486
|
+
if (data.tenantId !== this.tenantId) {
|
|
487
|
+
throw new Error(
|
|
488
|
+
`Tenant mismatch: service initialized with "${this.tenantId}" but data has "${data.tenantId}"`
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
return await this.adapter.files.create(data);
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Get file by ID
|
|
495
|
+
*/
|
|
496
|
+
async getFile(fileId) {
|
|
497
|
+
const file2 = await this.adapter.files.findById(fileId);
|
|
498
|
+
if (file2?.deletedAt) {
|
|
499
|
+
return null;
|
|
500
|
+
}
|
|
501
|
+
return file2;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Get file by ID or throw
|
|
505
|
+
*/
|
|
506
|
+
async getFileOrThrow(fileId) {
|
|
507
|
+
const file2 = await this.getFile(fileId);
|
|
508
|
+
if (!file2) {
|
|
509
|
+
throw new Error(`File with id "${fileId}" not found`);
|
|
510
|
+
}
|
|
511
|
+
return file2;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Update file metadata
|
|
515
|
+
*
|
|
516
|
+
* @param fileId - File UUID
|
|
517
|
+
* @param data - Data to update
|
|
518
|
+
* @returns Updated file
|
|
519
|
+
*/
|
|
520
|
+
async updateFile(fileId, data) {
|
|
521
|
+
await this.getFileOrThrow(fileId);
|
|
522
|
+
return await this.adapter.files.update(fileId, data);
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Delete file (soft delete)
|
|
526
|
+
*
|
|
527
|
+
* @param fileId - File UUID
|
|
528
|
+
* @param options - Delete options
|
|
529
|
+
*/
|
|
530
|
+
async deleteFile(fileId, options) {
|
|
531
|
+
if (options?.checkOwnership && options.userId) {
|
|
532
|
+
const file2 = await this.getFileOrThrow(fileId);
|
|
533
|
+
if (file2.uploadedBy !== options.userId) {
|
|
534
|
+
throw new Error("You can only delete files you uploaded");
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
if (options?.hard) {
|
|
538
|
+
await this.adapter.files.hardDelete(fileId);
|
|
539
|
+
} else {
|
|
540
|
+
await this.adapter.files.delete(fileId);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* List files for the tenant
|
|
545
|
+
*/
|
|
546
|
+
async listFiles(options) {
|
|
547
|
+
return await this.adapter.files.list(this.tenantId, options);
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* List files by folder
|
|
551
|
+
*/
|
|
552
|
+
async listFilesByFolder(folderPath) {
|
|
553
|
+
return await this.adapter.files.findByFolder(this.tenantId, folderPath);
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* List files uploaded by a specific user
|
|
557
|
+
*/
|
|
558
|
+
async listFilesByUploader(uploadedBy) {
|
|
559
|
+
return await this.adapter.files.findByUploader(uploadedBy);
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Change file visibility
|
|
563
|
+
*
|
|
564
|
+
* @param fileId - File UUID
|
|
565
|
+
* @param visibility - New visibility level
|
|
566
|
+
* @param allowedUsers - Users allowed to access (if restricted)
|
|
567
|
+
*/
|
|
568
|
+
async changeVisibility(fileId, visibility, allowedUsers) {
|
|
569
|
+
const updateData = { visibility };
|
|
570
|
+
if (visibility === "restricted" && allowedUsers) {
|
|
571
|
+
updateData.allowedUsers = allowedUsers;
|
|
572
|
+
}
|
|
573
|
+
return await this.updateFile(fileId, updateData);
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Grant access to a file for specific users
|
|
577
|
+
*
|
|
578
|
+
* @param fileId - File UUID
|
|
579
|
+
* @param userIds - User IDs to grant access
|
|
580
|
+
*/
|
|
581
|
+
async grantAccess(fileId, userIds) {
|
|
582
|
+
const file2 = await this.getFileOrThrow(fileId);
|
|
583
|
+
const currentAllowed = file2.allowedUsers || [];
|
|
584
|
+
const newAllowed = [.../* @__PURE__ */ new Set([...currentAllowed, ...userIds])];
|
|
585
|
+
return await this.updateFile(fileId, {
|
|
586
|
+
visibility: "restricted",
|
|
587
|
+
allowedUsers: newAllowed
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Revoke access to a file for specific users
|
|
592
|
+
*
|
|
593
|
+
* @param fileId - File UUID
|
|
594
|
+
* @param userIds - User IDs to revoke access
|
|
595
|
+
*/
|
|
596
|
+
async revokeAccess(fileId, userIds) {
|
|
597
|
+
const file2 = await this.getFileOrThrow(fileId);
|
|
598
|
+
const currentAllowed = file2.allowedUsers || [];
|
|
599
|
+
const newAllowed = currentAllowed.filter((id) => !userIds.includes(id));
|
|
600
|
+
return await this.updateFile(fileId, {
|
|
601
|
+
allowedUsers: newAllowed
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Check if user has access to a file
|
|
606
|
+
*
|
|
607
|
+
* @param fileId - File UUID
|
|
608
|
+
* @param userId - User ID to check
|
|
609
|
+
* @returns true if user can access the file
|
|
610
|
+
*/
|
|
611
|
+
async canAccess(fileId, userId) {
|
|
612
|
+
const file2 = await this.getFile(fileId);
|
|
613
|
+
if (!file2) {
|
|
614
|
+
return false;
|
|
615
|
+
}
|
|
616
|
+
if (file2.visibility === "public") {
|
|
617
|
+
return true;
|
|
618
|
+
}
|
|
619
|
+
if (file2.uploadedBy === userId) {
|
|
620
|
+
return true;
|
|
621
|
+
}
|
|
622
|
+
if (file2.visibility === "restricted") {
|
|
623
|
+
return file2.allowedUsers?.includes(userId) ?? false;
|
|
624
|
+
}
|
|
625
|
+
return false;
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Move file to different folder
|
|
629
|
+
*/
|
|
630
|
+
async moveToFolder(fileId, newFolderPath) {
|
|
631
|
+
return await this.updateFile(fileId, { folderPath: newFolderPath });
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Add tags to file
|
|
635
|
+
*/
|
|
636
|
+
async addTags(fileId, tags) {
|
|
637
|
+
const file2 = await this.getFileOrThrow(fileId);
|
|
638
|
+
const currentTags = file2.tags || [];
|
|
639
|
+
const newTags = [.../* @__PURE__ */ new Set([...currentTags, ...tags])];
|
|
640
|
+
return await this.updateFile(fileId, { tags: newTags });
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Remove tags from file
|
|
644
|
+
*/
|
|
645
|
+
async removeTags(fileId, tags) {
|
|
646
|
+
const file2 = await this.getFileOrThrow(fileId);
|
|
647
|
+
const currentTags = file2.tags || [];
|
|
648
|
+
const newTags = currentTags.filter((tag) => !tags.includes(tag));
|
|
649
|
+
return await this.updateFile(fileId, { tags: newTags });
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
// src/runtime/services/geocoding.service.ts
|
|
654
|
+
var GeocodingService = class {
|
|
655
|
+
constructor(adapter) {
|
|
656
|
+
this.adapter = adapter;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Search for address suggestions as the user types
|
|
660
|
+
*/
|
|
661
|
+
async autocomplete(params) {
|
|
662
|
+
if (!params.query || params.query.trim().length < 2) {
|
|
663
|
+
return [];
|
|
664
|
+
}
|
|
665
|
+
return await this.adapter.autocomplete({
|
|
666
|
+
...params,
|
|
667
|
+
query: params.query.trim(),
|
|
668
|
+
limit: params.limit ?? 5
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Reverse geocode coordinates to an address
|
|
673
|
+
*/
|
|
674
|
+
async reverse(params) {
|
|
675
|
+
if (!this.adapter.reverse) {
|
|
676
|
+
throw new Error("Reverse geocoding is not supported by the configured adapter");
|
|
677
|
+
}
|
|
678
|
+
return await this.adapter.reverse(params);
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Geocode a structured address to coordinates
|
|
682
|
+
*/
|
|
683
|
+
async geocode(params) {
|
|
684
|
+
if (!this.adapter.geocode) {
|
|
685
|
+
throw new Error("Geocoding is not supported by the configured adapter");
|
|
686
|
+
}
|
|
687
|
+
return await this.adapter.geocode(params);
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
// src/builders/attribute-builders.ts
|
|
692
|
+
import { z } from "zod";
|
|
693
|
+
var BaseAttributeBuilder = class {
|
|
694
|
+
constructor(type, name, label) {
|
|
695
|
+
this.validateVariableName(name);
|
|
696
|
+
this.attr = {
|
|
697
|
+
name,
|
|
698
|
+
label,
|
|
699
|
+
type,
|
|
700
|
+
required: false
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* Validate attribute name as valid variable identifier
|
|
705
|
+
* Accepts: camelCase, snake_case, PascalCase, UPPER_CASE
|
|
706
|
+
*/
|
|
707
|
+
validateVariableName(name) {
|
|
708
|
+
const variableNameSchema = z.string().min(1, "Attribute name cannot be empty").max(63, "Attribute name is too long (max 63 characters)").regex(/^[a-zA-Z_][a-zA-Z0-9_]*$/, {
|
|
709
|
+
message: "Invalid attribute name format.\nName must be a valid variable identifier:\n \u2705 Valid: 'firstName', 'first_name', 'FirstName', 'FIRST_NAME'\n \u274C Invalid: 'first-name', 'first name', '123name', 'first.name'"
|
|
710
|
+
});
|
|
711
|
+
try {
|
|
712
|
+
variableNameSchema.parse(name);
|
|
713
|
+
} catch (error) {
|
|
714
|
+
if (error instanceof z.ZodError) {
|
|
715
|
+
throw new Error(`[AttributeBuilder] ${error.errors[0].message}`);
|
|
716
|
+
}
|
|
717
|
+
throw error;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
setRequired(value) {
|
|
721
|
+
this.attr.required = value;
|
|
722
|
+
}
|
|
723
|
+
system() {
|
|
724
|
+
this.attr.system = true;
|
|
725
|
+
return this;
|
|
726
|
+
}
|
|
727
|
+
disabled() {
|
|
728
|
+
this.attr.disabled = true;
|
|
729
|
+
return this;
|
|
730
|
+
}
|
|
731
|
+
hidden() {
|
|
732
|
+
this.attr.hidden = true;
|
|
733
|
+
return this;
|
|
734
|
+
}
|
|
735
|
+
placeholder(value) {
|
|
736
|
+
this.attr.placeholder = value;
|
|
737
|
+
return this;
|
|
738
|
+
}
|
|
739
|
+
description(value) {
|
|
740
|
+
this.attr.description = value;
|
|
741
|
+
return this;
|
|
742
|
+
}
|
|
743
|
+
icon(value) {
|
|
744
|
+
this.attr.icon = value;
|
|
745
|
+
return this;
|
|
746
|
+
}
|
|
747
|
+
order(value) {
|
|
748
|
+
this.attr.order = value;
|
|
749
|
+
return this;
|
|
750
|
+
}
|
|
751
|
+
defaultValue(value) {
|
|
752
|
+
this.attr.defaultValue = value;
|
|
753
|
+
return this;
|
|
754
|
+
}
|
|
755
|
+
metadata(value) {
|
|
756
|
+
this.attr.metadata = value;
|
|
757
|
+
return this;
|
|
758
|
+
}
|
|
759
|
+
build() {
|
|
760
|
+
return this.attr;
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
var TextAttributeBuilder = class extends BaseAttributeBuilder {
|
|
764
|
+
constructor(name, label) {
|
|
765
|
+
super("text", name, label);
|
|
766
|
+
}
|
|
767
|
+
minLength(value) {
|
|
768
|
+
this.attr.minLength = value;
|
|
769
|
+
return this;
|
|
770
|
+
}
|
|
771
|
+
maxLength(value) {
|
|
772
|
+
this.attr.maxLength = value;
|
|
773
|
+
return this;
|
|
774
|
+
}
|
|
775
|
+
pattern(value) {
|
|
776
|
+
this.attr.pattern = value;
|
|
777
|
+
return this;
|
|
778
|
+
}
|
|
779
|
+
required() {
|
|
780
|
+
this.setRequired(true);
|
|
781
|
+
return this;
|
|
782
|
+
}
|
|
783
|
+
optional() {
|
|
784
|
+
this.setRequired(false);
|
|
785
|
+
return this;
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Set email validation pattern
|
|
789
|
+
*/
|
|
790
|
+
email() {
|
|
791
|
+
this.attr.pattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
|
|
792
|
+
return this;
|
|
793
|
+
}
|
|
794
|
+
/**
|
|
795
|
+
* Set URL validation pattern
|
|
796
|
+
*/
|
|
797
|
+
url() {
|
|
798
|
+
this.attr.pattern = "^https?:\\/\\/[\\w\\-]+(\\.[\\w\\-]+)+[/#?]?.*$";
|
|
799
|
+
return this;
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Set slug validation pattern
|
|
803
|
+
*/
|
|
804
|
+
slug() {
|
|
805
|
+
this.attr.pattern = "^[a-z0-9]+(?:-[a-z0-9]+)*$";
|
|
806
|
+
return this;
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
function text(config) {
|
|
810
|
+
return new TextAttributeBuilder(config.name, config.label);
|
|
811
|
+
}
|
|
812
|
+
var TextAreaAttributeBuilder = class extends BaseAttributeBuilder {
|
|
813
|
+
constructor(name, label) {
|
|
814
|
+
super("textarea", name, label);
|
|
815
|
+
}
|
|
816
|
+
required() {
|
|
817
|
+
this.setRequired(true);
|
|
818
|
+
return this;
|
|
819
|
+
}
|
|
820
|
+
optional() {
|
|
821
|
+
this.setRequired(false);
|
|
822
|
+
return this;
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
function textarea(config) {
|
|
826
|
+
return new TextAreaAttributeBuilder(config.name, config.label);
|
|
827
|
+
}
|
|
828
|
+
var NumberAttributeBuilder = class extends BaseAttributeBuilder {
|
|
829
|
+
constructor(name, label) {
|
|
830
|
+
super("number", name, label);
|
|
831
|
+
}
|
|
832
|
+
min(value) {
|
|
833
|
+
this.attr.min = value;
|
|
834
|
+
return this;
|
|
835
|
+
}
|
|
836
|
+
max(value) {
|
|
837
|
+
this.attr.max = value;
|
|
838
|
+
return this;
|
|
839
|
+
}
|
|
840
|
+
integer() {
|
|
841
|
+
this.attr.unit = "integer";
|
|
842
|
+
this.attr.decimals = 0;
|
|
843
|
+
return this;
|
|
844
|
+
}
|
|
845
|
+
decimal(decimals = 2) {
|
|
846
|
+
this.attr.unit = "decimal";
|
|
847
|
+
this.attr.decimals = decimals;
|
|
848
|
+
return this;
|
|
849
|
+
}
|
|
850
|
+
percentage() {
|
|
851
|
+
this.attr.unit = "percentage";
|
|
852
|
+
return this;
|
|
853
|
+
}
|
|
854
|
+
required() {
|
|
855
|
+
this.setRequired(true);
|
|
856
|
+
return this;
|
|
857
|
+
}
|
|
858
|
+
optional() {
|
|
859
|
+
this.setRequired(false);
|
|
860
|
+
return this;
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
function number(config) {
|
|
864
|
+
return new NumberAttributeBuilder(config.name, config.label);
|
|
865
|
+
}
|
|
866
|
+
var CheckboxAttributeBuilder = class extends BaseAttributeBuilder {
|
|
867
|
+
constructor(name, label) {
|
|
868
|
+
super("checkbox", name, label);
|
|
869
|
+
}
|
|
870
|
+
required() {
|
|
871
|
+
this.setRequired(true);
|
|
872
|
+
return this;
|
|
873
|
+
}
|
|
874
|
+
optional() {
|
|
875
|
+
this.setRequired(false);
|
|
876
|
+
return this;
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
function checkbox(config) {
|
|
880
|
+
return new CheckboxAttributeBuilder(config.name, config.label);
|
|
881
|
+
}
|
|
882
|
+
var DateAttributeBuilder = class extends BaseAttributeBuilder {
|
|
883
|
+
constructor(name, label) {
|
|
884
|
+
super("date", name, label);
|
|
885
|
+
}
|
|
886
|
+
format(value) {
|
|
887
|
+
this.attr.dateFormat = value;
|
|
888
|
+
return this;
|
|
889
|
+
}
|
|
890
|
+
minDate(value) {
|
|
891
|
+
this.attr.minDate = value;
|
|
892
|
+
return this;
|
|
893
|
+
}
|
|
894
|
+
maxDate(value) {
|
|
895
|
+
this.attr.maxDate = value;
|
|
896
|
+
return this;
|
|
897
|
+
}
|
|
898
|
+
required() {
|
|
899
|
+
this.setRequired(true);
|
|
900
|
+
return this;
|
|
901
|
+
}
|
|
902
|
+
optional() {
|
|
903
|
+
this.setRequired(false);
|
|
904
|
+
return this;
|
|
905
|
+
}
|
|
906
|
+
};
|
|
907
|
+
function date(config) {
|
|
908
|
+
return new DateAttributeBuilder(config.name, config.label);
|
|
909
|
+
}
|
|
910
|
+
var PhoneAttributeBuilder = class extends BaseAttributeBuilder {
|
|
911
|
+
constructor(name, label) {
|
|
912
|
+
super("phone", name, label);
|
|
913
|
+
}
|
|
914
|
+
defaultCountry(value) {
|
|
915
|
+
this.attr.defaultCountryCode = value;
|
|
916
|
+
return this;
|
|
917
|
+
}
|
|
918
|
+
required() {
|
|
919
|
+
this.setRequired(true);
|
|
920
|
+
return this;
|
|
921
|
+
}
|
|
922
|
+
optional() {
|
|
923
|
+
this.setRequired(false);
|
|
924
|
+
return this;
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
function phone(config) {
|
|
928
|
+
return new PhoneAttributeBuilder(config.name, config.label);
|
|
929
|
+
}
|
|
930
|
+
var CurrencyAttributeBuilder = class extends BaseAttributeBuilder {
|
|
931
|
+
constructor(name, label) {
|
|
932
|
+
super("currency", name, label);
|
|
933
|
+
}
|
|
934
|
+
defaultCurrency(value) {
|
|
935
|
+
this.attr.defaultCurrency = value;
|
|
936
|
+
return this;
|
|
937
|
+
}
|
|
938
|
+
allowedCurrencies(value) {
|
|
939
|
+
this.attr.allowedCurrencies = value;
|
|
940
|
+
return this;
|
|
941
|
+
}
|
|
942
|
+
required() {
|
|
943
|
+
this.setRequired(true);
|
|
944
|
+
return this;
|
|
945
|
+
}
|
|
946
|
+
optional() {
|
|
947
|
+
this.setRequired(false);
|
|
948
|
+
return this;
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
function currency(config) {
|
|
952
|
+
return new CurrencyAttributeBuilder(config.name, config.label);
|
|
953
|
+
}
|
|
954
|
+
var StatusAttributeBuilder = class extends BaseAttributeBuilder {
|
|
955
|
+
constructor(name, label) {
|
|
956
|
+
super("status", name, label);
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* Set all options at once (preserves literal types for values)
|
|
960
|
+
*/
|
|
961
|
+
options(value) {
|
|
962
|
+
this.attr.options = value;
|
|
963
|
+
return this;
|
|
964
|
+
}
|
|
965
|
+
option(config) {
|
|
966
|
+
if (!this.attr.options) {
|
|
967
|
+
this.attr.options = [];
|
|
968
|
+
}
|
|
969
|
+
this.attr.options.push(config);
|
|
970
|
+
return this;
|
|
971
|
+
}
|
|
972
|
+
required() {
|
|
973
|
+
this.setRequired(true);
|
|
974
|
+
return this;
|
|
975
|
+
}
|
|
976
|
+
optional() {
|
|
977
|
+
this.setRequired(false);
|
|
978
|
+
return this;
|
|
979
|
+
}
|
|
980
|
+
};
|
|
981
|
+
function status(config) {
|
|
982
|
+
return new StatusAttributeBuilder(config.name, config.label);
|
|
983
|
+
}
|
|
984
|
+
var SelectAttributeBuilder = class extends BaseAttributeBuilder {
|
|
985
|
+
constructor(name, label) {
|
|
986
|
+
super("select", name, label);
|
|
987
|
+
}
|
|
988
|
+
/**
|
|
989
|
+
* Set all options at once (preserves literal types for values)
|
|
990
|
+
*/
|
|
991
|
+
options(value) {
|
|
992
|
+
this.attr.options = value;
|
|
993
|
+
return this;
|
|
994
|
+
}
|
|
995
|
+
option(config) {
|
|
996
|
+
if (!this.attr.options) {
|
|
997
|
+
this.attr.options = [];
|
|
998
|
+
}
|
|
999
|
+
this.attr.options.push(config);
|
|
1000
|
+
return this;
|
|
1001
|
+
}
|
|
1002
|
+
required() {
|
|
1003
|
+
this.setRequired(true);
|
|
1004
|
+
return this;
|
|
1005
|
+
}
|
|
1006
|
+
optional() {
|
|
1007
|
+
this.setRequired(false);
|
|
1008
|
+
return this;
|
|
1009
|
+
}
|
|
1010
|
+
};
|
|
1011
|
+
function select(config) {
|
|
1012
|
+
return new SelectAttributeBuilder(config.name, config.label);
|
|
1013
|
+
}
|
|
1014
|
+
var MultiselectAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1015
|
+
constructor(name, label) {
|
|
1016
|
+
super("multiselect", name, label);
|
|
1017
|
+
}
|
|
1018
|
+
/**
|
|
1019
|
+
* Set all options at once (preserves literal types for values)
|
|
1020
|
+
*/
|
|
1021
|
+
options(value) {
|
|
1022
|
+
this.attr.options = value;
|
|
1023
|
+
return this;
|
|
1024
|
+
}
|
|
1025
|
+
option(config) {
|
|
1026
|
+
if (!this.attr.options) {
|
|
1027
|
+
this.attr.options = [];
|
|
1028
|
+
}
|
|
1029
|
+
this.attr.options.push(config);
|
|
1030
|
+
return this;
|
|
1031
|
+
}
|
|
1032
|
+
required() {
|
|
1033
|
+
this.setRequired(true);
|
|
1034
|
+
return this;
|
|
1035
|
+
}
|
|
1036
|
+
optional() {
|
|
1037
|
+
this.setRequired(false);
|
|
1038
|
+
return this;
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
function multiselect(config) {
|
|
1042
|
+
return new MultiselectAttributeBuilder(config.name, config.label);
|
|
1043
|
+
}
|
|
1044
|
+
var LocationAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1045
|
+
constructor(name, label) {
|
|
1046
|
+
super("location", name, label);
|
|
1047
|
+
}
|
|
1048
|
+
granularity(value) {
|
|
1049
|
+
this.attr.granularity = value;
|
|
1050
|
+
return this;
|
|
1051
|
+
}
|
|
1052
|
+
enableAutocomplete() {
|
|
1053
|
+
this.attr.enableAutocomplete = true;
|
|
1054
|
+
return this;
|
|
1055
|
+
}
|
|
1056
|
+
enableMap() {
|
|
1057
|
+
this.attr.enableMap = true;
|
|
1058
|
+
return this;
|
|
1059
|
+
}
|
|
1060
|
+
defaultCountry(value) {
|
|
1061
|
+
this.attr.defaultCountry = value;
|
|
1062
|
+
return this;
|
|
1063
|
+
}
|
|
1064
|
+
allowedCountries(value) {
|
|
1065
|
+
this.attr.allowedCountries = value;
|
|
1066
|
+
return this;
|
|
1067
|
+
}
|
|
1068
|
+
displayFormat(value) {
|
|
1069
|
+
this.attr.displayFormat = value;
|
|
1070
|
+
return this;
|
|
1071
|
+
}
|
|
1072
|
+
required() {
|
|
1073
|
+
this.setRequired(true);
|
|
1074
|
+
return this;
|
|
1075
|
+
}
|
|
1076
|
+
optional() {
|
|
1077
|
+
this.setRequired(false);
|
|
1078
|
+
return this;
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
function location(config) {
|
|
1082
|
+
return new LocationAttributeBuilder(config.name, config.label);
|
|
1083
|
+
}
|
|
1084
|
+
var TimestampAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1085
|
+
constructor(name, label) {
|
|
1086
|
+
super("timestamp", name, label);
|
|
1087
|
+
}
|
|
1088
|
+
autoUpdate() {
|
|
1089
|
+
this.attr.autoUpdate = true;
|
|
1090
|
+
return this;
|
|
1091
|
+
}
|
|
1092
|
+
required() {
|
|
1093
|
+
this.setRequired(true);
|
|
1094
|
+
return this;
|
|
1095
|
+
}
|
|
1096
|
+
optional() {
|
|
1097
|
+
this.setRequired(false);
|
|
1098
|
+
return this;
|
|
1099
|
+
}
|
|
1100
|
+
};
|
|
1101
|
+
function timestamp(config) {
|
|
1102
|
+
return new TimestampAttributeBuilder(config.name, config.label);
|
|
1103
|
+
}
|
|
1104
|
+
var FileAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1105
|
+
constructor(name, label) {
|
|
1106
|
+
super("file", name, label);
|
|
1107
|
+
}
|
|
1108
|
+
maxFiles(value) {
|
|
1109
|
+
this.attr.maxFiles = value;
|
|
1110
|
+
return this;
|
|
1111
|
+
}
|
|
1112
|
+
maxSize(bytes) {
|
|
1113
|
+
this.attr.maxSize = bytes;
|
|
1114
|
+
return this;
|
|
1115
|
+
}
|
|
1116
|
+
allowedTypes(types) {
|
|
1117
|
+
this.attr.allowedTypes = types;
|
|
1118
|
+
return this;
|
|
1119
|
+
}
|
|
1120
|
+
verification(config) {
|
|
1121
|
+
this.attr.verification = config;
|
|
1122
|
+
return this;
|
|
1123
|
+
}
|
|
1124
|
+
required() {
|
|
1125
|
+
this.setRequired(true);
|
|
1126
|
+
return this;
|
|
1127
|
+
}
|
|
1128
|
+
optional() {
|
|
1129
|
+
this.setRequired(false);
|
|
1130
|
+
return this;
|
|
1131
|
+
}
|
|
1132
|
+
};
|
|
1133
|
+
function file(config) {
|
|
1134
|
+
return new FileAttributeBuilder(config.name, config.label);
|
|
1135
|
+
}
|
|
1136
|
+
var UserAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1137
|
+
constructor(name, label) {
|
|
1138
|
+
super("user", name, label);
|
|
1139
|
+
}
|
|
1140
|
+
allowedRoles(roles) {
|
|
1141
|
+
this.attr.allowedRoles = roles;
|
|
1142
|
+
return this;
|
|
1143
|
+
}
|
|
1144
|
+
required() {
|
|
1145
|
+
this.setRequired(true);
|
|
1146
|
+
return this;
|
|
1147
|
+
}
|
|
1148
|
+
optional() {
|
|
1149
|
+
this.setRequired(false);
|
|
1150
|
+
return this;
|
|
1151
|
+
}
|
|
1152
|
+
};
|
|
1153
|
+
function user(config) {
|
|
1154
|
+
return new UserAttributeBuilder(config.name, config.label);
|
|
1155
|
+
}
|
|
1156
|
+
var SingleRelationAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1157
|
+
constructor(name, label) {
|
|
1158
|
+
super("relation", name, label);
|
|
1159
|
+
this.attr.cardinality = "one";
|
|
1160
|
+
this.attr.targets = [];
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* Add a target object that can be linked
|
|
1164
|
+
* @param objectName - Name of the object (e.g., "companies")
|
|
1165
|
+
* @param options - Display template and filter options
|
|
1166
|
+
*/
|
|
1167
|
+
to(objectName, options) {
|
|
1168
|
+
const target = {
|
|
1169
|
+
object: objectName,
|
|
1170
|
+
...options
|
|
1171
|
+
};
|
|
1172
|
+
this.attr.targets?.push(target);
|
|
1173
|
+
return this;
|
|
1174
|
+
}
|
|
1175
|
+
/**
|
|
1176
|
+
* Set the deletion strategy when target record is deleted
|
|
1177
|
+
* @param strategy - "restrict" (default), "cascade", or "set_null"
|
|
1178
|
+
*/
|
|
1179
|
+
onDelete(strategy) {
|
|
1180
|
+
this.attr.onDelete = strategy;
|
|
1181
|
+
return this;
|
|
1182
|
+
}
|
|
1183
|
+
/**
|
|
1184
|
+
* Convert to a multi-relation (cardinality: "many")
|
|
1185
|
+
*/
|
|
1186
|
+
many() {
|
|
1187
|
+
const multiBuilder = new MultiRelationAttributeBuilder(
|
|
1188
|
+
this.attr.name,
|
|
1189
|
+
this.attr.label ?? "",
|
|
1190
|
+
{
|
|
1191
|
+
targets: this.attr.targets,
|
|
1192
|
+
onDelete: this.attr.onDelete,
|
|
1193
|
+
isRequired: this.attr.required
|
|
1194
|
+
}
|
|
1195
|
+
);
|
|
1196
|
+
return multiBuilder;
|
|
1197
|
+
}
|
|
1198
|
+
required() {
|
|
1199
|
+
this.setRequired(true);
|
|
1200
|
+
return this;
|
|
1201
|
+
}
|
|
1202
|
+
optional() {
|
|
1203
|
+
this.setRequired(false);
|
|
1204
|
+
return this;
|
|
1205
|
+
}
|
|
1206
|
+
};
|
|
1207
|
+
var MultiRelationAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1208
|
+
constructor(name, label, initOptions) {
|
|
1209
|
+
super("relation", name, label);
|
|
1210
|
+
this.attr.cardinality = "many";
|
|
1211
|
+
this.attr.targets = initOptions?.targets ?? [];
|
|
1212
|
+
this.attr.defaultValue = [];
|
|
1213
|
+
if (initOptions?.onDelete) {
|
|
1214
|
+
this.attr.onDelete = initOptions.onDelete;
|
|
1215
|
+
}
|
|
1216
|
+
if (initOptions?.isRequired) {
|
|
1217
|
+
this.setRequired(true);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
/**
|
|
1221
|
+
* Add a target object that can be linked
|
|
1222
|
+
* @param objectName - Name of the object (e.g., "companies")
|
|
1223
|
+
* @param options - Display template and filter options
|
|
1224
|
+
*/
|
|
1225
|
+
to(objectName, options) {
|
|
1226
|
+
const target = {
|
|
1227
|
+
object: objectName,
|
|
1228
|
+
...options
|
|
1229
|
+
};
|
|
1230
|
+
this.attr.targets?.push(target);
|
|
1231
|
+
return this;
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Set the deletion strategy when target record is deleted
|
|
1235
|
+
* @param strategy - "restrict" (default), "cascade", or "set_null"
|
|
1236
|
+
*/
|
|
1237
|
+
onDelete(strategy) {
|
|
1238
|
+
this.attr.onDelete = strategy;
|
|
1239
|
+
return this;
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Set minimum number of relations required
|
|
1243
|
+
*/
|
|
1244
|
+
minItems(count) {
|
|
1245
|
+
this.attr.minItems = count;
|
|
1246
|
+
return this;
|
|
1247
|
+
}
|
|
1248
|
+
/**
|
|
1249
|
+
* Set maximum number of relations allowed
|
|
1250
|
+
*/
|
|
1251
|
+
maxItems(count) {
|
|
1252
|
+
this.attr.maxItems = count;
|
|
1253
|
+
return this;
|
|
1254
|
+
}
|
|
1255
|
+
required() {
|
|
1256
|
+
this.setRequired(true);
|
|
1257
|
+
return this;
|
|
1258
|
+
}
|
|
1259
|
+
optional() {
|
|
1260
|
+
this.setRequired(false);
|
|
1261
|
+
return this;
|
|
1262
|
+
}
|
|
1263
|
+
};
|
|
1264
|
+
function relation(config) {
|
|
1265
|
+
return new SingleRelationAttributeBuilder(config.name, config.label);
|
|
1266
|
+
}
|
|
1267
|
+
var RatingAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1268
|
+
constructor(name, label) {
|
|
1269
|
+
super("rating", name, label);
|
|
1270
|
+
}
|
|
1271
|
+
max(value) {
|
|
1272
|
+
this.attr.max = value;
|
|
1273
|
+
return this;
|
|
1274
|
+
}
|
|
1275
|
+
iconType(value) {
|
|
1276
|
+
this.attr.iconType = value;
|
|
1277
|
+
return this;
|
|
1278
|
+
}
|
|
1279
|
+
required() {
|
|
1280
|
+
this.setRequired(true);
|
|
1281
|
+
return this;
|
|
1282
|
+
}
|
|
1283
|
+
optional() {
|
|
1284
|
+
this.setRequired(false);
|
|
1285
|
+
return this;
|
|
1286
|
+
}
|
|
1287
|
+
};
|
|
1288
|
+
function rating(config) {
|
|
1289
|
+
return new RatingAttributeBuilder(config.name, config.label);
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
// src/builders/object-builder.ts
|
|
1293
|
+
import z2 from "zod";
|
|
1294
|
+
var ObjectBuilder = class {
|
|
1295
|
+
constructor(config) {
|
|
1296
|
+
this.validateName(config.name);
|
|
1297
|
+
this.obj = {
|
|
1298
|
+
name: config.name,
|
|
1299
|
+
label: config.label,
|
|
1300
|
+
attributes: [],
|
|
1301
|
+
system: false
|
|
1302
|
+
};
|
|
1303
|
+
}
|
|
1304
|
+
/**
|
|
1305
|
+
* Set object description
|
|
1306
|
+
*/
|
|
1307
|
+
description(value) {
|
|
1308
|
+
this.obj.description = value;
|
|
1309
|
+
return this;
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Set object icon
|
|
1313
|
+
*/
|
|
1314
|
+
icon(value) {
|
|
1315
|
+
this.obj.icon = value;
|
|
1316
|
+
return this;
|
|
1317
|
+
}
|
|
1318
|
+
/**
|
|
1319
|
+
* Mark as system/native object (defined in code, protected)
|
|
1320
|
+
* System objects cannot be deleted/modified by clients
|
|
1321
|
+
*/
|
|
1322
|
+
system() {
|
|
1323
|
+
this.obj.system = true;
|
|
1324
|
+
return this;
|
|
1325
|
+
}
|
|
1326
|
+
/**
|
|
1327
|
+
* Add an attribute to the object with type accumulation
|
|
1328
|
+
*
|
|
1329
|
+
* The return type accumulates the attribute's name and value type
|
|
1330
|
+
* into the TRecord generic, enabling full inference.
|
|
1331
|
+
*
|
|
1332
|
+
* @param attr - Attribute definition or builder (auto-builds if builder)
|
|
1333
|
+
*/
|
|
1334
|
+
attribute(attr) {
|
|
1335
|
+
if (!this.obj.attributes) {
|
|
1336
|
+
this.obj.attributes = [];
|
|
1337
|
+
}
|
|
1338
|
+
const finalAttr = "build" in attr && typeof attr.build === "function" ? attr.build() : attr;
|
|
1339
|
+
this.obj.attributes.push(finalAttr);
|
|
1340
|
+
return this;
|
|
1341
|
+
}
|
|
1342
|
+
/**
|
|
1343
|
+
* Add multiple attributes at once
|
|
1344
|
+
* Note: For full type inference, prefer chaining .attribute() calls
|
|
1345
|
+
*/
|
|
1346
|
+
attributes(attrs) {
|
|
1347
|
+
if (!this.obj.attributes) {
|
|
1348
|
+
this.obj.attributes = [];
|
|
1349
|
+
}
|
|
1350
|
+
const finalAttrs = attrs.map((attr) => "build" in attr ? attr.build() : attr);
|
|
1351
|
+
this.obj.attributes.push(...finalAttrs);
|
|
1352
|
+
return this;
|
|
1353
|
+
}
|
|
1354
|
+
/**
|
|
1355
|
+
* Set metadata
|
|
1356
|
+
*/
|
|
1357
|
+
metadata(value) {
|
|
1358
|
+
this.obj.metadata = value;
|
|
1359
|
+
return this;
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* Set the title attribute (the text attribute used to display the object's name)
|
|
1363
|
+
* Must reference a text attribute that has been added to the object.
|
|
1364
|
+
* If not set, the first text attribute becomes the title attribute.
|
|
1365
|
+
*
|
|
1366
|
+
* @param attributeName - The name of a text attribute
|
|
1367
|
+
*/
|
|
1368
|
+
titleAttribute(attributeName) {
|
|
1369
|
+
this._titleAttribute = attributeName;
|
|
1370
|
+
return this;
|
|
1371
|
+
}
|
|
1372
|
+
/**
|
|
1373
|
+
* Build the final ObjectDefinition
|
|
1374
|
+
* @throws Error if required fields are missing or invalid
|
|
1375
|
+
*/
|
|
1376
|
+
build() {
|
|
1377
|
+
if (!this.obj.name) {
|
|
1378
|
+
throw new Error("[ObjectBuilder] Missing required field: name");
|
|
1379
|
+
}
|
|
1380
|
+
if (!this.obj.label) {
|
|
1381
|
+
throw new Error("[ObjectBuilder] Missing required field: label");
|
|
1382
|
+
}
|
|
1383
|
+
const textAttributes = (this.obj.attributes ?? []).filter((attr) => attr.type === "text");
|
|
1384
|
+
if (textAttributes.length === 0) {
|
|
1385
|
+
throw new Error(
|
|
1386
|
+
`[ObjectBuilder] Object "${this.obj.name}" must have at least one text attribute.
|
|
1387
|
+
A text attribute is required to serve as the object's title/display name.
|
|
1388
|
+
\u2705 Example: .attribute(text({ name: 'name', label: 'Name' }).required())`
|
|
1389
|
+
);
|
|
1390
|
+
}
|
|
1391
|
+
const titleAttrName = this._titleAttribute ?? textAttributes[0].name;
|
|
1392
|
+
const titleAttr = textAttributes.find((attr) => attr.name === titleAttrName);
|
|
1393
|
+
if (!titleAttr) {
|
|
1394
|
+
const availableTextAttrs = textAttributes.map((a) => `'${a.name}'`).join(", ");
|
|
1395
|
+
throw new Error(
|
|
1396
|
+
`[ObjectBuilder] titleAttribute "${titleAttrName}" must be a text attribute.
|
|
1397
|
+
Available text attributes: ${availableTextAttrs}`
|
|
1398
|
+
);
|
|
1399
|
+
}
|
|
1400
|
+
return {
|
|
1401
|
+
...this.obj,
|
|
1402
|
+
titleAttribute: titleAttrName
|
|
1403
|
+
};
|
|
1404
|
+
}
|
|
1405
|
+
/**
|
|
1406
|
+
* Validate object name format
|
|
1407
|
+
* Must be kebab-case (slug format), max 63 characters
|
|
1408
|
+
* @internal
|
|
1409
|
+
*/
|
|
1410
|
+
validateName(name) {
|
|
1411
|
+
if (name === void 0) {
|
|
1412
|
+
return;
|
|
1413
|
+
}
|
|
1414
|
+
const objectNameSchema = z2.string().min(1, "Object name cannot be empty").max(63, "Object name is too long (max 63 characters)").regex(/^[a-z][a-z0-9-]*$/, {
|
|
1415
|
+
message: "Invalid object name format.\nName must be in kebab-case (slug format):\n \u2705 Valid: 'products', 'user-profiles', 'order-items'\n \u274C Invalid: 'Products', 'userProfiles', 'user_profiles'"
|
|
1416
|
+
});
|
|
1417
|
+
try {
|
|
1418
|
+
objectNameSchema.parse(name);
|
|
1419
|
+
} catch (error) {
|
|
1420
|
+
if (error instanceof z2.ZodError) {
|
|
1421
|
+
throw new Error(`[ObjectBuilder] ${error.errors[0].message}`);
|
|
1422
|
+
}
|
|
1423
|
+
throw error;
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
};
|
|
1427
|
+
function object(config) {
|
|
1428
|
+
return new ObjectBuilder(config);
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
// src/runtime/services/object-schema.service.ts
|
|
1432
|
+
var ObjectSchemaService = class {
|
|
1433
|
+
constructor(adapter, nativeRegistry) {
|
|
1434
|
+
this.adapter = adapter;
|
|
1435
|
+
this.nativeRegistry = nativeRegistry;
|
|
1436
|
+
}
|
|
1437
|
+
/**
|
|
1438
|
+
* Create a new custom object
|
|
1439
|
+
* Validates name format and ensures system=false
|
|
1440
|
+
*
|
|
1441
|
+
* @param definition - Object definition (attributes can be raw definitions or builders)
|
|
1442
|
+
* @param tenantId - Tenant ID owning the object
|
|
1443
|
+
* @returns Created ObjectDefinition
|
|
1444
|
+
*/
|
|
1445
|
+
async createCustomObject(definition, tenantId) {
|
|
1446
|
+
const builder = object({
|
|
1447
|
+
name: definition.name,
|
|
1448
|
+
label: definition.label
|
|
1449
|
+
});
|
|
1450
|
+
if (definition.description) builder.description(definition.description);
|
|
1451
|
+
if (definition.icon) builder.icon(definition.icon);
|
|
1452
|
+
if (definition.metadata) builder.metadata(definition.metadata);
|
|
1453
|
+
if (definition.attributes && definition.attributes.length > 0) {
|
|
1454
|
+
builder.attributes(definition.attributes);
|
|
1455
|
+
}
|
|
1456
|
+
const objectDef = builder.build();
|
|
1457
|
+
const existing = await this.adapter.objects.findByName(tenantId, objectDef.name);
|
|
1458
|
+
if (existing) {
|
|
1459
|
+
throw new Error(`Object with name "${objectDef.name}" already exists`);
|
|
1460
|
+
}
|
|
1461
|
+
const metadata = {
|
|
1462
|
+
...objectDef.metadata,
|
|
1463
|
+
icon: objectDef.icon
|
|
1464
|
+
};
|
|
1465
|
+
const dbObject = await this.adapter.objects.create({
|
|
1466
|
+
tenantId,
|
|
1467
|
+
name: objectDef.name,
|
|
1468
|
+
label: objectDef.label,
|
|
1469
|
+
description: objectDef.description,
|
|
1470
|
+
system: false,
|
|
1471
|
+
// Always false for custom objects
|
|
1472
|
+
metadata
|
|
1473
|
+
});
|
|
1474
|
+
const createdAttributes = [];
|
|
1475
|
+
if (objectDef.attributes.length > 0) {
|
|
1476
|
+
for (const attr of objectDef.attributes) {
|
|
1477
|
+
const dbAttr = await this.adapter.attributes.create({
|
|
1478
|
+
objectId: dbObject.id,
|
|
1479
|
+
name: attr.name,
|
|
1480
|
+
label: attr.label,
|
|
1481
|
+
type: attr.type,
|
|
1482
|
+
required: attr.required ?? false,
|
|
1483
|
+
system: false,
|
|
1484
|
+
// Always false for custom attributes
|
|
1485
|
+
config: {
|
|
1486
|
+
...attr,
|
|
1487
|
+
// Remove common fields stored in columns
|
|
1488
|
+
id: void 0,
|
|
1489
|
+
name: void 0,
|
|
1490
|
+
label: void 0,
|
|
1491
|
+
type: void 0,
|
|
1492
|
+
required: void 0,
|
|
1493
|
+
system: void 0
|
|
1494
|
+
}
|
|
1495
|
+
});
|
|
1496
|
+
createdAttributes.push(this.convertDBAttributeToAttribute(dbAttr));
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
return {
|
|
1500
|
+
...objectDef,
|
|
1501
|
+
id: dbObject.id,
|
|
1502
|
+
attributes: createdAttributes,
|
|
1503
|
+
system: false
|
|
1504
|
+
};
|
|
1505
|
+
}
|
|
1506
|
+
/**
|
|
1507
|
+
* Add a custom attribute to an existing object
|
|
1508
|
+
* Can be used on both custom and native objects
|
|
1509
|
+
* Attributes added via API are always custom (system=false)
|
|
1510
|
+
*
|
|
1511
|
+
* @param objectId - Object UUID from database
|
|
1512
|
+
* @param attribute - Attribute definition
|
|
1513
|
+
* @returns Created Attribute
|
|
1514
|
+
*/
|
|
1515
|
+
async addAttributeToObject(objectId, attribute) {
|
|
1516
|
+
this.validateAttributeName(attribute.name);
|
|
1517
|
+
const dbObject = await this.adapter.objects.findById(objectId);
|
|
1518
|
+
if (!dbObject) {
|
|
1519
|
+
throw new Error(`Object with id "${objectId}" not found`);
|
|
1520
|
+
}
|
|
1521
|
+
const existingAttributes = await this.adapter.attributes.findByObjectId(objectId);
|
|
1522
|
+
const duplicate = existingAttributes.find((attr) => attr.name === attribute.name);
|
|
1523
|
+
if (duplicate) {
|
|
1524
|
+
throw new Error(
|
|
1525
|
+
`Attribute with name "${attribute.name}" already exists on object "${dbObject.name}"`
|
|
1526
|
+
);
|
|
1527
|
+
}
|
|
1528
|
+
const dbAttr = await this.adapter.attributes.create({
|
|
1529
|
+
objectId,
|
|
1530
|
+
name: attribute.name,
|
|
1531
|
+
label: attribute.label,
|
|
1532
|
+
type: attribute.type,
|
|
1533
|
+
required: attribute.required ?? false,
|
|
1534
|
+
unique: attribute.unique ?? false,
|
|
1535
|
+
system: false,
|
|
1536
|
+
// Always false for attributes added via API
|
|
1537
|
+
config: {
|
|
1538
|
+
// Store all extra properties in config
|
|
1539
|
+
...attribute,
|
|
1540
|
+
// Remove common fields stored in columns
|
|
1541
|
+
name: void 0,
|
|
1542
|
+
label: void 0,
|
|
1543
|
+
type: void 0,
|
|
1544
|
+
required: void 0,
|
|
1545
|
+
unique: void 0,
|
|
1546
|
+
system: void 0
|
|
1547
|
+
}
|
|
1548
|
+
});
|
|
1549
|
+
return this.convertDBAttributeToAttribute(dbAttr);
|
|
1550
|
+
}
|
|
1551
|
+
/**
|
|
1552
|
+
* Update an attribute
|
|
1553
|
+
* Can only update custom attributes (system=false)
|
|
1554
|
+
*
|
|
1555
|
+
* @param attributeId - Attribute UUID
|
|
1556
|
+
* @param updates - Partial attribute updates
|
|
1557
|
+
* @returns Updated Attribute
|
|
1558
|
+
*/
|
|
1559
|
+
async updateAttribute(attributeId, updates) {
|
|
1560
|
+
const dbAttr = await this.adapter.attributes.findById(attributeId);
|
|
1561
|
+
if (!dbAttr) {
|
|
1562
|
+
throw new Error(`Attribute with id "${attributeId}" not found`);
|
|
1563
|
+
}
|
|
1564
|
+
if (dbAttr.system) {
|
|
1565
|
+
throw new Error(
|
|
1566
|
+
`Cannot modify system attribute "${dbAttr.name}". System attributes are protected.`
|
|
1567
|
+
);
|
|
1568
|
+
}
|
|
1569
|
+
if (updates.name && updates.name !== dbAttr.name) {
|
|
1570
|
+
throw new Error(
|
|
1571
|
+
"Attribute name cannot be changed after creation. Create a new attribute instead."
|
|
1572
|
+
);
|
|
1573
|
+
}
|
|
1574
|
+
if (updates.type && updates.type !== dbAttr.type) {
|
|
1575
|
+
throw new Error(
|
|
1576
|
+
"Attribute type cannot be changed after creation. Create a new attribute instead."
|
|
1577
|
+
);
|
|
1578
|
+
}
|
|
1579
|
+
const updatedDbAttr = await this.adapter.attributes.update(attributeId, {
|
|
1580
|
+
label: updates.label,
|
|
1581
|
+
required: updates.required,
|
|
1582
|
+
config: updates ? {
|
|
1583
|
+
...dbAttr.config,
|
|
1584
|
+
...updates,
|
|
1585
|
+
// Remove common fields
|
|
1586
|
+
name: void 0,
|
|
1587
|
+
label: void 0,
|
|
1588
|
+
type: void 0,
|
|
1589
|
+
required: void 0,
|
|
1590
|
+
unique: void 0,
|
|
1591
|
+
system: void 0
|
|
1592
|
+
} : dbAttr.config
|
|
1593
|
+
});
|
|
1594
|
+
return this.convertDBAttributeToAttribute(updatedDbAttr);
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* Delete an attribute
|
|
1598
|
+
* Can only delete custom attributes (system=false)
|
|
1599
|
+
*
|
|
1600
|
+
* @param attributeId - Attribute UUID
|
|
1601
|
+
*/
|
|
1602
|
+
async deleteAttribute(attributeId) {
|
|
1603
|
+
const dbAttr = await this.adapter.attributes.findById(attributeId);
|
|
1604
|
+
if (!dbAttr) {
|
|
1605
|
+
throw new Error(`Attribute with id "${attributeId}" not found`);
|
|
1606
|
+
}
|
|
1607
|
+
if (dbAttr.system) {
|
|
1608
|
+
throw new Error(
|
|
1609
|
+
`Cannot delete system attribute "${dbAttr.name}". System attributes are protected.`
|
|
1610
|
+
);
|
|
1611
|
+
}
|
|
1612
|
+
await this.adapter.attributes.delete(attributeId);
|
|
1613
|
+
}
|
|
1614
|
+
/**
|
|
1615
|
+
* List attributes for an object
|
|
1616
|
+
*
|
|
1617
|
+
* @param objectId - Object UUID
|
|
1618
|
+
* @param options - Filter options
|
|
1619
|
+
* @returns List of attributes
|
|
1620
|
+
*/
|
|
1621
|
+
async listAttributes(objectId, options) {
|
|
1622
|
+
const dbAttributes = await this.adapter.attributes.findByObjectId(objectId);
|
|
1623
|
+
let filtered = dbAttributes;
|
|
1624
|
+
if (options?.systemOnly) {
|
|
1625
|
+
filtered = dbAttributes.filter((attr) => attr.system);
|
|
1626
|
+
} else if (options?.customOnly) {
|
|
1627
|
+
filtered = dbAttributes.filter((attr) => !attr.system);
|
|
1628
|
+
}
|
|
1629
|
+
return filtered.map((attr) => this.convertDBAttributeToAttribute(attr));
|
|
1630
|
+
}
|
|
1631
|
+
/**
|
|
1632
|
+
* Validate attribute name format (variable identifier)
|
|
1633
|
+
* @internal
|
|
1634
|
+
*/
|
|
1635
|
+
validateAttributeName(name) {
|
|
1636
|
+
const variableNameRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
1637
|
+
if (!name || name.length === 0) {
|
|
1638
|
+
throw new Error("Attribute name cannot be empty");
|
|
1639
|
+
}
|
|
1640
|
+
if (name.length > 63) {
|
|
1641
|
+
throw new Error("Attribute name is too long (max 63 characters)");
|
|
1642
|
+
}
|
|
1643
|
+
if (!variableNameRegex.test(name)) {
|
|
1644
|
+
throw new Error(
|
|
1645
|
+
"Invalid attribute name format.\nName must be a valid variable identifier:\n \u2705 Valid: 'firstName', 'first_name', 'FirstName', 'FIRST_NAME'\n \u274C Invalid: 'first-name', 'first name', '123name', 'first.name'"
|
|
1646
|
+
);
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
/**
|
|
1650
|
+
* Get complete object schema (system + custom attributes)
|
|
1651
|
+
* If object is native, merges registry definition with DB custom attributes
|
|
1652
|
+
* If object is custom, returns DB definition only
|
|
1653
|
+
*
|
|
1654
|
+
* @param objectId - Object UUID from database
|
|
1655
|
+
* @returns Complete ObjectDefinition with all attributes
|
|
1656
|
+
*
|
|
1657
|
+
* @example
|
|
1658
|
+
* ```typescript
|
|
1659
|
+
* const service = new ObjectSchemaService(adapter, registry);
|
|
1660
|
+
* const productSchema = await service.getObjectSchema("obj-123");
|
|
1661
|
+
*
|
|
1662
|
+
* console.log(productSchema.attributes); // System + custom attributes
|
|
1663
|
+
* ```
|
|
1664
|
+
*/
|
|
1665
|
+
async getObjectSchema(objectId) {
|
|
1666
|
+
const dbObject = await this.adapter.objects.findById(objectId);
|
|
1667
|
+
if (!dbObject) {
|
|
1668
|
+
throw new Error(`Object with id "${objectId}" not found`);
|
|
1669
|
+
}
|
|
1670
|
+
const dbAttributes = await this.adapter.attributes.findByObjectId(objectId);
|
|
1671
|
+
if (dbObject.system) {
|
|
1672
|
+
return this.mergeNativeObject(dbObject, dbAttributes);
|
|
1673
|
+
}
|
|
1674
|
+
return this.convertDBObjectToDefinition(dbObject, dbAttributes);
|
|
1675
|
+
}
|
|
1676
|
+
/**
|
|
1677
|
+
* Get object schema by name (for system/native objects)
|
|
1678
|
+
* @deprecated Use getObjectSchemaByNameForTenant for custom objects support
|
|
1679
|
+
*/
|
|
1680
|
+
async getObjectSchemaByName(name) {
|
|
1681
|
+
const dbObject = await this.adapter.objects.findSystemByName(name);
|
|
1682
|
+
if (!dbObject) {
|
|
1683
|
+
throw new Error(`Native object "${name}" not found in database`);
|
|
1684
|
+
}
|
|
1685
|
+
const dbAttributes = await this.adapter.attributes.findByObjectId(dbObject.id);
|
|
1686
|
+
return this.mergeNativeObject(dbObject, dbAttributes);
|
|
1687
|
+
}
|
|
1688
|
+
/**
|
|
1689
|
+
* Get object schema by name for a tenant (supports both native and custom objects)
|
|
1690
|
+
*/
|
|
1691
|
+
async getObjectSchemaByNameForTenant(name, tenantId) {
|
|
1692
|
+
let dbObject = await this.adapter.objects.findByName(tenantId, name);
|
|
1693
|
+
if (!dbObject) {
|
|
1694
|
+
dbObject = await this.adapter.objects.findSystemByName(name);
|
|
1695
|
+
}
|
|
1696
|
+
if (!dbObject) {
|
|
1697
|
+
throw new Error(`Object "${name}" not found`);
|
|
1698
|
+
}
|
|
1699
|
+
const dbAttributes = await this.adapter.attributes.findByObjectId(dbObject.id);
|
|
1700
|
+
if (dbObject.system) {
|
|
1701
|
+
return this.mergeNativeObject(dbObject, dbAttributes);
|
|
1702
|
+
}
|
|
1703
|
+
return this.convertDBObjectToDefinition(dbObject, dbAttributes);
|
|
1704
|
+
}
|
|
1705
|
+
/**
|
|
1706
|
+
* List all object schemas for a tenant
|
|
1707
|
+
*/
|
|
1708
|
+
async listObjectSchemas(tenantId) {
|
|
1709
|
+
const dbObjects = await this.adapter.objects.list(tenantId);
|
|
1710
|
+
const schemas = await Promise.all(
|
|
1711
|
+
dbObjects.map(async (dbObject) => {
|
|
1712
|
+
const dbAttributes = await this.adapter.attributes.findByObjectId(dbObject.id);
|
|
1713
|
+
if (dbObject.system) {
|
|
1714
|
+
return this.mergeNativeObject(dbObject, dbAttributes);
|
|
1715
|
+
}
|
|
1716
|
+
return this.convertDBObjectToDefinition(dbObject, dbAttributes);
|
|
1717
|
+
})
|
|
1718
|
+
);
|
|
1719
|
+
return schemas;
|
|
1720
|
+
}
|
|
1721
|
+
/**
|
|
1722
|
+
* Merge native object from registry with custom attributes from DB
|
|
1723
|
+
* @internal
|
|
1724
|
+
*/
|
|
1725
|
+
mergeNativeObject(dbObject, dbAttributes) {
|
|
1726
|
+
const nativeObject = this.nativeRegistry.getByName(dbObject.name);
|
|
1727
|
+
if (!nativeObject) {
|
|
1728
|
+
throw new Error(
|
|
1729
|
+
`Native object "${dbObject.name}" not found in registry. Make sure to register it before querying.`
|
|
1730
|
+
);
|
|
1731
|
+
}
|
|
1732
|
+
const systemAttributesFromDB = dbAttributes.filter((attr) => attr.system);
|
|
1733
|
+
const customAttributes = dbAttributes.filter((attr) => !attr.system);
|
|
1734
|
+
const dbAttrByName = new Map(systemAttributesFromDB.map((attr) => [attr.name, attr]));
|
|
1735
|
+
const systemAttrs = nativeObject.attributes.map((registryAttr) => {
|
|
1736
|
+
const dbAttr = dbAttrByName.get(registryAttr.name);
|
|
1737
|
+
if (dbAttr) {
|
|
1738
|
+
return {
|
|
1739
|
+
...registryAttr,
|
|
1740
|
+
id: dbAttr.id
|
|
1741
|
+
};
|
|
1742
|
+
}
|
|
1743
|
+
return registryAttr;
|
|
1744
|
+
});
|
|
1745
|
+
const customAttrs = customAttributes.map(
|
|
1746
|
+
(dbAttr) => this.convertDBAttributeToAttribute(dbAttr)
|
|
1747
|
+
);
|
|
1748
|
+
return {
|
|
1749
|
+
...nativeObject,
|
|
1750
|
+
id: dbObject.id,
|
|
1751
|
+
attributes: [
|
|
1752
|
+
...systemAttrs,
|
|
1753
|
+
// System attributes from registry (with DB IDs if synced)
|
|
1754
|
+
...customAttrs
|
|
1755
|
+
// Custom attributes from DB
|
|
1756
|
+
]
|
|
1757
|
+
};
|
|
1758
|
+
}
|
|
1759
|
+
/**
|
|
1760
|
+
* Convert DB object + attributes to ObjectDefinition
|
|
1761
|
+
* @internal
|
|
1762
|
+
*/
|
|
1763
|
+
convertDBObjectToDefinition(dbObject, dbAttributes) {
|
|
1764
|
+
const attributes = dbAttributes.map(
|
|
1765
|
+
(dbAttr) => this.convertDBAttributeToAttribute(dbAttr)
|
|
1766
|
+
);
|
|
1767
|
+
const titleAttribute = dbObject.metadata?.titleAttribute ?? attributes.find((attr) => attr.type === "text")?.name ?? attributes[0]?.name ?? "name";
|
|
1768
|
+
return {
|
|
1769
|
+
id: dbObject.id,
|
|
1770
|
+
name: dbObject.name,
|
|
1771
|
+
label: dbObject.label,
|
|
1772
|
+
description: dbObject.description,
|
|
1773
|
+
icon: dbObject.metadata?.icon,
|
|
1774
|
+
titleAttribute,
|
|
1775
|
+
attributes,
|
|
1776
|
+
system: dbObject.system,
|
|
1777
|
+
metadata: dbObject.metadata
|
|
1778
|
+
};
|
|
1779
|
+
}
|
|
1780
|
+
/**
|
|
1781
|
+
* Convert DB attribute to Attribute type
|
|
1782
|
+
* @internal
|
|
1783
|
+
*/
|
|
1784
|
+
convertDBAttributeToAttribute(dbAttr) {
|
|
1785
|
+
const baseAttr = {
|
|
1786
|
+
...dbAttr.config,
|
|
1787
|
+
id: dbAttr.id,
|
|
1788
|
+
name: dbAttr.name,
|
|
1789
|
+
label: dbAttr.label,
|
|
1790
|
+
type: dbAttr.type,
|
|
1791
|
+
required: dbAttr.required,
|
|
1792
|
+
system: dbAttr.system,
|
|
1793
|
+
unique: dbAttr.unique
|
|
1794
|
+
};
|
|
1795
|
+
return baseAttr;
|
|
1796
|
+
}
|
|
1797
|
+
};
|
|
1798
|
+
|
|
1799
|
+
// src/native/registry.ts
|
|
1800
|
+
var NativeObjectRegistryClass = class {
|
|
1801
|
+
constructor() {
|
|
1802
|
+
this.objects = /* @__PURE__ */ new Map();
|
|
1803
|
+
}
|
|
1804
|
+
/**
|
|
1805
|
+
* Register one or more native objects
|
|
1806
|
+
* Auto-builds if ObjectBuilder is passed instead of ObjectDefinition
|
|
1807
|
+
* @param objects - Single object/builder or array of objects/builders to register
|
|
1808
|
+
* @returns this (for chaining)
|
|
1809
|
+
* @throws Error if object is invalid or already registered
|
|
1810
|
+
*
|
|
1811
|
+
* @example
|
|
1812
|
+
* ```typescript
|
|
1813
|
+
* // No .build() needed - registry auto-builds!
|
|
1814
|
+
* const PRODUCT = object({ id: "obj-product", name: "products", label: "Product" })
|
|
1815
|
+
* .system()
|
|
1816
|
+
* .attribute(text({ id: "attr-name", name: "name", label: "Name" }));
|
|
1817
|
+
*
|
|
1818
|
+
* registry.register(PRODUCT); // ← Auto-builds here
|
|
1819
|
+
*
|
|
1820
|
+
* // Can still explicitly .build() if you want
|
|
1821
|
+
* registry.register(PRODUCT.build());
|
|
1822
|
+
*
|
|
1823
|
+
* // Register multiple
|
|
1824
|
+
* registry.register([PRODUCT, ORDER, CUSTOMER]);
|
|
1825
|
+
* ```
|
|
1826
|
+
*/
|
|
1827
|
+
register(objects) {
|
|
1828
|
+
const objectsArray = Array.isArray(objects) ? objects : [objects];
|
|
1829
|
+
for (const obj of objectsArray) {
|
|
1830
|
+
const object2 = "build" in obj && typeof obj.build === "function" ? obj.build() : obj;
|
|
1831
|
+
this.validateAndRegister(object2);
|
|
1832
|
+
}
|
|
1833
|
+
return this;
|
|
1834
|
+
}
|
|
1835
|
+
/**
|
|
1836
|
+
* Validate and register a single object
|
|
1837
|
+
* @internal
|
|
1838
|
+
*/
|
|
1839
|
+
validateAndRegister(object2) {
|
|
1840
|
+
if (!object2.system) {
|
|
1841
|
+
throw new Error(
|
|
1842
|
+
`[NativeObjectRegistry] Cannot register object "${object2.name}" (id: ${object2.id}): missing system flag.
|
|
1843
|
+
Native objects must have system=true. Did you forget to call .system() in your builder?`
|
|
1844
|
+
);
|
|
1845
|
+
}
|
|
1846
|
+
if (this.objects.has(object2.name)) {
|
|
1847
|
+
const existing = this.objects.get(object2.name);
|
|
1848
|
+
throw new Error(
|
|
1849
|
+
`[NativeObjectRegistry] Duplicate object name "${object2.name}":
|
|
1850
|
+
- Existing: "${existing?.label}" (id: ${existing?.id})
|
|
1851
|
+
- New: "${object2.label}" (id: ${object2.id})
|
|
1852
|
+
Please use unique names for each native object.`
|
|
1853
|
+
);
|
|
1854
|
+
}
|
|
1855
|
+
this.objects.set(object2.name, object2);
|
|
1856
|
+
}
|
|
1857
|
+
/**
|
|
1858
|
+
* Get a native object by its name
|
|
1859
|
+
* @param name - The object name
|
|
1860
|
+
* @returns The object definition or undefined if not found
|
|
1861
|
+
*
|
|
1862
|
+
* @example
|
|
1863
|
+
* ```typescript
|
|
1864
|
+
* const product = registry.getByName("products");
|
|
1865
|
+
* if (product) {
|
|
1866
|
+
* console.log(product.label); // "Product"
|
|
1867
|
+
* }
|
|
1868
|
+
* ```
|
|
1869
|
+
*/
|
|
1870
|
+
getByName(name) {
|
|
1871
|
+
return this.objects.get(name);
|
|
1872
|
+
}
|
|
1873
|
+
/**
|
|
1874
|
+
* Get a native object or throw if not found
|
|
1875
|
+
* @param name - The object name
|
|
1876
|
+
* @returns The object definition
|
|
1877
|
+
* @throws Error if not found
|
|
1878
|
+
*/
|
|
1879
|
+
getByNameOrThrow(name) {
|
|
1880
|
+
const object2 = this.objects.get(name);
|
|
1881
|
+
if (!object2) {
|
|
1882
|
+
throw new Error(
|
|
1883
|
+
`[NativeObjectRegistry] Native object "${name}" not found.
|
|
1884
|
+
Available objects: ${this.listNames().join(", ")}`
|
|
1885
|
+
);
|
|
1886
|
+
}
|
|
1887
|
+
return object2;
|
|
1888
|
+
}
|
|
1889
|
+
/**
|
|
1890
|
+
* Get all registered native objects
|
|
1891
|
+
* @returns Array of all registered object definitions
|
|
1892
|
+
*/
|
|
1893
|
+
getAll() {
|
|
1894
|
+
return Array.from(this.objects.values());
|
|
1895
|
+
}
|
|
1896
|
+
/**
|
|
1897
|
+
* Get all registered native object names
|
|
1898
|
+
* @returns Array of object names
|
|
1899
|
+
*/
|
|
1900
|
+
listNames() {
|
|
1901
|
+
return Array.from(this.objects.keys());
|
|
1902
|
+
}
|
|
1903
|
+
/**
|
|
1904
|
+
* Check if a native object is registered
|
|
1905
|
+
* @param name - The object name
|
|
1906
|
+
* @returns true if registered, false otherwise
|
|
1907
|
+
*/
|
|
1908
|
+
has(name) {
|
|
1909
|
+
return this.objects.has(name);
|
|
1910
|
+
}
|
|
1911
|
+
/**
|
|
1912
|
+
* Get the number of registered objects
|
|
1913
|
+
*/
|
|
1914
|
+
get size() {
|
|
1915
|
+
return this.objects.size;
|
|
1916
|
+
}
|
|
1917
|
+
/**
|
|
1918
|
+
* Clear all registered objects (useful for testing)
|
|
1919
|
+
* @internal
|
|
1920
|
+
*/
|
|
1921
|
+
clear() {
|
|
1922
|
+
this.objects.clear();
|
|
1923
|
+
}
|
|
1924
|
+
/**
|
|
1925
|
+
* Get registry summary for debugging
|
|
1926
|
+
*/
|
|
1927
|
+
summary() {
|
|
1928
|
+
const objects = this.getAll();
|
|
1929
|
+
return [
|
|
1930
|
+
`[NativeObjectRegistry] ${this.size} native object(s) registered:`,
|
|
1931
|
+
...objects.map(
|
|
1932
|
+
(obj) => ` - ${obj.name}: "${obj.label}" (${obj.attributes.length} attributes)`
|
|
1933
|
+
)
|
|
1934
|
+
].join("\n");
|
|
1935
|
+
}
|
|
1936
|
+
/**
|
|
1937
|
+
* Log registry summary to console
|
|
1938
|
+
*/
|
|
1939
|
+
debug() {
|
|
1940
|
+
console.info(this.summary());
|
|
1941
|
+
}
|
|
1942
|
+
};
|
|
1943
|
+
var registry = new NativeObjectRegistryClass();
|
|
1944
|
+
|
|
1945
|
+
// src/validation/validators.ts
|
|
1946
|
+
import { z as z3 } from "zod";
|
|
1947
|
+
function createTextValidator(attr) {
|
|
1948
|
+
let schema = z3.string();
|
|
1949
|
+
if (attr.minLength !== void 0) {
|
|
1950
|
+
schema = schema.min(
|
|
1951
|
+
attr.minLength,
|
|
1952
|
+
`${attr.label} must be at least ${attr.minLength} characters`
|
|
1953
|
+
);
|
|
1954
|
+
}
|
|
1955
|
+
if (attr.maxLength !== void 0) {
|
|
1956
|
+
schema = schema.max(
|
|
1957
|
+
attr.maxLength,
|
|
1958
|
+
`${attr.label} must be at most ${attr.maxLength} characters`
|
|
1959
|
+
);
|
|
1960
|
+
}
|
|
1961
|
+
if (attr.pattern) {
|
|
1962
|
+
schema = schema.regex(new RegExp(attr.pattern), `${attr.label} format is invalid`);
|
|
1963
|
+
}
|
|
1964
|
+
return schema;
|
|
1965
|
+
}
|
|
1966
|
+
function createNumberValidator(attr) {
|
|
1967
|
+
let schema = z3.number();
|
|
1968
|
+
if (attr.min !== void 0) {
|
|
1969
|
+
schema = schema.min(attr.min, `${attr.label} must be at least ${attr.min}`);
|
|
1970
|
+
}
|
|
1971
|
+
if (attr.max !== void 0) {
|
|
1972
|
+
schema = schema.max(attr.max, `${attr.label} must be at most ${attr.max}`);
|
|
1973
|
+
}
|
|
1974
|
+
if (attr.unit === "integer") {
|
|
1975
|
+
schema = schema.int(`${attr.label} must be an integer`);
|
|
1976
|
+
}
|
|
1977
|
+
return schema;
|
|
1978
|
+
}
|
|
1979
|
+
function createCheckboxValidator(_attr) {
|
|
1980
|
+
return z3.boolean();
|
|
1981
|
+
}
|
|
1982
|
+
function createDateValidator(attr) {
|
|
1983
|
+
return z3.string().datetime({ message: `${attr.label} must be a valid ISO date` });
|
|
1984
|
+
}
|
|
1985
|
+
function createPhoneValidator(_attr) {
|
|
1986
|
+
return z3.object({
|
|
1987
|
+
countryCode: z3.string().length(3),
|
|
1988
|
+
phoneNumber: z3.string().min(1)
|
|
1989
|
+
});
|
|
1990
|
+
}
|
|
1991
|
+
function createCurrencyValidator(_attr) {
|
|
1992
|
+
return z3.object({
|
|
1993
|
+
code: z3.string().length(3),
|
|
1994
|
+
value: z3.number().min(0)
|
|
1995
|
+
});
|
|
1996
|
+
}
|
|
1997
|
+
function createStatusValidator(attr) {
|
|
1998
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
1999
|
+
return z3.enum(validValues, {
|
|
2000
|
+
errorMap: () => ({
|
|
2001
|
+
message: `${attr.label} must be one of: ${validValues.join(", ")}`
|
|
2002
|
+
})
|
|
2003
|
+
});
|
|
2004
|
+
}
|
|
2005
|
+
function createSelectValidator(attr) {
|
|
2006
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
2007
|
+
return z3.enum(validValues, {
|
|
2008
|
+
errorMap: () => ({
|
|
2009
|
+
message: `${attr.label} must be one of: ${validValues.join(", ")}`
|
|
2010
|
+
})
|
|
2011
|
+
});
|
|
2012
|
+
}
|
|
2013
|
+
function createMultiselectValidator(attr) {
|
|
2014
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
2015
|
+
return z3.array(
|
|
2016
|
+
z3.enum(validValues, {
|
|
2017
|
+
errorMap: () => ({
|
|
2018
|
+
message: `Each value must be one of: ${validValues.join(", ")}`
|
|
2019
|
+
})
|
|
2020
|
+
})
|
|
2021
|
+
);
|
|
2022
|
+
}
|
|
2023
|
+
function createLocationValidator(_attr) {
|
|
2024
|
+
return z3.object({
|
|
2025
|
+
address: z3.string().optional(),
|
|
2026
|
+
address2: z3.string().optional(),
|
|
2027
|
+
city: z3.string().optional(),
|
|
2028
|
+
state: z3.string().optional(),
|
|
2029
|
+
postalCode: z3.string().optional(),
|
|
2030
|
+
country: z3.string().length(3).optional(),
|
|
2031
|
+
latitude: z3.number().optional(),
|
|
2032
|
+
longitude: z3.number().optional()
|
|
2033
|
+
});
|
|
2034
|
+
}
|
|
2035
|
+
function createTimestampValidator(_attr) {
|
|
2036
|
+
return z3.number().int().positive();
|
|
2037
|
+
}
|
|
2038
|
+
function createFileValidator(_attr) {
|
|
2039
|
+
return z3.string().uuid();
|
|
2040
|
+
}
|
|
2041
|
+
function createUserValidator(_attr) {
|
|
2042
|
+
return z3.string().uuid();
|
|
2043
|
+
}
|
|
2044
|
+
function createSingleRelationValidator(attr) {
|
|
2045
|
+
const uuidSchema = z3.string().uuid({
|
|
2046
|
+
message: `${attr.label} must be a valid record ID`
|
|
2047
|
+
});
|
|
2048
|
+
return z3.union([uuidSchema, z3.null()]);
|
|
2049
|
+
}
|
|
2050
|
+
function createMultiRelationValidator(attr) {
|
|
2051
|
+
const uuidSchema = z3.string().uuid({
|
|
2052
|
+
message: `Each ${attr.label} item must be a valid record ID`
|
|
2053
|
+
});
|
|
2054
|
+
let arraySchema = z3.array(uuidSchema);
|
|
2055
|
+
if (attr.minItems !== void 0) {
|
|
2056
|
+
arraySchema = arraySchema.min(
|
|
2057
|
+
attr.minItems,
|
|
2058
|
+
`${attr.label} must have at least ${attr.minItems} item${attr.minItems > 1 ? "s" : ""}`
|
|
2059
|
+
);
|
|
2060
|
+
}
|
|
2061
|
+
if (attr.maxItems !== void 0) {
|
|
2062
|
+
arraySchema = arraySchema.max(
|
|
2063
|
+
attr.maxItems,
|
|
2064
|
+
`${attr.label} must have at most ${attr.maxItems} item${attr.maxItems > 1 ? "s" : ""}`
|
|
2065
|
+
);
|
|
2066
|
+
}
|
|
2067
|
+
return arraySchema;
|
|
2068
|
+
}
|
|
2069
|
+
function createRelationValidator(attr) {
|
|
2070
|
+
if (attr.cardinality === "many") {
|
|
2071
|
+
return createMultiRelationValidator(attr);
|
|
2072
|
+
}
|
|
2073
|
+
return createSingleRelationValidator(attr);
|
|
2074
|
+
}
|
|
2075
|
+
function createRatingValidator(attr) {
|
|
2076
|
+
let schema = z3.number().int().min(0);
|
|
2077
|
+
if (attr.max !== void 0) {
|
|
2078
|
+
schema = schema.max(attr.max, `${attr.label} must be at most ${attr.max}`);
|
|
2079
|
+
}
|
|
2080
|
+
return schema;
|
|
2081
|
+
}
|
|
2082
|
+
function createAttributeValidator(attr) {
|
|
2083
|
+
switch (attr.type) {
|
|
2084
|
+
case "text":
|
|
2085
|
+
return createTextValidator(attr);
|
|
2086
|
+
case "number":
|
|
2087
|
+
return createNumberValidator(attr);
|
|
2088
|
+
case "checkbox":
|
|
2089
|
+
return createCheckboxValidator(attr);
|
|
2090
|
+
case "date":
|
|
2091
|
+
return createDateValidator(attr);
|
|
2092
|
+
case "phone":
|
|
2093
|
+
return createPhoneValidator(attr);
|
|
2094
|
+
case "currency":
|
|
2095
|
+
return createCurrencyValidator(attr);
|
|
2096
|
+
case "status":
|
|
2097
|
+
return createStatusValidator(attr);
|
|
2098
|
+
case "location":
|
|
2099
|
+
return createLocationValidator(attr);
|
|
2100
|
+
case "timestamp":
|
|
2101
|
+
return createTimestampValidator(attr);
|
|
2102
|
+
case "select":
|
|
2103
|
+
return createSelectValidator(attr);
|
|
2104
|
+
case "multiselect":
|
|
2105
|
+
return createMultiselectValidator(attr);
|
|
2106
|
+
case "file":
|
|
2107
|
+
return createFileValidator(attr);
|
|
2108
|
+
case "user":
|
|
2109
|
+
return createUserValidator(attr);
|
|
2110
|
+
case "relation":
|
|
2111
|
+
return createRelationValidator(attr);
|
|
2112
|
+
case "rating":
|
|
2113
|
+
return createRatingValidator(attr);
|
|
2114
|
+
default:
|
|
2115
|
+
return z3.unknown();
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
function createObjectValidator(objectDef) {
|
|
2119
|
+
const shape = {};
|
|
2120
|
+
for (const attr of objectDef.attributes) {
|
|
2121
|
+
let validator = createAttributeValidator(attr);
|
|
2122
|
+
if (!attr.required) {
|
|
2123
|
+
validator = validator.optional();
|
|
2124
|
+
}
|
|
2125
|
+
shape[attr.name] = validator;
|
|
2126
|
+
}
|
|
2127
|
+
return z3.object(shape);
|
|
2128
|
+
}
|
|
2129
|
+
function validateAttribute(attr, value) {
|
|
2130
|
+
const validator = createAttributeValidator(attr);
|
|
2131
|
+
if (!attr.required && (value === void 0 || value === null)) {
|
|
2132
|
+
return { success: true, data: { [attr.name]: value } };
|
|
2133
|
+
}
|
|
2134
|
+
const result = validator.safeParse(value);
|
|
2135
|
+
if (result.success) {
|
|
2136
|
+
return {
|
|
2137
|
+
success: true,
|
|
2138
|
+
data: { [attr.name]: result.data }
|
|
2139
|
+
};
|
|
2140
|
+
}
|
|
2141
|
+
return {
|
|
2142
|
+
success: false,
|
|
2143
|
+
errors: result.error.errors.map((err) => ({
|
|
2144
|
+
path: [attr.name, ...err.path.map(String)],
|
|
2145
|
+
message: err.message
|
|
2146
|
+
}))
|
|
2147
|
+
};
|
|
2148
|
+
}
|
|
2149
|
+
function validateObject(objectDef, data) {
|
|
2150
|
+
const validator = createObjectValidator(objectDef);
|
|
2151
|
+
const result = validator.safeParse(data);
|
|
2152
|
+
if (result.success) {
|
|
2153
|
+
return {
|
|
2154
|
+
success: true,
|
|
2155
|
+
data: result.data
|
|
2156
|
+
};
|
|
2157
|
+
}
|
|
2158
|
+
return {
|
|
2159
|
+
success: false,
|
|
2160
|
+
errors: result.error.errors.map((err) => ({
|
|
2161
|
+
path: err.path.map(String),
|
|
2162
|
+
message: err.message
|
|
2163
|
+
}))
|
|
2164
|
+
};
|
|
2165
|
+
}
|
|
2166
|
+
function validateObjectOrThrow(objectDef, data) {
|
|
2167
|
+
const result = validateObject(objectDef, data);
|
|
2168
|
+
if (!result.success) {
|
|
2169
|
+
const errorMessages = result.errors?.map((err) => `${err.path.join(".")}: ${err.message}`).join("\n") || "Unknown validation error";
|
|
2170
|
+
throw new Error(`Validation failed for ${objectDef.label}:
|
|
2171
|
+
${errorMessages}`);
|
|
2172
|
+
}
|
|
2173
|
+
return result.data;
|
|
2174
|
+
}
|
|
2175
|
+
function createDraftValidator(objectDef) {
|
|
2176
|
+
const shape = {};
|
|
2177
|
+
for (const attr of objectDef.attributes) {
|
|
2178
|
+
const validator = createAttributeValidator(attr).optional();
|
|
2179
|
+
shape[attr.name] = validator;
|
|
2180
|
+
}
|
|
2181
|
+
return z3.object(shape);
|
|
2182
|
+
}
|
|
2183
|
+
function validateDraft(objectDef, data) {
|
|
2184
|
+
const validator = createDraftValidator(objectDef);
|
|
2185
|
+
const result = validator.safeParse(data);
|
|
2186
|
+
if (result.success) {
|
|
2187
|
+
return {
|
|
2188
|
+
success: true,
|
|
2189
|
+
data: result.data
|
|
2190
|
+
};
|
|
2191
|
+
}
|
|
2192
|
+
return {
|
|
2193
|
+
success: false,
|
|
2194
|
+
errors: result.error.errors.map((err) => ({
|
|
2195
|
+
path: err.path.map(String),
|
|
2196
|
+
message: err.message
|
|
2197
|
+
}))
|
|
2198
|
+
};
|
|
2199
|
+
}
|
|
2200
|
+
function validateDraftOrThrow(objectDef, data) {
|
|
2201
|
+
const result = validateDraft(objectDef, data);
|
|
2202
|
+
if (!result.success) {
|
|
2203
|
+
const errorMessages = result.errors?.map((err) => `${err.path.join(".")}: ${err.message}`).join("\n") || "Unknown validation error";
|
|
2204
|
+
throw new Error(`Draft validation failed for ${objectDef.label}:
|
|
2205
|
+
${errorMessages}`);
|
|
2206
|
+
}
|
|
2207
|
+
return result.data;
|
|
2208
|
+
}
|
|
2209
|
+
function isValuePresent(value) {
|
|
2210
|
+
if (value === void 0 || value === null) {
|
|
2211
|
+
return false;
|
|
2212
|
+
}
|
|
2213
|
+
if (typeof value === "string" && value.trim() === "") {
|
|
2214
|
+
return false;
|
|
2215
|
+
}
|
|
2216
|
+
return true;
|
|
2217
|
+
}
|
|
2218
|
+
function getMissingRequiredAttributes(objectDef, data) {
|
|
2219
|
+
const missing = [];
|
|
2220
|
+
for (const attr of objectDef.attributes) {
|
|
2221
|
+
if (attr.required && !isValuePresent(data[attr.name])) {
|
|
2222
|
+
missing.push(attr);
|
|
2223
|
+
}
|
|
2224
|
+
}
|
|
2225
|
+
return missing;
|
|
2226
|
+
}
|
|
2227
|
+
function isRecordComplete(objectDef, data) {
|
|
2228
|
+
const missing = getMissingRequiredAttributes(objectDef, data);
|
|
2229
|
+
if (missing.length > 0) {
|
|
2230
|
+
return false;
|
|
2231
|
+
}
|
|
2232
|
+
const validation = validateObject(objectDef, data);
|
|
2233
|
+
return validation.success;
|
|
2234
|
+
}
|
|
2235
|
+
function computeRecordStatus(objectDef, data) {
|
|
2236
|
+
return isRecordComplete(objectDef, data) ? "complete" : "draft";
|
|
2237
|
+
}
|
|
2238
|
+
|
|
2239
|
+
// src/runtime/services/relation.service.ts
|
|
2240
|
+
var RelationService = class {
|
|
2241
|
+
constructor(adapter, nativeRegistry) {
|
|
2242
|
+
this.adapter = adapter;
|
|
2243
|
+
this.schemaService = new ObjectSchemaService(adapter, nativeRegistry);
|
|
2244
|
+
}
|
|
2245
|
+
/**
|
|
2246
|
+
* Validate all relation attributes in the data
|
|
2247
|
+
*
|
|
2248
|
+
* @param schema - Object schema containing attribute definitions
|
|
2249
|
+
* @param data - Record data to validate
|
|
2250
|
+
* @returns Validation result with errors if any
|
|
2251
|
+
*
|
|
2252
|
+
* @example
|
|
2253
|
+
* ```typescript
|
|
2254
|
+
* const result = await relationService.validateRelations(schema, {
|
|
2255
|
+
* company: "rec-123",
|
|
2256
|
+
* contacts: ["rec-456", "rec-789"]
|
|
2257
|
+
* });
|
|
2258
|
+
*
|
|
2259
|
+
* if (!result.valid) {
|
|
2260
|
+
* console.log(result.errors);
|
|
2261
|
+
* // [{ attribute: "company", message: "Record not found", invalidIds: ["rec-123"] }]
|
|
2262
|
+
* }
|
|
2263
|
+
* ```
|
|
2264
|
+
*/
|
|
2265
|
+
async validateRelations(schema, data) {
|
|
2266
|
+
const errors = [];
|
|
2267
|
+
const relationAttrs = schema.attributes.filter(
|
|
2268
|
+
(attr) => attr.type === "relation"
|
|
2269
|
+
);
|
|
2270
|
+
for (const attr of relationAttrs) {
|
|
2271
|
+
const value = data[attr.name];
|
|
2272
|
+
if (value === void 0 || value === null) {
|
|
2273
|
+
continue;
|
|
2274
|
+
}
|
|
2275
|
+
const attrErrors = await this.validateRelationAttribute(attr, value);
|
|
2276
|
+
errors.push(...attrErrors);
|
|
2277
|
+
}
|
|
2278
|
+
return {
|
|
2279
|
+
valid: errors.length === 0,
|
|
2280
|
+
errors
|
|
2281
|
+
};
|
|
2282
|
+
}
|
|
2283
|
+
/**
|
|
2284
|
+
* Validate a single relation attribute value
|
|
2285
|
+
*/
|
|
2286
|
+
async validateRelationAttribute(attr, value) {
|
|
2287
|
+
const errors = [];
|
|
2288
|
+
const ids = this.extractIds(attr, value);
|
|
2289
|
+
if (ids.length === 0) {
|
|
2290
|
+
return errors;
|
|
2291
|
+
}
|
|
2292
|
+
const validObjectIds = await this.getValidObjectIds(attr.targets);
|
|
2293
|
+
if (validObjectIds.size === 0) {
|
|
2294
|
+
errors.push({
|
|
2295
|
+
attribute: attr.name,
|
|
2296
|
+
message: `No valid target objects found for ${attr.label}`
|
|
2297
|
+
});
|
|
2298
|
+
return errors;
|
|
2299
|
+
}
|
|
2300
|
+
const invalidIds = [];
|
|
2301
|
+
for (const id of ids) {
|
|
2302
|
+
const record = await this.adapter.objectRecords.findById(id);
|
|
2303
|
+
if (!record) {
|
|
2304
|
+
invalidIds.push(id);
|
|
2305
|
+
continue;
|
|
2306
|
+
}
|
|
2307
|
+
if (!validObjectIds.has(record.objectId)) {
|
|
2308
|
+
invalidIds.push(id);
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
if (invalidIds.length > 0) {
|
|
2312
|
+
errors.push({
|
|
2313
|
+
attribute: attr.name,
|
|
2314
|
+
message: `Invalid or non-existent records for ${attr.label}`,
|
|
2315
|
+
invalidIds
|
|
2316
|
+
});
|
|
2317
|
+
}
|
|
2318
|
+
return errors;
|
|
2319
|
+
}
|
|
2320
|
+
/**
|
|
2321
|
+
* Extract IDs from relation value based on cardinality
|
|
2322
|
+
*/
|
|
2323
|
+
extractIds(attr, value) {
|
|
2324
|
+
if (attr.cardinality === "many") {
|
|
2325
|
+
if (!Array.isArray(value)) {
|
|
2326
|
+
return [];
|
|
2327
|
+
}
|
|
2328
|
+
return value.filter((v) => typeof v === "string");
|
|
2329
|
+
}
|
|
2330
|
+
if (typeof value === "string") {
|
|
2331
|
+
return [value];
|
|
2332
|
+
}
|
|
2333
|
+
return [];
|
|
2334
|
+
}
|
|
2335
|
+
/**
|
|
2336
|
+
* Get valid object IDs from relation targets
|
|
2337
|
+
*/
|
|
2338
|
+
async getValidObjectIds(targets) {
|
|
2339
|
+
const objectIds = /* @__PURE__ */ new Set();
|
|
2340
|
+
for (const target of targets) {
|
|
2341
|
+
try {
|
|
2342
|
+
const objectSchema = await this.schemaService.getObjectSchemaByName(target.object);
|
|
2343
|
+
if (objectSchema?.id) {
|
|
2344
|
+
objectIds.add(objectSchema.id);
|
|
2345
|
+
}
|
|
2346
|
+
} catch {
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
return objectIds;
|
|
2350
|
+
}
|
|
2351
|
+
/**
|
|
2352
|
+
* Validate relations and throw if invalid
|
|
2353
|
+
*/
|
|
2354
|
+
async validateRelationsOrThrow(schema, data) {
|
|
2355
|
+
const result = await this.validateRelations(schema, data);
|
|
2356
|
+
if (!result.valid) {
|
|
2357
|
+
const messages = result.errors.map((e) => `${e.attribute}: ${e.message}`).join("; ");
|
|
2358
|
+
throw new Error(`Relation validation failed: ${messages}`);
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2361
|
+
// ============================================================================
|
|
2362
|
+
// OPTIONS & RESOLUTION
|
|
2363
|
+
// ============================================================================
|
|
2364
|
+
/**
|
|
2365
|
+
* Get available options for a relation attribute
|
|
2366
|
+
* Searches across all target objects defined in the relation
|
|
2367
|
+
*
|
|
2368
|
+
* @param attribute - Relation attribute definition
|
|
2369
|
+
* @param tenantId - Tenant ID for multi-tenant isolation
|
|
2370
|
+
* @param params - Query parameters
|
|
2371
|
+
*
|
|
2372
|
+
* @example
|
|
2373
|
+
* ```typescript
|
|
2374
|
+
* const options = await relationService.getOptions(attribute, "tenant-1", {
|
|
2375
|
+
* query: "nike",
|
|
2376
|
+
* page: 1,
|
|
2377
|
+
* pageSize: 20
|
|
2378
|
+
* });
|
|
2379
|
+
* ```
|
|
2380
|
+
*/
|
|
2381
|
+
async getOptions(attribute, tenantId, params = {}) {
|
|
2382
|
+
const { query = "", page = 1, pageSize = 20, targetObject } = params;
|
|
2383
|
+
const targets = attribute.targets;
|
|
2384
|
+
const filteredTargets = targetObject ? targets.filter((t) => t.object === targetObject) : targets;
|
|
2385
|
+
if (filteredTargets.length === 0) {
|
|
2386
|
+
return { options: [], hasMore: false, total: 0 };
|
|
2387
|
+
}
|
|
2388
|
+
const allOptions = [];
|
|
2389
|
+
let totalCount = 0;
|
|
2390
|
+
const recordService = new RecordService(this.adapter, tenantId);
|
|
2391
|
+
for (const target of filteredTargets) {
|
|
2392
|
+
const objectSchema = await this.schemaService.getObjectSchemaByName(target.object);
|
|
2393
|
+
if (!objectSchema?.id) {
|
|
2394
|
+
continue;
|
|
2395
|
+
}
|
|
2396
|
+
const result = query ? await recordService.searchRecords(objectSchema.id, query, {
|
|
2397
|
+
limit: pageSize,
|
|
2398
|
+
offset: (page - 1) * pageSize
|
|
2399
|
+
}) : await recordService.listRecords(objectSchema.id, {
|
|
2400
|
+
limit: pageSize,
|
|
2401
|
+
offset: (page - 1) * pageSize
|
|
2402
|
+
});
|
|
2403
|
+
totalCount += result.total;
|
|
2404
|
+
for (const record of result.records) {
|
|
2405
|
+
const label = this.resolveLabel(
|
|
2406
|
+
record.values,
|
|
2407
|
+
objectSchema.titleAttribute,
|
|
2408
|
+
target.displayTemplate
|
|
2409
|
+
);
|
|
2410
|
+
allOptions.push({
|
|
2411
|
+
id: record.id,
|
|
2412
|
+
objectId: objectSchema.id,
|
|
2413
|
+
objectName: objectSchema.name,
|
|
2414
|
+
objectLabel: objectSchema.label,
|
|
2415
|
+
objectIcon: objectSchema.icon,
|
|
2416
|
+
label,
|
|
2417
|
+
data: record.values
|
|
2418
|
+
});
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2421
|
+
const hasMore = totalCount > page * pageSize;
|
|
2422
|
+
return {
|
|
2423
|
+
options: allOptions.slice(0, pageSize),
|
|
2424
|
+
hasMore,
|
|
2425
|
+
total: totalCount
|
|
2426
|
+
};
|
|
2427
|
+
}
|
|
2428
|
+
/**
|
|
2429
|
+
* Resolve record IDs to their display labels
|
|
2430
|
+
* Useful for displaying current values in the UI
|
|
2431
|
+
*
|
|
2432
|
+
* @param ids - Record IDs to resolve
|
|
2433
|
+
* @param tenantId - Tenant ID for multi-tenant isolation
|
|
2434
|
+
*
|
|
2435
|
+
* @example
|
|
2436
|
+
* ```typescript
|
|
2437
|
+
* const resolved = await relationService.resolveIds(["rec-1", "rec-2"], "tenant-1");
|
|
2438
|
+
* // [{ id: "rec-1", label: "Nike Air Max", objectName: "products", ... }]
|
|
2439
|
+
* ```
|
|
2440
|
+
*/
|
|
2441
|
+
async resolveIds(ids, tenantId) {
|
|
2442
|
+
if (!ids || ids.length === 0) {
|
|
2443
|
+
return [];
|
|
2444
|
+
}
|
|
2445
|
+
const recordService = new RecordService(this.adapter, tenantId);
|
|
2446
|
+
const resolved = [];
|
|
2447
|
+
for (const id of ids) {
|
|
2448
|
+
const record = await recordService.getRecord(id);
|
|
2449
|
+
if (!record) {
|
|
2450
|
+
continue;
|
|
2451
|
+
}
|
|
2452
|
+
const objectSchema = await this.schemaService.getObjectSchema(record.objectId);
|
|
2453
|
+
if (!objectSchema) {
|
|
2454
|
+
continue;
|
|
2455
|
+
}
|
|
2456
|
+
const label = this.resolveLabel(record.values, objectSchema.titleAttribute);
|
|
2457
|
+
resolved.push({
|
|
2458
|
+
id: record.id,
|
|
2459
|
+
objectId: record.objectId,
|
|
2460
|
+
objectName: objectSchema.name,
|
|
2461
|
+
objectLabel: objectSchema.label,
|
|
2462
|
+
objectIcon: objectSchema.icon,
|
|
2463
|
+
label,
|
|
2464
|
+
data: record.values
|
|
2465
|
+
});
|
|
2466
|
+
}
|
|
2467
|
+
return resolved;
|
|
2468
|
+
}
|
|
2469
|
+
/**
|
|
2470
|
+
* Find a relation attribute by ID
|
|
2471
|
+
*/
|
|
2472
|
+
async findAttributeById(attributeId) {
|
|
2473
|
+
const attribute = await this.adapter.attributes.findById(attributeId);
|
|
2474
|
+
if (!attribute || attribute.type !== "relation") {
|
|
2475
|
+
return null;
|
|
2476
|
+
}
|
|
2477
|
+
return {
|
|
2478
|
+
...attribute.config,
|
|
2479
|
+
...attribute
|
|
2480
|
+
};
|
|
2481
|
+
}
|
|
2482
|
+
/**
|
|
2483
|
+
* Resolve display label from record values
|
|
2484
|
+
* Uses displayTemplate if provided, otherwise falls back to titleAttribute
|
|
2485
|
+
*/
|
|
2486
|
+
resolveLabel(values, titleAttribute, displayTemplate) {
|
|
2487
|
+
if (displayTemplate) {
|
|
2488
|
+
return displayTemplate.replace(/\{(\w+)\}/g, (_, key) => {
|
|
2489
|
+
const value = values[key];
|
|
2490
|
+
return value !== void 0 && value !== null ? String(value) : "";
|
|
2491
|
+
});
|
|
2492
|
+
}
|
|
2493
|
+
const titleValue = values[titleAttribute];
|
|
2494
|
+
return titleValue !== void 0 && titleValue !== null ? String(titleValue) : "(Untitled)";
|
|
2495
|
+
}
|
|
2496
|
+
};
|
|
2497
|
+
|
|
2498
|
+
// src/runtime/services/record.service.ts
|
|
2499
|
+
var RecordService = class {
|
|
2500
|
+
constructor(adapter, tenantId) {
|
|
2501
|
+
this.adapter = adapter;
|
|
2502
|
+
this.tenantId = tenantId;
|
|
2503
|
+
this.schemaService = new ObjectSchemaService(adapter, registry);
|
|
2504
|
+
this.relationService = new RelationService(adapter, registry);
|
|
2505
|
+
}
|
|
2506
|
+
/**
|
|
2507
|
+
* Create a new record with validation
|
|
2508
|
+
*
|
|
2509
|
+
* @param objectId - Object UUID
|
|
2510
|
+
* @param data - Record data (attribute values)
|
|
2511
|
+
* @param options - Creation options
|
|
2512
|
+
* @returns Created record with computed completionStatus
|
|
2513
|
+
*
|
|
2514
|
+
* @example
|
|
2515
|
+
* ```typescript
|
|
2516
|
+
* const service = new RecordService(adapter, "tenant-123");
|
|
2517
|
+
*
|
|
2518
|
+
* // Create a complete record (strict validation)
|
|
2519
|
+
* const product = await service.createRecord("obj-product", {
|
|
2520
|
+
* name: "Nike Air Max",
|
|
2521
|
+
* price: 129.99,
|
|
2522
|
+
* status: "active"
|
|
2523
|
+
* });
|
|
2524
|
+
* // → product.completionStatus = "complete"
|
|
2525
|
+
*
|
|
2526
|
+
* // Create a draft record (allows missing required fields)
|
|
2527
|
+
* const draft = await service.createRecord("obj-product", {
|
|
2528
|
+
* name: "Draft Product"
|
|
2529
|
+
* }, { allowDraft: true });
|
|
2530
|
+
* // → draft.completionStatus = "draft"
|
|
2531
|
+
* ```
|
|
2532
|
+
*/
|
|
2533
|
+
async createRecord(objectId, data, options) {
|
|
2534
|
+
const schema = await this.schemaService.getObjectSchema(objectId);
|
|
2535
|
+
if (options?.validate !== false) {
|
|
2536
|
+
if (options?.allowDraft) {
|
|
2537
|
+
validateDraftOrThrow(schema, data);
|
|
2538
|
+
} else {
|
|
2539
|
+
validateObjectOrThrow(schema, data);
|
|
2540
|
+
}
|
|
2541
|
+
if (!options?.skipRelationValidation) {
|
|
2542
|
+
await this.relationService.validateRelationsOrThrow(schema, data);
|
|
2543
|
+
}
|
|
2544
|
+
}
|
|
2545
|
+
const completionStatus = computeRecordStatus(schema, data);
|
|
2546
|
+
const record = await this.adapter.objectRecords.create({
|
|
2547
|
+
tenantId: this.tenantId,
|
|
2548
|
+
objectId,
|
|
2549
|
+
data,
|
|
2550
|
+
completionStatus
|
|
2551
|
+
});
|
|
2552
|
+
return record;
|
|
2553
|
+
}
|
|
2554
|
+
/**
|
|
2555
|
+
* Get a record by ID
|
|
2556
|
+
*
|
|
2557
|
+
* @param recordId - Record UUID
|
|
2558
|
+
* @param options - Query options
|
|
2559
|
+
* @returns Record or null if not found
|
|
2560
|
+
*/
|
|
2561
|
+
async getRecord(recordId, options) {
|
|
2562
|
+
const record = await this.adapter.objectRecords.findById(recordId);
|
|
2563
|
+
if (!record) {
|
|
2564
|
+
return null;
|
|
2565
|
+
}
|
|
2566
|
+
if (options?.includeSchema) {
|
|
2567
|
+
const schema = await this.schemaService.getObjectSchema(record.objectId);
|
|
2568
|
+
const recordWithSchema = record;
|
|
2569
|
+
recordWithSchema.schema = schema;
|
|
2570
|
+
return recordWithSchema;
|
|
2571
|
+
}
|
|
2572
|
+
return record;
|
|
2573
|
+
}
|
|
2574
|
+
/**
|
|
2575
|
+
* Get a record by ID or throw if not found
|
|
2576
|
+
*/
|
|
2577
|
+
async getRecordOrThrow(recordId) {
|
|
2578
|
+
const record = await this.getRecord(recordId);
|
|
2579
|
+
if (!record) {
|
|
2580
|
+
throw new Error(`Record with id "${recordId}" not found`);
|
|
2581
|
+
}
|
|
2582
|
+
return record;
|
|
2583
|
+
}
|
|
2584
|
+
/**
|
|
2585
|
+
* Update a record with validation
|
|
2586
|
+
*
|
|
2587
|
+
* The completion status is automatically recalculated after each update.
|
|
2588
|
+
* A draft record becomes complete when all required fields are filled.
|
|
2589
|
+
*
|
|
2590
|
+
* @param recordId - Record UUID
|
|
2591
|
+
* @param data - Partial data to update
|
|
2592
|
+
* @param options - Update options
|
|
2593
|
+
* @returns Updated record with recalculated completionStatus
|
|
2594
|
+
*
|
|
2595
|
+
* @example
|
|
2596
|
+
* ```typescript
|
|
2597
|
+
* // Update a draft record to make it complete
|
|
2598
|
+
* const updated = await service.updateRecord(draftId, {
|
|
2599
|
+
* price: 99.99,
|
|
2600
|
+
* status: "active"
|
|
2601
|
+
* });
|
|
2602
|
+
* // → updated.completionStatus = "complete" if all required fields now present
|
|
2603
|
+
* ```
|
|
2604
|
+
*/
|
|
2605
|
+
async updateRecord(recordId, data, options) {
|
|
2606
|
+
const existing = await this.getRecordOrThrow(recordId);
|
|
2607
|
+
const schema = await this.schemaService.getObjectSchema(existing.objectId);
|
|
2608
|
+
const mergedData = { ...existing.values, ...data };
|
|
2609
|
+
if (options?.validate !== false) {
|
|
2610
|
+
if (options?.partial) {
|
|
2611
|
+
validateDraftOrThrow(schema, mergedData);
|
|
2612
|
+
} else {
|
|
2613
|
+
validateObjectOrThrow(schema, mergedData);
|
|
2614
|
+
}
|
|
2615
|
+
if (!options?.skipRelationValidation) {
|
|
2616
|
+
await this.relationService.validateRelationsOrThrow(schema, data);
|
|
2617
|
+
}
|
|
2618
|
+
}
|
|
2619
|
+
const completionStatus = computeRecordStatus(schema, mergedData);
|
|
2620
|
+
const updated = await this.adapter.objectRecords.update(recordId, {
|
|
2621
|
+
...data,
|
|
2622
|
+
__completionStatus: completionStatus
|
|
2623
|
+
});
|
|
2624
|
+
return updated;
|
|
2625
|
+
}
|
|
2626
|
+
/**
|
|
2627
|
+
* Delete a record
|
|
2628
|
+
*
|
|
2629
|
+
* @param recordId - Record UUID
|
|
2630
|
+
* @param options - Delete options
|
|
2631
|
+
*/
|
|
2632
|
+
async deleteRecord(recordId, options) {
|
|
2633
|
+
if (options?.checkSystem) {
|
|
2634
|
+
const record = await this.getRecordOrThrow(recordId);
|
|
2635
|
+
const schema = await this.schemaService.getObjectSchema(record.objectId);
|
|
2636
|
+
if (schema.system) {
|
|
2637
|
+
throw new Error(`Cannot delete record of system object "${schema.label}"`);
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
await this.adapter.objectRecords.delete(recordId);
|
|
2641
|
+
}
|
|
2642
|
+
/**
|
|
2643
|
+
* List records for an object with pagination
|
|
2644
|
+
*
|
|
2645
|
+
* @param objectId - Object UUID
|
|
2646
|
+
* @param options - List options
|
|
2647
|
+
* @returns Records and total count
|
|
2648
|
+
*/
|
|
2649
|
+
async listRecords(objectId, options) {
|
|
2650
|
+
return await this.adapter.objectRecords.list(this.tenantId, objectId, options);
|
|
2651
|
+
}
|
|
2652
|
+
/**
|
|
2653
|
+
* Search records using full-text search
|
|
2654
|
+
*
|
|
2655
|
+
* @param objectId - Object UUID
|
|
2656
|
+
* @param query - Search query
|
|
2657
|
+
* @param options - Search options
|
|
2658
|
+
* @returns Matching records and total count
|
|
2659
|
+
*/
|
|
2660
|
+
async searchRecords(objectId, query, options) {
|
|
2661
|
+
return await this.adapter.objectRecords.search(this.tenantId, objectId, query, options);
|
|
2662
|
+
}
|
|
2663
|
+
/**
|
|
2664
|
+
* Validate data against object schema without saving
|
|
2665
|
+
*
|
|
2666
|
+
* @param objectId - Object UUID
|
|
2667
|
+
* @param data - Data to validate
|
|
2668
|
+
* @returns Validation result
|
|
2669
|
+
*/
|
|
2670
|
+
async validateData(objectId, data) {
|
|
2671
|
+
const schema = await this.schemaService.getObjectSchema(objectId);
|
|
2672
|
+
return validateObject(schema, data);
|
|
2673
|
+
}
|
|
2674
|
+
/**
|
|
2675
|
+
* Compute the completion status for given data without saving.
|
|
2676
|
+
* Useful for UI to show draft/complete status before submitting.
|
|
2677
|
+
*
|
|
2678
|
+
* @param objectId - Object UUID
|
|
2679
|
+
* @param data - Data to check
|
|
2680
|
+
* @returns Computed completion status
|
|
2681
|
+
*/
|
|
2682
|
+
async computeStatus(objectId, data) {
|
|
2683
|
+
const schema = await this.schemaService.getObjectSchema(objectId);
|
|
2684
|
+
return computeRecordStatus(schema, data);
|
|
2685
|
+
}
|
|
2686
|
+
/**
|
|
2687
|
+
* Refresh the completion status of an existing record.
|
|
2688
|
+
* Useful when schema changes and you need to recompute statuses.
|
|
2689
|
+
*
|
|
2690
|
+
* @param recordId - Record UUID
|
|
2691
|
+
* @returns Updated completion status
|
|
2692
|
+
*/
|
|
2693
|
+
async refreshRecordStatus(recordId) {
|
|
2694
|
+
const record = await this.getRecordOrThrow(recordId);
|
|
2695
|
+
const schema = await this.schemaService.getObjectSchema(record.objectId);
|
|
2696
|
+
const newStatus = computeRecordStatus(schema, record.values);
|
|
2697
|
+
if (record.completionStatus !== newStatus) {
|
|
2698
|
+
await this.adapter.objectRecords.update(recordId, {
|
|
2699
|
+
__completionStatus: newStatus
|
|
2700
|
+
});
|
|
2701
|
+
}
|
|
2702
|
+
return newStatus;
|
|
2703
|
+
}
|
|
2704
|
+
};
|
|
2705
|
+
|
|
2706
|
+
// src/runtime/services/user-profile.service.ts
|
|
2707
|
+
var UserProfileService = class {
|
|
2708
|
+
constructor(adapter, tenantId) {
|
|
2709
|
+
this.adapter = adapter;
|
|
2710
|
+
this.tenantId = tenantId;
|
|
2711
|
+
}
|
|
2712
|
+
/**
|
|
2713
|
+
* Create a new user profile (typically after first auth)
|
|
2714
|
+
*
|
|
2715
|
+
* @param data - User profile creation data
|
|
2716
|
+
* @returns Created user profile
|
|
2717
|
+
*
|
|
2718
|
+
* @example
|
|
2719
|
+
* ```typescript
|
|
2720
|
+
* const service = new UserProfileService(adapter, "tenant-123");
|
|
2721
|
+
*
|
|
2722
|
+
* // After Supabase auth
|
|
2723
|
+
* const profile = await service.createProfile({
|
|
2724
|
+
* tenantId: "tenant-123",
|
|
2725
|
+
* authId: authUser.id,
|
|
2726
|
+
* email: authUser.email,
|
|
2727
|
+
* firstName: authUser.user_metadata.first_name,
|
|
2728
|
+
* lastName: authUser.user_metadata.last_name,
|
|
2729
|
+
* role: "member",
|
|
2730
|
+
* status: "active"
|
|
2731
|
+
* });
|
|
2732
|
+
* ```
|
|
2733
|
+
*/
|
|
2734
|
+
async createProfile(data) {
|
|
2735
|
+
if (data.tenantId !== this.tenantId) {
|
|
2736
|
+
throw new Error(
|
|
2737
|
+
`Tenant mismatch: service initialized with "${this.tenantId}" but data has "${data.tenantId}"`
|
|
2738
|
+
);
|
|
2739
|
+
}
|
|
2740
|
+
const existing = await this.adapter.userProfiles.findByAuthId(data.authId);
|
|
2741
|
+
if (existing) {
|
|
2742
|
+
throw new Error(`User profile with authId "${data.authId}" already exists`);
|
|
2743
|
+
}
|
|
2744
|
+
const existingEmail = await this.adapter.userProfiles.findByEmail(this.tenantId, data.email);
|
|
2745
|
+
if (existingEmail) {
|
|
2746
|
+
throw new Error(`User profile with email "${data.email}" already exists in this tenant`);
|
|
2747
|
+
}
|
|
2748
|
+
return await this.adapter.userProfiles.create(data);
|
|
2749
|
+
}
|
|
2750
|
+
/**
|
|
2751
|
+
* Get user profile by ID
|
|
2752
|
+
*/
|
|
2753
|
+
async getProfile(profileId) {
|
|
2754
|
+
return await this.adapter.userProfiles.findById(profileId);
|
|
2755
|
+
}
|
|
2756
|
+
/**
|
|
2757
|
+
* Get user profile by ID or throw
|
|
2758
|
+
*/
|
|
2759
|
+
async getProfileOrThrow(profileId) {
|
|
2760
|
+
const profile = await this.getProfile(profileId);
|
|
2761
|
+
if (!profile) {
|
|
2762
|
+
throw new Error(`User profile with id "${profileId}" not found`);
|
|
2763
|
+
}
|
|
2764
|
+
return profile;
|
|
2765
|
+
}
|
|
2766
|
+
/**
|
|
2767
|
+
* Get user profile by auth provider ID
|
|
2768
|
+
*
|
|
2769
|
+
* @param authId - Auth provider user ID (Supabase, Clerk, etc.)
|
|
2770
|
+
* @returns User profile or null
|
|
2771
|
+
*/
|
|
2772
|
+
async getProfileByAuthId(authId) {
|
|
2773
|
+
return await this.adapter.userProfiles.findByAuthId(authId);
|
|
2774
|
+
}
|
|
2775
|
+
/**
|
|
2776
|
+
* Get or create user profile (idempotent operation)
|
|
2777
|
+
* Useful for auth callbacks - ensures profile exists
|
|
2778
|
+
*
|
|
2779
|
+
* @param authId - Auth provider user ID
|
|
2780
|
+
* @param data - Profile data to create if doesn't exist
|
|
2781
|
+
* @returns Existing or newly created profile
|
|
2782
|
+
*
|
|
2783
|
+
* @example
|
|
2784
|
+
* ```typescript
|
|
2785
|
+
* // In Supabase auth callback
|
|
2786
|
+
* const profile = await service.getOrCreateProfile(
|
|
2787
|
+
* authUser.id,
|
|
2788
|
+
* {
|
|
2789
|
+
* tenantId: "tenant-123",
|
|
2790
|
+
* authId: authUser.id,
|
|
2791
|
+
* email: authUser.email,
|
|
2792
|
+
* role: "member",
|
|
2793
|
+
* status: "active"
|
|
2794
|
+
* }
|
|
2795
|
+
* );
|
|
2796
|
+
* ```
|
|
2797
|
+
*/
|
|
2798
|
+
async getOrCreateProfile(authId, data) {
|
|
2799
|
+
const existing = await this.getProfileByAuthId(authId);
|
|
2800
|
+
if (existing) {
|
|
2801
|
+
return existing;
|
|
2802
|
+
}
|
|
2803
|
+
return await this.createProfile(data);
|
|
2804
|
+
}
|
|
2805
|
+
/**
|
|
2806
|
+
* Update user profile
|
|
2807
|
+
*/
|
|
2808
|
+
async updateProfile(profileId, data) {
|
|
2809
|
+
await this.getProfileOrThrow(profileId);
|
|
2810
|
+
return await this.adapter.userProfiles.update(profileId, data);
|
|
2811
|
+
}
|
|
2812
|
+
/**
|
|
2813
|
+
* Delete user profile
|
|
2814
|
+
*
|
|
2815
|
+
* @param profileId - Profile UUID
|
|
2816
|
+
* @param options - Delete options
|
|
2817
|
+
*/
|
|
2818
|
+
async deleteProfile(profileId, options) {
|
|
2819
|
+
if (options?.checkAdmin) {
|
|
2820
|
+
const profile = await this.getProfileOrThrow(profileId);
|
|
2821
|
+
if (profile.role === "admin") {
|
|
2822
|
+
const allProfiles = await this.listProfiles();
|
|
2823
|
+
const adminCount = allProfiles.filter((p) => p.role === "admin").length;
|
|
2824
|
+
if (adminCount <= 1) {
|
|
2825
|
+
throw new Error("Cannot delete the last admin user");
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
}
|
|
2829
|
+
await this.adapter.userProfiles.delete(profileId);
|
|
2830
|
+
}
|
|
2831
|
+
/**
|
|
2832
|
+
* List all user profiles for the tenant
|
|
2833
|
+
*/
|
|
2834
|
+
async listProfiles(options) {
|
|
2835
|
+
return await this.adapter.userProfiles.list(this.tenantId, options);
|
|
2836
|
+
}
|
|
2837
|
+
/**
|
|
2838
|
+
* Update last login timestamp
|
|
2839
|
+
*
|
|
2840
|
+
* @param profileId - Profile UUID
|
|
2841
|
+
*
|
|
2842
|
+
* @example
|
|
2843
|
+
* ```typescript
|
|
2844
|
+
* // After successful auth
|
|
2845
|
+
* await service.updateLastLogin(profile.id);
|
|
2846
|
+
* ```
|
|
2847
|
+
*/
|
|
2848
|
+
async updateLastLogin(profileId) {
|
|
2849
|
+
await this.adapter.userProfiles.updateLastLogin(profileId);
|
|
2850
|
+
}
|
|
2851
|
+
/**
|
|
2852
|
+
* Change user role
|
|
2853
|
+
*
|
|
2854
|
+
* @param profileId - Profile UUID
|
|
2855
|
+
* @param newRole - New role
|
|
2856
|
+
*/
|
|
2857
|
+
async changeRole(profileId, newRole) {
|
|
2858
|
+
return await this.updateProfile(profileId, { role: newRole });
|
|
2859
|
+
}
|
|
2860
|
+
/**
|
|
2861
|
+
* Change user status
|
|
2862
|
+
*
|
|
2863
|
+
* @param profileId - Profile UUID
|
|
2864
|
+
* @param newStatus - New status
|
|
2865
|
+
*/
|
|
2866
|
+
async changeStatus(profileId, newStatus) {
|
|
2867
|
+
return await this.updateProfile(profileId, { status: newStatus });
|
|
2868
|
+
}
|
|
2869
|
+
/**
|
|
2870
|
+
* Get user by email
|
|
2871
|
+
*/
|
|
2872
|
+
async getProfileByEmail(email) {
|
|
2873
|
+
return await this.adapter.userProfiles.findByEmail(this.tenantId, email);
|
|
2874
|
+
}
|
|
2875
|
+
/**
|
|
2876
|
+
* Check if user has role
|
|
2877
|
+
*/
|
|
2878
|
+
async hasRole(profileId, role) {
|
|
2879
|
+
const profile = await this.getProfile(profileId);
|
|
2880
|
+
return profile?.role === role;
|
|
2881
|
+
}
|
|
2882
|
+
/**
|
|
2883
|
+
* Check if user is admin
|
|
2884
|
+
*/
|
|
2885
|
+
async isAdmin(profileId) {
|
|
2886
|
+
return await this.hasRole(profileId, "admin");
|
|
2887
|
+
}
|
|
2888
|
+
};
|
|
2889
|
+
|
|
2890
|
+
// src/runtime/sync.ts
|
|
2891
|
+
var BASE_ATTRIBUTE_KEYS = /* @__PURE__ */ new Set([
|
|
2892
|
+
"id",
|
|
2893
|
+
"name",
|
|
2894
|
+
"label",
|
|
2895
|
+
"type",
|
|
2896
|
+
"required",
|
|
2897
|
+
"system",
|
|
2898
|
+
"unique",
|
|
2899
|
+
"order"
|
|
2900
|
+
]);
|
|
2901
|
+
function extractAttributeConfig(attr) {
|
|
2902
|
+
return Object.fromEntries(
|
|
2903
|
+
Object.entries(attr).filter(
|
|
2904
|
+
([key, value]) => !BASE_ATTRIBUTE_KEYS.has(key) && value !== void 0
|
|
2905
|
+
)
|
|
2906
|
+
);
|
|
2907
|
+
}
|
|
2908
|
+
async function syncNativeObjects(adapter, nativeRegistry, options = {}) {
|
|
2909
|
+
const result = {
|
|
2910
|
+
success: true,
|
|
2911
|
+
objectsSynced: 0,
|
|
2912
|
+
attributesSynced: 0,
|
|
2913
|
+
objectsCreated: 0,
|
|
2914
|
+
objectsUpdated: 0,
|
|
2915
|
+
attributesCreated: 0,
|
|
2916
|
+
attributesUpdated: 0,
|
|
2917
|
+
attributesDeleted: 0,
|
|
2918
|
+
errors: []
|
|
2919
|
+
};
|
|
2920
|
+
const nativeObjects = nativeRegistry.getAll();
|
|
2921
|
+
if (options.verbose) {
|
|
2922
|
+
console.info(`[SyncService] Starting sync for ${nativeObjects.length} native objects...`);
|
|
2923
|
+
}
|
|
2924
|
+
try {
|
|
2925
|
+
await adapter.transaction(async (tx) => {
|
|
2926
|
+
for (const nativeObject of nativeObjects) {
|
|
2927
|
+
try {
|
|
2928
|
+
await syncSingleObject(tx, nativeObject, result, options);
|
|
2929
|
+
} catch (error) {
|
|
2930
|
+
result.success = false;
|
|
2931
|
+
result.errors.push({
|
|
2932
|
+
objectName: nativeObject.name,
|
|
2933
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2934
|
+
});
|
|
2935
|
+
if (options.verbose) {
|
|
2936
|
+
console.error(`[SyncService] \u2717 Failed to sync ${nativeObject.name}:`, error);
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
}
|
|
2940
|
+
});
|
|
2941
|
+
} catch (error) {
|
|
2942
|
+
result.success = false;
|
|
2943
|
+
result.errors.push({
|
|
2944
|
+
objectName: "transaction",
|
|
2945
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2946
|
+
});
|
|
2947
|
+
}
|
|
2948
|
+
if (options.verbose) {
|
|
2949
|
+
console.info(
|
|
2950
|
+
`[SyncService] ${result.success ? "\u2713" : "\u2717"} Sync complete:
|
|
2951
|
+
Objects: ${result.objectsCreated} created, ${result.objectsUpdated} updated
|
|
2952
|
+
Attributes: ${result.attributesCreated} created, ${result.attributesUpdated} updated, ${result.attributesDeleted} deleted
|
|
2953
|
+
Errors: ${result.errors.length}`
|
|
2954
|
+
);
|
|
2955
|
+
}
|
|
2956
|
+
return result;
|
|
2957
|
+
}
|
|
2958
|
+
async function syncSingleObject(adapter, nativeObject, result, options) {
|
|
2959
|
+
if (!nativeObject.system) {
|
|
2960
|
+
throw new Error(`Object ${nativeObject.name} is not marked as system`);
|
|
2961
|
+
}
|
|
2962
|
+
const existingObject = await adapter.objects.findSystemByName(nativeObject.name);
|
|
2963
|
+
const isNew = !existingObject;
|
|
2964
|
+
updateObjectStats(result, isNew);
|
|
2965
|
+
if (options.dryRun) {
|
|
2966
|
+
await handleDryRun(adapter, nativeObject, existingObject, result, options, isNew);
|
|
2967
|
+
return;
|
|
2968
|
+
}
|
|
2969
|
+
const dbObject = await upsertObject(adapter, nativeObject, options);
|
|
2970
|
+
await syncAttributes(adapter, nativeObject, dbObject, existingObject, result);
|
|
2971
|
+
if (!isNew) {
|
|
2972
|
+
await cleanupAttributes(adapter, dbObject.id, nativeObject, result);
|
|
2973
|
+
}
|
|
2974
|
+
logSyncSuccess(nativeObject, options);
|
|
2975
|
+
}
|
|
2976
|
+
function updateObjectStats(result, isNew) {
|
|
2977
|
+
if (isNew) {
|
|
2978
|
+
result.objectsCreated++;
|
|
2979
|
+
} else {
|
|
2980
|
+
result.objectsUpdated++;
|
|
2981
|
+
}
|
|
2982
|
+
result.objectsSynced++;
|
|
2983
|
+
}
|
|
2984
|
+
async function handleDryRun(adapter, nativeObject, existingObject, result, options, isNew) {
|
|
2985
|
+
if (options.verbose) {
|
|
2986
|
+
console.info(`[SyncService] Would ${isNew ? "create" : "update"} object: ${nativeObject.name}`);
|
|
2987
|
+
}
|
|
2988
|
+
for (const attr of nativeObject.attributes) {
|
|
2989
|
+
const existingAttr = existingObject ? await adapter.attributes.findByObjectId(existingObject.id).then((attrs) => attrs.find((a) => a.name === attr.name)) : null;
|
|
2990
|
+
if (existingAttr) {
|
|
2991
|
+
result.attributesUpdated++;
|
|
2992
|
+
} else {
|
|
2993
|
+
result.attributesCreated++;
|
|
2994
|
+
}
|
|
2995
|
+
result.attributesSynced++;
|
|
2996
|
+
}
|
|
2997
|
+
}
|
|
2998
|
+
async function upsertObject(adapter, nativeObject, options) {
|
|
2999
|
+
return await adapter.objects.upsert({
|
|
3000
|
+
tenantId: options.tenantId || "default",
|
|
3001
|
+
system: true,
|
|
3002
|
+
name: nativeObject.name,
|
|
3003
|
+
label: nativeObject.label,
|
|
3004
|
+
description: nativeObject.description,
|
|
3005
|
+
metadata: nativeObject.metadata
|
|
3006
|
+
});
|
|
3007
|
+
}
|
|
3008
|
+
async function syncAttributes(adapter, nativeObject, dbObject, existingObject, result) {
|
|
3009
|
+
for (const [index, attr] of nativeObject.attributes.entries()) {
|
|
3010
|
+
const existingAttr = existingObject ? await adapter.attributes.findByObjectId(dbObject.id).then((attrs) => attrs.find((a) => a.name === attr.name)) : null;
|
|
3011
|
+
await adapter.attributes.upsert({
|
|
3012
|
+
objectId: dbObject.id,
|
|
3013
|
+
name: attr.name,
|
|
3014
|
+
label: attr.label,
|
|
3015
|
+
type: attr.type,
|
|
3016
|
+
description: attr.description,
|
|
3017
|
+
icon: attr.icon,
|
|
3018
|
+
system: attr.system ?? true,
|
|
3019
|
+
required: attr.required ?? false,
|
|
3020
|
+
order: attr.order ?? index,
|
|
3021
|
+
hidden: attr.hidden,
|
|
3022
|
+
archived: attr.archived,
|
|
3023
|
+
deprecated: attr.deprecated,
|
|
3024
|
+
defaultValue: attr.defaultValue,
|
|
3025
|
+
config: extractAttributeConfig(attr)
|
|
3026
|
+
});
|
|
3027
|
+
if (existingAttr) {
|
|
3028
|
+
result.attributesUpdated++;
|
|
3029
|
+
} else {
|
|
3030
|
+
result.attributesCreated++;
|
|
3031
|
+
}
|
|
3032
|
+
result.attributesSynced++;
|
|
3033
|
+
}
|
|
3034
|
+
}
|
|
3035
|
+
async function cleanupAttributes(adapter, objectId, nativeObject, result) {
|
|
3036
|
+
const attributeNames = nativeObject.attributes.map((attr) => attr.name);
|
|
3037
|
+
const deletedCount = await adapter.attributes.deleteByNames(objectId, attributeNames);
|
|
3038
|
+
result.attributesDeleted += deletedCount;
|
|
3039
|
+
}
|
|
3040
|
+
function logSyncSuccess(nativeObject, options) {
|
|
3041
|
+
if (options.verbose) {
|
|
3042
|
+
console.info(
|
|
3043
|
+
`[SyncService] \u2713 Synced ${nativeObject.name}: ${nativeObject.attributes.length} attributes`
|
|
3044
|
+
);
|
|
3045
|
+
}
|
|
3046
|
+
}
|
|
3047
|
+
async function verifyNativeObjectsSync(adapter, nativeRegistry) {
|
|
3048
|
+
const nativeObjects = nativeRegistry.getAll();
|
|
3049
|
+
for (const obj of nativeObjects) {
|
|
3050
|
+
if (!obj.system) continue;
|
|
3051
|
+
const existing = await adapter.objects.findSystemByName(obj.name);
|
|
3052
|
+
if (!existing) {
|
|
3053
|
+
return false;
|
|
3054
|
+
}
|
|
3055
|
+
}
|
|
3056
|
+
return true;
|
|
3057
|
+
}
|
|
3058
|
+
async function getSyncPreview(adapter, nativeRegistry) {
|
|
3059
|
+
return await syncNativeObjects(adapter, nativeRegistry, { dryRun: true });
|
|
3060
|
+
}
|
|
3061
|
+
|
|
3062
|
+
// src/types/geocoding.ts
|
|
3063
|
+
var NoopGeocodingAdapter = class {
|
|
3064
|
+
async autocomplete() {
|
|
3065
|
+
return await Promise.resolve([]);
|
|
3066
|
+
}
|
|
3067
|
+
async reverse() {
|
|
3068
|
+
return await Promise.resolve(null);
|
|
3069
|
+
}
|
|
3070
|
+
async geocode() {
|
|
3071
|
+
return await Promise.resolve(null);
|
|
3072
|
+
}
|
|
3073
|
+
};
|
|
3074
|
+
|
|
3075
|
+
export {
|
|
3076
|
+
NoopGeocodingAdapter,
|
|
3077
|
+
generateId,
|
|
3078
|
+
generatePrefixedId,
|
|
3079
|
+
text,
|
|
3080
|
+
textarea,
|
|
3081
|
+
number,
|
|
3082
|
+
checkbox,
|
|
3083
|
+
date,
|
|
3084
|
+
phone,
|
|
3085
|
+
currency,
|
|
3086
|
+
status,
|
|
3087
|
+
select,
|
|
3088
|
+
multiselect,
|
|
3089
|
+
location,
|
|
3090
|
+
timestamp,
|
|
3091
|
+
file,
|
|
3092
|
+
user,
|
|
3093
|
+
relation,
|
|
3094
|
+
rating,
|
|
3095
|
+
ObjectBuilder,
|
|
3096
|
+
object,
|
|
3097
|
+
registry,
|
|
3098
|
+
createTextValidator,
|
|
3099
|
+
createNumberValidator,
|
|
3100
|
+
createCheckboxValidator,
|
|
3101
|
+
createDateValidator,
|
|
3102
|
+
createPhoneValidator,
|
|
3103
|
+
createCurrencyValidator,
|
|
3104
|
+
createStatusValidator,
|
|
3105
|
+
createSelectValidator,
|
|
3106
|
+
createMultiselectValidator,
|
|
3107
|
+
createLocationValidator,
|
|
3108
|
+
createTimestampValidator,
|
|
3109
|
+
createFileValidator,
|
|
3110
|
+
createUserValidator,
|
|
3111
|
+
createSingleRelationValidator,
|
|
3112
|
+
createMultiRelationValidator,
|
|
3113
|
+
createRelationValidator,
|
|
3114
|
+
createRatingValidator,
|
|
3115
|
+
createAttributeValidator,
|
|
3116
|
+
createObjectValidator,
|
|
3117
|
+
validateAttribute,
|
|
3118
|
+
validateObject,
|
|
3119
|
+
validateObjectOrThrow,
|
|
3120
|
+
createDraftValidator,
|
|
3121
|
+
validateDraft,
|
|
3122
|
+
validateDraftOrThrow,
|
|
3123
|
+
getMissingRequiredAttributes,
|
|
3124
|
+
isRecordComplete,
|
|
3125
|
+
computeRecordStatus,
|
|
3126
|
+
createMockAdapter,
|
|
3127
|
+
FileService,
|
|
3128
|
+
GeocodingService,
|
|
3129
|
+
ObjectSchemaService,
|
|
3130
|
+
RelationService,
|
|
3131
|
+
RecordService,
|
|
3132
|
+
UserProfileService,
|
|
3133
|
+
syncNativeObjects,
|
|
3134
|
+
verifyNativeObjectsSync,
|
|
3135
|
+
getSyncPreview
|
|
3136
|
+
};
|