@arcote.tech/arc 0.0.24 → 0.0.26

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.
@@ -1,5 +1,5 @@
1
1
  import { ArcContextElement } from "../context/element";
2
- import type { ArcIdAny } from "../elements/id";
2
+ import { type ArcIdAny } from "../elements/id";
3
3
  import { type ArcObjectAny } from "../elements/object";
4
4
  import { objectUtil, type DeepPartial, type util } from "../utils";
5
5
  import type { DataStorage } from "../data-storage";
@@ -74,7 +74,7 @@ export declare class ArcIndexedCollection<Name extends string, Id extends ArcIdA
74
74
  readonly indexes: I;
75
75
  constructor(name: Name, id: Id, schema: Schema, options: ArcCollectionOptions<Id, Schema>, indexes: I);
76
76
  commandContext(dataStorage: DataStorage, publishEvent: (event: this["$event"]) => Promise<void>): CollectionCommandContext<Id, Schema> & {
77
- [func in keyof I]: (args: IndexValue<Schema, Indexes, I, func>) => Deserialize<Id, Schema>[];
77
+ [func in keyof I]: (args: objectUtil.simplify<IndexQueryArgument<objectUtil.simplify<IndexValue<Schema, Indexes, I, func>>>>) => Deserialize<Id, Schema>[];
78
78
  };
