@travetto/model-firestore 6.0.0-rc.0 → 6.0.0-rc.2
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 +8 -13
- package/__index__.ts +2 -2
- package/package.json +5 -5
- package/src/config.ts +13 -7
- package/src/service.ts +5 -7
package/README.md
CHANGED
|
@@ -16,8 +16,8 @@ yarn add @travetto/model-firestore
|
|
|
16
16
|
This module provides an [Firestore](https://firebase.google.com/docs/firestore)-based implementation of the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations."). This source allows the [Data Modeling Support](https://github.com/travetto/travetto/tree/main/module/model#readme "Datastore abstraction for core operations.") module to read, write and query against [Firestore](https://firebase.google.com/docs/firestore).
|
|
17
17
|
|
|
18
18
|
Supported features:
|
|
19
|
-
* [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/
|
|
20
|
-
* [Indexed](https://github.com/travetto/travetto/tree/main/module/model/src/
|
|
19
|
+
* [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L11)
|
|
20
|
+
* [Indexed](https://github.com/travetto/travetto/tree/main/module/model/src/types/indexed.ts#L11)
|
|
21
21
|
Out of the box, by installing the module, everything should be wired up by default.If you need to customize any aspect of the source or config, you can override and register it with the [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support.") module.
|
|
22
22
|
|
|
23
23
|
**Code: Wiring up a custom Model Source**
|
|
@@ -35,34 +35,29 @@ export class Init {
|
|
|
35
35
|
}
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
where the [FirestoreModelConfig](https://github.com/travetto/travetto/tree/main/module/model-firestore/src/config.ts#
|
|
38
|
+
where the [FirestoreModelConfig](https://github.com/travetto/travetto/tree/main/module/model-firestore/src/config.ts#L6) is defined by:
|
|
39
39
|
|
|
40
40
|
**Code: Structure of FirestoreModelConfig**
|
|
41
41
|
```typescript
|
|
42
|
-
import { RuntimeResources } from '@travetto/runtime';
|
|
43
|
-
import { Config } from '@travetto/config';
|
|
44
|
-
|
|
45
42
|
@Config('model.firestore')
|
|
46
43
|
export class FirestoreModelConfig {
|
|
47
|
-
|
|
48
44
|
databaseURL?: string;
|
|
49
45
|
credentialsFile?: string;
|
|
50
46
|
emulator?: string;
|
|
51
47
|
projectId: string;
|
|
52
48
|
namespace?: string;
|
|
53
49
|
autoCreate?: boolean;
|
|
54
|
-
credentials?:
|
|
55
|
-
client_email: string;
|
|
56
|
-
project_id: string;
|
|
57
|
-
private_key: string;
|
|
58
|
-
};
|
|
50
|
+
credentials?: FirestoreModelConfigCredentials;
|
|
59
51
|
|
|
60
52
|
async postConstruct(): Promise<void> {
|
|
61
53
|
if (this.emulator) {
|
|
62
54
|
process.env.FIRESTORE_EMULATOR_HOST = this.emulator;
|
|
63
55
|
}
|
|
64
56
|
if (this.credentialsFile && !this.credentials) {
|
|
65
|
-
this.credentials =
|
|
57
|
+
this.credentials = FirestoreModelConfigCredentials.from(
|
|
58
|
+
JSON.parse(await RuntimeResources.read(this.credentialsFile))
|
|
59
|
+
);
|
|
60
|
+
await SchemaValidator.validate(FirestoreModelConfigCredentials, this.credentials);
|
|
66
61
|
}
|
|
67
62
|
}
|
|
68
63
|
}
|
package/__index__.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './src/service';
|
|
2
|
-
export * from './src/config';
|
|
1
|
+
export * from './src/service.ts';
|
|
2
|
+
export * from './src/config.ts';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-firestore",
|
|
3
|
-
"version": "6.0.0-rc.
|
|
3
|
+
"version": "6.0.0-rc.2",
|
|
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.0",
|
|
29
|
-
"@travetto/cli": "^6.0.0-rc.
|
|
30
|
-
"@travetto/config": "^6.0.0-rc.
|
|
31
|
-
"@travetto/model": "^6.0.0-rc.
|
|
32
|
-
"@travetto/runtime": "^6.0.0-rc.
|
|
29
|
+
"@travetto/cli": "^6.0.0-rc.2",
|
|
30
|
+
"@travetto/config": "^6.0.0-rc.2",
|
|
31
|
+
"@travetto/model": "^6.0.0-rc.2",
|
|
32
|
+
"@travetto/runtime": "^6.0.0-rc.2"
|
|
33
33
|
},
|
|
34
34
|
"travetto": {
|
|
35
35
|
"displayName": "Firestore Model Support"
|
package/src/config.ts
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
import { RuntimeResources } from '@travetto/runtime';
|
|
2
2
|
import { Config } from '@travetto/config';
|
|
3
|
+
import { Schema, SchemaValidator } from '@travetto/schema';
|
|
4
|
+
|
|
5
|
+
@Schema()
|
|
6
|
+
class FirestoreModelConfigCredentials {
|
|
7
|
+
client_email: string;
|
|
8
|
+
project_id: string;
|
|
9
|
+
private_key: string;
|
|
10
|
+
}
|
|
3
11
|
|
|
4
12
|
@Config('model.firestore')
|
|
5
13
|
export class FirestoreModelConfig {
|
|
6
|
-
|
|
7
14
|
databaseURL?: string;
|
|
8
15
|
credentialsFile?: string;
|
|
9
16
|
emulator?: string;
|
|
10
17
|
projectId: string;
|
|
11
18
|
namespace?: string;
|
|
12
19
|
autoCreate?: boolean;
|
|
13
|
-
credentials?:
|
|
14
|
-
client_email: string;
|
|
15
|
-
project_id: string;
|
|
16
|
-
private_key: string;
|
|
17
|
-
};
|
|
20
|
+
credentials?: FirestoreModelConfigCredentials;
|
|
18
21
|
|
|
19
22
|
async postConstruct(): Promise<void> {
|
|
20
23
|
if (this.emulator) {
|
|
21
24
|
process.env.FIRESTORE_EMULATOR_HOST = this.emulator;
|
|
22
25
|
}
|
|
23
26
|
if (this.credentialsFile && !this.credentials) {
|
|
24
|
-
this.credentials =
|
|
27
|
+
this.credentials = FirestoreModelConfigCredentials.from(
|
|
28
|
+
JSON.parse(await RuntimeResources.read(this.credentialsFile))
|
|
29
|
+
);
|
|
30
|
+
await SchemaValidator.validate(FirestoreModelConfigCredentials, this.credentials);
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
33
|
}
|
package/src/service.ts
CHANGED
|
@@ -5,12 +5,10 @@ import { Injectable } from '@travetto/di';
|
|
|
5
5
|
import {
|
|
6
6
|
ModelCrudSupport, ModelRegistry, ModelStorageSupport,
|
|
7
7
|
ModelIndexedSupport, ModelType, NotFoundError, OptionalId,
|
|
8
|
+
ModelCrudUtil, ModelIndexedUtil,
|
|
8
9
|
} from '@travetto/model';
|
|
9
10
|
|
|
10
|
-
import {
|
|
11
|
-
import { ModelIndexedUtil } from '@travetto/model/src/internal/service/indexed';
|
|
12
|
-
|
|
13
|
-
import { FirestoreModelConfig } from './config';
|
|
11
|
+
import { FirestoreModelConfig } from './config.ts';
|
|
14
12
|
|
|
15
13
|
const clone = structuredClone;
|
|
16
14
|
|
|
@@ -58,10 +56,10 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
58
56
|
|
|
59
57
|
// Crud
|
|
60
58
|
async get<T extends ModelType>(cls: Class<T>, id: string): Promise<T> {
|
|
61
|
-
const
|
|
59
|
+
const result = await this.#getCollection(cls).doc(id).get();
|
|
62
60
|
|
|
63
|
-
if (
|
|
64
|
-
return await ModelCrudUtil.load(cls,
|
|
61
|
+
if (result && result.exists) {
|
|
62
|
+
return await ModelCrudUtil.load(cls, result.data()!);
|
|
65
63
|
}
|
|
66
64
|
throw new NotFoundError(cls, id);
|
|
67
65
|
}
|