BASE

better-sqlite3

better-sqlite3

Database / data tool · Free

A Node.js SQLite binding with a synchronous API and transaction support: it embeds a local SQL database in server or desktop applications.

Also known as: WiseLibs/better-sqlite3; sqlite3-node

Resource illustration: better-sqlite3

Open resource ↗

What you can use for free

Code and local use under MIT.

Paid features and costs

Computing resources, self-hosting, and external services are paid for separately if used.

Access and limitations

Code and local use under MIT. Computing resources, self-hosting, and external services are paid for separately if used.

Use cases

  • Embed SQLite in a Node.js application
  • Connect synchronous queries and a transaction for a related write operation

Example workflow

A possible workflow; this is not a report of hands-on testing.

Embed SQLite in a Node.js application.

Before you start

  • A schema, test dataset, and queries.
  • A supported database or runtime.

Steps

  1. Define tables and prepare SQL queries for the selected scenario. The README demonstrates Database and prepare(...).get(...) with a bound parameter; use query parameters for your own inputs.
  2. Connect synchronous queries and a transaction for a related write operation. db.transaction creates a synchronous function: normal completion commits, and an exception rolls back. Do not put async/await inside it; the API explicitly warns of premature transaction completion.
  3. Check schema changes, indexes, and data recovery. The API describes db.backup as a Promise producing a SQLite backup file; a possible check opens the restored copy and compares reference queries. Successfully creating a file does not replace a recovery test.

Check your result

  • Query results match the reference data.
  • Check access permissions, backup recovery, and load before production use.

Choosing better-sqlite3

Embed SQLite in a Node.js application

Choose this when

  • Embed SQLite in a Node.js application
  • Connect synchronous queries and a transaction for a related write operation

Requirements

  • A Node.js version supported at installation time.
  • Install the package in a supported Node.js environment and check the native binding for your build. Define the SQL schema, prepared statement parameters, read/write patterns, and backup recovery.

Things to consider

  • Costs and limits of free use: Computing resources, self-hosting, and external services are paid for separately if used.
  • Terms of use: MIT for better-sqlite3 code: use, modification, distribution, and sale are permitted with the copyright notice and permission text retained. Code is provided without warranties; dependencies and third-party services have their own terms.
  • The README separately warns about heavy write concurrency and very large databases.

Frequently asked questions

What can you use for free in better-sqlite3?

Code and local use under MIT.

What additional costs might arise with better-sqlite3?

Computing resources, self-hosting, and external services are paid for separately if used.

What terms of use matter for better-sqlite3?

MIT for better-sqlite3 code: use, modification, distribution, and sale are permitted with the copyright notice and permission text retained. Code is provided without warranties; dependencies and third-party services have their own terms.

How do you get started with better-sqlite3?

Define tables and prepare SQL queries for the selected scenario. The README demonstrates Database and prepare(...).get(...) with a bound parameter; use query parameters for your own inputs. Connect synchronous queries and a transaction for a related write operation. db.transaction creates a synchronous function: normal completion commits, and an exception rolls back. Do not put async/await inside it; the API explicitly warns of premature transaction completion. Check schema changes, indexes, and data recovery. The API describes db.backup as a Promise producing a SQLite backup file; a possible check opens the restored copy and compares reference queries. Successfully creating a file does not replace a recovery test.

When might better-sqlite3 be unsuitable?

The README highlights heavy concurrent writes, many reads returning megabytes of data, and databases around a terabyte. For these situations, the author suggests a full server database; check this general guidance against project load.

What should I check when updating better-sqlite3?

Both the library API and bundled SQLite may change. The README recommends reading both projects' release notes; slow queries can be moved to worker threads.

Why is an async function unsuitable for db.transaction?

The documentation says transaction functions do not work with async functions: after the first await, the function returns control and the transaction ends before asynchronous code. Perform required asynchronous operations separately, then synchronous writes in a coordinated transaction.

How can I test rollback and a backup?

The API describes rollback on an exception from the transaction function and db.backup returning a Promise. A possible test deliberately triggers an error in a related change, checks for no partial write, then opens the backup as a SQLite database and compares data. With SQLite errors, account for SQLite possibly having already rolled back the transaction.

Does every SQLite task require a separate server database?

better-sqlite3 opens a SQLite file or :memory: inside Node.js. This can support a local application, test store, or small server scenario; the README separately warns about heavy concurrent writes and large result sets. Test the choice against your load model.

License

MIT for better-sqlite3 code: use, modification, distribution, and sale are permitted with the copyright notice and permission text retained. Code is provided without warranties; dependencies and third-party services have their own terms.

MIT — permissive license

You can use, modify, distribute, and sell the code, including in a closed-source commercial product. When sharing copies or substantial portions of the code, retain the license text and copyright notice. You do not need to open-source your entire project; the author provides no warranties.

License terms

Technologies and topics

  • better-sqlite3
  • database
  • Development

Sources

Other resources