import { Model } from "https://dotland.deno.dev/x/denodb@v1.4.0/lib/model.ts";
Model that can be used with a Database
.
Methods
Static Properties
Static Methods
Look for the current query's where clause for this model's primary key.
Look for a fieldName: Relationships.belongsTo(forModel)
field for a given model
.
Manually find the primary field by going through the schema fields.
Format a field or an object of fields, following a field matching table.
Defaulting to defaultCase
or field
otherwise.
Run event listeners given a query type and results.
Build the current query and run it on the associated database.
Alias for Model.on
, add an event listener for a specific operation/hook.
Flight.addEventListener('created', (model) => console.log('New model:', model));
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 the number of records of a model or filtered by a field name.
await Flight.count();
await Flight.where("destination", "Dublin").count();
Create one or multiple records in the current model.
await Flight.create({ departure: "Paris", destination: "Tokyo" });
await Flight.create([{ ... }, { ... }]);
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" }
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();
Format field or an object of fields from database to client.
Format field or an object of fields from client to database.
Group rows by a given field.
await Flight.groupBy('departure').all();
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();
Find associated values for the given model for one-to-one and one-to-many relationships.
Join a table to the current query.
await Flight.where(
Flight.field("departure"),
"Paris",
).join(
Airport,
Airport.field("id"),
Flight.field("airportId"),
).get()
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()
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 the number of results returned from the query.
await Flight.limit(10).get();
Find the maximum value of a field from all the selected records.
await Flight.max("flightDuration");
Find the minimum value of a field from all the selected records.
await Flight.min("flightDuration");
Skip n values in the results.
await Flight.offset(10).get();
await Flight.offset(10).limit(2).get();
Add an event listener for a specific operation/hook.
Flight.on('created', (model) => console.log('New model:', model));
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();
Indicate which fields should be returned/selected from the query.
await Flight.select("id").get();
await Flight.select("id", "destination").get();
Similar to offset
, skip n values in the results.
await Flight.skip(10).get();
await Flight.skip(10).take(2).get();
Compute the sum of a field's values from all the selected records.
await Flight.sum("flightDuration");
Similar to limit
, limit the number of results returned from the query.
await Flight.take(10).get();
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" });
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();