Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
sqlite-js-wrapper
Advanced tools
Tiny SQLite helper library (query builder) built on top of HTML5/Web SQL (DRAFT) API
Tiny SQLite helper library built on top of HTML5/Web SQL (DRAFT) API.
This library helps you to build sqlite query in an eloquent way.
Browser | Mobile | NodeJS |
---|---|---|
Any js library supports ES6 module (React, VueJS...) | React Native, Cordova (JS based others) | Try and inform me please :) |
I need to use sqlite in a project and i did some research. I found some libraries, for example websqlite and TypeORM.
TypeORM has model support and more complex than i need (at least for my project). Also force you to use typescript. Websqlite also is a good library but i want to query sqlite database and get result format in a different way (Promises and result as object). At the same time i want the library to be handy and easy to read. That's why i decided to implement one.
This is a fast imlementation took one workday. So if you commit bugs or issues i will be grateful
Hope you enjoy it!
Using npm:
npm install --save sqlite-js-wrapper
Using yarn
yarn add sqlite-js-wrapper
It supports various sqlite syntax. Here is the feature list
Feature | Description |
---|---|
query | Executes single raw query |
queryMulti | Executes multiple raw query |
sqlBatch | Executes multiple raw query using plugin-specific API calls support |
insert | Insert single object or array of objects to the table given |
createTable | Creates table using column argument and returns true or throws error |
dropTable | Drops table if exists given as first argument and returns true or throws error |
isTableExists | Check if a table exists or not |
table | The magic! Takes table name as an argument then let you to chain with other functions such as (select, delete, update, where, whereIn, whereRaw, whereBetween, distinct, join, orderBy, groupBy, having) |
Chaining functions
Function | Description |
---|---|
select | Return the data from the query builder. Arguments: fields (string default is '*'. It takes comma separated string), limit (int), offset (int) |
delete | Deletes the records. Arguments: limit (int), offset (int) |
update | Updates the matching records. Arguments: data (object), limit (int), offset (int) |
where | Adds where condition to the query builder. Arguments: field (string), value, operator (string default is '=' example: =,<,>, like ...), andOr (string default is 'AND') |
whereIn | Adds where condition to filter the records that matches with the array given. Arguments: field (string), valueArray, andOr (string default is 'AND') |
whereBetween | Adds where condition to filter the records that between the array given. Arguments: field (string), valueArray (two item only), andOr (string default is 'AND') |
whereRaw | Adds raw where condition to write complex where clause. Arguments: condition (string), andOr (string default is 'AND') |
distinct | Removes duplicates from result set. Takes no argument |
join | Joins the table to another. This is a little more complex. See the examples below Arguments: joinTable (string), joinTableAlias (string), joinCallback (function (j){}), joinType (string default is 'INNER') |
orderBy | Adds order by clause to the query. It can be used multiple times Arguments: field (string), type (string default is 'ASC' values are ASC, DESC) |
groupBy | Groups the query using the array given. Arguments: groupByArray (array of string) |
having | Adds raw having clause to a grouped query. Arguments: havingStr (string) |
exists | Check if records exists or not |
key | value | required |
---|---|---|
columnName | string | ✅ |
dataType | string (null, integer, real, text, blob) | |
primaryKey | boolean | |
autoIncrement | boolean | |
notNull | boolean | |
unique | boolean | |
default | string | |
option | string (extra attribs such as "CHECK" when needed ) |
const db = SQLite.openDatabase(
{ name: 'test.db', location: 'default' },
succ => console.log('DB Created: '),
err => console.log('Err:', err)
);
const sw = new SQLiteWrapper(db);
sw.dropTable('user');
sw.dropTable('score');
sw.createTable('user', [
{
columnName: 'id',
dataType: 'integer',
primaryKey: true,
autoIncrement: true,
},
{
columnName: 'name',
dataType: 'text',
notNull: true,
unique: true,
},
{
columnName: 'team',
dataType: 'text',
default: 'gala',
notNull: true,
},
]);
// Result is: true
sw.createTable('score', [
{
columnName: 'id',
dataType: 'integer',
primaryKey: true,
autoIncrement: true,
},
{
columnName: 'game',
dataType: 'integer',
notNull: true,
},
{
columnName: 'userId',
dataType: 'integer',
notNull: true,
},
{
columnName: 'score',
dataType: 'integer',
notNull: true,
},
]);
// Result is: true
const users = [
{ id: 1, name: 'user1' },
{ id: 2, name: 'user2', team: 'madrid' },
{ id: 3, name: 'user3', team: 'barca' },
{ id: 4, name: 'user4', team: 'arsenal' },
{ id: 5, name: 'user5', team: 'barca' },
{ id: 6, name: 'user6', team: 'gala' },
];
const scores = [
{ game: 1, userId: 1, score: 5 },
{ game: 2, userId: 2, score: 2 },
{ game: 3, userId: 3, score: 4 },
{ game: 4, userId: 1, score: 8 },
{ game: 1, userId: 2, score: 3 },
{ game: 2, userId: 4, score: 1 },
{ game: 3, userId: 2, score: 2 },
{ game: 4, userId: 3, score: 4 },
{ game: 5, userId: 1, score: 3 },
{ game: 5, userId: 2, score: 1 },
{ game: 6, userId: 3, score: 5 },
{ game: 6, userId: 4, score: 2 },
{ game: 7, userId: 3, score: 2 },
{ game: 7, userId: 1, score: 1 },
{ game: 8, userId: 2, score: 4 },
{ game: 8, userId: 4, score: 3 },
];
sw.insert('user', users);
// Result: true
sw.insert('score', scores);
// Result: true
const { insertId } = sw.insert('score', { game: 9, userId: 1, score: 4 });
// Result: InsertId: 17
const rowsAffected = sw
.table('user')
.where('team', 'gala')
.update({ team: 'galatasaray' });
// rowsAffected: 2
const rowsDeleted = sw
.table('user')
.whereBetween('id', [4, 6])
.delete();
// rowsDeleted: 3
sw.table('user').select()
/*
Result:
{
"data":[
{
"team":"galatasaray",
"name":"user1",
"id":1
},
{
"team":"madrid",
"name":"user2",
"id":2
},
{
"team":"barca",
"name":"user3",
"id":3
},
{
"team":"arsenal",
"name":"user4",
"id":4
},
{
"team":"barca",
"name":"user5",
"id":5
},
{
"team":"galatasaray",
"name":"user6",
"id":6
}
],
"rowsAffected":0,
"length":6
}
*/
const teams = sw
.table('user')
.distinct()
.select('team');
const teamArray = teams.data.map(x => x.team)
// Result: ["galatasaray", "madrid", "barca", "arsenal"]
This query return the users and total scores with user name sorted descendant by sumOfScore where sums bigger than 12 and team is not equal to arsenal
const maxScoreList = sw
.table('score', 'S')
.join('user', 'U', j => {
j.on('U.id', 'S.userId');
j.whereIn('U.id', [1, 2, 3, 4, 5]);
})
.where('U.team', 'arsenal', '!=')
.groupBy(['userId'])
.having('sumOfScore > 12')
.orderBy('sumOfScore', 'DESC')
.select('U.name, SUM(S.score) as sumOfScore');
/*
Result:
{
"data":[
{
"sumOfScore":21,
"name":"user1"
},
{
"sumOfScore":15,
"name":"user3"
}
],
"rowsAffected":0,
"length":2
}
*/
There are 2 ways to get the results from functions
const records = await sw.table('user').select();
sw.table('score')
.select()
.then(result => console.log(result));
try {
await sw.table('tableNotExists').select();
} catch (err) {
console.log(err);
}
sw.table('tableNotExists')
.select()
.then(() => {})
.catch(err => console.log(err));
All bugs, feature requests, feedback, etc., are welcome.
If this project help you reduce time to develop, you can give me a cup of coffee ☕️ :)
FAQs
Tiny SQLite helper library (query builder) built on top of HTML5/Web SQL (DRAFT) API
The npm package sqlite-js-wrapper receives a total of 3 weekly downloads. As such, sqlite-js-wrapper popularity was classified as not popular.
We found that sqlite-js-wrapper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.