Skip to main content

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 ParameterDescription
C extends objectThe class type.
P extends Properties<C>-

Parameters

ParameterTypeDescription
klassNewable<C>The class of the entity.
propertiesNonEmptyObject<P>The properties of the entity.
build?(builder: EntityBuilder<P, C>) => voidAn 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

ParameterTypeDescription
propertiesNonEmptyObject<P>The properties of the entity.
build?(builder: EntityBuilder<P, UnwrapProperties<P>>) => voidAn optional builder function.

Returns

EntityModel<P, UnwrapProperties<P>>

The created entity model.

Defined in

packages/verse-core/src/model/builder.ts:111