Skip to content

austindh/sqlite3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published