Skip to main content

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:

  1. Add the new property to a database model.
    • modules/<module>/core/models/<model>.ts
  2. 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:

  1. [optional] Add a new column to the insights database via a migration script.
    • services/insights/sql/
  2. [optional] Update the ETL transformer.
    • services/<service>/api/migrations/tasks/<model>-transformers.ts
  3. [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: