Introduction Extend Entity

When you application grows, you entities will grow too. At some point, it could be useful to refactor your entities to make them more readable and maintainable by making them smaller.

Imagine a Task having an id, title, completed and description fields (and way more!). It could be good to refactor it to have a Task entity with only the id, title and completed fields, and then have a TaskExtra entity with the rest of the fields, here description.

Extending an entity is a powerful feature that allows you to add new fields to an existing entity without changing the base entity. By doing this, you will not overload the base entity with fields that are not relevant all the time.

How to

Create a new entity that extends the base entity and add the new fields to it.

shared/TaskExtra.ts
import { Entity, Fields } from 'remult'
import { Task } from './Task.js'
@Entity<TaskExtra>('TaskExtraKey', {
dbName: 'tasks',
})
export class TaskExtra extends Task {
@Fields.string()
description = ''
}

Code Explanation

  • You need to have a dedicated key for this new entity, here TaskExtraKey.
    • It’s to really differentiate between the two entities for remult.
    • It will also create two different entries in Admin UI.
  • You need to set the dbName option to point to the right database name (same as the base entity).
    • Yes, by default, remult will use the entity key as the database name.

Try it out

// Get Task fields
await repo(Task).find()
// Get Task & TaskExtra fields
await repo(TaskExtra).find()
Powered by WebContainers
Files
Preparing Environment
  • Installing dependencies
  • Starting http server