@travetto/model-elasticsearch
Version:
Elasticsearch backing for the travetto model module, with real-time modeling support for Elasticsearch mappings.
111 lines (94 loc) • 4.49 kB
Markdown
<!-- This file was generated by /doc and should not be modified directly -->
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/model-elasticsearch/DOC.tsx and execute "npx trv doc" to rebuild -->
# Elasticsearch Model Source
## Elasticsearch backing for the travetto model module, with real-time modeling support for Elasticsearch mappings.
**Install: /model-elasticsearch**
```bash
npm install /model-elasticsearch
# or
yarn add /model-elasticsearch
```
This module provides an [elasticsearch](https://elastic.co)-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 [elasticsearch](https://elastic.co). In development mode, [ElasticsearchModelService](https://github.com/travetto/travetto/tree/main/module/model-elasticsearch/src/service.ts#L30) will also modify the [elasticsearch](https://elastic.co) schema in real time to minimize impact to development.
Supported features:
* [CRUD](https://github.com/travetto/travetto/tree/main/module/model/src/types/crud.ts#L11)
* [Bulk](https://github.com/travetto/travetto/tree/main/module/model/src/types/bulk.ts#L64)
* [Expiry](https://github.com/travetto/travetto/tree/main/module/model/src/types/expiry.ts#L10)
* [Indexed](https://github.com/travetto/travetto/tree/main/module/model/src/types/indexed.ts#L11)
* [Query Crud](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/crud.ts#L11)
* [Facet](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/facet.ts#L14)
* [Query](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/query.ts#L10)
* [Suggest](https://github.com/travetto/travetto/tree/main/module/model-query/src/types/suggest.ts#L12)
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.
**Code: Wiring up a custom Model Source**
```typescript
import { InjectableFactory } from '@travetto/di';
import { ElasticsearchModelConfig, ElasticsearchModelService } from '@travetto/model-elasticsearch';
export class Init {
static getModelSource(conf: ElasticsearchModelConfig) {
return new ElasticsearchModelService(conf);
}
}
```
where the [ElasticsearchModelConfig](https://github.com/travetto/travetto/tree/main/module/model-elasticsearch/src/config.ts#L11) is defined by:
**Code: Structure of ElasticsearchModelConfig**
```typescript
export class ElasticsearchModelConfig {
/**
* List of hosts to support
*/
hosts = ['127.0.0.1'];
/**
* Port to listen on
*/
port = 9200;
/**
* Raw elasticsearch options
*/
options = {};
/**
* Index prefix
*/
namespace = 'app';
/**
* Auto-create, disabled in prod by default
*/
autoCreate?: boolean;
/**
* Should we store the id as a string in the document
*/
storeId?: boolean;
/**
* Base schema config for elasticsearch
*/
schemaConfig: EsSchemaConfig = {
caseSensitive: false
};
/**
* Base index create settings
*/
indexCreate = {
['number_of_replicas']: 0,
['number_of_shards']: 1
};
/**
* Frequency of culling for cullable content
*/
cullRate?: number | TimeSpan;
/**
* Build final hosts
*/
postConstruct(): void {
console.debug('Constructed', { config: this });
this.hosts = this.hosts
.map(x => x.includes(':') ? x : `${x}:${this.port}`)
.map(x => x.startsWith('http') ? x : `http://${x}`);
}
}
```
Additionally, you can see that the class is registered with the [](https://github.com/travetto/travetto/tree/main/module/config/src/decorator.ts#L13) annotation, and so these values can be overridden using the standard [Configuration](https://github.com/travetto/travetto/tree/main/module/config#readme "Configuration support") resolution paths.