1.3.4 • Published 2 years ago

@sqlite3-libs/promised-sqlite3 v1.3.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

promised-sqlite3

A wrapper for sqlite3 node.js package to use Promise.

NPM version

Motivation

sqlite3 is a callback-based SQLite3 binding for Node.js.
Promise is a cool javascript feature.

The goal of promised-sqlite3 is to provide a wrapper arround sqlite3 to provide Promise-friendly methods.

Install

npm install @sqlite3-libs/promised-sqlite3

Usage

const { PromisedDatabase } = require("@sqlite3-libs/promised-sqlite3"); // import the class

const db = new PromisedDatabase(); // create a instance of PromisedDatabase
// note: at this stade, the wrapped sqlite3.Database object is not created.

async function init() {
    try {
        await db.open("./db.sqlite"); // create a sqlite3.Database object & open the database on the passed filepath.

        // run some sql request.
        await db.run("CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY AUTOINCREMENT, a TEXT NOT NULL, b TEXT)"); 
        await db.run("INSERT INTO foo (a, b) VALUES (?, ?)", "alpha", "beta");
        await db.run("INSERT INTO foo (a, b) VALUES ($goo, $hoo)", { $goo: "GOO !", $hoo: "HOO :" });
        await db.run("INSERT INTO foo (a, b) VALUES (?, ?)", ["Value of a", "Value of b"]);

        // read database
        const row = await db.get("SELECT * FROM foo WHERE id = ?", 2);
        console.log(row2);

        const rows = await db.all("SELECT * FROM foo");
        console.log(rows);

        await db.each("SELECT * FROM foo WHERE id > ?", 5,
            function(row) {
                console.log(row);
            }
        );

        // get the wrapped sqlite3.Database object
        const sqliteDB = db.db;

        // close the database
        await db.close();

    } catch(err) {
        console.error(err);
    }
}

init();

Documentation

See in ./docs/ folder.

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago