# Entity

Decorates classes that should be used as entities. Receives a key and an array of EntityOptions.

example

import { Entity, Fields } from "remult";
@Entity("tasks",{
   allowApiCrud: true
})
export class Task {
   @Fields.uuid()
   id!: string;
   @Fields.string()
   title = '';
   @Fields.boolean()
   completed = false;
}

note

EntityOptions can be set in two ways:

example

// as an object
@Entity("tasks",{allowApiCrud:true })

example

// as an arrow function that receives `remult` as a parameter
@Entity("tasks",(options,remult) => options.allowApiCrud = true)

# caption

A human readable name for the entity

# allowApiRead

Determines if this Entity is available for get requests using Rest Api

see allowed (opens new window)

# allowApiUpdate

Determines if this entity can be updated through the api.

see allowed (opens new window)

# allowApiDelete

Determines if entries for this entity can be deleted through the api.

see allowed (opens new window)

# allowApiInsert

Determines if new entries for this entity can be posted through the api.

see allowed (opens new window)

# allowApiCrud

sets the allowApiUpdate, allowApiDelete and allowApiInsert properties in a single set

# apiPrefilter

A filter that determines which rows can be queries using the api.

example

apiPrefilter: { archive:false }

see EntityFilter (opens new window)

# backendPrefilter

A filter that will be used for all queries from this entity both from the API and from within the backend.

example

backendPrefilter: { archive:false }

see EntityFilter (opens new window)

# defaultOrderBy

An order by to be used, in case no order by was specified

example

defaultOrderBy: { name: "asc" }

example

defaultOrderBy: { price: "desc", name: "asc" }

# saving

An event that will be fired before the Entity will be saved to the database. If the error property of the entity's ref or any of its fields will be set, the save will be aborted and an exception will be thrown. this is the place to run logic that we want to run in any case before an entity is saved.

example

@Entity<task>("tasks",{
saving: async task => {
     task.lastUpdated = new Date()
 }
})

Arguments:

  • row
  • proceedWithoutSavingToDb

# saved

will be called after the Entity was saved to the data source.

Arguments:

  • row

# deleting

Will be called before an Entity is deleted.

Arguments:

  • row

# deleted

Will be called after an Entity is deleted

Arguments:

  • row

# validation

Will be called when the entity is being validated, usually prior to the saving event

Arguments:

  • row
  • ref

# dbName

The name of the table in the database that holds the data for this entity. If no name is set, the key will be used instead.

example

dbName:'myProducts'

# sqlExpression

For entities that are based on SQL expressions instead of a physical table or view

# id

An arrow function that identifies the id column to use for this entity

example

//Single column id
@Entity<products>("products",{ id:p=>p.productCode })

example

//Multiple columns id
@Entity<orderdetails>("orderdetails",{ id:od=> [od.orderId, od.productCode] })

Arguments:

  • entity

# entityRefInit

  • entityRefInit

Arguments:

  • ref
  • row

# apiRequireId

  • apiRequireId