79
79
  queryBuilder(): CollectionQueryBuilder<ArcCollection<Name, Id, Schema>> & {
80
80
  [func in keyof I]: (args: objectUtil.simplify<IndexQueryArgument<objectUtil.simplify<IndexValue<Schema, Indexes, I, func>>>>) => ArcIndexedItemsQueryBuilder<ArcIndexedCollection<Name, Id, Schema, Indexes, I>>;
@@ -1,10 +1,16 @@
1
1
  import type { util } from "../utils";
2
2
  import { ArcBranded } from "./branded";
3
3
  import { ArcString } from "./string";
4
+ export declare class ArcCustomId<Brand extends string | symbol, CreateFn extends (...args: any[]) => string> extends ArcBranded<ArcString, Brand> {
5
+ private createFn;
6
+ constructor(name: Brand, createFn: CreateFn);
7
+ get(...args: Parameters<CreateFn>): util.GetType<this>;
8
+ }
4
9
  export declare class ArcId<Brand extends string | symbol> extends ArcBranded<ArcString, Brand> {
5
10
  constructor(name: Brand);
6
11
  generate(): util.GetType<this>;
7
12
  }
8
- export type ArcIdAny = ArcId<any>;
13
+ export type ArcIdAny = ArcId<any> | ArcCustomId<any, any>;
9
14
  export declare function id<Brand extends string | symbol>(name: Brand): ArcId<Brand>;
15
+ export declare function customId<Brand extends string | symbol, CreateFn extends (...args: any[]) => string>(name: Brand, createFn: CreateFn): ArcCustomId<Brand, CreateFn>;
10
16
  //# sourceMappingURL=id.d.ts.map
package/dist/index.js CHANGED
@@ -3,6 +3,146 @@ class ArcContextElement {
3
3
  $event;
4
4
  }
5
5
 
6
+ // elements/optional.ts
7
+ class ArcOptional {
8
+ parent;
9
+ constructor(parent) {
10
+ this.parent = parent;
11
+ }
12
+ parse(value) {
13
+ if (!value)
14
+ return null;
15
+ return this.parent.parse(value);
16
+ }
17
+ serialize(value) {
18
+ if (value)
19
+ return this.parent.serialize(value);
20
+ return null;
21
+ }
22
+ deserialize(value) {
23
+ if (!value)
24
+ return null;
25
+ return this.parent.deserialize(value);
26
+ }
27
+ }
28
+
29
+ // elements/branded.ts
30
+ class ArcBranded {
31
+ parent;
32
+ brand;
33
+ constructor(parent, brand) {
34
+ this.parent = parent;
35
+ this.brand = brand;
36
+ }
37
+ serialize(value) {
38
+ return this.parent.serialize(value);
39
+ }
40
+ deserialize(value) {
41
+ return this.parent.deserialize(value);
42
+ }
43
+ parse(value) {
44
+ return this.parent.parse(value);
45
+ }
46
+ optional() {
47
+ return new ArcOptional(this);
48
+ }
49
+ }
50
+
51
+ // elements/default.ts
52
+ class ArcDefault {
53
+ parent;
54
+ defaultValueOrCallback;
55
+ constructor(parent, defaultValueOrCallback) {
56
+ this.parent = parent;
57
+ this.defaultValueOrCallback = defaultValueOrCallback;
58
+ }
59
+ parse(value) {
60
+ if (value)
61
+ return this.parent.parse(value);
62
+ if (typeof this.defaultValueOrCallback === "function") {
63
+ return this.defaultValueOrCallback();
64
+ } else
65
+ return this.defaultValueOrCallback;
66
+ }
67
+ serialize(value) {
68
+ return this.parent.serialize(value);
69
+ }
70
+ deserialize(value) {
71
+ if (value)
72
+ return this.parent.deserialize(value);
73
+ if (typeof this.defaultValueOrCallback === "function") {
74
+ return this.defaultValueOrCallback();
75
+ } else
76
+ return this.defaultValueOrCallback;
77
+ }
78
+ }
79
+
80
+ // elements/abstract.ts
81
+ class ArcAbstract {
82
+ default(defaultValueOrCallback) {
83
+ return new ArcDefault(this, defaultValueOrCallback);
84
+ }
85
+ optional() {
86
+ return new ArcOptional(this);
87
+ }
88
+ branded(name) {
89
+ return new ArcBranded(this, name);
90
+ }
91
+ }
92
+
93
+ // elements/abstract-primitive.ts
94
+ class ArcPrimitive extends ArcAbstract {
95
+ constructor() {
96
+ super();
97
+ }
98
+ serialize(value) {
99
+ return value;
100
+ }
101
+ parse(value) {
102
+ return value;
103
+ }
104
+ deserialize(value) {
105
+ return value;
106
+ }
107
+ }
108
+
109
+ // elements/string.ts
110
+ class ArcString extends ArcPrimitive {
111
+ }
112
+ function string() {
113
+ return new ArcString;
114
+ }
115
+
116
+ // elements/id.ts
117
+ class ArcCustomId extends ArcBranded {
118
+ createFn;
119
+ constructor(name, createFn) {
120
+ super(string(), name);
121
+ this.createFn = createFn;
122
+ }
123
+ get(...args) {
124
+ return this.createFn(...args);
125
+ }
126
+ }
127
+
128
+ class ArcId extends ArcBranded {
129
+ constructor(name) {
130
+ super(string(), name);
131
+ }
132
+ generate() {
133
+ var timestamp = (new Date().getTime() / 1000 | 0).toString(16);
134
+ return timestamp + "xxxxxxxxxxxxxxxx".replace(/[x]/g, function() {
135
+ return (Math.random() * 16 | 0).toString(16);
136
+ }).toLowerCase();
137
+ }
138
+ }
139
+ function id(name) {
140
+ return new ArcId(name);
141
+ }
142
+ function customId(name, createFn) {
143
+ return new ArcCustomId(name, createFn);
144
+ }
145
+
6
146
  // context/query.ts
7
147
  class ArcQuery {
8
148
  lastResult;
@@ -54,8 +194,8 @@ class QueryCollectionResult {
54
194
  constructor(result) {
55
195
  this.result = result;
56
196
  }
57
- get(id) {
58
- return id ? this.result.find((r) => r._id === id) : undefined;
197
+ get(id2) {
198
+ return id2 ? this.result.find((r) => r._id === id2) : undefined;
59
199
  }
60
200
  map(callbackfn) {
61
201
  return this.result.map(callbackfn);
@@ -233,10 +373,10 @@ class ArcIndexedItemsQueryBuilder extends ArcManyItemsQueryBuilder {
233
373
 
234
374
  // collection/queries/one-item.ts
235
375
  class ArcOneItemQuery extends ArcCollectionQuery {
236
- id;
237
- constructor(collection, id) {
376
+ id2;
377
+ constructor(collection, id2) {
238
378
  super(collection);
239
- this.id = id;
379
+ this.id = id2;
240
380
  }
241
381
  onChange(change) {
242
382
  if (change.id !== this.id)
@@ -256,11 +396,11 @@ class ArcOneItemQuery extends ArcCollectionQuery {
256
396
  // collection/query-builders/one-item.ts
257
397
  class ArcOneItemQueryBuilder extends ArcQueryBuilder {
258
398
  collection;
259
- id;
260
- constructor(collection, id) {
399
+ id2;
400
+ constructor(collection, id2) {
261
401
  super();
262
402
  this.collection = collection;
263
- this.id = id;
403
+ this.id = id2;
264
404
  }
265
405
  toQuery() {
266
406
  return new ArcOneItemQuery(this.collection, this.id);
@@ -270,13 +410,13 @@ class ArcOneItemQueryBuilder extends ArcQueryBuilder {
270
410
  // collection/collection.ts
271
411
  class ArcCollection extends ArcContextElement {
272
412
  name;
273
- id;
413
+ id2;
274
414
  schema;
275
415
  options;
276
- constructor(name, id, schema, options) {
416
+ constructor(name, id2, schema, options) {
277
417
  super();
278
418
  this.name = name;
279
- this.id = id;
419
+ this.id = id2;
280
420
  this.schema = schema;
281
421
  this.options = options;
282
422
  }
@@ -295,17 +435,19 @@ class ArcCollection extends ArcContextElement {
295
435
  queryBuilder() {
296
436
  return {
297
437
  all: () => new ArcAllItemsQueryBuilder(this),
298
- one: (id) => new ArcOneItemQueryBuilder(this, id)
438
+ one: (id2) => new ArcOneItemQueryBuilder(this, id2)
299
439
  };
300
440
  }
301
441
  commandContext(dataStorage, publishEvent) {
302
442
  const store = dataStorage.getStore(this.name);
303
443
  return {
304
444
  add: async (data) => {
305
- const id = this.id.generate();
445
+ if (this.id instanceof ArcCustomId)
446
+ throw new Error("Collection with custom id not support 'add' method");
447
+ const id2 = this.id.generate();
306
448
  const parsed = this.schema.parse(data);
307
449
  const body = {
308
- _id: id,
450
+ _id: id2,
309
451
  ...parsed
310
452
  };
311
453
  await store.set(body);
@@ -313,16 +455,16 @@ class ArcCollection extends ArcContextElement {
313
455
  type: "set",
314
456
  to: body
315
457
  });
316
- return { id };
458
+ return { id: id2 };
317
459
  },
318
- remove: async (id) => {
319
- await store.remove(id);
460
+ remove: async (id2) => {
461
+ await store.remove(id2);
320
462
  return { success: true };
321
463
  },
322
- set: async (id, data) => {
464
+ set: async (id2, data) => {
323
465
  const parsed = this.schema.parse(data);
324
466
  const body = {
325
- _id: id,
467
+ _id: id2,
326
468
  ...parsed
327
469
  };
328
470
  await store.set(body);
@@ -335,12 +477,12 @@ class ArcCollection extends ArcContextElement {
335
477
  all: async () => {
336
478
  return store.findAll();
337
479
  },
338
- one: async (id) => {
339
- return store.findById(id);
480
+ one: async (id2) => {
481
+ return store.findById(id2);
340
482
  },
341
- modify: async (id, data) => {
483
+ modify: async (id2, data) => {
342
484
  const deserialized = this.schema.serializePartial(data);
343
- const { from, to } = await store.modify(id, deserialized);
485
+ const { from, to } = await store.modify(id2, deserialized);
344
486
  await publishEvent({
345
487
  type: "modify",
346
488
  changes: deserialized,
@@ -348,8 +490,8 @@ class ArcCollection extends ArcContextElement {
348
490
  to
349
491
  });
350
492
  },
351
- edit: async (id, editCallback) => {
352
- const { from, to } = await store.mutate(id, editCallback);
493
+ edit: async (id2, editCallback) => {
494
+ const { from, to } = await store.mutate(id2, editCallback);
353
495
  await publishEvent({
354
496
  type: "mutate",
355
497
  from,
@@ -365,8 +507,8 @@ class ArcCollection extends ArcContextElement {
365
507
 
366
508
  class ArcIndexedCollection extends ArcCollection {
367
509
  indexes;
368
- constructor(name, id, schema, options, indexes) {
369
- super(name, id, schema, options);
510
+ constructor(name, id2, schema, options, indexes) {
511
+ super(name, id2, schema, options);
370
512
  this.indexes = indexes;
371
513
  }
372
514
  commandContext(dataStorage, publishEvent) {
@@ -397,8 +539,8 @@ class ArcIndexedCollection extends ArcCollection {
397
539
  });
398
540
  }
399
541
  }
400
- function collection(name, id, schema, options = {}) {
401
- return new ArcCollection(name, id, schema, options);
542
+ function collection(name, id2, schema, options = {}) {
543
+ return new ArcCollection(name, id2, schema, options);
402
544
  }
403
545
  // context/context.ts
404
546
  class ArcContext {
@@ -412,16 +554,16 @@ class ArcContext {
412
554
  this.elements = elements;
413
555
  this.commands = commands;
414
556
  this.listeners = listeners;
415
- this.elementsMap = Object.fromEntries(elements.map((collection3) => [collection3.name, collection3]));
557
+ this.elementsMap = Object.fromEntries(elements.map((element) => [element.name, element]));
416
558
  }
417
559
  queryBuilder() {
418
560
  return new Proxy({}, {
419
561
  get: (target, name) => {
420
- const collection3 = this.elementsMap[name];
421
- if (!collection3) {
562
+ const element = this.elementsMap[name];
563
+ if (!element) {
422
564
  throw new Error(`Collection "${name}" not found`);
423
565
  }
424
- return collection3.queryBuilder();
566
+ return element.queryBuilder();
425
567
  }
426
568
  });
427
569
  }
@@ -522,30 +664,30 @@ class StoreState {
522
664
  };
523
665
  await this.applyChanges([change]);
524
666
  }
525
- async remove(id) {
667
+ async remove(id2) {
526
668
  const change = {
527
669
  type: "delete",
528
- id
670
+ id: id2
529
671
  };
530
672
  await this.applyChanges([change]);
531
673
  }
532
- async modify(id, data) {
674
+ async modify(id2, data) {
533
675
  const change = {
534
676
  type: "modify",
535
- id,
677
+ id: id2,
536
678
  data
537
679
  };
538
680
  const { from, to } = await this.applyChange(change);
539
681
  return { from, to };
540
682
  }
541
- async mutate(id, editCallback) {
542
- const object = await this.findById(id);
683
+ async mutate(id2, editCallback) {
684
+ const object = await this.findById(id2);
543
685
  const [draft, finalize] = create(object || {}, { enablePatches: true });
544
686
  await editCallback(draft);
545
687
  const [_, patches] = finalize();
546
688
  const change = {
547
689
  type: "mutate",
548
- id,
690
+ id: id2,
549
691
  patches
550
692
  };
551
693
  const { from, to } = await this.applyChange(change);
@@ -646,12 +788,12 @@ class ForkedStoreState extends StoreState {
646
788
  this.notifyListeners(events);
647
789
  }
648
790
  }
649
- async findById(id, listener) {
791
+ async findById(id2, listener) {
650
792
  if (listener)
651
- this.listeners.set(listener, { callback: listener, id });
652
- if (this.changedItems.has(id))
653
- return this.changedItems.get(id);
654
- return await this.master.findById(id);
793
+ this.listeners.set(listener, { callback: listener, id: id2 });
794
+ if (this.changedItems.has(id2))
795
+ return this.changedItems.get(id2);
796
+ return await this.master.findById(id2);
655
797
  }
656
798
  async findByIndex(index, data, listener) {
657
799
  if (listener)
@@ -659,16 +801,16 @@ class ForkedStoreState extends StoreState {
659
801
  const parentResult = await this.master.findByIndex(index, data);
660
802
  const results = new Map;
661
803
  parentResult.forEach((item) => results.set(item._id, item));
662
- for (const [id, changedItem] of this.changedItems) {
804
+ for (const [id2, changedItem] of this.changedItems) {
663
805
  if (changedItem === null) {
664
- results.delete(id);
806
+ results.delete(id2);
665
807
  continue;
666
808
  }
667
809
  const matches = Object.entries(data).every(([key, value]) => changedItem[key] === value);
668
810
  if (matches) {
669
- results.set(id, changedItem);
811
+ results.set(id2, changedItem);
670
812
  } else {
671
- results.delete(id);
813
+ results.delete(id2);
672
814
  }
673
815
  }
674
816
  return Array.from(results.values());
@@ -678,9 +820,9 @@ class ForkedStoreState extends StoreState {
678
820
  this.listeners.set(listener, { callback: listener });
679
821
  const parentResult = await this.master.findAll();
680
822
  return parentResult.map((item) => {
681
- const id = item._id;
682
- if (this.changedItems.has(id))
683
- return this.changedItems.get(id);
823
+ const id2 = item._id;
824
+ if (this.changedItems.has(id2))
825
+ return this.changedItems.get(id2);
684
826
  return item;
685
827
  });
686
828
  }
@@ -808,19 +950,19 @@ class MasterStoreState extends StoreState {
808
950
  this.notifyListeners(events);
809
951
  }
810
952
  }
811
- async findById(id, listener) {
812
- if (!id)
953
+ async findById(id2, listener) {
954
+ if (!id2)
813
955
  return;
814
956
  if (listener)
815
- this.listeners.set(listener, { callback: listener, id });
816
- if (this.items.has(id))
817
- return this.items.get(id);
957
+ this.listeners.set(listener, { callback: listener, id: id2 });
958
+ if (this.items.has(id2))
959
+ return this.items.get(id2);
818
960
  const transaction = await this.dataStorage.getReadTransaction();
819
- const result = await transaction.findById(this.storeName, id);
961
+ const result = await transaction.findById(this.storeName, id2);
820
962
  const item = result && this.deserialize ? this.deserialize(result) : result;
821
963
  if (!item)
822
964
  return;
823
- this.items.set(id, item);
965
+ this.items.set(id2, item);
824
966
  return item;
825
967
  }
826
968
  async findByIndex(index, data, listener) {
@@ -837,9 +979,9 @@ class MasterStoreState extends StoreState {
837
979
  const transaction = await this.dataStorage.getReadTransaction();
838
980
  const dbResults = await transaction.findByIndex(this.storeName, index, data);
839
981
  const results = dbResults.map((item) => {
840
- const id = item._id;
841
- if (this.items.has(id))
842
- return this.items.get(id);
982
+ const id2 = item._id;
983
+ if (this.items.has(id2))
984
+ return this.items.get(id2);
843
985
  const processedItem = this.deserialize ? this.deserialize(item) : item;
844
986
  return processedItem;
845
987
  });
@@ -853,9 +995,9 @@ class MasterStoreState extends StoreState {
853
995
  const transaction = await this.dataStorage.getReadTransaction();
854
996
  const dbResults = await transaction.findAll(this.storeName);
855
997
  const items = dbResults.map((item) => {
856
- const id = item._id;
857
- if (this.items.has(id))
858
- return this.items.get(id);
998
+ const id2 = item._id;
999
+ if (this.items.has(id2))
1000
+ return this.items.get(id2);
859
1001
  const processedItem = this.deserialize ? this.deserialize(item) : item;
860
1002
  this.items.set(processedItem._id, processedItem);
861
1003
  return processedItem;
@@ -908,93 +1050,6 @@ class MasterDataStorage extends DataStorage {
908
1050
  await this.rtcAdapter.sync(progressCallback);
909
1051
  }
910
1052
  }
911
- // elements/optional.ts
912
- class ArcOptional {
913
- parent;
914
- constructor(parent) {
915
- this.parent = parent;
916
- }
917
- parse(value) {
918
- if (!value)
919
- return null;
920
- return this.parent.parse(value);
921
- }
922
- serialize(value) {
923
- if (value)
924
- return this.parent.serialize(value);
925
- return null;
926
- }
927
- deserialize(value) {
928
- if (!value)
929
- return null;
930
- return this.parent.deserialize(value);
931
- }
932
- }
933
-
934
- // elements/branded.ts
935
- class ArcBranded {
936
- parent;
937
- brand;
938
- constructor(parent, brand) {
939
- this.parent = parent;
940
- this.brand = brand;
941
- }
942
- serialize(value) {
943
- return this.parent.serialize(value);
944
- }
945
- deserialize(value) {
946
- return this.parent.deserialize(value);
947
- }
948
- parse(value) {
949
- return this.parent.parse(value);
950
- }
951
- optional() {
952
- return new ArcOptional(this);
953
- }
954
- }
955
-
956
- // elements/default.ts
957
- class ArcDefault {
958
- parent;
959
- defaultValueOrCallback;
960
- constructor(parent, defaultValueOrCallback) {
961
- this.parent = parent;
962
- this.defaultValueOrCallback = defaultValueOrCallback;
963
- }
964
- parse(value) {
965
- if (value)
966
- return this.parent.parse(value);
967
- if (typeof this.defaultValueOrCallback === "function") {
968
- return this.defaultValueOrCallback();
969
- } else
970
- return this.defaultValueOrCallback;
971
- }
972
- serialize(value) {
973
- return this.parent.serialize(value);
974
- }
975
- deserialize(value) {
976
- if (value)
977
- return this.parent.deserialize(value);
978
- if (typeof this.defaultValueOrCallback === "function") {
979
- return this.defaultValueOrCallback();
980
- } else
981
- return this.defaultValueOrCallback;
982
- }
983
- }
984
-
985
- // elements/abstract.ts
986
- class ArcAbstract {
987
- default(defaultValueOrCallback) {
988
- return new ArcDefault(this, defaultValueOrCallback);
989
- }
990
- optional() {
991
- return new ArcOptional(this);
992
- }
993
- branded(name) {
994
- return new ArcBranded(this, name);
995
- }
996
- }
997
-
998
1053
  // elements/object.ts
999
1054
  class ArcObject extends ArcAbstract {
1000
1055
  rawShape;
@@ -1091,22 +1146,6 @@ class ArcArray extends ArcAbstract {
1091
1146
  function array(element) {
1092
1147
  return new ArcArray(element);
1093
1148
  }
1094
- // elements/abstract-primitive.ts
1095
- class ArcPrimitive extends ArcAbstract {
1096
- constructor() {
1097
- super();
1098
- }
1099
- serialize(value) {
1100
- return value;
1101
- }
1102
- parse(value) {
1103
- return value;
1104
- }
1105
- deserialize(value) {
1106
- return value;
1107
- }
1108
- }
1109
-
1110
1149
  // elements/boolean.ts
1111
1150
  class ArcBoolean extends ArcPrimitive {
1112
1151
  }
@@ -1131,28 +1170,6 @@ class ArcDate extends ArcAbstract {
1131
1170
  function date() {
1132
1171
  return new ArcDate;
1133
1172
  }
1134
- // elements/string.ts
1135
- class ArcString extends ArcPrimitive {
1136
- }
1137
- function string() {
1138
- return new ArcString;
1139
- }
1140
-
1141
- // elements/id.ts
1142
- class ArcId extends ArcBranded {
1143
- constructor(name) {
1144
- super(string(), name);
1145
- }
1146
- generate() {
1147
- var timestamp = (new Date().getTime() / 1000 | 0).toString(16);
1148
- return timestamp + "xxxxxxxxxxxxxxxx".replace(/[x]/g, function() {
1149
- return (Math.random() * 16 | 0).toString(16);
1150
- }).toLowerCase();
1151
- }
1152
- }
1153
- function id(name) {
1154
- return new ArcId(name);
1155
- }
1156
1173
  // elements/number.ts
1157
1174
  class ArcNumber extends ArcPrimitive {
1158
1175
  }
@@ -1434,6 +1451,7 @@ export {
1434
1451
  number,
1435
1452
  id,
1436
1453
  date,
1454
+ customId,
1437
1455
  context,
1438
1456
  collection,
1439
1457
  boolean,
@@ -1456,6 +1474,7 @@ export {
1456
1474
  ArcIndexedCollection,
1457
1475
  ArcId,
1458
1476
  ArcDate,
1477
+ ArcCustomId,
1459
1478
  ArcCollectionQuery,
1460
1479
  ArcCollection,
1461
1480
  ArcBranded,
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
7
- "version": "0.0.24",
7
+ "version": "0.0.26",
8
8
  "private": false,
9
9
  "author": "Przemysław Krasiński [arcote.tech]",
10
10
  "description": "Arc is a framework designed to align code closely with business logic, streamlining development and enhancing productivity.",