@goatlab/fluent-firebase 0.7.4 → 0.7.8

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.
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
- 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;
18
+ findMany<T extends FluentQuery<ModelDTO>>(query?: T): Promise<QueryOutput<T, ModelDTO>[]>;
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
  }
@@ -39,7 +39,7 @@ class FirebaseConnector extends fluent_2.BaseConnector {
39
39
  ...validatedData
40
40
  };
41
41
  await this.collection.doc(id).set(item);
42
- return this.outputSchema.parse(this.clearEmpties(js_utils_1.Objects.deleteNulls(item)));
42
+ return this.outputSchema.parse(js_utils_1.Objects.clearEmpties(js_utils_1.Objects.deleteNulls(item)));
43
43
  }
44
44
  async insertMany(data) {
45
45
  const validatedData = this.inputSchema.array().parse(data);
@@ -54,27 +54,9 @@ class FirebaseConnector extends fluent_2.BaseConnector {
54
54
  });
55
55
  await batch.commit();
56
56
  return this.outputSchema.array().parse(batchInserted.map(d => {
57
- return this.clearEmpties(js_utils_1.Objects.deleteNulls(d));
57
+ return js_utils_1.Objects.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 = [];
@@ -96,7 +78,7 @@ class FirebaseConnector extends fluent_2.BaseConnector {
96
78
  }
97
79
  let found = [...new Map(results.map(v => [v.id, v])).values()];
98
80
  found.map(d => {
99
- this.clearEmpties(js_utils_1.Objects.deleteNulls(d));
81
+ js_utils_1.Objects.clearEmpties(js_utils_1.Objects.deleteNulls(d));
100
82
  });
101
83
  if (query?.include) {
102
84
  found = await this.loadRelatedData(found, js_utils_1.Objects.flatten(query?.include || {}));
@@ -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(js_utils_1.Objects.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(js_utils_1.Objects.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 = [];
@@ -170,8 +262,8 @@ class FirebaseConnector extends fluent_2.BaseConnector {
170
262
  }
171
263
  let andWhereQuery = this.collection;
172
264
  let orWhereQueries = [];
173
- const orConditions = this.extractConditions(where['OR']);
174
- const andConditions = this.extractConditions(where['AND']);
265
+ const orConditions = (0, fluent_1.extractConditions)(where['OR']);
266
+ const andConditions = (0, fluent_1.extractConditions)(where['AND']);
175
267
  const copy = js_utils_1.Objects.clone(where);
176
268
  if (!!copy['AND']) {
177
269
  delete copy['AND'];
@@ -179,7 +271,7 @@ class FirebaseConnector extends fluent_2.BaseConnector {
179
271
  if (!!copy['OR']) {
180
272
  delete copy['OR'];
181
273
  }
182
- const rootLevelConditions = this.extractConditions([copy]);
274
+ const rootLevelConditions = (0, fluent_1.extractConditions)([copy]);
183
275
  for (const condition of andConditions) {
184
276
  const { element, operator, value } = condition;
185
277
  switch (operator) {
@@ -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
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FirebaseInit = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const admin = tslib_1.__importStar(require("firebase-admin"));
6
- const fireorm = tslib_1.__importStar(require("fireorm"));
7
6
  const FirebaseInit = ({ host, port, databaseName, serviceAccount }) => {
8
7
  if (admin.apps.length) {
9
8
  return;
@@ -19,7 +18,6 @@ const FirebaseInit = ({ host, port, databaseName, serviceAccount }) => {
19
18
  });
20
19
  const fireStore = admin.firestore();
21
20
  fireStore.settings({ ignoreUndefinedProperties: true });
22
- fireorm.initialize(fireStore);
23
21
  if (!fireStore) {
24
22
  throw new Error('Could not initialize FireStore');
25
23
  }
package/dist/index.d.ts CHANGED
File without changes
package/dist/index.js CHANGED
File without changes