@verdant-web/common 1.11.0 → 1.11.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 (141) hide show
  1. package/dist/cjs/EventSubscriber.d.ts +17 -17
  2. package/dist/cjs/EventSubscriber.js +66 -66
  3. package/dist/cjs/baseline.d.ts +5 -5
  4. package/dist/cjs/baseline.js +2 -2
  5. package/dist/cjs/batching.d.ts +41 -41
  6. package/dist/cjs/batching.js +88 -88
  7. package/dist/cjs/files.d.ts +19 -19
  8. package/dist/cjs/files.js +18 -18
  9. package/dist/cjs/index.d.ts +18 -18
  10. package/dist/cjs/index.js +35 -35
  11. package/dist/cjs/indexes.d.ts +14 -14
  12. package/dist/cjs/indexes.js +115 -115
  13. package/dist/cjs/indexes.test.d.ts +1 -1
  14. package/dist/cjs/indexes.test.js +127 -127
  15. package/dist/cjs/migration.d.ts +98 -98
  16. package/dist/cjs/migration.js +127 -127
  17. package/dist/cjs/oids.d.ts +87 -87
  18. package/dist/cjs/oids.js +374 -374
  19. package/dist/cjs/oids.test.d.ts +1 -1
  20. package/dist/cjs/oids.test.js +245 -245
  21. package/dist/cjs/operation.d.ts +139 -139
  22. package/dist/cjs/operation.js +532 -532
  23. package/dist/cjs/operation.test.d.ts +1 -1
  24. package/dist/cjs/operation.test.js +484 -484
  25. package/dist/cjs/patch.d.ts +26 -26
  26. package/dist/cjs/patch.js +226 -226
  27. package/dist/cjs/presence.d.ts +27 -27
  28. package/dist/cjs/presence.js +2 -2
  29. package/dist/cjs/protocol.d.ts +145 -145
  30. package/dist/cjs/protocol.js +3 -3
  31. package/dist/cjs/refs.d.ts +5 -5
  32. package/dist/cjs/refs.js +21 -21
  33. package/dist/cjs/replica.d.ts +28 -28
  34. package/dist/cjs/replica.js +28 -28
  35. package/dist/cjs/schema/defaults.d.ts +4 -4
  36. package/dist/cjs/schema/defaults.js +71 -71
  37. package/dist/cjs/schema/defaults.test.d.ts +1 -1
  38. package/dist/cjs/schema/defaults.test.js +20 -20
  39. package/dist/cjs/schema/index.d.ts +8 -8
  40. package/dist/cjs/schema/index.js +28 -28
  41. package/dist/cjs/schema/indexFilters.d.ts +6 -6
  42. package/dist/cjs/schema/indexFilters.js +30 -30
  43. package/dist/cjs/schema/types/collection.d.ts +51 -51
  44. package/dist/cjs/schema/types/collection.js +2 -2
  45. package/dist/cjs/schema/types/compounds.d.ts +12 -12
  46. package/dist/cjs/schema/types/compounds.js +2 -2
  47. package/dist/cjs/schema/types/fields.d.ts +59 -59
  48. package/dist/cjs/schema/types/fields.js +2 -2
  49. package/dist/cjs/schema/types/filters.d.ts +29 -29
  50. package/dist/cjs/schema/types/filters.js +2 -2
  51. package/dist/cjs/schema/types/shapes.d.ts +21 -21
  52. package/dist/cjs/schema/types/shapes.js +2 -2
  53. package/dist/cjs/schema/types/synthetics.d.ts +28 -28
  54. package/dist/cjs/schema/types/synthetics.js +2 -2
  55. package/dist/cjs/schema/types.d.ts +18 -18
  56. package/dist/cjs/schema/types.js +22 -22
  57. package/dist/cjs/timestamp.d.ts +48 -48
  58. package/dist/cjs/timestamp.js +243 -243
  59. package/dist/cjs/timestamp.test.d.ts +1 -1
  60. package/dist/cjs/timestamp.test.js +93 -93
  61. package/dist/cjs/undo.d.ts +3 -3
  62. package/dist/cjs/undo.js +198 -198
  63. package/dist/cjs/utils.d.ts +15 -15
  64. package/dist/cjs/utils.js +127 -127
  65. package/dist/cjs/utils.test.d.ts +1 -1
  66. package/dist/cjs/utils.test.js +36 -36
  67. package/dist/esm/EventSubscriber.d.ts +17 -17
  68. package/dist/esm/EventSubscriber.js +62 -62
  69. package/dist/esm/baseline.d.ts +5 -5
  70. package/dist/esm/baseline.js +1 -1
  71. package/dist/esm/batching.d.ts +41 -41
  72. package/dist/esm/batching.js +83 -83
  73. package/dist/esm/files.d.ts +19 -19
  74. package/dist/esm/files.js +12 -12
  75. package/dist/esm/index.d.ts +18 -18
  76. package/dist/esm/index.js +15 -15
  77. package/dist/esm/indexes.d.ts +14 -14
  78. package/dist/esm/indexes.js +105 -105
  79. package/dist/esm/indexes.test.d.ts +1 -1
  80. package/dist/esm/indexes.test.js +125 -125
  81. package/dist/esm/migration.d.ts +98 -98
  82. package/dist/esm/migration.js +121 -121
  83. package/dist/esm/oids.d.ts +87 -87
  84. package/dist/esm/oids.js +347 -347
  85. package/dist/esm/oids.test.d.ts +1 -1
  86. package/dist/esm/oids.test.js +243 -243
  87. package/dist/esm/operation.d.ts +139 -139
  88. package/dist/esm/operation.js +517 -517
  89. package/dist/esm/operation.test.d.ts +1 -1
  90. package/dist/esm/operation.test.js +482 -482
  91. package/dist/esm/patch.d.ts +26 -26
  92. package/dist/esm/patch.js +222 -222
  93. package/dist/esm/presence.d.ts +27 -27
  94. package/dist/esm/presence.js +1 -1
  95. package/dist/esm/protocol.d.ts +145 -145
  96. package/dist/esm/protocol.js +2 -2
  97. package/dist/esm/refs.d.ts +5 -5
  98. package/dist/esm/refs.js +16 -16
  99. package/dist/esm/replica.d.ts +28 -28
  100. package/dist/esm/replica.js +25 -25
  101. package/dist/esm/schema/defaults.d.ts +4 -4
  102. package/dist/esm/schema/defaults.js +65 -65
  103. package/dist/esm/schema/defaults.test.d.ts +1 -1
  104. package/dist/esm/schema/defaults.test.js +18 -18
  105. package/dist/esm/schema/index.d.ts +8 -8
  106. package/dist/esm/schema/index.js +9 -9
  107. package/dist/esm/schema/indexFilters.d.ts +6 -6
  108. package/dist/esm/schema/indexFilters.js +22 -22
  109. package/dist/esm/schema/types/collection.d.ts +51 -51
  110. package/dist/esm/schema/types/collection.js +1 -1
  111. package/dist/esm/schema/types/compounds.d.ts +12 -12
  112. package/dist/esm/schema/types/compounds.js +1 -1
  113. package/dist/esm/schema/types/fields.d.ts +59 -59
  114. package/dist/esm/schema/types/fields.js +1 -1
  115. package/dist/esm/schema/types/filters.d.ts +29 -29
  116. package/dist/esm/schema/types/filters.js +1 -1
  117. package/dist/esm/schema/types/shapes.d.ts +21 -21
  118. package/dist/esm/schema/types/shapes.js +1 -1
  119. package/dist/esm/schema/types/synthetics.d.ts +28 -28
  120. package/dist/esm/schema/types/synthetics.js +1 -1
  121. package/dist/esm/schema/types.d.ts +18 -18
  122. package/dist/esm/schema/types.js +6 -6
  123. package/dist/esm/timestamp.d.ts +48 -48
  124. package/dist/esm/timestamp.js +228 -228
  125. package/dist/esm/timestamp.test.d.ts +1 -1
  126. package/dist/esm/timestamp.test.js +91 -91
  127. package/dist/esm/undo.d.ts +3 -3
  128. package/dist/esm/undo.js +194 -194
  129. package/dist/esm/utils.d.ts +15 -15
  130. package/dist/esm/utils.js +113 -113
  131. package/dist/esm/utils.test.d.ts +1 -1
  132. package/dist/esm/utils.test.js +34 -34
  133. package/dist/tsconfig-cjs.tsbuildinfo +1 -1
  134. package/dist/tsconfig.tsbuildinfo +1 -1
  135. package/package.json +1 -1
  136. package/dist/cjs/files.test.d.ts +0 -1
  137. package/dist/cjs/files.test.js +0 -81
  138. package/dist/cjs/files.test.js.map +0 -1
  139. package/dist/esm/files.test.d.ts +0 -1
  140. package/dist/esm/files.test.js +0 -79
  141. package/dist/esm/files.test.js.map +0 -1
