Given a base object representing a Model, hydrates its D1, R2 and KV properties. Fetches all KV and R2 data concurrently.
The hydrated model instance
Lists all instances of a given Model from D1. A model without a primary key cannot be listed, and this method will return an empty array in that case.
Array of listed model instances
Given a new Model object, performs an upsert operation for D1 and KV.
Concurrently performs all D1 and KV operations.
Some KV results depend on a successful D1 upsert to resolve their keys, and will be uploaded only after the D1 upsert completes.
If a Model is missing a primary key, and that primary key is of Integer type, it will be auto-incremented by D1. Else, upsert will fail if the primary key is missing.
Constructor of the model to upsert
The new model object to upsert
Include tree specifying which navigation properties to include
The upserted model instance, or null if upsert failed
StaticdefaultGiven a model, retrieves the default data source for the model, which includes all KV, R2, 1:1, 1:M and M:M relationships.
Does not include nested relationships of 1:M and M:M relationships to avoid excessively large data retrievals.
Constructor of the model to retrieve the default data source for
The default data source for the model.
StaticfromCreates an instance of an Orm
The Wrangler environment containing Cloudflare bindings.
StaticmapMaps D1 results into model instances. Capable of mapping a flat result set
(ie, SELECT * FROM Model) or a joined result granted it is aliased as select_model would produce.
Does not hydrate into an instance of the model; for that, use hydrate after mapping.
Array of mapped model instances
StaticselectGenerates a SELECT query string for a given Model, retrieving the model and its relations aliased as JSON.
The generated SELECT query string
Orm.select(Boss, Boss.withAll);
// Example result:
const result = `
SELECT
"Boss"."id" AS "id",
"Person_1"."id" AS "persons.id",
"Person_1"."bossId" AS "persons.bossId",
"Dog_2"."id" AS "persons.dogs.id",
"Dog_2"."personId" AS "persons.dogs.personId",
"Cat_3"."id" AS "persons.cats.id",
"Cat_3"."personId" AS "persons.cats.personId"
FROM "Boss"
LEFT JOIN "Person" AS "Person_1"
ON "Boss"."id" = "Person_1"."bossId"
LEFT JOIN "Dog" AS "Dog_2"
ON "Person_1"."id" = "Dog_2"."personId"
LEFT JOIN "Cat" AS "Cat_3"
ON "Person_1"."id" = "Cat_3"."personId"
`;
Fetches a model by its primary key ID or key parameters.
primaryKeymust provide each key column.