@data-weave/backend-firestore 0.4.2 → 0.4.3

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,251 +0,0 @@
1
- import { WithoutId } from '@data-weave/datamanager'
2
- import type FirestoreTypes from '@firebase/firestore'
3
- import type { FieldPath, WithFieldValue as FirestoreWithFieldValue, QueryConstraint } from '@firebase/firestore'
4
- import type { HttpsCallable, HttpsCallableOptions } from '@firebase/functions-types'
5
- import { FieldValue, Transaction } from '@google-cloud/firestore'
6
- import { injectable } from 'inversify'
7
-
8
- export type DocumentData = FirestoreTypes.DocumentData
9
-
10
- export { FieldValue, FirestoreTypes, Transaction }
11
-
12
- /**
13
- * Same as Partial but all fields are required to be included
14
- */
15
- type OptionallyUndefined<T> = { [P in keyof T]: T[P] | undefined }
16
-
17
- export declare interface InternalFirestoreDataConverter<
18
- T extends DocumentData,
19
- SerializedT extends DocumentData = DocumentData,
20
- > {
21
- toFirestore(
22
- modelObject: Partial<WithFieldValue<WithoutId<T>>>,
23
- options?: FirestoreTypes.SetOptions
24
- ): WithFieldValue<WithoutId<SerializedT>>
25
- fromFirestore(
26
- snapshot: FirestoreTypes.QueryDocumentSnapshot<T, SerializedT>,
27
- options?: FirestoreTypes.SnapshotOptions
28
- ): T
29
- }
30
-
31
- export type WithTimestamps<T> = {
32
- [K in keyof T]: T[K] extends Date
33
- ? FirestoreTypes.Timestamp
34
- : T[K] extends Date | null
35
- ? FirestoreTypes.Timestamp | null
36
- : T[K] extends Date | undefined
37
- ? FirestoreTypes.Timestamp | undefined
38
- : T[K] extends Date | null | undefined
39
- ? FirestoreTypes.Timestamp | null | undefined
40
- : T[K] extends object
41
- ? T[K] extends any[]
42
- ? T[K] extends (infer U)[]
43
- ? WithTimestamps<U>[]
44
- : T[K]
45
- : WithTimestamps<T[K]>
46
- : T[K]
47
- }
48
-
49
- /**
50
- * Converts data between model and serialized data. Differs from InternalFirestoreDataConverter
51
- * by using `OptionallyUndefined` and `Required` to ensure that all fields are included in de/serialization
52
- * even if some model defined properties are optional.
53
- */
54
- export declare interface FirestoreDataConverter<ModelObject, SerializedModelObject = ModelObject> {
55
- toFirestore(
56
- modelObject: WithoutId<ModelObject>,
57
- options?: FirestoreTypes.SetOptions
58
- ): OptionallyUndefined<Required<WithoutId<WithFieldValue<SerializedModelObject>>>>
59
- fromFirestore(
60
- snapshot: FirestoreTypes.QueryDocumentSnapshot<WithTimestamps<SerializedModelObject>>,
61
- options?: FirestoreTypes.SnapshotOptions
62
- ): ModelObject
63
- }
64
-
65
- export type FirestoreQuery<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData> = (
66
- reference: FirestoreTypes.Query<AppModelType, DbModelType>,
67
- filter: any
68
- ) => FirestoreTypes.Query<AppModelType, DbModelType>
69
- export type FirestoreWhere = (field: string | FieldPath, op: string, value: unknown) => any
70
-
71
- export type FirestoreReadMode = 'realtime' | 'static'
72
-
73
- export interface FirestoreReadOptions {
74
- readonly transaction?: FirestoreTypes.Transaction
75
- }
76
-
77
- export interface FirestoreWriteOptions {
78
- readonly transaction?: FirestoreTypes.Transaction
79
- readonly batcher?: FirestoreTypes.WriteBatch
80
- }
81
-
82
- export interface FirebaseCreateOptions extends FirestoreWriteOptions {
83
- id?: string
84
- merge?: boolean
85
- }
86
-
87
- export declare type WithFieldValue<T> = {
88
- [K in keyof T]: FirestoreWithFieldValue<T[K]> | FirestoreTypes.FieldValue
89
- }
90
-
91
- type GenNode<K extends string, IsRoot extends boolean> = IsRoot extends true ? `${K}` : `.${K}`
92
-
93
- export type FilterableFields<
94
- T extends object,
95
- IsRoot extends boolean = true,
96
- K extends keyof T = keyof T,
97
- > = K extends string
98
- ? // Handle firebase timestamp fields
99
- T[K] extends FirestoreTypes.Timestamp
100
- ? `${K}`
101
- : // Handle Date fields
102
- T[K] extends Date
103
- ? `${K}`
104
- : // eslint-disable-next-line @typescript-eslint/no-explicit-any
105
- T[K] extends any[]
106
- ? GenNode<K, IsRoot>
107
- : T[K] extends object
108
- ? `${GenNode<K, IsRoot>}${FilterableFields<T[K], false>}`
109
- : GenNode<K, IsRoot>
110
- : never
111
-
112
- type GetValue<T, K extends string> = K extends `${infer L}.${infer R}`
113
- ? L extends keyof T
114
- ? GetValue<T[L], R>
115
- : never
116
- : K extends keyof T
117
- ? T[K] extends Date
118
- ? Date | FirestoreTypes.Timestamp
119
- : T[K]
120
- : K extends string
121
- ? string | number | boolean
122
- : never
123
-
124
- export type PrimitiveFilterOp = '<' | '<=' | '==' | '!=' | '>=' | '>'
125
-
126
- export type FilterBy<T extends object, Field extends string = FilterableFields<T>> = Field extends string
127
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
128
- GetValue<T, Field> extends any[]
129
- ? [Field, 'array-contains', string] | [Field, 'array-contains-any', string[]]
130
- : [Field, PrimitiveFilterOp, GetValue<T, Field>] | [Field, 'in', string[]]
131
- : never
132
-
133
- export type OrderBy<T extends DocumentData, Fields extends string = FilterableFields<T>> = [
134
- Fields,
135
- FirestoreTypes.OrderByDirection,
136
- ]
137
-
138
- export abstract class FieldValues {
139
- public abstract serverTimestamp(): FirestoreTypes.FieldValue
140
- public abstract delete(): FirestoreTypes.FieldValue
141
- public abstract increment(n: number): FirestoreTypes.FieldValue
142
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
143
- public abstract arrayUnion(...elements: any[]): FirestoreTypes.FieldValue
144
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
145
- public abstract arrayRemove(...elements: any[]): FirestoreTypes.FieldValue
146
- }
147
-
148
- /**********
149
- *
150
- *
151
- * INJECTABLES
152
- *
153
- *
154
- * **********/
155
-
156
- export interface FirestoreAppSettings {
157
- persistence?: boolean
158
- cacheSizeBytes?: number
159
- host?: string
160
- ssl?: boolean
161
- ignoreUndefinedProperties?: boolean
162
- serverTimestampBehavior?: 'estimate' | 'previous' | 'none'
163
- }
164
-
165
- export abstract class FirestoreApp {
166
- public abstract type?: string
167
- public abstract toJSON?(): object
168
- public abstract terminate?(): Promise<void>
169
- public abstract settings?(settings: FirestoreAppSettings): void
170
- public abstract useEmulator?(host: string, port: number): void
171
- public abstract runTransaction?<T>(
172
- updateFunction: (transaction: Transaction) => Promise<T>,
173
- options?: FirestoreTypes.TransactionOptions
174
- ): Promise<T>
175
- }
176
-
177
- @injectable()
178
- export abstract class Firestore {
179
- public abstract app: FirestoreApp
180
- public abstract collection(reference: FirestoreTypes.CollectionReference | FirestoreApp, path: string): any
181
- public abstract getDocs<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
182
- reference: FirestoreTypes.Query<AppModelType, DbModelType>
183
- ): Promise<FirestoreTypes.QuerySnapshot<AppModelType, DbModelType>>
184
- public abstract getDoc<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
185
- reference: FirestoreTypes.DocumentReference<AppModelType, DbModelType>,
186
- path?: string
187
- ): Promise<FirestoreTypes.DocumentSnapshot<AppModelType, DbModelType>>
188
- public abstract serverTimestamp(): FirestoreTypes.FieldValue
189
- public abstract increment(n: number): FirestoreTypes.FieldValue
190
- public abstract query<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
191
- reference: FirestoreTypes.Query<AppModelType, DbModelType>,
192
- filter: any
193
- ): FirestoreTypes.Query<AppModelType, DbModelType>
194
- public abstract where(field: string | FieldPath, op: string, value: unknown): QueryConstraint
195
- public abstract limit(limit: number): any
196
- public abstract orderBy(orderBy: string, direction: FirestoreTypes.OrderByDirection): any
197
- public abstract setDoc<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
198
- reference: FirestoreTypes.DocumentReference<AppModelType, DbModelType>,
199
- data: any,
200
- options?: FirestoreTypes.SetOptions
201
- ): Promise<void>
202
- public abstract updateDoc<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
203
- reference: FirestoreTypes.DocumentReference<AppModelType, DbModelType>,
204
- data: any
205
- ): Promise<void>
206
- public abstract doc<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
207
- reference: FirestoreTypes.Query<AppModelType, DbModelType>,
208
- path?: string
209
- ): FirestoreTypes.DocumentReference<AppModelType, DbModelType>
210
- public abstract deleteDoc<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
211
- reference: FirestoreTypes.DocumentReference<AppModelType, DbModelType>
212
- ): Promise<void>
213
- public abstract onSnapshot<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
214
- reference: FirestoreTypes.DocumentReference<AppModelType, DbModelType>,
215
- onNext: (snapshot: FirestoreTypes.DocumentSnapshot<AppModelType, DbModelType>) => void,
216
- onError?: (error: Error) => void
217
- ): () => void
218
- public abstract onSnapshot<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData>(
219
- reference: FirestoreTypes.Query<AppModelType, DbModelType>,
220
- onNext: (snapshot: FirestoreTypes.QuerySnapshot<AppModelType, DbModelType>) => void,
221
- onError?: (error: Error) => void
222
- ): () => void
223
- public abstract runTransaction<T>(
224
- firestore: FirestoreApp,
225
- transaction: (transaction: Transaction) => Promise<T>,
226
- options?: FirestoreTypes.TransactionOptions
227
- ): Promise<T>
228
- }
229
-
230
- @injectable()
231
- export abstract class FirestoreSettings {
232
- public abstract readMode: FirestoreReadMode
233
- }
234
-
235
- @injectable()
236
- export abstract class FirestoreFunctions {
237
- public abstract httpsCallable(name: string, options?: HttpsCallableOptions): HttpsCallable
238
- public abstract useEmulator(host: string, port: number): void
239
- public abstract useFunctionsEmulator(origin: string): void
240
- }
241
-
242
- @injectable()
243
- export class DummyFirestoreFunctions implements FirestoreFunctions {
244
- public httpsCallable(): HttpsCallable {
245
- return () => {
246
- throw new Error('httpsCallable not implemented - DummyFirestoreFunctions')
247
- }
248
- }
249
- public useEmulator() {}
250
- public useFunctionsEmulator() {}
251
- }
package/lib/index.ts DELETED
@@ -1,6 +0,0 @@
1
- export * from './FirestoreDataManager'
2
- export * from './FirestoreList'
3
- export * from './FirestoreMetadata'
4
- export * from './FirestoreReference'
5
- export * from './firestoreTypes'
6
- export * from './utils'
package/lib/utils.ts DELETED
@@ -1,113 +0,0 @@
1
- import { WithoutId } from '@data-weave/datamanager'
2
- import {
3
- DocumentData,
4
- Firestore,
5
- FirestoreTypes,
6
- InternalFirestoreDataConverter,
7
- Transaction,
8
- WithFieldValue,
9
- } from './firestoreTypes'
10
-
11
- import {
12
- Firestore as FirebaseFirestore,
13
- collection,
14
- deleteDoc,
15
- doc,
16
- getDoc,
17
- getDocs,
18
- increment,
19
- limit,
20
- onSnapshot,
21
- orderBy,
22
- query,
23
- runTransaction,
24
- serverTimestamp,
25
- setDoc,
26
- updateDoc,
27
- where,
28
- } from 'firebase/firestore'
29
-
30
- export class MergeConverters<
31
- T extends DocumentData,
32
- SerializedT extends DocumentData,
33
- G extends DocumentData,
34
- SerializedG extends DocumentData,
35
- > implements InternalFirestoreDataConverter<T & G, SerializedT & SerializedG>
36
- {
37
- constructor(
38
- private converter1: InternalFirestoreDataConverter<T, SerializedT>,
39
- private converter2: InternalFirestoreDataConverter<G, SerializedG>
40
- ) {}
41
-
42
- toFirestore(
43
- modelObject: WithoutId<Partial<WithFieldValue<T>>> & WithoutId<Partial<WithFieldValue<G>>>,
44
- options?: FirestoreTypes.SetOptions
45
- ) {
46
- if (options) {
47
- return {
48
- ...this.converter1.toFirestore(modelObject, options),
49
- ...this.converter2.toFirestore(modelObject, options),
50
- }
51
- } else {
52
- return {
53
- ...this.converter1.toFirestore(modelObject),
54
- ...this.converter2.toFirestore(modelObject),
55
- }
56
- }
57
- }
58
-
59
- fromFirestore(
60
- snapshot: FirestoreTypes.QueryDocumentSnapshot<T & G, SerializedT & SerializedG>,
61
- options: FirestoreTypes.SnapshotOptions
62
- ) {
63
- return {
64
- ...this.converter1.fromFirestore(snapshot, options),
65
- ...this.converter2.fromFirestore(snapshot, options),
66
- }
67
- }
68
- }
69
-
70
- export function createModularFirestoreAdapter(firestore: FirebaseFirestore): Firestore {
71
- return {
72
- app: firestore,
73
- collection,
74
- getDocs,
75
- getDoc,
76
- serverTimestamp,
77
- query,
78
- where,
79
- limit,
80
- orderBy,
81
- setDoc,
82
- updateDoc,
83
- deleteDoc,
84
- doc,
85
- onSnapshot,
86
- increment,
87
- // TODO: fix this
88
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
89
- runTransaction: runTransaction as any,
90
- }
91
- }
92
-
93
- // Value can be anything
94
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
- export const isFieldValue = (value: any): value is FirestoreTypes.FieldValue => {
96
- return value !== undefined && typeof value._toFieldTransform === 'function'
97
- }
98
-
99
- // Modular firestore uses exists, namespaced uses exists()
100
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
101
- export const checkIfReferenceExists = (value: any): boolean => {
102
- if (value == null) return false
103
- if (typeof value.exists === 'function') return value.exists()
104
- return value.exists
105
- }
106
-
107
- export const withTransaction = (
108
- firestore: Firestore,
109
- transaction: (transaction: Transaction) => Promise<void>,
110
- options?: FirestoreTypes.TransactionOptions
111
- ) => {
112
- return firestore.runTransaction!(firestore.app, transaction, options)
113
- }