Function: entity()
entity(klass, properties, build)
entity<
C,P>(klass:Newable<C>,properties:NonEmptyObject<P>,build?: (builder:EntityBuilder<P,C>) =>void):EntityModel<P,FromClass<C>>
Builds an EntityModel based on a Class.
Example
import { EntityType } from "@operativa/verse";
import { entity, int, string } from "@operativa/verse/model/builder";
class Artist {
constructor(
readonly id: number,
public name: string
) {}
}
class Album {
constructor(
readonly id: number,
public name: string,
public artistId: number
) {}
}
const classModel = {
albums: entity(
Album,
{
id: int(),
name: string({ maxLength: 100 }),
artistId: int({ nullable: false, column: "ArtistId" }),
},
builder => {
builder.table("Albums");
builder.key("id");
builder.references(Artist, "artistId");
}
),
};
Type Parameters
| Type Parameter | Description |
|---|---|
C extends object | The class type. |
P extends Properties<C> | - |
Parameters
| Parameter | Type | Description |
|---|---|---|
klass | Newable<C> | The class of the entity. |
properties | NonEmptyObject<P> | The properties of the entity. |
build? | (builder: EntityBuilder<P, C>) => void | An optional builder function. |
Returns
EntityModel<P, FromClass<C>>
The created entity model.
Defined in
packages/verse-core/src/model/builder.ts:95
entity(properties, build)
entity<
O,P>(properties:NonEmptyObject<P>,build?: (builder:EntityBuilder<P,UnwrapProperties<P>>) =>void):EntityModel<P,UnwrapProperties<P>>
Builds an EntityModel based on an Object. The type of the entity can be inferred using the EntityType utility type.
Example
const objectModel = {
albums: entity(
{
id: int(),
name: string({ maxLength: 100 }),
artistId: int({ nullable: false, column: "ArtistId" }),
},
builder => {
builder.table("Albums");
builder.key("id");
builder.references(Artist, "artistId");
}
),
};
type AlbumType = EntityType<typeof objectModel.albums>;
Type Parameters
| Type Parameter |
|---|
O extends object |
P extends Properties<O> |
Parameters
| Parameter | Type | Description |
|---|---|---|
properties | NonEmptyObject<P> | The properties of the entity. |
build? | (builder: EntityBuilder<P, UnwrapProperties<P>>) => void | An optional builder function. |
Returns
EntityModel<P, UnwrapProperties<P>>
The created entity model.