@livequery/client 1.0.23 → 1.0.24

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.
@@ -28,6 +28,8 @@ export declare class CollectionObservable<T extends {
28
28
  private ref;
29
29
  private collection_options;
30
30
  private is_collection_ref;
31
+ private collection_ref;
32
+ private document_id;
31
33
  constructor(ref: string, collection_options: CollectionOption<T>);
32
34
  private push_item;
33
35
  private sync;
@@ -36,11 +38,11 @@ export declare class CollectionObservable<T extends {
36
38
  reset(): void;
37
39
  fetch_more(): void;
38
40
  filter(filters: Partial<QueryOption<T>>): void;
39
- add(payload: T, local?: boolean): Promise<void>;
40
- remove(id: string): Promise<void>;
41
- update({ id, ...payload }: {
41
+ add(payload: T): Promise<void>;
42
+ remove(remove_document_id?: string): Promise<void>;
43
+ update({ id: update_payload_id, ...payload }: Partial<T & {
42
44
  id: string;
43
- } & Partial<T>): Promise<void>;
44
- trigger(name: string, document_id: string | null, payload?: object): Promise<unknown>;
45
+ }>): Promise<void>;
46
+ trigger(name: string, payload?: object, trigger_document_id?: string): Promise<unknown>;
45
47
  }
46
48
  export {};
@@ -38,7 +38,6 @@ exports.CollectionObservable = void 0;
38
38
  const rxjs_1 = require("rxjs");
39
39
  const get_sort_function_1 = require("./helpers/get_sort_function");
40
40
  const operators_1 = require("rxjs/operators");
41
- const uuid_1 = require("uuid");
42
41
  class CollectionObservable extends rxjs_1.Observable {
43
42
  constructor(ref, collection_options) {
44
43
  super(o => {
@@ -57,12 +56,15 @@ class CollectionObservable extends rxjs_1.Observable {
57
56
  _subscriptions.set(this, new Set());
58
57
  _state.set(this, void 0);
59
58
  _next_cursor.set(this, null);
59
+ if (ref.startsWith('/') || ref.endsWith('/'))
60
+ throw 'INVAILD_REF_FORMAT';
60
61
  const refs = ref.split('/');
61
62
  this.is_collection_ref = refs.length % 2 == 1;
63
+ this.collection_ref = refs.slice(0, refs.length - (this.is_collection_ref ? 0 : 1)).join('/');
64
+ this.document_id = this.is_collection_ref ? null : refs[refs.length - 1];
62
65
  }
63
66
  push_item(data) {
64
- const { id } = data;
65
- const item = Object.assign(Object.assign({ __adding: false, __updating: true, __removing: false }, data), { __remove: () => this.remove(id), __trigger: (name, payload) => this.trigger(name, id, payload), __update: (payload) => this.update(Object.assign({ id }, payload)) });
67
+ const item = Object.assign(Object.assign({ __adding: false, __updating: true, __removing: false }, data), { __remove: () => this.remove(data === null || data === void 0 ? void 0 : data.id), __trigger: (name, payload) => this.trigger(name, payload, data === null || data === void 0 ? void 0 : data.id), __update: (payload) => this.update(Object.assign({ id: data === null || data === void 0 ? void 0 : data.id }, payload)) });
66
68
  __classPrivateFieldGet(this, _state).items.push(item);
67
69
  }
68
70
  sync(stream) {
@@ -167,26 +169,14 @@ class CollectionObservable extends rxjs_1.Observable {
167
169
  filter(filters) {
168
170
  this.fetch_data(filters, true);
169
171
  }
170
- add(payload, local = false) {
172
+ add(payload) {
171
173
  return __awaiter(this, void 0, void 0, function* () {
172
- if (local) {
173
- const data = Object.assign(Object.assign({ id: uuid_1.v4() }, payload), { __adding: true });
174
- this.sync([{
175
- data: {
176
- changes: [{
177
- data: Object.assign(Object.assign({}, payload), { __adding: true }),
178
- ref: this.ref,
179
- type: 'added'
180
- }]
181
- }
182
- }]);
183
- return yield this.collection_options.transporter.add(`${this.ref}`, data);
184
- }
185
- return yield this.collection_options.transporter.add(`${this.ref}`, payload);
174
+ return yield this.collection_options.transporter.add(`${this.collection_ref}`, payload);
186
175
  });
187
176
  }
188
- remove(id) {
177
+ remove(remove_document_id) {
189
178
  return __awaiter(this, void 0, void 0, function* () {
179
+ const id = remove_document_id || this.document_id;
190
180
  this.sync([{
191
181
  data: {
192
182
  changes: [{
@@ -197,13 +187,14 @@ class CollectionObservable extends rxjs_1.Observable {
197
187
  }
198
188
  }]);
199
189
  // Trigger
200
- const ref = `${this.ref}${this.is_collection_ref ? `/${id}` : ''}`;
190
+ const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
201
191
  return yield this.collection_options.transporter.remove(ref);
202
192
  });
203
193
  }
204
194
  update(_a) {
205
- var { id } = _a, payload = __rest(_a, ["id"]);
195
+ var { id: update_payload_id } = _a, payload = __rest(_a, ["id"]);
206
196
  return __awaiter(this, void 0, void 0, function* () {
197
+ const id = update_payload_id || this.document_id;
207
198
  // Trigger local update
208
199
  this.sync([{
209
200
  data: {
@@ -214,13 +205,14 @@ class CollectionObservable extends rxjs_1.Observable {
214
205
  }]
215
206
  }
216
207
  }]);
217
- const ref = `${this.ref}${this.is_collection_ref ? `/${id}` : ''}`;
208
+ const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
218
209
  return yield this.collection_options.transporter.update(ref, payload);
219
210
  });
220
211
  }
221
- trigger(name, document_id, payload) {
212
+ trigger(name, payload, trigger_document_id) {
222
213
  return __awaiter(this, void 0, void 0, function* () {
223
- const ref = `${this.ref}${this.is_collection_ref ? `/${document_id}` : ''}`;
214
+ const id = trigger_document_id || this.document_id;
215
+ const ref = `${this.collection_ref}${id ? `/${id}` : ''}`;
224
216
  return yield this.collection_options.transporter.trigger(ref, name, {}, payload);
225
217
  });
226
218
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "repository": {
4
4
  "url": "https://github.com/livequery/client"
5
5
  },
6
- "version": "1.0.23",
6
+ "version": "1.0.24",
7
7
  "description": "",
8
8
  "main": "build/index.js",
9
9
  "types": "build/index.d.ts",