@docstack/client 0.0.1

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.
Files changed (77) hide show
  1. package/README.md +214 -0
  2. package/lib/core/attribute.d.ts +174 -0
  3. package/lib/core/attribute.js +296 -0
  4. package/lib/core/attribute.js.map +1 -0
  5. package/lib/core/class.d.ts +340 -0
  6. package/lib/core/class.js +540 -0
  7. package/lib/core/class.js.map +1 -0
  8. package/lib/core/crypto-engine/index.d.ts +121 -0
  9. package/lib/core/crypto-engine/index.js +130 -0
  10. package/lib/core/crypto-engine/index.js.map +1 -0
  11. package/lib/core/crypto-engine/utils.d.ts +20 -0
  12. package/lib/core/crypto-engine/utils.js +76 -0
  13. package/lib/core/crypto-engine/utils.js.map +1 -0
  14. package/lib/core/datamodel/index.d.ts +3 -0
  15. package/lib/core/datamodel/index.js +1186 -0
  16. package/lib/core/datamodel/index.js.map +1 -0
  17. package/lib/core/domain.d.ts +200 -0
  18. package/lib/core/domain.js +285 -0
  19. package/lib/core/domain.js.map +1 -0
  20. package/lib/core/index.d.ts +176 -0
  21. package/lib/core/index.js +379 -0
  22. package/lib/core/index.js.map +1 -0
  23. package/lib/core/job-engine/index.d.ts +110 -0
  24. package/lib/core/job-engine/index.js +116 -0
  25. package/lib/core/job-engine/index.js.map +1 -0
  26. package/lib/core/policy-engine/index.d.ts +97 -0
  27. package/lib/core/policy-engine/index.js +150 -0
  28. package/lib/core/policy-engine/index.js.map +1 -0
  29. package/lib/core/query-engine/accumulators.d.ts +9 -0
  30. package/lib/core/query-engine/accumulators.js +258 -0
  31. package/lib/core/query-engine/accumulators.js.map +1 -0
  32. package/lib/core/query-engine/evaluator.d.ts +37 -0
  33. package/lib/core/query-engine/evaluator.js +179 -0
  34. package/lib/core/query-engine/evaluator.js.map +1 -0
  35. package/lib/core/query-engine/executor.d.ts +14 -0
  36. package/lib/core/query-engine/executor.js +405 -0
  37. package/lib/core/query-engine/executor.js.map +1 -0
  38. package/lib/core/query-engine/index.d.ts +4 -0
  39. package/lib/core/query-engine/index.js +4 -0
  40. package/lib/core/query-engine/index.js.map +1 -0
  41. package/lib/core/query-engine/parser.d.ts +10 -0
  42. package/lib/core/query-engine/parser.js +515 -0
  43. package/lib/core/query-engine/parser.js.map +1 -0
  44. package/lib/core/query-engine/planner.d.ts +27 -0
  45. package/lib/core/query-engine/planner.js +330 -0
  46. package/lib/core/query-engine/planner.js.map +1 -0
  47. package/lib/core/stack.d.ts +497 -0
  48. package/lib/core/stack.js +1507 -0
  49. package/lib/core/stack.js.map +1 -0
  50. package/lib/core/test-utils/docstack.d.ts +29 -0
  51. package/lib/core/test-utils/docstack.js +222 -0
  52. package/lib/core/test-utils/docstack.js.map +1 -0
  53. package/lib/core/trigger/index.d.ts +31 -0
  54. package/lib/core/trigger/index.js +81 -0
  55. package/lib/core/trigger/index.js.map +1 -0
  56. package/lib/index.d.ts +4 -0
  57. package/lib/index.js +8237 -0
  58. package/lib/index.js.map +1 -0
  59. package/lib/plugins/pouchdb.d.ts +9 -0
  60. package/lib/plugins/pouchdb.js +403 -0
  61. package/lib/plugins/pouchdb.js.map +1 -0
  62. package/lib/utils/crypto/index.d.ts +3 -0
  63. package/lib/utils/crypto/index.js +34 -0
  64. package/lib/utils/crypto/index.js.map +1 -0
  65. package/lib/utils/index.d.ts +4 -0
  66. package/lib/utils/index.js +58 -0
  67. package/lib/utils/index.js.map +1 -0
  68. package/lib/utils/logger/index.d.ts +4 -0
  69. package/lib/utils/logger/index.js +20 -0
  70. package/lib/utils/logger/index.js.map +1 -0
  71. package/lib/utils/logger/transport.d.ts +11 -0
  72. package/lib/utils/logger/transport.js +28 -0
  73. package/lib/utils/logger/transport.js.map +1 -0
  74. package/lib/workers/dataModel.d.ts +1 -0
  75. package/lib/workers/dataModel.js +48 -0
  76. package/lib/workers/dataModel.js.map +1 -0
  77. package/package.json +60 -0
