Manage sqlite databases with node.js
Import the package
const Sqlite = require('tru_sqlite')({});
- Create a new database
Sqlite.createDatabase({
dbPath: <yourDbPath>,
onSuccess: () => {},
onError: err => {}
});
- Add a table
Sqlite.createTable({
dbPath: <yourDbPath>,
name: 'users',
cols: [
{key: 'id', type: 'INT'},
{key: 'email', type: 'VARCHAR(30)'},
{key: 'password', type: 'VARCHAR(20)'},
{key: 'grade', type: 'INT'},
{key: 'token', type: 'VARCHAR(50)'}
],
onSuccess: () => {},
onError: err => {}
});
- Get table schema
Sqlite.tableInfo({
dbPath: <yourDbPath>,
table: 'users',
onSuccess: results => {},
onError: err => {}
});
- Run CRUD queries
Sqlite.query({
dbPath: <yourDbPath>,
sql: 'SELECT * FROM users',
onSuccess: results => {},
onError: err => {}
});
Sqlite.query({
dbPath: <yourDbPath>,
sql: 'INSERT INTO users VALUES (NULL, :email, :password, :grade, :token)',
args: {
email: '[email protected]',
password: 'abc123efg',
grade: 1,
token: 'eyAiaXNzIjogImVraW5vLmNvbSIsICJuYW1lIjogIkpvaG4gRG9lIiwgImFkbWluIjogdHJ1ZSB9'
},
onSuccess: results => {},
onError: err => {}
});
Sqlite.query({
dbPath: <yourDbPath>,
sql: 'UPDATE users SET grade = :grade WHERE id = :id',
args: {
grade: 2,
id: 1
},
onSuccess: results => {},
onError: err => {}
});
Sqlite.query({
dbPath: <yourDbPath>,
sql: 'DELETE FROM users WHERE email = :email',
args: {
email: '[email protected]',
},
onSuccess: results => {},
onError: err => {}
});