@@ -1,98 +1,98 @@
1
- import { StorageCollectionSchema, StorageSchema, StorageDocument, CollectionFilter, StorageDocumentInit } from './index.js';
2
- export interface DroppedCollectionMigrationStrategy<Old extends StorageCollectionSchema<any, any, any>> {
3
- (old: Old): void | Promise<void>;
4
- }
5
- export interface PreservedCollectionMigrationStrategy<Old extends StorageCollectionSchema<any, any, any>, New extends StorageCollectionSchema<any, any, any>> {
6
- (old: StorageDocument<Old>): StorageDocument<New> | Promise<StorageDocument<New>>;
7
- }
8
- declare type MigrationStrategy<Old extends StorageCollectionSchema<any, any, any>, New extends StorageCollectionSchema<any, any, any>> = DroppedCollectionMigrationStrategy<Old> | PreservedCollectionMigrationStrategy<Old, New>;
9
- export declare type MigrationsKeyedOnCollection<Old extends StorageSchema<any>, New extends StorageSchema<any>> = PreservedCollectionMigrations<Old, New> | DroppedCollectionMigrations<Old, New>;
10
- declare type NotInSchema<Name extends string | number | symbol, Schema extends StorageSchema<any>> = Name extends keyof Schema['collections'] ? never : Name;
11
- declare type InSchema<Name extends string | number | symbol, Schema extends StorageSchema<any>> = Name extends keyof Schema['collections'] ? Name : never;
12
- declare type DroppedCollections<Old extends StorageSchema<any>, New extends StorageSchema<any>> = {
13
- [Key in keyof Old['collections'] as NotInSchema<Key, New>]: StorageCollectionSchema<any, any, any>;
14
- };
15
- declare type PreservedCollections<Old extends StorageSchema<any>, New extends StorageSchema<any>> = {
16
- [Key in keyof Old['collections'] as InSchema<Key, New>]: StorageCollectionSchema<any, any, any>;
17
- };
18
- declare type DroppedCollectionMigrations<Old extends StorageSchema<any>, New extends StorageSchema<any>> = {
19
- [Key in keyof DroppedCollections<Old, New>]: DroppedCollectionMigrationStrategy<Old['collections'][Key]>;
20
- };
21
- declare type PreservedCollectionMigrations<Old extends StorageSchema<any>, New extends StorageSchema<any>> = {
22
- [Key in keyof PreservedCollections<Old, New>]: PreservedCollectionMigrationStrategy<Old['collections'][Key], New['collections'][Key]>;
23
- };
24
- declare type StrategyFor<Key extends string, Old extends StorageSchema<any>, New extends StorageSchema<any>> = Key extends keyof New['collections'] ? PreservedCollectionMigrationStrategy<Old['collections'][Key], New['collections'][Key]> : DroppedCollectionMigrationStrategy<Old['collections'][Key]>;
25
- declare type MigrationRunner<Old extends StorageSchema<any>, New extends StorageSchema<any>> = <Collection extends Extract<keyof Old['collections'], string>>(collection: Collection, strategy: StrategyFor<Collection, Old, New>) => Promise<void>;
26
- declare type MigrationQueryMaker<Collection extends StorageCollectionSchema<any, any, any>> = {
27
- get(primaryKey: string): Promise<StorageDocument<Collection> | undefined>;
28
- findOne(query: CollectionFilter): Promise<StorageDocument<Collection> | undefined>;
29
- findAll(query?: CollectionFilter): Promise<StorageDocument<Collection>[]>;
30
- };
31
- declare type MigrationQueries<Old extends StorageSchema<any>> = {
32
- [Key in keyof Old['collections']]: MigrationQueryMaker<Old['collections'][Key]>;
33
- };
34
- declare type MigrationMutations<New extends StorageSchema> = {
35
- [Key in keyof New['collections']]: {
36
- put(document: StorageDocumentInit<New['collections'][Key]>): Promise<StorageDocument<New['collections'][Key]>>;
37
- delete(primaryKey: string): Promise<void>;
38
- };
39
- };
40
- export interface MigrationTools<Old extends StorageSchema<any>, New extends StorageSchema<any>> {
41
- migrate: MigrationRunner<Old, New>;
42
- identity: <T>(val: T) => T;
43
- withDefaults: (collectionName: string, value: any) => any;
44
- queries: MigrationQueries<Old>;
45
- mutations: MigrationMutations<New>;
46
- info: {
47
- changedCollections: keyof Old['collections'][];
48
- addedCollections: keyof New['collections'][];
49
- removedCollections: keyof Old['collections'][];
50
- };
51
- }
52
- export interface MigrationEngine<Old extends StorageSchema, New extends StorageSchema> {
53
- migrate: (collection: string, strategy: MigrationStrategy<any, any>) => Promise<void>;
54
- queries: MigrationQueries<Old>;
55
- mutations: MigrationMutations<New>;
56
- /** OIDs of any new documents created during the migration */
57
- newOids: string[];
58
- /** Promises that should be resolved before completing the migration */
59
- awaitables: Promise<any>[];
60
- }
61
- declare type MigrationProcedure<Old extends StorageSchema, New extends StorageSchema> = (tools: MigrationTools<Old, New>) => Promise<void>;
62
- declare type EmptySchema = {
63
- version: 0;
64
- collections: {};
65
- };
66
- export declare function migrate<Schema extends StorageSchema>(schema: Schema, procedure: MigrationProcedure<EmptySchema, Schema>): Migration<EmptySchema, Schema>;
67
- export declare function migrate<Old extends StorageSchema, New extends StorageSchema>(oldSchema: Old, newSchema: New, procedure: MigrationProcedure<Old, New>): Migration<Old, New>;
68
- export interface MigrationIndexDescription {
69
- name: string;
70
- multiEntry: boolean;
71
- synthetic: boolean;
72
- compound: boolean;
73
- }
74
- export interface Migration<Old extends StorageSchema = any, New extends StorageSchema = any> {
75
- version: number;
76
- oldSchema: Old;
77
- newSchema: New;
78
- migrate: (engine: MigrationEngine<Old, New>) => Promise<void>;
79
- /** Collections which are added in the new schema and not present in the old */
80
- addedCollections: string[];
81
- /** Collections which were removed from the old schema */
82
- removedCollections: string[];
83
- /** All collections which exist after the migration has completed - i.e. the ones in the new schema */
84
- allCollections: string[];
85
- /** Only the collections which were in the old schema */
86
- oldCollections: string[];
87
- /** Collections whose fields or indexes changed between schemas */
88
- changedCollections: string[];
89
- addedIndexes: Record<string, MigrationIndexDescription[]>;
90
- removedIndexes: Record<string, MigrationIndexDescription[]>;
91
- }
92
- export declare function migrationRange(from: number, to: number): number[];
93
- export declare function createDefaultMigration(schema: StorageSchema): Migration<{
94
- version: 0;
95
- collections: {};
96
- }>;
97
- export declare function createDefaultMigration<Old extends StorageSchema>(oldSchema: Old, newSchema: StorageSchema): Migration<Old>;
98
- export {};
1
+ import { StorageCollectionSchema, StorageSchema, StorageDocument, CollectionFilter, StorageDocumentInit } from './index.js';
2
+ export interface DroppedCollectionMigrationStrategy<Old extends StorageCollectionSchema<any, any, any>> {
3
+ (old: Old): void | Promise<void>;
4
+ }
5
+ export interface PreservedCollectionMigrationStrategy<Old extends StorageCollectionSchema<any, any, any>, New extends StorageCollectionSchema<any, any, any>> {
6
+ (old: StorageDocument<Old>): StorageDocument<New> | Promise<StorageDocument<New>>;
7
+ }
8
+ declare type MigrationStrategy<Old extends StorageCollectionSchema<any, any, any>, New extends StorageCollectionSchema<any, any, any>> = DroppedCollectionMigrationStrategy<Old> | PreservedCollectionMigrationStrategy<Old, New>;
9
+ export declare type MigrationsKeyedOnCollection<Old extends StorageSchema<any>, New extends StorageSchema<any>> = PreservedCollectionMigrations<Old, New> | DroppedCollectionMigrations<Old, New>;
10
+ declare type NotInSchema<Name extends string | number | symbol, Schema extends StorageSchema<any>> = Name extends keyof Schema['collections'] ? never : Name;
11
+ declare type InSchema<Name extends string | number | symbol, Schema extends StorageSchema<any>> = Name extends keyof Schema['collections'] ? Name : never;
12
+ declare type DroppedCollections<Old extends StorageSchema<any>, New extends StorageSchema<any>> = {
13
+ [Key in keyof Old['collections'] as NotInSchema<Key, New>]: StorageCollectionSchema<any, any, any>;
14
+ };
15
+ declare type PreservedCollections<Old extends StorageSchema<any>, New extends StorageSchema<any>> = {
16
+ [Key in keyof Old['collections'] as InSchema<Key, New>]: StorageCollectionSchema<any, any, any>;
17
+ };
18
+ declare type DroppedCollectionMigrations<Old extends StorageSchema<any>, New extends StorageSchema<any>> = {
19
+ [Key in keyof DroppedCollections<Old, New>]: DroppedCollectionMigrationStrategy<Old['collections'][Key]>;
20
+ };
21
+ declare type PreservedCollectionMigrations<Old extends StorageSchema<any>, New extends StorageSchema<any>> = {
22
+ [Key in keyof PreservedCollections<Old, New>]: PreservedCollectionMigrationStrategy<Old['collections'][Key], New['collections'][Key]>;
23
+ };
24
+ declare type StrategyFor<Key extends string, Old extends StorageSchema<any>, New extends StorageSchema<any>> = Key extends keyof New['collections'] ? PreservedCollectionMigrationStrategy<Old['collections'][Key], New['collections'][Key]> : DroppedCollectionMigrationStrategy<Old['collections'][Key]>;
25
+ declare type MigrationRunner<Old extends StorageSchema<any>, New extends StorageSchema<any>> = <Collection extends Extract<keyof Old['collections'], string>>(collection: Collection, strategy: StrategyFor<Collection, Old, New>) => Promise<void>;
26
+ declare type MigrationQueryMaker<Collection extends StorageCollectionSchema<any, any, any>> = {
27
+ get(primaryKey: string): Promise<StorageDocument<Collection> | undefined>;
28
+ findOne(query: CollectionFilter): Promise<StorageDocument<Collection> | undefined>;
29
+ findAll(query?: CollectionFilter): Promise<StorageDocument<Collection>[]>;
30
+ };
31
+ declare type MigrationQueries<Old extends StorageSchema<any>> = {
32
+ [Key in keyof Old['collections']]: MigrationQueryMaker<Old['collections'][Key]>;
33
+ };
34
+ declare type MigrationMutations<New extends StorageSchema> = {
35
+ [Key in keyof New['collections']]: {
36
+ put(document: StorageDocumentInit<New['collections'][Key]>): Promise<StorageDocument<New['collections'][Key]>>;
37
+ delete(primaryKey: string): Promise<void>;
38
+ };
39
+ };
40
+ export interface MigrationTools<Old extends StorageSchema<any>, New extends StorageSchema<any>> {
41
+ migrate: MigrationRunner<Old, New>;
42
+ identity: <T>(val: T) => T;
43
+ withDefaults: (collectionName: string, value: any) => any;
44
+ queries: MigrationQueries<Old>;
45
+ mutations: MigrationMutations<New>;
46
+ info: {
47
+ changedCollections: keyof Old['collections'][];
48
+ addedCollections: keyof New['collections'][];
49
+ removedCollections: keyof Old['collections'][];
50
+ };
51
+ }
52
+ export interface MigrationEngine<Old extends StorageSchema, New extends StorageSchema> {
53
+ migrate: (collection: string, strategy: MigrationStrategy<any, any>) => Promise<void>;
54
+ queries: MigrationQueries<Old>;
55
+ mutations: MigrationMutations<New>;
56
+ /** OIDs of any new documents created during the migration */
57
+ newOids: string[];
58
+ /** Promises that should be resolved before completing the migration */
59
+ awaitables: Promise<any>[];
60
+ }
61
+ declare type MigrationProcedure<Old extends StorageSchema, New extends StorageSchema> = (tools: MigrationTools<Old, New>) => Promise<void>;
62
+ declare type EmptySchema = {
63
+ version: 0;
64
+ collections: {};
65
+ };
66
+ export declare function migrate<Schema extends StorageSchema>(schema: Schema, procedure: MigrationProcedure<EmptySchema, Schema>): Migration<EmptySchema, Schema>;
67
+ export declare function migrate<Old extends StorageSchema, New extends StorageSchema>(oldSchema: Old, newSchema: New, procedure: MigrationProcedure<Old, New>): Migration<Old, New>;
68
+ export interface MigrationIndexDescription {
69
+ name: string;
70
+ multiEntry: boolean;
71
+ synthetic: boolean;
72
+ compound: boolean;
73
+ }
74
+ export interface Migration<Old extends StorageSchema = any, New extends StorageSchema = any> {
75
+ version: number;
76
+ oldSchema: Old;
77
+ newSchema: New;
78
+ migrate: (engine: MigrationEngine<Old, New>) => Promise<void>;
79
+ /** Collections which are added in the new schema and not present in the old */
80
+ addedCollections: string[];
81
+ /** Collections which were removed from the old schema */
82
+ removedCollections: string[];
83
+ /** All collections which exist after the migration has completed - i.e. the ones in the new schema */
84
+ allCollections: string[];
85
+ /** Only the collections which were in the old schema */
86
+ oldCollections: string[];
87
+ /** Collections whose fields or indexes changed between schemas */
88
+ changedCollections: string[];
89
+ addedIndexes: Record<string, MigrationIndexDescription[]>;
90
+ removedIndexes: Record<string, MigrationIndexDescription[]>;
91
+ }
92
+ export declare function migrationRange(from: number, to: number): number[];
93
+ export declare function createDefaultMigration(schema: StorageSchema): Migration<{
94
+ version: 0;
95
+ collections: {};
96
+ }>;
97
+ export declare function createDefaultMigration<Old extends StorageSchema>(oldSchema: Old, newSchema: StorageSchema): Migration<Old>;
98
+ export {};
@@ -1,128 +1,128 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createDefaultMigration = exports.migrationRange = exports.migrate = void 0;
4
- const index_js_1 = require("./index.js");
5
- const emptySchema = {
6
- version: 0,
7
- collections: {},
8
- };
9
- function migrate(oldSchemaOrNewSchema, newSchemaOrProcedure, procedureIfTwoSchemas) {
10
- const isProcedureSecondArgument = typeof newSchemaOrProcedure === 'function';
11
- const oldSchema = isProcedureSecondArgument
12
- ? emptySchema
13
- : oldSchemaOrNewSchema;
14
- const newSchema = isProcedureSecondArgument
15
- ? oldSchemaOrNewSchema
16
- : newSchemaOrProcedure;
17
- const procedure = isProcedureSecondArgument
18
- ? newSchemaOrProcedure
19
- : procedureIfTwoSchemas;
20
- // diff to determine changed collections
21
- const changedCollections = Object.keys(newSchema.collections).filter((key) => oldSchema.collections[key] &&
22
- (0, index_js_1.stableStringify)(oldSchema.collections[key]) !==
23
- (0, index_js_1.stableStringify)(newSchema.collections[key]));
24
- const removedCollections = Object.keys(oldSchema.collections).filter((key) => !newSchema.collections[key]);
25
- const addedCollections = Object.keys(newSchema.collections).filter((key) => !oldSchema.collections[key]);
26
- const addedIndexes = {};
27
- const removedIndexes = {};
28
- for (const changed of [...changedCollections, ...addedCollections]) {
29
- const oldIndexes = getIndexes(oldSchema.collections[changed]);
30
- const newIndexes = getIndexes(newSchema.collections[changed]);
31
- const added = newIndexes.filter((index) => !oldIndexes.find((i) => i.name === index.name));
32
- const removed = oldIndexes.filter((index) => !newIndexes.find((i) => i.name === index.name));
33
- if (added.length > 0) {
34
- addedIndexes[changed] = added;
35
- }
36
- if (removed.length > 0) {
37
- removedIndexes[changed] = removed;
38
- }
39
- }
40
- return {
41
- version: newSchema.version,
42
- migrate: async (engine) => {
43
- const migratedCollections = [];
44
- await procedure({
45
- migrate: async (collection, strategy) => {
46
- // @ts-ignore
47
- await engine.migrate(collection, strategy);
48
- migratedCollections.push(collection);
49
- },
50
- identity: (val) => val,
51
- withDefaults: (collectionName, val) => {
52
- return (0, index_js_1.addFieldDefaults)(newSchema.collections[collectionName], val);
53
- },
54
- info: {
55
- changedCollections,
56
- addedCollections,
57
- removedCollections,
58
- },
59
- queries: engine.queries,
60
- mutations: engine.mutations,
61
- });
62
- const unmigrated = changedCollections.filter((collection) => !migratedCollections.includes(collection));
63
- if (unmigrated.length > 0) {
64
- console.error('Unmigrated changed collections:', unmigrated);
65
- }
66
- },
67
- removedCollections,
68
- addedIndexes,
69
- removedIndexes,
70
- allCollections: Object.keys(newSchema.collections),
71
- changedCollections,
72
- addedCollections,
73
- oldCollections: Object.keys(oldSchema.collections),
74
- oldSchema,
75
- newSchema,
76
- };
77
- }
78
- exports.migrate = migrate;
79
- function migrationRange(from, to) {
80
- return [...Array(to - from).keys()].map((i) => 1 + i + from);
81
- }
82
- exports.migrationRange = migrationRange;
83
- function getIndexes(collection) {
84
- if (!collection)
85
- return [];
86
- const fields = Object.keys(collection.fields)
87
- .filter((key) => collection.fields[key].indexed)
88
- .map((key) => ({
89
- name: key,
90
- multiEntry: collection.fields[key].type === 'array',
91
- synthetic: false,
92
- compound: false,
93
- }));
94
- return [
95
- ...fields,
96
- ...Object.keys(collection.synthetics || {}).map((key) => ({
97
- name: key,
98
- multiEntry: ['array', 'string[]', 'number[]', 'boolean[]'].includes(collection.synthetics[key].type),
99
- synthetic: true,
100
- compound: false,
101
- })),
102
- ...Object.keys(collection.compounds || {}).map((key) => ({
103
- name: key,
104
- multiEntry: collection.compounds[key].of.some((fieldName) => (collection.fields[fieldName] || collection.synthetics[fieldName])
105
- .type === 'array'),
106
- synthetic: false,
107
- compound: true,
108
- })),
109
- ];
110
- }
111
- function createDefaultMigration(schema, newSchema) {
112
- let oldSchema = newSchema
113
- ? schema
114
- : {
115
- version: 0,
116
- collections: {},
117
- };
118
- return migrate(oldSchema, newSchema || schema, async ({ migrate, withDefaults, info }) => {
119
- if ((newSchema || schema).version === 1)
120
- return;
121
- for (const collection of info.changedCollections) {
122
- // @ts-ignore indefinite type resolution
123
- await migrate(collection, (old) => withDefaults(collection, old));
124
- }
125
- });
126
- }
127
- exports.createDefaultMigration = createDefaultMigration;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDefaultMigration = exports.migrationRange = exports.migrate = void 0;
4
+ const index_js_1 = require("./index.js");
5
+ const emptySchema = {
6
+ version: 0,
7
+ collections: {},
8
+ };
9
+ function migrate(oldSchemaOrNewSchema, newSchemaOrProcedure, procedureIfTwoSchemas) {
10
+ const isProcedureSecondArgument = typeof newSchemaOrProcedure === 'function';
11
+ const oldSchema = isProcedureSecondArgument
12
+ ? emptySchema
13
+ : oldSchemaOrNewSchema;
14
+ const newSchema = isProcedureSecondArgument
15
+ ? oldSchemaOrNewSchema
16
+ : newSchemaOrProcedure;
17
+ const procedure = isProcedureSecondArgument
18
+ ? newSchemaOrProcedure
19
+ : procedureIfTwoSchemas;
20
+ // diff to determine changed collections
21
+ const changedCollections = Object.keys(newSchema.collections).filter((key) => oldSchema.collections[key] &&
22
+ (0, index_js_1.stableStringify)(oldSchema.collections[key]) !==
23
+ (0, index_js_1.stableStringify)(newSchema.collections[key]));
24
+ const removedCollections = Object.keys(oldSchema.collections).filter((key) => !newSchema.collections[key]);
25
+ const addedCollections = Object.keys(newSchema.collections).filter((key) => !oldSchema.collections[key]);
26
+ const addedIndexes = {};
27
+ const removedIndexes = {};
28
+ for (const changed of [...changedCollections, ...addedCollections]) {
29
+ const oldIndexes = getIndexes(oldSchema.collections[changed]);
30
+ const newIndexes = getIndexes(newSchema.collections[changed]);
31
+ const added = newIndexes.filter((index) => !oldIndexes.find((i) => i.name === index.name));
32
+ const removed = oldIndexes.filter((index) => !newIndexes.find((i) => i.name === index.name));
33
+ if (added.length > 0) {
34
+ addedIndexes[changed] = added;
35
+ }
36
+ if (removed.length > 0) {
37
+ removedIndexes[changed] = removed;
38
+ }
39
+ }
40
+ return {
41
+ version: newSchema.version,
42
+ migrate: async (engine) => {
43
+ const migratedCollections = [];
44
+ await procedure({
45
+ migrate: async (collection, strategy) => {
46
+ // @ts-ignore
47
+ await engine.migrate(collection, strategy);
48
+ migratedCollections.push(collection);
49
+ },
50
+ identity: (val) => val,
51
+ withDefaults: (collectionName, val) => {
52
+ return (0, index_js_1.addFieldDefaults)(newSchema.collections[collectionName], val);
53
+ },
54
+ info: {
55
+ changedCollections,
56
+ addedCollections,
57
+ removedCollections,
58
+ },
59
+ queries: engine.queries,
60
+ mutations: engine.mutations,
61
+ });
62
+ const unmigrated = changedCollections.filter((collection) => !migratedCollections.includes(collection));
63
+ if (unmigrated.length > 0) {
64
+ console.error('Unmigrated changed collections:', unmigrated);
65
+ }
66
+ },
67
+ removedCollections,
68
+ addedIndexes,
69
+ removedIndexes,
70
+ allCollections: Object.keys(newSchema.collections),
71
+ changedCollections,
72
+ addedCollections,
73
+ oldCollections: Object.keys(oldSchema.collections),
74
+ oldSchema,
75
+ newSchema,
76
+ };
77
+ }
78
+ exports.migrate = migrate;
79
+ function migrationRange(from, to) {
80
+ return [...Array(to - from).keys()].map((i) => 1 + i + from);
81
+ }
82
+ exports.migrationRange = migrationRange;
83
+ function getIndexes(collection) {
84
+ if (!collection)
85
+ return [];
86
+ const fields = Object.keys(collection.fields)
87
+ .filter((key) => collection.fields[key].indexed)
88
+ .map((key) => ({
89
+ name: key,
90
+ multiEntry: collection.fields[key].type === 'array',
91
+ synthetic: false,
92
+ compound: false,
93
+ }));
94
+ return [
95
+ ...fields,
96
+ ...Object.keys(collection.synthetics || {}).map((key) => ({
97
+ name: key,
98
+ multiEntry: ['array', 'string[]', 'number[]', 'boolean[]'].includes(collection.synthetics[key].type),
99
+ synthetic: true,
100
+ compound: false,
101
+ })),
102
+ ...Object.keys(collection.compounds || {}).map((key) => ({
103
+ name: key,
104
+ multiEntry: collection.compounds[key].of.some((fieldName) => (collection.fields[fieldName] || collection.synthetics[fieldName])
105
+ .type === 'array'),
106
+ synthetic: false,
107
+ compound: true,
108
+ })),
109
+ ];
110
+ }
111
+ function createDefaultMigration(schema, newSchema) {
112
+ let oldSchema = newSchema
113
+ ? schema
114
+ : {
115
+ version: 0,
116
+ collections: {},
117
+ };
118
+ return migrate(oldSchema, newSchema || schema, async ({ migrate, withDefaults, info }) => {
119
+ if ((newSchema || schema).version === 1)
120
+ return;
121
+ for (const collection of info.changedCollections) {
122
+ // @ts-ignore indefinite type resolution
123
+ await migrate(collection, (old) => withDefaults(collection, old));
124
+ }
125
+ });
126
+ }
127
+ exports.createDefaultMigration = createDefaultMigration;
128
128
  //# sourceMappingURL=migration.js.map
