Skip to content

Releases

v4.4.6

Choose a tag to compare

@github-actions github-actions released this 12 Nov 00:34

Fixes

  • fn.now() works on SQLite. It was always CURRENT_TIMESTAMP(6), or CURRENT_TIMESTAMP(precision), which SQLite rejects with a syntax error. On SQLite it's now CURRENT_TIMESTAMP, like knex's knex.fn.now(): SQLite has no precision, so the time is to the second. Inherited from objection.js.

Docs

  • The objection module API page has examples for transaction() and transaction.start(), ref() and Model.ref(), val(), fn(), RelationExpression, has() and clear(), mixin() and compose() with two real plugins, the format() and parse() of snakeCaseMappers(), AjvValidator, ValidationError, NotFoundError and the database errors, with a table of which error properties each database sets. Every example was run on SQLite, MySQL 8.4, MariaDB 11.4 and postgres.

v4.4.5

Choose a tag to compare

@github-actions github-actions released this 11 Nov 17:14

Fixes

  • With skipUndefined(), an undefined id made deleteById() delete every row of the table, and patchAndFetchById() and updateAndFetchById() update every row: the methods left out their only condition, the id. findById() returned an arbitrary row. An undefined id now throws "undefined was passed to ..." with or without skipUndefined(), as it already did without it. Inherited from objection.js (3.1.5 does the same).

v4.4.4

Choose a tag to compare

@github-actions github-actions released this 11 Nov 16:46

Fixes

  • A modifier or relation listed twice in a relation expression, like withGraphFetched('pets(onlyDogs, onlyDogs)') or '[pets, pets]', is applied once. It was applied twice: the modifier's where clause was repeated, and the relation was loaded with two identical queries, unless the expression was merged with one of 10 or more modifiers or relations. In that case it was applied once: union(), which merges them, kept the duplicates of its first array only when both arrays were shorter than 10 items. It now always returns the unique values of both, like lodash's union(), and string expressions go through the same normalization as object ones. Inherited from objection.js.

Docs

  • The validation guide explains that insertGraph() and upsertGraph() validate each model without its relation properties, while insert() and patch() validate with them. This changes in 5.0.
  • The custom query builder recipe uses a static get QueryBuilder() getter, and links to the TypeScript notes on builders in their own files, WithGraphQueryBuilder and select().

v4.4.3

Choose a tag to compare

@github-actions github-actions released this 11 Nov 05:47

Fixes

  • On MySQL and MariaDB, an upsertGraph() without a transaction could deadlock with itself when it unrelated the many-to-many rows of several parents, for example a tag removed from two posts. It ran one unrelate per parent, 4 at a time, and each locked a range of the join table's key. On MariaDB 10.6, 109 of 300 such calls failed with ER_LOCK_DEADLOCK, with no other query running. The unrelates of a relation are now one query for all parents, which deletes the join rows by their (owner, related) pairs: 0 of 300, and faster, since it's one round trip (one tag unrelated from each of 20 owners, median of 30 calls in three runs: 31.7–35.2 → 5.8–5.9 ms on MySQL 8.4, 11.1–11.8 → 3.5–3.6 ms on MariaDB 10.6). An unrelate of one parent's rows keeps its query. Where one query wouldn't be the same as the queries it replaces (relations with a modify filter, join models with static beforeDelete or afterDelete hooks), the queries of a relation run one at a time instead, like its many-to-many deletes. Found by the new random graph tests.

v4.4.2

Choose a tag to compare

@github-actions github-actions released this 11 Nov 05:32

Fixes

  • In upsertGraph() with relate: true, a related model that has relations of its own is upserted with them by a separate upsertGraph() call, after the rest of the graph. A #ref or #ref{} from the graph of one such model to a model in another one's graph threw could not resolve reference, because the calls ran at the same time and couldn't see each other's models. The calls now run in the order of their references, and the reference gets the written model, with its id. Calls without references between them still run at the same time. A cycle of references throws the object graph contains cyclic references before anything is written.
  • A #ref from the rest of the graph to a model inside such a graph threw TypeError: Cannot read properties of null (reading 'obj'), after the deletes, inserts and patches before it had run. It can't work, since the rest of the graph is written first, so it now throws a ValidationError that says so, before anything is written.
  • The could not resolve reference error no longer mentions that limitation, which is gone.

