tssqlite v1.0.1
TypeScript SQLite Wrapper (Object Relation Mapper)
Use this package to use sqlite commands in a generic way. The package automatically creates your database, you just need to specify the create table command. You can specify the file name of the database file via your package.json or in the constructor as the last parameter.
Installation
Install this package either via npm or the TypeScript Module Manager (tsmm).
npm:
npm install tssqlite --saveTypeScript Module Manager (tsmm):
tsmm install https://github.com/JanPeter/TypeScriptSQLite.gitAPI
class Sqlite<T>Constructor
You need to provide either the filePath parameter in the constructor or you can use the databasePath key in your package.json like that:
{
"name": "yourpackagename",
"databasePath": "yourdatabasefile.db"
...
}Without one of this options, the package will not be able to create your database. If you didn't provide one of this, your callback will get an Error with the message no databasePath provided.
constructor(createCmd: string, map: (row: any) => T, callback?: (error: Error) => void, filePath?: string)
createCommandstring, the create table command (I highly recommend to use CREATE TABLE IF NOT EXISTS...)mapfunction(row: any) => T, used to map the database row to your object of your class Trowany, database row object (access with row"colname" or row.colname)
callbackoptional function, called when the database and the table is createderrorError
filePathoptional string, location of your database file (will be created if not exists)
Example (TypeScript)
import Sqlite = require('sqlite');
class User {
constructor(public id: number, public name: string) { }
}
var database = new Sqlite<User>('CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY, name TEXT NOT NULL)',
(row) => {
return new User(row.id, row.name);
});All
all(callback: (error: Error, elements: T[])
callbackfunction will be called after execution ofallfunctionerrorErrorelementsT[], list of all objects from table
Example (TypeScript)
import Sqlite = require('sqlite');
class User {
constructor(public id: number, public name: string) { }
}
var database = new Sqlite<User>('CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY, name TEXT NOT NULL)',
(row) => {
return new User(row.id, row.name);
}, function(error: Error) {
if (error === null) {
database.all(function(error: Error, users: User[]) {
if (error === null) {
console.log(users);
} else {
console.log(error);
}
}
} else {
console.log(error);
}
});Where
where(select: string, params: any[], callback: (error: Error, elements: T[]) => void)
selectstring, sql select commandparamsany[], values to be inserted in the where clausecallbackfunction will be called afterwherefunction is executederrorErrorelementsT[], list of selected objects
Example (TypeScript)
import Sqlite = require('sqlite');
class User {
constructor(public id: number, public name: string) { }
}
var database = new Sqlite<User>('CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY, name TEXT NOT NULL)',
(row) => {
return new User(row.id, row.name);
}, function(error: Error) {
if (error === null) {
database.where('SELECT * FROM user WHERE name = ?', ['karl'], function(error: Error, users: User[]) {
if (error === null) {
console.log(users);
} else {
console.log(error);
}
}
} else {
console.log(error);
}
});
Get
get(select: string, params: any[], callback: (error: Error, element: T) => void)
selectstring, sql select commandparamsany[], values to be inserted in the where clausecallbackfunction will be called afterwherefunction is executederrorErrorelementT, first selected object
Example (TypeScript)
import Sqlite = require('sqlite');
class User {
constructor(public id: number, public name: string) { }
}
var database = new Sqlite<User>('CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY, name TEXT NOT NULL)',
(row) => {
return new User(row.id, row.name);
}, function(error: Error) {
if (error === null) {
database.get('SELECT * FROM user WHERE name = ?', ['karl'], function(error: Error, user: User) {
if (error === null) {
console.log(user);
} else {
console.log(error);
}
}
} else {
console.log(error);
}
});Insert
insert(cmd: string, params: any[], callback: (error: Error) => void)
cmdstring, sql insert commandparamsany[], values to be insertedcallbackfunction will be called after insert command is executederrorError
Example (TypeScript)
import Sqlite = require('sqlite');
class User {
constructor(public id: number, public name: string) { }
}
var database = new Sqlite<User>('CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY, name TEXT NOT NULL)',
(row) => {
return new User(row.id, row.name);
}, function(error: Error) {
if (error === null) {
database.insert('INSERT INTO user (name) VALUES (?)', ['karl'], function(error: Error) {
if (error === null) {
console.log('successfully inserted karl');
} else {
console.log(error);
}
}
} else {
console.log(error);
}
});Delete
delete(cmd: string, params: any[], callback: (error: Error) => void)
cmdstring, sql delete commandparamsany[], values to be inserted in the where clausecallbackfunction will be called after delete command is executederrorError
Example (TypeScript)
import Sqlite = require('sqlite');
class User {
constructor(public id: number, public name: string) { }
}
var database = new Sqlite<User>('CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY, name TEXT NOT NULL)',
(row) => {
return new User(row.id, row.name);
}, function(error: Error) {
if (error === null) {
database.insert('INSERT INTO user (name) VALUES (?)', ['karl'], function(error: Error) {
if (error === null) {
database.insert('DELETE FROM user WHERE name = ?)', ['karl'], function(error: Error) {
if (error === null) {
console.log('successfully deleted karl');
} else {
console.log(error);
}
}
} else {
console.log(error);
}
}
} else {
console.log(error);
}
});Close
You can explicitly close the database connection with this function.
close(callback?: (error: Error) => void))
callbackfunction will be called after database is closederrorError
Example (TypeScript)
import Sqlite = require('sqlite');
class User {
constructor(public id: number, public name: string) { }
}
var database = new Sqlite<User>('CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY, name TEXT NOT NULL)',
(row) => {
return new User(row.id, row.name);
}, function(error: Error) {
if (error === null) {
database.close(function(error: Error) {
if (error === null) {
// Any further executed functions to the database object won't work now
database.all(function(error: Error, users: User[]) {
// error object should not be null now
});
} else {
console.log(error);
}
});
} else {
console.log(error);
}
});