@travetto/model-firestore 8.0.0-alpha.23 → 8.0.0-alpha.25
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 +3 -3
- package/package.json +1 -1
- package/src/service.ts +9 -5
- package/support/cli.firestore_indexes.ts +83 -0
package/README.md
CHANGED
|
@@ -42,7 +42,7 @@ where the [FirestoreModelConfig](https://github.com/travetto/travetto/tree/main/
|
|
|
42
42
|
```typescript
|
|
43
43
|
@Config('model.firestore')
|
|
44
44
|
export class FirestoreModelConfig {
|
|
45
|
-
|
|
45
|
+
databaseId?: string;
|
|
46
46
|
credentialsFile?: string;
|
|
47
47
|
emulator?: string;
|
|
48
48
|
projectId?: string;
|
|
@@ -52,8 +52,8 @@ export class FirestoreModelConfig {
|
|
|
52
52
|
|
|
53
53
|
@PostConstruct()
|
|
54
54
|
async finalizeConfig(): Promise<void> {
|
|
55
|
-
if (!this.
|
|
56
|
-
this.projectId
|
|
55
|
+
if (!this.projectId && !Runtime.production) {
|
|
56
|
+
this.projectId = 'trv-local-dev';
|
|
57
57
|
this.emulator ??= 'localhost:7000'; // From docker
|
|
58
58
|
}
|
|
59
59
|
if (this.emulator) {
|
package/package.json
CHANGED
package/src/service.ts
CHANGED
|
@@ -25,6 +25,14 @@ const setMissingValues = <T>(input: T, missingValue: unknown): T =>
|
|
|
25
25
|
@Injectable()
|
|
26
26
|
export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupport, ModelIndexedSupport {
|
|
27
27
|
|
|
28
|
+
static resolveTable(cls: Class, namespace?: string): string {
|
|
29
|
+
let table = ModelRegistryIndex.getStoreName(cls);
|
|
30
|
+
if (namespace) {
|
|
31
|
+
table = `${namespace}_${table}`;
|
|
32
|
+
}
|
|
33
|
+
return table;
|
|
34
|
+
}
|
|
35
|
+
|
|
28
36
|
idSource = ModelCrudUtil.uuidSource();
|
|
29
37
|
client: Firestore;
|
|
30
38
|
config: FirestoreModelConfig;
|
|
@@ -32,11 +40,7 @@ export class FirestoreModelService implements ModelCrudSupport, ModelStorageSupp
|
|
|
32
40
|
constructor(config: FirestoreModelConfig) { this.config = config; }
|
|
33
41
|
|
|
34
42
|
#resolveTable(cls: Class): string {
|
|
35
|
-
|
|
36
|
-
if (this.config.namespace) {
|
|
37
|
-
table = `${this.config.namespace}_${table}`;
|
|
38
|
-
}
|
|
39
|
-
return table;
|
|
43
|
+
return FirestoreModelService.resolveTable(cls, this.config.namespace);
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
#getCollection(cls: Class): FirebaseFirestore.CollectionReference<DocumentData> {
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type CliCommandShape, CliCommand, CliModuleFlag } from '@travetto/cli';
|
|
2
|
+
import { JSONUtil, Env } from '@travetto/runtime';
|
|
3
|
+
import { Registry } from '@travetto/registry';
|
|
4
|
+
import { DependencyRegistryIndex } from '@travetto/di';
|
|
5
|
+
import { ModelRegistryIndex } from '@travetto/model';
|
|
6
|
+
import { isModelIndexedIndex } from '@travetto/model-indexed';
|
|
7
|
+
import { ManifestFileUtil, ManifestUtil } from '@travetto/manifest';
|
|
8
|
+
|
|
9
|
+
import { FirestoreModelConfig } from '../src/config.ts';
|
|
10
|
+
import { FirestoreModelService } from '../src/service.ts';
|
|
11
|
+
|
|
12
|
+
type FirestoreIndexField = {
|
|
13
|
+
fieldPath: string;
|
|
14
|
+
order: 'ASCENDING' | 'DESCENDING';
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type FirestoreIndexDefinition = {
|
|
18
|
+
collectionGroup: string;
|
|
19
|
+
queryScope: 'COLLECTION' | 'COLLECTION_GROUP';
|
|
20
|
+
fields: FirestoreIndexField[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type FirestoreIndexSet = {
|
|
24
|
+
indexes: FirestoreIndexDefinition[];
|
|
25
|
+
fieldOverrides: unknown[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Generate the Firestore composite indexes JSON for all registered models.
|
|
30
|
+
*
|
|
31
|
+
* The resulting JSON can be written to stdout or to a file path for use in
|
|
32
|
+
* firebase-cli deployments.
|
|
33
|
+
*/
|
|
34
|
+
@CliCommand()
|
|
35
|
+
export class FirestoreIndexesCommand implements CliCommandShape {
|
|
36
|
+
|
|
37
|
+
/** Output file */
|
|
38
|
+
output?: string;
|
|
39
|
+
|
|
40
|
+
@CliModuleFlag({ short: 'm' })
|
|
41
|
+
module: string;
|
|
42
|
+
|
|
43
|
+
finalize(): void {
|
|
44
|
+
Env.DEBUG.set(false);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async main(): Promise<void> {
|
|
48
|
+
await Registry.init();
|
|
49
|
+
|
|
50
|
+
const config = await DependencyRegistryIndex.getInstance(FirestoreModelConfig);
|
|
51
|
+
|
|
52
|
+
const indexesList: FirestoreIndexDefinition[] = [];
|
|
53
|
+
|
|
54
|
+
for (const cls of ModelRegistryIndex.getClasses()) {
|
|
55
|
+
const indices = ModelRegistryIndex.getIndices(cls)
|
|
56
|
+
.filter(isModelIndexedIndex)
|
|
57
|
+
// We need at least 2 fields. All 1 field indices are already handled
|
|
58
|
+
.filter((idx => (idx.keyTemplate?.length ?? 0) + (idx.sortTemplate?.length ?? 0) >= 2));
|
|
59
|
+
|
|
60
|
+
for (const idx of indices) {
|
|
61
|
+
indexesList.push({
|
|
62
|
+
collectionGroup: FirestoreModelService.resolveTable(cls, config.namespace),
|
|
63
|
+
queryScope: 'COLLECTION',
|
|
64
|
+
fields: [...idx.keyTemplate, ...idx.sortTemplate]
|
|
65
|
+
.map(part => ({ fieldPath: part.path.join('.'), order: part.value === -1 ? 'DESCENDING' : 'ASCENDING' }))
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const outputData: FirestoreIndexSet = {
|
|
71
|
+
indexes: indexesList,
|
|
72
|
+
fieldOverrides: []
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const text = JSONUtil.toUTF8Pretty(outputData);
|
|
76
|
+
|
|
77
|
+
if (this.output === '-' || !this.output) {
|
|
78
|
+
console.log!(text);
|
|
79
|
+
} else {
|
|
80
|
+
await ManifestFileUtil.bufferedFileWrite(this.output, text);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|