Hey 👋
Transactions are getting a lot of love in this one!
As part an effort to replace Knex with Kysely, B4nan, the author of mikro-orm drove the new setAccessMode('read only'|'read write') method when starting transactions.
You can now commit/rollback transactions manually and there's even savepoint support:
const trx = await db.startTransaction().execute()
try {
// do stuff with `trx`, including work with savepoints via the new `savepoint(name)`, `rollbackToSavepoint(name)` and `releaseSavepoint(name)` methods!
await trx.commit().execute()
} catch (error) {
await trx.rollback().execute()
throw error
}We also added using keyword support, so now you can write:
await using db = new Kysely({...})and db.destroy() will be called automatically once the current scope is exited.
If you plan on trying this out (it is optional, you can still const db = new Kysely({...}) and await db.destroy() manually), the using keyword requires typescript >= 5.2 and the following tsconfig.json options:
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ESNext", ...],
...
}
...
}We also added a plugin to handle in () and not in (). It comes with 2 handling strategies, one similar to how Knex.js, PrismaORM, Laravel and SQLAlchemy do it, and one similar to how TypeORM and Sequelize do it. It also supports custom strategies, e.g. throwing an error to avoid making a call to the database and wasting resources. Here's an example with one of the strategies we ship:
import {
// ...
HandleEmptyInListsPlugin,
// ...
replaceWithNoncontingentExpression,
// ...
} from 'kysely'
const db = new Kysely<Database>({
// ...
plugins: [
new HandleEmptyInListsPlugin({
strategy: replaceWithNoncontingentExpression
})
],
})
// ...
.where('id', 'in', [])
.where('first_name', 'not in', []) // => `where 1 = 0 and 1 = 1`🚀 Features
InferResultshould output plural. by @igalklebanov in #1064- Speedup types with huge databases. Fixes #867 by @koskimas in #1080
- implement missing expression features by @koskimas in #1085
- Remove preventAwait by @wirekang in #1160
- add
ControlledTransaction. by @igalklebanov in #962 & #1193 await using kysely = new Kysely()support. by @igalklebanov in #1167- feat: add HandleEmtpyInListsPlugin. by @austinwoon and @igalklebanov in #925
- Add Date as a valid return type for max and min by @samclearman & @igalklebanov in #1062
- Add support for cross join and cross join lateral by @ersinakinci in #1325
- dry up joins. by @igalklebanov in c95f499
- revisiting orderBy - deprecations, new order by item builder (nullFirst(), nullsLast(), collate()). by @igalklebanov in #1326
- add
queryIdtoCompiledQueryand all transformer methods. by @igalklebanov in #176 - feat: Add disableTransactions option to Migrator by @reidswan in #1335
- Redundant export UpdateValuesNode removed by @Ciantic in #1379
- Fix ctrl transaction mutation issues by @koskimas in #1406
- feat: escape single quotes in string literals. by @igalklebanov in #1392
PostgreSQL 🐘 / MySQL 🐬
PostgreSQL 🐘 / MS SQL Server 🥅
PostgreSQL 🐘 / SQLite 📘
PostgreSQL 🐘
- feat: support refresh materialized view by @QuentinJanuel in #990
- add
returningsupport inMERGEqueries. by @igalklebanov in #1171 - Support json_agg(column_ref) by @SimonSimCity in #1316
- feat: expands limit in select accepting null value by @alenap93 in #1347
- feat: make create type as enum values argument readonly. by @igalklebanov in #1390
- add support for constraint renaming. by @koskimas in #1408
MySQL 🐬
MS SQL Server 🥅
- Add outer and cross apply (mssql) by @drew-marsh in #1074
- refactor: extract
validateConnectionsandresetConnectionsOnReleaseto root of config, flip defaultresetConnectionsOnReleasebehavior. by @igalklebanov in #1388
SQLite 📘
- SQLite's OR CONFLICT clause for inserts by @vincentiusvin & @igalklebanov in #976
🐞 Bugfixes
- fix: no logging in streams. by @igalklebanov in #1382
- fix: ImmediateValueTransformer not handling PrimitiveValueListNodes. by @igalklebanov in #1396
- fix: allow empty array for sql.join by @Sealos in #1395
PostgreSQL 🐘
- fix: postgres auto increment introspection is wrong after column renames. by @igalklebanov in #1391
📖 Documentation
- add reusable helpers recipe by @koskimas in #1085
- fix jsdocs. by @igalklebanov in 1c5e03a
📦 CICD & Tooling
- ci: run 22.x by @igalklebanov in 9736aeb
- chore: enforce min TS version by @igalklebanov in #1194
- fix package-lock. by @igalklebanov in f348dfb
- add TypeScript benchmarks. by @igalklebanov in #1314
- improve join tests dialect coverage. by @igalklebanov in 6eaf754
- minor ci tweaks. by @igalklebanov in ca11632
⚠️ Breaking Changes
InferResultnow outputsInsertResult[],UpdateResult[],DeleteResult[],MergeResult[], instead ofInsertResult,UpdateResult,DeleteResult,MergeResult. To get the singular form, usetype Result = InferResult<T>[number].- Some generic/wide usages of
QueryCreator's methods should no longer pass type checks. We never supported these officially. - As
preventAwaitis now removed on all builders, you must avoid awaiting builders without callingexecute-like methods on your own. - TypeScript versions 4.5 and older are no longer supported. You will get an immediate compilation error telling you to upgrade.
QueryResult.numUpdatedOrDeletedRowshas been removed (after spending ~2 years in deprecation). We still log a warning. Outdated dialects that don't useQueryResult.numAffectedRowsshould be updated OR forked.DefaultQueryExecutor.compileQuerynow requires passing aqueryIdargument. Use the newly exportedcreateQueryId()as that argument value from now on.UpdateValuesNodetype has been removed.MssqlDialectConfig.tedious.resetConnectionOnReleasehas been deprecated, and had it's default flipped tofalse. UseMssqlDialectConfig.resetConnectionsOnReleaseinstead.MssqlDialectConfig.tarn.options.validateConnectionshas been deprecated. UseMssqlDialectConfig.validateConnectionsinstead.- String literals are now
'injection protected, hopefully. Please report any issues.
🐤 New Contributors
- @QuentinJanuel made their first contribution in #990
- @austinwoon made their first contribution in #925
- @samclearman made their first contribution in #1062
- @drew-marsh made their first contribution in #1074
- @SimonSimCity made their first contribution in #1316
- @ersinakinci made their first contribution in #1325
- @reidswan made their first contribution in #1335
- @B4nan made their first contribution in #1342
- @Ciantic made their first contribution in #1379
- @Sealos made their first contribution in #1395
Full Changelog: 0.27.6...0.28.0