-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutent.d.ts
713 lines (672 loc) · 17.5 KB
/
mutent.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/**
* Lazy value definition with optional argument.
*/
export type Lazy<T> = T | (() => T);
/**
* A nullish value (plus "void" because TS is a special baby).
*/
export type Nullish = null | undefined | void;
/**
* Sync or async iterable. Arrays are iterables.
*/
export type Many<T> = Iterable<T> | AsyncIterable<T>;
/**
* The value itself or wrapped around a `Promise`.
*/
export type One<T> = T | Promise<T>;
/**
* Make a type nullish-able (void is also nullish).
*/
export type Maybe<T> = T | Nullish;
/**
* Custom error (usable by plugins).
*/
export declare class MutentError extends Error {
/**
* Error's code.
*/
code: string;
/**
* Error extended info.
*/
info: any;
/**
* @constructor
* @param code Error identifier. All CAPS words and underscores.
* @param message Human readable message.
* @param info Detailed info object.
*/
constructor(code?: string, message?: string, info?: any);
/**
* Custom JSON serialization function.
*/
toJSON(): { error: string; message: string; info: any };
}
/**
* Represents an entity.
*/
export declare class Entity<T> {
/**
* Declares a new entity that should be created inside the datastore.
*/
static create<X>(data: X): Entity<X>;
/**
* Declares an entity that is already persisted.
*/
static read<X>(data: X): Entity<X>;
/**
* Contains the entity's data fetched from the datastore. A null value indicates that this entity is new.
*/
source: T | null;
/**
* Contains the last version of the entity after all mutations are applied.
*/
target: T;
/**
* Status flag.
*/
created: boolean;
/**
* Status flag.
*/
updated: boolean;
/**
* Status flag.
*/
deleted: boolean;
/**
* Returns true when this entity needs to be created inside the datastore.
*/
get shouldCreate(): boolean;
/**
* Returns true when this entity needs to be updated at the datastore.
*/
get shouldUpdate(): boolean;
/**
* Returns true when this entity needs to be deleted from the datastore.
*/
get shouldDelete(): boolean;
/**
* Returns true when this entity needs to be sync-ed with the datastore.
*/
get shouldCommit(): boolean;
/**
* Entity's metadata (raw data used by plugins).
*/
meta: Record<string, any>;
/**
* @constructor
*/
constructor(data: T);
/**
* Flags this entity as persisted and returns itself.
*/
commit(): this;
/**
* Flags this entity as to be deleted from the datastore.
*/
delete(): this;
/**
* Updates entity's data without altering its status flags.
*/
set(data: T): this;
/**
* Updates entity's data and flags It as not persisted anymore.
*/
update(data: T): this;
/**
* Returns the raw entity's value.
*/
valueOf(): T;
}
/**
* Mutation commit mode.
* - AUTO: Automatically commit all pending changes.
* - SAFE: Throw when an entity contains uncommited changes.
* - MANUAL: All entities must be manually commited.
*/
export type CommitMode = "AUTO" | "MANUAL" | "SAFE";
/**
* Current mutation's intent.
* - CREATE: This mutation was generated from the store#create method.
* - FIND: This mutation was generated from the store#find method.
* - READ: This mutation was generated from the store#read method.
* - FILTER: This mutation was generated from the store#filter method.
* - FROM: This mutation was generated from the store#from method.
*/
export type Intent = "CREATE" | "FIND" | "READ" | "FILTER" | "FROM";
/**
* Adapter write mode.
* - AUTO: Choose the appropriate write mode from the current context.
* - SEQUENTIAL: Sequential write (uses #create, #update, and #delete methods).
* - BULK: Force bulk write (uses #bulk method). Use "writeSize" option to configure how entities should be handled.
* - CONCURRENT: Send multiple writes concurrently (uses #create, #update, and #delete methods). Use "writeSize" option to configure how many writes.
*/
export type WriteMode = "AUTO" | "BULK" | "CONCURRENT" | "SEQUENTIAL";
/**
* Interface that contains all generics declared by the user.
*/
export interface Generics {
adapter?: unknown;
entity?: unknown;
query?: unknown;
options?: unknown;
}
export interface Hooks<G extends Generics> {
onFind: Array<QueryHook<G>>;
onFilter: Array<QueryHook<G>>;
onEntity: Array<EntityHook<G>>;
beforeCreate: Array<EntityHook<G>>;
beforeUpdate: Array<EntityHook<G>>;
beforeDelete: Array<EntityHook<G>>;
afterCreate: Array<EntityHook<G>>;
afterUpdate: Array<EntityHook<G>>;
afterDelete: Array<EntityHook<G>>;
}
/**
* Mutation context.
*/
export interface Context<G extends Generics> {
/**
* Used adapter.
*/
adapter: Adapter<G>;
/**
* The argument that has generated this mutation (store's method argument).
*/
argument: G["query"] | Lazy<One<Maybe<G["entity"]>>> | Many<G["entity"]>;
/**
* Current commit mode.
*/
commitMode: CommitMode;
/**
* Normalized hooks collection.
*/
hooks: Hooks<G>;
/**
* The intent that has generated this mutation (store method name).
*/
intent: Intent;
/**
* True when multiple entities can be retrieved.
*/
multiple: boolean;
/**
* An opaque value for the current execution.
*/
opaque?: unknown;
/**
* Adapter's options.
*/
options: UnwrapOptions<G>;
/**
* Store write mode.
*/
writeMode: WriteMode;
/**
* Store write size.
*/
writeSize: number;
}
/**
* Hook containing the adapter's query. Promises are supported.
*/
export type QueryHook<G extends Generics> = (
query: G["query"] | undefined,
ctx: Context<G>
) => any;
/**
* Hook containing the currently processed entity instance. Promises are supported.
*/
export type EntityHook<G extends Generics> = (
entity: Entity<G["entity"]>,
ctx: Context<G>
) => any;
/**
* Array of items or record where the keys are indexes.
*/
export type ArrayLike<T> = T[] | Record<string, T>;
/**
* Abstract datastore adapter.
*/
export interface Adapter<G extends Generics> {
/**
* Lower-level Adapter's representation.
*/
readonly raw: G["adapter"];
/**
* Find the first Entity matching the requested query.
* Can return a `Promise`.
*
* This method is the simplified version of `findEntity` method.
*/
find?(
query: G["query"] | undefined,
options: UnwrapOptions<G>
): One<Maybe<G["entity"]>>;
/**
* Find the Entities matching the requested query.
* Must return an interable or an async iterable.
*
* This method is the simplified version of `filterEntities` method.
*/
filter?(
query: G["query"] | undefined,
options: UnwrapOptions<G>
): Many<G["entity"]>;
/**
* Persists a new Entity inside the datastore.
* Can optionally return a new version of the Entity if the datastore adds other info (like an identifier).
* Promises are also supported.
*/
create?(
data: G["entity"],
options: UnwrapOptions<G>
): One<Maybe<G["entity"]>>;
/**
*
*/
update?(
oldData: G["entity"],
newData: G["entity"],
options: UnwrapOptions<G>
): One<Maybe<G["entity"]>>;
/**
*
*/
delete?(data: G["entity"], options: UnwrapOptions<G>): any;
/**
*
*/
bulk?(
actions: Array<BulkAction<G["entity"]>>,
options: UnwrapOptions<G>
): One<Maybe<ArrayLike<G["entity"]>>>;
/**
*
*/
findEntity?(
query: G["query"] | undefined,
ctx: Context<G>
): One<Maybe<G["entity"]>>;
/**
*
*/
filterEntities?(
query: G["query"] | undefined,
ctx: Context<G>
): Many<G["entity"]>;
/**
*
*/
createEntity?(entity: Entity<G["entity"]>, ctx: Context<G>): any;
/**
*
*/
updateEntity?(entity: Entity<G["entity"]>, ctx: Context<G>): any;
/**
*
*/
deleteEntity?(entity: Entity<G["entity"]>, ctx: Context<G>): any;
/**
*
*/
bulkEntities?(entities: Array<Entity<G["entity"]>>, ctx: Context<G>): any;
}
/**
* Bulk action descriptor.
*/
export type BulkAction<T> =
| BulkActionCreate<T>
| BulkActionUpdate<T>
| BulkActionDelete<T>;
/**
* Represents an entity creation intent.
*/
export interface BulkActionCreate<T> {
/**
* Bulk action type.
*/
type: "CREATE";
/**
* Raw data to create.
*/
data: T;
}
/**
* Represents an entity-update intent.
*/
export interface BulkActionUpdate<T> {
/**
* Bulk action type.
*/
type: "UPDATE";
/**
* Original entity data retrieved from the database.
*/
oldData: T;
/**
* Updated entity data after all actions are applied.
*/
newData: T;
}
/**
* Represents an entity deletion intent.
*/
export interface BulkActionDelete<T> {
/**
* Bulk action type.
*/
type: "DELETE";
/**
* Original entity data retrieved from the database.
*/
data: T;
}
/**
* Value or array of values.
*/
export type Values<T> = T | T[];
export interface MutentOptions<G extends Generics> {
/**
* Default commit mode.
* @default "AUTO"
*/
commitMode?: CommitMode;
/**
* Event hooks.
*/
hooks?: {
/**
* Triggered when a single entity is fetched from the datastore.
*/
onFind?: Values<QueryHook<G>>;
/**
* Triggered when multiple entities are fetched from the datastore.
*/
onFilter?: Values<QueryHook<G>>;
/**
* Triggered when an entity is ready to be processed by the mutation.
*/
onEntity?: Values<EntityHook<G>>;
/**
* Triggered before any entity creation.
*/
beforeCreate?: Values<EntityHook<G>>;
/**
* Triggered before any entity update.
*/
beforeUpdate?: Values<EntityHook<G>>;
/**
* Triggered before any entity deletion.
*/
beforeDelete?: Values<EntityHook<G>>;
/**
* Triggered after any entity creation.
*/
afterCreate?: Values<EntityHook<G>>;
/**
* Triggered after any entity update.
*/
afterUpdate?: Values<EntityHook<G>>;
/**
* Triggered after any entity deletion.
*/
afterDelete?: Values<EntityHook<G>>;
};
/**
* Mutators applied before anything else.
*/
mutators?: Array<Mutator<G>>;
/**
* Mutators applied after the full mutation chain is defined.
*/
handlers?: Array<Mutator<G>>;
/**
* An opaque value for the current execution.
*/
opaque?: unknown;
/**
* Default write mode.
* @default "AUTO"
*/
writeMode?: WriteMode;
/**
* Default write size (used by bulk or concurrent write modes).
* @default 16
*/
writeSize?: number;
}
/**
* Mutation's unwrap options.
*/
export type UnwrapOptions<G extends Generics> = Partial<G["options"]> & {
/**
* Override Store's options.
*/
mutent?: MutentOptions<G>;
};
/**
* A function that takes a entity (async) iterable as input, and returns a new entity (async) iterable.
*/
export type Mutator<G extends Generics> = (
iterable: AsyncIterable<Entity<G["entity"]>>,
ctx: Context<G>
) => AsyncIterable<Entity<G["entity"]>>;
export interface Mutation<G extends Generics, U = unknown> {
/**
* Change the behavior of `.pipe` method.
* If `true`, the Mutation will change at any mutator applied.
*/
mutable: boolean;
/**
* Runs Object.assign() against all entities.
* @param objects
*/
assign(...objects: Array<Partial<G["entity"]>>): Mutation<G, U>;
/**
* Commits (writes) all necessary entities.
*/
commit(): Mutation<G, U>;
/**
* Applies configured mutators to the targeted entities and returns a Promise that will resolve to the number of handled entities.
*/
consume(options?: UnwrapOptions<G>): Promise<number>;
/**
* Deletes all entities.
*/
delete(
predicate?: (data: G["entity"], index: number) => any
): Mutation<G, U>;
/**
* Creates an entity with this data if there are no other matches.
*/
ensure(one: One<G["entity"]>): Mutation<G, U>;
/**
* Ignores the entities that don't satisfy the predicate.
* @param predicate A function that accepts the current entity and its index. If it returns true, the current entity is kept.
*/
filter(
predicate: (data: G["entity"], index: number) => any
): Mutation<G, U>;
/**
* Applies configured mutators to the targeted entities and returns an entities (async) iterable.
* @param options Adapter specific options.
*/
iterate(options?: UnwrapOptions<G>): AsyncIterable<G["entity"]>;
/**
* Limit the total number of returned documents.
*/
limit(n: number): Mutation<G, U>;
/**
* Adds mutators to the current ones and returns a new Mutation instance.
* @param mutators Mutators chain.
*/
pipe(...mutators: Array<Mutator<G>>): Mutation<G, U>;
/**
* Skip (ignore) the first N documents.
*/
skip(n: number): Mutation<G, U>;
/**
* Performs a side-effect against all entities.
* @param callback A function that accepts the current entity and its index. Promises are supported.
*/
tap(callback: (data: G["entity"], index: number) => any): Mutation<G, U>;
/**
* Applies configured mutators to the targeted entities and returns a Promise containing the result.
* @param options Adapter specific options.
*/
unwrap(options?: UnwrapOptions<G>): Promise<U>;
/**
* Updates entities.
* @param mapper A function that accepts the current entity and its index. Must return a new object representing the updated entity. Promises are supported. A nullish result will skip the update.
*/
update(
mapper: (data: G["entity"], index: number) => One<Maybe<G["entity"]>>
): Mutation<G, U>;
}
/**
* This mutations's unwrap may result in an entity.
*/
export type MutationNullable<G extends Generics> = Mutation<
G,
G["entity"] | null
>;
/**
* This mutation's unwrap will always result in an entity.
*/
export type MutationSingle<G extends Generics> = Mutation<G, G["entity"]>;
/**
* This mutation's unwrap will result in an array of entities.
*/
export type MutationMultiple<G extends Generics> = Mutation<
G,
Array<G["entity"]>
>;
/**
* Store's constructor options.
*/
export interface StoreOptions<G extends Generics> extends MutentOptions<G> {
/**
* Adapter definition.
*/
adapter: Adapter<G>;
/**
* Make all Mutations mutable.
*/
mutable?: boolean;
/**
* Plugins to apply to this store.
*/
plugins?: Array<MutentOptions<G>>;
}
export declare class Store<G extends Generics> implements StoreOptions<G> {
adapter: Adapter<G>;
commitMode: CommitMode;
handlers: Array<Mutator<G>>;
hooks: Hooks<G>;
mutable: boolean;
mutators: Array<Mutator<G>>;
writeMode: WriteMode;
writeSize: number;
/**
* Getter pointing to Adapter's `raw` property.
*/
get raw(): G["adapter"];
/**
* @constructor
*/
constructor(options: StoreOptions<G>);
/**
* Declares one or many new entities.
*/
create(many: Many<G["entity"]>): MutationMultiple<G>;
create(one: Lazy<One<G["entity"]>>): MutationSingle<G>;
create(oneMaybe: Lazy<One<Maybe<G["entity"]>>>): MutationNullable<G>;
/**
* Declares one entity that matches the query and could exist.
*/
find(query?: G["query"]): MutationNullable<G>;
/**
* Declares one required entity that matches the query.
*/
read(query?: G["query"]): MutationSingle<G>;
/**
* Declares many existing entities that match the query.
*/
filter(query?: G["query"]): MutationMultiple<G>;
/**
* Declares one or many existing entities.
*/
from(many: Many<G["entity"]>): MutationMultiple<G>;
from(one: Lazy<One<G["entity"]>>): MutationSingle<G>;
from(oneMaybe: Lazy<One<Maybe<G["entity"]>>>): MutationNullable<G>;
/**
* Merge current options with new options.
*/
register(plugin: MutentOptions<G>): this;
}
/**
* Runs Object.assign() against all entities.
* @param objects
*/
export declare function assign<G extends Generics>(
...objects: Array<Partial<G["entity"]>>
): Mutator<G>;
/**
* Commits (writes) all necessary entities.
*/
export declare function commit(): Mutator<any>;
/**
* Deletes all entities.
*/
export declare function ddelete(): Mutator<any>;
export declare function ddelete<G extends Generics>(
predicate: (data: G["entity"], index: number) => any
): Mutator<G>;
/**
* Ignores the entities that don't satisfy the predicate.
* @param predicate A function that accepts the current entity and its index. If it returns true, the current entity is kept.
*/
export declare function filter<G extends Generics>(
predicate: (data: G["entity"], index: number) => any
): Mutator<G>;
/**
* Reduces multiple mutators into a single one.
* @param mutators Mutators chain to reduce.
*/
export declare function pipe<G extends Generics>(
...mutators: Array<Mutator<G>>
): Mutator<G>;
/**
* Performs a side-effect against all entities.
* @param callback A function that accepts the current entity and its index. Promises are supported.
*/
export declare function tap<G extends Generics>(
callback: (data: G["entity"], index: number) => any
): Mutator<G>;
/**
* Updates entities.
* @param mapper A function that accepts the current entity and its index. Must return a new object representing the updated entity. Promises are supported. A nullish result will skip the update.
*/
export declare function update<G extends Generics>(
mapper: (data: G["entity"], index: number) => One<Maybe<G["entity"]>>
): Mutator<G>;
/**
* Creates an entity with this data if there are no other matches.
*/
export declare function ensure<G extends Generics>(
one: Lazy<One<G["entity"]>>
): Mutator<G>;
/**
* Get internal Adapter's name, otherwise returns `"Unknown Adapter"`.
*/
export declare function getAdapterName(obj: unknown): string;
/**
* Returns a mutator that limits the total amount of documents returned by the Mutation.
*/
export declare function limit(n: number): Mutator<any>;
/**
* Returns a mutator that skip the first `n` found documents.
*/
export declare function skip(n: number): Mutator<any>;