v4.4.1

Choose a tag to compare

@github-actions github-actions released this 11 Nov 03:18

Internal

  • Removed the type stand-ins left from the move to TypeScript. The operations, the joins and the relation properties use the classes' own types (GraphUpsert, RelationJoiner, RelationExpression, ReferenceBuilder, Model and QueryBuilder) instead of InstanceType<typeof X> aliases and interfaces that copied parts of them, and the joins share the Modifiers type of the operations. Comments that described removed code are fixed. The compiled JavaScript is the same apart from comments.

v4.4.0

Choose a tag to compare

@github-actions github-actions released this 11 Nov 03:06

What's new

  • Node.js 22 is supported again. Nothing in the library needed Node.js 24: the whole test suite passes on Node.js 22, and only one test used Promise.try(), which Node.js 22 doesn't have. engines now says >=22, the tests run on 22, 24 and 26, and @types/node stays at 22 so the compiler rejects newer APIs. Node.js 22 is maintained until April 2027; see Node.js 22 or newer is required.

v4.3.1

Choose a tag to compare

@github-actions github-actions released this 10 Nov 01:49

Fixes

  • In a custom query builder method, the methods of the query builder that withGraphFetched<RM, E>() and withGraphJoined<RM, E>() return couldn't be called: this.withGraphFetched<MessageModel, 'user'>('user').modifyGraph<UserModel>('user', ...) failed with TS2349 and TS7006. WithGraphQueryBuilder was a conditional type, which has no members while this is generic. It's now always a rebound query builder, with the conditions in its type arguments, like the builders that first() and findById() return there. The types of withGraphFetched() calls without type arguments are unchanged. Reported from chat-api.

v4.3.0

Choose a tag to compare

@github-actions github-actions released this 10 Nov 01:25

What's new

  • Custom query builder methods can load relations without casts. Inside a builder method this is generic, so the types can't know which model the builder belongs to, and this.withGraphFetched('user') didn't compile (TS2345, then TS2349 and TS7006 on a following modifyGraph()). withGraphFetched(), withGraphJoined() and allowGraph() now take the model as a type argument, like modifyGraph<RM>(): this.withGraphFetched<MessageModel>('user') returns the query builder as it was. Given the expression as a second type argument, this.withGraphFetched<MessageModel, 'user'>('user') checks the expression against the model and returns a WithGraphQueryBuilder<this, 'user'>, so a method can say which relations it loads, and (await Message.query().withUser())[0].user is typed. WithGraphQueryBuilder is now exported explicitly and documented. Calls without type arguments are checked as before, with the same error messages. Reported from chat-api.
  • A runAfter() callback that returns its input, or a promise of it, keeps the type of the query builder, so a builder method typed this can return this.runAfter(...). Callbacks that return something else change the result type as before.

Docs

  • The TypeScript guide shows builder methods that load relations, a runAfter() that keeps this, and static get QueryBuilder() for a builder in its own file: with static QueryBuilder = MessageQueryBuilder, the builder is undefined when its file, which imports the model, is loaded first. It also says that select() doesn't narrow the row type in builder methods.

v4.2.13

Choose a tag to compare

@github-actions github-actions released this 10 Nov 01:10

Security

  • Binding a model class to a knex instance, which bindKnex(), bindTransaction() and every transaction do, created the bound subclass with new Function and put the class's name into its code. A model class whose name came from data, for example one class per tenant named after it, could run code from that name, and a name that isn't an identifier, like 'Weird Model', threw a SyntaxError. The subclass is now named through a computed property, without any code built from the name. This came from objection.js.

Performance

  • Binding a model class to a knex instance takes 13.9% fewer instructions (mock benchmark of bindKnex() with a validated insert): it no longer compiles a new function for each bound class.

Internal

  • Removed duplicated code from the model and the utilities: transaction.start() is knex.transaction(), transaction() uses the isModelClass() of resolveModel, bindKnex() uses its cache directly, jsonEquals() lost a comparison parameter that was always strict equality, and the walk for schema defaults is one function. objectUtils.flatten() and groupBy() stay: on the small arrays objection passes, they are faster than Array.prototype.flat() (several times) and Map.groupBy().