@goatlab/fluent-firebase 0.7.2 → 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.
- package/CHANGELOG.md +1020 -0
- package/changelog-template.hbs +52 -0
- package/dist/FirebaseConnector.d.ts +8 -12
- package/dist/FirebaseConnector.js +117 -140
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
|
@@ -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}}
|
|
@@ -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>): FirebaseConnector<ModelDTO, InputDTO, OutputDTO>;
|
|
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
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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,14 +103,127 @@ 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
|
-
const
|
|
126
|
-
|
|
160
|
+
const newInstance = this.clone();
|
|
161
|
+
newInstance.setRelatedQuery({
|
|
162
|
+
entity: this.entity,
|
|
163
|
+
repository: this,
|
|
164
|
+
query: {
|
|
165
|
+
...query,
|
|
166
|
+
limit: 1
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
return newInstance;
|
|
170
|
+
}
|
|
171
|
+
loadById(id) {
|
|
172
|
+
const newInstance = this.clone();
|
|
173
|
+
newInstance.setRelatedQuery({
|
|
127
174
|
entity: this.entity,
|
|
128
175
|
repository: this,
|
|
129
|
-
query
|
|
176
|
+
query: {
|
|
177
|
+
where: {
|
|
178
|
+
id
|
|
179
|
+
}
|
|
180
|
+
}
|
|
130
181
|
});
|
|
131
|
-
return
|
|
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;
|
|
132
227
|
}
|
|
133
228
|
getGeneratedQueries(query) {
|
|
134
229
|
let { andWhere, orWhere } = this.getFirebaseWhereQuery(query?.where);
|
|
@@ -313,123 +408,5 @@ class FirebaseConnector extends fluent_2.BaseConnector {
|
|
|
313
408
|
orWhere: orWhereQueries
|
|
314
409
|
};
|
|
315
410
|
}
|
|
316
|
-
async updateById(id, data) {
|
|
317
|
-
const dataToInsert = this.outputKeys.includes('updated')
|
|
318
|
-
? {
|
|
319
|
-
...data,
|
|
320
|
-
...{ updated: new Date() }
|
|
321
|
-
}
|
|
322
|
-
: data;
|
|
323
|
-
const validatedData = this.inputSchema.parse(dataToInsert);
|
|
324
|
-
await this.collection.doc(id).update({
|
|
325
|
-
...validatedData,
|
|
326
|
-
id
|
|
327
|
-
});
|
|
328
|
-
const dbResult = await this.findById(id);
|
|
329
|
-
if (!dbResult) {
|
|
330
|
-
throw new Error(`Object not found: ${id}`);
|
|
331
|
-
}
|
|
332
|
-
return this.outputSchema?.parse(this.clearEmpties(js_utils_1.Objects.deleteNulls(dbResult)));
|
|
333
|
-
}
|
|
334
|
-
async replaceById(id, data) {
|
|
335
|
-
const value = await this.findById(id);
|
|
336
|
-
const flatValue = js_utils_1.Objects.flatten(JSON.parse(JSON.stringify(value)));
|
|
337
|
-
Object.keys(flatValue).forEach(key => {
|
|
338
|
-
flatValue[key] = null;
|
|
339
|
-
});
|
|
340
|
-
const nullObject = js_utils_1.Objects.nest(flatValue);
|
|
341
|
-
const newValue = { ...nullObject, ...data };
|
|
342
|
-
delete newValue._id;
|
|
343
|
-
delete newValue.id;
|
|
344
|
-
delete newValue.created;
|
|
345
|
-
delete newValue.updated;
|
|
346
|
-
const dataToInsert = this.outputKeys.includes('updated')
|
|
347
|
-
? {
|
|
348
|
-
...data,
|
|
349
|
-
...{ updated: new Date() }
|
|
350
|
-
}
|
|
351
|
-
: data;
|
|
352
|
-
const validatedData = this.inputSchema.parse(dataToInsert);
|
|
353
|
-
await this.collection
|
|
354
|
-
.doc(id)
|
|
355
|
-
.update(validatedData);
|
|
356
|
-
const val = await this.findById(id);
|
|
357
|
-
return this.outputSchema.parse(this.clearEmpties(js_utils_1.Objects.deleteNulls(val)));
|
|
358
|
-
}
|
|
359
|
-
async clear() {
|
|
360
|
-
const query = this.collection.orderBy('__name__').limit(300);
|
|
361
|
-
return new Promise((resolve, reject) => {
|
|
362
|
-
this.deleteQueryBatch(admin.firestore(), query, 300, resolve, reject);
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
deleteQueryBatch(db, query, batchSize, resolve, reject) {
|
|
366
|
-
query
|
|
367
|
-
.get()
|
|
368
|
-
.then(snapshot => {
|
|
369
|
-
if (snapshot.size == 0) {
|
|
370
|
-
return 0;
|
|
371
|
-
}
|
|
372
|
-
const batch = db.batch();
|
|
373
|
-
snapshot.docs.forEach(doc => {
|
|
374
|
-
batch.delete(doc.ref);
|
|
375
|
-
});
|
|
376
|
-
return batch.commit().then(() => snapshot.size);
|
|
377
|
-
})
|
|
378
|
-
.then(numDeleted => {
|
|
379
|
-
if (numDeleted === 0) {
|
|
380
|
-
resolve();
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
process.nextTick(() => {
|
|
384
|
-
this.deleteQueryBatch(db, query, batchSize, resolve, reject);
|
|
385
|
-
});
|
|
386
|
-
})
|
|
387
|
-
.catch(reject);
|
|
388
|
-
}
|
|
389
|
-
loadById(id) {
|
|
390
|
-
const newInstance = this.clone();
|
|
391
|
-
newInstance.setRelatedQuery({
|
|
392
|
-
entity: this.entity,
|
|
393
|
-
repository: this,
|
|
394
|
-
query: {
|
|
395
|
-
where: {
|
|
396
|
-
id
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
});
|
|
400
|
-
return newInstance;
|
|
401
|
-
}
|
|
402
|
-
clone() {
|
|
403
|
-
return new this.constructor();
|
|
404
|
-
}
|
|
405
|
-
async findByIds(ids, q) {
|
|
406
|
-
let data = await this.findMany({
|
|
407
|
-
where: {
|
|
408
|
-
id: {
|
|
409
|
-
in: ids
|
|
410
|
-
}
|
|
411
|
-
},
|
|
412
|
-
limit: q?.limit,
|
|
413
|
-
select: q?.select,
|
|
414
|
-
include: q?.include
|
|
415
|
-
});
|
|
416
|
-
return this.outputSchema?.array().parse(data);
|
|
417
|
-
}
|
|
418
|
-
async findById(id, q) {
|
|
419
|
-
const found = await this.findByIds([id], q);
|
|
420
|
-
return found[0];
|
|
421
|
-
}
|
|
422
|
-
async requireById(id, q) {
|
|
423
|
-
let found = await this.findById(id, {
|
|
424
|
-
select: q?.select,
|
|
425
|
-
include: q?.include,
|
|
426
|
-
limit: 1
|
|
427
|
-
});
|
|
428
|
-
if (!found) {
|
|
429
|
-
throw new Error(`Object ${id} not found`);
|
|
430
|
-
}
|
|
431
|
-
found = this.clearEmpties(js_utils_1.Objects.deleteNulls(found));
|
|
432
|
-
return this.outputSchema?.parse(found);
|
|
433
|
-
}
|
|
434
411
|
}
|
|
435
412
|
exports.FirebaseConnector = FirebaseConnector;
|