How do I add a new property to a database model?
Objective: This article explains how to add a new property to a database model, seed the database with a default value and update ETL scripts.
Steps:
- Add the new property to a database model.
modules/<module>/core/models/<model>.ts
- Seed the database with a default value for the new property.
services/<service>/api/migrations/scripts/
The following steps are applicable if the data needs to be added to the insights:
- [optional] Add a new column to the insights database via a migration script.
services/insights/sql/
- [optional] Update the ETL transformer.
services/<service>/api/migrations/tasks/<model>-transformers.ts
- [optional] Update the ETL tasks in Raven Studio, mapping the table and ID column.
Example:
// Example seed migration script:
import { MyModel } from '@hectare/platform.modules.<module>.core'
import { utils } from '@hectare/platform.components.ravendb'
import { DocumentSession, DocumentStore } from 'ravendb'
export const up = async (store: DocumentStore) => {
const patchModel = async (session: DocumentSession, model: MyModel) => {
// Check if the property exists, if not, seed it with a default value.
if (!Object.prototype.hasOwnProperty.call(model, 'newProperty')) {
model.newProperty = 123
}
}
await utils.patch(MyModel, store, patchModel)
}
export const down = async () => {}
Related Articles: