Repository navigation
Releases
Releases · abedmurrar/objection.js
v4.4.6
Fixes
fn.now()works on SQLite. It was alwaysCURRENT_TIMESTAMP(6), orCURRENT_TIMESTAMP(precision), which SQLite rejects with a syntax error. On SQLite it's nowCURRENT_TIMESTAMP, like knex'sknex.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()andtransaction.start(),ref()andModel.ref(),val(),fn(),RelationExpression,has()andclear(),mixin()andcompose()with two real plugins, theformat()andparse()ofsnakeCaseMappers(),AjvValidator,ValidationError,NotFoundErrorand 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
Fixes
- With
skipUndefined(), an undefined id madedeleteById()delete every row of the table, andpatchAndFetchById()andupdateAndFetchById()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 withoutskipUndefined(), as it already did without it. Inherited from objection.js (3.1.5 does the same).
v4.4.4
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'swhereclause 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'sunion(), and string expressions go through the same normalization as object ones. Inherited from objection.js.
Docs
- The validation guide explains that
insertGraph()andupsertGraph()validate each model without its relation properties, whileinsert()andpatch()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,WithGraphQueryBuilderandselect().
v4.4.3
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 withER_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 amodifyfilter, join models with staticbeforeDeleteorafterDeletehooks), 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
Fixes
- In
upsertGraph()withrelate: true, a related model that has relations of its own is upserted with them by a separateupsertGraph()call, after the rest of the graph. A#refor#ref{}from the graph of one such model to a model in another one's graph threwcould 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 throwsthe object graph contains cyclic referencesbefore anything is written. - A
#reffrom the rest of the graph to a model inside such a graph threwTypeError: 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 aValidationErrorthat says so, before anything is written. - The
could not resolve referenceerror no longer mentions that limitation, which is gone.
v4.4.1
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,ModelandQueryBuilder) instead ofInstanceType<typeof X>aliases and interfaces that copied parts of them, and the joins share theModifierstype of the operations. Comments that described removed code are fixed. The compiled JavaScript is the same apart from comments.
v4.4.0
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.enginesnow says>=22, the tests run on 22, 24 and 26, and@types/nodestays 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
Fixes
- In a custom query builder method, the methods of the query builder that
withGraphFetched<RM, E>()andwithGraphJoined<RM, E>()return couldn't be called:this.withGraphFetched<MessageModel, 'user'>('user').modifyGraph<UserModel>('user', ...)failed with TS2349 and TS7006.WithGraphQueryBuilderwas a conditional type, which has no members whilethisis generic. It's now always a rebound query builder, with the conditions in its type arguments, like the builders thatfirst()andfindById()return there. The types ofwithGraphFetched()calls without type arguments are unchanged. Reported from chat-api.
v4.3.0
What's new
- Custom query builder methods can load relations without casts. Inside a builder method
thisis generic, so the types can't know which model the builder belongs to, andthis.withGraphFetched('user')didn't compile (TS2345, then TS2349 and TS7006 on a followingmodifyGraph()).withGraphFetched(),withGraphJoined()andallowGraph()now take the model as a type argument, likemodifyGraph<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 aWithGraphQueryBuilder<this, 'user'>, so a method can say which relations it loads, and(await Message.query().withUser())[0].useris typed.WithGraphQueryBuilderis 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 typedthiscan returnthis.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 keepsthis, andstatic get QueryBuilder()for a builder in its own file: withstatic QueryBuilder = MessageQueryBuilder, the builder isundefinedwhen its file, which imports the model, is loaded first. It also says thatselect()doesn't narrow the row type in builder methods.
v4.2.13
Security
- Binding a model class to a knex instance, which
bindKnex(),bindTransaction()and every transaction do, created the bound subclass withnew Functionand put the class'snameinto its code. A model class whosenamecame 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 aSyntaxError. 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()isknex.transaction(),transaction()uses theisModelClass()ofresolveModel,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()andgroupBy()stay: on the small arrays objection passes, they are faster thanArray.prototype.flat()(several times) andMap.groupBy().