Skip to main content
Module

x/denodb_esgi/mod.ts>Model

MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno
Latest
class Model
import { Model } from "https://deno.land/x/denodb_esgi@1.0.15.1/mod.ts";

Model that can be used with a Database.

Methods

Return the instance current value for its primary key.

Delete this record from the database.

await flight.delete();

Create a new record for the model.

const flight = new Flight();
flight.departure = "Toronto";
flight.destination = "Paris";
await flight.save();

Update this record using its current field values.

flight.destination = "London";
await flight.update();

Static Properties

private
_currentQuery: QueryBuilder

Model current query being built.

private
_database: Database

Database which this model will be attached to.

private
_fieldMatching: { toDatabase: FieldMatchingTable; toClient: FieldMatchingTable; }

Model field matching, from database to client and vice versa.

private
_isCreatedInDatabase: boolean

If the model has been created in the database.

private
_options: ModelOptions

Options this model was initialized with.

private
_primaryKey: string

Model primary key. Manually found through _findPrimaryKey().

private
_queryBuilder: QueryBuilder

Query builder instance.

defaults: ModelDefaults

Default values for the model fields.

fields: ModelFields

Model fields.

pivot: ModelPivotModels

Pivot table to use for a given model.

table: string

Table name as it should be saved in the database.

timestamps: boolean

Should this model have created_at and updated_at fields by default.

Static Methods

Look for the current query's where clause for this model's primary key.

private
_findModelForeignKeyField(model: ModelSchema, forModel?: ModelSchema): string

Look for a fieldName: Relationships.belongsTo(forModel) field for a given model.

private
_findPrimaryField(): FieldOptions

Manually find the primary field by going through the schema fields.

private
_findPrimaryKey(): string

Manually find the primary key by going through the schema fields.

private
_formatField(
fieldMatching: FieldMatchingTable,
field: string | { [fieldName: string]: any; },
defaultCase?: (field: string) => string,
): string | { [fieldName: string]: any; }

Format a field or an object of fields, following a field matching table. Defaulting to defaultCase or field otherwise.

private
_runQuery(query: QueryDescription)

Build the current query and run it on the associated database.

all()

Fetch all the model records.

await Flight.all();

await Flight.select("id").all();
avg(field: string)

Compute the average value of a field's values from all the selected records.

await Flight.avg("flightDuration");

await Flight.where("destination", "San Francisco").avg("flightDuration");
count(field?: string)

Count the number of records of a model or filtered by a field name.

await Flight.count();

await Flight.where("destination", "Dublin").count();
create(values: Values | Values[])

Create one or multiple records in the current model.

await Flight.create({ departure: "Paris", destination: "Tokyo" });

await Flight.create([{ ... }, { ... }]);

Create a model in the database. Should not be called from a child model.

Delete selected records.

await Flight.where("destination", "Paris").delete();
deleteById(id: FieldValue)

Delete a record by a primary key value.

await Flight.deleteById("1");

Drop a model in the database.

field(field: string): string

Return the table name followed by a field name. Can also rename a field using nameAs.

Flight.field("departure") => "flights.departure"

Flight.field("id", "flight_id") => { flight_id: "flights.id" }
field(field: string, nameAs: string): FieldAlias
find(idOrIds: FieldValue | FieldValue[])

Find one or multiple records based on the model primary key.

await Flight.find("1");

Return the first record that matches the current query.

await Flight.where("id", ">", "1").first();
formatFieldToClient(field: string | Object)

Format field or an object of fields from database to client.

formatFieldToDatabase(field: string | Object)

Format field or an object of fields from client to database.

get()

Run the current query.

Return the model computed primary key.

Return the field properties of the primary key

getComputedPrimaryType(): FieldTypeString

Return the field type of the primary key.

groupBy<T extends ModelSchema>(this: T, field: string)

Group rows by a given field.

await Flight.groupBy('departure').all();
hasMany<T extends ModelSchema>(this: T, model: ModelSchema): Promise<any[]>

Find associated values for the given model for one-to-many and many-to-many relationships.

class Airport {
  static flights() {
    return this.hasMany(Flight);
  }
}

Airport.where("id", "1").flights();
hasOne<T extends ModelSchema>(this: T, model: ModelSchema)

Find associated values for the given model for one-to-one and one-to-many relationships.

join<T extends ModelSchema>(
this: T,
joinTable: ModelSchema,
originField: string,
targetField: string,
)

Join a table to the current query.

await Flight.where(
  Flight.field("departure"),
  "Paris",
).join(
  Airport,
  Airport.field("id"),
  Flight.field("airportId"),
).get()
leftJoin<T extends ModelSchema>(
this: T,
joinTable: ModelSchema,
originField: string,
targetField: string,
)

Join a table with left statement to the current query.

await Flight.where(
  Flight.field("departure"),
  "Paris",
).leftJoin(
  Airport,
  Airport.field("id"),
  Flight.field("airportId"),
).get()
leftOuterJoin<T extends ModelSchema>(
this: T,
joinTable: ModelSchema,
originField: string,
targetField: string,
)

Join a table with left outer statement to the current query.

await Flight.where(
  Flight.field("departure"),
  "Paris",
).leftOuterJoin(
  Airport,
  Airport.field("id"),
  Flight.field("airportId"),
).get()
limit<T extends ModelSchema>(this: T, limit: number)

Limit the number of results returned from the query.

await Flight.limit(10).get();
max(field: string)

Find the maximum value of a field from all the selected records.

await Flight.max("flightDuration");
min(field: string)

Find the minimum value of a field from all the selected records.

await Flight.min("flightDuration");
offset<T extends ModelSchema>(this: T, offset: number)

Skip n values in the results.

await Flight.offset(10).get();

await Flight.offset(10).limit(2).get();
orderBy<T extends ModelSchema>(
this: T,
fieldOrFields: string | OrderByClauses,
orderDirection?: OrderDirection,
)

Order query results based on a field name and an optional direction.

await Flight.orderBy("departure").all();

await Flight.orderBy("departure", "desc").all();

await Flight.orderBy({ departure: "desc", destination: "asc" }).all();
select<T extends ModelSchema>(this: T, ...fields: (string | FieldAlias)[])

Indicate which fields should be returned/selected from the query.

await Flight.select("id").get();

await Flight.select("id", "destination").get();
skip<T extends ModelSchema>(this: T, offset: number)

Similar to offset, skip n values in the results.

await Flight.skip(10).get();

await Flight.skip(10).take(2).get();
sum(field: string)

Compute the sum of a field's values from all the selected records.

await Flight.sum("flightDuration");
take<T extends ModelSchema>(this: T, limit: number)

Similar to limit, limit the number of results returned from the query.

await Flight.take(10).get();
update(fieldOrFields: string | Values, fieldValue?: FieldValue)

Update one or multiple records. Also update updated_at if timestamps is true.

await Flight.where("departure", "Dublin").update("departure", "Tokyo");

await Flight.where("departure", "Dublin").update({ destination: "Tokyo" });
where<T extends ModelSchema>(
this: T,
fieldOrFields: string | Values,
operatorOrFieldValue?: Operator | FieldValue,
fieldValue?: FieldValue,
)

Add a where clause to your query.

await Flight.where("id", "1").get();

await Flight.where("id", ">", "1").get();

await Flight.where({ id: "1", departure: "Paris" }).get();