Skip to content

modules_model

Rafael Corrêa Chaves edited this page Jul 18, 2021 · 1 revision

https://gitlab.com/acaijs/modules/interfaces.git https://www.npmjs.com/package/@acai/model https://www.npmjs.com/package/@acai/model https://www.npmjs.com/package/@acai/model

Models are a easy way to group your data with methods to format it, save and other crud operations.

Usage

Declare model

import Model from "@acai/model";

@Model.Table("user", "connection2")
export class User extends Model {
	@Model.Field()
	public id: string;

	@Model.Field()
	public name: string;

	@Model.Field()
	public email: string;

	@Model.Field()
	public avatar?: string;
}

CRUD operations

// get
const user = await User.find("id");
const user2 = await User.query().where("name", "John").first();

// create/update
const user = new User();
await user.save();

// delete
const user = await User.find("id");
await user.delete();

Extending types

Types parts

  • onCreate When setting a field value into the model
  • onSave From model to database
  • onRetrieve From database to model
  • onSerialize From model to JS object

Clone this wiki locally