firetender-admin 0.10.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.
- package/LICENSE +20 -0
- package/README.md +349 -0
- package/dist/FiretenderCollection.d.ts +116 -0
- package/dist/FiretenderCollection.js +225 -0
- package/dist/FiretenderCollection.js.map +1 -0
- package/dist/FiretenderDoc.d.ts +172 -0
- package/dist/FiretenderDoc.js +361 -0
- package/dist/FiretenderDoc.js.map +1 -0
- package/dist/errors.d.ts +30 -0
- package/dist/errors.js +49 -0
- package/dist/errors.js.map +1 -0
- package/dist/firestore-deps-admin.d.ts +26 -0
- package/dist/firestore-deps-admin.js +62 -0
- package/dist/firestore-deps-admin.js.map +1 -0
- package/dist/firestore-deps-web.d.ts +10 -0
- package/dist/firestore-deps-web.js +29 -0
- package/dist/firestore-deps-web.js.map +1 -0
- package/dist/firestore-deps.d.ts +26 -0
- package/dist/firestore-deps.js +62 -0
- package/dist/firestore-deps.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/proxies.d.ts +62 -0
- package/dist/proxies.js +243 -0
- package/dist/proxies.js.map +1 -0
- package/dist/timestamps.d.ts +37 -0
- package/dist/timestamps.js +58 -0
- package/dist/timestamps.js.map +1 -0
- package/dist/ts-helpers.d.ts +14 -0
- package/dist/ts-helpers.js +19 -0
- package/dist/ts-helpers.js.map +1 -0
- package/package.json +72 -0
- package/src/FiretenderCollection.ts +311 -0
- package/src/FiretenderDoc.ts +483 -0
- package/src/errors.ts +47 -0
- package/src/firestore-deps-admin.ts +111 -0
- package/src/firestore-deps-web.ts +14 -0
- package/src/firestore-deps.ts +111 -0
- package/src/index.ts +29 -0
- package/src/proxies.ts +286 -0
- package/src/timestamps.ts +62 -0
- package/src/ts-helpers.ts +39 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides all dependencies normally imported from "firebase/firestore".
|
|
3
|
+
*
|
|
4
|
+
* For web clients, the "firebase/firestore" API is simply re-exported.
|
|
5
|
+
*
|
|
6
|
+
* For the server client API, ...
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
CollectionGroup,
|
|
11
|
+
CollectionReference,
|
|
12
|
+
DocumentReference,
|
|
13
|
+
DocumentSnapshot,
|
|
14
|
+
FieldValue,
|
|
15
|
+
Filter,
|
|
16
|
+
Firestore,
|
|
17
|
+
Query,
|
|
18
|
+
QuerySnapshot,
|
|
19
|
+
Timestamp,
|
|
20
|
+
UpdateData,
|
|
21
|
+
WithFieldValue,
|
|
22
|
+
} from "@google-cloud/firestore";
|
|
23
|
+
|
|
24
|
+
export const FIRESTORE_DEPS_TYPE: "web" | "admin" = "admin";
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
CollectionReference,
|
|
28
|
+
DocumentReference,
|
|
29
|
+
DocumentSnapshot,
|
|
30
|
+
Firestore,
|
|
31
|
+
Query,
|
|
32
|
+
QuerySnapshot,
|
|
33
|
+
Timestamp,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type QueryConstraint = Filter;
|
|
37
|
+
export const where = Filter.where;
|
|
38
|
+
export const arrayRemove = FieldValue.arrayRemove;
|
|
39
|
+
export const deleteField = FieldValue.delete;
|
|
40
|
+
export const serverTimestamp = FieldValue.serverTimestamp;
|
|
41
|
+
|
|
42
|
+
export const isServerTimestamp = (x: any) => x instanceof FieldValue;
|
|
43
|
+
|
|
44
|
+
export const addDoc = <T>(
|
|
45
|
+
ref: CollectionReference<T>,
|
|
46
|
+
data: WithFieldValue<T>
|
|
47
|
+
): Promise<DocumentReference<T>> => ref.add(data);
|
|
48
|
+
|
|
49
|
+
export const collection = (
|
|
50
|
+
firestoreOrRef: Firestore | CollectionReference,
|
|
51
|
+
path: string,
|
|
52
|
+
...pathSegments: string[]
|
|
53
|
+
): CollectionReference =>
|
|
54
|
+
firestoreOrRef instanceof Firestore
|
|
55
|
+
? firestoreOrRef.collection([path, ...pathSegments].join("/"))
|
|
56
|
+
: firestoreOrRef.firestore.collection(
|
|
57
|
+
[firestoreOrRef.path, path, ...pathSegments].join("/")
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
export const collectionGroup = (
|
|
61
|
+
firestore: Firestore,
|
|
62
|
+
collectionID: string
|
|
63
|
+
): CollectionGroup => firestore.collectionGroup(collectionID);
|
|
64
|
+
|
|
65
|
+
export const deleteDoc = async (
|
|
66
|
+
ref: DocumentReference<unknown>
|
|
67
|
+
): Promise<void> => {
|
|
68
|
+
await ref.delete();
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const doc = (
|
|
72
|
+
firestoreOrRef: Firestore | CollectionReference,
|
|
73
|
+
path?: string,
|
|
74
|
+
...pathSegments: string[]
|
|
75
|
+
): DocumentReference =>
|
|
76
|
+
firestoreOrRef instanceof Firestore
|
|
77
|
+
? firestoreOrRef.doc([path, ...pathSegments].join("/"))
|
|
78
|
+
: path
|
|
79
|
+
? firestoreOrRef.doc([path, ...pathSegments].join("/"))
|
|
80
|
+
: firestoreOrRef.doc();
|
|
81
|
+
|
|
82
|
+
export const getDoc = <T>(
|
|
83
|
+
ref: DocumentReference<T>
|
|
84
|
+
): Promise<DocumentSnapshot<T>> => ref.get();
|
|
85
|
+
|
|
86
|
+
export const getDocs = <T>(query: Query<T>): Promise<QuerySnapshot<T>> =>
|
|
87
|
+
query.get();
|
|
88
|
+
|
|
89
|
+
export const query = <T>(
|
|
90
|
+
ref: Query<T>,
|
|
91
|
+
...queryConstraints: QueryConstraint[]
|
|
92
|
+
): Query<T> =>
|
|
93
|
+
queryConstraints.length === 0
|
|
94
|
+
? ref
|
|
95
|
+
: queryConstraints.length === 1
|
|
96
|
+
? ref.where(queryConstraints[0])
|
|
97
|
+
: ref.where(Filter.and(...queryConstraints));
|
|
98
|
+
|
|
99
|
+
export const setDoc = async <T>(
|
|
100
|
+
ref: DocumentReference<T>,
|
|
101
|
+
data: WithFieldValue<T>
|
|
102
|
+
): Promise<void> => {
|
|
103
|
+
await ref.set(data);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export const updateDoc = async <T>(
|
|
107
|
+
ref: DocumentReference<T>,
|
|
108
|
+
data: UpdateData<T>
|
|
109
|
+
): Promise<void> => {
|
|
110
|
+
await ref.update(data);
|
|
111
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FiretenderError,
|
|
3
|
+
FiretenderInternalError,
|
|
4
|
+
FiretenderIOError,
|
|
5
|
+
FiretenderUsageError,
|
|
6
|
+
} from "./errors";
|
|
7
|
+
import { FiretenderCollection } from "./FiretenderCollection";
|
|
8
|
+
import type { FiretenderDocOptions } from "./FiretenderDoc";
|
|
9
|
+
import { FiretenderDoc } from "./FiretenderDoc";
|
|
10
|
+
import {
|
|
11
|
+
futureTimestampDays,
|
|
12
|
+
serverTimestamp,
|
|
13
|
+
serverTimestampWithClientTime,
|
|
14
|
+
timestampSchema,
|
|
15
|
+
} from "./timestamps";
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
FiretenderCollection,
|
|
19
|
+
FiretenderDoc,
|
|
20
|
+
FiretenderDocOptions,
|
|
21
|
+
FiretenderError,
|
|
22
|
+
FiretenderInternalError,
|
|
23
|
+
FiretenderIOError,
|
|
24
|
+
FiretenderUsageError,
|
|
25
|
+
futureTimestampDays,
|
|
26
|
+
serverTimestamp,
|
|
27
|
+
serverTimestampWithClientTime,
|
|
28
|
+
timestampSchema,
|
|
29
|
+
};
|
package/src/proxies.ts
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import { arrayRemove, deleteField } from "./firestore-deps";
|
|
4
|
+
import { assertKeyIsString } from "./ts-helpers";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Given a Zod schema representing a collection, returns the sub-schema of the
|
|
8
|
+
* specified property.
|
|
9
|
+
*/
|
|
10
|
+
function getPropertySchema(
|
|
11
|
+
parent: any,
|
|
12
|
+
parentSchema: z.ZodTypeAny,
|
|
13
|
+
propertyKey: string
|
|
14
|
+
): z.ZodTypeAny {
|
|
15
|
+
let schema: any = parentSchema;
|
|
16
|
+
// eslint-disable-next-line no-constant-condition
|
|
17
|
+
while (true) {
|
|
18
|
+
switch (schema._def.typeName) {
|
|
19
|
+
// If the schema object is wrapped (e.g., by being optional or having a
|
|
20
|
+
// default), unwrap it until we get to the underlying collection type.
|
|
21
|
+
case z.ZodFirstPartyTypeKind.ZodOptional:
|
|
22
|
+
case z.ZodFirstPartyTypeKind.ZodNullable:
|
|
23
|
+
schema = schema.unwrap();
|
|
24
|
+
continue;
|
|
25
|
+
case z.ZodFirstPartyTypeKind.ZodDefault:
|
|
26
|
+
schema = schema.removeDefault();
|
|
27
|
+
continue;
|
|
28
|
+
case z.ZodFirstPartyTypeKind.ZodEffects:
|
|
29
|
+
schema = schema.innerType();
|
|
30
|
+
continue;
|
|
31
|
+
// Return the sub-schemas of supported collection types.
|
|
32
|
+
case z.ZodFirstPartyTypeKind.ZodRecord:
|
|
33
|
+
if (
|
|
34
|
+
schema.keySchema._def.typeName !== z.ZodFirstPartyTypeKind.ZodString
|
|
35
|
+
) {
|
|
36
|
+
throw TypeError(
|
|
37
|
+
`The ZodRecord for property ${propertyKey} has keys of type ${schema.keySchema._def.typeName}. Only strings are supported.`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return schema.valueSchema;
|
|
41
|
+
case z.ZodFirstPartyTypeKind.ZodArray:
|
|
42
|
+
return schema.element;
|
|
43
|
+
case z.ZodFirstPartyTypeKind.ZodObject:
|
|
44
|
+
return schema.shape[propertyKey];
|
|
45
|
+
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
|
|
46
|
+
return (schema as any).optionsMap.get(
|
|
47
|
+
parent[(schema as any).discriminator]
|
|
48
|
+
);
|
|
49
|
+
// If the parent is of type ZodAny, so are its properties.
|
|
50
|
+
case z.ZodFirstPartyTypeKind.ZodAny:
|
|
51
|
+
return z.any();
|
|
52
|
+
default:
|
|
53
|
+
throw TypeError(
|
|
54
|
+
`Unsupported schema type for property "${propertyKey}": ${schema._def.typeName}`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Getting this symbol from one of our proxies returns the proxy's target.
|
|
62
|
+
*/
|
|
63
|
+
const PROXY_TARGET_SYMBOL = Symbol("proxy_target");
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Wraps a top-level array, its elements, or its elements' subfields in a proxy
|
|
67
|
+
* that watches for changes.
|
|
68
|
+
*
|
|
69
|
+
* Firestore supports limited modification of arrays; see the 2018 blog entry at
|
|
70
|
+
* https://firebase.blog/posts/2018/08/better-arrays-in-cloud-firestore for
|
|
71
|
+
* details and some history. The tl;dr is that we can't update array entries;
|
|
72
|
+
* we can only append or remove them.
|
|
73
|
+
*
|
|
74
|
+
* For this reason, this proxy only works with top-level arrays: the wrapped
|
|
75
|
+
* array cannot be inside a parent array, though it may be in a record or nested
|
|
76
|
+
* records. Child proxies maintain a link to the top-level array and trigger a
|
|
77
|
+
* rewrite of the whole thing if there are any changes to the contents. As a
|
|
78
|
+
* consequence, the watchFieldForChanges proxy (below) can produce child
|
|
79
|
+
* watchArrayForChanges proxies, but all children of watchArrayForChanges will
|
|
80
|
+
* always use the watchArrayForChanges proxy to maintain a reference to the
|
|
81
|
+
* top-level array.
|
|
82
|
+
*
|
|
83
|
+
* Appending an element to the top-level array could in theory be supported, but
|
|
84
|
+
* Firestore's arrayUnion operator only appends entries that don't already
|
|
85
|
+
* exist. That is not how Javascript arrays work, so to reduce logical overhead
|
|
86
|
+
* we don't bother with it.
|
|
87
|
+
*
|
|
88
|
+
* Deletion of entries in the top-level array are handled using Firestore's
|
|
89
|
+
* arrayRemove operator. The deleted element gets removed entirely, so the
|
|
90
|
+
* resulting array remains dense. Note that this behavior differs from how
|
|
91
|
+
* Javascript's delete operator normally works with arrays, but here we favor
|
|
92
|
+
* Firestore's array semantics.
|
|
93
|
+
*
|
|
94
|
+
* @param arrayPath the dot-delimited path of the top-level array.
|
|
95
|
+
* @param array a reference to the top-level array.
|
|
96
|
+
* @param fieldSchema schema of the array element or a field nested within it.
|
|
97
|
+
* @param field reference to the array element or one of its subfields.
|
|
98
|
+
* @param addToUpdateList callback to register modifications to this array.
|
|
99
|
+
*/
|
|
100
|
+
export function watchArrayForChanges<
|
|
101
|
+
ArrayElementType,
|
|
102
|
+
FieldSchemaType extends z.ZodTypeAny
|
|
103
|
+
>(
|
|
104
|
+
arrayPath: string[],
|
|
105
|
+
array: ArrayElementType[],
|
|
106
|
+
fieldSchema: FieldSchemaType,
|
|
107
|
+
field: z.infer<FieldSchemaType>,
|
|
108
|
+
addToUpdateList: (path: string[], newValue: any) => void
|
|
109
|
+
): z.infer<FieldSchemaType> {
|
|
110
|
+
return new Proxy(field, {
|
|
111
|
+
get(target, propertyKey) {
|
|
112
|
+
const property = target[propertyKey];
|
|
113
|
+
if (property instanceof Function) {
|
|
114
|
+
// All methods of an array or its children are presumed to make
|
|
115
|
+
// modifications, thus triggering an update of the full array.
|
|
116
|
+
// TODO: #11 Only mark the change for mutating function calls.
|
|
117
|
+
const result = (...args: any[]) => property.apply(field, args);
|
|
118
|
+
addToUpdateList(arrayPath, array);
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
if (typeof propertyKey === "symbol") {
|
|
122
|
+
if (propertyKey === PROXY_TARGET_SYMBOL) {
|
|
123
|
+
return target;
|
|
124
|
+
}
|
|
125
|
+
// Allow all other symbols to pass through.
|
|
126
|
+
return property;
|
|
127
|
+
}
|
|
128
|
+
if (property instanceof Object) {
|
|
129
|
+
// Wrap nested objects, including nested arrays, in child proxies.
|
|
130
|
+
return watchArrayForChanges(
|
|
131
|
+
arrayPath,
|
|
132
|
+
array,
|
|
133
|
+
getPropertySchema(field, fieldSchema, propertyKey),
|
|
134
|
+
property,
|
|
135
|
+
addToUpdateList
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
// Otherwise we must be getting a primitive. No need to wrap it.
|
|
139
|
+
return property;
|
|
140
|
+
},
|
|
141
|
+
set(target, propertyKey, value) {
|
|
142
|
+
if (typeof propertyKey === "symbol") {
|
|
143
|
+
// Allow symbols to pass through.
|
|
144
|
+
return Reflect.set(target, propertyKey, value);
|
|
145
|
+
}
|
|
146
|
+
let processedValue = value;
|
|
147
|
+
// If the new value is an object wrapped in a Firetender proxy, which can
|
|
148
|
+
// commonly happen when referencing it inside a mutator function passed to
|
|
149
|
+
// FiretenderDoc.prototype.update(), unwrap it.
|
|
150
|
+
if (value instanceof Object) {
|
|
151
|
+
const valueTarget = value[PROXY_TARGET_SYMBOL];
|
|
152
|
+
if (valueTarget !== undefined) {
|
|
153
|
+
processedValue = valueTarget;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// An array element or one of its subfields is being set to a new value.
|
|
157
|
+
// Parse the new value with the appropriate schema, set it in the local
|
|
158
|
+
// data, and mark the entire top-level array as needing to be written.
|
|
159
|
+
const propertySchema = getPropertySchema(field, fieldSchema, propertyKey);
|
|
160
|
+
processedValue = propertySchema.parse(processedValue);
|
|
161
|
+
const result = Reflect.set(target, propertyKey, processedValue);
|
|
162
|
+
addToUpdateList(arrayPath, array);
|
|
163
|
+
return result;
|
|
164
|
+
},
|
|
165
|
+
deleteProperty(target, propertyKey) {
|
|
166
|
+
assertKeyIsString(propertyKey);
|
|
167
|
+
// Calling Reflect.deleteProperty on an array item sets it to undefined,
|
|
168
|
+
// which causes Firestore updates to fail unless ignoreUndefinedProperties
|
|
169
|
+
// is set, and which is generally not what we want. Hence splice.
|
|
170
|
+
const removedValues = array.splice(Number(propertyKey), 1);
|
|
171
|
+
if (removedValues.length !== 1) {
|
|
172
|
+
throw RangeError(
|
|
173
|
+
`Failed to delete array item with index ${propertyKey}. Out of bounds?`
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
// Only top-level elements can be deleted with Firestore's arrayRemove.
|
|
177
|
+
if (target === array) {
|
|
178
|
+
addToUpdateList(arrayPath, arrayRemove(removedValues[0]));
|
|
179
|
+
} else {
|
|
180
|
+
addToUpdateList(arrayPath, array);
|
|
181
|
+
}
|
|
182
|
+
return true;
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Wraps an object based on a Zod schema in a proxy that watches for changes.
|
|
189
|
+
*
|
|
190
|
+
* Nested fields are monitored by creating a chain of proxies. Consider this
|
|
191
|
+
* case:
|
|
192
|
+
*
|
|
193
|
+
* testDoc.w.record1.record2.someStringValue = "foo";
|
|
194
|
+
*
|
|
195
|
+
* There is a top-level proxy (call it proxy 0) for the ".w" accessor. Getting
|
|
196
|
+
* ".record1" from it returns a child proxy (1) wrapping that field. Getting
|
|
197
|
+
* ".record2" from that returns another child proxy (2) wrapping that subfield.
|
|
198
|
+
* Finally, setting ".someStringValue" uses the setter of proxy 2 to set the new
|
|
199
|
+
* value locally and register an update to be sent to Firestore.
|
|
200
|
+
*
|
|
201
|
+
* The proxy returned by watchFieldForChanges is used for fields and subfields
|
|
202
|
+
* when no parent is an array. If a field or subfield is an array, the proxy
|
|
203
|
+
* returned by watchArrayForChanges is used for it and all of its children,
|
|
204
|
+
* regardless of whether they are arrays.
|
|
205
|
+
*
|
|
206
|
+
* @param fieldPath the dot-delimited path of the object being wrapped.
|
|
207
|
+
* @param fieldSchema schema of the object's field.
|
|
208
|
+
* @param field reference to the field in the local document data.
|
|
209
|
+
* @param addToUpdateList callback to register modifications to this field.
|
|
210
|
+
*/
|
|
211
|
+
export function watchFieldForChanges<FieldSchemaType extends z.ZodTypeAny>(
|
|
212
|
+
fieldPath: string[],
|
|
213
|
+
fieldSchema: FieldSchemaType,
|
|
214
|
+
field: z.infer<FieldSchemaType>,
|
|
215
|
+
addToUpdateList: (path: string[], newValue: any) => void
|
|
216
|
+
): z.infer<FieldSchemaType> {
|
|
217
|
+
return new Proxy(field, {
|
|
218
|
+
get(target, propertyKey) {
|
|
219
|
+
const property = target[propertyKey];
|
|
220
|
+
if (property instanceof Function) {
|
|
221
|
+
// Provide methods with a "this" reference for the underlying field.
|
|
222
|
+
return (...args: any[]) => property.apply(field, args);
|
|
223
|
+
}
|
|
224
|
+
if (typeof propertyKey === "symbol") {
|
|
225
|
+
if (propertyKey === PROXY_TARGET_SYMBOL) {
|
|
226
|
+
return target;
|
|
227
|
+
}
|
|
228
|
+
// Allow all other symbols to pass through.
|
|
229
|
+
return property;
|
|
230
|
+
}
|
|
231
|
+
if (property instanceof Array) {
|
|
232
|
+
// Wrap array subfields in the watchArrayForChanges proxy. It is
|
|
233
|
+
// necessarily a top-level array, because otherwise we would be in
|
|
234
|
+
// watchArrayForChanges already.
|
|
235
|
+
return watchArrayForChanges(
|
|
236
|
+
[...fieldPath, propertyKey],
|
|
237
|
+
property,
|
|
238
|
+
getPropertySchema(field, fieldSchema, propertyKey),
|
|
239
|
+
property,
|
|
240
|
+
addToUpdateList
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
if (property instanceof Object) {
|
|
244
|
+
// Wrap nested objects in another instance of this proxy.
|
|
245
|
+
return watchFieldForChanges(
|
|
246
|
+
[...fieldPath, propertyKey],
|
|
247
|
+
getPropertySchema(field, fieldSchema, propertyKey),
|
|
248
|
+
property,
|
|
249
|
+
addToUpdateList
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
// Otherwise we must be getting a primitive. No need to wrap it.
|
|
253
|
+
return property;
|
|
254
|
+
},
|
|
255
|
+
set(target, propertyKey, value) {
|
|
256
|
+
if (typeof propertyKey === "symbol") {
|
|
257
|
+
// Allow symbols to pass through.
|
|
258
|
+
return Reflect.set(target, propertyKey, value);
|
|
259
|
+
}
|
|
260
|
+
let processedValue = value;
|
|
261
|
+
// If the new value is an object wrapped in a Firetender proxy, which can
|
|
262
|
+
// commonly happen when referencing it inside a mutator function passed to
|
|
263
|
+
// FiretenderDoc.prototype.update(), unwrap it.
|
|
264
|
+
if (value instanceof Object) {
|
|
265
|
+
const valueTarget = value[PROXY_TARGET_SYMBOL];
|
|
266
|
+
if (valueTarget !== undefined) {
|
|
267
|
+
processedValue = valueTarget;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// A property of this object is being set to a new value. Parse the new
|
|
271
|
+
// value with the appropriate schema, set it in the local data, and mark
|
|
272
|
+
// the entire top-level array as needing to be written.
|
|
273
|
+
const propertySchema = getPropertySchema(field, fieldSchema, propertyKey);
|
|
274
|
+
processedValue = propertySchema.parse(processedValue);
|
|
275
|
+
addToUpdateList([...fieldPath, propertyKey], processedValue);
|
|
276
|
+
return Reflect.set(target, propertyKey, processedValue);
|
|
277
|
+
},
|
|
278
|
+
deleteProperty(target, propertyKey) {
|
|
279
|
+
assertKeyIsString(propertyKey);
|
|
280
|
+
// Delete the field in Firestore by marking it with the deleteField
|
|
281
|
+
// operator.
|
|
282
|
+
addToUpdateList([...fieldPath, propertyKey], deleteField());
|
|
283
|
+
return Reflect.deleteProperty(target, propertyKey);
|
|
284
|
+
},
|
|
285
|
+
});
|
|
286
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
isServerTimestamp,
|
|
5
|
+
serverTimestamp as firestoreServerTimestamp,
|
|
6
|
+
Timestamp,
|
|
7
|
+
} from "./firestore-deps";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Timestamp representation used by Firestore: seconds and nanoseconds since the
|
|
11
|
+
* epoch.
|
|
12
|
+
*/
|
|
13
|
+
export const timestampSchema = z.custom<Timestamp>(
|
|
14
|
+
(data: any) => data instanceof Timestamp || isServerTimestamp(data)
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns a Firestore Timestamp for some future date. The result is typically
|
|
19
|
+
* used for writing TTLs.
|
|
20
|
+
*
|
|
21
|
+
* The client's clock (specifically `Date.now()`) is used to generate the
|
|
22
|
+
* timestamp. For TTLs days in the future, this is generally not a concern.
|
|
23
|
+
* However, this function should not be depended on for short offsets.
|
|
24
|
+
*
|
|
25
|
+
* @param daysFromNow days in the future to set this Timestamp.
|
|
26
|
+
*/
|
|
27
|
+
export function futureTimestampDays(daysFromNow: number) {
|
|
28
|
+
return Timestamp.fromMillis(Date.now() + daysFromNow * 24 * 60 * 60 * 1000);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns a sentinel to include a server-generated timestamp in the written
|
|
33
|
+
* data.
|
|
34
|
+
*
|
|
35
|
+
* Note that the sentinel, despite being typed as a Timestamp, has none of that
|
|
36
|
+
* class's properties or methods. For a sentinel that can be immediately used
|
|
37
|
+
* as a Timestamp, see {@link serverTimestampWithClientTime}.
|
|
38
|
+
*/
|
|
39
|
+
export function serverTimestamp(): Timestamp {
|
|
40
|
+
return firestoreServerTimestamp() as Timestamp;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns a sentinel to include a server-generated timestamp in the written
|
|
45
|
+
* data. The returned object also includes all properties and methods of
|
|
46
|
+
* Timestamp, allowing its immediate use without retrieving the server-set time.
|
|
47
|
+
*
|
|
48
|
+
* Note that the time returned by the Timestamp methods will likely differ from
|
|
49
|
+
* the time set by the server. It may differ substantially if the client's
|
|
50
|
+
* clock is incorrect. Caveat coder.
|
|
51
|
+
*/
|
|
52
|
+
export function serverTimestampWithClientTime(): Timestamp {
|
|
53
|
+
const sentinel = firestoreServerTimestamp();
|
|
54
|
+
const timestamp = Timestamp.now();
|
|
55
|
+
Object.assign(sentinel, timestamp);
|
|
56
|
+
(sentinel as any).isEqual = (other: Timestamp) => timestamp.isEqual(other);
|
|
57
|
+
(sentinel as any).toDate = () => timestamp.toDate();
|
|
58
|
+
(sentinel as any).toMillis = () => timestamp.toMillis();
|
|
59
|
+
(sentinel as any).toString = () => timestamp.toString();
|
|
60
|
+
(sentinel as any).valueOf = () => timestamp.valueOf();
|
|
61
|
+
return sentinel as Timestamp;
|
|
62
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typescript-related helper functions and types.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type DeepPartial<T> = T extends object
|
|
6
|
+
? { [K in keyof T]?: DeepPartial<T[K]> }
|
|
7
|
+
: T;
|
|
8
|
+
|
|
9
|
+
export type DeepReadonly<T> = T extends Array<infer ArrKey>
|
|
10
|
+
? ReadonlyArray<DeepReadonly<ArrKey>>
|
|
11
|
+
: T extends Map<infer MapKey, infer MapVal>
|
|
12
|
+
? ReadonlyMap<DeepReadonly<MapKey>, DeepReadonly<MapVal>>
|
|
13
|
+
: T extends Set<infer SetKey>
|
|
14
|
+
? ReadonlySet<DeepReadonly<SetKey>>
|
|
15
|
+
: T extends Record<any, unknown>
|
|
16
|
+
? { readonly [ObjKey in keyof T]: DeepReadonly<T[ObjKey]> }
|
|
17
|
+
: T;
|
|
18
|
+
|
|
19
|
+
export type MakeFieldsWithDefaultsOptional<T> = T extends Array<infer ArrKey>
|
|
20
|
+
? ReadonlyArray<DeepReadonly<ArrKey>>
|
|
21
|
+
: T extends Map<infer MapKey, infer MapVal>
|
|
22
|
+
? ReadonlyMap<DeepReadonly<MapKey>, DeepReadonly<MapVal>>
|
|
23
|
+
: T extends Set<infer SetKey>
|
|
24
|
+
? ReadonlySet<DeepReadonly<SetKey>>
|
|
25
|
+
: T extends Record<any, unknown>
|
|
26
|
+
? { readonly [ObjKey in keyof T]: DeepReadonly<T[ObjKey]> }
|
|
27
|
+
: T;
|
|
28
|
+
|
|
29
|
+
export function assertIsDefined<T>(value: T): asserts value is NonNullable<T> {
|
|
30
|
+
if (value === undefined || value === null) {
|
|
31
|
+
throw new TypeError(`${value} is not defined`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function assertKeyIsString(key: any): asserts key is string {
|
|
36
|
+
if (typeof key !== "string") {
|
|
37
|
+
throw TypeError("Property access using symbols is not supported.");
|
|
38
|
+
}
|
|
39
|
+
}
|