@goatlab/fluent-firebase 0.7.4 → 0.7.6

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.
@@ -0,0 +1,52 @@
1
+ {{#each releases}}
2
+ #
3
+ {{title}}
4
+ [{{isoDate}}]
5
+
6
+ {{! lit commits with feature: in message, use feature: or Feature: }}
7
+ {{#commit-list
8
+ commits
9
+ heading='### Features'
10
+ message='(feat: )|(Feature: )'
11
+ exclude='(fix: )|(Fix: )|(break: )|(Break: )'
12
+ }}
13
+ *
14
+ {{subject}}
15
+ ([`{{shorthash}}`]({{href}}))
16
+ {{/commit-list}}
17
+
18
+ {{! use fix: or Fix: }}
19
+ {{#commit-list
20
+ commits
21
+ heading='### Fixes'
22
+ message='(fix: )|(Fix: )'
23
+ exclude='(break: )|(Break: )|(Feature: )|(feat: )'
24
+ }}
25
+ *
26
+ {{subject}}
27
+ ([`{{shorthash}}`]({{href}}))
28
+ {{/commit-list}}
29
+
30
+ {{! user break: or Break: }}
31
+ {{#commit-list
32
+ commits
33
+ heading='### Breaking changes'
34
+ message='(break: )|(Break: )'
35
+ exclude='(fix: )|(Fix: )|(Feat: )|(feat: )'
36
+ }}
37
+ *
38
+ {{subject}}
39
+ ([`{{shorthash}}`]({{href}}))
40
+ {{/commit-list}}
41
+
42
+ {{#commit-list
43
+ commits
44
+ heading='### Other Changes'
45
+ exclude='(fix: )|(Fix: )|(break: )|(Break: ) (feat: )|(Feat: )'
46
+ }}
47
+ *
48
+ {{subject}}
49
+ ([`{{shorthash}}`]({{href}}))
50
+ {{/commit-list}}
51
+
52
+ {{/each}}
File without changes
package/dist/Firebase.js CHANGED
File without changes
@@ -2,8 +2,6 @@ import * as admin from 'firebase-admin';
2
2
  import { AnyObject, FluentQuery, LoadedResult, QueryOutput } from '@goatlab/fluent';
3
3
  import { BaseConnector, FluentConnectorInterface } from '@goatlab/fluent';
4
4
  import { z } from 'zod';
5
- import { FindByIdFilter } from '@goatlab/fluent';
6
- import { SingleQueryOutput } from '@goatlab/fluent';
7
5
  export interface FirebaseConnectorParams<Input, Output> {
8
6
  entity: any;
9
7
  inputSchema: z.ZodType<Input>;
@@ -17,20 +15,18 @@ export declare class FirebaseConnector<ModelDTO = AnyObject, InputDTO = ModelDTO
17
15
  constructor({ entity, inputSchema, outputSchema }: FirebaseConnectorParams<InputDTO, OutputDTO>);
18
16
  insert(data: InputDTO): Promise<OutputDTO>;
19
17
  insertMany(data: InputDTO[]): Promise<OutputDTO[]>;
20
- raw(): admin.firestore.CollectionReference<ModelDTO>;
21
- rawFirebase(): admin.firestore.Firestore;
22
- protected loadRelatedData(data: any[], loadedKeys: AnyObject): Promise<admin.firestore.DocumentData[]>;
23
18
  findMany<T extends FluentQuery<ModelDTO>>(query?: T): Promise<QueryOutput<T, ModelDTO, OutputDTO>>;
24
- loadFirst(query?: FluentQuery<ModelDTO>): LoadedResult<this>;
25
- private getGeneratedQueries;
26
- private getFirebaseWhereQuery;
27
19
  updateById(id: string, data: InputDTO): Promise<OutputDTO>;
28
20
  replaceById(id: string, data: InputDTO): Promise<OutputDTO>;
21
+ deleteById(id: string): Promise<string>;
29
22
  clear(): Promise<boolean>;
30
- private deleteQueryBatch;
23
+ loadFirst(query?: FluentQuery<ModelDTO>): LoadedResult<this>;
31
24
  loadById(id: string): LoadedResult<this>;
25
+ raw(): admin.firestore.CollectionReference<ModelDTO>;
26
+ rawFirebase(): admin.firestore.Firestore;
27
+ private deleteQueryBatch;
32
28
  protected clone(): any;
33
- findByIds<T extends FindByIdFilter<ModelDTO>>(ids: string[], q?: T): Promise<QueryOutput<T, ModelDTO, OutputDTO>>;
34
- findById<T extends FindByIdFilter<ModelDTO>>(id: string, q?: T): Promise<SingleQueryOutput<T, ModelDTO, OutputDTO>>;
35
- requireById(id: string, q?: FindByIdFilter<ModelDTO>): Promise<SingleQueryOutput<FindByIdFilter<ModelDTO>, ModelDTO, OutputDTO>>;
29
+ protected loadRelatedData(data: any[], loadedKeys: AnyObject): Promise<admin.firestore.DocumentData[]>;
30
+ private getGeneratedQueries;
31
+ private getFirebaseWhereQuery;
36
32
  }
@@ -57,24 +57,6 @@ class FirebaseConnector extends fluent_2.BaseConnector {
57
57
  return this.clearEmpties(js_utils_1.Objects.deleteNulls(d));
58
58
  }));
