@revisium/schema-toolkit 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +184 -0
  3. package/dist/consts/index.cjs +26 -0
  4. package/dist/consts/index.cjs.map +1 -0
  5. package/dist/consts/index.d.cts +17 -0
  6. package/dist/consts/index.d.ts +17 -0
  7. package/dist/consts/index.js +23 -0
  8. package/dist/consts/index.js.map +1 -0
  9. package/dist/index.cjs +1874 -0
  10. package/dist/index.cjs.map +1 -0
  11. package/dist/index.d.cts +13 -0
  12. package/dist/index.d.ts +13 -0
  13. package/dist/index.js +1787 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/json-patch.types-DiJBqfxV.d.ts +28 -0
  16. package/dist/json-patch.types-lByaF-OL.d.cts +28 -0
  17. package/dist/json-string.store-O1J0j-a1.d.cts +228 -0
  18. package/dist/json-string.store-S9FXT39Q.d.ts +228 -0
  19. package/dist/json.types-46Cq-WxU.d.cts +8 -0
  20. package/dist/json.types-46Cq-WxU.d.ts +8 -0
  21. package/dist/lib/index.cjs +1346 -0
  22. package/dist/lib/index.cjs.map +1 -0
  23. package/dist/lib/index.d.cts +91 -0
  24. package/dist/lib/index.d.ts +91 -0
  25. package/dist/lib/index.js +1315 -0
  26. package/dist/lib/index.js.map +1 -0
  27. package/dist/mocks/index.cjs +92 -0
  28. package/dist/mocks/index.cjs.map +1 -0
  29. package/dist/mocks/index.d.cts +26 -0
  30. package/dist/mocks/index.d.ts +26 -0
  31. package/dist/mocks/index.js +81 -0
  32. package/dist/mocks/index.js.map +1 -0
  33. package/dist/model/index.cjs +718 -0
  34. package/dist/model/index.cjs.map +1 -0
  35. package/dist/model/index.d.cts +19 -0
  36. package/dist/model/index.d.ts +19 -0
  37. package/dist/model/index.js +697 -0
  38. package/dist/model/index.js.map +1 -0
  39. package/dist/plugins/index.cjs +122 -0
  40. package/dist/plugins/index.cjs.map +1 -0
  41. package/dist/plugins/index.d.cts +21 -0
  42. package/dist/plugins/index.d.ts +21 -0
  43. package/dist/plugins/index.js +112 -0
  44. package/dist/plugins/index.js.map +1 -0
  45. package/dist/schema.types-Q3MYTp8z.d.cts +68 -0
  46. package/dist/schema.types-Q3MYTp8z.d.ts +68 -0
  47. package/dist/types/index.cjs +15 -0
  48. package/dist/types/index.cjs.map +1 -0
  49. package/dist/types/index.d.cts +56 -0
  50. package/dist/types/index.d.ts +56 -0
  51. package/dist/types/index.js +13 -0
  52. package/dist/types/index.js.map +1 -0
  53. package/dist/validation-schemas/index.cjs +380 -0
  54. package/dist/validation-schemas/index.cjs.map +1 -0
  55. package/dist/validation-schemas/index.d.cts +32 -0
  56. package/dist/validation-schemas/index.d.ts +32 -0
  57. package/dist/validation-schemas/index.js +365 -0
  58. package/dist/validation-schemas/index.js.map +1 -0
  59. package/package.json +169 -0
@@ -0,0 +1,1315 @@
1
+ import { nanoid } from 'nanoid';
2
+ import { EventEmitter } from 'events';
3
+
4
+ // src/lib/addSharedFieldsFromStore.ts
5
+ var addSharedFieldsFromState = (schema, state) => {
6
+ if (state.title) {
7
+ schema.title = state.title;
8
+ }
9
+ if (state.description) {
10
+ schema.description = state.description;
11
+ }
12
+ if (state.deprecated) {
13
+ schema.deprecated = state.deprecated;
14
+ }
15
+ return schema;
16
+ };
17
+ var JsonArrayStore = class {
18
+ constructor(_items, nodeId = nanoid()) {
19
+ this._items = _items;
20
+ this.nodeId = nodeId;
21
+ this.items.parent = this;
22
+ }
23
+ type = "array" /* Array */;
24
+ $ref = "";
25
+ name = "";
26
+ parent = null;
27
+ default = [];
28
+ title;
29
+ description;
30
+ deprecated;
31
+ valuesMap = /* @__PURE__ */ new Map();
32
+ get items() {
33
+ return this._items;
34
+ }
35
+ registerValue(value) {
36
+ const length = this.getOrCreateValues(value.rowId).push(value);
37
+ return length - 1;
38
+ }
39
+ getValue(rowId, index = 0) {
40
+ return this.getOrCreateValues(rowId)[index];
41
+ }
42
+ migrateItems(items) {
43
+ const previousItems = this._items;
44
+ previousItems.parent = null;
45
+ this._items = items;
46
+ this._items.parent = this;
47
+ const event = { items, previousItems };
48
+ for (const value of this.iterValues()) {
49
+ value.migrateItems(event);
50
+ }
51
+ }
52
+ replaceItems(items) {
53
+ const previousItems = this._items;
54
+ previousItems.parent = null;
55
+ this._items = items;
56
+ this._items.parent = this;
57
+ const event = { items, previousItems };
58
+ for (const value of this.iterValues()) {
59
+ value.replaceItems(event);
60
+ }
61
+ }
62
+ getPlainSchema(options) {
63
+ if (this.$ref && options?.skip$Ref !== true) {
64
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
65
+ }
66
+ return addSharedFieldsFromState(
67
+ {
68
+ type: this.type,
69
+ items: this.items.getPlainSchema(options)
70
+ },
71
+ this
72
+ );
73
+ }
74
+ getOrCreateValues(rowId) {
75
+ let values = this.valuesMap.get(rowId);
76
+ if (!values) {
77
+ values = [];
78
+ this.valuesMap.set(rowId, values);
79
+ }
80
+ return values;
81
+ }
82
+ *iterValues() {
83
+ for (const values of this.valuesMap.values()) {
84
+ for (const value of values) {
85
+ yield value;
86
+ }
87
+ }
88
+ }
89
+ };
90
+ var JsonBooleanStore = class extends EventEmitter {
91
+ constructor(nodeId = nanoid()) {
92
+ super();
93
+ this.nodeId = nodeId;
94
+ }
95
+ type = "boolean" /* Boolean */;
96
+ $ref = "";
97
+ name = "";
98
+ parent = null;
99
+ default = false;
100
+ readOnly;
101
+ title;
102
+ description;
103
+ deprecated;
104
+ valuesMap = /* @__PURE__ */ new Map();
105
+ registerValue(value) {
106
+ const length = this.getOrCreateValues(value.rowId).push(value);
107
+ return length - 1;
108
+ }
109
+ getValue(rowId, index = 0) {
110
+ return this.getOrCreateValues(rowId)[index];
111
+ }
112
+ getPlainSchema(options) {
113
+ if (this.$ref && options?.skip$Ref !== true) {
114
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
115
+ }
116
+ return addSharedFieldsFromState(
117
+ {
118
+ type: this.type,
119
+ default: this.default,
120
+ ...this.readOnly ? { readOnly: this.readOnly } : {}
121
+ },
122
+ this
123
+ );
124
+ }
125
+ getOrCreateValues(rowId) {
126
+ let values = this.valuesMap.get(rowId);
127
+ if (!values) {
128
+ values = [];
129
+ this.valuesMap.set(rowId, values);
130
+ }
131
+ return values;
132
+ }
133
+ };
134
+ var JsonNumberStore = class extends EventEmitter {
135
+ constructor(nodeId = nanoid()) {
136
+ super();
137
+ this.nodeId = nodeId;
138
+ }
139
+ type = "number" /* Number */;
140
+ $ref = "";
141
+ name = "";
142
+ parent = null;
143
+ default = 0;
144
+ readOnly;
145
+ title;
146
+ description;
147
+ deprecated;
148
+ valuesMap = /* @__PURE__ */ new Map();
149
+ registerValue(value) {
150
+ const length = this.getOrCreateValues(value.rowId).push(value);
151
+ return length - 1;
152
+ }
153
+ getValue(rowId, index = 0) {
154
+ return this.getOrCreateValues(rowId)[index];
155
+ }
156
+ getPlainSchema(options) {
157
+ if (this.$ref && options?.skip$Ref !== true) {
158
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
159
+ }
160
+ return addSharedFieldsFromState(
161
+ {
162
+ type: this.type,
163
+ default: this.default,
164
+ ...this.readOnly ? { readOnly: this.readOnly } : {}
165
+ },
166
+ this
167
+ );
168
+ }
169
+ getOrCreateValues(rowId) {
170
+ let values = this.valuesMap.get(rowId);
171
+ if (!values) {
172
+ values = [];
173
+ this.valuesMap.set(rowId, values);
174
+ }
175
+ return values;
176
+ }
177
+ };
178
+ var JsonStringStore = class extends EventEmitter {
179
+ constructor(nodeId = nanoid()) {
180
+ super();
181
+ this.nodeId = nodeId;
182
+ }
183
+ type = "string" /* String */;
184
+ $ref = "";
185
+ name = "";
186
+ parent = null;
187
+ default = "";
188
+ readOnly;
189
+ title;
190
+ description;
191
+ deprecated;
192
+ foreignKey;
193
+ pattern;
194
+ enum;
195
+ format;
196
+ contentMediaType;
197
+ valuesMap = /* @__PURE__ */ new Map();
198
+ registerValue(value) {
199
+ const length = this.getOrCreateValues(value.rowId).push(value);
200
+ return length - 1;
201
+ }
202
+ getValue(rowId, index = 0) {
203
+ return this.getOrCreateValues(rowId)[index];
204
+ }
205
+ getPlainSchema(options) {
206
+ if (this.$ref && options?.skip$Ref !== true) {
207
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
208
+ }
209
+ return addSharedFieldsFromState(
210
+ {
211
+ type: this.type,
212
+ default: this.default,
213
+ ...this.foreignKey ? { foreignKey: this.foreignKey } : {},
214
+ ...this.readOnly ? { readOnly: this.readOnly } : {},
215
+ ...this.pattern ? { pattern: this.pattern } : {},
216
+ ...this.enum ? { enum: this.enum } : {},
217
+ ...this.format ? { format: this.format } : {},
218
+ ...this.contentMediaType ? { contentMediaType: this.contentMediaType } : {}
219
+ },
220
+ this
221
+ );
222
+ }
223
+ getOrCreateValues(rowId) {
224
+ let values = this.valuesMap.get(rowId);
225
+ if (!values) {
226
+ values = [];
227
+ this.valuesMap.set(rowId, values);
228
+ }
229
+ return values;
230
+ }
231
+ };
232
+ var JsonObjectStore = class {
233
+ constructor(nodeId = nanoid()) {
234
+ this.nodeId = nodeId;
235
+ }
236
+ type = "object" /* Object */;
237
+ $ref = "";
238
+ name = "";
239
+ parent = null;
240
+ default = {};
241
+ title;
242
+ description;
243
+ deprecated;
244
+ additionalProperties = false;
245
+ required = [];
246
+ properties = {};
247
+ valuesMap = /* @__PURE__ */ new Map();
248
+ get empty() {
249
+ return Object.keys(this.properties).length === 0;
250
+ }
251
+ registerValue(value) {
252
+ const length = this.getOrCreateValues(value.rowId).push(value);
253
+ return length - 1;
254
+ }
255
+ getValue(rowId, index = 0) {
256
+ return this.getOrCreateValues(rowId)[index];
257
+ }
258
+ addPropertyWithStore(name, store) {
259
+ if (this.properties[name] || this.required.includes(name)) {
260
+ throw new Error("this name already exists");
261
+ }
262
+ store.parent = this;
263
+ store.name = name;
264
+ this.required.push(name);
265
+ this.required.sort((a, b) => a.localeCompare(b));
266
+ this.properties[name] = store;
267
+ this.default[name] = store.default;
268
+ const event = { name, property: store };
269
+ for (const value of this.iterValues()) {
270
+ value.addProperty(event);
271
+ }
272
+ return store;
273
+ }
274
+ migratePropertyWithStore(name, store) {
275
+ const item = this.properties[name];
276
+ if (!item) {
277
+ throw new Error("this name does not exist");
278
+ }
279
+ item.parent = null;
280
+ store.parent = this;
281
+ store.name = name;
282
+ this.properties[name] = store;
283
+ this.default[name] = store.default;
284
+ const event = {
285
+ name,
286
+ property: store,
287
+ previousProperty: item
288
+ };
289
+ for (const value of this.iterValues()) {
290
+ value.migrateProperty(event);
291
+ }
292
+ return store;
293
+ }
294
+ changeName(fromName, toName) {
295
+ const item = this.properties[fromName];
296
+ if (!item) {
297
+ throw new Error("this fromName does not exist");
298
+ }
299
+ delete this.properties[fromName];
300
+ delete this.default[fromName];
301
+ const foundRequiredIndex = this.required.findIndex(
302
+ (required) => required === fromName
303
+ );
304
+ if (foundRequiredIndex !== -1) {
305
+ this.required.splice(foundRequiredIndex, 1);
306
+ }
307
+ if (!this.required.includes(toName)) {
308
+ this.required.push(toName);
309
+ this.required.sort((a, b) => a.localeCompare(b));
310
+ }
311
+ this.properties[toName] = item;
312
+ this.default[toName] = item.default;
313
+ const event = {
314
+ fromName,
315
+ toName,
316
+ property: item
317
+ };
318
+ for (const value of this.iterValues()) {
319
+ value.changeName(event);
320
+ }
321
+ }
322
+ removeProperty(name) {
323
+ const item = this.properties[name];
324
+ if (!item) {
325
+ throw new Error("this name does not exist");
326
+ }
327
+ item.parent = null;
328
+ item.name = "";
329
+ delete this.properties[name];
330
+ delete this.default[name];
331
+ const foundRequiredIndex = this.required.findIndex(
332
+ (required) => required === name
333
+ );
334
+ if (foundRequiredIndex !== -1) {
335
+ this.required.splice(foundRequiredIndex, 1);
336
+ }
337
+ const event = {
338
+ name,
339
+ property: item
340
+ };
341
+ for (const value of this.iterValues()) {
342
+ value.removeProperty(event);
343
+ }
344
+ }
345
+ getProperty(name) {
346
+ return this.properties[name];
347
+ }
348
+ getPlainSchema(options) {
349
+ if (this.$ref && options?.skip$Ref !== true) {
350
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
351
+ }
352
+ return addSharedFieldsFromState(
353
+ {
354
+ type: this.type,
355
+ additionalProperties: this.additionalProperties,
356
+ required: this.required,
357
+ properties: Object.entries(this.properties).reduce((result, [name, store]) => {
358
+ result[name] = store.getPlainSchema(options);
359
+ return result;
360
+ }, {})
361
+ },
362
+ this
363
+ );
364
+ }
365
+ getOrCreateValues(rowId) {
366
+ let values = this.valuesMap.get(rowId);
367
+ if (!values) {
368
+ values = [];
369
+ this.valuesMap.set(rowId, values);
370
+ }
371
+ return values;
372
+ }
373
+ *iterValues() {
374
+ for (const values of this.valuesMap.values()) {
375
+ for (const value of values) {
376
+ yield value;
377
+ }
378
+ }
379
+ }
380
+ };
381
+
382
+ // src/lib/createJsonSchemaStore.ts
383
+ var createJsonSchemaStore = (schema, refs = {}) => {
384
+ if ("$ref" in schema) {
385
+ const refSchema = refs[schema.$ref];
386
+ if (!refSchema) {
387
+ throw new Error(`Not found schema for $ref="${schema.$ref}"`);
388
+ }
389
+ const refStore = createJsonSchemaStore(refSchema, refs);
390
+ saveSharedFields(refStore, schema);
391
+ refStore.$ref = schema.$ref;
392
+ return refStore;
393
+ } else if (schema.type === "object" /* Object */) {
394
+ const objectStore = createJsonObjectSchemaStore(schema, refs);
395
+ saveSharedFields(objectStore, schema);
396
+ return objectStore;
397
+ } else if (schema.type === "array" /* Array */) {
398
+ const itemsStore = createJsonSchemaStore(schema.items, refs);
399
+ const arrayStore = new JsonArrayStore(itemsStore);
400
+ saveSharedFields(arrayStore, schema);
401
+ return arrayStore;
402
+ } else {
403
+ const primitivesStore = createPrimitiveStoreBySchema(schema);
404
+ saveSharedFields(primitivesStore, schema);
405
+ primitivesStore.readOnly = schema.readOnly;
406
+ return primitivesStore;
407
+ }
408
+ };
409
+ var createJsonObjectSchemaStore = (value, refs) => {
410
+ const store = new JsonObjectStore();
411
+ for (const requiredField of value.required) {
412
+ if (!value.properties[requiredField]) {
413
+ throw new Error(
414
+ `Not found required field "${requiredField}" in "properties"`
415
+ );
416
+ }
417
+ }
418
+ Object.entries(value.properties).forEach(([name, item]) => {
419
+ store.addPropertyWithStore(name, createJsonSchemaStore(item, refs));
420
+ });
421
+ return store;
422
+ };
423
+ var createPrimitiveStoreBySchema = (schema) => {
424
+ if (schema.type === "string" /* String */) {
425
+ const stringStore = new JsonStringStore();
426
+ stringStore.foreignKey = schema.foreignKey;
427
+ stringStore.format = schema.format;
428
+ stringStore.enum = schema.enum;
429
+ stringStore.contentMediaType = schema.contentMediaType;
430
+ stringStore.pattern = schema.pattern;
431
+ return stringStore;
432
+ } else if (schema.type === "number" /* Number */) {
433
+ return new JsonNumberStore();
434
+ } else if (schema.type === "boolean" /* Boolean */) {
435
+ return new JsonBooleanStore();
436
+ } else {
437
+ throw new Error("this type is not allowed");
438
+ }
439
+ };
440
+ var saveSharedFields = (store, schema) => {
441
+ store.title = schema.title;
442
+ store.description = schema.description;
443
+ store.deprecated = schema.deprecated;
444
+ };
445
+
446
+ // src/lib/getJsonSchemaStoreByPath.ts
447
+ var getJsonSchemaStoreByPath = (store, path) => {
448
+ if (path === "") {
449
+ return store;
450
+ }
451
+ if (path === "/") {
452
+ throw new Error(
453
+ 'invalid root path, need to use path="" instead of path="/"'
454
+ );
455
+ }
456
+ const tokens = path.split("/");
457
+ tokens.shift();
458
+ let currentStore = store;
459
+ let currentToken = tokens.shift();
460
+ let currentPath = "";
461
+ while (currentToken) {
462
+ if (currentStore.type === "object" /* Object */) {
463
+ if (currentToken !== "properties") {
464
+ throw new Error(
465
+ `Expected "${currentPath}/properties/*" instead of ${currentPath}/${currentToken}/*`
466
+ );
467
+ }
468
+ currentPath = `${currentPath}/${currentToken}`;
469
+ currentToken = tokens.shift();
470
+ if (!currentToken) {
471
+ throw new Error(`Expected property name after "${currentPath}"`);
472
+ }
473
+ const foundCurrentStore = currentStore.getProperty(currentToken);
474
+ if (!foundCurrentStore) {
475
+ throw new Error(`Not found "${currentToken}" in "${currentPath}"`);
476
+ }
477
+ currentStore = foundCurrentStore;
478
+ currentPath = `${currentPath}/${currentToken}`;
479
+ currentToken = tokens.shift();
480
+ } else if (currentStore.type === "array" /* Array */) {
481
+ if (currentToken !== "items") {
482
+ throw new Error(
483
+ `Expected "${currentPath}/items/*" instead of ${currentPath}/${currentToken}/*`
484
+ );
485
+ }
486
+ currentPath = `${currentPath}/${currentToken}`;
487
+ currentStore = currentStore.items;
488
+ currentToken = tokens.shift();
489
+ } else {
490
+ throw new Error(`Unexpected "${currentToken}" in "${currentPath}"`);
491
+ }
492
+ }
493
+ return currentStore;
494
+ };
495
+
496
+ // src/lib/getParentForPath.ts
497
+ var getParentForPath = (path) => {
498
+ if (path === "" || path === "/") {
499
+ throw new Error("Invalid path");
500
+ }
501
+ const tokens = path.split("/");
502
+ tokens.shift();
503
+ let currentToken = tokens.shift();
504
+ let parentPath = "";
505
+ let field = "";
506
+ while (currentToken) {
507
+ if (currentToken === "properties") {
508
+ currentToken = tokens.shift();
509
+ if (!currentToken) {
510
+ throw new Error("Invalid path");
511
+ }
512
+ field = currentToken;
513
+ currentToken = tokens.shift();
514
+ if (currentToken) {
515
+ parentPath = `${parentPath}/properties/${field}`;
516
+ }
517
+ } else if (currentToken === "items") {
518
+ field = currentToken;
519
+ currentToken = tokens.shift();
520
+ if (currentToken && !["items", "properties"].includes(currentToken)) {
521
+ throw new Error("Invalid path");
522
+ } else if (currentToken) {
523
+ parentPath = `${parentPath}/items`;
524
+ }
525
+ } else {
526
+ throw new Error("Invalid path");
527
+ }
528
+ }
529
+ return {
530
+ parentPath,
531
+ field
532
+ };
533
+ };
534
+
535
+ // src/lib/validateJsonFieldName.ts
536
+ var maxLength = 64;
537
+ var VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE = `It must contain between 1 and ${maxLength} characters, start with a letter or underscore (_), cannot start with two underscores (__), and can only include letters (a-z, A-Z), numbers (0-9), hyphens (-), and underscores (_).`;
538
+ var validPattern = /^(?!__)[a-zA-Z_][a-zA-Z0-9-_]*$/;
539
+ var validateJsonFieldName = (id) => {
540
+ const isInvalid = id.length < 1 || id.length > maxLength || !validPattern.test(id);
541
+ return !isInvalid;
542
+ };
543
+
544
+ // src/lib/applyPatches.ts
545
+ var applyReplacePatch = (store, patch, refs = {}) => {
546
+ const patchStore = createJsonSchemaStore(patch.value, refs);
547
+ const foundStore = getJsonSchemaStoreByPath(store, patch.path);
548
+ const parent = foundStore.parent;
549
+ if (!parent) {
550
+ return patchStore;
551
+ }
552
+ if (parent.type === "object" /* Object */) {
553
+ parent.migratePropertyWithStore(foundStore.name, patchStore);
554
+ } else if (parent.type === "array" /* Array */) {
555
+ parent.migrateItems(patchStore);
556
+ } else {
557
+ throw new Error("Invalid parent");
558
+ }
559
+ return store;
560
+ };
561
+ var applyRemovePatch = (rootStore, patch) => {
562
+ const foundStore = getJsonSchemaStoreByPath(rootStore, patch.path);
563
+ const parent = foundStore.parent;
564
+ if (!parent) {
565
+ throw new Error("Parent does not exist");
566
+ }
567
+ if (parent.type !== "object" /* Object */) {
568
+ throw new Error("Cannot remove from non-object");
569
+ }
570
+ parent.removeProperty(foundStore.name);
571
+ };
572
+ var applyAddPatch = (rootStore, patch, refs = {}) => {
573
+ const patchStore = createJsonSchemaStore(patch.value, refs);
574
+ const { parentPath, field } = getParentForPath(patch.path);
575
+ const foundParent = getJsonSchemaStoreByPath(rootStore, parentPath);
576
+ if (!foundParent) {
577
+ throw new Error("Parent does not exist");
578
+ }
579
+ if (foundParent.type !== "object" /* Object */) {
580
+ throw new Error("Cannot add to non-object");
581
+ }
582
+ if (foundParent.getProperty(field)) {
583
+ throw new Error(`Field "${field}" already exists in parent`);
584
+ }
585
+ foundParent.addPropertyWithStore(field, patchStore);
586
+ };
587
+ var applyMovePatch = (store, patch) => {
588
+ const { parentPath: fromParentPath, field: fromField } = getParentForPath(
589
+ patch.from
590
+ );
591
+ const { parentPath: toParentPath, field: toField } = getParentForPath(
592
+ patch.path
593
+ );
594
+ const foundFromParent = getJsonSchemaStoreByPath(store, fromParentPath);
595
+ const foundToParent = getJsonSchemaStoreByPath(store, toParentPath);
596
+ const isValidToField = validateJsonFieldName(toField);
597
+ if (!isValidToField) {
598
+ throw new Error(
599
+ `Invalid name: ${toField}. ${VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE}`
600
+ );
601
+ }
602
+ if (!foundFromParent || !foundToParent) {
603
+ throw new Error("Cannot move from or to non-existent parent");
604
+ }
605
+ if (foundFromParent.type !== "object" /* Object */) {
606
+ throw new Error("Cannot move from non-object parent");
607
+ }
608
+ const foundFromField = getJsonSchemaStoreByPath(store, patch.from);
609
+ const isMovedPropertyInSameParentPatch = foundFromParent === foundToParent && foundFromParent.type === "object" /* Object */ && foundFromParent.getProperty(fromField);
610
+ if (isMovedPropertyInSameParentPatch) {
611
+ return foundFromParent.changeName(fromField, toField);
612
+ }
613
+ if (foundToParent.type === "object" /* Object */) {
614
+ foundFromParent.removeProperty(fromField);
615
+ if (foundToParent.getProperty(toField)) {
616
+ foundToParent.removeProperty(toField);
617
+ }
618
+ foundToParent.addPropertyWithStore(toField, foundFromField);
619
+ return;
620
+ }
621
+ if (foundToParent.type === "array" /* Array */) {
622
+ foundFromParent.removeProperty(fromField);
623
+ foundToParent.replaceItems(foundFromField);
624
+ return;
625
+ }
626
+ throw new Error('Invalid type of "to" parent');
627
+ };
628
+
629
+ // src/model/value/value-transformation.ts
630
+ var equel = (value) => value;
631
+ var fromNumberToString = (value, defaultValue = "") => value.toString() || defaultValue;
632
+ var fromBooleanToString = (value, defaultValue = "") => value.toString() || defaultValue;
633
+ var fromStringToBoolean = (value, defaultValue = false) => {
634
+ if (!value) {
635
+ return defaultValue;
636
+ }
637
+ if (value.toLowerCase() === "false") {
638
+ return false;
639
+ }
640
+ return true;
641
+ };
642
+ var fromStringToNumber = (value, defaultValue = 0) => {
643
+ const number = Number.parseFloat(value);
644
+ if (Number.isNaN(number)) {
645
+ return defaultValue;
646
+ }
647
+ return number;
648
+ };
649
+ var fromBooleanToNumber = (value) => {
650
+ return Number(value);
651
+ };
652
+ var fromNumberToBoolean = (value) => {
653
+ return Boolean(value);
654
+ };
655
+ var toArrayTransformation = (transformation) => (value) => {
656
+ const result = transformation(value);
657
+ return [result];
658
+ };
659
+ var fromArrayTransformation = (transformation) => (value) => {
660
+ if (Array.isArray(value) && value.length) {
661
+ return transformation(value[0]);
662
+ }
663
+ return void 0;
664
+ };
665
+ var replaceTransformationsMapper = [
666
+ {
667
+ fromType: "number" /* Number */,
668
+ toType: "string" /* String */,
669
+ transformation: fromNumberToString
670
+ },
671
+ {
672
+ fromType: "string" /* String */,
673
+ toType: "number" /* Number */,
674
+ transformation: fromStringToNumber
675
+ },
676
+ {
677
+ fromType: "boolean" /* Boolean */,
678
+ toType: "string" /* String */,
679
+ transformation: fromBooleanToString
680
+ },
681
+ {
682
+ fromType: "string" /* String */,
683
+ toType: "boolean" /* Boolean */,
684
+ transformation: fromStringToBoolean
685
+ },
686
+ {
687
+ fromType: "boolean" /* Boolean */,
688
+ toType: "number" /* Number */,
689
+ transformation: fromBooleanToNumber
690
+ },
691
+ {
692
+ fromType: "number" /* Number */,
693
+ toType: "boolean" /* Boolean */,
694
+ transformation: fromNumberToBoolean
695
+ }
696
+ ];
697
+ var getTransformation = (from, to) => {
698
+ if (to instanceof JsonArrayStore) {
699
+ const transformation = findTransformation(from.type, to.items.type);
700
+ if (!transformation) {
701
+ return;
702
+ }
703
+ return toArrayTransformation(transformation);
704
+ } else if (from instanceof JsonArrayStore) {
705
+ const transformation = findTransformation(from.items.type, to.type);
706
+ if (!transformation) {
707
+ return;
708
+ }
709
+ return fromArrayTransformation(transformation);
710
+ }
711
+ return findTransformation(from.type, to.type);
712
+ };
713
+ var findTransformation = (from, to) => {
714
+ if (from === to) {
715
+ return equel;
716
+ }
717
+ for (const item of replaceTransformationsMapper) {
718
+ if (item.fromType === from && item.toType === to) {
719
+ return item.transformation;
720
+ }
721
+ }
722
+ return void 0;
723
+ };
724
+
725
+ // src/model/value/json-array-value.store.ts
726
+ var JsonArrayValueStore = class {
727
+ constructor(schema, rowId, value) {
728
+ this.schema = schema;
729
+ this.rowId = rowId;
730
+ this.value = value;
731
+ this.index = this.schema.registerValue(this);
732
+ this.init();
733
+ }
734
+ type = "array" /* Array */;
735
+ index;
736
+ parent = null;
737
+ getPlainValue() {
738
+ return this.value.map((item) => item.getPlainValue());
739
+ }
740
+ migrateItems(event) {
741
+ const transformation = getTransformation(event.previousItems, event.items);
742
+ this.value = this.value.map((valueItem) => {
743
+ const rawValue = transformation ? transformation(
744
+ valueItem.getPlainValue(),
745
+ event.items.default
746
+ ) : event.items.default;
747
+ return createJsonValueStore(event.items, this.rowId, rawValue);
748
+ });
749
+ }
750
+ replaceItems(event) {
751
+ this.value = this.value.map(() => {
752
+ const rawValue = this.getReplacedValue(event);
753
+ return createJsonValueStore(event.items, this.rowId, rawValue);
754
+ });
755
+ }
756
+ getReplacedValue(event) {
757
+ const previousValue = event.items.getValue(this.rowId);
758
+ if (previousValue) {
759
+ return previousValue.getPlainValue();
760
+ }
761
+ return event.items.default;
762
+ }
763
+ init() {
764
+ for (const value of this.value) {
765
+ value.parent = this;
766
+ }
767
+ }
768
+ };
769
+
770
+ // src/model/value/json-boolean-value.store.ts
771
+ var JsonBooleanValueStore = class {
772
+ constructor(schema, rowId, value = null) {
773
+ this.schema = schema;
774
+ this.rowId = rowId;
775
+ this.value = value;
776
+ this.index = this.schema.registerValue(this);
777
+ }
778
+ type = "boolean" /* Boolean */;
779
+ index;
780
+ parent = null;
781
+ getPlainValue() {
782
+ return this.value ?? this.schema.default;
783
+ }
784
+ };
785
+
786
+ // src/model/value/json-number-value.store.ts
787
+ var JsonNumberValueStore = class {
788
+ constructor(schema, rowId, value = null) {
789
+ this.schema = schema;
790
+ this.rowId = rowId;
791
+ this.value = value;
792
+ this.index = this.schema.registerValue(this);
793
+ }
794
+ type = "number" /* Number */;
795
+ index;
796
+ parent = null;
797
+ getPlainValue() {
798
+ return this.value ?? this.schema.default;
799
+ }
800
+ };
801
+
802
+ // src/model/value/json-object-value.store.ts
803
+ var JsonObjectValueStore = class {
804
+ constructor(schema, rowId, value) {
805
+ this.schema = schema;
806
+ this.rowId = rowId;
807
+ this.value = value;
808
+ this.index = this.schema.registerValue(this);
809
+ this.init();
810
+ }
811
+ type = "object" /* Object */;
812
+ index;
813
+ parent = null;
814
+ getPlainValue() {
815
+ return Object.entries(this.value).reduce(
816
+ (result, [name, store]) => {
817
+ result[name] = store.getPlainValue();
818
+ return result;
819
+ },
820
+ {}
821
+ );
822
+ }
823
+ migrateProperty(event) {
824
+ const rawValue = this.getMigratedValue(event);
825
+ this.value[event.name] = createJsonValueStore(
826
+ event.property,
827
+ this.rowId,
828
+ rawValue
829
+ );
830
+ }
831
+ addProperty(event) {
832
+ const rawValue = this.getAddedValue(event);
833
+ this.value[event.name] = createJsonValueStore(
834
+ event.property,
835
+ this.rowId,
836
+ rawValue
837
+ );
838
+ }
839
+ removeProperty(event) {
840
+ delete this.value[event.name];
841
+ }
842
+ changeName(event) {
843
+ const itemValue = this.value[event.fromName];
844
+ if (itemValue !== void 0) {
845
+ delete this.value[event.fromName];
846
+ this.value[event.toName] = itemValue;
847
+ }
848
+ }
849
+ getAddedValue(event) {
850
+ const previousValue = event.property.getValue(this.rowId, this.index);
851
+ if (previousValue) {
852
+ return previousValue.getPlainValue();
853
+ }
854
+ return event.property.default;
855
+ }
856
+ getMigratedValue(event) {
857
+ const transformation = getTransformation(
858
+ event.previousProperty,
859
+ event.property
860
+ );
861
+ const valueStore = this.value[event.name];
862
+ if (transformation && valueStore) {
863
+ return transformation(
864
+ valueStore.getPlainValue(),
865
+ event.property.default
866
+ );
867
+ }
868
+ return event.property.default;
869
+ }
870
+ init() {
871
+ for (const value of Object.values(this.value)) {
872
+ value.parent = this;
873
+ }
874
+ }
875
+ };
876
+
877
+ // src/model/value/json-string-value.store.ts
878
+ var JsonStringValueStore = class {
879
+ constructor(schema, rowId, value = null) {
880
+ this.schema = schema;
881
+ this.rowId = rowId;
882
+ this.value = value;
883
+ this.index = this.schema.registerValue(this);
884
+ }
885
+ type = "string" /* String */;
886
+ index;
887
+ parent = null;
888
+ get foreignKey() {
889
+ return this.schema.foreignKey;
890
+ }
891
+ getPlainValue() {
892
+ return this.value ?? this.schema.default;
893
+ }
894
+ };
895
+
896
+ // src/lib/createJsonValueStore.ts
897
+ var createJsonValueStore = (schema, rowId, rawValue) => {
898
+ if (schema.type === "object" /* Object */) {
899
+ return createJsonObjectValueStore(schema, rowId, rawValue);
900
+ } else if (schema.type === "array" /* Array */) {
901
+ return createJsonArrayValueStore(schema, rowId, rawValue);
902
+ } else {
903
+ return createPrimitiveValueStore(schema, rowId, rawValue);
904
+ }
905
+ };
906
+ var createJsonObjectValueStore = (schema, rowId, rawValue) => {
907
+ const value = Object.entries(rawValue).reduce(
908
+ (reduceValue, [key, itemValue]) => {
909
+ const itemSchema = schema.getProperty(key);
910
+ if (itemSchema === void 0 || itemValue === void 0) {
911
+ throw new Error("Invalid item");
912
+ }
913
+ reduceValue[key] = createJsonValueStore(itemSchema, rowId, itemValue);
914
+ return reduceValue;
915
+ },
916
+ {}
917
+ );
918
+ return new JsonObjectValueStore(schema, rowId, value);
919
+ };
920
+ var createJsonArrayValueStore = (schema, rowId, rawValue) => {
921
+ const value = rawValue.map(
922
+ (value2) => createJsonValueStore(schema.items, rowId, value2)
923
+ );
924
+ return new JsonArrayValueStore(schema, rowId, value);
925
+ };
926
+ var createPrimitiveValueStore = (schema, rowId, rawValue) => {
927
+ if (schema.type === "string" /* String */) {
928
+ return new JsonStringValueStore(schema, rowId, rawValue);
929
+ } else if (schema.type === "number" /* Number */) {
930
+ return new JsonNumberValueStore(schema, rowId, rawValue);
931
+ } else if (schema.type === "boolean" /* Boolean */) {
932
+ return new JsonBooleanValueStore(schema, rowId, rawValue);
933
+ } else {
934
+ throw new Error("this type is not allowed");
935
+ }
936
+ };
937
+
938
+ // src/lib/getDBJsonPathByJsonSchemaStore.ts
939
+ var getDBJsonPathByJsonSchemaStore = (store) => {
940
+ let node = store;
941
+ let path = "";
942
+ while (node.parent) {
943
+ if (node.parent.type === "object" /* Object */) {
944
+ path = `.${node.name}${path}`;
945
+ } else if (node.parent.type === "array" /* Array */) {
946
+ path = `[*]${path}`;
947
+ }
948
+ node = node.parent;
949
+ }
950
+ if (!path) {
951
+ return "$";
952
+ }
953
+ return `$${path}`;
954
+ };
955
+
956
+ // src/lib/getPathByStore.ts
957
+ var getPathByStore = (store) => {
958
+ let node = store;
959
+ let path = "";
960
+ while (node.parent) {
961
+ if (node.parent.type === "object" /* Object */) {
962
+ path = `/properties/${node.name}${path}`;
963
+ } else if (node.parent.type === "array" /* Array */) {
964
+ path = `/items${path}`;
965
+ }
966
+ node = node.parent;
967
+ }
968
+ if (!path) {
969
+ return "/";
970
+ }
971
+ return `${path}`;
972
+ };
973
+
974
+ // src/lib/traverseStore.ts
975
+ var traverseStore = (store, callback) => {
976
+ callback(store);
977
+ if (store.type === "object" /* Object */) {
978
+ Object.values(store.properties).forEach((item) => {
979
+ traverseStore(item, callback);
980
+ });
981
+ } else if (store.type === "array" /* Array */) {
982
+ traverseStore(store.items, callback);
983
+ }
984
+ };
985
+
986
+ // src/lib/getForeignKeyPatchesFromSchema.ts
987
+ var getForeignKeyPatchesFromSchema = (store, options) => {
988
+ const stores = [];
989
+ traverseStore(store, (item) => {
990
+ if (item.type === "string" /* String */ && item.foreignKey === options.tableId) {
991
+ item.foreignKey = options.nextTableId;
992
+ const patch = {
993
+ op: "replace",
994
+ path: getPathByStore(item),
995
+ value: item.getPlainSchema()
996
+ };
997
+ stores.push(patch);
998
+ }
999
+ });
1000
+ return stores;
1001
+ };
1002
+
1003
+ // src/lib/getForeignKeysFromSchema.ts
1004
+ var getForeignKeysFromSchema = (store) => {
1005
+ const foreignKeys = /* @__PURE__ */ new Set();
1006
+ traverseStore(store, (item) => {
1007
+ if (item.type === "string" /* String */ && item.foreignKey) {
1008
+ foreignKeys.add(item.foreignKey);
1009
+ }
1010
+ });
1011
+ return [...foreignKeys].sort((a, b) => a.localeCompare(b));
1012
+ };
1013
+
1014
+ // src/lib/traverseValue.ts
1015
+ var traverseValue = (store, callback) => {
1016
+ callback(store);
1017
+ if (store.type === "object" /* Object */) {
1018
+ Object.values(store.value).forEach((item) => {
1019
+ traverseValue(item, callback);
1020
+ });
1021
+ } else if (store.type === "array" /* Array */) {
1022
+ store.value.forEach((itemValue) => {
1023
+ traverseValue(itemValue, callback);
1024
+ });
1025
+ }
1026
+ };
1027
+
1028
+ // src/lib/getForeignKeysFromValue.ts
1029
+ var getForeignKeysFromValue = (value) => {
1030
+ const foreignKeys = /* @__PURE__ */ new Map();
1031
+ traverseValue(value, (item) => {
1032
+ if (item.type === "string" /* String */ && item.foreignKey) {
1033
+ let tableForeignKey = foreignKeys.get(item.foreignKey);
1034
+ if (!tableForeignKey) {
1035
+ tableForeignKey = /* @__PURE__ */ new Set();
1036
+ foreignKeys.set(item.foreignKey, tableForeignKey);
1037
+ }
1038
+ tableForeignKey.add(item.getPlainValue());
1039
+ }
1040
+ });
1041
+ return [...foreignKeys].map(([tableId, rowIds]) => ({
1042
+ tableId,
1043
+ rowIds: [...rowIds].sort((a, b) => a.localeCompare(b))
1044
+ }));
1045
+ };
1046
+
1047
+ // src/lib/getInvalidFieldNamesInSchema.ts
1048
+ var getInvalidFieldNamesInSchema = (schema, refs = {}) => {
1049
+ const schemaStore = createJsonSchemaStore(schema, refs);
1050
+ const invalidFields = [];
1051
+ traverseStore(schemaStore, (item) => {
1052
+ if (item.parent?.type === "object" /* Object */) {
1053
+ if (!validateJsonFieldName(item.name)) {
1054
+ invalidFields.push(item);
1055
+ }
1056
+ }
1057
+ });
1058
+ return invalidFields;
1059
+ };
1060
+
1061
+ // src/lib/getJsonValueByPath.ts
1062
+ var getJsonValueStoreByPath = (root, path) => {
1063
+ if (!path) {
1064
+ return root;
1065
+ }
1066
+ const segments = getSegments(path);
1067
+ let current = root;
1068
+ for (const seg of segments) {
1069
+ if (current instanceof JsonObjectValueStore) {
1070
+ const next = current.value[String(seg)];
1071
+ if (!next) {
1072
+ throw new Error(`Path not found at segment "${seg}"`);
1073
+ }
1074
+ current = next;
1075
+ } else if (current instanceof JsonArrayValueStore) {
1076
+ if (typeof seg !== "number") {
1077
+ throw new Error(`Invalid array index "${seg}"`);
1078
+ }
1079
+ const next = current.value[seg];
1080
+ if (!next) {
1081
+ throw new Error(`Path not found at segment "${seg}"`);
1082
+ }
1083
+ current = next;
1084
+ } else {
1085
+ throw new Error(`Cannot navigate into primitive at segment "${seg}"`);
1086
+ }
1087
+ }
1088
+ return current;
1089
+ };
1090
+ var regex = /([^.[\]]+)|\[(\d+)]/g;
1091
+ var getSegments = (path) => {
1092
+ const segments = [];
1093
+ let match;
1094
+ while (match = regex.exec(path)) {
1095
+ if (match[1] !== void 0) {
1096
+ segments.push(match[1]);
1097
+ } else if (match[2] !== void 0) {
1098
+ segments.push(Number(match[2]));
1099
+ }
1100
+ }
1101
+ return segments;
1102
+ };
1103
+
1104
+ // src/lib/replaceForeignKeyValue.ts
1105
+ var replaceForeignKeyValue = (options) => {
1106
+ let wasUpdated = false;
1107
+ traverseValue(options.valueStore, (item) => {
1108
+ if (item.type === "string" /* String */ && item.foreignKey === options.foreignKey && item.value === options.value) {
1109
+ item.value = options.nextValue;
1110
+ wasUpdated = true;
1111
+ }
1112
+ });
1113
+ return wasUpdated;
1114
+ };
1115
+
1116
+ // src/plugins/row-id.schema.ts
1117
+ var rowIdSchema = {
1118
+ type: "string" /* String */,
1119
+ default: "",
1120
+ readOnly: true
1121
+ };
1122
+
1123
+ // src/plugins/row-version-id.schema.ts
1124
+ var rowVersionIdSchema = {
1125
+ type: "string" /* String */,
1126
+ default: "",
1127
+ readOnly: true
1128
+ };
1129
+
1130
+ // src/plugins/row-created-id.schema.ts
1131
+ var rowCreatedIdSchema = {
1132
+ type: "string" /* String */,
1133
+ default: "",
1134
+ readOnly: true
1135
+ };
1136
+
1137
+ // src/plugins/row-created-at.schema.ts
1138
+ var rowCreatedAtSchema = {
1139
+ type: "string" /* String */,
1140
+ default: "",
1141
+ readOnly: true
1142
+ };
1143
+
1144
+ // src/plugins/row-published-at.schema.ts
1145
+ var rowPublishedAtSchema = {
1146
+ type: "string" /* String */,
1147
+ default: ""
1148
+ };
1149
+
1150
+ // src/plugins/row-updated-at.schema.ts
1151
+ var rowUpdatedAtSchema = {
1152
+ type: "string" /* String */,
1153
+ default: "",
1154
+ readOnly: true
1155
+ };
1156
+
1157
+ // src/plugins/row-hash.schema.ts
1158
+ var rowHashSchema = {
1159
+ type: "string" /* String */,
1160
+ default: "",
1161
+ readOnly: true
1162
+ };
1163
+
1164
+ // src/plugins/row-schema-hash.schema.ts
1165
+ var rowSchemaHashSchema = {
1166
+ type: "string" /* String */,
1167
+ default: "",
1168
+ readOnly: true
1169
+ };
1170
+
1171
+ // src/plugins/file-schema.ts
1172
+ var fileSchema = {
1173
+ type: "object" /* Object */,
1174
+ properties: {
1175
+ status: { type: "string" /* String */, default: "", readOnly: true },
1176
+ fileId: { type: "string" /* String */, default: "", readOnly: true },
1177
+ url: { type: "string" /* String */, default: "", readOnly: true },
1178
+ fileName: { type: "string" /* String */, default: "" },
1179
+ hash: {
1180
+ type: "string" /* String */,
1181
+ default: "",
1182
+ readOnly: true
1183
+ },
1184
+ extension: {
1185
+ type: "string" /* String */,
1186
+ default: "",
1187
+ readOnly: true
1188
+ },
1189
+ mimeType: {
1190
+ type: "string" /* String */,
1191
+ default: "",
1192
+ readOnly: true
1193
+ },
1194
+ size: {
1195
+ type: "number" /* Number */,
1196
+ default: 0,
1197
+ readOnly: true
1198
+ },
1199
+ width: {
1200
+ type: "number" /* Number */,
1201
+ default: 0,
1202
+ readOnly: true
1203
+ },
1204
+ height: {
1205
+ type: "number" /* Number */,
1206
+ default: 0,
1207
+ readOnly: true
1208
+ }
1209
+ },
1210
+ required: [
1211
+ "status",
1212
+ "fileId",
1213
+ "url",
1214
+ "fileName",
1215
+ "hash",
1216
+ "extension",
1217
+ "mimeType",
1218
+ "size",
1219
+ "width",
1220
+ "height"
1221
+ ],
1222
+ additionalProperties: false
1223
+ };
1224
+
1225
+ // src/lib/resolveRefs.ts
1226
+ var pluginRefs = {
1227
+ ["urn:jsonschema:io:revisium:row-id-schema:1.0.0" /* RowId */]: rowIdSchema,
1228
+ ["urn:jsonschema:io:revisium:row-version-id-schema:1.0.0" /* RowVersionId */]: rowVersionIdSchema,
1229
+ ["urn:jsonschema:io:revisium:row-created-id-schema:1.0.0" /* RowCreatedId */]: rowCreatedIdSchema,
1230
+ ["urn:jsonschema:io:revisium:row-created-at-schema:1.0.0" /* RowCreatedAt */]: rowCreatedAtSchema,
1231
+ ["urn:jsonschema:io:revisium:row-published-at-schema:1.0.0" /* RowPublishedAt */]: rowPublishedAtSchema,
1232
+ ["urn:jsonschema:io:revisium:row-updated-at-schema:1.0.0" /* RowUpdatedAt */]: rowUpdatedAtSchema,
1233
+ ["urn:jsonschema:io:revisium:row-hash-schema:1.0.0" /* RowHash */]: rowHashSchema,
1234
+ ["urn:jsonschema:io:revisium:row-schema-hash-schema:1.0.0" /* RowSchemaHash */]: rowSchemaHashSchema,
1235
+ ["urn:jsonschema:io:revisium:file-schema:1.0.0" /* File */]: fileSchema
1236
+ };
1237
+ var resolveRefs = (schema) => {
1238
+ const store = createJsonSchemaStore(schema, pluginRefs);
1239
+ return store.getPlainSchema({ skip$Ref: true });
1240
+ };
1241
+
1242
+ // src/lib/schema-table.ts
1243
+ var SchemaTable = class {
1244
+ constructor(schema, refs = {}) {
1245
+ this.refs = refs;
1246
+ this.store = createJsonSchemaStore(schema, refs);
1247
+ }
1248
+ rows = /* @__PURE__ */ new Map();
1249
+ store;
1250
+ applyPatches(patches) {
1251
+ patches.forEach((patch) => {
1252
+ switch (patch.op) {
1253
+ case "replace": {
1254
+ const nextStore = applyReplacePatch(this.store, patch, this.refs);
1255
+ if (nextStore !== this.store) {
1256
+ this.migrateRows(nextStore);
1257
+ }
1258
+ break;
1259
+ }
1260
+ case "remove": {
1261
+ applyRemovePatch(this.store, patch);
1262
+ break;
1263
+ }
1264
+ case "add": {
1265
+ applyAddPatch(this.store, patch, this.refs);
1266
+ break;
1267
+ }
1268
+ case "move": {
1269
+ applyMovePatch(this.store, patch);
1270
+ break;
1271
+ }
1272
+ default:
1273
+ throw new Error(`Unsupported patch operation`);
1274
+ }
1275
+ });
1276
+ }
1277
+ getSchema() {
1278
+ return this.store.getPlainSchema();
1279
+ }
1280
+ addRow(rowId, data) {
1281
+ const row = createJsonValueStore(this.store, rowId, data);
1282
+ this.rows.set(rowId, row);
1283
+ }
1284
+ getRow(id) {
1285
+ const row = this.rows.get(id);
1286
+ if (!row) {
1287
+ throw new Error("Invalid id");
1288
+ }
1289
+ return row.getPlainValue();
1290
+ }
1291
+ getRows() {
1292
+ return [...this.rows].map(([id, data]) => ({
1293
+ id,
1294
+ data: data.getPlainValue()
1295
+ }));
1296
+ }
1297
+ migrateRows(nextStore) {
1298
+ const transformation = getTransformation(this.store, nextStore);
1299
+ if (transformation) {
1300
+ for (const [rowId, row] of this.rows) {
1301
+ const rawNextValue = transformation(
1302
+ row.getPlainValue(),
1303
+ nextStore.default
1304
+ );
1305
+ const nextRow = createJsonValueStore(nextStore, rowId, rawNextValue);
1306
+ this.rows.set(rowId, nextRow);
1307
+ }
1308
+ }
1309
+ this.store = nextStore;
1310
+ }
1311
+ };
1312
+
1313
+ export { SchemaTable, VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE, addSharedFieldsFromState, applyAddPatch, applyMovePatch, applyRemovePatch, applyReplacePatch, createJsonArrayValueStore, createJsonObjectSchemaStore, createJsonObjectValueStore, createJsonSchemaStore, createJsonValueStore, createPrimitiveStoreBySchema, createPrimitiveValueStore, getDBJsonPathByJsonSchemaStore, getForeignKeyPatchesFromSchema, getForeignKeysFromSchema, getForeignKeysFromValue, getInvalidFieldNamesInSchema, getJsonSchemaStoreByPath, getJsonValueStoreByPath, getParentForPath, getPathByStore, pluginRefs, replaceForeignKeyValue, resolveRefs, saveSharedFields, traverseStore, traverseValue, validateJsonFieldName };
1314
+ //# sourceMappingURL=index.js.map
1315
+ //# sourceMappingURL=index.js.map