liwi-subscribe-store 8.3.1 → 9.0.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 (41) hide show
  1. package/.eslintrc.json +1 -1
  2. package/CHANGELOG.md +18 -0
  3. package/dist/AbstractSubscribableStoreQuery.d.ts.map +1 -1
  4. package/dist/SubscribeStore.d.ts +6 -5
  5. package/dist/SubscribeStore.d.ts.map +1 -1
  6. package/dist/index-browser.cjs.js +505 -148
  7. package/dist/index-browser.cjs.js.map +1 -1
  8. package/dist/index-browser.es.js +500 -150
  9. package/dist/index-browser.es.js.map +1 -1
  10. package/dist/index-browsermodern.es.js +37 -32
  11. package/dist/index-browsermodern.es.js.map +1 -1
  12. package/dist/{index-browser-dev.cjs.js → index-node14.cjs} +38 -33
  13. package/dist/index-node14.cjs.map +1 -0
  14. package/dist/{index-node12-dev.es.js → index-node14.mjs} +38 -33
  15. package/dist/index-node14.mjs.map +1 -0
  16. package/dist/index.d.ts +2 -3
  17. package/dist/index.d.ts.map +1 -1
  18. package/package.json +25 -42
  19. package/rollup.config.mjs +4 -2
  20. package/src/.eslintrc.json +19 -2
  21. package/src/AbstractSubscribableStoreQuery.ts +3 -2
  22. package/src/SubscribeStore.ts +19 -11
  23. package/src/index.ts +3 -4
  24. package/tsconfig.eslint.json +7 -0
  25. package/dist/index-browser-dev.cjs.js.map +0 -1
  26. package/dist/index-browser-dev.es.js +0 -185
  27. package/dist/index-browser-dev.es.js.map +0 -1
  28. package/dist/index-browsermodern-dev.es.js +0 -185
  29. package/dist/index-browsermodern-dev.es.js.map +0 -1
  30. package/dist/index-node12-dev.cjs.js +0 -190
  31. package/dist/index-node12-dev.cjs.js.map +0 -1
  32. package/dist/index-node12-dev.es.js.map +0 -1
  33. package/dist/index-node12-dev.mjs +0 -185
  34. package/dist/index-node12-dev.mjs.map +0 -1
  35. package/dist/index-node12.cjs.js +0 -190
  36. package/dist/index-node12.cjs.js.map +0 -1
  37. package/dist/index-node12.es.js +0 -185
  38. package/dist/index-node12.es.js.map +0 -1
  39. package/dist/index-node12.mjs +0 -185
  40. package/dist/index-node12.mjs.map +0 -1
  41. package/index.js +0 -6