@@ -0,0 +1,1186 @@
1
+ import semver from "semver";
2
+ const syspatches = [];
3
+ const hashContent = async (content) => {
4
+ const data = new TextEncoder().encode(content);
5
+ const hashBuffer = await globalThis.crypto.subtle.digest('SHA-256', data);
6
+ return Array.from(new Uint8Array(hashBuffer)).map(b => b.toString(16).padStart(2, '0')).join('');
7
+ };
8
+ // TODO: implement _rev auto handler:
9
+ // If patches include updates to existing documents,
10
+ // we need to ensure that the _rev field is handled correctly to avoid conflicts.
11
+ const sys_001 = {
12
+ "_id": "~sys-0.0.1",
13
+ "~class": "patch",
14
+ "version": "0.0.1",
15
+ "target": "system",
16
+ "changelog": "### Initial patch with originators: Class and Domain",
17
+ "docs": [
18
+ {
19
+ "_id": "class",
20
+ "active": true,
21
+ "name": "class",
22
+ "description": "A class document representing a data model class",
23
+ "~class": "~self",
24
+ "schema": {
25
+ "name": {
26
+ "name": "name",
27
+ "type": "string",
28
+ "config": {
29
+ "primaryKey": true,
30
+ "mandatory": true,
31
+ "isArray": false,
32
+ "maxLength": 100
33
+ }
34
+ },
35
+ "description": {
36
+ "name": "description",
37
+ "type": "string",
38
+ "config": {
39
+ "mandatory": false,
40
+ "isArray": false,
41
+ "maxLength": 500
42
+ }
43
+ },
44
+ "~class": {
45
+ "name": "~class",
46
+ "type": "string",
47
+ "config": {
48
+ "mandatory": true,
49
+ "isArray": false,
50
+ "maxLength": 50,
51
+ "defaultValue": "class"
52
+ }
53
+ },
54
+ "schema": { "name": "schema", "type": "object", "config": { "maxLength": 1000, "isArray": false } },
55
+ }
56
+ },
57
+ {
58
+ "_id": "domain",
59
+ "active": true,
60
+ "name": "domain",
61
+ "description": "A domain document representing a data model domain",
62
+ "~class": "~self",
63
+ "schema": {
64
+ "~class": { name: "~class", type: "string", config: { defaultValue: "domain" } },
65
+ "relation": {
66
+ name: "relation", type: "enum", config: {
67
+ isArray: false, values: [
68
+ { value: "1:1" }, { value: "1:N" }, { value: "N:1" }, { value: "N:N" }
69
+ ]
70
+ }
71
+ },
72
+ "sourceClass": {
73
+ name: "sourceClass",
74
+ type: "foreign_key",
75
+ config: {
76
+ targetClass: "class",
77
+ isArray: false
78
+ }
79
+ },
80
+ "targetClass": {
81
+ name: "targetClass",
82
+ type: "foreign_key",
83
+ config: {
84
+ targetClass: "class",
85
+ isArray: false
86
+ }
87
+ },
88
+ }
89
+ }
90
+ ]
91
+ };
92
+ // const sys_002: Patch = {
93
+ // "_id": "~sys-0.0.2",
94
+ // "~class": "patch",
95
+ // "version": "0.0.2",
96
+ // "target": "system",
97
+ // "changelog": "### Initial patch with system classes and domains",
98
+ // "docs": [
99
+ // {
100
+ // "_id": "log",
101
+ // "active": true,
102
+ // "name": "log",
103
+ // "description": "Contains the system logs",
104
+ // "~class": "class",
105
+ // "schema": {
106
+ // "log": {
107
+ // "name": "log",
108
+ // "type": "object",
109
+ // "config": {
110
+ // "isArray": false
111
+ // }
112
+ // }
113
+ // }
114
+ // }
115
+ // ]
116
+ // }
117
+ const sys_003 = {
118
+ "_id": "~sys-0.0.3",
119
+ "~class": "patch",
120
+ "version": "0.0.3",
121
+ "target": "system",
122
+ "changelog": "### Schema Patch: v0.0.3\\\n#### New Classes: ~User, ~UserSession",
123
+ "docs": [
124
+ {
125
+ "_id": "~User",
126
+ "active": true,
127
+ "name": "User",
128
+ "description": "A user class for secure login",
129
+ "~class": "class",
130
+ "schema": {
131
+ "username": {
132
+ "name": "username",
133
+ "type": "string",
134
+ "config": {
135
+ "primaryKey": true,
136
+ "maxLength": 50,
137
+ "mandatory": true,
138
+ "isArray": false
139
+ }
140
+ },
141
+ "password": {
142
+ "name": "password",
143
+ "type": "string",
144
+ "config": {
145
+ "maxLength": 50,
146
+ "mandatory": true,
147
+ "isArray": false
148
+ }
149
+ },
150
+ "email": {
151
+ "name": "email",
152
+ "type": "string",
153
+ "config": {
154
+ "maxLength": 50,
155
+ "isArray": false
156
+ }
157
+ },
158
+ "firstName": {
159
+ "name": "firstName",
160
+ "type": "string",
161
+ "config": {
162
+ "maxLength": 50,
163
+ "isArray": false
164
+ }
165
+ },
166
+ "lastName": {
167
+ "name": "lastName",
168
+ "type": "string",
169
+ "config": {
170
+ "maxLength": 50,
171
+ "isArray": false
172
+ }
173
+ }
174
+ }
175
+ },
176
+ {
177
+ "_id": "~UserSession",
178
+ "name": "UserSession",
179
+ "active": true,
180
+ "description": "Tracks user sessions",
181
+ "~class": "class",
182
+ "schema": {
183
+ "username": {
184
+ "name": "username",
185
+ "type": "string",
186
+ "config": {
187
+ "maxLength": 50,
188
+ "isArray": false,
189
+ "primaryKey": true,
190
+ "mandatory": true
191
+ }
192
+ },
193
+ "sessionId": {
194
+ "name": "sessionId",
195
+ "type": "string",
196
+ "config": {
197
+ "maxLength": 200,
198
+ "primaryKey": true,
199
+ "isArray": false,
200
+ "mandatory": true
201
+ }
202
+ },
203
+ "sessionStart": {
204
+ "name": "sessionStart",
205
+ "type": "string",
206
+ "config": {
207
+ "maxLength": 100,
208
+ "isArray": false,
209
+ "mandatory": true
210
+ }
211
+ },
212
+ "sessionStatus": {
213
+ "name": "sessionStatus",
214
+ "type": "string",
215
+ "config": {
216
+ "maxLength": 100,
217
+ "isArray": false,
218
+ "mandatory": true
219
+ }
220
+ },
221
+ "sessionEnd": {
222
+ "name": "sessionEnd",
223
+ "type": "string",
224
+ "config": {
225
+ "maxLength": 100,
226
+ "isArray": false,
227
+ "mandatory": false
228
+ }
229
+ }
230
+ }
231
+ }
232
+ ]
233
+ };
234
+ const classicAuthJobContent = `
235
+ const toHex = (buffer) => Array.from(new Uint8Array(buffer)).map((b) => b.toString(16).padStart(2, "0")).join("");
236
+
237
+ async function deriveKeyMaterial(password, salt, iterations = 120000, length = 32) {
238
+ if (!globalThis.crypto || !globalThis.crypto.subtle) {
239
+ throw new Error("WebCrypto unavailable for key derivation");
240
+ }
241
+
242
+ const encoder = new TextEncoder();
243
+ const keyMaterial = await globalThis.crypto.subtle.importKey(
244
+ "raw",
245
+ encoder.encode(String(password)),
246
+ { name: "PBKDF2" },
247
+ false,
248
+ ["deriveBits"]
249
+ );
250
+
251
+ const derived = await globalThis.crypto.subtle.deriveBits(
252
+ {
253
+ name: "PBKDF2",
254
+ salt: encoder.encode(String(salt)),
255
+ iterations,
256
+ hash: "SHA-256",
257
+ },
258
+ keyMaterial,
259
+ length * 8
260
+ );
261
+
262
+ return toHex(derived);
263
+ }
264
+
265
+ async function execute(_stack, params) {
266
+ const password = params?.password ?? "";
267
+ const salt = params?.salt ?? params?.keyDerivationSalt ?? "";
268
+
269
+ if (!password) {
270
+ throw new Error("Password is required for key derivation");
271
+ }
272
+ if (!salt) {
273
+ throw new Error("Salt is required for key derivation");
274
+ }
275
+
276
+ const derivedKey = await deriveKeyMaterial(password, salt);
277
+ const metadata = { derivedKey, derivedAt: Date.now() };
278
+ return { metadata };
279
+ }
280
+ `;
281
+ const sys_004 = {
282
+ "_id": "~sys-0.0.4",
283
+ "~class": "patch",
284
+ "version": "0.0.4",
285
+ "target": "system",
286
+ "changelog": "### Schema Patch: v0.0.4\\n#### New Classes: ~Job, ~JobRun, ~Policy, ~AuthModule\\n#### Updates: ~User authentication fields",
287
+ "docs": [
288
+ {
289
+ "_id": "~User",
290
+ "_rev": "auto",
291
+ "active": true,
292
+ "name": "User",
293
+ "description": "A user class for secure login",
294
+ "~class": "class",
295
+ "schema": {
296
+ "username": {
297
+ "name": "username",
298
+ "type": "string",
299
+ "config": {
300
+ "primaryKey": true,
301
+ "maxLength": 50,
302
+ "mandatory": true,
303
+ "isArray": false
304
+ }
305
+ },
306
+ "password": {
307
+ "name": "password",
308
+ "type": "string",
309
+ "config": {
310
+ "maxLength": 50,
311
+ "mandatory": true,
312
+ "isArray": false
313
+ }
314
+ },
315
+ "email": {
316
+ "name": "email",
317
+ "type": "string",
318
+ "config": {
319
+ "maxLength": 50,
320
+ "isArray": false
321
+ }
322
+ },
323
+ "firstName": {
324
+ "name": "firstName",
325
+ "type": "string",
326
+ "config": {
327
+ "maxLength": 50,
328
+ "isArray": false
329
+ }
330
+ },
331
+ "lastName": {
332
+ "name": "lastName",
333
+ "type": "string",
334
+ "config": {
335
+ "maxLength": 50,
336
+ "isArray": false
337
+ }
338
+ },
339
+ "authMethod": {
340
+ "name": "authMethod",
341
+ "type": "string",
342
+ "config": {
343
+ "mandatory": true,
344
+ "maxLength": 100,
345
+ "isArray": false
346
+ }
347
+ },
348
+ "externalId": {
349
+ "name": "externalId",
350
+ "type": "string",
351
+ "config": {
352
+ "maxLength": 200,
353
+ "isArray": false
354
+ }
355
+ },
356
+ "keyDerivationSalt": {
357
+ "name": "keyDerivationSalt",
358
+ "type": "string",
359
+ "config": {
360
+ "mandatory": true,
361
+ "maxLength": 200,
362
+ "isArray": false
363
+ }
364
+ }
365
+ }
366
+ },
367
+ {
368
+ "_id": "~Job",
369
+ "active": true,
370
+ "name": "Job",
371
+ "description": "A long running or scheduled job",
372
+ "~class": "class",
373
+ "schema": {
374
+ "_id": { "name": "_id", "type": "string", "config": { "primaryKey": true, "mandatory": true } },
375
+ "name": { "name": "name", "type": "string", "config": { "mandatory": true } },
376
+ "description": { "name": "description", "type": "string", "config": { "isArray": false } },
377
+ "type": { "name": "type", "type": "string", "config": { "mandatory": true } },
378
+ "workerPlatform": { "name": "workerPlatform", "type": "string", "config": { "mandatory": true } },
379
+ "content": { "name": "content", "type": "string", "config": { "mandatory": true } },
380
+ "hash": { "name": "hash", "type": "string", "config": { "mandatory": true } },
381
+ "schedule": { "name": "schedule", "type": "string", "config": { "mandatory": false } },
382
+ "isSingleton": { "name": "isSingleton", "type": "boolean", "config": { "defaultValue": false } },
383
+ "isEnabled": { "name": "isEnabled", "type": "boolean", "config": { "mandatory": true, "defaultValue": true } },
384
+ "nextRunTimestamp": { "name": "nextRunTimestamp", "type": "integer", "config": { "mandatory": false } },
385
+ "defaultParams": { "name": "defaultParams", "type": "object", "config": { "mandatory": false } },
386
+ "metadata": { "name": "metadata", "type": "object", "config": { "mandatory": false } }
387
+ }
388
+ },
389
+ {
390
+ "_id": "~JobRun",
391
+ "active": true,
392
+ "name": "JobRun",
393
+ "description": "Execution history for jobs",
394
+ "~class": "class",
395
+ "schema": {
396
+ "_id": { "name": "_id", "type": "string", "config": { "primaryKey": true, "mandatory": true } },
397
+ "~class": { "name": "~class", "type": "string", "config": { "mandatory": true, "defaultValue": "~JobRun" } },
398
+ "jobId": { "name": "jobId", "type": "string", "config": { "mandatory": true } },
399
+ "status": { "name": "status", "type": "string", "config": { "mandatory": true } },
400
+ "triggerType": { "name": "triggerType", "type": "string", "config": { "mandatory": true } },
401
+ "startTime": { "name": "startTime", "type": "integer", "config": { "mandatory": true } },
402
+ "endTime": { "name": "endTime", "type": "integer", "config": { "mandatory": false } },
403
+ "durationMs": { "name": "durationMs", "type": "integer", "config": { "mandatory": false } },
404
+ "runtimeArgs": { "name": "runtimeArgs", "type": "object", "config": { "mandatory": false } },
405
+ "initialMetadata": { "name": "initialMetadata", "type": "object", "config": { "mandatory": false } },
406
+ "finalMetadata": { "name": "finalMetadata", "type": "object", "config": { "mandatory": false } },
407
+ "errorMessage": { "name": "errorMessage", "type": "string", "config": { "mandatory": false } },
408
+ "errorStack": { "name": "errorStack", "type": "string", "config": { "mandatory": false } },
409
+ "logs": { "name": "logs", "type": "object", "config": { "mandatory": false } },
410
+ "workerId": { "name": "workerId", "type": "string", "config": { "mandatory": false } }
411
+ }
412
+ },
413
+ {
414
+ "_id": "~Policy",
415
+ "active": true,
416
+ "name": "Policy",
417
+ "description": "Access policies for classes and documents",
418
+ "~class": "class",
419
+ "schema": {
420
+ "userId": { "name": "userId", "type": "string", "config": { "mandatory": true } },
421
+ "rule": { "name": "rule", "type": "string", "config": { "mandatory": true, "maxLength": 2000 } },
422
+ "description": { "name": "description", "type": "string", "config": { "mandatory": false } },
423
+ "targetClass": { "name": "targetClass", "type": "foreign_key", "config": { "mandatory": true, "isArray": true, "targetClass": "class" } }
424
+ }
425
+ },
426
+ {
427
+ "_id": "~AuthModule",
428
+ "active": true,
429
+ "name": "AuthModule",
430
+ "description": "Authentication module descriptor",
431
+ "~class": "class",
432
+ "schema": {
433
+ "name": { "name": "name", "type": "string", "config": { "mandatory": true } },
434
+ "config": { "name": "config", "type": "object", "config": { "mandatory": false } },
435
+ "jobId": { "name": "jobId", "type": "foreign_key", "config": { "mandatory": true, "targetClass": "~Job" } }
436
+ }
437
+ },
438
+ ]
439
+ };
440
+ const sys_005 = {
441
+ "_id": "~sys-0.0.5",
442
+ "~class": "patch",
443
+ "version": "0.0.5",
444
+ "target": "system",
445
+ "changelog": "### Schema Patch: v0.0.5\\n#### New Documents: Job-Auth-Classic, AuthMod-Classic, Policy-System-Classes",
446
+ "docs": [
447
+ {
448
+ "_id": "Job-Auth-Classic",
449
+ "~class": "~Job",
450
+ "active": true,
451
+ "name": "Classic authentication",
452
+ "description": "Derive key material from password and salt",
453
+ "type": "user",
454
+ "workerPlatform": "client",
455
+ "content": classicAuthJobContent,
456
+ "hash": "e63887145113429477843420c388d35116e255403148163f4c4a436f0b8338b1", // Pre-calculated hash
457
+ "isEnabled": true,
458
+ "isSingleton": true,
459
+ "defaultParams": {},
460
+ "metadata": {}
461
+ },
462
+ ]
463
+ };
464
+ const sys_006 = {
465
+ "_id": "~sys-00.6",
466
+ "~class": "patch",
467
+ "version": "0.0.6",
468
+ "target": "system",
469
+ "changelog": "### Schema Patch: v0.0.6\\n#### New Documents: AuthMod-Classic, Policy-System-Classes",
470
+ "docs": [
471
+ {
472
+ "_id": "AuthMod-Classic",
473
+ "~class": "~AuthModule",
474
+ "active": true,
475
+ "name": "Classic Email/Password",
476
+ "config": { "mode": "password" },
477
+ "jobId": "Job-Auth-Classic"
478
+ },
479
+ {
480
+ "_id": "Policy-System-Classes",
481
+ "~class": "~Policy",
482
+ "active": true,
483
+ "userId": "system",
484
+ "rule": "return true;",
485
+ "description": "Default system policy",
486
+ "targetClass": ["class", "~User", "~UserSession", "~Policy", "~Job", "~JobRun", "~AuthModule"]
487
+ }
488
+ ]
489
+ };
490
+ const sys_007 = {
491
+ "_id": "~sys-0.0.7",
492
+ "~class": "patch",
493
+ "version": "0.0.7",
494
+ "target": "system",
495
+ "changelog": "### Schema Patch: v0.0.7\\n#### Updates: enforce enum/foreign key definitions for job, policy, and user auth fields",
496
+ "docs": [
497
+ {
498
+ "_id": "~User",
499
+ "_rev": "auto",
500
+ "active": true,
501
+ "name": "User",
502
+ "description": "A user class for secure login",
503
+ "~class": "class",
504
+ "schema": {
505
+ "username": {
506
+ "name": "username",
507
+ "type": "string",
508
+ "config": {
509
+ "primaryKey": true,
510
+ "maxLength": 50,
511
+ "mandatory": true,
512
+ "isArray": false
513
+ }
514
+ },
515
+ "password": {
516
+ "name": "password",
517
+ "type": "string",
518
+ "config": {
519
+ "maxLength": 50,
520
+ "mandatory": true,
521
+ "isArray": false
522
+ }
523
+ },
524
+ "email": {
525
+ "name": "email",
526
+ "type": "string",
527
+ "config": {
528
+ "maxLength": 50,
529
+ "isArray": false
530
+ }
531
+ },
532
+ "firstName": {
533
+ "name": "firstName",
534
+ "type": "string",
535
+ "config": {
536
+ "maxLength": 50,
537
+ "isArray": false
538
+ }
539
+ },
540
+ "lastName": {
541
+ "name": "lastName",
542
+ "type": "string",
543
+ "config": {
544
+ "maxLength": 50,
545
+ "isArray": false
546
+ }
547
+ },
548
+ "authMethod": {
549
+ "name": "authMethod",
550
+ "type": "foreign_key",
551
+ "config": { "mandatory": true, "targetClass": "~AuthModule" }
552
+ },
553
+ "externalId": {
554
+ "name": "externalId",
555
+ "type": "string",
556
+ "config": {
557
+ "maxLength": 200,
558
+ "isArray": false
559
+ }
560
+ },
561
+ "keyDerivationSalt": {
562
+ "name": "keyDerivationSalt",
563
+ "type": "string",
564
+ "config": {
565
+ "mandatory": true,
566
+ "maxLength": 200,
567
+ "isArray": false
568
+ }
569
+ }
570
+ }
571
+ },
572
+ {
573
+ "_id": "~Job",
574
+ "_rev": "auto",
575
+ "active": true,
576
+ "name": "Job",
577
+ "description": "A long running or scheduled job",
578
+ "~class": "class",
579
+ "schema": {
580
+ "_id": { "name": "_id", "type": "string", "config": { "primaryKey": true, "mandatory": true } },
581
+ "name": { "name": "name", "type": "string", "config": { "mandatory": true } },
582
+ "description": { "name": "description", "type": "string", "config": { "isArray": false } },
583
+ "type": {
584
+ "name": "type",
585
+ "type": "enum",
586
+ "config": { "mandatory": true, "values": [{ value: "system" }, { value: "user" }] }
587
+ },
588
+ "workerPlatform": {
589
+ "name": "workerPlatform",
590
+ "type": "enum",
591
+ "config": { "mandatory": true, "values": [{ value: "client" }, { value: "server" }, { value: "hybrid" }] }
592
+ },
593
+ "content": { "name": "content", "type": "string", "config": { "mandatory": true } },
594
+ "hash": { "name": "hash", "type": "string", "config": { "mandatory": true } },
595
+ "schedule": { "name": "schedule", "type": "string", "config": { "mandatory": false } },
596
+ "isSingleton": { "name": "isSingleton", "type": "boolean", "config": { "defaultValue": false } },
597
+ "isEnabled": { "name": "isEnabled", "type": "boolean", "config": { "mandatory": true, "defaultValue": true } },
598
+ "nextRunTimestamp": { "name": "nextRunTimestamp", "type": "integer", "config": { "mandatory": false } },
599
+ "defaultParams": { "name": "defaultParams", "type": "object", "config": { "mandatory": false } },
600
+ "metadata": { "name": "metadata", "type": "object", "config": { "mandatory": false } }
601
+ }
602
+ },
603
+ {
604
+ "_id": "~JobRun",
605
+ "_rev": "auto",
606
+ "active": true,
607
+ "name": "JobRun",
608
+ "description": "Execution history for jobs",
609
+ "~class": "class",
610
+ "schema": {
611
+ "_id": { "name": "_id", "type": "string", "config": { "primaryKey": true, "mandatory": true } },
612
+ "~class": { "name": "~class", "type": "string", "config": { "mandatory": true, "defaultValue": "~JobRun" } },
613
+ "jobId": { "name": "jobId", "type": "foreign_key", "config": { "mandatory": true, "targetClass": "~Job" } },
614
+ "status": {
615
+ "name": "status",
616
+ "type": "enum",
617
+ "config": {
618
+ "mandatory": true,
619
+ "values": [
620
+ { value: "PENDING" },
621
+ { value: "RUNNING" },
622
+ { value: "SUCCESS" },
623
+ { value: "FAILURE" },
624
+ { value: "CANCELED" },
625
+ { value: "SKIPPED" }
626
+ ]
627
+ }
628
+ },
629
+ "triggerType": {
630
+ "name": "triggerType",
631
+ "type": "enum",
632
+ "config": { "mandatory": true, "values": [{ value: "manual" }, { value: "scheduled" }, { value: "event" }] }
633
+ },
634
+ "startTime": { "name": "startTime", "type": "integer", "config": { "mandatory": true } },
635
+ "endTime": { "name": "endTime", "type": "integer", "config": { "mandatory": false } },
636
+ "durationMs": { "name": "durationMs", "type": "integer", "config": { "mandatory": false } },
637
+ "runtimeArgs": { "name": "runtimeArgs", "type": "object", "config": { "mandatory": false } },
638
+ "initialMetadata": { "name": "initialMetadata", "type": "object", "config": { "mandatory": false } },
639
+ "finalMetadata": { "name": "finalMetadata", "type": "object", "config": { "mandatory": false } },
640
+ "errorMessage": { "name": "errorMessage", "type": "string", "config": { "mandatory": false } },
641
+ "errorStack": { "name": "errorStack", "type": "string", "config": { "mandatory": false } },
642
+ "logs": { "name": "logs", "type": "object", "config": { "mandatory": false } },
643
+ "workerId": { "name": "workerId", "type": "string", "config": { "mandatory": false } }
644
+ }
645
+ },
646
+ {
647
+ "_id": "~Policy",
648
+ "_rev": "auto",
649
+ "active": true,
650
+ "name": "Policy",
651
+ "description": "Access policies for classes and documents",
652
+ "~class": "class",
653
+ "schema": {
654
+ "userId": { "name": "userId", "type": "foreign_key", "config": { "mandatory": true, "targetClass": "~User" } },
655
+ "rule": { "name": "rule", "type": "string", "config": { "mandatory": true, "maxLength": 2000 } },
656
+ "description": { "name": "description", "type": "string", "config": { "mandatory": false } },
657
+ "targetClass": { "name": "targetClass", "type": "foreign_key", "config": { "mandatory": true, "isArray": true, "targetClass": "class" } }
658
+ }
659
+ },
660
+ {
661
+ "_id": "system",
662
+ "~class": "~User",
663
+ "active": true,
664
+ "username": "system",
665
+ "password": "system",
666
+ "email": "",
667
+ "firstName": "System",
668
+ "lastName": "User",
669
+ "authMethod": "AuthMod-Classic",
670
+ "externalId": "",
671
+ "keyDerivationSalt": "system-salt"
672
+ }
673
+ ]
674
+ };
675
+ const sys_008 = {
676
+ "_id": "~sys-0.0.8",
677
+ "~class": "patch",
678
+ "version": "0.0.8",
679
+ "target": "system",
680
+ "changelog": "### Schema Patch: v0.0.8\\n#### Updates: support wrapped document keys and encrypted user secrets",
681
+ "docs": [
682
+ {
683
+ "_id": "~User",
684
+ "_rev": "auto",
685
+ "active": true,
686
+ "name": "User",
687
+ "description": "A user class for secure login",
688
+ "~class": "class",
689
+ "schema": {
690
+ "username": {
691
+ "name": "username",
692
+ "type": "string",
693
+ "config": {
694
+ "primaryKey": true,
695
+ "maxLength": 50,
696
+ "mandatory": true,
697
+ "isArray": false
698
+ }
699
+ },
700
+ "password": {
701
+ "name": "password",
702
+ "type": "string",
703
+ "config": {
704
+ "maxLength": 50,
705
+ "mandatory": true,
706
+ "isArray": false,
707
+ "encrypted": true
708
+ }
709
+ },
710
+ "email": {
711
+ "name": "email",
712
+ "type": "string",
713
+ "config": {
714
+ "maxLength": 50,
715
+ "isArray": false
716
+ }
717
+ },
718
+ "firstName": {
719
+ "name": "firstName",
720
+ "type": "string",
721
+ "config": {
722
+ "maxLength": 50,
723
+ "isArray": false
724
+ }
725
+ },
726
+ "lastName": {
727
+ "name": "lastName",
728
+ "type": "string",
729
+ "config": {
730
+ "maxLength": 50,
731
+ "isArray": false
732
+ }
733
+ },
734
+ "authMethod": {
735
+ "name": "authMethod",
736
+ "type": "foreign_key",
737
+ "config": { "mandatory": true, "targetClass": "~AuthModule" }
738
+ },
739
+ "externalId": {
740
+ "name": "externalId",
741
+ "type": "string",
742
+ "config": {
743
+ "maxLength": 200,
744
+ "isArray": false
745
+ }
746
+ },
747
+ "keyDerivationSalt": {
748
+ "name": "keyDerivationSalt",
749
+ "type": "string",
750
+ "config": {
751
+ "mandatory": true,
752
+ "maxLength": 200,
753
+ "isArray": false
754
+ }
755
+ },
756
+ "wrappedDocumentKey": {
757
+ "name": "wrappedDocumentKey",
758
+ "type": "string",
759
+ "config": {
760
+ "mandatory": false,
761
+ "maxLength": 4096,
762
+ "isArray": false,
763
+ "encrypted": true
764
+ }
765
+ }
766
+ }
767
+ },
768
+ {
769
+ "_id": "system",
770
+ "_rev": "auto",
771
+ "~class": "~User",
772
+ "active": true,
773
+ "username": "system",
774
+ "password": "system",
775
+ "email": "",
776
+ "firstName": "System",
777
+ "lastName": "User",
778
+ "authMethod": "AuthMod-Classic",
779
+ "externalId": "",
780
+ "keyDerivationSalt": "system-salt",
781
+ "wrappedDocumentKey": ""
782
+ }
783
+ ]
784
+ };
785
+ const sys_009 = {
786
+ "_id": "~sys-0.0.9",
787
+ "~class": "patch",
788
+ "version": "0.0.9",
789
+ "target": "system",
790
+ "changelog": "### Schema Patch: v0.0.9\\n#### Updates: restrict ~User access to the owner and system user",
791
+ "docs": [
792
+ {
793
+ "_id": "Policy-System-Classes",
794
+ "_rev": "auto",
795
+ "~class": "~Policy",
796
+ "active": true,
797
+ "userId": "system",
798
+ "rule": "return true;",
799
+ "description": "Default system policy",
800
+ "targetClass": ["class", "~UserSession", "~Policy", "~Job", "~JobRun", "~AuthModule"]
801
+ },
802
+ {
803
+ "_id": "Policy-User-SelfAccess",
804
+ "~class": "~Policy",
805
+ "active": true,
806
+ "userId": "system",
807
+ "rule": "if (!session || session.sessionStatus !== 'active') return false; if (session.username === 'system') return true; const targetUsername = document?.username || document?._id; return targetUsername === session.username;",
808
+ "description": "Allow users to access only their own user document or the system user",
809
+ "targetClass": ["~User"]
810
+ }
811
+ ]
812
+ };
813
+ const sys_010 = {
814
+ "_id": "~sys-0.0.10",
815
+ "~class": "patch",
816
+ "version": "0.0.10",
817
+ "target": "system",
818
+ "changelog": "### Schema Patch: v0.0.10\\n#### Updates: enforce ~UserSession references an existing ~User via userId",
819
+ "docs": [
820
+ {
821
+ "_id": "~UserSession",
822
+ "_rev": "auto",
823
+ "name": "UserSession",
824
+ "active": true,
825
+ "description": "Tracks user sessions",
826
+ "~class": "class",
827
+ "schema": {
828
+ "userId": {
829
+ "name": "userId",
830
+ "type": "foreign_key",
831
+ "config": {
832
+ "mandatory": true,
833
+ "targetClass": "~User",
834
+ "isArray": false
835
+ }
836
+ },
837
+ "username": {
838
+ "name": "username",
839
+ "type": "string",
840
+ "config": {
841
+ "maxLength": 50,
842
+ "isArray": false,
843
+ "primaryKey": true,
844
+ "mandatory": true
845
+ }
846
+ },
847
+ "sessionId": {
848
+ "name": "sessionId",
849
+ "type": "string",
850
+ "config": {
851
+ "maxLength": 200,
852
+ "primaryKey": true,
853
+ "isArray": false,
854
+ "mandatory": true
855
+ }
856
+ },
857
+ "sessionStart": {
858
+ "name": "sessionStart",
859
+ "type": "string",
860
+ "config": {
861
+ "maxLength": 100,
862
+ "isArray": false,
863
+ "mandatory": true
864
+ }
865
+ },
866
+ "sessionStatus": {
867
+ "name": "sessionStatus",
868
+ "type": "string",
869
+ "config": {
870
+ "maxLength": 100,
871
+ "isArray": false,
872
+ "mandatory": true
873
+ }
874
+ },
875
+ "sessionEnd": {
876
+ "name": "sessionEnd",
877
+ "type": "string",
878
+ "config": {
879
+ "maxLength": 100,
880
+ "isArray": false,
881
+ "mandatory": false
882
+ }
883
+ }
884
+ }
885
+ }
886
+ ]
887
+ };
888
+ syspatches.push(sys_001, /* sys_002 ,*/ sys_003, sys_004, sys_005, sys_006, sys_007, sys_008, sys_009, sys_010);
889
+ const sys_011 = {
890
+ "_id": "~sys-0.0.11",
891
+ "~class": "patch",
892
+ "version": "0.0.11",
893
+ "target": "system",
894
+ "changelog": "### Schema Patch: v0.0.11\\n#### New Class: ~Group",
895
+ "docs": [
896
+ {
897
+ "_id": "~Group",
898
+ "active": true,
899
+ "name": "Group",
900
+ "description": "Represents a user group",
901
+ "~class": "class",
902
+ "schema": {
903
+ "name": { "name": "name", "type": "string", "config": { "primaryKey": true, "mandatory": true, "maxLength": 100, "isArray": false } }
904
+ }
905
+ }
906
+ ]
907
+ };
908
+ const sys_012 = {
909
+ "_id": "~sys-0.0.12",
910
+ "~class": "patch",
911
+ "version": "0.0.12",
912
+ "target": "system",
913
+ "changelog": "### Schema Patch: v0.0.12\\n#### Updates: introduce groups, optional policy principals, and group-aware sessions",
914
+ "docs": [
915
+ {
916
+ "_id": "~User",
917
+ "_rev": "auto",
918
+ "active": true,
919
+ "name": "User",
920
+ "description": "A user class for secure login",
921
+ "~class": "class",
922
+ "schema": {
923
+ "username": {
924
+ "name": "username",
925
+ "type": "string",
926
+ "config": {
927
+ "primaryKey": true,
928
+ "maxLength": 50,
929
+ "mandatory": true,
930
+ "isArray": false
931
+ }
932
+ },
933
+ "password": {
934
+ "name": "password",
935
+ "type": "string",
936
+ "config": {
937
+ "maxLength": 50,
938
+ "mandatory": true,
939
+ "isArray": false,
940
+ "encrypted": true
941
+ }
942
+ },
943
+ "groupId": {
944
+ "name": "groupId",
945
+ "type": "foreign_key",
946
+ "config": {
947
+ "mandatory": true,
948
+ "isArray": true,
949
+ "targetClass": "~Group"
950
+ }
951
+ },
952
+ "email": {
953
+ "name": "email",
954
+ "type": "string",
955
+ "config": {
956
+ "maxLength": 50,
957
+ "isArray": false
958
+ }
959
+ },
960
+ "firstName": {
961
+ "name": "firstName",
962
+ "type": "string",
963
+ "config": {
964
+ "maxLength": 50,
965
+ "isArray": false
966
+ }
967
+ },
968
+ "lastName": {
969
+ "name": "lastName",
970
+ "type": "string",
971
+ "config": {
972
+ "maxLength": 50,
973
+ "isArray": false
974
+ }
975
+ },
976
+ "authMethod": {
977
+ "name": "authMethod",
978
+ "type": "foreign_key",
979
+ "config": { "mandatory": true, "targetClass": "~AuthModule" }
980
+ },
981
+ "externalId": {
982
+ "name": "externalId",
983
+ "type": "string",
984
+ "config": {
985
+ "maxLength": 200,
986
+ "isArray": false
987
+ }
988
+ },
989
+ "keyDerivationSalt": {
990
+ "name": "keyDerivationSalt",
991
+ "type": "string",
992
+ "config": {
993
+ "mandatory": true,
994
+ "maxLength": 200,
995
+ "isArray": false
996
+ }
997
+ },
998
+ "wrappedDocumentKey": {
999
+ "name": "wrappedDocumentKey",
1000
+ "type": "string",
1001
+ "config": {
1002
+ "mandatory": false,
1003
+ "maxLength": 4096,
1004
+ "isArray": false,
1005
+ "encrypted": true
1006
+ }
1007
+ }
1008
+ }
1009
+ },
1010
+ {
1011
+ "_id": "~UserSession",
1012
+ "_rev": "auto",
1013
+ "name": "UserSession",
1014
+ "active": true,
1015
+ "description": "Tracks user sessions",
1016
+ "~class": "class",
1017
+ "schema": {
1018
+ "userId": {
1019
+ "name": "userId",
1020
+ "type": "foreign_key",
1021
+ "config": {
1022
+ "mandatory": true,
1023
+ "targetClass": "~User",
1024
+ "isArray": false
1025
+ }
1026
+ },
1027
+ "groupId": {
1028
+ "name": "groupId",
1029
+ "type": "foreign_key",
1030
+ "config": {
1031
+ "mandatory": true,
1032
+ "targetClass": "~Group",
1033
+ "isArray": true
1034
+ }
1035
+ },
1036
+ "username": {
1037
+ "name": "username",
1038
+ "type": "string",
1039
+ "config": {
1040
+ "maxLength": 50,
1041
+ "isArray": false,
1042
+ "primaryKey": true,
1043
+ "mandatory": true
1044
+ }
1045
+ },
1046
+ "sessionId": {
1047
+ "name": "sessionId",
1048
+ "type": "string",
1049
+ "config": {
1050
+ "maxLength": 200,
1051
+ "primaryKey": true,
1052
+ "isArray": false,
1053
+ "mandatory": true
1054
+ }
1055
+ },
1056
+ "sessionStart": {
1057
+ "name": "sessionStart",
1058
+ "type": "string",
1059
+ "config": {
1060
+ "maxLength": 100,
1061
+ "isArray": false,
1062
+ "mandatory": true
1063
+ }
1064
+ },
1065
+ "sessionStatus": {
1066
+ "name": "sessionStatus",
1067
+ "type": "string",
1068
+ "config": {
1069
+ "maxLength": 100,
1070
+ "isArray": false,
1071
+ "mandatory": true
1072
+ }
1073
+ },
1074
+ "sessionEnd": {
1075
+ "name": "sessionEnd",
1076
+ "type": "string",
1077
+ "config": {
1078
+ "maxLength": 100,
1079
+ "isArray": false,
1080
+ "mandatory": false
1081
+ }
1082
+ }
1083
+ }
1084
+ },
1085
+ {
1086
+ "_id": "~Policy",
1087
+ "_rev": "auto",
1088
+ "active": true,
1089
+ "name": "Policy",
1090
+ "description": "Access policies for classes and documents",
1091
+ "~class": "class",
1092
+ "schema": {
1093
+ "userId": { "name": "userId", "type": "foreign_key", "config": { "mandatory": false, "targetClass": "~User" } },
1094
+ "groupId": { "name": "groupId", "type": "foreign_key", "config": { "mandatory": false, "targetClass": "~Group" } },
1095
+ "rule": { "name": "rule", "type": "string", "config": { "mandatory": true, "maxLength": 2000 } },
1096
+ "description": { "name": "description", "type": "string", "config": { "mandatory": false } },
1097
+ "targetClass": { "name": "targetClass", "type": "foreign_key", "config": { "mandatory": true, "isArray": true, "targetClass": "class" } }
1098
+ }
1099
+ }
1100
+ ]
1101
+ };
1102
+ const sys_013 = {
1103
+ "_id": "~sys-0.0.13",
1104
+ "~class": "patch",
1105
+ "version": "0.0.13",
1106
+ "target": "system",
1107
+ "changelog": "### Schema Patch: v0.0.13\\n#### New Documents: Group-Admin, Group-Default",
1108
+ "docs": [
1109
+ {
1110
+ "_id": "Group-Admin",
1111
+ "~class": "~Group",
1112
+ "active": true,
1113
+ "name": "Admin"
1114
+ },
1115
+ {
1116
+ "_id": "Group-Default",
1117
+ "~class": "~Group",
1118
+ "active": true,
1119
+ "name": "Default"
1120
+ }
1121
+ ]
1122
+ };
1123
+ const sys_014 = {
1124
+ "_id": "~sys-0.0.14",
1125
+ "~class": "patch",
1126
+ "version": "0.0.14",
1127
+ "target": "system",
1128
+ "changelog": "### Schema Patch: v0.0.14\\n#### New Documents: Policy-Admin, Policy-User-SelfAccess\\n#### Updates: system user with groupId",
1129
+ "docs": [
1130
+ {
1131
+ "_id": "system",
1132
+ "_rev": "auto",
1133
+ "~class": "~User",
1134
+ "active": true,
1135
+ "username": "system",
1136
+ "password": "system",
1137
+ "groupId": ["Group-Admin"],
1138
+ "email": "",
1139
+ "firstName": "System",
1140
+ "lastName": "User",
1141
+ "authMethod": "AuthMod-Classic",
1142
+ "externalId": "",
1143
+ "keyDerivationSalt": "system-salt",
1144
+ "wrappedDocumentKey": ""
1145
+ },
1146
+ {
1147
+ "_id": "Policy-Admin",
1148
+ "~class": "~Policy",
1149
+ "active": true,
1150
+ "groupId": "Group-Admin",
1151
+ "rule": "return true;",
1152
+ "description": "Default admin policy",
1153
+ "targetClass": ["class", "~UserSession", "~Policy", "~Job", "~JobRun", "~AuthModule", "~Group"]
1154
+ },
1155
+ {
1156
+ "_id": "Policy-User-SelfAccess",
1157
+ "_rev": "auto",
1158
+ "~class": "~Policy",
1159
+ "active": true,
1160
+ "userId": "system",
1161
+ "rule": "if (!session || session.sessionStatus !== 'active') return false; if (session.username === 'system') return true; const targetUsername = document?.username || document?._id; return targetUsername === session.username;",
1162
+ "description": "Allow users to access only their own user document or the system user",
1163
+ "targetClass": ["~User"]
1164
+ }
1165
+ ]
1166
+ };
1167
+ syspatches.push(sys_011, sys_012, sys_013, sys_014);
1168
+ export async function getSystemPatches(currentVersion) {
1169
+ const classicAuthJobHash = await hashContent(classicAuthJobContent);
1170
+ const jobAuthClassic = sys_005.docs.find(d => d._id === 'Job-Auth-Classic');
1171
+ if (jobAuthClassic) {
1172
+ jobAuthClassic.hash = classicAuthJobHash;
1173
+ }
1174
+ return syspatches
1175
+ .sort((a, b) => semver.compare(a.version, b.version))
1176
+ .filter((patch) => semver.gt(patch.version, currentVersion));
1177
+ }
1178
+ export async function getAllSystemPatches() {
1179
+ const classicAuthJobHash = await hashContent(classicAuthJobContent);
1180
+ const jobAuthClassic = sys_005.docs.find(d => d._id === 'Job-Auth-Classic');
1181
+ if (jobAuthClassic) {
1182
+ jobAuthClassic.hash = classicAuthJobHash;
1183
+ }
1184
+ return syspatches.sort((a, b) => semver.compare(a.version, b.version));
1185
+ }
1186
+ //# sourceMappingURL=index.js.map