Model

A Model is a class returned by sedentary.model(). Instances represent table rows (entries).

Model attributes

Each attribute defined in AttributesDefinition becomes a property on the model class (e.g. Foo.id, Foo.name) and on entry instances. Use the class property when defining Foreign keys (e.g. db.FKey(Foo.id)).

const db = new SedentaryPG(config);
const Foo = db.model("Foo", {
    name: db.VarChar({ size: 255 })
});
const Bar = db.model("Bar", {
    foo: db.FKey(Foo.id)  // Foo.id is the target attribute
});

Model.load(where[, order][, limit][, tx][, lock])

  • where: Where condition - required - The query condition.

  • order?: string | Array - optional - Attribute name(s) for ORDER BY. Prefix with - for DESC.

  • limit?: number - optional - Maximum number of rows to return.

  • tx?: Transaction - optional - Run within a transaction.

  • lock?: boolean - optional - Acquire row lock (e.g. SELECT ... FOR UPDATE). Requires tx.

  • returns a Promise of entry instances.

Loads entries from the table.

Model.cancel(where[, tx])

  • where: Where condition - required - The condition for rows to delete.

  • tx?: Transaction - optional - Run within a transaction.

  • returns a Promise of the number of deleted rows.

Deletes rows matching the condition.

Where condition

  • Object: { attr: value } for equality, or { attr: [operator, value] } for =, >, <, >=, <=, <>, IN, IS NULL, LIKE, NOT.

  • String: Raw SQL condition (e.g. "id > 0").

  • Array: Logical combinations: ["AND", cond1, cond2], ["OR", cond1, cond2], ["NOT", cond].

Entry.save()

  • returns a Promise of number | false (number of affected rows, or false for inserts).

Saves the entry (insert or update).

Entry.remove()

  • returns a Promise of the number of deleted rows.

Removes the entry from the database. Fails if the entry was never saved.

Transaction

Returned by sedentary.begin(). Use it as the tx argument to Model.load, Model.cancel, and when constructing new entries (see Entries initialization). Call tx.commit() or tx.rollback() when done.