@nu-art/firebase-backend 0.500.0 → 0.500.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.
Files changed (56) hide show
  1. package/ModuleBE_Firebase.d.ts +15 -1
  2. package/ModuleBE_Firebase.js +39 -3
  3. package/auth/firebase-session.d.ts +12 -8
  4. package/auth/firebase-session.js +53 -15
  5. package/firestore/DocWrapper.d.ts +30 -0
  6. package/{firestore-v3/DocWrapperV3.js → firestore/DocWrapper.js} +30 -32
  7. package/firestore/FirestoreCollection.d.ts +141 -59
  8. package/firestore/FirestoreCollection.js +419 -147
  9. package/firestore/FirestoreInterface.d.ts +2 -3
  10. package/firestore/FirestoreInterface.js +1 -5
  11. package/firestore/FirestoreWrapperBE.d.ts +5 -6
  12. package/firestore/FirestoreWrapperBE.js +119 -9
  13. package/firestore/MongoCollection.d.ts +81 -0
  14. package/firestore/MongoCollection.js +426 -0
  15. package/firestore/MongoInterface.d.ts +18 -0
  16. package/firestore/MongoInterface.js +132 -0
  17. package/firestore/MongoWrapperBE.d.ts +18 -0
  18. package/firestore/MongoWrapperBE.js +95 -0
  19. package/firestore/consts.d.ts +23 -0
  20. package/firestore/consts.js +34 -0
  21. package/firestore/types.d.ts +6 -1
  22. package/firestore/types.js +0 -24
  23. package/{functions-v2 → functions}/ModuleBE_BaseFunction.d.ts +6 -3
  24. package/{functions-v2 → functions}/ModuleBE_BaseFunction.js +1 -0
  25. package/{functions-v2/ModuleBE_ExpressFunction_V2.d.ts → functions/ModuleBE_ExpressFunction_Class.d.ts} +5 -3
  26. package/{functions-v2/ModuleBE_ExpressFunction_V2.js → functions/ModuleBE_ExpressFunction_Class.js} +7 -3
  27. package/{functions-v2 → functions}/ModuleBE_FirebaseDBListener.d.ts +2 -1
  28. package/{functions-v2 → functions}/ModuleBE_FirebaseDBListener.js +1 -0
  29. package/{functions-v2 → functions}/ModuleBE_FirebaseScheduler.js +1 -0
  30. package/{functions-v2 → functions}/ModuleBE_FirestoreListener.d.ts +5 -1
  31. package/{functions-v2 → functions}/ModuleBE_FirestoreListener.js +1 -0
  32. package/{functions-v2 → functions}/ModuleBE_PubSubFunction.d.ts +5 -2
  33. package/{functions-v2 → functions}/ModuleBE_PubSubFunction.js +1 -0
  34. package/{functions-v2 → functions}/ModuleBE_StorageListener.js +1 -0
  35. package/index.d.ts +16 -13
  36. package/index.js +16 -13
  37. package/package.json +7 -6
  38. package/firestore/FirestoreTransaction.d.ts +0 -30
  39. package/firestore/FirestoreTransaction.js +0 -153
  40. package/firestore-v3/DocWrapperV3.d.ts +0 -33
  41. package/firestore-v3/FirestoreCollectionV3.d.ts +0 -155
  42. package/firestore-v3/FirestoreCollectionV3.js +0 -466
  43. package/firestore-v3/FirestoreInterfaceV3.d.ts +0 -10
  44. package/firestore-v3/FirestoreInterfaceV3.js +0 -107
  45. package/firestore-v3/FirestoreWrapperBEV3.d.ts +0 -16
  46. package/firestore-v3/FirestoreWrapperBEV3.js +0 -154
  47. package/firestore-v3/consts.d.ts +0 -13
  48. package/firestore-v3/consts.js +0 -18
  49. package/firestore-v3/types.d.ts +0 -6
  50. package/firestore-v3/types.js +0 -1
  51. package/functions/firebase-function.d.ts +0 -38
  52. package/functions/firebase-function.js +0 -53
  53. package/v1.d.ts +0 -21
  54. package/v1.js +0 -38
  55. /package/{functions-v2 → functions}/ModuleBE_FirebaseScheduler.d.ts +0 -0
  56. /package/{functions-v2 → functions}/ModuleBE_StorageListener.d.ts +0 -0
