@goatlab/fluent-firebase 0.7.13 → 0.7.15

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.
@@ -10,9 +10,10 @@ export interface FirebaseConnectorParams<Input, Output> {
10
10
  export declare class FirebaseConnector<ModelDTO = AnyObject, InputDTO = ModelDTO, OutputDTO = InputDTO> extends BaseConnector<ModelDTO, InputDTO, OutputDTO> implements FluentConnectorInterface<ModelDTO, InputDTO, OutputDTO> {
11
11
  private readonly inputSchema;
12
12
  private readonly outputSchema;
13
- private readonly collection;
13
+ private collection;
14
14
  private readonly entity;
15
15
  constructor({ entity, inputSchema, outputSchema }: FirebaseConnectorParams<InputDTO, OutputDTO>);
16
+ initDB(): number;
16
17
  insert(data: InputDTO): Promise<OutputDTO>;
17
18
  insertMany(data: InputDTO[]): Promise<OutputDTO[]>;
18
19
  findMany<T extends FluentQuery<ModelDTO>>(query?: T): Promise<QueryOutput<T, ModelDTO>[]>;
@@ -13,7 +13,9 @@ class FirebaseConnector extends fluent_2.BaseConnector {
13
13
  this.outputSchema =
14
14
  outputSchema || inputSchema;
15
15
  this.entity = entity;
16
- const relationShipBuilder = fluent_1.modelGeneratorDataSource.getRepository(entity);
16
+ }
17
+ initDB() {
18
+ const relationShipBuilder = fluent_1.modelGeneratorDataSource.getRepository(this.entity);
17
19
  const name = relationShipBuilder.metadata.givenTableName;
18
20
  if (!name) {
19
21
  throw new Error(`Could not find table by name. Did you include @f.entity in your model?`);
@@ -24,8 +26,10 @@ class FirebaseConnector extends fluent_2.BaseConnector {
24
26
  const { relations } = (0, fluent_1.getRelationsFromModelGenerator)(relationShipBuilder);
25
27
  this.modelRelations = relations;
26
28
  this.outputKeys = (0, fluent_2.getOutputKeys)(relationShipBuilder) || [];
29
+ return 1;
27
30
  }
28
31
  async insert(data) {
32
+ this.initDB();
29
33
  const validatedData = this.inputSchema.parse(data);
30
34
  if (data['id']) {
31
35
  const found = await this.findById(data['id']);
@@ -42,6 +46,7 @@ class FirebaseConnector extends fluent_2.BaseConnector {
42
46
  return this.outputSchema.parse(js_utils_1.Objects.clearEmpties(js_utils_1.Objects.deleteNulls(item)));
43
47
  }
44
48
  async insertMany(data) {
49
+ this.initDB();
45
50
  const validatedData = this.inputSchema.array().parse(data);
46
51
  const batch = admin.firestore().batch();
47
52
  const batchInserted = [];
@@ -58,6 +63,7 @@ class FirebaseConnector extends fluent_2.BaseConnector {
58
63
  }));
59
64
  }
60
65
  async findMany(query) {
66
+ this.initDB();
61
67
  const [andQuery, orQueries] = this.getGeneratedQueries(query);
62
68
  const results = [];
63
69
  if (andQuery) {
@@ -104,6 +110,7 @@ class FirebaseConnector extends fluent_2.BaseConnector {
104
110
  return this.outputSchema?.array().parse(found);
105
111
  }
106
112
  async updateById(id, data) {
113
+ this.initDB();
107
114
  const dataToInsert = this.outputKeys.includes('updated')
108
115
  ? {
109
116
  ...data,
@@ -122,6 +129,7 @@ class FirebaseConnector extends fluent_2.BaseConnector {
122
129
  return this.outputSchema?.parse(js_utils_1.Objects.clearEmpties(js_utils_1.Objects.deleteNulls(dbResult)));
123
130
  }
124
131
  async replaceById(id, data) {
132
+ this.initDB();
125
133
  const value = await this.findById(id);
126
134
  const flatValue = js_utils_1.Objects.flatten(JSON.parse(JSON.stringify(value)));
127
135
  Object.keys(flatValue).forEach(key => {
@@ -147,16 +155,19 @@ class FirebaseConnector extends fluent_2.BaseConnector {
147
155
  return this.outputSchema.parse(js_utils_1.Objects.clearEmpties(js_utils_1.Objects.deleteNulls(val)));
148
156
  }
149
157
  async deleteById(id) {
158
+ this.initDB();
150
159
  await this.collection.doc(id).delete();
151
160
  return id;
152
161
  }
153
162
  async clear() {
163
+ this.initDB();
154
164
  const query = this.collection.orderBy('__name__').limit(300);
155
165
  return new Promise((resolve, reject) => {
156
166
  this.deleteQueryBatch(admin.firestore(), query, 300, resolve, reject);
157
167
  });
158
168
  }
159
169
  loadFirst(query) {
170
+ this.initDB();
160
171
  const newInstance = this.clone();
161
172
  newInstance.setRelatedQuery({
162
173
  entity: this.entity,
@@ -169,6 +180,7 @@ class FirebaseConnector extends fluent_2.BaseConnector {
169
180
  return newInstance;
170
181
  }
171
182
  loadById(id) {
183
+ this.initDB();
172
184
  const newInstance = this.clone();
173
185
  newInstance.setRelatedQuery({
174
186
  entity: this.entity,
@@ -182,12 +194,15 @@ class FirebaseConnector extends fluent_2.BaseConnector {
182
194
  return newInstance;
183
195
  }
184
196
  raw() {
197
+ this.initDB();
185
198
  return this.collection;
186
199
  }
187
200
  rawFirebase() {
201
+ this.initDB();
188
202
  return admin.firestore();
189
203
  }
190
204
  deleteQueryBatch(db, query, batchSize, resolve, reject) {
205
+ this.initDB();
191
206
  query
192
207
  .get()
193
208
  .then(snapshot => {
@@ -212,6 +227,7 @@ class FirebaseConnector extends fluent_2.BaseConnector {
212
227
  .catch(reject);
213
228
  }
214
229
  clone() {
230
+ this.initDB();
215
231
  return new this.constructor();
216
232
  }
217
233
  async loadRelatedData(data, loadedKeys) {
@@ -409,4 +425,10 @@ class FirebaseConnector extends fluent_2.BaseConnector {
409
425
  };
410
426
  }
411
427
  }
428
+ tslib_1.__decorate([
429
+ js_utils_1.Memo.syncMethod(),
430
+ tslib_1.__metadata("design:type", Function),
431
+ tslib_1.__metadata("design:paramtypes", []),
432
+ tslib_1.__metadata("design:returntype", void 0)
433
+ ], FirebaseConnector.prototype, "initDB", null);
412
434
  exports.FirebaseConnector = FirebaseConnector;