@@ -1,87 +1,87 @@
1
- import { ObjectRef } from './operation.js';
2
- /**
3
- * OIDs
4
- *
5
- * OIDs are used to identify objects in the document. They also encode
6
- * information about the object useful to identifying an object found
7
- * on its own and associating it back to its parent.
8
- *
9
- * An OID is structured as such:
10
- * <collection>/<root id>[/<key paths>]:<random>
11
- *
12
- * OIDs have a few characteristics:
13
- * - They include the collection name of the parent document
14
- * - They include the primary key of the parent document
15
- * - They include the key path of the object within the document
16
- * - They include a random sequence to identify different objects which
17
- * exist at the same key path
18
- *
19
- * Collection name and document key are used to link any isolated
20
- * object back to its parent document.
21
- *
22
- * The key path is used for authorization - to associate the object
23
- * (or an operation related to it by OID) with the field it inhabits
24
- * to utilize authorization rules from that field in the schema.
25
- *
26
- * The random sequence allows the application to encode different
27
- * identities for objects at the same position in a document for
28
- * conflict resolution purposes
29
- */
30
- export declare type ObjectIdentifier = string;
31
- export declare type KeyPath = (string | number)[];
32
- export declare const LEGACY_OID_KEY = "__@@oid_do_not_use";
33
- export declare const OID_KEY = "@@id";
34
- export declare function getOid(obj: any): string;
35
- export declare function maybeGetOid(obj: any): ObjectIdentifier | undefined;
36
- export declare function assignOid(obj: any, oid: ObjectIdentifier): any;
37
- export declare function hasOid(obj: any): boolean;
38
- export declare function removeOid(obj: any): any;
39
- export declare function isOidKey(key: string): boolean;
40
- /**
41
- * For sub-objects, assign a random sub-OID if no OID
42
- * is already assigned.
43
- */
44
- export declare function ensureOid(obj: any, rootOid: ObjectIdentifier, key: string | number, createSubId?: () => string): string;
45
- export declare function createOid(collection: string, documentId: string, keyPath?: KeyPath, subId?: string): string;
46
- export declare function createSubOid(root: ObjectIdentifier, key: string | number, createSubId?: () => string): string;
47
- export declare function decomposeOid(oid: ObjectIdentifier): {
48
- collection: string;
49
- id: string;
50
- subId?: string;
51
- keyPath: KeyPath;
52
- };
53
- export declare function assertAllLevelsHaveOids(obj: any, root?: any): void;
54
- export declare function assignOidsToAllSubObjects(obj: any, createSubId?: () => string): void;
55
- export declare function assignOidProperty(obj: any, oid: ObjectIdentifier): any;
56
- export declare function maybeGetOidProperty(obj: any): any;
57
- /**
58
- * Assigns a special property to all objects in the given object
59
- * which have an OID
60
- */
61
- export declare function assignOidPropertiesToAllSubObjects(obj: any): void;
62
- /**
63
- *
64
- * Removes the special property from all objects in the given object
65
- * which have an OID, transferring the OID from the property to the OID
66
- * system in-memory.
67
- */
68
- export declare function removeOidPropertiesFromAllSubObjects(obj: any): void;
69
- export declare function removeOidsFromAllSubObjects(obj: any): void;
70
- export declare function createOidSubId(): string;
71
- export declare function createRef(oid: ObjectIdentifier): ObjectRef;
72
- export declare function normalize(obj: any, refs?: Map<ObjectIdentifier, any>): Map<ObjectIdentifier, any>;
73
- /**
74
- * Only normalizes direct children. The created map
75
- * of objects will still have nested objects.
76
- */
77
- export declare function normalizeFirstLevel(obj: any): {
78
- refs: Map<string, any>;
79
- oidKeyPairs: Map<string, string | number>;
80
- };
81
- export declare function getOidRoot(oid: ObjectIdentifier): string;
82
- /**
83
- * Returns an inclusive range of OIDs that represent
84
- * a root object and all of its sub-objects.
85
- */
86
- export declare function getOidRange(oid: ObjectIdentifier): string[];
87
- export declare function getRoots(oids: ObjectIdentifier[]): string[];
1
+ import { ObjectRef } from './operation.js';
2
+ /**
3
+ * OIDs
4
+ *
5
+ * OIDs are used to identify objects in the document. They also encode
6
+ * information about the object useful to identifying an object found
7
+ * on its own and associating it back to its parent.
8
+ *
9
+ * An OID is structured as such:
10
+ * <collection>/<root id>[/<key paths>]:<random>
11
+ *
12
+ * OIDs have a few characteristics:
13
+ * - They include the collection name of the parent document
14
+ * - They include the primary key of the parent document
15
+ * - They include the key path of the object within the document
16
+ * - They include a random sequence to identify different objects which
17
+ * exist at the same key path
18
+ *
19
+ * Collection name and document key are used to link any isolated
20
+ * object back to its parent document.
21
+ *
22
+ * The key path is used for authorization - to associate the object
23
+ * (or an operation related to it by OID) with the field it inhabits
24
+ * to utilize authorization rules from that field in the schema.
25
+ *
26
+ * The random sequence allows the application to encode different
27
+ * identities for objects at the same position in a document for
28
+ * conflict resolution purposes
29
+ */
30
+ export declare type ObjectIdentifier = string;
31
+ export declare type KeyPath = (string | number)[];
32
+ export declare const LEGACY_OID_KEY = "__@@oid_do_not_use";
33
+ export declare const OID_KEY = "@@id";
34
+ export declare function getOid(obj: any): string;
35
+ export declare function maybeGetOid(obj: any): ObjectIdentifier | undefined;
36
+ export declare function assignOid(obj: any, oid: ObjectIdentifier): any;
37
+ export declare function hasOid(obj: any): boolean;
38
+ export declare function removeOid(obj: any): any;
39
+ export declare function isOidKey(key: string): boolean;
40
+ /**
41
+ * For sub-objects, assign a random sub-OID if no OID
42
+ * is already assigned.
43
+ */
44
+ export declare function ensureOid(obj: any, rootOid: ObjectIdentifier, key: string | number, createSubId?: () => string): string;
45
+ export declare function createOid(collection: string, documentId: string, keyPath?: KeyPath, subId?: string): string;
46
+ export declare function createSubOid(root: ObjectIdentifier, key: string | number, createSubId?: () => string): string;
47
+ export declare function decomposeOid(oid: ObjectIdentifier): {
48
+ collection: string;
49
+ id: string;
50
+ subId?: string;
51
+ keyPath: KeyPath;
52
+ };
53
+ export declare function assertAllLevelsHaveOids(obj: any, root?: any): void;
54
+ export declare function assignOidsToAllSubObjects(obj: any, createSubId?: () => string): void;
55
+ export declare function assignOidProperty(obj: any, oid: ObjectIdentifier): any;
56
+ export declare function maybeGetOidProperty(obj: any): any;
57
+ /**
58
+ * Assigns a special property to all objects in the given object
59
+ * which have an OID
60
+ */
61
+ export declare function assignOidPropertiesToAllSubObjects(obj: any): void;
62
+ /**
63
+ *
64
+ * Removes the special property from all objects in the given object
65
+ * which have an OID, transferring the OID from the property to the OID
66
+ * system in-memory.
67
+ */
68
+ export declare function removeOidPropertiesFromAllSubObjects(obj: any): void;
69
+ export declare function removeOidsFromAllSubObjects(obj: any): void;
70
+ export declare function createOidSubId(): string;
71
+ export declare function createRef(oid: ObjectIdentifier): ObjectRef;
72
+ export declare function normalize(obj: any, refs?: Map<ObjectIdentifier, any>): Map<ObjectIdentifier, any>;
73
+ /**
74
+ * Only normalizes direct children. The created map
75
+ * of objects will still have nested objects.
76
+ */
77
+ export declare function normalizeFirstLevel(obj: any): {
78
+ refs: Map<string, any>;
79
+ oidKeyPairs: Map<string, string | number>;
80
+ };
81
+ export declare function getOidRoot(oid: ObjectIdentifier): string;
82
+ /**
83
+ * Returns an inclusive range of OIDs that represent
84
+ * a root object and all of its sub-objects.
85
+ */
86
+ export declare function getOidRange(oid: ObjectIdentifier): string[];
87
+ export declare function getRoots(oids: ObjectIdentifier[]): string[];