@adh/sqlite3
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

sqlite3

This is a basic (incomplete) wrapper around the sqlite3 library. I found myself copying and pasting the same boilerplate code into a few different projects, so I created this package to consolidate that into one place.

Installation

$ npm install @adh/sqlite3

Example Usage

import { Sqlite } from '@adh/sqlite3';

const dbPath = 'path/to/test.db';
const db = new Sqlite(dbPath);

const main = async () => {
	// Get rows
	const rows = await db.selectAll('select * from table');

	// Get single row
	const row = await db.selectOne('select * from table where id = ?', 1);

	// Insert a row
	const newId = await db.run('insert into table(key, value) values(?, ?)', 'key', 'value');

	// Delete a row
	await db.run('delete from from table where id = ?', 1);
};
main();

Running SQL from a file

import { Sqlite } from '@adh/sqlite';
const dbPath = 'path/to/test.db';
const db = new Sqlite(dbPath);

const main = async () => {
	const sqlFile = 'path/to/queries.sql';
	await db.executeFile(sqlFile);
};
main();

Notes

  • A connection to the database is opened when a method is called (method calls after the first one will use a cached database connection)
  • After the connection is opened, PRAGMA foreign_keys = ON is executed against the database to enable foreign key constraints

Readme

Keywords

none

Package Sidebar

Install

npm i @adh/sqlite3

Weekly Downloads

2

Version

1.1.2

License

MIT

Unpacked Size

8.91 kB

Total Files

6

Last publish

Collaborators

  • austindh