@travetto/model-firestore 7.0.0-rc.1 → 7.0.0-rc.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.
- package/README.md +4 -4
- package/package.json +5 -5
- package/src/config.ts +8 -4
- package/src/service.ts +22 -22
package/README.md
CHANGED
|
@@ -30,8 +30,8 @@ export class Init {
|
|
|
30
30
|
@InjectableFactory({
|
|
31
31
|
primary: true
|
|
32
32
|
})
|
|
33
|
-
static getModelSource(
|
|
34
|
-
return new FirestoreModelService(
|
|
33
|
+
static getModelSource(config: FirestoreModelConfig) {
|
|
34
|
+
return new FirestoreModelService(config);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
```
|
|
@@ -47,7 +47,7 @@ export class FirestoreModelConfig {
|
|
|
47
47
|
emulator?: string;
|
|
48
48
|
projectId: string;
|
|
49
49
|
namespace?: string;
|
|
50
|
-
|
|
50
|
+
modifyStorage?: boolean;
|
|
51
51
|
credentials?: FirestoreModelConfigCredentials;
|
|
52
52
|
|
|
53
53
|
async postConstruct(): Promise<void> {
|
|
@@ -56,7 +56,7 @@ export class FirestoreModelConfig {
|
|
|
56
56
|
}
|
|
57
57
|
if (this.credentialsFile && !this.credentials) {
|
|
58
58
|
this.credentials = FirestoreModelConfigCredentials.from(
|
|
59
|
-
|
|
59
|
+
await RuntimeResources.readJSON(this.credentialsFile)
|
|
60
60
|
);
|
|
61
61
|
await SchemaValidator.validate(FirestoreModelConfigCredentials, this.credentials);
|
|
62
62
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-firestore",
|
|
3
|
-
"version": "7.0.0-rc.
|
|
3
|
+
"version": "7.0.0-rc.3",
|
|
4
4
|
"description": "Firestore backing for the travetto model module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@google-cloud/firestore": "^7.11.6",
|
|
29
|
-
"@travetto/cli": "^7.0.0-rc.
|
|
30
|
-
"@travetto/config": "^7.0.0-rc.
|
|
31
|
-
"@travetto/model": "^7.0.0-rc.
|
|
32
|
-
"@travetto/runtime": "^7.0.0-rc.
|
|
29
|
+
"@travetto/cli": "^7.0.0-rc.3",
|
|
30
|
+
"@travetto/config": "^7.0.0-rc.3",
|
|
31
|
+
"@travetto/model": "^7.0.0-rc.3",
|
|
32
|
+
"@travetto/runtime": "^7.0.0-rc.3"
|
|
33
33
|
},
|
|
34
34
|
"travetto": {
|
|
35
35
|
"displayName": "Firestore Model Support"
|
package/src/config.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RuntimeResources } from '@travetto/runtime';
|
|
1
|
+
import { Runtime, RuntimeResources } from '@travetto/runtime';
|
|
2
2
|
import { Config } from '@travetto/config';
|
|
3
3
|
import { Schema, SchemaValidator } from '@travetto/schema';
|
|
4
4
|
|
|
@@ -14,18 +14,22 @@ export class FirestoreModelConfig {
|
|
|
14
14
|
databaseURL?: string;
|
|
15
15
|
credentialsFile?: string;
|
|
16
16
|
emulator?: string;
|
|
17
|
-
projectId
|
|
17
|
+
projectId?: string;
|
|
18
18
|
namespace?: string;
|
|
19
|
-
|
|
19
|
+
modifyStorage?: boolean;
|
|
20
20
|
credentials?: FirestoreModelConfigCredentials;
|
|
21
21
|
|
|
22
22
|
async postConstruct(): Promise<void> {
|
|
23
|
+
if (!this.databaseURL && !Runtime.production) {
|
|
24
|
+
this.projectId ??= 'trv-local-dev';
|
|
25
|
+
this.emulator ??= 'localhost:7000'; // From docker
|
|
26
|
+
}
|
|
23
27
|
if (this.emulator) {
|
|
24
28
|
process.env.FIRESTORE_EMULATOR_HOST = this.emulator;
|
|
25
29
|
}
|
|
26
30
|
if (this.credentialsFile && !this.credentials) {
|
|
27
31
|
this.credentials = FirestoreModelConfigCredentials.from(
|
|
28
|
-
|
|
32
|
+
await RuntimeResources.readJSON(this.credentialsFile)
|
|
29
33
|
);
|
|
30
34
|
await SchemaValidator.validate(FirestoreModelConfigCredentials, this.credentials);
|
|
31
35
|
}
|
package/src/service.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DocumentData, FieldValue, Firestore, PartialWithFieldValue, Query } from '@google-cloud/firestore';
|
|
2
2
|
|
|
3
|
-
import { ShutdownManager, type Class, type DeepPartial } from '@travetto/runtime';
|
|
3
|
+
import { JSONUtil, ShutdownManager, type Class, type DeepPartial } from '@travetto/runtime';
|
|
4
4
|
import { Injectable } from '@travetto/di';
|
|
5
5
|
import {
|
|
6
6
|
ModelCrudSupport, ModelRegistryIndex, ModelStorageSupport,
|
|
@@ -12,8 +12,8 @@ import { FirestoreModelConfig } from './config.ts';
|
|
|
12
12
|
|
|
13
13
|
const clone = structuredClone;
|
|
14
14
|
|
|
15
|
-
const
|
|
16
|
-
|
|
15
|
+
const toSimpleObject = <T>(input: T, missingValue: unknown = null): PartialWithFieldValue<DocumentData> =>
|
|
16
|
+
JSONUtil.parseSafe(JSON.stringify(input, (_, value) => value ?? null), (_, value) => value ?? missingValue);
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* A model service backed by Firestore
|
|
@@ -49,8 +49,8 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
49
49
|
async deleteStorage(): Promise<void> { }
|
|
50
50
|
|
|
51
51
|
async deleteModel(cls: Class): Promise<void> {
|
|
52
|
-
for await (const
|
|
53
|
-
await this.delete(cls,
|
|
52
|
+
for await (const item of this.list(cls)) {
|
|
53
|
+
await this.delete(cls, item.id);
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
@@ -88,7 +88,7 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
88
88
|
ModelCrudUtil.ensureNotSubType(cls);
|
|
89
89
|
const id = item.id;
|
|
90
90
|
const full = await ModelCrudUtil.naivePartialUpdate(cls, () => this.get(cls, id), item, view);
|
|
91
|
-
const cleaned =
|
|
91
|
+
const cleaned = toSimpleObject(full, FieldValue.delete());
|
|
92
92
|
await this.#getCollection(cls).doc(id).set(cleaned, { mergeFields: Object.keys(full) });
|
|
93
93
|
return this.get(cls, id);
|
|
94
94
|
}
|
|
@@ -97,22 +97,22 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
97
97
|
ModelCrudUtil.ensureNotSubType(cls);
|
|
98
98
|
try {
|
|
99
99
|
await this.#getCollection(cls).doc(id).delete({ exists: true });
|
|
100
|
-
} catch (
|
|
101
|
-
if (
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (error && error instanceof Error && error.message.includes('NOT_FOUND')) {
|
|
102
102
|
throw new NotFoundError(cls, id);
|
|
103
103
|
}
|
|
104
|
-
throw
|
|
104
|
+
throw error;
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
async * list<T extends ModelType>(cls: Class<T>): AsyncIterable<T> {
|
|
109
109
|
const batch = await this.#getCollection(cls).select().get();
|
|
110
|
-
for (const
|
|
110
|
+
for (const item of batch.docs) {
|
|
111
111
|
try {
|
|
112
|
-
yield await this.get(cls,
|
|
113
|
-
} catch (
|
|
114
|
-
if (!(
|
|
115
|
-
throw
|
|
112
|
+
yield await this.get(cls, item.id);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (!(error instanceof NotFoundError)) {
|
|
115
|
+
throw error;
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
118
|
}
|
|
@@ -124,7 +124,7 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
124
124
|
|
|
125
125
|
const { fields } = ModelIndexedUtil.computeIndexParts(cls, idx, body);
|
|
126
126
|
const query = fields.reduce<Query>(
|
|
127
|
-
(
|
|
127
|
+
(result, { path, value }) => result.where(path.join('.'), '==', value),
|
|
128
128
|
this.#getCollection(cls)
|
|
129
129
|
);
|
|
130
130
|
|
|
@@ -133,7 +133,7 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
133
133
|
if (item && !item.empty) {
|
|
134
134
|
return item.docs[0].id;
|
|
135
135
|
}
|
|
136
|
-
throw new NotFoundError(`${cls.name} Index=${idx}`, ModelIndexedUtil.computeIndexKey(cls, idx, body, {
|
|
136
|
+
throw new NotFoundError(`${cls.name} Index=${idx}`, ModelIndexedUtil.computeIndexKey(cls, idx, body, { separator: '; ' })?.key);
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
async getByIndex<T extends ModelType>(cls: Class<T>, idx: string, body: DeepPartial<T>): Promise<T> {
|
|
@@ -151,17 +151,17 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
151
151
|
async * listByIndex<T extends ModelType>(cls: Class<T>, idx: string, body: DeepPartial<T>): AsyncIterable<T> {
|
|
152
152
|
ModelCrudUtil.ensureNotSubType(cls);
|
|
153
153
|
|
|
154
|
-
const
|
|
155
|
-
const { fields, sorted } = ModelIndexedUtil.computeIndexParts(cls,
|
|
156
|
-
let query = fields.reduce<Query>((
|
|
157
|
-
|
|
154
|
+
const config = ModelRegistryIndex.getIndex(cls, idx, ['sorted', 'unsorted']);
|
|
155
|
+
const { fields, sorted } = ModelIndexedUtil.computeIndexParts(cls, config, body, { emptySortValue: null });
|
|
156
|
+
let query = fields.reduce<Query>((result, { path, value }) =>
|
|
157
|
+
result.where(path.join('.'), '==', value), this.#getCollection(cls));
|
|
158
158
|
|
|
159
159
|
if (sorted) {
|
|
160
160
|
query = query.orderBy(sorted.path.join('.'), sorted.dir === 1 ? 'asc' : 'desc');
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
for (const
|
|
164
|
-
yield await ModelCrudUtil.load(cls,
|
|
163
|
+
for (const item of (await query.get()).docs) {
|
|
164
|
+
yield await ModelCrudUtil.load(cls, item.data()!);
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
}
|