@travetto/model-firestore 8.0.0-alpha.24 → 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 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
- databaseURL?: string;
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.databaseURL && !Runtime.production) {
56
- this.projectId ??= 'trv-local-dev';
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-firestore",
3
- "version": "8.0.0-alpha.24",
3
+ "version": "8.0.0-alpha.25",
4
4
  "description": "Firestore backing for the travetto model module.",
5
5
  "keywords": [
6
6
  "typescript",
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
- let table = ModelRegistryIndex.getStoreName(cls);
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> {
@@ -1,14 +1,29 @@
1
- import fs from 'node:fs/promises';
2
- import path from 'node:path';
3
-
4
1
  import { type CliCommandShape, CliCommand, CliModuleFlag } from '@travetto/cli';
5
2
  import { JSONUtil, Env } from '@travetto/runtime';
6
3
  import { Registry } from '@travetto/registry';
7
4
  import { DependencyRegistryIndex } from '@travetto/di';
8
5
  import { ModelRegistryIndex } from '@travetto/model';
9
6
  import { isModelIndexedIndex } from '@travetto/model-indexed';
7
+ import { ManifestFileUtil, ManifestUtil } from '@travetto/manifest';
10
8
 
11
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
+ };
12
27
 
13
28
  /**
14
29
  * Generate the Firestore composite indexes JSON for all registered models.
@@ -34,49 +49,25 @@ export class FirestoreIndexesCommand implements CliCommandShape {
34
49
 
35
50
  const config = await DependencyRegistryIndex.getInstance(FirestoreModelConfig);
36
51
 
37
- const indexesList: unknown[] = [];
52
+ const indexesList: FirestoreIndexDefinition[] = [];
38
53
 
39
54
  for (const cls of ModelRegistryIndex.getClasses()) {
40
- let collectionGroup = ModelRegistryIndex.getStoreName(cls);
41
- if (config.namespace) {
42
- collectionGroup = `${config.namespace}_${collectionGroup}`;
43
- }
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));
44
59
 
45
- const indices = ModelRegistryIndex.getIndices(cls);
46
60
  for (const idx of indices) {
47
- if (isModelIndexedIndex(idx)) {
48
- const fieldsCount = (idx.keyTemplate?.length ?? 0) + (idx.sortTemplate?.length ?? 0);
49
- // Only create composite indexes if we have at least 2 fields
50
- if (fieldsCount >= 2) {
51
- const fields: { fieldPath: string, order: 'ASCENDING' | 'DESCENDING' }[] = [];
52
-
53
- // Add key fields first
54
- for (const part of idx.keyTemplate) {
55
- fields.push({
56
- fieldPath: part.path.join('.'),
57
- order: 'ASCENDING'
58
- });
59
- }
60
-
61
- // Add sort fields second
62
- for (const part of idx.sortTemplate) {
63
- fields.push({
64
- fieldPath: part.path.join('.'),
65
- order: part.value === 1 ? 'ASCENDING' : 'DESCENDING'
66
- });
67
- }
68
-
69
- indexesList.push({
70
- collectionGroup,
71
- queryScope: 'COLLECTION',
72
- fields
73
- });
74
- }
75
- }
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
+ });
76
67
  }
77
68
  }
78
69
 
79
- const outputData = {
70
+ const outputData: FirestoreIndexSet = {
80
71
  indexes: indexesList,
81
72
  fieldOverrides: []
82
73
  };
@@ -86,8 +77,7 @@ export class FirestoreIndexesCommand implements CliCommandShape {
86
77
  if (this.output === '-' || !this.output) {
87
78
  console.log!(text);
88
79
  } else {
89
- await fs.mkdir(path.dirname(this.output), { recursive: true });
90
- await fs.writeFile(this.output, text, 'utf8');
80
+ await ManifestFileUtil.bufferedFileWrite(this.output, text);
91
81
  }
92
82
  }
93
83
  }