better-sqlite3
semver
>=7.0.0 <13.0.0postconditions11functions8last verified2026-04-16coverage score89%Postconditions — what we check
- Statement.run · run-constraint-errorerrorWhenStatement.run() is called without a try/catch block and the SQL operation may violate a unique constraint, foreign key, or other database constraint.Throws
SqliteErrorRequired handlingWrap Statement.run() in a try/catch block. Check err.code for SQLITE_CONSTRAINT to handle constraint violations gracefully.costhighin prodimmediate exceptionusers seelost datavisibilityvisibleSources[1] - Statement.get · get-execution-errorwarningWhenStatement.get() is called without a try/catch block in a context where database errors (locked, constraint) could occur.Throws
SqliteErrorRequired handlingWrap Statement.get() in a try/catch block. Handle SqliteError to prevent unhandled crashes on database errors.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - Statement.all · all-execution-errorwarningWhenStatement.all() is called without a try/catch block in a context where database errors could occur.Throws
SqliteErrorRequired handlingWrap Statement.all() in a try/catch block. Handle SqliteError to prevent unhandled crashes on database errors.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - Database.exec · exec-no-rollback-errorerrorWhenDatabase.exec() is called without a try/catch block. If an error occurs mid-execution, prior statements are NOT rolled back.Throws
SqliteErrorRequired handlingWrap Database.exec() in a try/catch block. Be aware that partial execution cannot be rolled back — use transactions for atomicity.costhighin prodimmediate exceptionusers seelost datavisibilityvisibleSources[1] - Database.backup · backup-unhandled-rejectionerrorWhenDatabase.backup() is called without .catch() or await inside try/catch. If the backup fails (directory missing, disk full, database error), the returned Promise is rejected with an Error.Throws
ErrorRequired handlingAwait db.backup() inside a try/catch block or chain .catch() on the returned Promise. Log or handle the error to prevent silent backup failures in production.costhighin proddelayed failureusers seelost datavisibilitysilentSources[1] - Database.backup · backup-directory-missingwarningWhenDatabase.backup() is called with a destination path whose parent directory does not exist. The backup() method checks directory existence before starting; throws TypeError synchronously then the promise rejects.Throws
TypeErrorRequired handlingEnsure the destination directory exists before calling backup(), or wrap in try/catch to handle the TypeError from missing directory. Use fs.mkdirSync(path.dirname(dest), { recursive: true }) before backup.costmediumin proddelayed failureusers seelost datavisibilitysilentSources[1] - Database · database-file-must-exist-errorerrorWhennew Database(path, { fileMustExist: true }) is called but the database file at the given path does not exist. The constructor throws a SqliteError immediately — no database is created, no fallback occurs.Throws
SqliteErrorRequired handlingWrap new Database() in a try/catch when using fileMustExist: true. Catch SqliteError and handle the missing-file case (e.g., log, alert, or gracefully degrade rather than crashing the application).costhighin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - Database · database-directory-missing-errorerrorWhennew Database(path) is called where the parent directory of the path does not exist. The constructor throws a TypeError synchronously: "Cannot open database because the directory does not exist."Throws
TypeErrorRequired handlingEnsure the parent directory exists before calling new Database(path). Use fs.mkdirSync(path.dirname(dbPath), { recursive: true }) first, or wrap in try/catch to handle the TypeError.costhighin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - Database.transaction · transaction-async-function-errorerrorWhenAn async function is passed to db.transaction(). The transaction wrapper detects a returned Promise and throws TypeError('Transaction function cannot return a promise'). The transaction is rolled back. This is a common mistake when migrating code from async/await patterns to better-sqlite3.Throws
TypeErrorRequired handlingOnly pass synchronous functions to db.transaction(). All database operations inside the transaction function must be synchronous better-sqlite3 calls. Do not use async/await, Promises, or any async I/O inside the transaction callback.costhighin prodimmediate exceptionusers seelost datavisibilityvisibleSources[1] - Database.transaction · transaction-inner-exception-rollbackerrorWhenA function wrapped by db.transaction() throws an exception during execution (e.g., a SqliteError from a constraint violation or a JavaScript error). The transaction is automatically rolled back. If the calling code does not catch the exception, it propagates up and may crash the application.Throws
SqliteErrorRequired handlingWrap the invocation of a transaction function in a try/catch block. Even though better-sqlite3 auto-rolls back the transaction, the exception still propagates and must be caught to prevent application crashes. Handle constraint violations (SQLITE_CONSTRAINT) to implement retry or conflict resolution logic.costhighin prodimmediate exceptionusers seelost datavisibilityvisibleSources[1] - Statement.iterate · iterate-deferred-execution-errorwarningWhenStatement.iterate() is called and the result is consumed in a for...of loop without a try/catch wrapping the iteration. If the database is locked or the statement fails mid-iteration, an Error is thrown inside the loop and the iterator is closed. The error does NOT appear at the iterate() call site.Throws
SqliteErrorRequired handlingWrap the for...of loop consuming the iterator in a try/catch block, NOT just the iterate() call site. The error surface is deferred to iteration time, so catching at the call site is insufficient. Example: try { for (const row of stmt.iterate()) { ... } } catch (err) { ... }costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1]
Sources
Every postcondition cites at least one of these. Numbered to match the footnotes above.
Need a different package?
Request a profile