Skip to main content
Module

x/cotton/mod.ts>Model

SQL Database Toolkit for Deno
Go to Latest
class Model
Abstract
import { Model } from "https://deno.land/x/cotton@v0.4.9/mod.ts";

Database model

Properties

private
_isSaved: boolean
private
optional
_original: Model
id: number

Methods

private
_clone()

Clone the instance, used for comparing with the original instance

private
_compareWithOriginal(): { isDirty: boolean; changedFields: string[]; }

Compare the current values with the last saved data

private
_getColumns(): ColumnDescription[]

Get all columns information

private
_getValues(columns?: string[]): { [key: string]: any; }

Get model values as a plain JavaScript object

private
_normalize()

Normalize model value

private
_normalizeValue(value: any, type: FieldType): any

Normalize data with the expected type

isDirty(): boolean

Check if this instance's fields are changed

isSaved(): boolean

Check if a this instance is saved to the database

remove(): Promise<void>

Delete model from the database

save(): Promise<this>

Save model to the database

Static Properties

adapter: Adapter
primaryKey: string
tableName: string

Static Methods

private
_bulkSave<T extends Model>(models: T[]): Promise<T[]>

Save multiple records to the database efficiently

private
createModel<T>(data: { [key: string]: any; }, fromDatabase?: boolean): T

Transform single plain JavaScript object to Model class

private
createModels<T>(data: { [key: string]: any; }[], fromDatabase?: boolean): T[]

Transform multiple plain JavaScript objects to Model classes

delete<T extends Model>(this: ExtendedModel<T>, options: FindOptions<T>): Promise<void>

Delete multiple records

deleteOne<T extends Model>(this: ExtendedModel<T>, id: number): Promise<void>

Delete a single record by id

find<T extends Model>(this: ExtendedModel<T>, options?: FindOptions<T>): Promise<T[]>

Search for multiple instance

findOne<T extends Model>(this: ExtendedModel<T>): Promise<T | null>

Get the first record in a table or null null if none can be found

findOne<T extends Model>(this: ExtendedModel<T>, id: number): Promise<T | null>

Get a single record by primary key or null if none can be found

findOne<T extends Model>(this: ExtendedModel<T>, where: Partial<T>): Promise<T | null>

Search for a single instance. Returns the first instance found, or null if none can be found

insert<T extends Model>(this: ExtendedModel<T>, data: Partial<T>): Promise<T>

Create a model instance and save it to the database.

insert<T extends Model>(this: ExtendedModel<T>, data: Partial<T>[]): Promise<T[]>

Create a model instance and save it to the database.

truncate(): Promise<void>

Remove all records from a table.