@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,2673 @@
|
|
|
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/builders/attribute-builders.ts
|
|
654
|
+
import { z } from "zod";
|
|
655
|
+
var BaseAttributeBuilder = class {
|
|
656
|
+
constructor(type, name, label) {
|
|
657
|
+
this.validateVariableName(name);
|
|
658
|
+
this.attr = {
|
|
659
|
+
name,
|
|
660
|
+
label,
|
|
661
|
+
type,
|
|
662
|
+
required: false
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Validate attribute name as valid variable identifier
|
|
667
|
+
* Accepts: camelCase, snake_case, PascalCase, UPPER_CASE
|
|
668
|
+
*/
|
|
669
|
+
validateVariableName(name) {
|
|
670
|
+
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_]*$/, {
|
|
671
|
+
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'"
|
|
672
|
+
});
|
|
673
|
+
try {
|
|
674
|
+
variableNameSchema.parse(name);
|
|
675
|
+
} catch (error) {
|
|
676
|
+
if (error instanceof z.ZodError) {
|
|
677
|
+
throw new Error(`[AttributeBuilder] ${error.errors[0].message}`);
|
|
678
|
+
}
|
|
679
|
+
throw error;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
setRequired(value) {
|
|
683
|
+
this.attr.required = value;
|
|
684
|
+
}
|
|
685
|
+
system() {
|
|
686
|
+
this.attr.system = true;
|
|
687
|
+
return this;
|
|
688
|
+
}
|
|
689
|
+
disabled() {
|
|
690
|
+
this.attr.disabled = true;
|
|
691
|
+
return this;
|
|
692
|
+
}
|
|
693
|
+
hidden() {
|
|
694
|
+
this.attr.hidden = true;
|
|
695
|
+
return this;
|
|
696
|
+
}
|
|
697
|
+
placeholder(value) {
|
|
698
|
+
this.attr.placeholder = value;
|
|
699
|
+
return this;
|
|
700
|
+
}
|
|
701
|
+
description(value) {
|
|
702
|
+
this.attr.description = value;
|
|
703
|
+
return this;
|
|
704
|
+
}
|
|
705
|
+
icon(value) {
|
|
706
|
+
this.attr.icon = value;
|
|
707
|
+
return this;
|
|
708
|
+
}
|
|
709
|
+
order(value) {
|
|
710
|
+
this.attr.order = value;
|
|
711
|
+
return this;
|
|
712
|
+
}
|
|
713
|
+
defaultValue(value) {
|
|
714
|
+
this.attr.defaultValue = value;
|
|
715
|
+
return this;
|
|
716
|
+
}
|
|
717
|
+
metadata(value) {
|
|
718
|
+
this.attr.metadata = value;
|
|
719
|
+
return this;
|
|
720
|
+
}
|
|
721
|
+
build() {
|
|
722
|
+
return this.attr;
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
var TextAttributeBuilder = class extends BaseAttributeBuilder {
|
|
726
|
+
constructor(name, label) {
|
|
727
|
+
super("text", name, label);
|
|
728
|
+
}
|
|
729
|
+
minLength(value) {
|
|
730
|
+
this.attr.minLength = value;
|
|
731
|
+
return this;
|
|
732
|
+
}
|
|
733
|
+
maxLength(value) {
|
|
734
|
+
this.attr.maxLength = value;
|
|
735
|
+
return this;
|
|
736
|
+
}
|
|
737
|
+
pattern(value) {
|
|
738
|
+
this.attr.pattern = value;
|
|
739
|
+
return this;
|
|
740
|
+
}
|
|
741
|
+
required() {
|
|
742
|
+
this.setRequired(true);
|
|
743
|
+
return this;
|
|
744
|
+
}
|
|
745
|
+
optional() {
|
|
746
|
+
this.setRequired(false);
|
|
747
|
+
return this;
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* Set email validation pattern
|
|
751
|
+
*/
|
|
752
|
+
email() {
|
|
753
|
+
this.attr.pattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
|
|
754
|
+
return this;
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Set URL validation pattern
|
|
758
|
+
*/
|
|
759
|
+
url() {
|
|
760
|
+
this.attr.pattern = "^https?:\\/\\/[\\w\\-]+(\\.[\\w\\-]+)+[/#?]?.*$";
|
|
761
|
+
return this;
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* Set slug validation pattern
|
|
765
|
+
*/
|
|
766
|
+
slug() {
|
|
767
|
+
this.attr.pattern = "^[a-z0-9]+(?:-[a-z0-9]+)*$";
|
|
768
|
+
return this;
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
function text(config) {
|
|
772
|
+
return new TextAttributeBuilder(config.name, config.label);
|
|
773
|
+
}
|
|
774
|
+
var TextAreaAttributeBuilder = class extends BaseAttributeBuilder {
|
|
775
|
+
constructor(name, label) {
|
|
776
|
+
super("textarea", name, label);
|
|
777
|
+
}
|
|
778
|
+
required() {
|
|
779
|
+
this.setRequired(true);
|
|
780
|
+
return this;
|
|
781
|
+
}
|
|
782
|
+
optional() {
|
|
783
|
+
this.setRequired(false);
|
|
784
|
+
return this;
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
function textarea(config) {
|
|
788
|
+
return new TextAreaAttributeBuilder(config.name, config.label);
|
|
789
|
+
}
|
|
790
|
+
var NumberAttributeBuilder = class extends BaseAttributeBuilder {
|
|
791
|
+
constructor(name, label) {
|
|
792
|
+
super("number", name, label);
|
|
793
|
+
}
|
|
794
|
+
min(value) {
|
|
795
|
+
this.attr.min = value;
|
|
796
|
+
return this;
|
|
797
|
+
}
|
|
798
|
+
max(value) {
|
|
799
|
+
this.attr.max = value;
|
|
800
|
+
return this;
|
|
801
|
+
}
|
|
802
|
+
integer() {
|
|
803
|
+
this.attr.unit = "integer";
|
|
804
|
+
this.attr.decimals = 0;
|
|
805
|
+
return this;
|
|
806
|
+
}
|
|
807
|
+
decimal(decimals = 2) {
|
|
808
|
+
this.attr.unit = "decimal";
|
|
809
|
+
this.attr.decimals = decimals;
|
|
810
|
+
return this;
|
|
811
|
+
}
|
|
812
|
+
percentage() {
|
|
813
|
+
this.attr.unit = "percentage";
|
|
814
|
+
return this;
|
|
815
|
+
}
|
|
816
|
+
required() {
|
|
817
|
+
this.setRequired(true);
|
|
818
|
+
return this;
|
|
819
|
+
}
|
|
820
|
+
optional() {
|
|
821
|
+
this.setRequired(false);
|
|
822
|
+
return this;
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
function number(config) {
|
|
826
|
+
return new NumberAttributeBuilder(config.name, config.label);
|
|
827
|
+
}
|
|
828
|
+
var CheckboxAttributeBuilder = class extends BaseAttributeBuilder {
|
|
829
|
+
constructor(name, label) {
|
|
830
|
+
super("checkbox", name, label);
|
|
831
|
+
}
|
|
832
|
+
required() {
|
|
833
|
+
this.setRequired(true);
|
|
834
|
+
return this;
|
|
835
|
+
}
|
|
836
|
+
optional() {
|
|
837
|
+
this.setRequired(false);
|
|
838
|
+
return this;
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
function checkbox(config) {
|
|
842
|
+
return new CheckboxAttributeBuilder(config.name, config.label);
|
|
843
|
+
}
|
|
844
|
+
var DateAttributeBuilder = class extends BaseAttributeBuilder {
|
|
845
|
+
constructor(name, label) {
|
|
846
|
+
super("date", name, label);
|
|
847
|
+
}
|
|
848
|
+
format(value) {
|
|
849
|
+
this.attr.dateFormat = value;
|
|
850
|
+
return this;
|
|
851
|
+
}
|
|
852
|
+
minDate(value) {
|
|
853
|
+
this.attr.minDate = value;
|
|
854
|
+
return this;
|
|
855
|
+
}
|
|
856
|
+
maxDate(value) {
|
|
857
|
+
this.attr.maxDate = value;
|
|
858
|
+
return this;
|
|
859
|
+
}
|
|
860
|
+
required() {
|
|
861
|
+
this.setRequired(true);
|
|
862
|
+
return this;
|
|
863
|
+
}
|
|
864
|
+
optional() {
|
|
865
|
+
this.setRequired(false);
|
|
866
|
+
return this;
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
function date(config) {
|
|
870
|
+
return new DateAttributeBuilder(config.name, config.label);
|
|
871
|
+
}
|
|
872
|
+
var PhoneAttributeBuilder = class extends BaseAttributeBuilder {
|
|
873
|
+
constructor(name, label) {
|
|
874
|
+
super("phone", name, label);
|
|
875
|
+
}
|
|
876
|
+
defaultCountry(value) {
|
|
877
|
+
this.attr.defaultCountryCode = value;
|
|
878
|
+
return this;
|
|
879
|
+
}
|
|
880
|
+
required() {
|
|
881
|
+
this.setRequired(true);
|
|
882
|
+
return this;
|
|
883
|
+
}
|
|
884
|
+
optional() {
|
|
885
|
+
this.setRequired(false);
|
|
886
|
+
return this;
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
function phone(config) {
|
|
890
|
+
return new PhoneAttributeBuilder(config.name, config.label);
|
|
891
|
+
}
|
|
892
|
+
var CurrencyAttributeBuilder = class extends BaseAttributeBuilder {
|
|
893
|
+
constructor(name, label) {
|
|
894
|
+
super("currency", name, label);
|
|
895
|
+
}
|
|
896
|
+
defaultCurrency(value) {
|
|
897
|
+
this.attr.defaultCurrency = value;
|
|
898
|
+
return this;
|
|
899
|
+
}
|
|
900
|
+
allowedCurrencies(value) {
|
|
901
|
+
this.attr.allowedCurrencies = value;
|
|
902
|
+
return this;
|
|
903
|
+
}
|
|
904
|
+
required() {
|
|
905
|
+
this.setRequired(true);
|
|
906
|
+
return this;
|
|
907
|
+
}
|
|
908
|
+
optional() {
|
|
909
|
+
this.setRequired(false);
|
|
910
|
+
return this;
|
|
911
|
+
}
|
|
912
|
+
};
|
|
913
|
+
function currency(config) {
|
|
914
|
+
return new CurrencyAttributeBuilder(config.name, config.label);
|
|
915
|
+
}
|
|
916
|
+
var StatusAttributeBuilder = class extends BaseAttributeBuilder {
|
|
917
|
+
constructor(name, label) {
|
|
918
|
+
super("status", name, label);
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* Set all options at once (preserves literal types for values)
|
|
922
|
+
*/
|
|
923
|
+
options(value) {
|
|
924
|
+
this.attr.options = value;
|
|
925
|
+
return this;
|
|
926
|
+
}
|
|
927
|
+
option(config) {
|
|
928
|
+
if (!this.attr.options) {
|
|
929
|
+
this.attr.options = [];
|
|
930
|
+
}
|
|
931
|
+
this.attr.options.push(config);
|
|
932
|
+
return this;
|
|
933
|
+
}
|
|
934
|
+
required() {
|
|
935
|
+
this.setRequired(true);
|
|
936
|
+
return this;
|
|
937
|
+
}
|
|
938
|
+
optional() {
|
|
939
|
+
this.setRequired(false);
|
|
940
|
+
return this;
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
function status(config) {
|
|
944
|
+
return new StatusAttributeBuilder(config.name, config.label);
|
|
945
|
+
}
|
|
946
|
+
var SelectAttributeBuilder = class extends BaseAttributeBuilder {
|
|
947
|
+
constructor(name, label) {
|
|
948
|
+
super("select", name, label);
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* Set all options at once (preserves literal types for values)
|
|
952
|
+
*/
|
|
953
|
+
options(value) {
|
|
954
|
+
this.attr.options = value;
|
|
955
|
+
return this;
|
|
956
|
+
}
|
|
957
|
+
option(config) {
|
|
958
|
+
if (!this.attr.options) {
|
|
959
|
+
this.attr.options = [];
|
|
960
|
+
}
|
|
961
|
+
this.attr.options.push(config);
|
|
962
|
+
return this;
|
|
963
|
+
}
|
|
964
|
+
required() {
|
|
965
|
+
this.setRequired(true);
|
|
966
|
+
return this;
|
|
967
|
+
}
|
|
968
|
+
optional() {
|
|
969
|
+
this.setRequired(false);
|
|
970
|
+
return this;
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
function select(config) {
|
|
974
|
+
return new SelectAttributeBuilder(config.name, config.label);
|
|
975
|
+
}
|
|
976
|
+
var MultiselectAttributeBuilder = class extends BaseAttributeBuilder {
|
|
977
|
+
constructor(name, label) {
|
|
978
|
+
super("multiselect", name, label);
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
* Set all options at once (preserves literal types for values)
|
|
982
|
+
*/
|
|
983
|
+
options(value) {
|
|
984
|
+
this.attr.options = value;
|
|
985
|
+
return this;
|
|
986
|
+
}
|
|
987
|
+
option(config) {
|
|
988
|
+
if (!this.attr.options) {
|
|
989
|
+
this.attr.options = [];
|
|
990
|
+
}
|
|
991
|
+
this.attr.options.push(config);
|
|
992
|
+
return this;
|
|
993
|
+
}
|
|
994
|
+
required() {
|
|
995
|
+
this.setRequired(true);
|
|
996
|
+
return this;
|
|
997
|
+
}
|
|
998
|
+
optional() {
|
|
999
|
+
this.setRequired(false);
|
|
1000
|
+
return this;
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
function multiselect(config) {
|
|
1004
|
+
return new MultiselectAttributeBuilder(config.name, config.label);
|
|
1005
|
+
}
|
|
1006
|
+
var LocationAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1007
|
+
constructor(name, label) {
|
|
1008
|
+
super("location", name, label);
|
|
1009
|
+
}
|
|
1010
|
+
granularity(value) {
|
|
1011
|
+
this.attr.granularity = value;
|
|
1012
|
+
return this;
|
|
1013
|
+
}
|
|
1014
|
+
enableAutocomplete() {
|
|
1015
|
+
this.attr.enableAutocomplete = true;
|
|
1016
|
+
return this;
|
|
1017
|
+
}
|
|
1018
|
+
enableMap() {
|
|
1019
|
+
this.attr.enableMap = true;
|
|
1020
|
+
return this;
|
|
1021
|
+
}
|
|
1022
|
+
defaultCountry(value) {
|
|
1023
|
+
this.attr.defaultCountry = value;
|
|
1024
|
+
return this;
|
|
1025
|
+
}
|
|
1026
|
+
allowedCountries(value) {
|
|
1027
|
+
this.attr.allowedCountries = value;
|
|
1028
|
+
return this;
|
|
1029
|
+
}
|
|
1030
|
+
displayFormat(value) {
|
|
1031
|
+
this.attr.displayFormat = value;
|
|
1032
|
+
return this;
|
|
1033
|
+
}
|
|
1034
|
+
required() {
|
|
1035
|
+
this.setRequired(true);
|
|
1036
|
+
return this;
|
|
1037
|
+
}
|
|
1038
|
+
optional() {
|
|
1039
|
+
this.setRequired(false);
|
|
1040
|
+
return this;
|
|
1041
|
+
}
|
|
1042
|
+
};
|
|
1043
|
+
function location(config) {
|
|
1044
|
+
return new LocationAttributeBuilder(config.name, config.label);
|
|
1045
|
+
}
|
|
1046
|
+
var TimestampAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1047
|
+
constructor(name, label) {
|
|
1048
|
+
super("timestamp", name, label);
|
|
1049
|
+
}
|
|
1050
|
+
autoUpdate() {
|
|
1051
|
+
this.attr.autoUpdate = true;
|
|
1052
|
+
return this;
|
|
1053
|
+
}
|
|
1054
|
+
required() {
|
|
1055
|
+
this.setRequired(true);
|
|
1056
|
+
return this;
|
|
1057
|
+
}
|
|
1058
|
+
optional() {
|
|
1059
|
+
this.setRequired(false);
|
|
1060
|
+
return this;
|
|
1061
|
+
}
|
|
1062
|
+
};
|
|
1063
|
+
function timestamp(config) {
|
|
1064
|
+
return new TimestampAttributeBuilder(config.name, config.label);
|
|
1065
|
+
}
|
|
1066
|
+
var FileAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1067
|
+
constructor(name, label) {
|
|
1068
|
+
super("file", name, label);
|
|
1069
|
+
}
|
|
1070
|
+
maxFiles(value) {
|
|
1071
|
+
this.attr.maxFiles = value;
|
|
1072
|
+
return this;
|
|
1073
|
+
}
|
|
1074
|
+
maxSize(bytes) {
|
|
1075
|
+
this.attr.maxSize = bytes;
|
|
1076
|
+
return this;
|
|
1077
|
+
}
|
|
1078
|
+
allowedTypes(types) {
|
|
1079
|
+
this.attr.allowedTypes = types;
|
|
1080
|
+
return this;
|
|
1081
|
+
}
|
|
1082
|
+
verification(config) {
|
|
1083
|
+
this.attr.verification = config;
|
|
1084
|
+
return this;
|
|
1085
|
+
}
|
|
1086
|
+
required() {
|
|
1087
|
+
this.setRequired(true);
|
|
1088
|
+
return this;
|
|
1089
|
+
}
|
|
1090
|
+
optional() {
|
|
1091
|
+
this.setRequired(false);
|
|
1092
|
+
return this;
|
|
1093
|
+
}
|
|
1094
|
+
};
|
|
1095
|
+
function file(config) {
|
|
1096
|
+
return new FileAttributeBuilder(config.name, config.label);
|
|
1097
|
+
}
|
|
1098
|
+
var UserAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1099
|
+
constructor(name, label) {
|
|
1100
|
+
super("user", name, label);
|
|
1101
|
+
}
|
|
1102
|
+
allowedRoles(roles) {
|
|
1103
|
+
this.attr.allowedRoles = roles;
|
|
1104
|
+
return this;
|
|
1105
|
+
}
|
|
1106
|
+
required() {
|
|
1107
|
+
this.setRequired(true);
|
|
1108
|
+
return this;
|
|
1109
|
+
}
|
|
1110
|
+
optional() {
|
|
1111
|
+
this.setRequired(false);
|
|
1112
|
+
return this;
|
|
1113
|
+
}
|
|
1114
|
+
};
|
|
1115
|
+
function user(config) {
|
|
1116
|
+
return new UserAttributeBuilder(config.name, config.label);
|
|
1117
|
+
}
|
|
1118
|
+
var RelationAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1119
|
+
constructor(name, label) {
|
|
1120
|
+
super("relation", name, label);
|
|
1121
|
+
}
|
|
1122
|
+
relation(objectIds) {
|
|
1123
|
+
this.attr.relation = objectIds;
|
|
1124
|
+
return this;
|
|
1125
|
+
}
|
|
1126
|
+
limit(value) {
|
|
1127
|
+
this.attr.limit = value;
|
|
1128
|
+
return this;
|
|
1129
|
+
}
|
|
1130
|
+
cascadeDelete() {
|
|
1131
|
+
this.attr.cascadeDelete = true;
|
|
1132
|
+
return this;
|
|
1133
|
+
}
|
|
1134
|
+
required() {
|
|
1135
|
+
this.setRequired(true);
|
|
1136
|
+
return this;
|
|
1137
|
+
}
|
|
1138
|
+
optional() {
|
|
1139
|
+
this.setRequired(false);
|
|
1140
|
+
return this;
|
|
1141
|
+
}
|
|
1142
|
+
};
|
|
1143
|
+
function relation(config) {
|
|
1144
|
+
return new RelationAttributeBuilder(config.name, config.label);
|
|
1145
|
+
}
|
|
1146
|
+
var RatingAttributeBuilder = class extends BaseAttributeBuilder {
|
|
1147
|
+
constructor(name, label) {
|
|
1148
|
+
super("rating", name, label);
|
|
1149
|
+
}
|
|
1150
|
+
max(value) {
|
|
1151
|
+
this.attr.max = value;
|
|
1152
|
+
return this;
|
|
1153
|
+
}
|
|
1154
|
+
iconType(value) {
|
|
1155
|
+
this.attr.iconType = value;
|
|
1156
|
+
return this;
|
|
1157
|
+
}
|
|
1158
|
+
required() {
|
|
1159
|
+
this.setRequired(true);
|
|
1160
|
+
return this;
|
|
1161
|
+
}
|
|
1162
|
+
optional() {
|
|
1163
|
+
this.setRequired(false);
|
|
1164
|
+
return this;
|
|
1165
|
+
}
|
|
1166
|
+
};
|
|
1167
|
+
function rating(config) {
|
|
1168
|
+
return new RatingAttributeBuilder(config.name, config.label);
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
// src/builders/object-builder.ts
|
|
1172
|
+
import z2 from "zod";
|
|
1173
|
+
var ObjectBuilder = class {
|
|
1174
|
+
constructor(config) {
|
|
1175
|
+
this.validateName(config.name);
|
|
1176
|
+
this.obj = {
|
|
1177
|
+
name: config.name,
|
|
1178
|
+
label: config.label,
|
|
1179
|
+
attributes: [],
|
|
1180
|
+
system: false
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
/**
|
|
1184
|
+
* Set object description
|
|
1185
|
+
*/
|
|
1186
|
+
description(value) {
|
|
1187
|
+
this.obj.description = value;
|
|
1188
|
+
return this;
|
|
1189
|
+
}
|
|
1190
|
+
/**
|
|
1191
|
+
* Set object icon
|
|
1192
|
+
*/
|
|
1193
|
+
icon(value) {
|
|
1194
|
+
this.obj.icon = value;
|
|
1195
|
+
return this;
|
|
1196
|
+
}
|
|
1197
|
+
/**
|
|
1198
|
+
* Mark as system/native object (defined in code, protected)
|
|
1199
|
+
* System objects cannot be deleted/modified by clients
|
|
1200
|
+
*/
|
|
1201
|
+
system() {
|
|
1202
|
+
this.obj.system = true;
|
|
1203
|
+
return this;
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* Add an attribute to the object with type accumulation
|
|
1207
|
+
*
|
|
1208
|
+
* The return type accumulates the attribute's name and value type
|
|
1209
|
+
* into the TRecord generic, enabling full inference.
|
|
1210
|
+
*
|
|
1211
|
+
* @param attr - Attribute definition or builder (auto-builds if builder)
|
|
1212
|
+
*/
|
|
1213
|
+
attribute(attr) {
|
|
1214
|
+
if (!this.obj.attributes) {
|
|
1215
|
+
this.obj.attributes = [];
|
|
1216
|
+
}
|
|
1217
|
+
const finalAttr = "build" in attr && typeof attr.build === "function" ? attr.build() : attr;
|
|
1218
|
+
this.obj.attributes.push(finalAttr);
|
|
1219
|
+
return this;
|
|
1220
|
+
}
|
|
1221
|
+
/**
|
|
1222
|
+
* Add multiple attributes at once
|
|
1223
|
+
* Note: For full type inference, prefer chaining .attribute() calls
|
|
1224
|
+
*/
|
|
1225
|
+
attributes(attrs) {
|
|
1226
|
+
if (!this.obj.attributes) {
|
|
1227
|
+
this.obj.attributes = [];
|
|
1228
|
+
}
|
|
1229
|
+
const finalAttrs = attrs.map((attr) => "build" in attr ? attr.build() : attr);
|
|
1230
|
+
this.obj.attributes.push(...finalAttrs);
|
|
1231
|
+
return this;
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Set metadata
|
|
1235
|
+
*/
|
|
1236
|
+
metadata(value) {
|
|
1237
|
+
this.obj.metadata = value;
|
|
1238
|
+
return this;
|
|
1239
|
+
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Set the title attribute (the text attribute used to display the object's name)
|
|
1242
|
+
* Must reference a text attribute that has been added to the object.
|
|
1243
|
+
* If not set, the first text attribute becomes the title attribute.
|
|
1244
|
+
*
|
|
1245
|
+
* @param attributeName - The name of a text attribute
|
|
1246
|
+
*/
|
|
1247
|
+
titleAttribute(attributeName) {
|
|
1248
|
+
this._titleAttribute = attributeName;
|
|
1249
|
+
return this;
|
|
1250
|
+
}
|
|
1251
|
+
/**
|
|
1252
|
+
* Build the final ObjectDefinition
|
|
1253
|
+
* @throws Error if required fields are missing or invalid
|
|
1254
|
+
*/
|
|
1255
|
+
build() {
|
|
1256
|
+
if (!this.obj.name) {
|
|
1257
|
+
throw new Error("[ObjectBuilder] Missing required field: name");
|
|
1258
|
+
}
|
|
1259
|
+
if (!this.obj.label) {
|
|
1260
|
+
throw new Error("[ObjectBuilder] Missing required field: label");
|
|
1261
|
+
}
|
|
1262
|
+
const textAttributes = (this.obj.attributes ?? []).filter((attr) => attr.type === "text");
|
|
1263
|
+
if (textAttributes.length === 0) {
|
|
1264
|
+
throw new Error(
|
|
1265
|
+
`[ObjectBuilder] Object "${this.obj.name}" must have at least one text attribute.
|
|
1266
|
+
A text attribute is required to serve as the object's title/display name.
|
|
1267
|
+
\u2705 Example: .attribute(text({ name: 'name', label: 'Name' }).required())`
|
|
1268
|
+
);
|
|
1269
|
+
}
|
|
1270
|
+
const titleAttrName = this._titleAttribute ?? textAttributes[0].name;
|
|
1271
|
+
const titleAttr = textAttributes.find((attr) => attr.name === titleAttrName);
|
|
1272
|
+
if (!titleAttr) {
|
|
1273
|
+
const availableTextAttrs = textAttributes.map((a) => `'${a.name}'`).join(", ");
|
|
1274
|
+
throw new Error(
|
|
1275
|
+
`[ObjectBuilder] titleAttribute "${titleAttrName}" must be a text attribute.
|
|
1276
|
+
Available text attributes: ${availableTextAttrs}`
|
|
1277
|
+
);
|
|
1278
|
+
}
|
|
1279
|
+
return {
|
|
1280
|
+
...this.obj,
|
|
1281
|
+
titleAttribute: titleAttrName
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
/**
|
|
1285
|
+
* Validate object name format
|
|
1286
|
+
* Must be kebab-case (slug format), max 63 characters
|
|
1287
|
+
* @internal
|
|
1288
|
+
*/
|
|
1289
|
+
validateName(name) {
|
|
1290
|
+
if (name === void 0) {
|
|
1291
|
+
return;
|
|
1292
|
+
}
|
|
1293
|
+
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-]*$/, {
|
|
1294
|
+
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'"
|
|
1295
|
+
});
|
|
1296
|
+
try {
|
|
1297
|
+
objectNameSchema.parse(name);
|
|
1298
|
+
} catch (error) {
|
|
1299
|
+
if (error instanceof z2.ZodError) {
|
|
1300
|
+
throw new Error(`[ObjectBuilder] ${error.errors[0].message}`);
|
|
1301
|
+
}
|
|
1302
|
+
throw error;
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
};
|
|
1306
|
+
function object(config) {
|
|
1307
|
+
return new ObjectBuilder(config);
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
// src/runtime/services/object-schema.service.ts
|
|
1311
|
+
var ObjectSchemaService = class {
|
|
1312
|
+
constructor(adapter, nativeRegistry) {
|
|
1313
|
+
this.adapter = adapter;
|
|
1314
|
+
this.nativeRegistry = nativeRegistry;
|
|
1315
|
+
}
|
|
1316
|
+
/**
|
|
1317
|
+
* Create a new custom object
|
|
1318
|
+
* Validates name format and ensures system=false
|
|
1319
|
+
*
|
|
1320
|
+
* @param definition - Object definition (attributes can be raw definitions or builders)
|
|
1321
|
+
* @param tenantId - Tenant ID owning the object
|
|
1322
|
+
* @returns Created ObjectDefinition
|
|
1323
|
+
*/
|
|
1324
|
+
async createCustomObject(definition, tenantId) {
|
|
1325
|
+
const builder = object({
|
|
1326
|
+
name: definition.name,
|
|
1327
|
+
label: definition.label
|
|
1328
|
+
});
|
|
1329
|
+
if (definition.description) builder.description(definition.description);
|
|
1330
|
+
if (definition.icon) builder.icon(definition.icon);
|
|
1331
|
+
if (definition.metadata) builder.metadata(definition.metadata);
|
|
1332
|
+
if (definition.attributes && definition.attributes.length > 0) {
|
|
1333
|
+
builder.attributes(definition.attributes);
|
|
1334
|
+
}
|
|
1335
|
+
const objectDef = builder.build();
|
|
1336
|
+
const existing = await this.adapter.objects.findByName(tenantId, objectDef.name);
|
|
1337
|
+
if (existing) {
|
|
1338
|
+
throw new Error(`Object with name "${objectDef.name}" already exists`);
|
|
1339
|
+
}
|
|
1340
|
+
const metadata = {
|
|
1341
|
+
...objectDef.metadata,
|
|
1342
|
+
icon: objectDef.icon
|
|
1343
|
+
};
|
|
1344
|
+
const dbObject = await this.adapter.objects.create({
|
|
1345
|
+
tenantId,
|
|
1346
|
+
name: objectDef.name,
|
|
1347
|
+
label: objectDef.label,
|
|
1348
|
+
description: objectDef.description,
|
|
1349
|
+
system: false,
|
|
1350
|
+
// Always false for custom objects
|
|
1351
|
+
metadata
|
|
1352
|
+
});
|
|
1353
|
+
const createdAttributes = [];
|
|
1354
|
+
if (objectDef.attributes.length > 0) {
|
|
1355
|
+
for (const attr of objectDef.attributes) {
|
|
1356
|
+
const dbAttr = await this.adapter.attributes.create({
|
|
1357
|
+
objectId: dbObject.id,
|
|
1358
|
+
name: attr.name,
|
|
1359
|
+
label: attr.label,
|
|
1360
|
+
type: attr.type,
|
|
1361
|
+
required: attr.required ?? false,
|
|
1362
|
+
system: false,
|
|
1363
|
+
// Always false for custom attributes
|
|
1364
|
+
config: {
|
|
1365
|
+
...attr,
|
|
1366
|
+
// Remove common fields stored in columns
|
|
1367
|
+
id: void 0,
|
|
1368
|
+
name: void 0,
|
|
1369
|
+
label: void 0,
|
|
1370
|
+
type: void 0,
|
|
1371
|
+
required: void 0,
|
|
1372
|
+
system: void 0
|
|
1373
|
+
}
|
|
1374
|
+
});
|
|
1375
|
+
createdAttributes.push(this.convertDBAttributeToAttribute(dbAttr));
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
return {
|
|
1379
|
+
...objectDef,
|
|
1380
|
+
id: dbObject.id,
|
|
1381
|
+
attributes: createdAttributes,
|
|
1382
|
+
system: false
|
|
1383
|
+
};
|
|
1384
|
+
}
|
|
1385
|
+
/**
|
|
1386
|
+
* Add a custom attribute to an existing object
|
|
1387
|
+
* Can be used on both custom and native objects
|
|
1388
|
+
* Attributes added via API are always custom (system=false)
|
|
1389
|
+
*
|
|
1390
|
+
* @param objectId - Object UUID from database
|
|
1391
|
+
* @param attribute - Attribute definition
|
|
1392
|
+
* @returns Created Attribute
|
|
1393
|
+
*/
|
|
1394
|
+
async addAttributeToObject(objectId, attribute) {
|
|
1395
|
+
this.validateAttributeName(attribute.name);
|
|
1396
|
+
const dbObject = await this.adapter.objects.findById(objectId);
|
|
1397
|
+
if (!dbObject) {
|
|
1398
|
+
throw new Error(`Object with id "${objectId}" not found`);
|
|
1399
|
+
}
|
|
1400
|
+
const existingAttributes = await this.adapter.attributes.findByObjectId(objectId);
|
|
1401
|
+
const duplicate = existingAttributes.find((attr) => attr.name === attribute.name);
|
|
1402
|
+
if (duplicate) {
|
|
1403
|
+
throw new Error(
|
|
1404
|
+
`Attribute with name "${attribute.name}" already exists on object "${dbObject.name}"`
|
|
1405
|
+
);
|
|
1406
|
+
}
|
|
1407
|
+
const dbAttr = await this.adapter.attributes.create({
|
|
1408
|
+
objectId,
|
|
1409
|
+
name: attribute.name,
|
|
1410
|
+
label: attribute.label,
|
|
1411
|
+
type: attribute.type,
|
|
1412
|
+
required: attribute.required ?? false,
|
|
1413
|
+
unique: attribute.unique ?? false,
|
|
1414
|
+
system: false,
|
|
1415
|
+
// Always false for attributes added via API
|
|
1416
|
+
config: {
|
|
1417
|
+
// Store all extra properties in config
|
|
1418
|
+
...attribute,
|
|
1419
|
+
// Remove common fields stored in columns
|
|
1420
|
+
name: void 0,
|
|
1421
|
+
label: void 0,
|
|
1422
|
+
type: void 0,
|
|
1423
|
+
required: void 0,
|
|
1424
|
+
unique: void 0,
|
|
1425
|
+
system: void 0
|
|
1426
|
+
}
|
|
1427
|
+
});
|
|
1428
|
+
return this.convertDBAttributeToAttribute(dbAttr);
|
|
1429
|
+
}
|
|
1430
|
+
/**
|
|
1431
|
+
* Update an attribute
|
|
1432
|
+
* Can only update custom attributes (system=false)
|
|
1433
|
+
*
|
|
1434
|
+
* @param attributeId - Attribute UUID
|
|
1435
|
+
* @param updates - Partial attribute updates
|
|
1436
|
+
* @returns Updated Attribute
|
|
1437
|
+
*/
|
|
1438
|
+
async updateAttribute(attributeId, updates) {
|
|
1439
|
+
const dbAttr = await this.adapter.attributes.findById(attributeId);
|
|
1440
|
+
if (!dbAttr) {
|
|
1441
|
+
throw new Error(`Attribute with id "${attributeId}" not found`);
|
|
1442
|
+
}
|
|
1443
|
+
if (dbAttr.system) {
|
|
1444
|
+
throw new Error(
|
|
1445
|
+
`Cannot modify system attribute "${dbAttr.name}". System attributes are protected.`
|
|
1446
|
+
);
|
|
1447
|
+
}
|
|
1448
|
+
if (updates.name && updates.name !== dbAttr.name) {
|
|
1449
|
+
throw new Error(
|
|
1450
|
+
"Attribute name cannot be changed after creation. Create a new attribute instead."
|
|
1451
|
+
);
|
|
1452
|
+
}
|
|
1453
|
+
if (updates.type && updates.type !== dbAttr.type) {
|
|
1454
|
+
throw new Error(
|
|
1455
|
+
"Attribute type cannot be changed after creation. Create a new attribute instead."
|
|
1456
|
+
);
|
|
1457
|
+
}
|
|
1458
|
+
const updatedDbAttr = await this.adapter.attributes.update(attributeId, {
|
|
1459
|
+
label: updates.label,
|
|
1460
|
+
required: updates.required,
|
|
1461
|
+
config: updates ? {
|
|
1462
|
+
...dbAttr.config,
|
|
1463
|
+
...updates,
|
|
1464
|
+
// Remove common fields
|
|
1465
|
+
name: void 0,
|
|
1466
|
+
label: void 0,
|
|
1467
|
+
type: void 0,
|
|
1468
|
+
required: void 0,
|
|
1469
|
+
unique: void 0,
|
|
1470
|
+
system: void 0
|
|
1471
|
+
} : dbAttr.config
|
|
1472
|
+
});
|
|
1473
|
+
return this.convertDBAttributeToAttribute(updatedDbAttr);
|
|
1474
|
+
}
|
|
1475
|
+
/**
|
|
1476
|
+
* Delete an attribute
|
|
1477
|
+
* Can only delete custom attributes (system=false)
|
|
1478
|
+
*
|
|
1479
|
+
* @param attributeId - Attribute UUID
|
|
1480
|
+
*/
|
|
1481
|
+
async deleteAttribute(attributeId) {
|
|
1482
|
+
const dbAttr = await this.adapter.attributes.findById(attributeId);
|
|
1483
|
+
if (!dbAttr) {
|
|
1484
|
+
throw new Error(`Attribute with id "${attributeId}" not found`);
|
|
1485
|
+
}
|
|
1486
|
+
if (dbAttr.system) {
|
|
1487
|
+
throw new Error(
|
|
1488
|
+
`Cannot delete system attribute "${dbAttr.name}". System attributes are protected.`
|
|
1489
|
+
);
|
|
1490
|
+
}
|
|
1491
|
+
await this.adapter.attributes.delete(attributeId);
|
|
1492
|
+
}
|
|
1493
|
+
/**
|
|
1494
|
+
* List attributes for an object
|
|
1495
|
+
*
|
|
1496
|
+
* @param objectId - Object UUID
|
|
1497
|
+
* @param options - Filter options
|
|
1498
|
+
* @returns List of attributes
|
|
1499
|
+
*/
|
|
1500
|
+
async listAttributes(objectId, options) {
|
|
1501
|
+
const dbAttributes = await this.adapter.attributes.findByObjectId(objectId);
|
|
1502
|
+
let filtered = dbAttributes;
|
|
1503
|
+
if (options?.systemOnly) {
|
|
1504
|
+
filtered = dbAttributes.filter((attr) => attr.system);
|
|
1505
|
+
} else if (options?.customOnly) {
|
|
1506
|
+
filtered = dbAttributes.filter((attr) => !attr.system);
|
|
1507
|
+
}
|
|
1508
|
+
return filtered.map((attr) => this.convertDBAttributeToAttribute(attr));
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* Validate attribute name format (variable identifier)
|
|
1512
|
+
* @internal
|
|
1513
|
+
*/
|
|
1514
|
+
validateAttributeName(name) {
|
|
1515
|
+
const variableNameRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
1516
|
+
if (!name || name.length === 0) {
|
|
1517
|
+
throw new Error("Attribute name cannot be empty");
|
|
1518
|
+
}
|
|
1519
|
+
if (name.length > 63) {
|
|
1520
|
+
throw new Error("Attribute name is too long (max 63 characters)");
|
|
1521
|
+
}
|
|
1522
|
+
if (!variableNameRegex.test(name)) {
|
|
1523
|
+
throw new Error(
|
|
1524
|
+
"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'"
|
|
1525
|
+
);
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
/**
|
|
1529
|
+
* Get complete object schema (system + custom attributes)
|
|
1530
|
+
* If object is native, merges registry definition with DB custom attributes
|
|
1531
|
+
* If object is custom, returns DB definition only
|
|
1532
|
+
*
|
|
1533
|
+
* @param objectId - Object UUID from database
|
|
1534
|
+
* @returns Complete ObjectDefinition with all attributes
|
|
1535
|
+
*
|
|
1536
|
+
* @example
|
|
1537
|
+
* ```typescript
|
|
1538
|
+
* const service = new ObjectSchemaService(adapter, registry);
|
|
1539
|
+
* const productSchema = await service.getObjectSchema("obj-123");
|
|
1540
|
+
*
|
|
1541
|
+
* console.log(productSchema.attributes); // System + custom attributes
|
|
1542
|
+
* ```
|
|
1543
|
+
*/
|
|
1544
|
+
async getObjectSchema(objectId) {
|
|
1545
|
+
const dbObject = await this.adapter.objects.findById(objectId);
|
|
1546
|
+
if (!dbObject) {
|
|
1547
|
+
throw new Error(`Object with id "${objectId}" not found`);
|
|
1548
|
+
}
|
|
1549
|
+
const dbAttributes = await this.adapter.attributes.findByObjectId(objectId);
|
|
1550
|
+
if (dbObject.system) {
|
|
1551
|
+
return this.mergeNativeObject(dbObject, dbAttributes);
|
|
1552
|
+
}
|
|
1553
|
+
return this.convertDBObjectToDefinition(dbObject, dbAttributes);
|
|
1554
|
+
}
|
|
1555
|
+
/**
|
|
1556
|
+
* Get object schema by name (for system/native objects)
|
|
1557
|
+
*/
|
|
1558
|
+
async getObjectSchemaByName(name) {
|
|
1559
|
+
const dbObject = await this.adapter.objects.findSystemByName(name);
|
|
1560
|
+
if (!dbObject) {
|
|
1561
|
+
throw new Error(`Native object "${name}" not found in database`);
|
|
1562
|
+
}
|
|
1563
|
+
const dbAttributes = await this.adapter.attributes.findByObjectId(dbObject.id);
|
|
1564
|
+
return this.mergeNativeObject(dbObject, dbAttributes);
|
|
1565
|
+
}
|
|
1566
|
+
/**
|
|
1567
|
+
* List all object schemas for a tenant
|
|
1568
|
+
*/
|
|
1569
|
+
async listObjectSchemas(tenantId) {
|
|
1570
|
+
const dbObjects = await this.adapter.objects.list(tenantId);
|
|
1571
|
+
const schemas = await Promise.all(
|
|
1572
|
+
dbObjects.map(async (dbObject) => {
|
|
1573
|
+
const dbAttributes = await this.adapter.attributes.findByObjectId(dbObject.id);
|
|
1574
|
+
if (dbObject.system) {
|
|
1575
|
+
return this.mergeNativeObject(dbObject, dbAttributes);
|
|
1576
|
+
}
|
|
1577
|
+
return this.convertDBObjectToDefinition(dbObject, dbAttributes);
|
|
1578
|
+
})
|
|
1579
|
+
);
|
|
1580
|
+
return schemas;
|
|
1581
|
+
}
|
|
1582
|
+
/**
|
|
1583
|
+
* Merge native object from registry with custom attributes from DB
|
|
1584
|
+
* @internal
|
|
1585
|
+
*/
|
|
1586
|
+
mergeNativeObject(dbObject, dbAttributes) {
|
|
1587
|
+
const nativeObject = this.nativeRegistry.getByName(dbObject.name);
|
|
1588
|
+
if (!nativeObject) {
|
|
1589
|
+
throw new Error(
|
|
1590
|
+
`Native object "${dbObject.name}" not found in registry. Make sure to register it before querying.`
|
|
1591
|
+
);
|
|
1592
|
+
}
|
|
1593
|
+
const customAttributes = dbAttributes.filter((attr) => !attr.system);
|
|
1594
|
+
const customAttrs = customAttributes.map(
|
|
1595
|
+
(dbAttr) => this.convertDBAttributeToAttribute(dbAttr)
|
|
1596
|
+
);
|
|
1597
|
+
return {
|
|
1598
|
+
...nativeObject,
|
|
1599
|
+
id: dbObject.id,
|
|
1600
|
+
attributes: [
|
|
1601
|
+
...nativeObject.attributes,
|
|
1602
|
+
// System attributes from code
|
|
1603
|
+
...customAttrs
|
|
1604
|
+
// Custom attributes from DB
|
|
1605
|
+
]
|
|
1606
|
+
};
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* Convert DB object + attributes to ObjectDefinition
|
|
1610
|
+
* @internal
|
|
1611
|
+
*/
|
|
1612
|
+
convertDBObjectToDefinition(dbObject, dbAttributes) {
|
|
1613
|
+
const attributes = dbAttributes.map(
|
|
1614
|
+
(dbAttr) => this.convertDBAttributeToAttribute(dbAttr)
|
|
1615
|
+
);
|
|
1616
|
+
const titleAttribute = dbObject.metadata?.titleAttribute ?? attributes.find((attr) => attr.type === "text")?.name ?? attributes[0]?.name ?? "name";
|
|
1617
|
+
return {
|
|
1618
|
+
id: dbObject.id,
|
|
1619
|
+
name: dbObject.name,
|
|
1620
|
+
label: dbObject.label,
|
|
1621
|
+
description: dbObject.description,
|
|
1622
|
+
icon: dbObject.metadata?.icon,
|
|
1623
|
+
titleAttribute,
|
|
1624
|
+
attributes,
|
|
1625
|
+
system: dbObject.system,
|
|
1626
|
+
metadata: dbObject.metadata
|
|
1627
|
+
};
|
|
1628
|
+
}
|
|
1629
|
+
/**
|
|
1630
|
+
* Convert DB attribute to Attribute type
|
|
1631
|
+
* @internal
|
|
1632
|
+
*/
|
|
1633
|
+
convertDBAttributeToAttribute(dbAttr) {
|
|
1634
|
+
const baseAttr = {
|
|
1635
|
+
...dbAttr.config,
|
|
1636
|
+
id: dbAttr.id,
|
|
1637
|
+
name: dbAttr.name,
|
|
1638
|
+
label: dbAttr.label,
|
|
1639
|
+
type: dbAttr.type,
|
|
1640
|
+
required: dbAttr.required,
|
|
1641
|
+
system: dbAttr.system,
|
|
1642
|
+
unique: dbAttr.unique
|
|
1643
|
+
};
|
|
1644
|
+
return baseAttr;
|
|
1645
|
+
}
|
|
1646
|
+
};
|
|
1647
|
+
|
|
1648
|
+
// src/native/registry.ts
|
|
1649
|
+
var NativeObjectRegistryClass = class {
|
|
1650
|
+
constructor() {
|
|
1651
|
+
this.objects = /* @__PURE__ */ new Map();
|
|
1652
|
+
}
|
|
1653
|
+
/**
|
|
1654
|
+
* Register one or more native objects
|
|
1655
|
+
* Auto-builds if ObjectBuilder is passed instead of ObjectDefinition
|
|
1656
|
+
* @param objects - Single object/builder or array of objects/builders to register
|
|
1657
|
+
* @returns this (for chaining)
|
|
1658
|
+
* @throws Error if object is invalid or already registered
|
|
1659
|
+
*
|
|
1660
|
+
* @example
|
|
1661
|
+
* ```typescript
|
|
1662
|
+
* // No .build() needed - registry auto-builds!
|
|
1663
|
+
* const PRODUCT = object({ id: "obj-product", name: "products", label: "Product" })
|
|
1664
|
+
* .system()
|
|
1665
|
+
* .attribute(text({ id: "attr-name", name: "name", label: "Name" }));
|
|
1666
|
+
*
|
|
1667
|
+
* registry.register(PRODUCT); // ← Auto-builds here
|
|
1668
|
+
*
|
|
1669
|
+
* // Can still explicitly .build() if you want
|
|
1670
|
+
* registry.register(PRODUCT.build());
|
|
1671
|
+
*
|
|
1672
|
+
* // Register multiple
|
|
1673
|
+
* registry.register([PRODUCT, ORDER, CUSTOMER]);
|
|
1674
|
+
* ```
|
|
1675
|
+
*/
|
|
1676
|
+
register(objects) {
|
|
1677
|
+
const objectsArray = Array.isArray(objects) ? objects : [objects];
|
|
1678
|
+
for (const obj of objectsArray) {
|
|
1679
|
+
const object2 = "build" in obj && typeof obj.build === "function" ? obj.build() : obj;
|
|
1680
|
+
this.validateAndRegister(object2);
|
|
1681
|
+
}
|
|
1682
|
+
return this;
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* Validate and register a single object
|
|
1686
|
+
* @internal
|
|
1687
|
+
*/
|
|
1688
|
+
validateAndRegister(object2) {
|
|
1689
|
+
if (!object2.system) {
|
|
1690
|
+
throw new Error(
|
|
1691
|
+
`[NativeObjectRegistry] Cannot register object "${object2.name}" (id: ${object2.id}): missing system flag.
|
|
1692
|
+
Native objects must have system=true. Did you forget to call .system() in your builder?`
|
|
1693
|
+
);
|
|
1694
|
+
}
|
|
1695
|
+
if (this.objects.has(object2.name)) {
|
|
1696
|
+
const existing = this.objects.get(object2.name);
|
|
1697
|
+
throw new Error(
|
|
1698
|
+
`[NativeObjectRegistry] Duplicate object name "${object2.name}":
|
|
1699
|
+
- Existing: "${existing?.label}" (id: ${existing?.id})
|
|
1700
|
+
- New: "${object2.label}" (id: ${object2.id})
|
|
1701
|
+
Please use unique names for each native object.`
|
|
1702
|
+
);
|
|
1703
|
+
}
|
|
1704
|
+
this.objects.set(object2.name, object2);
|
|
1705
|
+
}
|
|
1706
|
+
/**
|
|
1707
|
+
* Get a native object by its name
|
|
1708
|
+
* @param name - The object name
|
|
1709
|
+
* @returns The object definition or undefined if not found
|
|
1710
|
+
*
|
|
1711
|
+
* @example
|
|
1712
|
+
* ```typescript
|
|
1713
|
+
* const product = registry.getByName("products");
|
|
1714
|
+
* if (product) {
|
|
1715
|
+
* console.log(product.label); // "Product"
|
|
1716
|
+
* }
|
|
1717
|
+
* ```
|
|
1718
|
+
*/
|
|
1719
|
+
getByName(name) {
|
|
1720
|
+
return this.objects.get(name);
|
|
1721
|
+
}
|
|
1722
|
+
/**
|
|
1723
|
+
* Get a native object or throw if not found
|
|
1724
|
+
* @param name - The object name
|
|
1725
|
+
* @returns The object definition
|
|
1726
|
+
* @throws Error if not found
|
|
1727
|
+
*/
|
|
1728
|
+
getByNameOrThrow(name) {
|
|
1729
|
+
const object2 = this.objects.get(name);
|
|
1730
|
+
if (!object2) {
|
|
1731
|
+
throw new Error(
|
|
1732
|
+
`[NativeObjectRegistry] Native object "${name}" not found.
|
|
1733
|
+
Available objects: ${this.listNames().join(", ")}`
|
|
1734
|
+
);
|
|
1735
|
+
}
|
|
1736
|
+
return object2;
|
|
1737
|
+
}
|
|
1738
|
+
/**
|
|
1739
|
+
* Get all registered native objects
|
|
1740
|
+
* @returns Array of all registered object definitions
|
|
1741
|
+
*/
|
|
1742
|
+
getAll() {
|
|
1743
|
+
return Array.from(this.objects.values());
|
|
1744
|
+
}
|
|
1745
|
+
/**
|
|
1746
|
+
* Get all registered native object names
|
|
1747
|
+
* @returns Array of object names
|
|
1748
|
+
*/
|
|
1749
|
+
listNames() {
|
|
1750
|
+
return Array.from(this.objects.keys());
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* Check if a native object is registered
|
|
1754
|
+
* @param name - The object name
|
|
1755
|
+
* @returns true if registered, false otherwise
|
|
1756
|
+
*/
|
|
1757
|
+
has(name) {
|
|
1758
|
+
return this.objects.has(name);
|
|
1759
|
+
}
|
|
1760
|
+
/**
|
|
1761
|
+
* Get the number of registered objects
|
|
1762
|
+
*/
|
|
1763
|
+
get size() {
|
|
1764
|
+
return this.objects.size;
|
|
1765
|
+
}
|
|
1766
|
+
/**
|
|
1767
|
+
* Clear all registered objects (useful for testing)
|
|
1768
|
+
* @internal
|
|
1769
|
+
*/
|
|
1770
|
+
clear() {
|
|
1771
|
+
this.objects.clear();
|
|
1772
|
+
}
|
|
1773
|
+
/**
|
|
1774
|
+
* Get registry summary for debugging
|
|
1775
|
+
*/
|
|
1776
|
+
summary() {
|
|
1777
|
+
const objects = this.getAll();
|
|
1778
|
+
return [
|
|
1779
|
+
`[NativeObjectRegistry] ${this.size} native object(s) registered:`,
|
|
1780
|
+
...objects.map(
|
|
1781
|
+
(obj) => ` - ${obj.name}: "${obj.label}" (${obj.attributes.length} attributes)`
|
|
1782
|
+
)
|
|
1783
|
+
].join("\n");
|
|
1784
|
+
}
|
|
1785
|
+
/**
|
|
1786
|
+
* Log registry summary to console
|
|
1787
|
+
*/
|
|
1788
|
+
debug() {
|
|
1789
|
+
console.info(this.summary());
|
|
1790
|
+
}
|
|
1791
|
+
};
|
|
1792
|
+
var registry = new NativeObjectRegistryClass();
|
|
1793
|
+
|
|
1794
|
+
// src/validation/validators.ts
|
|
1795
|
+
import { z as z3 } from "zod";
|
|
1796
|
+
function createTextValidator(attr) {
|
|
1797
|
+
let schema = z3.string();
|
|
1798
|
+
if (attr.minLength !== void 0) {
|
|
1799
|
+
schema = schema.min(
|
|
1800
|
+
attr.minLength,
|
|
1801
|
+
`${attr.label} must be at least ${attr.minLength} characters`
|
|
1802
|
+
);
|
|
1803
|
+
}
|
|
1804
|
+
if (attr.maxLength !== void 0) {
|
|
1805
|
+
schema = schema.max(
|
|
1806
|
+
attr.maxLength,
|
|
1807
|
+
`${attr.label} must be at most ${attr.maxLength} characters`
|
|
1808
|
+
);
|
|
1809
|
+
}
|
|
1810
|
+
if (attr.pattern) {
|
|
1811
|
+
schema = schema.regex(new RegExp(attr.pattern), `${attr.label} format is invalid`);
|
|
1812
|
+
}
|
|
1813
|
+
return schema;
|
|
1814
|
+
}
|
|
1815
|
+
function createNumberValidator(attr) {
|
|
1816
|
+
let schema = z3.number();
|
|
1817
|
+
if (attr.min !== void 0) {
|
|
1818
|
+
schema = schema.min(attr.min, `${attr.label} must be at least ${attr.min}`);
|
|
1819
|
+
}
|
|
1820
|
+
if (attr.max !== void 0) {
|
|
1821
|
+
schema = schema.max(attr.max, `${attr.label} must be at most ${attr.max}`);
|
|
1822
|
+
}
|
|
1823
|
+
if (attr.unit === "integer") {
|
|
1824
|
+
schema = schema.int(`${attr.label} must be an integer`);
|
|
1825
|
+
}
|
|
1826
|
+
return schema;
|
|
1827
|
+
}
|
|
1828
|
+
function createCheckboxValidator(_attr) {
|
|
1829
|
+
return z3.boolean();
|
|
1830
|
+
}
|
|
1831
|
+
function createDateValidator(attr) {
|
|
1832
|
+
return z3.string().datetime({ message: `${attr.label} must be a valid ISO date` });
|
|
1833
|
+
}
|
|
1834
|
+
function createPhoneValidator(_attr) {
|
|
1835
|
+
return z3.object({
|
|
1836
|
+
countryCode: z3.string().length(3),
|
|
1837
|
+
phoneNumber: z3.string().min(1)
|
|
1838
|
+
});
|
|
1839
|
+
}
|
|
1840
|
+
function createCurrencyValidator(_attr) {
|
|
1841
|
+
return z3.object({
|
|
1842
|
+
code: z3.string().length(3),
|
|
1843
|
+
value: z3.number().min(0)
|
|
1844
|
+
});
|
|
1845
|
+
}
|
|
1846
|
+
function createStatusValidator(attr) {
|
|
1847
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
1848
|
+
return z3.enum(validValues, {
|
|
1849
|
+
errorMap: () => ({
|
|
1850
|
+
message: `${attr.label} must be one of: ${validValues.join(", ")}`
|
|
1851
|
+
})
|
|
1852
|
+
});
|
|
1853
|
+
}
|
|
1854
|
+
function createSelectValidator(attr) {
|
|
1855
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
1856
|
+
return z3.enum(validValues, {
|
|
1857
|
+
errorMap: () => ({
|
|
1858
|
+
message: `${attr.label} must be one of: ${validValues.join(", ")}`
|
|
1859
|
+
})
|
|
1860
|
+
});
|
|
1861
|
+
}
|
|
1862
|
+
function createMultiselectValidator(attr) {
|
|
1863
|
+
const validValues = attr.options.map((opt) => opt.value);
|
|
1864
|
+
return z3.array(
|
|
1865
|
+
z3.enum(validValues, {
|
|
1866
|
+
errorMap: () => ({
|
|
1867
|
+
message: `Each value must be one of: ${validValues.join(", ")}`
|
|
1868
|
+
})
|
|
1869
|
+
})
|
|
1870
|
+
);
|
|
1871
|
+
}
|
|
1872
|
+
function createLocationValidator(_attr) {
|
|
1873
|
+
return z3.object({
|
|
1874
|
+
address: z3.string().optional(),
|
|
1875
|
+
address2: z3.string().optional(),
|
|
1876
|
+
city: z3.string().optional(),
|
|
1877
|
+
state: z3.string().optional(),
|
|
1878
|
+
postalCode: z3.string().optional(),
|
|
1879
|
+
country: z3.string().length(3).optional(),
|
|
1880
|
+
latitude: z3.number().optional(),
|
|
1881
|
+
longitude: z3.number().optional()
|
|
1882
|
+
});
|
|
1883
|
+
}
|
|
1884
|
+
function createTimestampValidator(_attr) {
|
|
1885
|
+
return z3.number().int().positive();
|
|
1886
|
+
}
|
|
1887
|
+
function createFileValidator(_attr) {
|
|
1888
|
+
return z3.string().uuid();
|
|
1889
|
+
}
|
|
1890
|
+
function createUserValidator(_attr) {
|
|
1891
|
+
return z3.string().uuid();
|
|
1892
|
+
}
|
|
1893
|
+
function createRelationValidator(_attr) {
|
|
1894
|
+
return z3.string().uuid();
|
|
1895
|
+
}
|
|
1896
|
+
function createRatingValidator(attr) {
|
|
1897
|
+
let schema = z3.number().int().min(0);
|
|
1898
|
+
if (attr.max !== void 0) {
|
|
1899
|
+
schema = schema.max(attr.max, `${attr.label} must be at most ${attr.max}`);
|
|
1900
|
+
}
|
|
1901
|
+
return schema;
|
|
1902
|
+
}
|
|
1903
|
+
function createAttributeValidator(attr) {
|
|
1904
|
+
switch (attr.type) {
|
|
1905
|
+
case "text":
|
|
1906
|
+
return createTextValidator(attr);
|
|
1907
|
+
case "number":
|
|
1908
|
+
return createNumberValidator(attr);
|
|
1909
|
+
case "checkbox":
|
|
1910
|
+
return createCheckboxValidator(attr);
|
|
1911
|
+
case "date":
|
|
1912
|
+
return createDateValidator(attr);
|
|
1913
|
+
case "phone":
|
|
1914
|
+
return createPhoneValidator(attr);
|
|
1915
|
+
case "currency":
|
|
1916
|
+
return createCurrencyValidator(attr);
|
|
1917
|
+
case "status":
|
|
1918
|
+
return createStatusValidator(attr);
|
|
1919
|
+
case "location":
|
|
1920
|
+
return createLocationValidator(attr);
|
|
1921
|
+
case "timestamp":
|
|
1922
|
+
return createTimestampValidator(attr);
|
|
1923
|
+
case "select":
|
|
1924
|
+
return createSelectValidator(attr);
|
|
1925
|
+
case "multiselect":
|
|
1926
|
+
return createMultiselectValidator(attr);
|
|
1927
|
+
case "file":
|
|
1928
|
+
return createFileValidator(attr);
|
|
1929
|
+
case "user":
|
|
1930
|
+
return createUserValidator(attr);
|
|
1931
|
+
case "relation":
|
|
1932
|
+
return createRelationValidator(attr);
|
|
1933
|
+
case "rating":
|
|
1934
|
+
return createRatingValidator(attr);
|
|
1935
|
+
default:
|
|
1936
|
+
return z3.unknown();
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
function createObjectValidator(objectDef) {
|
|
1940
|
+
const shape = {};
|
|
1941
|
+
for (const attr of objectDef.attributes) {
|
|
1942
|
+
let validator = createAttributeValidator(attr);
|
|
1943
|
+
if (!attr.required) {
|
|
1944
|
+
validator = validator.optional();
|
|
1945
|
+
}
|
|
1946
|
+
shape[attr.name] = validator;
|
|
1947
|
+
}
|
|
1948
|
+
return z3.object(shape);
|
|
1949
|
+
}
|
|
1950
|
+
function validateAttribute(attr, value) {
|
|
1951
|
+
const validator = createAttributeValidator(attr);
|
|
1952
|
+
if (!attr.required && (value === void 0 || value === null)) {
|
|
1953
|
+
return { success: true, data: { [attr.name]: value } };
|
|
1954
|
+
}
|
|
1955
|
+
const result = validator.safeParse(value);
|
|
1956
|
+
if (result.success) {
|
|
1957
|
+
return {
|
|
1958
|
+
success: true,
|
|
1959
|
+
data: { [attr.name]: result.data }
|
|
1960
|
+
};
|
|
1961
|
+
}
|
|
1962
|
+
return {
|
|
1963
|
+
success: false,
|
|
1964
|
+
errors: result.error.errors.map((err) => ({
|
|
1965
|
+
path: [attr.name, ...err.path.map(String)],
|
|
1966
|
+
message: err.message
|
|
1967
|
+
}))
|
|
1968
|
+
};
|
|
1969
|
+
}
|
|
1970
|
+
function validateObject(objectDef, data) {
|
|
1971
|
+
const validator = createObjectValidator(objectDef);
|
|
1972
|
+
const result = validator.safeParse(data);
|
|
1973
|
+
if (result.success) {
|
|
1974
|
+
return {
|
|
1975
|
+
success: true,
|
|
1976
|
+
data: result.data
|
|
1977
|
+
};
|
|
1978
|
+
}
|
|
1979
|
+
return {
|
|
1980
|
+
success: false,
|
|
1981
|
+
errors: result.error.errors.map((err) => ({
|
|
1982
|
+
path: err.path.map(String),
|
|
1983
|
+
message: err.message
|
|
1984
|
+
}))
|
|
1985
|
+
};
|
|
1986
|
+
}
|
|
1987
|
+
function validateObjectOrThrow(objectDef, data) {
|
|
1988
|
+
const result = validateObject(objectDef, data);
|
|
1989
|
+
if (!result.success) {
|
|
1990
|
+
const errorMessages = result.errors?.map((err) => `${err.path.join(".")}: ${err.message}`).join("\n") || "Unknown validation error";
|
|
1991
|
+
throw new Error(`Validation failed for ${objectDef.label}:
|
|
1992
|
+
${errorMessages}`);
|
|
1993
|
+
}
|
|
1994
|
+
return result.data;
|
|
1995
|
+
}
|
|
1996
|
+
function createDraftValidator(objectDef) {
|
|
1997
|
+
const shape = {};
|
|
1998
|
+
for (const attr of objectDef.attributes) {
|
|
1999
|
+
const validator = createAttributeValidator(attr).optional();
|
|
2000
|
+
shape[attr.name] = validator;
|
|
2001
|
+
}
|
|
2002
|
+
return z3.object(shape);
|
|
2003
|
+
}
|
|
2004
|
+
function validateDraft(objectDef, data) {
|
|
2005
|
+
const validator = createDraftValidator(objectDef);
|
|
2006
|
+
const result = validator.safeParse(data);
|
|
2007
|
+
if (result.success) {
|
|
2008
|
+
return {
|
|
2009
|
+
success: true,
|
|
2010
|
+
data: result.data
|
|
2011
|
+
};
|
|
2012
|
+
}
|
|
2013
|
+
return {
|
|
2014
|
+
success: false,
|
|
2015
|
+
errors: result.error.errors.map((err) => ({
|
|
2016
|
+
path: err.path.map(String),
|
|
2017
|
+
message: err.message
|
|
2018
|
+
}))
|
|
2019
|
+
};
|
|
2020
|
+
}
|
|
2021
|
+
function validateDraftOrThrow(objectDef, data) {
|
|
2022
|
+
const result = validateDraft(objectDef, data);
|
|
2023
|
+
if (!result.success) {
|
|
2024
|
+
const errorMessages = result.errors?.map((err) => `${err.path.join(".")}: ${err.message}`).join("\n") || "Unknown validation error";
|
|
2025
|
+
throw new Error(`Draft validation failed for ${objectDef.label}:
|
|
2026
|
+
${errorMessages}`);
|
|
2027
|
+
}
|
|
2028
|
+
return result.data;
|
|
2029
|
+
}
|
|
2030
|
+
function isValuePresent(value) {
|
|
2031
|
+
if (value === void 0 || value === null) {
|
|
2032
|
+
return false;
|
|
2033
|
+
}
|
|
2034
|
+
if (typeof value === "string" && value.trim() === "") {
|
|
2035
|
+
return false;
|
|
2036
|
+
}
|
|
2037
|
+
return true;
|
|
2038
|
+
}
|
|
2039
|
+
function getMissingRequiredAttributes(objectDef, data) {
|
|
2040
|
+
const missing = [];
|
|
2041
|
+
for (const attr of objectDef.attributes) {
|
|
2042
|
+
if (attr.required && !isValuePresent(data[attr.name])) {
|
|
2043
|
+
missing.push(attr);
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
return missing;
|
|
2047
|
+
}
|
|
2048
|
+
function isRecordComplete(objectDef, data) {
|
|
2049
|
+
const missing = getMissingRequiredAttributes(objectDef, data);
|
|
2050
|
+
if (missing.length > 0) {
|
|
2051
|
+
return false;
|
|
2052
|
+
}
|
|
2053
|
+
const validation = validateObject(objectDef, data);
|
|
2054
|
+
return validation.success;
|
|
2055
|
+
}
|
|
2056
|
+
function computeRecordStatus(objectDef, data) {
|
|
2057
|
+
return isRecordComplete(objectDef, data) ? "complete" : "draft";
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
// src/runtime/services/record.service.ts
|
|
2061
|
+
var RecordService = class {
|
|
2062
|
+
constructor(adapter, tenantId) {
|
|
2063
|
+
this.adapter = adapter;
|
|
2064
|
+
this.tenantId = tenantId;
|
|
2065
|
+
this.schemaService = new ObjectSchemaService(adapter, registry);
|
|
2066
|
+
}
|
|
2067
|
+
/**
|
|
2068
|
+
* Create a new record with validation
|
|
2069
|
+
*
|
|
2070
|
+
* @param objectId - Object UUID
|
|
2071
|
+
* @param data - Record data (attribute values)
|
|
2072
|
+
* @param options - Creation options
|
|
2073
|
+
* @returns Created record with computed completionStatus
|
|
2074
|
+
*
|
|
2075
|
+
* @example
|
|
2076
|
+
* ```typescript
|
|
2077
|
+
* const service = new RecordService(adapter, "tenant-123");
|
|
2078
|
+
*
|
|
2079
|
+
* // Create a complete record (strict validation)
|
|
2080
|
+
* const product = await service.createRecord("obj-product", {
|
|
2081
|
+
* name: "Nike Air Max",
|
|
2082
|
+
* price: 129.99,
|
|
2083
|
+
* status: "active"
|
|
2084
|
+
* });
|
|
2085
|
+
* // → product.completionStatus = "complete"
|
|
2086
|
+
*
|
|
2087
|
+
* // Create a draft record (allows missing required fields)
|
|
2088
|
+
* const draft = await service.createRecord("obj-product", {
|
|
2089
|
+
* name: "Draft Product"
|
|
2090
|
+
* }, { allowDraft: true });
|
|
2091
|
+
* // → draft.completionStatus = "draft"
|
|
2092
|
+
* ```
|
|
2093
|
+
*/
|
|
2094
|
+
async createRecord(objectId, data, options) {
|
|
2095
|
+
const schema = await this.schemaService.getObjectSchema(objectId);
|
|
2096
|
+
if (options?.validate !== false) {
|
|
2097
|
+
if (options?.allowDraft) {
|
|
2098
|
+
validateDraftOrThrow(schema, data);
|
|
2099
|
+
} else {
|
|
2100
|
+
validateObjectOrThrow(schema, data);
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
const completionStatus = computeRecordStatus(schema, data);
|
|
2104
|
+
const record = await this.adapter.objectRecords.create({
|
|
2105
|
+
tenantId: this.tenantId,
|
|
2106
|
+
objectId,
|
|
2107
|
+
data,
|
|
2108
|
+
completionStatus
|
|
2109
|
+
});
|
|
2110
|
+
return record;
|
|
2111
|
+
}
|
|
2112
|
+
/**
|
|
2113
|
+
* Get a record by ID
|
|
2114
|
+
*
|
|
2115
|
+
* @param recordId - Record UUID
|
|
2116
|
+
* @param options - Query options
|
|
2117
|
+
* @returns Record or null if not found
|
|
2118
|
+
*/
|
|
2119
|
+
async getRecord(recordId, options) {
|
|
2120
|
+
const record = await this.adapter.objectRecords.findById(recordId);
|
|
2121
|
+
if (!record) {
|
|
2122
|
+
return null;
|
|
2123
|
+
}
|
|
2124
|
+
if (options?.includeSchema) {
|
|
2125
|
+
const schema = await this.schemaService.getObjectSchema(record.objectId);
|
|
2126
|
+
const recordWithSchema = record;
|
|
2127
|
+
recordWithSchema.schema = schema;
|
|
2128
|
+
return recordWithSchema;
|
|
2129
|
+
}
|
|
2130
|
+
return record;
|
|
2131
|
+
}
|
|
2132
|
+
/**
|
|
2133
|
+
* Get a record by ID or throw if not found
|
|
2134
|
+
*/
|
|
2135
|
+
async getRecordOrThrow(recordId) {
|
|
2136
|
+
const record = await this.getRecord(recordId);
|
|
2137
|
+
if (!record) {
|
|
2138
|
+
throw new Error(`Record with id "${recordId}" not found`);
|
|
2139
|
+
}
|
|
2140
|
+
return record;
|
|
2141
|
+
}
|
|
2142
|
+
/**
|
|
2143
|
+
* Update a record with validation
|
|
2144
|
+
*
|
|
2145
|
+
* The completion status is automatically recalculated after each update.
|
|
2146
|
+
* A draft record becomes complete when all required fields are filled.
|
|
2147
|
+
*
|
|
2148
|
+
* @param recordId - Record UUID
|
|
2149
|
+
* @param data - Partial data to update
|
|
2150
|
+
* @param options - Update options
|
|
2151
|
+
* @returns Updated record with recalculated completionStatus
|
|
2152
|
+
*
|
|
2153
|
+
* @example
|
|
2154
|
+
* ```typescript
|
|
2155
|
+
* // Update a draft record to make it complete
|
|
2156
|
+
* const updated = await service.updateRecord(draftId, {
|
|
2157
|
+
* price: 99.99,
|
|
2158
|
+
* status: "active"
|
|
2159
|
+
* });
|
|
2160
|
+
* // → updated.completionStatus = "complete" if all required fields now present
|
|
2161
|
+
* ```
|
|
2162
|
+
*/
|
|
2163
|
+
async updateRecord(recordId, data, options) {
|
|
2164
|
+
const existing = await this.getRecordOrThrow(recordId);
|
|
2165
|
+
const schema = await this.schemaService.getObjectSchema(existing.objectId);
|
|
2166
|
+
const mergedData = { ...existing.values, ...data };
|
|
2167
|
+
if (options?.validate !== false) {
|
|
2168
|
+
if (options?.partial) {
|
|
2169
|
+
validateDraftOrThrow(schema, mergedData);
|
|
2170
|
+
} else {
|
|
2171
|
+
validateObjectOrThrow(schema, mergedData);
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
const completionStatus = computeRecordStatus(schema, mergedData);
|
|
2175
|
+
const updated = await this.adapter.objectRecords.update(recordId, {
|
|
2176
|
+
...data,
|
|
2177
|
+
__completionStatus: completionStatus
|
|
2178
|
+
});
|
|
2179
|
+
return updated;
|
|
2180
|
+
}
|
|
2181
|
+
/**
|
|
2182
|
+
* Delete a record
|
|
2183
|
+
*
|
|
2184
|
+
* @param recordId - Record UUID
|
|
2185
|
+
* @param options - Delete options
|
|
2186
|
+
*/
|
|
2187
|
+
async deleteRecord(recordId, options) {
|
|
2188
|
+
if (options?.checkSystem) {
|
|
2189
|
+
const record = await this.getRecordOrThrow(recordId);
|
|
2190
|
+
const schema = await this.schemaService.getObjectSchema(record.objectId);
|
|
2191
|
+
if (schema.system) {
|
|
2192
|
+
throw new Error(`Cannot delete record of system object "${schema.label}"`);
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
await this.adapter.objectRecords.delete(recordId);
|
|
2196
|
+
}
|
|
2197
|
+
/**
|
|
2198
|
+
* List records for an object with pagination
|
|
2199
|
+
*
|
|
2200
|
+
* @param objectId - Object UUID
|
|
2201
|
+
* @param options - List options
|
|
2202
|
+
* @returns Records and total count
|
|
2203
|
+
*/
|
|
2204
|
+
async listRecords(objectId, options) {
|
|
2205
|
+
return await this.adapter.objectRecords.list(this.tenantId, objectId, options);
|
|
2206
|
+
}
|
|
2207
|
+
/**
|
|
2208
|
+
* Search records using full-text search
|
|
2209
|
+
*
|
|
2210
|
+
* @param objectId - Object UUID
|
|
2211
|
+
* @param query - Search query
|
|
2212
|
+
* @param options - Search options
|
|
2213
|
+
* @returns Matching records and total count
|
|
2214
|
+
*/
|
|
2215
|
+
async searchRecords(objectId, query, options) {
|
|
2216
|
+
return await this.adapter.objectRecords.search(this.tenantId, objectId, query, options);
|
|
2217
|
+
}
|
|
2218
|
+
/**
|
|
2219
|
+
* Validate data against object schema without saving
|
|
2220
|
+
*
|
|
2221
|
+
* @param objectId - Object UUID
|
|
2222
|
+
* @param data - Data to validate
|
|
2223
|
+
* @returns Validation result
|
|
2224
|
+
*/
|
|
2225
|
+
async validateData(objectId, data) {
|
|
2226
|
+
const schema = await this.schemaService.getObjectSchema(objectId);
|
|
2227
|
+
return validateObject(schema, data);
|
|
2228
|
+
}
|
|
2229
|
+
/**
|
|
2230
|
+
* Compute the completion status for given data without saving.
|
|
2231
|
+
* Useful for UI to show draft/complete status before submitting.
|
|
2232
|
+
*
|
|
2233
|
+
* @param objectId - Object UUID
|
|
2234
|
+
* @param data - Data to check
|
|
2235
|
+
* @returns Computed completion status
|
|
2236
|
+
*/
|
|
2237
|
+
async computeStatus(objectId, data) {
|
|
2238
|
+
const schema = await this.schemaService.getObjectSchema(objectId);
|
|
2239
|
+
return computeRecordStatus(schema, data);
|
|
2240
|
+
}
|
|
2241
|
+
/**
|
|
2242
|
+
* Refresh the completion status of an existing record.
|
|
2243
|
+
* Useful when schema changes and you need to recompute statuses.
|
|
2244
|
+
*
|
|
2245
|
+
* @param recordId - Record UUID
|
|
2246
|
+
* @returns Updated completion status
|
|
2247
|
+
*/
|
|
2248
|
+
async refreshRecordStatus(recordId) {
|
|
2249
|
+
const record = await this.getRecordOrThrow(recordId);
|
|
2250
|
+
const schema = await this.schemaService.getObjectSchema(record.objectId);
|
|
2251
|
+
const newStatus = computeRecordStatus(schema, record.values);
|
|
2252
|
+
if (record.completionStatus !== newStatus) {
|
|
2253
|
+
await this.adapter.objectRecords.update(recordId, {
|
|
2254
|
+
__completionStatus: newStatus
|
|
2255
|
+
});
|
|
2256
|
+
}
|
|
2257
|
+
return newStatus;
|
|
2258
|
+
}
|
|
2259
|
+
};
|
|
2260
|
+
|
|
2261
|
+
// src/runtime/services/user-profile.service.ts
|
|
2262
|
+
var UserProfileService = class {
|
|
2263
|
+
constructor(adapter, tenantId) {
|
|
2264
|
+
this.adapter = adapter;
|
|
2265
|
+
this.tenantId = tenantId;
|
|
2266
|
+
}
|
|
2267
|
+
/**
|
|
2268
|
+
* Create a new user profile (typically after first auth)
|
|
2269
|
+
*
|
|
2270
|
+
* @param data - User profile creation data
|
|
2271
|
+
* @returns Created user profile
|
|
2272
|
+
*
|
|
2273
|
+
* @example
|
|
2274
|
+
* ```typescript
|
|
2275
|
+
* const service = new UserProfileService(adapter, "tenant-123");
|
|
2276
|
+
*
|
|
2277
|
+
* // After Supabase auth
|
|
2278
|
+
* const profile = await service.createProfile({
|
|
2279
|
+
* tenantId: "tenant-123",
|
|
2280
|
+
* authId: authUser.id,
|
|
2281
|
+
* email: authUser.email,
|
|
2282
|
+
* firstName: authUser.user_metadata.first_name,
|
|
2283
|
+
* lastName: authUser.user_metadata.last_name,
|
|
2284
|
+
* role: "member",
|
|
2285
|
+
* status: "active"
|
|
2286
|
+
* });
|
|
2287
|
+
* ```
|
|
2288
|
+
*/
|
|
2289
|
+
async createProfile(data) {
|
|
2290
|
+
if (data.tenantId !== this.tenantId) {
|
|
2291
|
+
throw new Error(
|
|
2292
|
+
`Tenant mismatch: service initialized with "${this.tenantId}" but data has "${data.tenantId}"`
|
|
2293
|
+
);
|
|
2294
|
+
}
|
|
2295
|
+
const existing = await this.adapter.userProfiles.findByAuthId(data.authId);
|
|
2296
|
+
if (existing) {
|
|
2297
|
+
throw new Error(`User profile with authId "${data.authId}" already exists`);
|
|
2298
|
+
}
|
|
2299
|
+
const existingEmail = await this.adapter.userProfiles.findByEmail(this.tenantId, data.email);
|
|
2300
|
+
if (existingEmail) {
|
|
2301
|
+
throw new Error(`User profile with email "${data.email}" already exists in this tenant`);
|
|
2302
|
+
}
|
|
2303
|
+
return await this.adapter.userProfiles.create(data);
|
|
2304
|
+
}
|
|
2305
|
+
/**
|
|
2306
|
+
* Get user profile by ID
|
|
2307
|
+
*/
|
|
2308
|
+
async getProfile(profileId) {
|
|
2309
|
+
return await this.adapter.userProfiles.findById(profileId);
|
|
2310
|
+
}
|
|
2311
|
+
/**
|
|
2312
|
+
* Get user profile by ID or throw
|
|
2313
|
+
*/
|
|
2314
|
+
async getProfileOrThrow(profileId) {
|
|
2315
|
+
const profile = await this.getProfile(profileId);
|
|
2316
|
+
if (!profile) {
|
|
2317
|
+
throw new Error(`User profile with id "${profileId}" not found`);
|
|
2318
|
+
}
|
|
2319
|
+
return profile;
|
|
2320
|
+
}
|
|
2321
|
+
/**
|
|
2322
|
+
* Get user profile by auth provider ID
|
|
2323
|
+
*
|
|
2324
|
+
* @param authId - Auth provider user ID (Supabase, Clerk, etc.)
|
|
2325
|
+
* @returns User profile or null
|
|
2326
|
+
*/
|
|
2327
|
+
async getProfileByAuthId(authId) {
|
|
2328
|
+
return await this.adapter.userProfiles.findByAuthId(authId);
|
|
2329
|
+
}
|
|
2330
|
+
/**
|
|
2331
|
+
* Get or create user profile (idempotent operation)
|
|
2332
|
+
* Useful for auth callbacks - ensures profile exists
|
|
2333
|
+
*
|
|
2334
|
+
* @param authId - Auth provider user ID
|
|
2335
|
+
* @param data - Profile data to create if doesn't exist
|
|
2336
|
+
* @returns Existing or newly created profile
|
|
2337
|
+
*
|
|
2338
|
+
* @example
|
|
2339
|
+
* ```typescript
|
|
2340
|
+
* // In Supabase auth callback
|
|
2341
|
+
* const profile = await service.getOrCreateProfile(
|
|
2342
|
+
* authUser.id,
|
|
2343
|
+
* {
|
|
2344
|
+
* tenantId: "tenant-123",
|
|
2345
|
+
* authId: authUser.id,
|
|
2346
|
+
* email: authUser.email,
|
|
2347
|
+
* role: "member",
|
|
2348
|
+
* status: "active"
|
|
2349
|
+
* }
|
|
2350
|
+
* );
|
|
2351
|
+
* ```
|
|
2352
|
+
*/
|
|
2353
|
+
async getOrCreateProfile(authId, data) {
|
|
2354
|
+
const existing = await this.getProfileByAuthId(authId);
|
|
2355
|
+
if (existing) {
|
|
2356
|
+
return existing;
|
|
2357
|
+
}
|
|
2358
|
+
return await this.createProfile(data);
|
|
2359
|
+
}
|
|
2360
|
+
/**
|
|
2361
|
+
* Update user profile
|
|
2362
|
+
*/
|
|
2363
|
+
async updateProfile(profileId, data) {
|
|
2364
|
+
await this.getProfileOrThrow(profileId);
|
|
2365
|
+
return await this.adapter.userProfiles.update(profileId, data);
|
|
2366
|
+
}
|
|
2367
|
+
/**
|
|
2368
|
+
* Delete user profile
|
|
2369
|
+
*
|
|
2370
|
+
* @param profileId - Profile UUID
|
|
2371
|
+
* @param options - Delete options
|
|
2372
|
+
*/
|
|
2373
|
+
async deleteProfile(profileId, options) {
|
|
2374
|
+
if (options?.checkAdmin) {
|
|
2375
|
+
const profile = await this.getProfileOrThrow(profileId);
|
|
2376
|
+
if (profile.role === "admin") {
|
|
2377
|
+
const allProfiles = await this.listProfiles();
|
|
2378
|
+
const adminCount = allProfiles.filter((p) => p.role === "admin").length;
|
|
2379
|
+
if (adminCount <= 1) {
|
|
2380
|
+
throw new Error("Cannot delete the last admin user");
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
await this.adapter.userProfiles.delete(profileId);
|
|
2385
|
+
}
|
|
2386
|
+
/**
|
|
2387
|
+
* List all user profiles for the tenant
|
|
2388
|
+
*/
|
|
2389
|
+
async listProfiles(options) {
|
|
2390
|
+
return await this.adapter.userProfiles.list(this.tenantId, options);
|
|
2391
|
+
}
|
|
2392
|
+
/**
|
|
2393
|
+
* Update last login timestamp
|
|
2394
|
+
*
|
|
2395
|
+
* @param profileId - Profile UUID
|
|
2396
|
+
*
|
|
2397
|
+
* @example
|
|
2398
|
+
* ```typescript
|
|
2399
|
+
* // After successful auth
|
|
2400
|
+
* await service.updateLastLogin(profile.id);
|
|
2401
|
+
* ```
|
|
2402
|
+
*/
|
|
2403
|
+
async updateLastLogin(profileId) {
|
|
2404
|
+
await this.adapter.userProfiles.updateLastLogin(profileId);
|
|
2405
|
+
}
|
|
2406
|
+
/**
|
|
2407
|
+
* Change user role
|
|
2408
|
+
*
|
|
2409
|
+
* @param profileId - Profile UUID
|
|
2410
|
+
* @param newRole - New role
|
|
2411
|
+
*/
|
|
2412
|
+
async changeRole(profileId, newRole) {
|
|
2413
|
+
return await this.updateProfile(profileId, { role: newRole });
|
|
2414
|
+
}
|
|
2415
|
+
/**
|
|
2416
|
+
* Change user status
|
|
2417
|
+
*
|
|
2418
|
+
* @param profileId - Profile UUID
|
|
2419
|
+
* @param newStatus - New status
|
|
2420
|
+
*/
|
|
2421
|
+
async changeStatus(profileId, newStatus) {
|
|
2422
|
+
return await this.updateProfile(profileId, { status: newStatus });
|
|
2423
|
+
}
|
|
2424
|
+
/**
|
|
2425
|
+
* Get user by email
|
|
2426
|
+
*/
|
|
2427
|
+
async getProfileByEmail(email) {
|
|
2428
|
+
return await this.adapter.userProfiles.findByEmail(this.tenantId, email);
|
|
2429
|
+
}
|
|
2430
|
+
/**
|
|
2431
|
+
* Check if user has role
|
|
2432
|
+
*/
|
|
2433
|
+
async hasRole(profileId, role) {
|
|
2434
|
+
const profile = await this.getProfile(profileId);
|
|
2435
|
+
return profile?.role === role;
|
|
2436
|
+
}
|
|
2437
|
+
/**
|
|
2438
|
+
* Check if user is admin
|
|
2439
|
+
*/
|
|
2440
|
+
async isAdmin(profileId) {
|
|
2441
|
+
return await this.hasRole(profileId, "admin");
|
|
2442
|
+
}
|
|
2443
|
+
};
|
|
2444
|
+
|
|
2445
|
+
// src/runtime/sync.ts
|
|
2446
|
+
var BASE_ATTRIBUTE_KEYS = /* @__PURE__ */ new Set([
|
|
2447
|
+
"id",
|
|
2448
|
+
"name",
|
|
2449
|
+
"label",
|
|
2450
|
+
"type",
|
|
2451
|
+
"required",
|
|
2452
|
+
"system",
|
|
2453
|
+
"unique",
|
|
2454
|
+
"order"
|
|
2455
|
+
]);
|
|
2456
|
+
function extractAttributeConfig(attr) {
|
|
2457
|
+
return Object.fromEntries(
|
|
2458
|
+
Object.entries(attr).filter(
|
|
2459
|
+
([key, value]) => !BASE_ATTRIBUTE_KEYS.has(key) && value !== void 0
|
|
2460
|
+
)
|
|
2461
|
+
);
|
|
2462
|
+
}
|
|
2463
|
+
async function syncNativeObjects(adapter, nativeRegistry, options = {}) {
|
|
2464
|
+
const result = {
|
|
2465
|
+
success: true,
|
|
2466
|
+
objectsSynced: 0,
|
|
2467
|
+
attributesSynced: 0,
|
|
2468
|
+
objectsCreated: 0,
|
|
2469
|
+
objectsUpdated: 0,
|
|
2470
|
+
attributesCreated: 0,
|
|
2471
|
+
attributesUpdated: 0,
|
|
2472
|
+
attributesDeleted: 0,
|
|
2473
|
+
errors: []
|
|
2474
|
+
};
|
|
2475
|
+
const nativeObjects = nativeRegistry.getAll();
|
|
2476
|
+
if (options.verbose) {
|
|
2477
|
+
console.info(`[SyncService] Starting sync for ${nativeObjects.length} native objects...`);
|
|
2478
|
+
}
|
|
2479
|
+
try {
|
|
2480
|
+
await adapter.transaction(async (tx) => {
|
|
2481
|
+
for (const nativeObject of nativeObjects) {
|
|
2482
|
+
try {
|
|
2483
|
+
await syncSingleObject(tx, nativeObject, result, options);
|
|
2484
|
+
} catch (error) {
|
|
2485
|
+
result.success = false;
|
|
2486
|
+
result.errors.push({
|
|
2487
|
+
objectName: nativeObject.name,
|
|
2488
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2489
|
+
});
|
|
2490
|
+
if (options.verbose) {
|
|
2491
|
+
console.error(`[SyncService] \u2717 Failed to sync ${nativeObject.name}:`, error);
|
|
2492
|
+
}
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
});
|
|
2496
|
+
} catch (error) {
|
|
2497
|
+
result.success = false;
|
|
2498
|
+
result.errors.push({
|
|
2499
|
+
objectName: "transaction",
|
|
2500
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2501
|
+
});
|
|
2502
|
+
}
|
|
2503
|
+
if (options.verbose) {
|
|
2504
|
+
console.info(
|
|
2505
|
+
`[SyncService] ${result.success ? "\u2713" : "\u2717"} Sync complete:
|
|
2506
|
+
Objects: ${result.objectsCreated} created, ${result.objectsUpdated} updated
|
|
2507
|
+
Attributes: ${result.attributesCreated} created, ${result.attributesUpdated} updated, ${result.attributesDeleted} deleted
|
|
2508
|
+
Errors: ${result.errors.length}`
|
|
2509
|
+
);
|
|
2510
|
+
}
|
|
2511
|
+
return result;
|
|
2512
|
+
}
|
|
2513
|
+
async function syncSingleObject(adapter, nativeObject, result, options) {
|
|
2514
|
+
if (!nativeObject.system) {
|
|
2515
|
+
throw new Error(`Object ${nativeObject.name} is not marked as system`);
|
|
2516
|
+
}
|
|
2517
|
+
const existingObject = await adapter.objects.findSystemByName(nativeObject.name);
|
|
2518
|
+
const isNew = !existingObject;
|
|
2519
|
+
updateObjectStats(result, isNew);
|
|
2520
|
+
if (options.dryRun) {
|
|
2521
|
+
await handleDryRun(adapter, nativeObject, existingObject, result, options, isNew);
|
|
2522
|
+
return;
|
|
2523
|
+
}
|
|
2524
|
+
const dbObject = await upsertObject(adapter, nativeObject, options);
|
|
2525
|
+
await syncAttributes(adapter, nativeObject, dbObject, existingObject, result);
|
|
2526
|
+
if (!isNew) {
|
|
2527
|
+
await cleanupAttributes(adapter, dbObject.id, nativeObject, result);
|
|
2528
|
+
}
|
|
2529
|
+
logSyncSuccess(nativeObject, options);
|
|
2530
|
+
}
|
|
2531
|
+
function updateObjectStats(result, isNew) {
|
|
2532
|
+
if (isNew) {
|
|
2533
|
+
result.objectsCreated++;
|
|
2534
|
+
} else {
|
|
2535
|
+
result.objectsUpdated++;
|
|
2536
|
+
}
|
|
2537
|
+
result.objectsSynced++;
|
|
2538
|
+
}
|
|
2539
|
+
async function handleDryRun(adapter, nativeObject, existingObject, result, options, isNew) {
|
|
2540
|
+
if (options.verbose) {
|
|
2541
|
+
console.info(`[SyncService] Would ${isNew ? "create" : "update"} object: ${nativeObject.name}`);
|
|
2542
|
+
}
|
|
2543
|
+
for (const attr of nativeObject.attributes) {
|
|
2544
|
+
const existingAttr = existingObject ? await adapter.attributes.findByObjectId(existingObject.id).then((attrs) => attrs.find((a) => a.name === attr.name)) : null;
|
|
2545
|
+
if (existingAttr) {
|
|
2546
|
+
result.attributesUpdated++;
|
|
2547
|
+
} else {
|
|
2548
|
+
result.attributesCreated++;
|
|
2549
|
+
}
|
|
2550
|
+
result.attributesSynced++;
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
async function upsertObject(adapter, nativeObject, options) {
|
|
2554
|
+
return await adapter.objects.upsert({
|
|
2555
|
+
tenantId: options.tenantId || "default",
|
|
2556
|
+
system: true,
|
|
2557
|
+
name: nativeObject.name,
|
|
2558
|
+
label: nativeObject.label,
|
|
2559
|
+
description: nativeObject.description,
|
|
2560
|
+
metadata: nativeObject.metadata
|
|
2561
|
+
});
|
|
2562
|
+
}
|
|
2563
|
+
async function syncAttributes(adapter, nativeObject, dbObject, existingObject, result) {
|
|
2564
|
+
for (const [index, attr] of nativeObject.attributes.entries()) {
|
|
2565
|
+
const existingAttr = existingObject ? await adapter.attributes.findByObjectId(dbObject.id).then((attrs) => attrs.find((a) => a.name === attr.name)) : null;
|
|
2566
|
+
await adapter.attributes.upsert({
|
|
2567
|
+
objectId: dbObject.id,
|
|
2568
|
+
name: attr.name,
|
|
2569
|
+
label: attr.label,
|
|
2570
|
+
type: attr.type,
|
|
2571
|
+
description: attr.description,
|
|
2572
|
+
icon: attr.icon,
|
|
2573
|
+
system: attr.system ?? true,
|
|
2574
|
+
required: attr.required ?? false,
|
|
2575
|
+
order: attr.order ?? index,
|
|
2576
|
+
hidden: attr.hidden,
|
|
2577
|
+
archived: attr.archived,
|
|
2578
|
+
deprecated: attr.deprecated,
|
|
2579
|
+
defaultValue: attr.defaultValue,
|
|
2580
|
+
config: extractAttributeConfig(attr)
|
|
2581
|
+
});
|
|
2582
|
+
if (existingAttr) {
|
|
2583
|
+
result.attributesUpdated++;
|
|
2584
|
+
} else {
|
|
2585
|
+
result.attributesCreated++;
|
|
2586
|
+
}
|
|
2587
|
+
result.attributesSynced++;
|
|
2588
|
+
}
|
|
2589
|
+
}
|
|
2590
|
+
async function cleanupAttributes(adapter, objectId, nativeObject, result) {
|
|
2591
|
+
const attributeNames = nativeObject.attributes.map((attr) => attr.name);
|
|
2592
|
+
const deletedCount = await adapter.attributes.deleteByNames(objectId, attributeNames);
|
|
2593
|
+
result.attributesDeleted += deletedCount;
|
|
2594
|
+
}
|
|
2595
|
+
function logSyncSuccess(nativeObject, options) {
|
|
2596
|
+
if (options.verbose) {
|
|
2597
|
+
console.info(
|
|
2598
|
+
`[SyncService] \u2713 Synced ${nativeObject.name}: ${nativeObject.attributes.length} attributes`
|
|
2599
|
+
);
|
|
2600
|
+
}
|
|
2601
|
+
}
|
|
2602
|
+
async function verifyNativeObjectsSync(adapter, nativeRegistry) {
|
|
2603
|
+
const nativeObjects = nativeRegistry.getAll();
|
|
2604
|
+
for (const obj of nativeObjects) {
|
|
2605
|
+
if (!obj.system) continue;
|
|
2606
|
+
const existing = await adapter.objects.findSystemByName(obj.name);
|
|
2607
|
+
if (!existing) {
|
|
2608
|
+
return false;
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
2611
|
+
return true;
|
|
2612
|
+
}
|
|
2613
|
+
async function getSyncPreview(adapter, nativeRegistry) {
|
|
2614
|
+
return await syncNativeObjects(adapter, nativeRegistry, { dryRun: true });
|
|
2615
|
+
}
|
|
2616
|
+
|
|
2617
|
+
export {
|
|
2618
|
+
generateId,
|
|
2619
|
+
generatePrefixedId,
|
|
2620
|
+
text,
|
|
2621
|
+
textarea,
|
|
2622
|
+
number,
|
|
2623
|
+
checkbox,
|
|
2624
|
+
date,
|
|
2625
|
+
phone,
|
|
2626
|
+
currency,
|
|
2627
|
+
status,
|
|
2628
|
+
select,
|
|
2629
|
+
multiselect,
|
|
2630
|
+
location,
|
|
2631
|
+
timestamp,
|
|
2632
|
+
file,
|
|
2633
|
+
user,
|
|
2634
|
+
relation,
|
|
2635
|
+
rating,
|
|
2636
|
+
ObjectBuilder,
|
|
2637
|
+
object,
|
|
2638
|
+
registry,
|
|
2639
|
+
createTextValidator,
|
|
2640
|
+
createNumberValidator,
|
|
2641
|
+
createCheckboxValidator,
|
|
2642
|
+
createDateValidator,
|
|
2643
|
+
createPhoneValidator,
|
|
2644
|
+
createCurrencyValidator,
|
|
2645
|
+
createStatusValidator,
|
|
2646
|
+
createSelectValidator,
|
|
2647
|
+
createMultiselectValidator,
|
|
2648
|
+
createLocationValidator,
|
|
2649
|
+
createTimestampValidator,
|
|
2650
|
+
createFileValidator,
|
|
2651
|
+
createUserValidator,
|
|
2652
|
+
createRelationValidator,
|
|
2653
|
+
createRatingValidator,
|
|
2654
|
+
createAttributeValidator,
|
|
2655
|
+
createObjectValidator,
|
|
2656
|
+
validateAttribute,
|
|
2657
|
+
validateObject,
|
|
2658
|
+
validateObjectOrThrow,
|
|
2659
|
+
createDraftValidator,
|
|
2660
|
+
validateDraft,
|
|
2661
|
+
validateDraftOrThrow,
|
|
2662
|
+
getMissingRequiredAttributes,
|
|
2663
|
+
isRecordComplete,
|
|
2664
|
+
computeRecordStatus,
|
|
2665
|
+
createMockAdapter,
|
|
2666
|
+
FileService,
|
|
2667
|
+
ObjectSchemaService,
|
|
2668
|
+
RecordService,
|
|
2669
|
+
UserProfileService,
|
|
2670
|
+
syncNativeObjects,
|
|
2671
|
+
verifyNativeObjectsSync,
|
|
2672
|
+
getSyncPreview
|
|
2673
|
+
};
|