Caution
This feature is deprecated. Do not use it. Use hatchedKoa.orm.models
instead.
hatchedKoa.model
is a collection of methods to create, retrieve, update and delete records using the underlying ORM. These methods
are grouped by Schema name.
For example, the following shows using Todo.findAll
to retrieve todo records as JavaScript objects:
import { hatchifyKoa } from "@hatchifyjs/koa";
const schemas = {
Todo: { ... },
User: { ... },
} satisfies Record<string, PartialSchema>
const hatchedKoa = hatchifyKoa(schemas, {
prefix: "/api",
database: { uri: "sqlite://localhost/:memory" },
})
const deserializedTodos = await hatchedKoa.model.Todo.findAll({
where: { id: "b559e3d9-bad7-4b3d-8b75-e406dfec4673" },
})
// deserializedTodos = [
// { id: "b559e3d9-bad7-4b3d-8b75-e406dfec4673", name: "Baking" }
// ]
Each model has the following methods:
A method that retrieves Sequelize objects from the underlying ORM and database.
hatchedKoa.model[schemaName].findAll(options : FindOptions) => Promise<[Model]>
const deserializedTodos = await hatchedKoa.model.Todo.findAll({
where: { id: "b559e3d9-bad7-4b3d-8b75-e406dfec4673" },
})
// deserializedTodos = [
// { id: "b559e3d9-bad7-4b3d-8b75-e406dfec4673", name: "Baking" }
// ]
Parameters
Property | Type | Default | Details |
---|---|---|---|
options | FindOptions | {} |
Specify what records to load. |
Returns
Rejects
Since this is exposing the actual Sequelize function, it can throw any Sequelize error.
Finds all the rows matching your query, within a specified offset / limit, and get the total number of rows matching your query. This is very useful for pagination.
hatchedKoa.model[schemaName].findAndCountAll(options : FindOptions) => Promise<{ count: number, rows: Model[] }>
const deserializedTodos = await hatchedKoa.model.Todo.findAll({
where: { id: "b559e3d9-bad7-4b3d-8b75-e406dfec4673" },
limit: 1,
offset: 0,
})
// deserializedTodos = {
// count: 10,
// rows: [
// { id: "b559e3d9-bad7-4b3d-8b75-e406dfec4673", name: "Baking" }
// ]
// }
Parameters
Property | Type | Default | Details |
---|---|---|---|
options | FindOptions | {} |
Specify what records to load. |
Returns
Promise<{ count: number, rows: Array<Model>}> | null>
Rejects
Since this is exposing the actual Sequelize function, it can throw any Sequelize error.
searches for a single instance. Returns the first instance found, or null if none can be found.
hatchedKoa.model[schemaName].findAll(options: FindOptions) => Promise<Model | null>
const deserializedTodo = await hatchedKoa.model.Todo.findOne({
where: { id: "b559e3d9-bad7-4b3d-8b75-e406dfec4673" },
})
// deserializedTodo = { id: "b559e3d9-bad7-4b3d-8b75-e406dfec4673", name: "Baking" }
Parameters
Property | Type | Default | Details |
---|---|---|---|
options | FindOptions | {} |
Specify what records to load. |
Returns
Rejects
Since this is exposing the actual Sequelize function, it can throw any Sequelize error.
creates a new instance.
hatchedKoa.model[schemaName].create(body: object, options: CreateOptions) => Promise<Model>
const deserializedTodo = await hatchedKoa.model.Todo.create({ name: "Baking" })
// deserializedTodo = { name: "Baking" }
Parameters
Property | Type | Default | Details |
---|---|---|---|
body | object | N/A | The data for the new instance. |
options | CreateOptions | {} |
Options for the creation. |
Returns
Promise<Model>
Rejects
Since this is exposing the actual Sequelize function, it can throw any Sequelize error.
updates one or more instances.
hatchedKoa.model[schemaName].update(body: object, options: UpdateOptions) => Promise<[number, Model[]]>
const [updatedCount, updatedTodos] = await hatchedKoa.model.Todo.update({ name: "Serving" }, { where: { id: "b559e3d9-bad7-4b3d-8b75-e406dfec4673" } })
// updatedCount = 1
// updatedTodos = [{ name: "Serving" }]
Parameters
Property | Type | Default | Details |
---|---|---|---|
values | object | N/A | The values to update. |
options | UpdateOptions | {} |
Options for the update. |
Returns
The promise returns an array with one or two elements. The first element is always the number of affected rows, while the second element is the actual affected rows (only supported in postgres with options.returning true).
Rejects
Since this is exposing the actual Sequelize function, it can throw any Sequelize error.
Deletes one or more instances.
hatchedKoa.model[schemaName].destroy(options: DestroyOptions, id?: Identifier) => Promise<number>
const deletedCount = await hatchedKoa.model.Todo.destroy({
where: { id: "b559e3d9-bad7-4b3d-8b75-e406dfec4673" },
})
// deletedCount = 1
Parameters
Property | Type | Default | Details |
---|---|---|---|
options | DestroyOptions | {} |
Options for the destroy. |
Returns
The promise returns the number of destroyed rows.
Rejects
Since this is exposing the actual Sequelize function, it can throw any Sequelize error.