@@ -1,153 +0,0 @@
1
- /*
2
- * Firebase is a simpler Typescript wrapper to all of firebase services.
3
- *
4
- * Copyright (C) 2020 Adam van der Kruk aka TacB0sS
5
- *
6
- * Licensed under the Apache License, Version 2.0 (the "License");
7
- * you may not use this file except in compliance with the License.
8
- * You may obtain a copy of the License at
9
- *
10
- * http://www.apache.org/licenses/LICENSE-2.0
11
- *
12
- * Unless required by applicable law or agreed to in writing, software
13
- * distributed under the License is distributed on an "AS IS" BASIS,
14
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- * See the License for the specific language governing permissions and
16
- * limitations under the License.
17
- */
18
- import { DocWrapper, } from './FirestoreCollection.js';
19
- import { BadImplementationException, merge } from '@nu-art/ts-common';
20
- import { FirestoreInterface } from './FirestoreInterface.js';
21
- export class FirestoreTransaction {
22
- transaction;
23
- constructor(transaction) {
24
- this.transaction = transaction;
25
- }
26
- async _query(collection, ourQuery) {
27
- const query = FirestoreInterface.buildQuery(collection, ourQuery);
28
- return (await this.transaction.get(query)).docs;
29
- }
30
- async _queryUnique(collection, ourQuery) {
31
- const results = await this._query(collection, ourQuery);
32
- return FirestoreInterface.assertUniqueDocument(results, ourQuery, collection.name);
33
- }
34
- async _queryItem(collection, instance) {
35
- const ourQuery = FirestoreInterface.buildUniqueQuery(collection, instance);
36
- const results = await this._query(collection, ourQuery);
37
- return FirestoreInterface.assertUniqueDocument(results, ourQuery, collection.name);
38
- }
39
- async query(collection, ourQuery) {
40
- return (await this._query(collection, ourQuery)).map(result => result.data());
41
- }
42
- async queryItem(collection, instance) {
43
- const ourQuery = FirestoreInterface.buildUniqueQuery(collection, instance);
44
- return this.queryUnique(collection, ourQuery);
45
- }
46
- async queryUnique(collection, ourQuery) {
47
- const doc = await this._queryUnique(collection, ourQuery);
48
- if (!doc)
49
- return;
50
- return doc.data();
51
- }
52
- async insert(collection, instance, _id) {
53
- const doc = collection.createDocumentReference(_id);
54
- try {
55
- await this.transaction.create(doc, instance);
56
- }
57
- catch (e) {
58
- console.error('Failed creating object: ', instance, e);
59
- throw e;
60
- }
61
- return instance;
62
- }
63
- async insertAll(collection, instances, _ids) {
64
- return await Promise.all(instances.map((instance, i) => this.insert(collection, instance, _ids?.[i])));
65
- }
66
- //------------------------
67
- async upsert(collection, instance, _id) {
68
- return (await this.upsert_Read(collection, instance, _id))();
69
- }
70
- async upsert_Read(collection, instance, _id) {
71
- const ref = await this.getOrCreateDocument(collection, instance, _id);
72
- return async () => {
73
- try {
74
- await this.transaction.set(ref, instance);
75
- }
76
- catch (e) {
77
- console.error('Failed creating object: ', instance, e);
78
- throw e;
79
- }
80
- return instance;
81
- };
82
- }
83
- async getOrCreateDocument(collection, instance, _id) {
84
- let ref = (await this._queryItem(collection, instance))?.ref;
85
- if (!ref)
86
- ref = collection.createDocumentReference(_id);
87
- return ref;
88
- }
89
- async upsertAll(collection, instances, _ids) {
90
- if (instances.length > 500)
91
- throw new BadImplementationException('Firestore transaction supports maximum 500 at a time');
92
- return (await this.upsertAll_Read(collection, instances, _ids))();
93
- }
94
- async upsertAll_Read(collection, instances, _ids) {
95
- const writes = await Promise.all(instances.map(async (instance, i) => this.upsert_Read(collection, instance, _ids?.[i])));
96
- return async () => Promise.all(writes.map(async (_write) => _write()));
97
- }
98
- async patch(collection, instance) {
99
- return (await this.patch_Read(collection, instance))();
100
- }
101
- async patch_Read(collection, instance) {
102
- const doc = await this._queryItem(collection, instance);
103
- if (!doc)
104
- throw new BadImplementationException(`Patching a non existent doc for query ${FirestoreInterface.buildUniqueQuery(collection, instance)}`);
105
- return async () => {
106
- const patchedInstance = merge(await doc.data(), instance);
107
- this.transaction.set(doc.ref, patchedInstance);
108
- return patchedInstance;
109
- };
110
- }
111
- async delete(collection, ourQuery) {
112
- return await (await this.delete_Read(collection, ourQuery))();
113
- }
114
- async delete_Read(collection, ourQuery) {
115
- const docs = await this._query(collection, ourQuery);
116
- if (docs.length > 500)
117
- throw new BadImplementationException(`Trying to delete ${docs.length} documents. Not allowed more then 5oo in a single transaction`);
118
- return async () => {
119
- const toReturn = docs.map(doc => doc.data());
120
- await Promise.all(docs.map(async (doc) => this.transaction.delete(doc.ref)));
121
- return toReturn;
122
- };
123
- }
124
- async deleteItem(collection, instance) {
125
- return this.deleteUnique(collection, FirestoreInterface.buildUniqueQuery(collection, instance));
126
- }
127
- async deleteUnique(collection, ourQuery) {
128
- const write = await this.deleteUnique_Read(collection, ourQuery);
129
- if (!write)
130
- return;
131
- return write();
132
- }
133
- async deleteUnique_Read(collection, ourQuery) {
134
- const doc = (await this._queryUnique(collection, ourQuery));
135
- if (!doc)
136
- return;
137
- return async () => {
138
- const result = doc.data();
139
- await this.transaction.delete(doc.ref);
140
- return result;
141
- };
142
- }
143
- async newQueryUnique(collection, ourQuery) {
144
- const doc = await this._queryUnique(collection, ourQuery);
145
- if (!doc || !doc.exists)
146
- return;
147
- return new DocWrapper(collection.wrapper, doc);
148
- }
149
- async newQuery(collection, ourQuery) {
150
- const docs = await this._query(collection, ourQuery);
151
- return docs.filter(doc => doc.exists).map(doc => new DocWrapper(collection.wrapper, doc));
152
- }
153
- }
@@ -1,33 +0,0 @@
1
- import { DB_Prototype } from '@nu-art/db-api-shared';
2
- import { TS_Object, UniqueId } from '@nu-art/ts-common';
3
- import { FirestoreType_DocumentReference } from '../firestore/types.js';
4
- import { Transaction } from 'firebase-admin/firestore';
5
- import { FirestoreCollectionV3 } from './FirestoreCollectionV3.js';
6
- import type { firestore as Fa } from 'firebase-admin';
7
- export type UpdateObject<DBType extends TS_Object> = {
8
- _id: UniqueId;
9
- } & Fa.UpdateData<DBType>;
10
- export declare class DocWrapperV3<Proto extends DB_Prototype> {
11
- readonly ref: FirestoreType_DocumentReference<Proto['dbType']>;
12
- readonly collection: FirestoreCollectionV3<Proto>;
13
- data?: Proto['dbType'];
14
- protected constructor(collection: FirestoreCollectionV3<Proto>, ref: FirestoreType_DocumentReference<Proto['dbType']>, data?: Proto['dbType']);
15
- fromCache: () => Proto["dbType"] | undefined;
16
- cleanCache: () => DocWrapperV3<Proto>;
17
- private assertId;
18
- get: (transaction?: Transaction) => Promise<Proto["dbType"] | undefined>;
19
- prepareForCreate: (preDBItem: Proto["uiType"], transaction?: Transaction, upgrade?: boolean) => Promise<Proto["dbType"]>;
20
- create: (preDBItem: Proto["uiType"], transaction?: Transaction) => Promise<Proto["dbType"]>;
21
- prepareForSet: (updatedDBItem: Proto["dbType"], dbItem?: Proto["dbType"], transaction?: Transaction, upgrade?: boolean) => Promise<Proto["dbType"]>;
22
- set: (item: Proto["uiType"] | Proto["dbType"], transaction?: Transaction) => Promise<Proto["dbType"]>;
23
- private postWriteProcessing;
24
- prepareForUpdate(updateData: UpdateObject<Proto['dbType']>, transaction?: Transaction): Promise<UpdateObject<Proto["dbType"]>>;
25
- /**
26
- * Recursively replaces any undefined or null fields in DB item with firestore.FieldValue.delete()
27
- * @private
28
- * @param updateData
29
- */
30
- private updateDeletedFields;
31
- update: (updateData: UpdateObject<Proto["dbType"]>) => Promise<Proto["dbType"] | undefined>;
32
- delete: (transaction?: Transaction) => Promise<Proto["dbType"] | undefined>;
33
- }
@@ -1,155 +0,0 @@
1
- import { Database, DB_Prototype } from '@nu-art/db-api-shared';
2
- import { CustomException, InvalidResult, Logger, TS_Object, UniqueId } from '@nu-art/ts-common';
3
- import { FirestoreType_Collection, FirestoreType_DocumentReference } from '../firestore/types.js';
4
- import { Clause_Where, FirestoreQuery, MultiWriteOperation } from '@nu-art/firebase-shared';
5
- import { FirestoreWrapperBEV3 } from './FirestoreWrapperBEV3.js';
6
- import { Transaction } from 'firebase-admin/firestore';
7
- import { firestore } from 'firebase-admin';
8
- import { DocWrapperV3, UpdateObject } from './DocWrapperV3.js';
9
- import UpdateData = firestore.UpdateData;
10
- export type PostWriteProcessingData<DBType extends TS_Object> = {
11
- before?: DBType | DBType[];
12
- updated?: DBType | DBType[];
13
- deleted?: DBType | DBType[] | null;
14
- };
15
- export type CollectionActionType = 'create' | 'set' | 'update' | 'delete';
16
- export type FirestoreCollectionHooks<DBType extends TS_Object> = {
17
- canDeleteItems: (dbItems: DBType[], transaction?: Transaction) => Promise<void>;
18
- preWriteProcessing?: (dbInstance: DBType, originalDbInstance?: DBType, transaction?: Transaction) => Promise<void>;
19
- manipulateQuery?: (query: FirestoreQuery<DBType>) => FirestoreQuery<DBType>;
20
- postWriteProcessing?: (data: PostWriteProcessingData<DBType>, actionType: CollectionActionType, transaction?: Transaction) => Promise<void>;
21
- upgradeInstances: (instances: DBType[]) => Promise<any>;
22
- };
23
- export type MultiWriteItem<Op extends MultiWriteOperation, DBType extends TS_Object> = Op extends 'delete' ? undefined : Op extends 'update' ? UpdateObject<DBType> : DBType;
24
- type MultiWriteType = 'bulk' | 'batch';
25
- /**
26
- * # <ins>FirestoreBulkException</ins>
27
- * @category Exceptions
28
- */
29
- export declare class FirestoreBulkException extends CustomException {
30
- causes?: Error[];
31
- constructor(causes?: Error[]);
32
- }
33
- /**
34
- * FirestoreCollection is a class for handling Firestore collections.
35
- */
36
- export declare class FirestoreCollectionV3<Proto extends DB_Prototype> extends Logger {
37
- readonly wrapper: FirestoreWrapperBEV3;
38
- readonly collection: FirestoreType_Collection;
39
- readonly dbDef: Database<Proto>;
40
- readonly uniqueKeys: Proto['uniqueKeys'][] | string[];
41
- private readonly validator;
42
- readonly hooks?: FirestoreCollectionHooks<Proto['dbType']>;
43
- constructor(wrapper: FirestoreWrapperBEV3, _dbDef: Database<Proto>, hooks?: FirestoreCollectionHooks<Proto['dbType']>);
44
- doc: Readonly<{
45
- _: (ref: FirestoreType_DocumentReference<Proto["dbType"]>, data?: Proto["dbType"]) => DocWrapperV3<Proto>;
46
- unique: (id: Proto["uniqueParam"]) => DocWrapperV3<Proto>;
47
- item: (item: Proto["uiType"]) => DocWrapperV3<Proto>;
48
- all: (_ids: (Proto["uniqueParam"])[]) => DocWrapperV3<Proto>[];
49
- allItems: (preDBItems: Proto["uiType"][]) => DocWrapperV3<Proto>[];
50
- query: (query: FirestoreQuery<Proto["dbType"]>, transaction?: Transaction) => Promise<DocWrapperV3<Proto>[]>;
51
- unManipulatedQuery: (query: FirestoreQuery<Proto["dbType"]>, transaction?: Transaction) => Promise<DocWrapperV3<Proto>[]>;
52
- }>;
53
- private getAll;
54
- private _customQuery;
55
- query: Readonly<{
56
- unique: (_id: Proto["uniqueParam"], transaction?: Transaction) => Promise<Proto["dbType"] | undefined>;
57
- uniqueAssert: (_id: Proto["uniqueParam"], transaction?: Transaction) => Promise<Proto["dbType"]>;
58
- uniqueWhere: (where: Clause_Where<Proto["dbType"]>, transaction?: Transaction) => Promise<Proto["dbType"]>;
59
- uniqueCustom: (query: FirestoreQuery<Proto["dbType"]>, transaction?: Transaction) => Promise<Proto["dbType"]>;
60
- all: (_ids: (Proto["uniqueParam"])[], transaction?: Transaction) => Promise<(Proto["dbType"] | undefined)[]>;
61
- custom: (query: FirestoreQuery<Proto["dbType"]>, transaction?: Transaction) => Promise<Proto["dbType"][]>;
62
- where: (where: Clause_Where<Proto["dbType"]>, transaction?: Transaction) => Promise<Proto["dbType"][]>;
63
- unManipulatedQuery: (query: FirestoreQuery<Proto["dbType"]>, transaction?: Transaction) => Promise<Proto["dbType"][]>;
64
- }>;
65
- uniqueGetOrCreate: (where: Clause_Where<Proto["dbType"]>, toCreate: (transaction?: Transaction) => Promise<Proto["uiType"]>, transaction?: Transaction) => Promise<Proto["dbType"] | Proto["uiType"]>;
66
- protected _createAll: (preDBItems: Proto["uiType"][], transaction?: Transaction, multiWriteType?: MultiWriteType) => Promise<Proto["dbType"][]>;
67
- create: Readonly<{
68
- item: (preDBItem: Proto["uiType"], transaction?: Transaction) => Promise<Proto["dbType"]>;
69
- all: (preDBItems: Proto["uiType"][], transaction?: Transaction, multiWriteType?: MultiWriteType) => Promise<Proto["dbType"][]>;
70
- }>;
71
- private _setAll;
72
- set: Readonly<{
73
- item: (preDBItem: Proto["uiType"], transaction?: Transaction) => Promise<Proto["dbType"]>;
74
- all: (items: (Proto["uiType"] | Proto["dbType"])[], transaction?: Transaction) => Promise<Awaited<Proto["dbType"]>[]>;
75
- /**
76
- * Multi is a non atomic operation
77
- */
78
- multi: (items: (Proto["uiType"] | Proto["dbType"])[], transaction?: Transaction, multiWriteType?: MultiWriteType) => Promise<Awaited<Proto["dbType"]>[]>;
79
- }>;
80
- private upgradeInstances;
81
- protected _updateAll: (updateData: UpdateObject<Proto["dbType"]>[], multiWriteType?: MultiWriteType) => Promise<Proto["dbType"][]>;
82
- validateUpdateData(updateData: UpdateData<Proto['dbType']>, transaction?: Transaction): Promise<void>;
83
- protected _deleteQuery: (query: FirestoreQuery<Proto["dbType"]>, transaction?: Transaction, multiWriteType?: MultiWriteType) => Promise<NonNullable<Proto["dbType"]>[]>;
84
- protected _deleteUnManipulatedQuery: (query: FirestoreQuery<Proto["dbType"]>, transaction?: Transaction, multiWriteType?: MultiWriteType) => Promise<NonNullable<Proto["dbType"]>[]>;
85
- protected _deleteAll: (docsToBeDeleted: DocWrapperV3<Proto>[], transaction?: Transaction, multiWriteType?: MultiWriteType) => Promise<Proto["dbType"][]>;
86
- private deleteCollection;
87
- delete: Readonly<{
88
- unique: (id: Proto["uniqueParam"], transaction?: Transaction) => Promise<Proto["dbType"] | undefined>;
89
- item: (item: Proto["uiType"], transaction?: Transaction) => Promise<Proto["dbType"] | undefined>;
90
- all: (ids: (Proto["uniqueParam"])[], transaction?: Transaction) => Promise<Proto["dbType"][]>;
91
- allDocs: (docs: DocWrapperV3<Proto>[], transaction?: Transaction) => Promise<Proto["dbType"][]>;
92
- allItems: (items: Proto["uiType"][], transaction?: Transaction) => Promise<Proto["dbType"][]>;
93
- query: (query: FirestoreQuery<Proto["dbType"]>, transaction?: Transaction) => Promise<Proto["dbType"][]>;
94
- unManipulatedQuery: (query: FirestoreQuery<Proto["dbType"]>, transaction?: Transaction) => Promise<Proto["dbType"][]>;
95
- where: (where: Clause_Where<Proto["dbType"]>, transaction?: Transaction) => Promise<Proto["dbType"][]>;
96
- /**
97
- * Multi is a non atomic operation - doesn't use transactions. Use 'all' variants for transaction.
98
- */
99
- multi: {
100
- all: (ids: UniqueId[], multiWriteType?: MultiWriteType) => Promise<Proto["dbType"][]>;
101
- items: (items: Proto["uiType"][], multiWriteType?: MultiWriteType) => Promise<Proto["dbType"][]>;
102
- allDocs: (docs: DocWrapperV3<Proto>[], multiWriteType?: MultiWriteType) => Promise<Proto["dbType"][]>;
103
- query: (query: FirestoreQuery<Proto["dbType"]>, multiWriteType?: MultiWriteType) => Promise<NonNullable<Proto["dbType"]>[]>;
104
- };
105
- yes: {
106
- iam: {
107
- sure: {
108
- iwant: {
109
- todelete: {
110
- the: {
111
- collection: {
112
- delete: () => Promise<void>;
113
- };
114
- };
115
- };
116
- };
117
- };
118
- };
119
- };
120
- }>;
121
- /**
122
- * @param writer Type of BulkWriter - can be Bulk writer or Batch writer
123
- * @param doc
124
- * @param operation create/update/set/delete
125
- * @param item - mandatory for everything but delete
126
- */
127
- private addToMultiWrite;
128
- private multiWrite;
129
- private bulkWrite;
130
- /**
131
- * @param docs docs to write to
132
- * @param operation create/update/set/delete
133
- * @param items mandatory for everything but delete
134
- */
135
- private batchWrite;
136
- /**
137
- * A firestore transaction is run globally on the firestore project and not specifically on any collection, locking specific documents in the project.
138
- * @param processor
139
- * @param transaction A transaction that was provided to be used
140
- */
141
- runTransaction: <ReturnType>(processor: (transaction: Transaction) => Promise<ReturnType>, transaction?: Transaction) => Promise<ReturnType>;
142
- runTransactionInChunks: <T = any, R = any>(items: T[], processor: (chunk: typeof items, transaction: Transaction) => Promise<R[]>, chunkSize?: number) => Promise<R[]>;
143
- getVersion: () => any;
144
- needsUpgrade: (version?: string) => boolean;
145
- validateItem(dbItem: Proto['dbType']): void;
146
- protected onValidationError(instance: Proto['dbType'], results: InvalidResult<Proto['dbType']>): void;
147
- private assertNoDuplicatedIds;
148
- composeDbObjectUniqueId: (item: Proto["uiType"]) => string;
149
- }
150
- /**
151
- * If the collection has unique keys, assert they exist, and use them to generate the _id.
152
- * In the case an _id already exists, verify it is not different from the uniqueKeys-generated _id.
153
- */
154
- export declare const assertUniqueId: <Proto extends DB_Prototype>(item: Proto["dbType"], keys: Proto["uniqueKeys"]) => Proto["dbType"]["_id"];
155
- export {};