@@ -1,185 +0,0 @@
1
- class AbstractSubscribableStoreQuery {
2
- changeParams() {
3
- throw new Error('Method not supported. Please create a new query.');
4
- }
5
-
6
- changePartialParams() {
7
- throw new Error('Method not supported. Please create a new query.');
8
- }
9
-
10
- setSubscribeStore(store) {
11
- this._subscribeStore = store;
12
- }
13
-
14
- getSubscribeStore() {
15
- if (!this._subscribeStore) {
16
- throw new Error('_subscribeStore is not initialized');
17
- }
18
-
19
- return this._subscribeStore;
20
- }
21
-
22
- fetchAndSubscribe(callback) {
23
- return this._subscribe(callback, true);
24
- }
25
-
26
- subscribe(callback) {
27
- return this._subscribe(callback, false);
28
- }
29
-
30
- }
31
-
32
- /* eslint-disable max-lines */
33
- class SubscribeStore {
34
- constructor(store) {
35
- this.listeners = new Set();
36
- this.store = store;
37
- this.keyPath = store.keyPath;
38
- }
39
-
40
- get connection() {
41
- return this.store.connection;
42
- }
43
-
44
- subscribe(callback) {
45
- this.listeners.add(callback);
46
- return () => this.listeners.delete(callback);
47
- }
48
-
49
- callSubscribed(action) {
50
- this.listeners.forEach(listener => listener(action));
51
- }
52
-
53
- createQuerySingleItem(options, transformer) {
54
- const query = this.store.createQuerySingleItem(options, transformer);
55
- query.setSubscribeStore(this);
56
- return query;
57
- }
58
-
59
- createQueryCollection(options, transformer) {
60
- const query = this.store.createQueryCollection(options, transformer);
61
- query.setSubscribeStore(this);
62
- return query;
63
- }
64
-
65
- findAll(criteria, sort) {
66
- return this.store.findAll(criteria, sort);
67
- }
68
-
69
- findByKey(key, criteria) {
70
- return this.store.findByKey(key, criteria);
71
- }
72
-
73
- findOne(criteria, sort) {
74
- return this.store.findOne(criteria, sort);
75
- }
76
-
77
- async insertOne(object) {
78
- const inserted = await this.store.insertOne(object);
79
- this.callSubscribed({
80
- type: 'inserted',
81
- next: [inserted]
82
- });
83
- return inserted;
84
- }
85
-
86
- async replaceOne(object) {
87
- const replaced = await this.store.replaceOne(object);
88
- this.callSubscribed({
89
- type: 'updated',
90
- changes: [[object, replaced]]
91
- });
92
- return replaced;
93
- }
94
-
95
- async replaceSeveral(objects) {
96
- const replacedObjects = await this.store.replaceSeveral(objects);
97
- this.callSubscribed({
98
- type: 'updated',
99
- changes: objects.map((prev, index) => [prev, replacedObjects[index]])
100
- });
101
- return replacedObjects;
102
- }
103
-
104
- async upsertOne(object, setOnInsertPartialObject) {
105
- const result = await this.upsertOneWithInfo(object, setOnInsertPartialObject);
106
- return result.object;
107
- }
108
-
109
- async upsertOneWithInfo(object, setOnInsertPartialObject) {
110
- const upsertedWithInfo = await this.store.upsertOneWithInfo(object, setOnInsertPartialObject);
111
-
112
- if (upsertedWithInfo.inserted) {
113
- this.callSubscribed({
114
- type: 'inserted',
115
- next: [upsertedWithInfo.object]
116
- });
117
- } else {
118
- throw new Error('TODO');
119
- }
120
-
121
- return upsertedWithInfo;
122
- }
123
-
124
- async partialUpdateByKey(key, partialUpdate, criteria) {
125
- return this.partialUpdateOne(await this.findOne({
126
- [this.store.keyPath]: key,
127
- ...criteria
128
- }), partialUpdate);
129
- }
130
-
131
- async partialUpdateOne(object, partialUpdate) {
132
- const updated = await this.store.partialUpdateOne(object, partialUpdate);
133
- this.callSubscribed({
134
- type: 'updated',
135
- changes: [[object, updated]]
136
- });
137
- return updated;
138
- }
139
-
140
- async partialUpdateMany(criteria, partialUpdate) {
141
- const cursor = await this.store.cursor(criteria);
142
- const changes = [];
143
- await cursor.forEach(async model => {
144
- const key = model[this.store.keyPath];
145
- const updated = await this.store.partialUpdateByKey(key, partialUpdate, criteria);
146
- changes.push([model, updated]);
147
- });
148
- this.callSubscribed({
149
- type: 'updated',
150
- changes
151
- });
152
- }
153
-
154
- async deleteByKey(key, criteria) {
155
- return this.deleteOne(await this.findByKey(key, criteria));
156
- }
157
-
158
- async deleteOne(object) {
159
- await this.store.deleteOne(object);
160
- this.callSubscribed({
161
- type: 'deleted',
162
- prev: [object]
163
- });
164
- }
165
-
166
- async deleteMany(criteria) {
167
- const cursor = await this.store.cursor(criteria);
168
- const prev = await cursor.toArray();
169
- await this.store.deleteMany(criteria);
170
- this.callSubscribed({
171
- type: 'deleted',
172
- prev
173
- });
174
- }
175
-
176
- async cursor(criteria, sort) {
177
- const cursor = await this.store.cursor(criteria, sort);
178
- cursor.overrideStore(this);
179
- return cursor;
180
- }
181
-
182
- }
183
-
184
- export { AbstractSubscribableStoreQuery, SubscribeStore };
185
- //# sourceMappingURL=index-browsermodern-dev.es.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index-browsermodern-dev.es.js","sources":["../src/AbstractSubscribableStoreQuery.ts","../src/SubscribeStore.ts"],"sourcesContent":["import type {\n Query,\n SubscribeCallback,\n QuerySubscription,\n QueryResult,\n QueryParams,\n} from 'liwi-store';\nimport type { BaseModel, InsertType, AllowedKeyValue } from 'liwi-types';\nimport type SubscribeStore from './SubscribeStore';\n\nexport default abstract class AbstractSubscribableStoreQuery<\n KeyPath extends keyof Model,\n KeyValue extends AllowedKeyValue,\n Model extends BaseModel & Record<KeyPath, KeyValue>,\n ModelInsertType extends InsertType<Model, KeyPath>,\n Params extends QueryParams<Params> = never,\n Result = Model\n> implements Query<Result, Params, KeyValue> {\n changeParams(params: Params): never {\n throw new Error('Method not supported. Please create a new query.');\n }\n\n changePartialParams(\n params: Params extends undefined ? never : Partial<Params>,\n ): never {\n throw new Error('Method not supported. Please create a new query.');\n }\n\n private _subscribeStore?: SubscribeStore<\n KeyPath,\n KeyValue,\n Model,\n ModelInsertType,\n any,\n any\n >;\n\n setSubscribeStore(\n store: SubscribeStore<KeyPath, KeyValue, Model, ModelInsertType, any, any>,\n ): void {\n this._subscribeStore = store;\n }\n\n getSubscribeStore(): SubscribeStore<\n KeyPath,\n KeyValue,\n Model,\n ModelInsertType,\n any,\n any\n > {\n if (!this._subscribeStore) {\n throw new Error('_subscribeStore is not initialized');\n }\n return this._subscribeStore;\n }\n\n abstract fetch<T>(\n onFulfilled: (result: QueryResult<Result>) => T,\n ): Promise<T>;\n\n fetchAndSubscribe(\n callback: SubscribeCallback<KeyValue, Result>,\n ): QuerySubscription {\n return this._subscribe(callback, true);\n }\n\n subscribe(callback: SubscribeCallback<KeyValue, Result>): QuerySubscription {\n return this._subscribe(callback, false);\n }\n\n abstract _subscribe(\n callback: SubscribeCallback<KeyValue, Result>,\n _includeInitial: boolean,\n ): QuerySubscription;\n}\n","/* eslint-disable max-lines */\nimport type {\n Store as StoreInterface,\n AbstractConnection,\n UpsertResult,\n SubscribableStoreQuery,\n AbstractStoreCursor,\n SubscribableStore,\n QueryParams,\n UpsertPartialObject,\n} from 'liwi-store';\nimport type {\n BaseModel,\n InsertType,\n Update,\n Criteria,\n Sort,\n QueryOptions,\n Transformer,\n AllowedKeyValue,\n OptionalBaseModelKeysForInsert,\n} from 'liwi-types';\n\nexport type Actions<Model> =\n | { type: 'inserted'; next: Model[] }\n | { type: 'updated'; changes: [Model, Model][] }\n | { type: 'deleted'; prev: Model[] };\n\nexport type Listener<Model> = (action: Actions<Model>) => unknown;\n\nexport default class SubscribeStore<\n KeyPath extends keyof Model,\n KeyValue extends AllowedKeyValue,\n Model extends BaseModel & Record<KeyPath, KeyValue>,\n ModelInsertType extends InsertType<Model, KeyPath>,\n Connection extends AbstractConnection,\n Store extends SubscribableStore<\n KeyPath,\n KeyValue,\n Model,\n ModelInsertType,\n Connection\n >\n> implements\n StoreInterface<KeyPath, KeyValue, Model, ModelInsertType, Connection> {\n private readonly store: Store;\n\n private readonly listeners: Set<Listener<Model>> = new Set();\n\n readonly keyPath: KeyPath;\n\n constructor(store: Store) {\n this.store = store;\n this.keyPath = store.keyPath;\n }\n\n get connection(): Connection {\n return this.store.connection;\n }\n\n subscribe(callback: Listener<Model>): () => void {\n this.listeners.add(callback);\n return () => this.listeners.delete(callback);\n }\n\n callSubscribed(action: Actions<Model>): void {\n this.listeners.forEach((listener) => listener(action));\n }\n\n createQuerySingleItem<\n Result extends Record<KeyPath, KeyValue>,\n Params extends QueryParams<Params>\n >(\n options: QueryOptions<Model>,\n transformer?: Transformer<Model, Result>,\n ): SubscribableStoreQuery<\n KeyPath,\n KeyValue,\n Model,\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Result,\n Params\n > {\n const query: SubscribableStoreQuery<\n KeyPath,\n KeyValue,\n Model,\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Result,\n Params\n > = this.store.createQuerySingleItem<Result, Params>(options, transformer);\n query.setSubscribeStore(this);\n return query;\n }\n\n createQueryCollection<\n Item extends Record<KeyPath, KeyValue>,\n Params extends QueryParams<Params>\n >(\n options: QueryOptions<Model>,\n transformer?: Transformer<Model, Item>,\n ): SubscribableStoreQuery<\n KeyPath,\n KeyValue,\n Model,\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Item[],\n Params\n > {\n const query: SubscribableStoreQuery<\n KeyPath,\n KeyValue,\n Model,\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Item[],\n Params\n > = this.store.createQueryCollection<Item, Params>(options, transformer);\n query.setSubscribeStore(this);\n return query;\n }\n\n findAll(criteria?: Criteria<Model>, sort?: Sort<Model>): Promise<Model[]> {\n return this.store.findAll(criteria, sort);\n }\n\n findByKey(key: any, criteria?: Criteria<Model>): Promise<Model | undefined> {\n return this.store.findByKey(key, criteria);\n }\n\n findOne(\n criteria: Criteria<Model>,\n sort?: Sort<Model>,\n ): Promise<Model | undefined> {\n return this.store.findOne(criteria, sort);\n }\n\n async insertOne(object: ModelInsertType): Promise<Model> {\n const inserted = await this.store.insertOne(object);\n this.callSubscribed({ type: 'inserted', next: [inserted] });\n return inserted;\n }\n\n async replaceOne(object: Model): Promise<Model> {\n const replaced = await this.store.replaceOne(object);\n this.callSubscribed({ type: 'updated', changes: [[object, replaced]] });\n return replaced;\n }\n\n async replaceSeveral(objects: Model[]): Promise<Model[]> {\n const replacedObjects = await this.store.replaceSeveral(objects);\n this.callSubscribed({\n type: 'updated',\n changes: objects.map((prev, index) => [prev, replacedObjects[index]]),\n });\n return replacedObjects;\n }\n\n async upsertOne<\n K extends Exclude<keyof Model, KeyPath | OptionalBaseModelKeysForInsert>\n >(\n object: UpsertPartialObject<KeyPath, KeyValue, Model, K>,\n setOnInsertPartialObject?: Pick<Model, K>,\n ): Promise<Model> {\n const result = await this.upsertOneWithInfo(\n object,\n setOnInsertPartialObject,\n );\n return result.object;\n }\n\n async upsertOneWithInfo<\n K extends Exclude<keyof Model, KeyPath | OptionalBaseModelKeysForInsert>\n >(\n object: UpsertPartialObject<KeyPath, KeyValue, Model, K>,\n setOnInsertPartialObject?: Pick<Model, K>,\n ): Promise<UpsertResult<Model>> {\n const upsertedWithInfo = await this.store.upsertOneWithInfo(\n object,\n setOnInsertPartialObject,\n );\n if (upsertedWithInfo.inserted) {\n this.callSubscribed({\n type: 'inserted',\n next: [upsertedWithInfo.object],\n });\n } else {\n throw new Error('TODO');\n }\n return upsertedWithInfo;\n }\n\n async partialUpdateByKey(\n key: any,\n partialUpdate: Update<Model>,\n criteria?: Criteria<Model>,\n ): Promise<Model> {\n return this.partialUpdateOne(\n (await this.findOne({ [this.store.keyPath]: key, ...criteria }))!,\n partialUpdate,\n );\n }\n\n async partialUpdateOne(\n object: Model,\n partialUpdate: Update<Model>,\n ): Promise<Model> {\n const updated = await this.store.partialUpdateOne(object, partialUpdate);\n this.callSubscribed({ type: 'updated', changes: [[object, updated]] });\n return updated;\n }\n\n async partialUpdateMany(\n criteria: Criteria<Model>,\n partialUpdate: Update<Model>,\n ): Promise<void> {\n const cursor = await this.store.cursor(criteria);\n const changes: [Model, Model][] = [];\n\n await cursor.forEach(async (model) => {\n const key = model[this.store.keyPath];\n const updated = await this.store.partialUpdateByKey(\n key,\n partialUpdate,\n criteria,\n );\n changes.push([model, updated]);\n });\n this.callSubscribed({ type: 'updated', changes });\n }\n\n async deleteByKey(key: any, criteria?: Criteria<Model>): Promise<void> {\n return this.deleteOne((await this.findByKey(key, criteria))!);\n }\n\n async deleteOne(object: Model): Promise<void> {\n await this.store.deleteOne(object);\n this.callSubscribed({ type: 'deleted', prev: [object] });\n }\n\n async deleteMany(criteria: Criteria<Model>): Promise<void> {\n const cursor = await this.store.cursor(criteria);\n const prev: Model[] = await cursor.toArray();\n await this.store.deleteMany(criteria);\n this.callSubscribed({ type: 'deleted', prev });\n }\n\n async cursor<Result = Model>(\n criteria?: Criteria<Model>,\n sort?: Sort<Model>,\n ): Promise<AbstractStoreCursor<any, KeyValue, Model, Result>> {\n const cursor = await this.store.cursor<Result>(criteria, sort);\n cursor.overrideStore(this);\n return cursor;\n }\n}\n"],"names":["AbstractSubscribableStoreQuery","changeParams","Error","changePartialParams","setSubscribeStore","store","_subscribeStore","getSubscribeStore","fetchAndSubscribe","callback","_subscribe","subscribe","SubscribeStore","constructor","listeners","Set","keyPath","connection","add","delete","callSubscribed","action","forEach","listener","createQuerySingleItem","options","transformer","query","createQueryCollection","findAll","criteria","sort","findByKey","key","findOne","insertOne","object","inserted","type","next","replaceOne","replaced","changes","replaceSeveral","objects","replacedObjects","map","prev","index","upsertOne","setOnInsertPartialObject","result","upsertOneWithInfo","upsertedWithInfo","partialUpdateByKey","partialUpdate","partialUpdateOne","updated","partialUpdateMany","cursor","model","push","deleteByKey","deleteOne","deleteMany","toArray","overrideStore"],"mappings":"AAUe,MAAeA,8BAAf,CAO8B;AAC3CC,EAAAA,YAAY,GAAwB;AAClC,UAAM,IAAIC,KAAJ,CAAU,kDAAV,CAAN;AACD;;AAEDC,EAAAA,mBAAmB,GAEV;AACP,UAAM,IAAID,KAAJ,CAAU,kDAAV,CAAN;AACD;;AAWDE,EAAAA,iBAAiB,CACfC,KADe,EAET;AACN,SAAKC,eAAL,GAAuBD,KAAvB;AACD;;AAEDE,EAAAA,iBAAiB,GAOf;AACA,QAAI,CAAC,KAAKD,eAAV,E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CAAkB7B,QAAlB,CAArB;AACA,UAAMiB,IAAa,GAAG,MAAMY,MAAM,CAACM,OAAP,EAA5B;AACA,UAAM,KAAK5D,KAAL,CAAW2D,UAAX,CAAsBlC,QAAtB,CAAN;AACA,SAAKV,cAAL,CAAoB;AAAEkB,MAAAA,IAAI,EAAE,SAAR;AAAmBS,MAAAA;AAAnB,KAApB;AACD;;AAEW,QAANY,MAAM,CACV7B,QADU,EAEVC,IAFU,EAGkD;AAC5D,UAAM4B,MAAM,GAAG,MAAM,KAAKtD,KAAL,CAAWsD,MAAX,CAA0B7B,QAA1B,EAAoCC,IAApC,CAArB;AACA4B,IAAAA,MAAM,CAACO,aAAP,CAAqB,IAArB;AACA,WAAOP,MAAP;AACD;;AAjNuE;;;;"}
@@ -1,190 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- class AbstractSubscribableStoreQuery {
6
- changeParams() {
7
- throw new Error('Method not supported. Please create a new query.');
8
- }
9
-
10
- changePartialParams() {
11
- throw new Error('Method not supported. Please create a new query.');
12
- }
13
-
14
- setSubscribeStore(store) {
15
- this._subscribeStore = store;
16
- }
17
-
18
- getSubscribeStore() {
19
- if (!this._subscribeStore) {
20
- throw new Error('_subscribeStore is not initialized');
21
- }
22
-
23
- return this._subscribeStore;
24
- }
25
-
26
- fetchAndSubscribe(callback) {
27
- return this._subscribe(callback, true);
28
- }
29
-
30
- subscribe(callback) {
31
- return this._subscribe(callback, false);
32
- }
33
-
34
- }
35
-
36
- /* eslint-disable max-lines */
37
- class SubscribeStore {
38
- constructor(store) {
39
- this.listeners = new Set();
40
- this.store = store;
41
- this.keyPath = store.keyPath;
42
- }
43
-
44
- get connection() {
45
- return this.store.connection;
46
- }
47
-
48
- subscribe(callback) {
49
- this.listeners.add(callback);
50
- return () => this.listeners.delete(callback);
51
- }
52
-
53
- callSubscribed(action) {
54
- this.listeners.forEach(listener => listener(action));
55
- }
56
-
57
- createQuerySingleItem(options, transformer) {
58
- const query = this.store.createQuerySingleItem(options, transformer);
59
- query.setSubscribeStore(this);
60
- return query;
61
- }
62
-
63
- createQueryCollection(options, transformer) {
64
- const query = this.store.createQueryCollection(options, transformer);
65
- query.setSubscribeStore(this);
66
- return query;
67
- }
68
-
69
- findAll(criteria, sort) {
70
- return this.store.findAll(criteria, sort);
71
- }
72
-
73
- findByKey(key, criteria) {
74
- return this.store.findByKey(key, criteria);
75
- }
76
-
77
- findOne(criteria, sort) {
78
- return this.store.findOne(criteria, sort);
79
- }
80
-
81
- async insertOne(object) {
82
- const inserted = await this.store.insertOne(object);
83
- this.callSubscribed({
84
- type: 'inserted',
85
- next: [inserted]
86
- });
87
- return inserted;
88
- }
89
-
90
- async replaceOne(object) {
91
- const replaced = await this.store.replaceOne(object);
92
- this.callSubscribed({
93
- type: 'updated',
94
- changes: [[object, replaced]]
95
- });
96
- return replaced;
97
- }
98
-
99
- async replaceSeveral(objects) {
100
- const replacedObjects = await this.store.replaceSeveral(objects);
101
- this.callSubscribed({
102
- type: 'updated',
103
- changes: objects.map((prev, index) => [prev, replacedObjects[index]])
104
- });
105
- return replacedObjects;
106
- }
107
-
108
- async upsertOne(object, setOnInsertPartialObject) {
109
- const result = await this.upsertOneWithInfo(object, setOnInsertPartialObject);
110
- return result.object;
111
- }
112
-
113
- async upsertOneWithInfo(object, setOnInsertPartialObject) {
114
- const upsertedWithInfo = await this.store.upsertOneWithInfo(object, setOnInsertPartialObject);
115
-
116
- if (upsertedWithInfo.inserted) {
117
- this.callSubscribed({
118
- type: 'inserted',
119
- next: [upsertedWithInfo.object]
120
- });
121
- } else {
122
- throw new Error('TODO');
123
- }
124
-
125
- return upsertedWithInfo;
126
- }
127
-
128
- async partialUpdateByKey(key, partialUpdate, criteria) {
129
- return this.partialUpdateOne(await this.findOne({
130
- [this.store.keyPath]: key,
131
- ...criteria
132
- }), partialUpdate);
133
- }
134
-
135
- async partialUpdateOne(object, partialUpdate) {
136
- const updated = await this.store.partialUpdateOne(object, partialUpdate);
137
- this.callSubscribed({
138
- type: 'updated',
139
- changes: [[object, updated]]
140
- });
141
- return updated;
142
- }
143
-
144
- async partialUpdateMany(criteria, partialUpdate) {
145
- const cursor = await this.store.cursor(criteria);
146
- const changes = [];
147
- await cursor.forEach(async model => {
148
- const key = model[this.store.keyPath];
149
- const updated = await this.store.partialUpdateByKey(key, partialUpdate, criteria);
150
- changes.push([model, updated]);
151
- });
152
- this.callSubscribed({
153
- type: 'updated',
154
- changes
155
- });
156
- }
157
-
158
- async deleteByKey(key, criteria) {
159
- return this.deleteOne(await this.findByKey(key, criteria));
160
- }
161
-
162
- async deleteOne(object) {
163
- await this.store.deleteOne(object);
164
- this.callSubscribed({
165
- type: 'deleted',
166
- prev: [object]
167
- });
168
- }
169
-
170
- async deleteMany(criteria) {
171
- const cursor = await this.store.cursor(criteria);
172
- const prev = await cursor.toArray();
173
- await this.store.deleteMany(criteria);
174
- this.callSubscribed({
175
- type: 'deleted',
176
- prev
177
- });
178
- }
179
-
180
- async cursor(criteria, sort) {
181
- const cursor = await this.store.cursor(criteria, sort);
182
- cursor.overrideStore(this);
183
- return cursor;
184
- }
185
-
186
- }
187
-
188
- exports.AbstractSubscribableStoreQuery = AbstractSubscribableStoreQuery;
189
- exports.SubscribeStore = SubscribeStore;
190
- //# sourceMappingURL=index-node12-dev.cjs.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index-node12-dev.cjs.js","sources":["../src/AbstractSubscribableStoreQuery.ts","../src/SubscribeStore.ts"],"sourcesContent":["import type {\n Query,\n SubscribeCallback,\n QuerySubscription,\n QueryResult,\n QueryParams,\n} from 'liwi-store';\nimport type { BaseModel, InsertType, AllowedKeyValue } from 'liwi-types';\nimport type SubscribeStore from './SubscribeStore';\n\nexport default abstract class AbstractSubscribableStoreQuery<\n KeyPath extends keyof Model,\n KeyValue extends AllowedKeyValue,\n Model extends BaseModel & Record<KeyPath, KeyValue>,\n ModelInsertType extends InsertType<Model, KeyPath>,\n Params extends QueryParams<Params> = never,\n Result = Model\n> implements Query<Result, Params, KeyValue> {\n changeParams(params: Params): never {\n throw new Error('Method not supported. Please create a new query.');\n }\n\n changePartialParams(\n params: Params extends undefined ? never : Partial<Params>,\n ): never {\n throw new Error('Method not supported. Please create a new query.');\n }\n\n private _subscribeStore?: SubscribeStore<\n KeyPath,\n KeyValue,\n Model,\n ModelInsertType,\n any,\n any\n >;\n\n setSubscribeStore(\n store: SubscribeStore<KeyPath, KeyValue, Model, ModelInsertType, any, any>,\n ): void {\n this._subscribeStore = store;\n }\n\n getSubscribeStore(): SubscribeStore<\n KeyPath,\n KeyValue,\n Model,\n ModelInsertType,\n any,\n any\n > {\n if (!this._subscribeStore) {\n throw new Error('_subscribeStore is not initialized');\n }\n return this._subscribeStore;\n }\n\n abstract fetch<T>(\n onFulfilled: (result: QueryResult<Result>) => T,\n ): Promise<T>;\n\n fetchAndSubscribe(\n callback: SubscribeCallback<KeyValue, Result>,\n ): QuerySubscription {\n return this._subscribe(callback, true);\n }\n\n subscribe(callback: SubscribeCallback<KeyValue, Result>): QuerySubscription {\n return this._subscribe(callback, false);\n }\n\n abstract _subscribe(\n callback: SubscribeCallback<KeyValue, Result>,\n _includeInitial: boolean,\n ): QuerySubscription;\n}\n","/* eslint-disable max-lines */\nimport type {\n Store as StoreInterface,\n AbstractConnection,\n UpsertResult,\n SubscribableStoreQuery,\n AbstractStoreCursor,\n SubscribableStore,\n QueryParams,\n UpsertPartialObject,\n} from 'liwi-store';\nimport type {\n BaseModel,\n InsertType,\n Update,\n Criteria,\n Sort,\n QueryOptions,\n Transformer,\n AllowedKeyValue,\n OptionalBaseModelKeysForInsert,\n} from 'liwi-types';\n\nexport type Actions<Model> =\n | { type: 'inserted'; next: Model[] }\n | { type: 'updated'; changes: [Model, Model][] }\n | { type: 'deleted'; prev: Model[] };\n\nexport type Listener<Model> = (action: Actions<Model>) => unknown;\n\nexport default class SubscribeStore<\n KeyPath extends keyof Model,\n KeyValue extends AllowedKeyValue,\n Model extends BaseModel & Record<KeyPath, KeyValue>,\n ModelInsertType extends InsertType<Model, KeyPath>,\n Connection extends AbstractConnection,\n Store extends SubscribableStore<\n KeyPath,\n KeyValue,\n Model,\n ModelInsertType,\n Connection\n >\n> implements\n StoreInterface<KeyPath, KeyValue, Model, ModelInsertType, Connection> {\n private readonly store: Store;\n\n private readonly listeners: Set<Listener<Model>> = new Set();\n\n readonly keyPath: KeyPath;\n\n constructor(store: Store) {\n this.store = store;\n this.keyPath = store.keyPath;\n }\n\n get connection(): Connection {\n return this.store.connection;\n }\n\n subscribe(callback: Listener<Model>): () => void {\n this.listeners.add(callback);\n return () => this.listeners.delete(callback);\n }\n\n callSubscribed(action: Actions<Model>): void {\n this.listeners.forEach((listener) => listener(action));\n }\n\n createQuerySingleItem<\n Result extends Record<KeyPath, KeyValue>,\n Params extends QueryParams<Params>\n >(\n options: QueryOptions<Model>,\n transformer?: Transformer<Model, Result>,\n ): SubscribableStoreQuery<\n KeyPath,\n KeyValue,\n Model,\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Result,\n Params\n > {\n const query: SubscribableStoreQuery<\n KeyPath,\n KeyValue,\n Model,\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Result,\n Params\n > = this.store.createQuerySingleItem<Result, Params>(options, transformer);\n query.setSubscribeStore(this);\n return query;\n }\n\n createQueryCollection<\n Item extends Record<KeyPath, KeyValue>,\n Params extends QueryParams<Params>\n >(\n options: QueryOptions<Model>,\n transformer?: Transformer<Model, Item>,\n ): SubscribableStoreQuery<\n KeyPath,\n KeyValue,\n Model,\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Item[],\n Params\n > {\n const query: SubscribableStoreQuery<\n KeyPath,\n KeyValue,\n Model,\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Item[],\n Params\n > = this.store.createQueryCollection<Item, Params>(options, transformer);\n query.setSubscribeStore(this);\n return query;\n }\n\n findAll(criteria?: Criteria<Model>, sort?: Sort<Model>): Promise<Model[]> {\n return this.store.findAll(criteria, sort);\n }\n\n findByKey(key: any, criteria?: Criteria<Model>): Promise<Model | undefined> {\n return this.store.findByKey(key, criteria);\n }\n\n findOne(\n criteria: Criteria<Model>,\n sort?: Sort<Model>,\n ): Promise<Model | undefined> {\n return this.store.findOne(criteria, sort);\n }\n\n async insertOne(object: ModelInsertType): Promise<Model> {\n const inserted = await this.store.insertOne(object);\n this.callSubscribed({ type: 'inserted', next: [inserted] });\n return inserted;\n }\n\n async replaceOne(object: Model): Promise<Model> {\n const replaced = await this.store.replaceOne(object);\n this.callSubscribed({ type: 'updated', changes: [[object, replaced]] });\n return replaced;\n }\n\n async replaceSeveral(objects: Model[]): Promise<Model[]> {\n const replacedObjects = await this.store.replaceSeveral(objects);\n this.callSubscribed({\n type: 'updated',\n changes: objects.map((prev, index) => [prev, replacedObjects[index]]),\n });\n return replacedObjects;\n }\n\n async upsertOne<\n K extends Exclude<keyof Model, KeyPath | OptionalBaseModelKeysForInsert>\n >(\n object: UpsertPartialObject<KeyPath, KeyValue, Model, K>,\n setOnInsertPartialObject?: Pick<Model, K>,\n ): Promise<Model> {\n const result = await this.upsertOneWithInfo(\n object,\n setOnInsertPartialObject,\n );\n return result.object;\n }\n\n async upsertOneWithInfo<\n K extends Exclude<keyof Model, KeyPath | OptionalBaseModelKeysForInsert>\n >(\n object: UpsertPartialObject<KeyPath, KeyValue, Model, K>,\n setOnInsertPartialObject?: Pick<Model, K>,\n ): Promise<UpsertResult<Model>> {\n const upsertedWithInfo = await this.store.upsertOneWithInfo(\n object,\n setOnInsertPartialObject,\n );\n if (upsertedWithInfo.inserted) {\n this.callSubscribed({\n type: 'inserted',\n next: [upsertedWithInfo.object],\n });\n } else {\n throw new Error('TODO');\n }\n return upsertedWithInfo;\n }\n\n async partialUpdateByKey(\n key: any,\n partialUpdate: Update<Model>,\n criteria?: Criteria<Model>,\n ): Promise<Model> {\n return this.partialUpdateOne(\n (await this.findOne({ [this.store.keyPath]: key, ...criteria }))!,\n partialUpdate,\n );\n }\n\n async partialUpdateOne(\n object: Model,\n partialUpdate: Update<Model>,\n ): Promise<Model> {\n const updated = await this.store.partialUpdateOne(object, partialUpdate);\n this.callSubscribed({ type: 'updated', changes: [[object, updated]] });\n return updated;\n }\n\n async partialUpdateMany(\n criteria: Criteria<Model>,\n partialUpdate: Update<Model>,\n ): Promise<void> {\n const cursor = await this.store.cursor(criteria);\n const changes: [Model, Model][] = [];\n\n await cursor.forEach(async (model) => {\n const key = model[this.store.keyPath];\n const updated = await this.store.partialUpdateByKey(\n key,\n partialUpdate,\n criteria,\n );\n changes.push([model, updated]);\n });\n this.callSubscribed({ type: 'updated', changes });\n }\n\n async deleteByKey(key: any, criteria?: Criteria<Model>): Promise<void> {\n return this.deleteOne((await this.findByKey(key, criteria))!);\n }\n\n async deleteOne(object: Model): Promise<void> {\n await this.store.deleteOne(object);\n this.callSubscribed({ type: 'deleted', prev: [object] });\n }\n\n async deleteMany(criteria: Criteria<Model>): Promise<void> {\n const cursor = await this.store.cursor(criteria);\n const prev: Model[] = await cursor.toArray();\n await this.store.deleteMany(criteria);\n this.callSubscribed({ type: 'deleted', prev });\n }\n\n async cursor<Result = Model>(\n criteria?: Criteria<Model>,\n sort?: Sort<Model>,\n ): Promise<AbstractStoreCursor<any, KeyValue, Model, Result>> {\n const cursor = await this.store.cursor<Result>(criteria, sort);\n cursor.overrideStore(this);\n return cursor;\n }\n}\n"],"names":["AbstractSubscribableStoreQuery","changeParams","Error","changePartialParams","setSubscribeStore","store","_subscribeStore","getSubscribeStore","fetchAndSubscribe","callback","_subscribe","subscribe","SubscribeStore","constructor","listeners","Set","keyPath","connection","add","delete","callSubscribed","action","forEach","listener","createQuerySingleItem","options","transformer","query","createQueryCollection","findAll","criteria","sort","findByKey","key","findOne","insertOne","object","inserted","type","next","replaceOne","replaced","changes","replaceSeveral","objects","replacedObjects","map","prev","index","upsertOne","setOnInsertPartialObject","result","upsertOneWithInfo","upsertedWithInfo","partialUpdateByKey","partialUpdate","partialUpdateOne","updated","partialUpdateMany","cursor","model","push","deleteByKey","deleteOne","deleteMany","toArray","overrideStore"],"mappings":";;;;AAUe,MAAeA,8BAAf,CAO8B;AAC3CC,EAAAA,YAAY,GAAwB;AAClC,UAAM,IAAIC,KAAJ,CAAU,kDAAV,CAAN;AACD;;AAEDC,EAAAA,mBAAmB,GAEV;AACP,UAAM,IAAID,KAAJ,CAAU,kDAAV,CAAN;AACD;;AAWDE,EAAAA,iBAAiB,CACfC,KADe,EAET;AACN,SAAKC,eAAL,GAAuBD,KAAvB;AACD;;AAEDE,EAAAA,iBAAiB,GAOf;AACA,QAAI,CAAC,KAAKD,eAAV,EAA2B;AACzB,YAAM,IAAIJ,KAAJ,CAAU,oCAAV,CAAN;AACD;;AACD,WAAO,KAAKI,eAAZ;AACD;;AAMDE,EAAAA,iBAAiB,CACfC,QADe,EAEI;AACnB,WAAO,KAAKC,UAAL,CAAgBD,QAAhB,EAA0B,IAA1B,CAAP;AACD;;AAEDE,EAAAA,SAAS,CAACF,QAAD,EAAmE;AAC1E,WAAO,KAAKC,UAAL,CAAgBD,QAAhB,EAA0B,KAA1B,CAAP;AACD;;AApD0C;;ACjB7C;AA8Be,MAAMG,cAAN,CAc2D;AAOxEC,EAAAA,WAAW,CAACR,KAAD,EAAe;AAAA,SAJTS,SAIS,GAJyB,IAAIC,GAAJ,EAIzB;AACxB,SAAKV,KAAL,GAAaA,KAAb;AACA,SAAKW,OAAL,GAAeX,KAAK,CAACW,OAArB;AACD;;AAEa,MAAVC,UAAU,GAAe;AAC3B,WAAO,KAAKZ,KAAL,CAAWY,UAAlB;AACD;;AAEDN,EAAAA,SAAS,CAACF,QAAD,EAAwC;AAC/C,SAAKK,SAAL,CAAeI,GAAf,CAAmBT,QAAnB;AACA,WAAO,MAAM,KAAKK,SAAL,CAAeK,MAAf,CAAsBV,QAAtB,CAAb;AACD;;AAEDW,EAAAA,cAAc,CAACC,MAAD,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AAWH,QAAX,EAAsD;AACrE,WAAO,KAAKiC,SAAL,CAAgB,MAAM,KAAK/B,SAAL,CAAeC,GAAf,EAAoBH,QAApB,CAAtB,CAAP;AACD;;AAEc,QAATiC,SAAS,CAAC3B,MAAD,EAA+B;AAC5C,UAAM,KAAK/B,KAAL,CAAW0D,SAAX,CAAqB3B,MAArB,CAAN;AACA,SAAKhB,cAAL,CAAoB;AAAEkB,MAAAA,IAAI,EAAE,SAAR;AAAmBS,MAAAA,IAAI,EAAE,CAACX,MAAD;AAAzB,KAApB;AACD;;AAEe,QAAV4B,UAAU,CAAClC,QAAD,EAA2C;AACzD,UAAM6B,MAAM,GAAG,MAAM,KAAKtD,KAAL,CAAWsD,MAAX,CAAkB7B,QAAlB,CAArB;AACA,UAAMiB,IAAa,GAAG,MAAMY,MAAM,CAACM,OAAP,EAA5B;AACA,UAAM,KAAK5D,KAAL,CAAW2D,UAAX,CAAsBlC,QAAtB,CAAN;AACA,SAAKV,cAAL,CAAoB;AAAEkB,MAAAA,IAAI,EAAE,SAAR;AAAmBS,MAAAA;AAAnB,KAApB;AACD;;AAEW,QAANY,MAAM,CACV7B,QADU,EAEVC,IAFU,EAGkD;AAC5D,UAAM4B,MAAM,GAAG,MAAM,KAAKtD,KAAL,CAAWsD,MAAX,CAA0B7B,QAA1B,EAAoCC,IAApC,CAArB;AACA4B,IAAAA,MAAM,CAACO,aAAP,CAAqB,IAArB;AACA,WAAOP,MAAP;AACD;;AAjNuE;;;;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index-node12-dev.es.js","sources":["../src/AbstractSubscribableStoreQuery.ts","../src/SubscribeStore.ts"],"sourcesContent":["import type {\n Query,\n SubscribeCallback,\n QuerySubscription,\n QueryResult,\n QueryParams,\n} from 'liwi-store';\nimport type { BaseModel, InsertType, AllowedKeyValue } from 'liwi-types';\nimport type SubscribeStore from './SubscribeStore';\n\nexport default abstract class AbstractSubscribableStoreQuery<\n KeyPath extends string,\n KeyValue extends AllowedKeyValue,\n Model extends BaseModel & Record<KeyPath, KeyValue>,\n ModelInsertType extends InsertType<Model, KeyPath>,\n Params extends QueryParams<Params> = never,\n Result = Model\n> implements Query<Result, Params, KeyValue> {\n changeParams(params: Params): never {\n throw new Error('Method not supported. Please create a new query.');\n }\n\n changePartialParams(\n params: Params extends undefined ? never : Partial<Params>,\n ): never {\n throw new Error('Method not supported. Please create a new query.');\n }\n\n private _subscribeStore?: SubscribeStore<\n KeyPath,\n KeyValue,\n Model,\n ModelInsertType,\n any,\n any\n >;\n\n setSubscribeStore(\n store: SubscribeStore<KeyPath, KeyValue, Model, ModelInsertType, any, any>,\n ): void {\n this._subscribeStore = store;\n }\n\n getSubscribeStore(): SubscribeStore<\n KeyPath,\n KeyValue,\n Model,\n ModelInsertType,\n any,\n any\n > {\n if (!this._subscribeStore) {\n throw new Error('_subscribeStore is not initialized');\n }\n return this._subscribeStore;\n }\n\n abstract fetch<T>(\n onFulfilled: (result: QueryResult<Result>) => T,\n ): Promise<T>;\n\n fetchAndSubscribe(\n callback: SubscribeCallback<KeyValue, Result>,\n ): QuerySubscription {\n return this._subscribe(callback, true);\n }\n\n subscribe(callback: SubscribeCallback<KeyValue, Result>): QuerySubscription {\n return this._subscribe(callback, false);\n }\n\n abstract _subscribe(\n callback: SubscribeCallback<KeyValue, Result>,\n _includeInitial: boolean,\n ): QuerySubscription;\n}\n","/* eslint-disable max-lines */\nimport type {\n Store as StoreInterface,\n AbstractConnection,\n UpsertResult,\n SubscribableStoreQuery,\n AbstractStoreCursor,\n SubscribableStore,\n QueryParams,\n} from 'liwi-store';\nimport type {\n BaseModel,\n InsertType,\n Update,\n Criteria,\n Sort,\n QueryOptions,\n Transformer,\n AllowedKeyValue,\n} from 'liwi-types';\n\nexport type Actions<Model> =\n | { type: 'inserted'; next: Model[] }\n | { type: 'updated'; changes: [Model, Model][] }\n | { type: 'deleted'; prev: Model[] };\n\nexport type Listener<Model> = (action: Actions<Model>) => unknown;\n\nexport default class SubscribeStore<\n KeyPath extends string,\n KeyValue extends AllowedKeyValue,\n Model extends BaseModel & Record<KeyPath, KeyValue>,\n ModelInsertType extends InsertType<Model, KeyPath>,\n Connection extends AbstractConnection,\n Store extends SubscribableStore<\n KeyPath,\n KeyValue,\n Model,\n ModelInsertType,\n Connection\n >\n>\n implements\n StoreInterface<KeyPath, KeyValue, Model, ModelInsertType, Connection> {\n private readonly store: Store;\n\n private readonly listeners: Set<Listener<Model>> = new Set();\n\n readonly keyPath: KeyPath;\n\n constructor(store: Store) {\n this.store = store;\n this.keyPath = store.keyPath;\n }\n\n get connection(): Connection {\n return this.store.connection;\n }\n\n subscribe(callback: Listener<Model>): () => void {\n this.listeners.add(callback);\n return () => this.listeners.delete(callback);\n }\n\n callSubscribed(action: Actions<Model>): void {\n this.listeners.forEach((listener) => listener(action));\n }\n\n createQuerySingleItem<\n Result extends Record<KeyPath, KeyValue>,\n Params extends QueryParams<Params>\n >(\n options: QueryOptions<Model>,\n transformer?: Transformer<Model, Result>,\n ): SubscribableStoreQuery<\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Result,\n Params,\n KeyValue\n > {\n const query: SubscribableStoreQuery<\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Result,\n Params,\n KeyValue\n > = this.store.createQuerySingleItem<Result, Params>(options, transformer);\n query.setSubscribeStore(this);\n return query;\n }\n\n createQueryCollection<\n Item extends Record<KeyPath, KeyValue>,\n Params extends QueryParams<Params>\n >(\n options: QueryOptions<Model>,\n transformer?: Transformer<Model, Item>,\n ): SubscribableStoreQuery<\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Item[],\n Params,\n KeyValue\n > {\n const query: SubscribableStoreQuery<\n SubscribableStore<KeyPath, KeyValue, Model, ModelInsertType, Connection>,\n Item[],\n Params,\n KeyValue\n > = this.store.createQueryCollection<Item, Params>(options, transformer);\n query.setSubscribeStore(this);\n return query;\n }\n\n findAll(criteria?: Criteria<Model>, sort?: Sort<Model>): Promise<Model[]> {\n return this.store.findAll(criteria, sort);\n }\n\n findByKey(key: any, criteria?: Criteria<Model>): Promise<Model | undefined> {\n return this.store.findByKey(key, criteria);\n }\n\n findOne(\n criteria: Criteria<Model>,\n sort?: Sort<Model>,\n ): Promise<Model | undefined> {\n return this.store.findOne(criteria, sort);\n }\n\n async insertOne(object: ModelInsertType): Promise<Model> {\n const inserted = await this.store.insertOne(object);\n this.callSubscribed({ type: 'inserted', next: [inserted] });\n return inserted;\n }\n\n async replaceOne(object: Model): Promise<Model> {\n const replaced = await this.store.replaceOne(object);\n this.callSubscribed({ type: 'updated', changes: [[object, replaced]] });\n return replaced;\n }\n\n async replaceSeveral(objects: Model[]): Promise<Model[]> {\n const replacedObjects = await this.store.replaceSeveral(objects);\n this.callSubscribed({\n type: 'updated',\n changes: objects.map((prev, index) => [prev, replacedObjects[index]]),\n });\n return replacedObjects;\n }\n\n async upsertOne<K extends keyof ModelInsertType>(\n object: Exclude<ModelInsertType, K>,\n setOnInsertPartialObject?: Pick<ModelInsertType, K>,\n ): Promise<Model> {\n const result = await this.upsertOneWithInfo(\n object,\n setOnInsertPartialObject,\n );\n return result.object;\n }\n\n async upsertOneWithInfo<K extends keyof ModelInsertType>(\n object: Exclude<ModelInsertType, K>,\n setOnInsertPartialObject?: Pick<ModelInsertType, K>,\n ): Promise<UpsertResult<Model>> {\n const upsertedWithInfo = await this.store.upsertOneWithInfo(\n object,\n setOnInsertPartialObject,\n );\n if (upsertedWithInfo.inserted) {\n this.callSubscribed({\n type: 'inserted',\n next: [upsertedWithInfo.object],\n });\n } else {\n throw new Error('TODO');\n }\n return upsertedWithInfo;\n }\n\n async partialUpdateByKey(\n key: any,\n partialUpdate: Update<Model>,\n criteria?: Criteria<Model>,\n ): Promise<Model> {\n return this.partialUpdateOne(\n (await this.findOne({ [this.store.keyPath]: key, ...criteria })) as Model,\n partialUpdate,\n );\n }\n\n async partialUpdateOne(\n object: Model,\n partialUpdate: Update<Model>,\n ): Promise<Model> {\n const updated = await this.store.partialUpdateOne(object, partialUpdate);\n this.callSubscribed({ type: 'updated', changes: [[object, updated]] });\n return updated;\n }\n\n async partialUpdateMany(\n criteria: Criteria<Model>,\n partialUpdate: Update<Model>,\n ): Promise<void> {\n const cursor = await this.store.cursor(criteria);\n const changes: [Model, Model][] = [];\n\n await cursor.forEach(async (model) => {\n const key = model[this.store.keyPath];\n const updated = await this.store.partialUpdateByKey(\n key,\n partialUpdate,\n criteria,\n );\n changes.push([model, updated]);\n });\n this.callSubscribed({ type: 'updated', changes });\n }\n\n async deleteByKey(key: any, criteria?: Criteria<Model>): Promise<void> {\n return this.deleteOne((await this.findByKey(key, criteria)) as Model);\n }\n\n async deleteOne(object: Model): Promise<void> {\n await this.store.deleteOne(object);\n this.callSubscribed({ type: 'deleted', prev: [object] });\n }\n\n async deleteMany(criteria: Criteria<Model>): Promise<void> {\n const cursor = await this.store.cursor(criteria);\n const prev: Model[] = await cursor.toArray();\n await this.store.deleteMany(criteria);\n this.callSubscribed({ type: 'deleted', prev });\n }\n\n async cursor<Result = Model>(\n criteria?: Criteria<Model>,\n sort?: Sort<Model>,\n ): Promise<AbstractStoreCursor<any, KeyValue, Model, Result>> {\n const cursor = await this.store.cursor<Result>(criteria, sort);\n cursor.overrideStore(this);\n return cursor;\n }\n}\n"],"names":["AbstractSubscribableStoreQuery","changeParams","Error","changePartialParams","setSubscribeStore","store","_subscribeStore","getSubscribeStore","fetchAndSubscribe","callback","_subscribe","subscribe","SubscribeStore","constructor","listeners","Set","keyPath","connection","add","delete","callSubscribed","action","forEach","listener","createQuerySingleItem","options","transformer","query","createQueryCollection","findAll","criteria","sort","findByKey","key","findOne","insertOne","object","inserted","type","next","replaceOne","replaced","changes","replaceSeveral","objects","replacedObjects","map","prev","index","upsertOne","setOnInsertPartialObject","result","upsertOneWithInfo","upsertedWithInfo","partialUpdateByKey","partialUpdate","partialUpdateOne","updated","partialUpdateMany","cursor","model","push","deleteByKey","deleteOne","deleteMany","toAr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MiC,SAAN,CAAgB3B,MAAhB,EAA8C;AAC5C,UAAM,KAAK/B,KAAL,CAAW0D,SAAX,CAAqB3B,MAArB,CAAN;AACA,SAAKhB,cAAL,CAAoB;AAAEkB,MAAAA,IAAI,EAAE,SAAR;AAAmBS,MAAAA,IAAI,EAAE,CAACX,MAAD;AAAzB,KAApB;AACD;;AAED,QAAM4B,UAAN,CAAiBlC,QAAjB,EAA2D;AACzD,UAAM6B,MAAM,GAAG,MAAM,KAAKtD,KAAL,CAAWsD,MAAX,CAAkB7B,QAAlB,CAArB;AACA,UAAMiB,IAAa,GAAG,MAAMY,MAAM,CAACM,OAAP,EAA5B;AACA,UAAM,KAAK5D,KAAL,CAAW2D,UAAX,CAAsBlC,QAAtB,CAAN;AACA,SAAKV,cAAL,CAAoB;AAAEkB,MAAAA,IAAI,EAAE,SAAR;AAAmBS,MAAAA;AAAnB,KAApB;AACD;;AAED,QAAMY,MAAN,CACE7B,QADF,EAEEC,IAFF,EAG8D;AAC5D,UAAM4B,MAAM,GAAG,MAAM,KAAKtD,KAAL,CAAWsD,MAAX,CAA0B7B,QAA1B,EAAoCC,IAApC,CAArB;AACA4B,IAAAA,MAAM,CAACO,aAAP,CAAqB,IAArB;AACA,WAAOP,MAAP;AACD;;AArMuE;;;;"}
@@ -1,185 +0,0 @@
1
- class AbstractSubscribableStoreQuery {
2
- changeParams() {
3
- throw new Error('Method not supported. Please create a new query.');
4
- }
5
-
6
- changePartialParams() {
7
- throw new Error('Method not supported. Please create a new query.');
8
- }
9
-
10
- setSubscribeStore(store) {
11
- this._subscribeStore = store;
12
- }
13
-
14
- getSubscribeStore() {
15
- if (!this._subscribeStore) {
16
- throw new Error('_subscribeStore is not initialized');
17
- }
18
-
19
- return this._subscribeStore;
20
- }
21
-
22
- fetchAndSubscribe(callback) {
23
- return this._subscribe(callback, true);
24
- }
25
-
26
- subscribe(callback) {
27
- return this._subscribe(callback, false);
28
- }
29
-
30
- }
31
-
32
- /* eslint-disable max-lines */
33
- class SubscribeStore {
34
- constructor(store) {
35
- this.listeners = new Set();
36
- this.store = store;
37
- this.keyPath = store.keyPath;
38
- }
39
-
40
- get connection() {
41
- return this.store.connection;
42
- }
43
-
44
- subscribe(callback) {
45
- this.listeners.add(callback);
46
- return () => this.listeners.delete(callback);
47
- }
48
-
49
- callSubscribed(action) {
50
- this.listeners.forEach(listener => listener(action));
51
- }
52
-
53
- createQuerySingleItem(options, transformer) {
54
- const query = this.store.createQuerySingleItem(options, transformer);
55
- query.setSubscribeStore(this);
56
- return query;
57
- }
58
-
59
- createQueryCollection(options, transformer) {
60
- const query = this.store.createQueryCollection(options, transformer);
61
- query.setSubscribeStore(this);
62
- return query;
63
- }
64
-
65
- findAll(criteria, sort) {
66
- return this.store.findAll(criteria, sort);
67
- }
68
-
69
- findByKey(key, criteria) {
70
- return this.store.findByKey(key, criteria);
71
- }
72
-
73
- findOne(criteria, sort) {
74
- return this.store.findOne(criteria, sort);
75
- }
76
-
77
- async insertOne(object) {
78
- const inserted = await this.store.insertOne(object);
79
- this.callSubscribed({
80
- type: 'inserted',
81
- next: [inserted]
82
- });
83
- return inserted;
84
- }
85
-
86
- async replaceOne(object) {
87
- const replaced = await this.store.replaceOne(object);
88
- this.callSubscribed({
89
- type: 'updated',
90
- changes: [[object, replaced]]
91
- });
92
- return replaced;
93
- }
94
-
95
- async replaceSeveral(objects) {
96
- const replacedObjects = await this.store.replaceSeveral(objects);
97
- this.callSubscribed({
98
- type: 'updated',
99
- changes: objects.map((prev, index) => [prev, replacedObjects[index]])
100
- });
101
- return replacedObjects;
102
- }
103
-
104
- async upsertOne(object, setOnInsertPartialObject) {
105
- const result = await this.upsertOneWithInfo(object, setOnInsertPartialObject);
106
- return result.object;
107
- }
108
-
109
- async upsertOneWithInfo(object, setOnInsertPartialObject) {
110
- const upsertedWithInfo = await this.store.upsertOneWithInfo(object, setOnInsertPartialObject);
111
-
112
- if (upsertedWithInfo.inserted) {
113
- this.callSubscribed({
114
- type: 'inserted',
115
- next: [upsertedWithInfo.object]
116
- });
117
- } else {
118
- throw new Error('TODO');
119
- }
120
-
121
- return upsertedWithInfo;
122
- }
123
-
124
- async partialUpdateByKey(key, partialUpdate, criteria) {
125
- return this.partialUpdateOne(await this.findOne({
126
- [this.store.keyPath]: key,
127
- ...criteria
128
- }), partialUpdate);
129
- }
130
-
131
- async partialUpdateOne(object, partialUpdate) {
132
- const updated = await this.store.partialUpdateOne(object, partialUpdate);
133
- this.callSubscribed({
134
- type: 'updated',
135
- changes: [[object, updated]]
136
- });
137
- return updated;
138
- }
139
-
140
- async partialUpdateMany(criteria, partialUpdate) {
141
- const cursor = await this.store.cursor(criteria);
142
- const changes = [];
143
- await cursor.forEach(async model => {
144
- const key = model[this.store.keyPath];
145
- const updated = await this.store.partialUpdateByKey(key, partialUpdate, criteria);
146
- changes.push([model, updated]);
147
- });
148
- this.callSubscribed({
149
- type: 'updated',
150
- changes
151
- });
152
- }
153
-
154
- async deleteByKey(key, criteria) {
155
- return this.deleteOne(await this.findByKey(key, criteria));
156
- }
157
-
158
- async deleteOne(object) {
159
- await this.store.deleteOne(object);
160
- this.callSubscribed({
161
- type: 'deleted',
162
- prev: [object]
163
- });
164
- }
165
-
166
- async deleteMany(criteria) {
167
- const cursor = await this.store.cursor(criteria);
168
- const prev = await cursor.toArray();
169
- await this.store.deleteMany(criteria);
170
- this.callSubscribed({
171
- type: 'deleted',
172
- prev
173
- });
174
- }
175
-
176
- async cursor(criteria, sort) {
177
- const cursor = await this.store.cursor(criteria, sort);
178
- cursor.overrideStore(this);
179
- return cursor;
180
- }
181
-
182
- }
183
-
184
- export { AbstractSubscribableStoreQuery, SubscribeStore };
185
- //# sourceMappingURL=index-node12-dev.mjs.map