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
description:
Determines if one has any access to the data of an entity.
see:
- allowed
- to restrict data based on a criteria, use apiPrefilter
allowApiUpdate
Determines if this entity can be updated through the api.
see:
allowApiDelete
Determines if entries for this entity can be deleted through the api.
see:
allowApiInsert
Determines if new entries for this entity can be posted through the api.
see:
allowApiCrud
sets the allowApiUpdate
, allowApiDelete
and allowApiInsert
properties in a single set
apiPrefilter
An optional filter that determines which rows can be queried using the API. This filter is applied to all CRUD operations to ensure that only authorized data is accessible.
Use apiPrefilter
to restrict data based on user profile or other conditions.
example:
// Only include non-archived items in API responses
apiPrefilter: { archive: false }
example:
// Allow admins to access all rows, but restrict non-admins to non-archived items
apiPrefilter: () => remult.isAllowed("admin") ? {} : { archive: false }
see:
apiPreprocessFilter
An optional function that allows for preprocessing or modifying the EntityFilter for a specific entity type before it is used in API CRUD operations. This function can be used to enforce additional access control rules or adjust the filter based on the current context or specific request.
example:
@Entity<Task>("tasks", {
apiPreprocessFilter: async (filter, { getPreciseValues }) => {
// Ensure that users can only query tasks for specific customers
const preciseValues = await getPreciseValues();
if (!preciseValues.customerId) {
throw new ForbiddenError("You must specify a valid customerId filter");
}
return filter;
}
})
backendPreprocessFilter
Similar to apiPreprocessFilter, but for backend operations.
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:
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, e) => {
if (e.isNew) {
task.createdAt = new Date(); // Set the creation date for new tasks.
}
task.lastUpdated = new Date(); // Update the last updated date.
},
})
link:
LifeCycleEvent object
see:
saved
A hook that runs after an entity has been successfully saved.
link:
LifeCycleEvent object
see:
deleting
A hook that runs before an entity is deleted.
link:
LifeCycleEvent object
see:
deleted
A hook that runs after an entity has been successfully deleted.
link:
LifeCycleEvent object
see:
validation
A hook that runs to perform validation checks on an entity before saving. This hook is also executed on the frontend.
link:
LifeCycleEvent object
see:
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'
You can also add your schema name to the table name
example:
dbName:'public."myProducts"'
sqlExpression
For entities that are based on SQL expressions instead of a physical table or view
example:
.@Entity('people',{
sqlExpression:`select id,name from employees
union all select id,name from contractors`,
})
export class Person{
.@Fields.string()
id=''
.@Fields.string()
name=''
}
id
An arrow function that identifies the id
column to use for this entity
example:
//Single column id
@Entity<Products>("products", { id: 'productCode' })
example:
//Multiple columns id
@Entity<OrderDetails>("orderDetails", { id:['orderId:', 'productCode'] })
entityRefInit
Arguments:
- ref
- row
apiRequireId
- apiRequireId