Install: @travetto/model-firestore
npm install @travetto/model-firestore
# or
yarn add @travetto/model-firestore
This module provides an Firestore-based implementation of the Data Modeling Support. This source allows the Data Modeling Support module to read, write and query against Firestore.
Supported features:
- CRUD
- Indexed 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 module.
Code: Wiring up a custom Model Source
import { InjectableFactory } from '@travetto/di';
import { FirestoreModelConfig, FirestoreModelService } from '@travetto/model-firestore';
export class Init {
@InjectableFactory({
primary: true
})
static getModelSource(conf: FirestoreModelConfig) {
return new FirestoreModelService(conf);
}
}
where the FirestoreModelConfig is defined by:
Code: Structure of FirestoreModelConfig
import { RuntimeResources } from '@travetto/runtime';
import { Config } from '@travetto/config';
@Config('model.firestore')
export class FirestoreModelConfig {
databaseURL?: string;
credentialsFile?: string;
emulator?: string;
projectId: string;
namespace?: string;
autoCreate?: boolean;
credentials?: {
client_email: string;
project_id: string;
private_key: string;
};
async postConstruct(): Promise<void> {
if (this.emulator) {
process.env.FIRESTORE_EMULATOR_HOST = this.emulator;
}
if (this.credentialsFile && !this.credentials) {
this.credentials = JSON.parse(await RuntimeResources.read(this.credentialsFile));
}
}
}
Additionally, you can see that the class is registered with the @Config annotation, and so these values can be overridden using the standard Configuration resolution paths.