@nu-art/firebase-backend 0.400.5
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/ModuleBE_Firebase.d.ts +20 -0
- package/ModuleBE_Firebase.js +69 -0
- package/auth/FirebaseBaseWrapper.d.ts +19 -0
- package/auth/FirebaseBaseWrapper.js +39 -0
- package/auth/FirebaseSession_Admin.d.ts +19 -0
- package/auth/FirebaseSession_Admin.js +45 -0
- package/auth/firebase-session.d.ts +61 -0
- package/auth/firebase-session.js +93 -0
- package/database/DatabaseWrapperBE.d.ts +71 -0
- package/database/DatabaseWrapperBE.js +182 -0
- package/database/types.d.ts +4 -0
- package/database/types.js +18 -0
- package/firestore/FirestoreCollection.d.ts +71 -0
- package/firestore/FirestoreCollection.js +184 -0
- package/firestore/FirestoreInterface.d.ts +11 -0
- package/firestore/FirestoreInterface.js +111 -0
- package/firestore/FirestoreTransaction.d.ts +30 -0
- package/firestore/FirestoreTransaction.js +153 -0
- package/firestore/FirestoreWrapperBE.d.ts +16 -0
- package/firestore/FirestoreWrapperBE.js +53 -0
- package/firestore/types.d.ts +6 -0
- package/firestore/types.js +25 -0
- package/firestore-v3/DocWrapperV3.d.ts +32 -0
- package/firestore-v3/DocWrapperV3.js +148 -0
- package/firestore-v3/FirestoreCollectionV3.d.ts +154 -0
- package/firestore-v3/FirestoreCollectionV3.js +470 -0
- package/firestore-v3/FirestoreInterfaceV3.d.ts +10 -0
- package/firestore-v3/FirestoreInterfaceV3.js +107 -0
- package/firestore-v3/FirestoreWrapperBEV3.d.ts +16 -0
- package/firestore-v3/FirestoreWrapperBEV3.js +154 -0
- package/firestore-v3/consts.d.ts +13 -0
- package/firestore-v3/consts.js +18 -0
- package/firestore-v3/types.d.ts +6 -0
- package/firestore-v3/types.js +1 -0
- package/functions/firebase-function.d.ts +38 -0
- package/functions/firebase-function.js +53 -0
- package/functions-v2/ModuleBE_BaseFunction.d.ts +11 -0
- package/functions-v2/ModuleBE_BaseFunction.js +32 -0
- package/functions-v2/ModuleBE_ExpressFunction_V2.d.ts +11 -0
- package/functions-v2/ModuleBE_ExpressFunction_V2.js +29 -0
- package/functions-v2/ModuleBE_FirebaseDBListener.d.ts +10 -0
- package/functions-v2/ModuleBE_FirebaseDBListener.js +24 -0
- package/functions-v2/ModuleBE_FirebaseScheduler.d.ts +32 -0
- package/functions-v2/ModuleBE_FirebaseScheduler.js +57 -0
- package/functions-v2/ModuleBE_FirestoreListener.d.ts +13 -0
- package/functions-v2/ModuleBE_FirestoreListener.js +21 -0
- package/functions-v2/ModuleBE_PubSubFunction.d.ts +13 -0
- package/functions-v2/ModuleBE_PubSubFunction.js +47 -0
- package/functions-v2/ModuleBE_StorageListener.d.ts +13 -0
- package/functions-v2/ModuleBE_StorageListener.js +34 -0
- package/index.d.ts +21 -0
- package/index.js +38 -0
- package/package.json +75 -0
- package/push/PushMessagesWrapperBE.d.ts +14 -0
- package/push/PushMessagesWrapperBE.js +44 -0
- package/push/types.d.ts +3 -0
- package/push/types.js +18 -0
- package/storage/StorageWrapperBE.d.ts +63 -0
- package/storage/StorageWrapperBE.js +246 -0
- package/storage/emulator.d.ts +4 -0
- package/storage/emulator.js +46 -0
- package/storage/types.d.ts +4 -0
- package/storage/types.js +18 -0
- package/tools/lock-operation.d.ts +10 -0
- package/tools/lock-operation.js +35 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Created by tacb0ss on 25/08/2018.
|
|
3
|
+
*/
|
|
4
|
+
import { Module } from '@nu-art/ts-common';
|
|
5
|
+
import { FirebaseSession_Admin } from './auth/FirebaseSession_Admin.js';
|
|
6
|
+
import { TestResetListener } from '@nu-art/ts-common/testing/types';
|
|
7
|
+
type ConfigType = {
|
|
8
|
+
isEmulator?: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare const FIREBASE_DEFAULT_PROJECT_ID = "local";
|
|
11
|
+
export declare class ModuleBE_Firebase_Class extends Module<ConfigType> implements TestResetListener {
|
|
12
|
+
private readonly adminSessions;
|
|
13
|
+
constructor();
|
|
14
|
+
protected init(): void;
|
|
15
|
+
__resetForTests(): Promise<void>;
|
|
16
|
+
createAdminSession(authKey?: string): FirebaseSession_Admin;
|
|
17
|
+
createModuleStateFirebaseRef<T>(module: Module, _relativePath: string): import("./index.js").FirebaseRef<T>;
|
|
18
|
+
}
|
|
19
|
+
export declare const ModuleBE_Firebase: ModuleBE_Firebase_Class;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
/**
|
|
19
|
+
* Created by tacb0ss on 25/08/2018.
|
|
20
|
+
*/
|
|
21
|
+
import { Module } from '@nu-art/ts-common';
|
|
22
|
+
import { FirebaseSession_Admin } from './auth/FirebaseSession_Admin.js';
|
|
23
|
+
// import {FirebaseSession_UserPassword} from "./auth/FirebaseSession_UserPassword.js";
|
|
24
|
+
import { readFileSync } from 'fs';
|
|
25
|
+
import { ModuleBE_Auth } from '@nu-art/google-services-backend';
|
|
26
|
+
import { deleteApp, getApps } from 'firebase-admin/app';
|
|
27
|
+
export const FIREBASE_DEFAULT_PROJECT_ID = 'local';
|
|
28
|
+
export class ModuleBE_Firebase_Class extends Module {
|
|
29
|
+
adminSessions = {};
|
|
30
|
+
constructor() {
|
|
31
|
+
super('firebase');
|
|
32
|
+
}
|
|
33
|
+
init() {
|
|
34
|
+
}
|
|
35
|
+
async __resetForTests() {
|
|
36
|
+
this.logWarning('__resetForTests');
|
|
37
|
+
for (const key in this.adminSessions)
|
|
38
|
+
delete this.adminSessions[key];
|
|
39
|
+
await Promise.all(getApps().map(app => deleteApp(app)));
|
|
40
|
+
}
|
|
41
|
+
createAdminSession(authKey = FIREBASE_DEFAULT_PROJECT_ID) {
|
|
42
|
+
const session = this.adminSessions[authKey];
|
|
43
|
+
if (session)
|
|
44
|
+
return session;
|
|
45
|
+
// try to fetch the service account from the auth serviceAccount by the authKey
|
|
46
|
+
let serviceAccount;
|
|
47
|
+
try {
|
|
48
|
+
serviceAccount = ModuleBE_Auth.getAuthConfig(authKey);
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
if (authKey !== FIREBASE_DEFAULT_PROJECT_ID)
|
|
52
|
+
throw e;
|
|
53
|
+
}
|
|
54
|
+
// if we received a string we assume it is a path to the service account, and we load it into our serviceAccount
|
|
55
|
+
if (typeof serviceAccount === 'string')
|
|
56
|
+
serviceAccount = JSON.parse(readFileSync(serviceAccount, 'utf8'));
|
|
57
|
+
// define a unique key for the firebase session, and any following requests for this auth key
|
|
58
|
+
this.logInfo(`Creating Firebase session for auth key: ${authKey}`, serviceAccount);
|
|
59
|
+
return this.adminSessions[authKey] = new FirebaseSession_Admin(authKey, serviceAccount).connect();
|
|
60
|
+
}
|
|
61
|
+
createModuleStateFirebaseRef(module, _relativePath) {
|
|
62
|
+
let relativePath = _relativePath;
|
|
63
|
+
if (relativePath.startsWith('/'))
|
|
64
|
+
relativePath = relativePath.substring(1);
|
|
65
|
+
const path = `/state/${module.getName()}/${relativePath}`;
|
|
66
|
+
return ModuleBE_Firebase.createAdminSession().getDatabase().ref(path);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export const ModuleBE_Firebase = new ModuleBE_Firebase_Class();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { FirebaseSession } from './firebase-session.js';
|
|
2
|
+
import { Logger } from '@nu-art/ts-common';
|
|
3
|
+
/**
|
|
4
|
+
* base wrapper
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class FirebaseBaseWrapper extends Logger {
|
|
7
|
+
readonly firebaseSession: FirebaseSession<any>;
|
|
8
|
+
/**
|
|
9
|
+
* Constructor that takes a FirebaseSession object and assigns it to this.firebaseSession.
|
|
10
|
+
* @param firebaseSession
|
|
11
|
+
* @protected
|
|
12
|
+
*/
|
|
13
|
+
protected constructor(firebaseSession: FirebaseSession<any>);
|
|
14
|
+
/**
|
|
15
|
+
* Returns true if the user associated with this FirebaseSession is an admin, false otherwise.
|
|
16
|
+
*/
|
|
17
|
+
isAdmin(): boolean;
|
|
18
|
+
abstract isEmulator(): boolean;
|
|
19
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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 { Logger } from '@nu-art/ts-common';
|
|
19
|
+
/**
|
|
20
|
+
* base wrapper
|
|
21
|
+
*/
|
|
22
|
+
export class FirebaseBaseWrapper extends Logger {
|
|
23
|
+
firebaseSession;
|
|
24
|
+
/**
|
|
25
|
+
* Constructor that takes a FirebaseSession object and assigns it to this.firebaseSession.
|
|
26
|
+
* @param firebaseSession
|
|
27
|
+
* @protected
|
|
28
|
+
*/
|
|
29
|
+
constructor(firebaseSession) {
|
|
30
|
+
super();
|
|
31
|
+
this.firebaseSession = firebaseSession;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Returns true if the user associated with this FirebaseSession is an admin, false otherwise.
|
|
35
|
+
*/
|
|
36
|
+
isAdmin() {
|
|
37
|
+
return this.firebaseSession.isAdmin();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { JWTInput } from 'google-auth-library';
|
|
2
|
+
import { FirebaseSession } from './firebase-session.js';
|
|
3
|
+
/**
|
|
4
|
+
Represents an admin session for interacting with Firebase services.
|
|
5
|
+
*/
|
|
6
|
+
export declare class FirebaseSession_Admin extends FirebaseSession<JWTInput & {
|
|
7
|
+
databaseURL?: string;
|
|
8
|
+
isEmulator?: boolean;
|
|
9
|
+
} | undefined> {
|
|
10
|
+
constructor(firebaseAppName: string, config?: any);
|
|
11
|
+
/**
|
|
12
|
+
* Establishes a connection to the Firebase project using the configuration data and the project ID.
|
|
13
|
+
*/
|
|
14
|
+
connect(): this;
|
|
15
|
+
/**
|
|
16
|
+
Returns an instance of the Firebase Authentication service.
|
|
17
|
+
*/
|
|
18
|
+
getAuth(): import("firebase-admin/auth").Auth;
|
|
19
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
/**
|
|
19
|
+
* Created by tacb0ss on 19/09/2018.
|
|
20
|
+
*/
|
|
21
|
+
import { initializeApp } from 'firebase-admin/app';
|
|
22
|
+
import { FirebaseSession } from './firebase-session.js';
|
|
23
|
+
import { getAuth } from 'firebase-admin/auth';
|
|
24
|
+
/**
|
|
25
|
+
Represents an admin session for interacting with Firebase services.
|
|
26
|
+
*/
|
|
27
|
+
export class FirebaseSession_Admin extends FirebaseSession {
|
|
28
|
+
constructor(firebaseAppName, config) {
|
|
29
|
+
super(config, firebaseAppName);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Establishes a connection to the Firebase project using the configuration data and the project ID.
|
|
33
|
+
*/
|
|
34
|
+
connect() {
|
|
35
|
+
this.app = initializeApp(this.config, this.firebaseAppName);
|
|
36
|
+
this.logWarning(this.app.options);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
Returns an instance of the Firebase Authentication service.
|
|
41
|
+
*/
|
|
42
|
+
getAuth() {
|
|
43
|
+
return getAuth(this.app);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Created by tacb0ss on 25/08/2018.
|
|
3
|
+
*/
|
|
4
|
+
import { Logger, TypedMap } from '@nu-art/ts-common';
|
|
5
|
+
import { DatabaseWrapperBE } from '../database/DatabaseWrapperBE.js';
|
|
6
|
+
import { StorageWrapperBE } from '../storage/StorageWrapperBE.js';
|
|
7
|
+
import { PushMessagesWrapperBE } from '../push/PushMessagesWrapperBE.js';
|
|
8
|
+
import { App } from 'firebase-admin/app';
|
|
9
|
+
import { FirestoreWrapperBEV3 } from '../firestore-v3/FirestoreWrapperBEV3.js';
|
|
10
|
+
import { FirebaseConfig } from '@nu-art/firebase-shared';
|
|
11
|
+
import { FirestoreWrapperBE } from '../firestore/FirestoreWrapperBE.js';
|
|
12
|
+
/**
|
|
13
|
+
* Represents the credentials of a Firebase user.
|
|
14
|
+
*/
|
|
15
|
+
export type Firebase_UserCredential = {
|
|
16
|
+
config: FirebaseConfig;
|
|
17
|
+
credentials: {
|
|
18
|
+
user: string;
|
|
19
|
+
password: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* An abstract class that serves as a base for Firebase session classes.
|
|
24
|
+
*/
|
|
25
|
+
export declare abstract class FirebaseSession<Config> extends Logger {
|
|
26
|
+
app: App;
|
|
27
|
+
protected databases: TypedMap<DatabaseWrapperBE>;
|
|
28
|
+
protected storage?: StorageWrapperBE;
|
|
29
|
+
protected firestoresV3: TypedMap<FirestoreWrapperBEV3>;
|
|
30
|
+
protected messaging?: PushMessagesWrapperBE;
|
|
31
|
+
protected firestores: TypedMap<FirestoreWrapperBE>;
|
|
32
|
+
protected config: Config;
|
|
33
|
+
protected firebaseAppName: string;
|
|
34
|
+
private readonly admin;
|
|
35
|
+
/**
|
|
36
|
+
* Initializes a new instance of the FirebaseSession class.
|
|
37
|
+
* @param config The configuration settings for the Firebase session.
|
|
38
|
+
* @param sessionName The name of the Firebase session.
|
|
39
|
+
* @param _admin A value indicating whether the session is an admin session.
|
|
40
|
+
*/
|
|
41
|
+
protected constructor(config: Config, sessionName: string, _admin?: boolean);
|
|
42
|
+
isAdmin(): boolean;
|
|
43
|
+
abstract connect(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Returns an instance of the DatabaseWrapperBE object, which provides access to a database.
|
|
46
|
+
*/
|
|
47
|
+
getDatabase(dbName?: string): DatabaseWrapperBE;
|
|
48
|
+
/**
|
|
49
|
+
* Returns an instance of the StorageWrapperBE object, which provides access to a cloud storage service.
|
|
50
|
+
*/
|
|
51
|
+
getStorage(): StorageWrapperBE;
|
|
52
|
+
/**
|
|
53
|
+
* Returns an instance of the FirestoreWrapperBE object, which provides access to the Firestore database service.
|
|
54
|
+
*/
|
|
55
|
+
getFirestore(dbName?: string): FirestoreWrapperBE;
|
|
56
|
+
getFirestoreV3(dbName?: string): FirestoreWrapperBEV3;
|
|
57
|
+
/**
|
|
58
|
+
* Returns an instance of the PushMessagesWrapperBE object, which provides access to push messaging services.
|
|
59
|
+
*/
|
|
60
|
+
getMessaging(): PushMessagesWrapperBE;
|
|
61
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
/**
|
|
19
|
+
* Created by tacb0ss on 25/08/2018.
|
|
20
|
+
*/
|
|
21
|
+
import { Logger } from '@nu-art/ts-common';
|
|
22
|
+
import { DatabaseWrapperBE } from '../database/DatabaseWrapperBE.js';
|
|
23
|
+
import { StorageWrapperBE } from '../storage/StorageWrapperBE.js';
|
|
24
|
+
import { PushMessagesWrapperBE } from '../push/PushMessagesWrapperBE.js';
|
|
25
|
+
import { FirestoreWrapperBEV3 } from '../firestore-v3/FirestoreWrapperBEV3.js';
|
|
26
|
+
import { FirestoreWrapperBE } from '../firestore/FirestoreWrapperBE.js';
|
|
27
|
+
// export type FirebaseApp = admin.app.App | firebase.app.App
|
|
28
|
+
/**
|
|
29
|
+
* An abstract class that serves as a base for Firebase session classes.
|
|
30
|
+
*/
|
|
31
|
+
export class FirebaseSession extends Logger {
|
|
32
|
+
app;
|
|
33
|
+
databases = {};
|
|
34
|
+
storage;
|
|
35
|
+
firestoresV3 = {};
|
|
36
|
+
messaging;
|
|
37
|
+
firestores = {};
|
|
38
|
+
config;
|
|
39
|
+
firebaseAppName;
|
|
40
|
+
admin;
|
|
41
|
+
/**
|
|
42
|
+
* Initializes a new instance of the FirebaseSession class.
|
|
43
|
+
* @param config The configuration settings for the Firebase session.
|
|
44
|
+
* @param sessionName The name of the Firebase session.
|
|
45
|
+
* @param _admin A value indicating whether the session is an admin session.
|
|
46
|
+
*/
|
|
47
|
+
constructor(config, sessionName, _admin = true) {
|
|
48
|
+
super(`firebase: ${sessionName}`);
|
|
49
|
+
this.firebaseAppName = sessionName;
|
|
50
|
+
this.config = config;
|
|
51
|
+
this.admin = _admin;
|
|
52
|
+
}
|
|
53
|
+
isAdmin() {
|
|
54
|
+
return this.admin;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns an instance of the DatabaseWrapperBE object, which provides access to a database.
|
|
58
|
+
*/
|
|
59
|
+
getDatabase(dbName) {
|
|
60
|
+
if (!this.databases[dbName ?? 'default'])
|
|
61
|
+
this.databases[dbName ?? 'default'] = new DatabaseWrapperBE(this, dbName);
|
|
62
|
+
return this.databases[dbName ?? 'default'];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Returns an instance of the StorageWrapperBE object, which provides access to a cloud storage service.
|
|
66
|
+
*/
|
|
67
|
+
getStorage() {
|
|
68
|
+
if (this.storage)
|
|
69
|
+
return this.storage;
|
|
70
|
+
return this.storage = new StorageWrapperBE(this);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns an instance of the FirestoreWrapperBE object, which provides access to the Firestore database service.
|
|
74
|
+
*/
|
|
75
|
+
getFirestore(dbName) {
|
|
76
|
+
if (!this.firestores[dbName ?? 'default'])
|
|
77
|
+
this.firestores[dbName ?? 'default'] = new FirestoreWrapperBE(this, dbName);
|
|
78
|
+
return this.firestores[dbName ?? 'default'];
|
|
79
|
+
}
|
|
80
|
+
getFirestoreV3(dbName) {
|
|
81
|
+
if (!this.firestoresV3[dbName ?? 'default'])
|
|
82
|
+
this.firestoresV3[dbName ?? 'default'] = new FirestoreWrapperBEV3(this, dbName);
|
|
83
|
+
return this.firestoresV3[dbName ?? 'default'];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Returns an instance of the PushMessagesWrapperBE object, which provides access to push messaging services.
|
|
87
|
+
*/
|
|
88
|
+
getMessaging() {
|
|
89
|
+
if (this.messaging)
|
|
90
|
+
return this.messaging;
|
|
91
|
+
return this.messaging = new PushMessagesWrapperBE(this);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { TS_Object } from '@nu-art/ts-common';
|
|
2
|
+
import { FirebaseSession } from '../auth/firebase-session.js';
|
|
3
|
+
import { FirebaseBaseWrapper } from '../auth/FirebaseBaseWrapper.js';
|
|
4
|
+
import { Reference } from 'firebase-admin/database';
|
|
5
|
+
/**
|
|
6
|
+
* simplified interface for interacting with Firebase
|
|
7
|
+
*/
|
|
8
|
+
export declare class DatabaseWrapperBE extends FirebaseBaseWrapper {
|
|
9
|
+
private readonly database;
|
|
10
|
+
constructor(firebaseSession: FirebaseSession<any>, url?: string);
|
|
11
|
+
getUrl(): string | undefined;
|
|
12
|
+
get<T>(path: string, fallbackValue?: T): Promise<T>;
|
|
13
|
+
private getRef;
|
|
14
|
+
listen<T>(path: string, callback: (value?: T) => void): (a: import("firebase-admin/database").DataSnapshot | null, b?: string | null) => any;
|
|
15
|
+
stopListening(path: string, listener: any): void;
|
|
16
|
+
set<T>(path: string, value: T): Promise<void>;
|
|
17
|
+
uploadByChunks(parentPath: string, data: TS_Object, maxSizeMB?: number, itemsToRef?: Promise<any>[]): Promise<void>;
|
|
18
|
+
update<T>(path: string, value: T): Promise<void>;
|
|
19
|
+
patch<T>(path: string, value: Partial<T>): Promise<void>;
|
|
20
|
+
remove(path: string, assertionRegexp?: string): Promise<void>;
|
|
21
|
+
delete(path: string, assertionRegexp?: string): Promise<void>;
|
|
22
|
+
transaction<T>(path: string, func: (currentValue: T) => T): ReturnType<Reference['transaction']>;
|
|
23
|
+
ref<T>(path: string): FirebaseRef<T>;
|
|
24
|
+
isEmulator(): boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* simplified interface for interacting with Firebase
|
|
28
|
+
*/
|
|
29
|
+
export declare class FirebaseRef<T> {
|
|
30
|
+
private readonly db;
|
|
31
|
+
private readonly path;
|
|
32
|
+
constructor(db: DatabaseWrapperBE, path: string);
|
|
33
|
+
get(): Promise<T | undefined>;
|
|
34
|
+
get(fallbackValue: T): Promise<T>;
|
|
35
|
+
set(value: T): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Updates the specified path in the database with the provided partial value.
|
|
38
|
+
* @param value
|
|
39
|
+
*/
|
|
40
|
+
patch(value: Partial<T>): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Deletes the specified path in the database, subject to the provided assertion regular expression.
|
|
43
|
+
* @param assertionRegexp
|
|
44
|
+
*/
|
|
45
|
+
delete(assertionRegexp?: string): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Listens for changes at the specified path in the database and invokes the provided callback with the new value.
|
|
48
|
+
* @param callback
|
|
49
|
+
*/
|
|
50
|
+
listen(callback: (value?: T) => void): (a: import("firebase-admin/database").DataSnapshot | null, b?: string | null) => any;
|
|
51
|
+
/**
|
|
52
|
+
* Stops listening for changes at the specified path in the database with the provided listener.
|
|
53
|
+
* @param listener
|
|
54
|
+
*/
|
|
55
|
+
stopListening(listener: any): void;
|
|
56
|
+
/**
|
|
57
|
+
* Executes a transaction at the specified path in the database, using the provided function to update the current value.
|
|
58
|
+
* @param func
|
|
59
|
+
*/
|
|
60
|
+
transaction(func: (currentValue: T) => T): Promise<{
|
|
61
|
+
committed: boolean;
|
|
62
|
+
value: T;
|
|
63
|
+
}>;
|
|
64
|
+
/**
|
|
65
|
+
* Uploads a large object to the specified path in the database in chunks, with a maximum chunk size specified in MB.
|
|
66
|
+
* @param value
|
|
67
|
+
* @param maxSizeMB
|
|
68
|
+
* @param itemsToRef
|
|
69
|
+
*/
|
|
70
|
+
uploadByChunks(value: TS_Object, maxSizeMB?: number, itemsToRef?: Promise<any>[]): Promise<void>;
|
|
71
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
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 { BadImplementationException, calculateJsonSizeMb } from '@nu-art/ts-common';
|
|
19
|
+
import { FirebaseBaseWrapper } from '../auth/FirebaseBaseWrapper.js';
|
|
20
|
+
import { getDatabase, getDatabaseWithUrl } from 'firebase-admin/database';
|
|
21
|
+
/**
|
|
22
|
+
* simplified interface for interacting with Firebase
|
|
23
|
+
*/
|
|
24
|
+
export class DatabaseWrapperBE extends FirebaseBaseWrapper {
|
|
25
|
+
database;
|
|
26
|
+
constructor(firebaseSession, url) {
|
|
27
|
+
super(firebaseSession);
|
|
28
|
+
if (url)
|
|
29
|
+
this.database = getDatabaseWithUrl(url, firebaseSession.app);
|
|
30
|
+
else
|
|
31
|
+
this.database = getDatabase(firebaseSession.app);
|
|
32
|
+
}
|
|
33
|
+
getUrl() {
|
|
34
|
+
return this.database.app.options.databaseURL;
|
|
35
|
+
}
|
|
36
|
+
async get(path, fallbackValue) {
|
|
37
|
+
const snapshot = await this.getRef(path).once('value');
|
|
38
|
+
let toRet = fallbackValue;
|
|
39
|
+
if (snapshot)
|
|
40
|
+
toRet = snapshot.val();
|
|
41
|
+
if (!toRet)
|
|
42
|
+
toRet = fallbackValue;
|
|
43
|
+
return toRet;
|
|
44
|
+
}
|
|
45
|
+
getRef(path) {
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
return this.database.ref(path);
|
|
48
|
+
}
|
|
49
|
+
listen(path, callback) {
|
|
50
|
+
try {
|
|
51
|
+
return this.database.ref(path).on('value', (snapshot) => callback(snapshot ? snapshot.val() : undefined));
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
throw new BadImplementationException(`Error while getting value from path: ${path}`, e);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
stopListening(path, listener) {
|
|
58
|
+
try {
|
|
59
|
+
this.database.ref(path).off('value', listener);
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
throw new BadImplementationException(`Error while getting value from path: ${path}`, e);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async set(path, value) {
|
|
66
|
+
try {
|
|
67
|
+
return await this.database.ref(path).set(value);
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
throw new BadImplementationException(`Error while setting value to path: ${path}`, e);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async uploadByChunks(parentPath, data, maxSizeMB = 3, itemsToRef = []) {
|
|
74
|
+
for (const key in data) {
|
|
75
|
+
const node = `${parentPath}/${key}`;
|
|
76
|
+
if (calculateJsonSizeMb(data[key]) < maxSizeMB)
|
|
77
|
+
await this.set(node, data[key]);
|
|
78
|
+
else
|
|
79
|
+
await this.uploadByChunks(node, data[key], maxSizeMB, itemsToRef);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async update(path, value) {
|
|
83
|
+
this.logWarning('update will be deprecated!! please use patch');
|
|
84
|
+
return this.patch(path, value);
|
|
85
|
+
}
|
|
86
|
+
async patch(path, value) {
|
|
87
|
+
try {
|
|
88
|
+
return await this.database.ref(path).update(value);
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
this.logError(e);
|
|
92
|
+
throw new BadImplementationException(`Error while updating value to path: ${path}`, e);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async remove(path, assertionRegexp = '^/.*?/.*') {
|
|
96
|
+
this.logWarning('remove will be deprecated!! please use delete');
|
|
97
|
+
return this.delete(path, assertionRegexp);
|
|
98
|
+
}
|
|
99
|
+
async delete(path, assertionRegexp = '^/.*?/.*') {
|
|
100
|
+
if (!path)
|
|
101
|
+
throw new BadImplementationException(`Falsy value, path: '${path}'`);
|
|
102
|
+
if (!path.match(new RegExp(assertionRegexp)))
|
|
103
|
+
throw new BadImplementationException(`path: '${path}' does not match assertion: '${assertionRegexp}'`);
|
|
104
|
+
try {
|
|
105
|
+
return await this.database.ref(path).remove();
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
throw new BadImplementationException(`Error while removing path: ${path}`, e);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async transaction(path, func) {
|
|
112
|
+
return this.database.ref(path).transaction(func);
|
|
113
|
+
}
|
|
114
|
+
ref(path) {
|
|
115
|
+
return new FirebaseRef(this, path);
|
|
116
|
+
}
|
|
117
|
+
isEmulator() {
|
|
118
|
+
return !!process.env.FIREBASE_DATABASE_EMULATOR_HOST || false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* simplified interface for interacting with Firebase
|
|
123
|
+
*/
|
|
124
|
+
export class FirebaseRef {
|
|
125
|
+
db;
|
|
126
|
+
path;
|
|
127
|
+
constructor(db, path) {
|
|
128
|
+
this.db = db;
|
|
129
|
+
this.path = path;
|
|
130
|
+
}
|
|
131
|
+
get(fallbackValue) {
|
|
132
|
+
return this.db.get(this.path, fallbackValue);
|
|
133
|
+
}
|
|
134
|
+
set(value) {
|
|
135
|
+
return this.db.set(this.path, value);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Updates the specified path in the database with the provided partial value.
|
|
139
|
+
* @param value
|
|
140
|
+
*/
|
|
141
|
+
patch(value) {
|
|
142
|
+
return this.db.patch(this.path, value);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Deletes the specified path in the database, subject to the provided assertion regular expression.
|
|
146
|
+
* @param assertionRegexp
|
|
147
|
+
*/
|
|
148
|
+
delete(assertionRegexp = '^/.*?/.*') {
|
|
149
|
+
return this.db.delete(this.path, assertionRegexp);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Listens for changes at the specified path in the database and invokes the provided callback with the new value.
|
|
153
|
+
* @param callback
|
|
154
|
+
*/
|
|
155
|
+
listen(callback) {
|
|
156
|
+
return this.db.listen(this.path, callback);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Stops listening for changes at the specified path in the database with the provided listener.
|
|
160
|
+
* @param listener
|
|
161
|
+
*/
|
|
162
|
+
stopListening(listener) {
|
|
163
|
+
return this.db.stopListening(this.path, listener);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Executes a transaction at the specified path in the database, using the provided function to update the current value.
|
|
167
|
+
* @param func
|
|
168
|
+
*/
|
|
169
|
+
async transaction(func) {
|
|
170
|
+
const result = (await this.db.transaction(this.path, func));
|
|
171
|
+
return { committed: result.committed, value: result.snapshot.val() };
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Uploads a large object to the specified path in the database in chunks, with a maximum chunk size specified in MB.
|
|
175
|
+
* @param value
|
|
176
|
+
* @param maxSizeMB
|
|
177
|
+
* @param itemsToRef
|
|
178
|
+
*/
|
|
179
|
+
async uploadByChunks(value, maxSizeMB = 3, itemsToRef = []) {
|
|
180
|
+
return this.db.uploadByChunks(this.path, value, maxSizeMB, itemsToRef);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
export {};
|