59
59
  }
60
- raw() {
61
- return this.collection;
62
- }
63
- rawFirebase() {
64
- return admin.firestore();
65
- }
66
- async loadRelatedData(data, loadedKeys) {
67
- let pivotData = [];
68
- const result = await (0, fluent_2.loadRelations)({
69
- data,
70
- relations: loadedKeys,
71
- modelRelations: this.modelRelations,
72
- provider: 'firebase',
73
- self: this,
74
- returnPivot: false
75
- });
76
- return result;
77
- }
78
60
  async findMany(query) {
79
61
  const [andQuery, orQueries] = this.getGeneratedQueries(query);
80
62
  const results = [];
@@ -121,6 +103,59 @@ class FirebaseConnector extends fluent_2.BaseConnector {
121
103
  }
122
104
  return this.outputSchema?.array().parse(found);
123
105
  }
106
+ async updateById(id, data) {
107
+ const dataToInsert = this.outputKeys.includes('updated')
108
+ ? {
109
+ ...data,
110
+ ...{ updated: new Date() }
111
+ }
112
+ : data;
113
+ const validatedData = this.inputSchema.parse(dataToInsert);
114
+ await this.collection.doc(id).update({
115
+ ...validatedData,
116
+ id
117
+ });
118
+ const dbResult = await this.findById(id);
119
+ if (!dbResult) {
120
+ throw new Error(`Object not found: ${id}`);
121
+ }
122
+ return this.outputSchema?.parse(this.clearEmpties(js_utils_1.Objects.deleteNulls(dbResult)));
123
+ }
124
+ async replaceById(id, data) {
125
+ const value = await this.findById(id);
126
+ const flatValue = js_utils_1.Objects.flatten(JSON.parse(JSON.stringify(value)));
127
+ Object.keys(flatValue).forEach(key => {
128
+ flatValue[key] = null;
129
+ });
130
+ const nullObject = js_utils_1.Objects.nest(flatValue);
131
+ const newValue = { ...nullObject, ...data };
132
+ delete newValue._id;
133
+ delete newValue.id;
134
+ delete newValue.created;
135
+ delete newValue.updated;
136
+ const dataToInsert = this.outputKeys.includes('updated')
137
+ ? {
138
+ ...data,
139
+ ...{ updated: new Date() }
140
+ }
141
+ : data;
142
+ const validatedData = this.inputSchema.parse(dataToInsert);
143
+ await this.collection
144
+ .doc(id)
145
+ .update(validatedData);
146
+ const val = await this.requireById(id);
147
+ return this.outputSchema.parse(this.clearEmpties(js_utils_1.Objects.deleteNulls(val)));
148
+ }
149
+ async deleteById(id) {
150
+ await this.collection.doc(id).delete();
151
+ return id;
152
+ }
153
+ async clear() {
154
+ const query = this.collection.orderBy('__name__').limit(300);
155
+ return new Promise((resolve, reject) => {
156
+ this.deleteQueryBatch(admin.firestore(), query, 300, resolve, reject);
157
+ });
158
+ }
124
159
  loadFirst(query) {
125
160
  const newInstance = this.clone();
126
161
  newInstance.setRelatedQuery({
@@ -133,6 +168,63 @@ class FirebaseConnector extends fluent_2.BaseConnector {
133
168
  });
134
169
  return newInstance;
135
170
  }
171
+ loadById(id) {
172
+ const newInstance = this.clone();
173
+ newInstance.setRelatedQuery({
174
+ entity: this.entity,
175
+ repository: this,
176
+ query: {
177
+ where: {
178
+ id
179
+ }
180
+ }
181
+ });
182
+ return newInstance;
183
+ }
184
+ raw() {
185
+ return this.collection;
186
+ }
187
+ rawFirebase() {
188
+ return admin.firestore();
189
+ }
190
+ deleteQueryBatch(db, query, batchSize, resolve, reject) {
191
+ query
192
+ .get()
193
+ .then(snapshot => {
194
+ if (snapshot.size == 0) {
195
+ return 0;
196
+ }
197
+ const batch = db.batch();
198
+ snapshot.docs.forEach(doc => {
199
+ batch.delete(doc.ref);
200
+ });
201
+ return batch.commit().then(() => snapshot.size);
202
+ })
203
+ .then(numDeleted => {
204
+ if (numDeleted === 0) {
205
+ resolve();
206
+ return;
207
+ }
208
+ process.nextTick(() => {
209
+ this.deleteQueryBatch(db, query, batchSize, resolve, reject);
210
+ });
211
+ })
212
+ .catch(reject);
213
+ }
214
+ clone() {
215
+ return new this.constructor();
216
+ }
217
+ async loadRelatedData(data, loadedKeys) {
218
+ const result = await (0, fluent_2.loadRelations)({
219
+ data,
220
+ relations: loadedKeys,
221
+ modelRelations: this.modelRelations,
222
+ provider: 'firebase',
223
+ self: this,
224
+ returnPivot: false
225
+ });
226
+ return result;
227
+ }
136
228
  getGeneratedQueries(query) {
137
229
  let { andWhere, orWhere } = this.getFirebaseWhereQuery(query?.where);
138
230
  let mergedQueries = [];
@@ -316,123 +408,5 @@ class FirebaseConnector extends fluent_2.BaseConnector {
316
408
  orWhere: orWhereQueries
317
409
  };
318
410
  }
319
- async updateById(id, data) {
320
- const dataToInsert = this.outputKeys.includes('updated')
321
- ? {
322
- ...data,
323
- ...{ updated: new Date() }
324
- }
325
- : data;
326
- const validatedData = this.inputSchema.parse(dataToInsert);
327
- await this.collection.doc(id).update({
328
- ...validatedData,
329
- id
330
- });
331
- const dbResult = await this.findById(id);
332
- if (!dbResult) {
333
- throw new Error(`Object not found: ${id}`);
334
- }
335
- return this.outputSchema?.parse(this.clearEmpties(js_utils_1.Objects.deleteNulls(dbResult)));
336
- }
337
- async replaceById(id, data) {
338
- const value = await this.findById(id);
339
- const flatValue = js_utils_1.Objects.flatten(JSON.parse(JSON.stringify(value)));
340
- Object.keys(flatValue).forEach(key => {
341
- flatValue[key] = null;
342
- });
343
- const nullObject = js_utils_1.Objects.nest(flatValue);
344
- const newValue = { ...nullObject, ...data };
345
- delete newValue._id;
346
- delete newValue.id;
347
- delete newValue.created;
348
- delete newValue.updated;
349
- const dataToInsert = this.outputKeys.includes('updated')
350
- ? {
351
- ...data,
352
- ...{ updated: new Date() }
353
- }
354
- : data;
355
- const validatedData = this.inputSchema.parse(dataToInsert);
356
- await this.collection
357
- .doc(id)
358
- .update(validatedData);
359
- const val = await this.findById(id);
360
- return this.outputSchema.parse(this.clearEmpties(js_utils_1.Objects.deleteNulls(val)));
361
- }
362
- async clear() {
363
- const query = this.collection.orderBy('__name__').limit(300);
364
- return new Promise((resolve, reject) => {
365
- this.deleteQueryBatch(admin.firestore(), query, 300, resolve, reject);
366
- });
367
- }
368
- deleteQueryBatch(db, query, batchSize, resolve, reject) {
369
- query
370
- .get()
371
- .then(snapshot => {
372
- if (snapshot.size == 0) {
373
- return 0;
374
- }
375
- const batch = db.batch();
376
- snapshot.docs.forEach(doc => {
377
- batch.delete(doc.ref);
378
- });
379
- return batch.commit().then(() => snapshot.size);
380
- })
381
- .then(numDeleted => {
382
- if (numDeleted === 0) {
383
- resolve();
384
- return;
385
- }
386
- process.nextTick(() => {
387
- this.deleteQueryBatch(db, query, batchSize, resolve, reject);
388
- });
389
- })
390
- .catch(reject);
391
- }
392
- loadById(id) {
393
- const newInstance = this.clone();
394
- newInstance.setRelatedQuery({
395
- entity: this.entity,
396
- repository: this,
397
- query: {
398
- where: {
399
- id
400
- }
401
- }
402
- });
403
- return newInstance;
404
- }
405
- clone() {
406
- return new this.constructor();
407
- }
408
- async findByIds(ids, q) {
409
- let data = await this.findMany({
410
- where: {
411
- id: {
412
- in: ids
413
- }
414
- },
415
- limit: q?.limit,
416
- select: q?.select,
417
- include: q?.include
418
- });
419
- return this.outputSchema?.array().parse(data);
420
- }
421
- async findById(id, q) {
422
- const found = await this.findByIds([id], q);
423
- return found[0];
424
- }
425
- async requireById(id, q) {
426
- let found = await this.findById(id, {
427
- select: q?.select,
428
- include: q?.include,
429
- limit: 1
430
- });
431
- if (!found) {
432
- throw new Error(`Object ${id} not found`);
433
- }
434
- found = this.clearEmpties(js_utils_1.Objects.deleteNulls(found));
435
- return this.outputSchema?.parse(found);
436
- }
437
411
  }
438
412
  exports.FirebaseConnector = FirebaseConnector;
File without changes
File without changes
package/dist/index.d.ts CHANGED
File without changes
package/dist/index.js CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes