@nu-art/firebase-backend 0.401.8 → 0.500.0

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.
@@ -1,12 +1,13 @@
1
- import { DBProto, UniqueId } from '@nu-art/ts-common';
1
+ import { DB_Prototype } from '@nu-art/db-api-shared';
2
+ import { TS_Object, UniqueId } from '@nu-art/ts-common';
2
3
  import { FirestoreType_DocumentReference } from '../firestore/types.js';
3
4
  import { Transaction } from 'firebase-admin/firestore';
4
5
  import { FirestoreCollectionV3 } from './FirestoreCollectionV3.js';
5
6
  import type { firestore as Fa } from 'firebase-admin';
6
- export type UpdateObject<Proto extends DBProto<any>> = {
7
+ export type UpdateObject<DBType extends TS_Object> = {
7
8
  _id: UniqueId;
8
- } & Fa.UpdateData<Proto['dbType']>;
9
- export declare class DocWrapperV3<Proto extends DBProto<any>> {
9
+ } & Fa.UpdateData<DBType>;
10
+ export declare class DocWrapperV3<Proto extends DB_Prototype> {
10
11
  readonly ref: FirestoreType_DocumentReference<Proto['dbType']>;
11
12
  readonly collection: FirestoreCollectionV3<Proto>;
12
13
  data?: Proto['dbType'];
@@ -1,4 +1,5 @@
1
- import { CustomException, DBDef_V3, DBProto, InvalidResult, Logger, UniqueId } from '@nu-art/ts-common';
1
+ import { Database, DB_Prototype } from '@nu-art/db-api-shared';
2
+ import { CustomException, InvalidResult, Logger, TS_Object, UniqueId } from '@nu-art/ts-common';
2
3
  import { FirestoreType_Collection, FirestoreType_DocumentReference } from '../firestore/types.js';
3
4
  import { Clause_Where, FirestoreQuery, MultiWriteOperation } from '@nu-art/firebase-shared';
4
5
  import { FirestoreWrapperBEV3 } from './FirestoreWrapperBEV3.js';
@@ -6,20 +7,20 @@ import { Transaction } from 'firebase-admin/firestore';
6
7
  import { firestore } from 'firebase-admin';
7
8
  import { DocWrapperV3, UpdateObject } from './DocWrapperV3.js';
8
9
  import UpdateData = firestore.UpdateData;
9
- export type PostWriteProcessingData<Proto extends DBProto<any>> = {
10
- before?: Proto['dbType'] | Proto['dbType'][];
11
- updated?: Proto['dbType'] | Proto['dbType'][];
12
- deleted?: Proto['dbType'] | Proto['dbType'][] | null;
10
+ export type PostWriteProcessingData<DBType extends TS_Object> = {
11
+ before?: DBType | DBType[];
12
+ updated?: DBType | DBType[];
13
+ deleted?: DBType | DBType[] | null;
13
14
  };
14
15
  export type CollectionActionType = 'create' | 'set' | 'update' | 'delete';
15
- export type FirestoreCollectionHooks<Proto extends DBProto<any>> = {
16
- canDeleteItems: (dbItems: Proto['dbType'][], transaction?: Transaction) => Promise<void>;
17
- preWriteProcessing?: (dbInstance: Proto['dbType'], originalDbInstance?: Proto['dbType'], transaction?: Transaction) => Promise<void>;
18
- manipulateQuery?: (query: FirestoreQuery<Proto['dbType']>) => FirestoreQuery<Proto['dbType']>;
19
- postWriteProcessing?: (data: PostWriteProcessingData<Proto>, actionType: CollectionActionType, transaction?: Transaction) => Promise<void>;
20
- upgradeInstances: (instances: Proto['dbType'][]) => Promise<any>;
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>;
21
22
  };
22
- export type MultiWriteItem<Op extends MultiWriteOperation, Proto extends DBProto<any>> = Op extends 'delete' ? undefined : Op extends 'update' ? UpdateObject<Proto['dbType']> : Proto;
23
+ export type MultiWriteItem<Op extends MultiWriteOperation, DBType extends TS_Object> = Op extends 'delete' ? undefined : Op extends 'update' ? UpdateObject<DBType> : DBType;
23
24
  type MultiWriteType = 'bulk' | 'batch';
24
25
  /**
25
26
  * # <ins>FirestoreBulkException</ins>
@@ -32,14 +33,14 @@ export declare class FirestoreBulkException extends CustomException {
32
33
  /**
33
34
  * FirestoreCollection is a class for handling Firestore collections.
34
35
  */
35
- export declare class FirestoreCollectionV3<Proto extends DBProto<any>> extends Logger {
36
+ export declare class FirestoreCollectionV3<Proto extends DB_Prototype> extends Logger {
36
37
  readonly wrapper: FirestoreWrapperBEV3;
37
38
  readonly collection: FirestoreType_Collection;
38
- readonly dbDef: DBDef_V3<Proto>;
39
+ readonly dbDef: Database<Proto>;
39
40
  readonly uniqueKeys: Proto['uniqueKeys'][] | string[];
40
41
  private readonly validator;
41
42
  readonly hooks?: FirestoreCollectionHooks<Proto['dbType']>;
42
- constructor(wrapper: FirestoreWrapperBEV3, _dbDef: DBDef_V3<Proto>, hooks?: FirestoreCollectionHooks<Proto['dbType']>);
43
+ constructor(wrapper: FirestoreWrapperBEV3, _dbDef: Database<Proto>, hooks?: FirestoreCollectionHooks<Proto['dbType']>);
43
44
  doc: Readonly<{
44
45
  _: (ref: FirestoreType_DocumentReference<Proto["dbType"]>, data?: Proto["dbType"]) => DocWrapperV3<Proto>;
45
46
  unique: (id: Proto["uniqueParam"]) => DocWrapperV3<Proto>;
@@ -150,5 +151,5 @@ export declare class FirestoreCollectionV3<Proto extends DBProto<any>> extends L
150
151
  * If the collection has unique keys, assert they exist, and use them to generate the _id.
151
152
  * In the case an _id already exists, verify it is not different from the uniqueKeys-generated _id.
152
153
  */
153
- export declare const assertUniqueId: <Proto extends DBProto<any>>(item: Proto["dbType"], keys: Proto["uniqueKeys"]) => string;
154
+ export declare const assertUniqueId: <Proto extends DB_Prototype>(item: Proto["dbType"], keys: Proto["uniqueKeys"]) => Proto["dbType"]["_id"];
154
155
  export {};
@@ -15,7 +15,8 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
18
- import { __stringify, _keys, ApiException, BadImplementationException, batchActionParallel, compare, Const_UniqueKeys, CustomException, DB_Object_validator, dbIdLength, dbObjectToId, deepClone, DefaultDBVersion, Exception, exists, filterDuplicates, filterInstances, generateHex, keepPartialObject, Logger, MUSTNeverHappenException, StaticLogger, tsValidateResult, tsValidateUniqueId, ValidationException } from '@nu-art/ts-common';
18
+ import { dbObjectToId } from '@nu-art/db-api-shared';
19
+ import { __stringify, _keys, ApiException, BadImplementationException, batchActionParallel, compare, Const_UniqueKeys, CustomException, DB_Object_validator, dbIdLength, deepClone, DefaultDBVersion, Exception, exists, filterDuplicates, filterInstances, generateHex, keepPartialObject, Logger, MUSTNeverHappenException, StaticLogger, tsValidateResult, tsValidateUniqueId, ValidationException } from '@nu-art/ts-common';
19
20
  import { FirestoreInterfaceV3 } from './FirestoreInterfaceV3.js';
20
21
  import { DocWrapperV3 } from './DocWrapperV3.js';
21
22
  import { composeDbObjectUniqueId } from '@nu-art/firebase-shared';
@@ -73,7 +74,6 @@ export class FirestoreCollectionV3 extends Logger {
73
74
  this.validator = getDbDefValidator(_dbDef);
74
75
  this.hooks = hooks;
75
76
  }
76
- // ############################## DocWrapper ##############################
77
77
  doc = Object.freeze({
78
78
  _: (ref, data) => {
79
79
  if (tsValidateResult(ref.id, tsValidateUniqueId))
@@ -84,9 +84,12 @@ export class FirestoreCollectionV3 extends Logger {
84
84
  unique: (id) => {
85
85
  if (!id)
86
86
  throw new MUSTNeverHappenException('Did not receive an _id at doc.unique!');
87
+ let idStr;
87
88
  if (typeof id !== 'string')
88
- id = assertUniqueId(id, this.uniqueKeys);
89
- const doc = this.wrapper.firestore.doc(`${this.collection.path}/${id}`);
89
+ idStr = assertUniqueId(id, this.uniqueKeys);
90
+ else
91
+ idStr = id;
92
+ const doc = this.wrapper.firestore.doc(`${this.collection.path}/${idStr}`);
90
93
  return this.doc._(doc);
91
94
  },
92
95
  item: (item) => {
@@ -105,7 +108,6 @@ export class FirestoreCollectionV3 extends Logger {
105
108
  return (await this._customQuery(query, false, transaction)).map(_snapshot => this.doc._(_snapshot.ref, _snapshot.data()));
106
109
  },
107
110
  });
108
- // ############################## Query ##############################
109
111
  getAll = async (docs, transaction) => {
110
112
  if (docs.length === 0)
111
113
  return [];
@@ -155,7 +157,6 @@ export class FirestoreCollectionV3 extends Logger {
155
157
  return toCreate(transaction);
156
158
  }
157
159
  };
158
- // ############################## Create ##############################
159
160
  _createAll = async (preDBItems, transaction, multiWriteType = defaultMultiWriteType) => {
160
161
  if (preDBItems.length === 1)
161
162
  return [await this.create.item(preDBItems[0], transaction)];
@@ -174,7 +175,6 @@ export class FirestoreCollectionV3 extends Logger {
174
175
  .create(preDBItem, transaction),
175
176
  all: this._createAll,
176
177
  });
177
- // ############################## Set ##############################
178
178
  _setAll = async (items, transaction, multiWriteType = defaultMultiWriteType, performUpgrade = true) => {
179
179
  //Get all items
180
180
  const docs = this.doc.allItems(items);
@@ -220,7 +220,6 @@ export class FirestoreCollectionV3 extends Logger {
220
220
  upgradeInstances = (items) => {
221
221
  return this._setAll(items, undefined, defaultMultiWriteType, false);
222
222
  };
223
- // ############################## Update ##############################
224
223
  _updateAll = async (updateData, multiWriteType = defaultMultiWriteType) => {
225
224
  const docs = this.doc.all(updateData.map(_data => _data._id));
226
225
  const toUpdate = await Promise.all(docs.map(async (_doc, i) => await _doc.prepareForUpdate(updateData[i])));
@@ -235,7 +234,6 @@ export class FirestoreCollectionV3 extends Logger {
235
234
  // item: (updateData: UpdateObject<Proto['dbType']>) => this.doc.unique(updateData._id).update(updateData),
236
235
  // all: this._updateAll,
237
236
  // });
238
- // ############################## Delete ##############################
239
237
  _deleteQuery = async (query, transaction, multiWriteType = defaultMultiWriteType) => {
240
238
  if (!exists(query) || compare(query, _EmptyQuery))
241
239
  throw new MUSTNeverHappenException('An empty query was passed to delete.query!');
@@ -334,7 +332,6 @@ export class FirestoreCollectionV3 extends Logger {
334
332
  },
335
333
  yes: { iam: { sure: { iwant: { todelete: { the: { collection: { delete: this.deleteCollection } } } } } } }
336
334
  });
337
- // ############################## Multi Write ##############################
338
335
  /**
339
336
  * @param writer Type of BulkWriter - can be Bulk writer or Batch writer
340
337
  * @param doc
@@ -392,7 +389,6 @@ export class FirestoreCollectionV3 extends Logger {
392
389
  await batch.commit();
393
390
  }
394
391
  };
395
- // ############################## General ##############################
396
392
  /**
397
393
  * A firestore transaction is run globally on the firestore project and not specifically on any collection, locking specific documents in the project.
398
394
  * @param processor
@@ -460,7 +456,7 @@ export class FirestoreCollectionV3 extends Logger {
460
456
  export const assertUniqueId = (item, keys) => {
461
457
  // If there are no specific uniqueKeys, generate a random _id.
462
458
  if (compare(keys, Const_UniqueKeys))
463
- return item._id ?? generateHex(dbIdLength);
459
+ return (item._id ?? generateHex(dbIdLength));
464
460
  const _id = composeDbObjectUniqueId(item, keys);
465
461
  // If the item has an _id, and it matches the uniqueKeys-generated _id, all is well.
466
462
  // If the uniqueKeys-generated _id doesn't match the existing _id, this means someone had changed the uniqueKeys or _id which must never happen.
@@ -1,10 +1,10 @@
1
1
  import { FirestoreQuery } from '@nu-art/firebase-shared';
2
2
  import { FirestoreType_DocumentSnapshot } from '../firestore/types.js';
3
3
  import { FirestoreCollectionV3 } from './FirestoreCollectionV3.js';
4
- import { DBProto } from '@nu-art/ts-common';
4
+ import { DB_Prototype } from '@nu-art/db-api-shared';
5
5
  import { Query } from 'firebase-admin/firestore';
6
6
  export declare class FirestoreInterfaceV3 {
7
- static buildQuery<Proto extends DBProto<any>>(collection: FirestoreCollectionV3<Proto>, query?: FirestoreQuery<Proto['dbType']>): Query<FirebaseFirestore.DocumentData, FirebaseFirestore.DocumentData>;
7
+ static buildQuery<Proto extends DB_Prototype>(collection: FirestoreCollectionV3<Proto>, query?: FirestoreQuery<Proto['dbType']>): Query<FirebaseFirestore.DocumentData, FirebaseFirestore.DocumentData>;
8
8
  private static isQueryObject;
9
9
  static assertUniqueDocument(results: FirestoreType_DocumentSnapshot[], query: FirestoreQuery<any>, collectionName: string): (FirestoreType_DocumentSnapshot | undefined);
10
10
  }
@@ -2,15 +2,15 @@ import { FirestoreCollectionHooks, FirestoreCollectionV3 } from './FirestoreColl
2
2
  import { FirestoreType, FirestoreType_Collection } from '../firestore/types.js';
3
3
  import { FirebaseSession } from '../auth/firebase-session.js';
4
4
  import { FirebaseBaseWrapper } from '../auth/FirebaseBaseWrapper.js';
5
- import { DBDef_V3, DBProto } from '@nu-art/ts-common';
5
+ import { Database, DB_Prototype } from '@nu-art/db-api-shared';
6
6
  import { Transaction } from 'firebase-admin/firestore';
7
7
  export declare class FirestoreWrapperBEV3 extends FirebaseBaseWrapper {
8
8
  readonly firestore: FirestoreType;
9
9
  private readonly collections;
10
10
  constructor(firebaseSession: FirebaseSession<any>, dbName?: string);
11
11
  runTransaction: <ReturnType>(processor: (transaction: Transaction) => Promise<ReturnType>, transaction?: Transaction) => Promise<ReturnType>;
12
- getCollection<Proto extends DBProto<any>>(dbDef: DBDef_V3<Proto>, hooks?: FirestoreCollectionHooks<Proto['dbType']>): FirestoreCollectionV3<Proto>;
13
- listen<Proto extends DBProto<any>>(collection: FirestoreCollectionV3<Proto>, doc: string): void;
12
+ getCollection<Proto extends DB_Prototype>(dbDef: Database<Proto>, hooks?: FirestoreCollectionHooks<Proto['dbType']>): FirestoreCollectionV3<Proto>;
13
+ listen<Proto extends DB_Prototype>(collection: FirestoreCollectionV3<Proto>, doc: string): void;
14
14
  listCollections(): Promise<FirestoreType_Collection[]>;
15
15
  isEmulator(): boolean;
16
16
  }
@@ -1,6 +1,6 @@
1
- import { DBProto } from '@nu-art/ts-common';
1
+ import { DB_Prototype } from '@nu-art/db-api-shared';
2
2
  import { DB_EntityDependencyV2 } from '@nu-art/firebase-shared';
3
3
  import { Transaction } from 'firebase-admin/firestore';
4
4
  export type CanDeleteDBEntitiesProto = {
5
- __canDeleteEntitiesProto: <T extends DBProto<any>>(type: T['dbKey'], itemIds: string[], transaction?: Transaction) => Promise<DB_EntityDependencyV2>;
5
+ __canDeleteEntitiesProto: <T extends DB_Prototype>(type: T['dbKey'], itemIds: string[], transaction?: Transaction) => Promise<DB_EntityDependencyV2>;
6
6
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nu-art/firebase-backend",
3
- "version": "0.401.8",
3
+ "version": "0.500.0",
4
4
  "description": "Storm - Express & Typescript based backend framework Backend",
5
5
  "keywords": [
6
6
  "TacB0sS",
@@ -30,17 +30,19 @@
30
30
  "scripts": {
31
31
  "build": "tsc",
32
32
  "run-tests": "firebase emulators:exec \"npm run test\"",
33
- "test": "ts-mocha -w -p src/test/tsconfig.json --timeout 0 --inspect=8107 --watch-files '**/*.ts' src/test/firestore-v2/__test.ts",
33
+ "test": "ts-mocha -p src/test/tsconfig.json --timeout 0 'src/test/**/*.test.firebase.ts'",
34
+ "test:watch": "ts-mocha -w -p src/test/tsconfig.json --timeout 0 --inspect=8107 --watch-files '**/*.ts' 'src/test/**/*.test.firebase.ts'",
34
35
  "rtv2": "firebase emulators:exec \"npm run test-v2\"",
35
- "test-v2": "ts-mocha -w -p src/test/tsconfig.json --timeout 0 --inspect=8107 --watch-files '**/*.ts' src/test/firestore-v2/__test.ts"
36
+ "test-v2": "ts-mocha -p src/test/tsconfig.json --timeout 0 'src/test/**/*.test.firebase.ts'"
36
37
  },
37
38
  "dependencies": {
38
- "@nu-art/firebase-shared": "0.401.8",
39
+ "@nu-art/db-api-shared": "0.500.0",
40
+ "@nu-art/firebase-shared": "0.500.0",
39
41
  "@google-cloud/common": "^5.0.2",
40
42
  "@google-cloud/firestore": "^7.11.0",
41
43
  "@google-cloud/storage": "^7.15.0",
42
- "@nu-art/google-services-backend": "0.401.8",
43
- "@nu-art/ts-common": "0.401.8",
44
+ "@nu-art/google-services-backend": "0.500.0",
45
+ "@nu-art/ts-common": "0.500.0",
44
46
  "express": "^4.18.2",
45
47
  "firebase": "^11.9.0",
46
48
  "firebase-admin": "13.4.0",
@@ -51,7 +53,7 @@
51
53
  "ws": "^8.13.0"
52
54
  },
53
55
  "devDependencies": {
54
- "@nu-art/testalot": "0.401.8",
56
+ "@nu-art/testalot": "0.500.0",
55
57
  "@types/compression": "^1.0.1",
56
58
  "@types/chai": "^4.3.4",
57
59
  "@types/mocha": "^10.0.1",