npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@surfy/sqlite

v1.1.3

Published

Asynchronous Library for sqlite3

Downloads

3

Readme

SQLite

Asynchronous Library for sqlite3

Installation

npm install @surfy/sqlite

Usage

ES6


// Import library
import SQLite from "@surfy/sqlite";

const db = new SQLite('PATH_TO_DB_FILE');
// DB File will be created automatically if not exists
// e.g. /var/data/my_awesome_db.db

CommonJS


// Import library
const SQLite = require('@surfy/sqlite');
const db = await SQLite('PATH_TO_DB_FILE');
// DB File will be created automatically if not exists
// e.g. /var/data/my_awesome_db.db

Methods

.run(query)


await db.run("CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, data TEXT);");

await db.run("INSERT OR IGNORE INTO test_table VALUES(NULL, 'Test Record', 'Data content');");

.table(table_name)

Returns Table instance for query processing


let table = await db.table('test_table');

.find(match, options)

Finds matches in a table


let match = {
	id: 1,
	name: {
		$in: ['Test 1', 'Test 2']
	}
};
// ...WHERE id=1 AND name IN ('Test 1', 'Test 2')

let options = {
	fields: ['id', 'name'],
	limit: 2,
	skip: 3
};

/*

Return only id and name fields
Skip first 3 rows
Limit results to 2

*/

let result = await table.find(match, options);

.findOne(match, options)

Finds one row in a table


let match = {
	id: 1
};

let options = {
	fields: ['id', 'name'],
	skip: 3
};

let result = await table.findOne(match, options);

Operators


let match = {
	id: 1,
	name: 'Test 1'
};

// ...WHERE id=1 AND name='Test 1'


match = {
	id: 1,
	name: {
		$in: ['Test 1', 'Test 2']
	}
};

// ...WHERE id=1 AND name IN ('Test 1', 'Test 2')


match = {
	id: 1,
	name: {
		$like: 'Test%'
	}
};

// ...WHERE id=1 AND name LIKE 'Test%'


match = {
	id: 1,
	$or: [
		{
			name: 'Name'
		},
		{
			extra: 'Extra'
		}
	]
};

// ...WHERE id=1 AND ( name='Name' OR extra='Extra' )

.insert(values)

Inserts data into the table


// Single value
let newRow = {
	name: 'Test name',
	data: {
		extraOption: 'Test Data'
	}
};

let insertedIDs = await table.insert(newRow);
// insertedIDs - Array [(int) Inserted_ID]

// Multiple values
let newRows = [
	{
		name: 'Test name 1',
		data: {
			extraOption: 'Test Data 1'
		}
	},
	{
		name: 'Test name 2',
		data: {
			extraOption: 'Test Data 2'
		}
	},
	{
		name: 'Test name 3',
		data: [1, 2, 3]
	}
];

let IDs = await table.insert(newRows);
// Return IDs - Array [(int) Inserted_ID, (int) Inserted_ID, ...]

.insertOne(values)

Inserts a single row into the table


// Single value
let newRow = {
	name: 'Test name'
};

let insertedID = await table.insertOne(newRow);
// insertedID - (int) Inserted_ID

.update(match, update)


let match = {
	id: 1
};

let update = {
	data: 'Updated Data',
	extra: new Date()
};

await test_table.update(match, update);

.each(match, options, callback)

Runs the SQL query with parameters and calls the callback once for each row


let table = await db.table('test_table');

let match = {
	name: 'Test name'
};

let options = {
	fields: ['id', 'name'],
	limit: 2,
	skip: 3
};

table.each(match, options, (err, row) => {
	console.log('Row', row);
});

Date

await db.run("CREATE TABLE IF NOT EXISTS time_table (id INTEGER PRIMARY KEY AUTOINCREMENT, time TEXT);");

let time_table = await db.table('time_table');

let insertedIDs = await time_table.insert([
	{
		id: 1,
		time: 'CURRENT_TIME'
	},
	{
		id: 2,
		time: new Date()
	}
]);

let rows = await time_table.find();

/*

Result
rows = [
	{
		id: 1,
		time: JS Date Object
	},
	{
		id: 2,
		time: JS Date Object
	}
]

*/

Global method

.get(query)

Returns rows or false if error occured


let rows = await db.get("SELECT * FROM test_table WHERE id=1");

.all(query)

Returns rows or false if error occured


let rows = await db.all("SELECT * FROM test_table");

.truncate(table_name)

Truncates table and reset Auto Increment


let result = await db.truncate('test_table');

// @Result True or False

.remove(DB_name)

Removes loaded DB file


let result = await db.removeDB();

/*

@Result
	False if DB file does not exist
	True If successful
*/

.getSQLite3()

Returns original SQLite3 object


const sqlite3 = db.getSQLite3();

MIT License

Alexander Yermolenko • surfy.one

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.