Skip to content

Commit 36cd0a5

Browse files
committed
first functions
1 parent 559d11b commit 36cd0a5

11 files changed

Lines changed: 1699 additions & 0 deletions

File tree

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./src/SQLiteTable');

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "sqlite-table",
3+
"version": "0.0.0",
4+
"description": "SQLiteTable class for sqlite3 module",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jasmine-node spec/"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git@github.com:finalclass/sqlite-table.git"
12+
},
13+
"keywords": [
14+
"sqlite3",
15+
"db",
16+
"table"
17+
],
18+
"author": "Szymon Wygnański <s@finalclass.net> (http://finalclass.net/)",
19+
"license": "ISC",
20+
"bugs": {
21+
"url": "https://github.com/finalclass/sqlite-table/issues"
22+
},
23+
"homepage": "https://github.com/finalclass/sqlite-table",
24+
"dependencies": {
25+
"sqlite3": "~2.2.7"
26+
}
27+
}

spec/sqlite-table.spec.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
var SQLiteTable = require('../src/SQLiteTable');
2+
var sqlite3 = require('sqlite3');
3+
4+
describe('SQLiteTable', function () {
5+
var db;
6+
var table;
7+
8+
beforeEach(function (next) {
9+
db = new sqlite3.Database(':memory:');
10+
var sql = 'CREATE TABLE user(' +
11+
'id INTEGER PRIMARY KEY, ' +
12+
'firstName TEXT NOT NULL,' +
13+
'lastName TEXT NOT NULL)';
14+
15+
db.run(sql, function (err) {
16+
if (err) {
17+
next(err);
18+
} else {
19+
table = new SQLiteTable(db);
20+
table._tableName = 'user';
21+
next();
22+
}
23+
});
24+
});
25+
26+
it('insert', function (next) {
27+
table.insert({firstName: 'Jan', lastName: 'Kowalski'}, function (err) {
28+
expect(err).toBeFalsy();
29+
db.get('select * from user', function (err, result) {
30+
expect(err).toBeFalsy();
31+
expect(result).not.toBeFalsy();
32+
next();
33+
});
34+
});
35+
});
36+
37+
it('update', function (next) {
38+
db.run('INSERT INTO user(firstName, lastName) VALUES(?, ?)', 'Jan', 'Kowalski', function (err) {
39+
table.update({id: 1, firstName: 'Jaś'}, function (err) {
40+
expect(err).toBeFalsy();
41+
db.all('select * from user', function (err, result) {
42+
expect(err).toBeFalsy();
43+
expect(result.length).toBe(1);
44+
expect(result[0].firstName).toBe('Jaś');
45+
next();
46+
});
47+
})
48+
});
49+
});
50+
51+
it('finds all', function (next) {
52+
db.run('INSERT INTO user(firstName, lastName) VALUES(?,?)', 'Jan', 'Kowalski', function () {
53+
db.run('INSERT INTO user(firstName, lastName) VALUES(?,?)', 'Jaś', 'Kowalski', function () {
54+
table.find(1, function (err, result) {
55+
expect(err).toBeFalsy();
56+
expect(result.firstName).toBe('Jan');
57+
expect(result.lastName).toBe('Kowalski');
58+
table.find(2, function (err, result) {
59+
expect(err).toBeFalsy();
60+
expect(result.firstName).toBe('Jaś');
61+
expect(result.lastName).toBe('Kowalski');
62+
table.all(function (err, results) {
63+
expect(err).toBeFalsy();
64+
expect(results.length).toBe(2);
65+
next();
66+
});
67+
});
68+
});
69+
});
70+
});
71+
});
72+
73+
});

src/SQLiteTable.js

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SQLiteTable.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/SQLiteTable.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/// <reference path="../../login-server/src/typings/tsd.d.ts"/>
2+
3+
import sqlite3 = require('sqlite3');
4+
5+
class SQLiteTable {
6+
7+
private _tableName:string;
8+
9+
constructor(private db:sqlite3.Database) {
10+
11+
}
12+
13+
public insert(data:any, next:(err?:Error)=>void):void {
14+
var keys:string[] = Object.keys(data);
15+
var keysVars:string[] = keys.map((key:string):string => '$' + key);
16+
var objVars:any = this.getObjVars(data);
17+
18+
var sql:string = 'INSERT INTO ' + this.tableName
19+
+ '(' + keys.join(',') + ') '
20+
+ 'VALUES(' + keysVars.join(',') + ')';
21+
22+
this.db.run(sql, objVars, next);
23+
}
24+
25+
private getObjVars(obj):any {
26+
var objVars:any = {};
27+
28+
Object.keys(obj).forEach((key:string):void => {
29+
objVars['$' + key] = obj[key];
30+
});
31+
32+
return objVars;
33+
}
34+
35+
public update(data:any, next:(err?:Error)=>void):void {
36+
if (!data.id) {
37+
next(new Error('id is not specified'));
38+
return;
39+
}
40+
var keys:string[] = Object.keys(data);
41+
var updateStmts:string[] = keys.map((key:string):string => {
42+
return key + '=$' + key;
43+
});
44+
var objVars:any = this.getObjVars(data);
45+
46+
var sql:string = 'UPDATE ' + this.tableName
47+
+ ' SET ' + updateStmts.join(' , ')
48+
+ ' WHERE id=$id';
49+
50+
this.db.run(sql, objVars, next);
51+
}
52+
53+
private getSQLSelectStmt(params:any):{sql:string; objVars:any} {
54+
var objVars:any = this.getObjVars(params);
55+
var whereStmts:string[] = Object.keys(params)
56+
.map((key:string):string => key + '=$' + key);
57+
var where:string = whereStmts.length > 0 ? ' WHERE ' + whereStmts.join(' AND ') : '';
58+
59+
return {
60+
sql: 'SELECT * FROM ' + this.tableName + where,
61+
objVars: objVars
62+
};
63+
}
64+
public all(next:(err?:Error)=>void):void;
65+
public all(params?:any, next?:(err?:Error)=>void):void {
66+
if (typeof params === 'function') {
67+
next = params;
68+
params = {};
69+
}
70+
var stmt:{sql:string;objVars:any} = this.getSQLSelectStmt(params);
71+
this.db.all(stmt.sql, stmt.objVars, next);
72+
}
73+
74+
public find(params:string, next:(err?:Error)=>void):void;
75+
public find(params:any, next:(err?:Error)=>void):void {
76+
var typeofParams:string = typeof params;
77+
if (typeofParams === 'string' || typeofParams === 'number') {
78+
params = {id: params};
79+
}
80+
var stmt:{sql:string;objVars:any} = this.getSQLSelectStmt(params);
81+
this.db.get(stmt.sql, stmt.objVars, next);
82+
}
83+
84+
public get tableName():string {
85+
if (!this._tableName) {
86+
throw new Error('Table name is not specified');
87+
}
88+
return this._tableName;
89+
}
90+
91+
}
92+
93+
export = SQLiteTable;

typings/debug/debug.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module "debug" {
2+
function debug(what:string):(msg:string, ...args:string[])=>void;
3+
4+
export = debug;
5+
}

0 commit comments

Comments
 (0)