@travetto/model-firestore 7.1.3 → 8.0.0-alpha.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/README.md +2 -3
- package/package.json +6 -6
- package/src/config.ts +3 -4
- package/src/service.ts +9 -10
package/README.md
CHANGED
|
@@ -59,9 +59,8 @@ export class FirestoreModelConfig {
|
|
|
59
59
|
process.env.FIRESTORE_EMULATOR_HOST = this.emulator;
|
|
60
60
|
}
|
|
61
61
|
if (this.credentialsFile && !this.credentials) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
);
|
|
62
|
+
const bytes = await RuntimeResources.readBinaryArray(this.credentialsFile);
|
|
63
|
+
this.credentials = FirestoreModelConfigCredentials.from(JSONUtil.fromBinaryArray(bytes));
|
|
65
64
|
await SchemaValidator.validate(FirestoreModelConfigCredentials, this.credentials);
|
|
66
65
|
}
|
|
67
66
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-firestore",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.0",
|
|
4
4
|
"description": "Firestore backing for the travetto model module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -25,13 +25,13 @@
|
|
|
25
25
|
"directory": "module/model-firestore"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@google-cloud/firestore": "^8.
|
|
29
|
-
"@travetto/config": "^
|
|
30
|
-
"@travetto/model": "^
|
|
31
|
-
"@travetto/runtime": "^
|
|
28
|
+
"@google-cloud/firestore": "^8.3.0",
|
|
29
|
+
"@travetto/config": "^8.0.0-alpha.0",
|
|
30
|
+
"@travetto/model": "^8.0.0-alpha.0",
|
|
31
|
+
"@travetto/runtime": "^8.0.0-alpha.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@travetto/cli": "^
|
|
34
|
+
"@travetto/cli": "^8.0.0-alpha.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependenciesMeta": {
|
|
37
37
|
"@travetto/cli": {
|
package/src/config.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Runtime, RuntimeResources } from '@travetto/runtime';
|
|
1
|
+
import { JSONUtil, Runtime, RuntimeResources } from '@travetto/runtime';
|
|
2
2
|
import { Config } from '@travetto/config';
|
|
3
3
|
import { Schema, SchemaValidator } from '@travetto/schema';
|
|
4
4
|
|
|
@@ -28,9 +28,8 @@ export class FirestoreModelConfig {
|
|
|
28
28
|
process.env.FIRESTORE_EMULATOR_HOST = this.emulator;
|
|
29
29
|
}
|
|
30
30
|
if (this.credentialsFile && !this.credentials) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
);
|
|
31
|
+
const bytes = await RuntimeResources.readBinaryArray(this.credentialsFile);
|
|
32
|
+
this.credentials = FirestoreModelConfigCredentials.from(JSONUtil.fromBinaryArray(bytes));
|
|
34
33
|
await SchemaValidator.validate(FirestoreModelConfigCredentials, this.credentials);
|
|
35
34
|
}
|
|
36
35
|
}
|
package/src/service.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type DocumentData, FieldValue, Firestore, type
|
|
1
|
+
import { type DocumentData, FieldValue, Firestore, type Query } from '@google-cloud/firestore';
|
|
2
2
|
|
|
3
3
|
import { JSONUtil, ShutdownManager, type Class, type DeepPartial } from '@travetto/runtime';
|
|
4
4
|
import { Injectable } from '@travetto/di';
|
|
@@ -10,10 +10,9 @@ import {
|
|
|
10
10
|
|
|
11
11
|
import type { FirestoreModelConfig } from './config.ts';
|
|
12
12
|
|
|
13
|
-
const clone =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
JSONUtil.parseSafe(JSON.stringify(input, (_, value) => value ?? null), (_, value) => value ?? missingValue);
|
|
13
|
+
const clone = JSONUtil.clone;
|
|
14
|
+
const setMissingValues = <T>(input: T, missingValue: unknown): T =>
|
|
15
|
+
JSONUtil.clone(input, { replacer: (_, value) => value ?? null, reviver: (_, value) => value ?? missingValue });
|
|
17
16
|
|
|
18
17
|
/**
|
|
19
18
|
* A model service backed by Firestore
|
|
@@ -40,7 +39,7 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
async postConstruct(): Promise<void> {
|
|
43
|
-
this.client = new Firestore(this.config);
|
|
42
|
+
this.client = new Firestore({ ...this.config, useBigInt: true });
|
|
44
43
|
ShutdownManager.signal.addEventListener('abort', () => this.client.terminate());
|
|
45
44
|
}
|
|
46
45
|
|
|
@@ -66,21 +65,21 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
66
65
|
|
|
67
66
|
async create<T extends ModelType>(cls: Class<T>, item: OptionalId<T>): Promise<T> {
|
|
68
67
|
const prepped = await ModelCrudUtil.preStore(cls, item, this);
|
|
69
|
-
await this.#getCollection(cls).doc(prepped.id).create(clone(prepped));
|
|
68
|
+
await this.#getCollection(cls).doc(prepped.id).create(clone<DocumentData>(prepped));
|
|
70
69
|
return prepped;
|
|
71
70
|
}
|
|
72
71
|
|
|
73
72
|
async update<T extends ModelType>(cls: Class<T>, item: T): Promise<T> {
|
|
74
73
|
ModelCrudUtil.ensureNotSubType(cls);
|
|
75
74
|
const prepped = await ModelCrudUtil.preStore(cls, item, this);
|
|
76
|
-
await this.#getCollection(cls).doc(item.id).
|
|
75
|
+
await this.#getCollection(cls).doc(item.id).set(clone<DocumentData>(prepped), { merge: false });
|
|
77
76
|
return prepped;
|
|
78
77
|
}
|
|
79
78
|
|
|
80
79
|
async upsert<T extends ModelType>(cls: Class<T>, item: OptionalId<T>): Promise<T> {
|
|
81
80
|
ModelCrudUtil.ensureNotSubType(cls);
|
|
82
81
|
const prepped = await ModelCrudUtil.preStore(cls, item, this);
|
|
83
|
-
await this.#getCollection(cls).doc(prepped.id).set(clone(prepped));
|
|
82
|
+
await this.#getCollection(cls).doc(prepped.id).set(clone<DocumentData>(prepped));
|
|
84
83
|
return prepped;
|
|
85
84
|
}
|
|
86
85
|
|
|
@@ -88,7 +87,7 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
88
87
|
ModelCrudUtil.ensureNotSubType(cls);
|
|
89
88
|
const id = item.id;
|
|
90
89
|
const full = await ModelCrudUtil.naivePartialUpdate(cls, () => this.get(cls, id), item, view);
|
|
91
|
-
const cleaned =
|
|
90
|
+
const cleaned = setMissingValues(full, FieldValue.delete());
|
|
92
91
|
await this.#getCollection(cls).doc(id).set(cleaned, { mergeFields: Object.keys(full) });
|
|
93
92
|
return this.get(cls, id);
|
|
94
93
|
}
|