Skip to content

Commit 5381f78

Browse files
committed
reorganization and cleanup
1 parent 8f926b1 commit 5381f78

File tree

7 files changed

+104
-71
lines changed

7 files changed

+104
-71
lines changed

index.d.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
declare module "promisesql";
22

3+
type DataObject = Object;
4+
type DataArray = DataObject[];
5+
6+
type QueryRetval = void | DataObject | DataArray | undefined;
7+
type QueryPromise = Promise<QueryRetval>;
8+
39
interface BaseOptions {
4-
filepath?: string,
10+
file?: string,
11+
}
12+
13+
interface RunOptions extends BaseOptions {
14+
statement: string,
15+
args?: any[]
516
}
617

718
interface InsertOptions extends BaseOptions {
@@ -10,7 +21,11 @@ interface InsertOptions extends BaseOptions {
1021
values: string[]
1122
}
1223

24+
type SelectionRetval = DataObject | DataArray | undefined;
25+
type SelectionPromise = Promise<SelectionRetval>;
26+
1327
interface SelectionOptions extends BaseOptions {
28+
first?: boolean,
1429
all?: boolean,
1530
columns?: string[],
1631
from: string,
@@ -28,7 +43,7 @@ interface DeleteOptions extends BaseOptions {
2843
where?: (string | Expression)[]
2944
}
3045

31-
interface Expression {
46+
interface BooleanExpression {
3247
lhs: string,
3348
operator: string,
3449
rhs: string

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module.exports = require('./lib/queries');
55
module.exports.PromiseDB = PromiseDB;
66

77
// Export conditions (expressions, logic operators, etc.)
8-
module.exports.expression = require('./util/operators/expression');
9-
module.exports.logic = require('./util/operators/logic');
10-
module.exports.aggregate = require('./util/operators/aggregate');
8+
module.exports.expression = {
9+
...require('./util/expressions/boolean'),
10+
...require('./util/expressions/numeric')
11+
};
12+
13+
module.exports.operator = require('./util/operators/logic');

lib/promisedb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class PromiseDB extends sqlite.Database {
1515
* Promise-based query OR command.
1616
* @param {string} sql
1717
* @param {string[]} args
18-
* @returns {Promise<object>}
18+
* @returns {QueryPromise}
1919
*/
2020
query(sql, args = []) {
2121
return new Promise((resolve, reject) => {

lib/queries.js

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ var db = undefined;
77

88
/**
99
* Opens a database file.
10-
* @param {string} filepath Path to the database file
10+
* @param {string} file Path to the database file
1111
*/
12-
function open(filepath) {
13-
db = new PromiseDB(filepath);
12+
function open(file) {
13+
db = new PromiseDB(file);
1414
}
1515

1616
/**
@@ -22,25 +22,22 @@ function close() {
2222
}
2323

2424
/**
25-
* Dynamically opens and closes a database if a filepath is provided.
26-
* @param {string} filepath
25+
* Dynamically opens and closes a database if a file is provided.
26+
* @param {string} file
2727
* @returns {Promise<Function>}
2828
*/
29-
function dynamicAccess(filepath) {
30-
// If filepath is undefined or db is already open, do nothing
31-
if (!filepath || db) {
29+
function dynamicAccess(file) {
30+
// If file is undefined or db is already open, do nothing
31+
if (!file || db) {
3232
return;
3333
}
3434

3535
// Open db and then close it when promise is fulfilled
3636
return new Promise((resolve, reject) => {
3737
try {
38-
open(filepath);
39-
resolve({
40-
then: () => close()
41-
});
42-
}
43-
catch(error) {
38+
open(file);
39+
resolve({ then: close });
40+
} catch(error) {
4441
reject(error);
4542
}
4643
});
@@ -50,68 +47,66 @@ module.exports = {
5047
// Export database open/close functions (synchronous!)
5148
open, close,
5249

53-
print: function() {
54-
console.log(typeof db);
55-
},
56-
5750
/**
5851
* Asynchronous database query.
59-
* @param {string} sql SQL statement
60-
* @param {string[]} args The arguments of the statement
61-
* @param {string} filepath Path to the database file
62-
* @returns {undefined|object}
52+
* @param {RunOptions} options SQL statement
53+
* @returns {QueryPromise}
6354
*/
64-
query: async function(sql, args = [], filepath = undefined) {
65-
dynamicAccess(filepath);
66-
return await db.query(sql, args);
55+
run: async function(options) {
56+
dynamicAccess(options.file);
57+
return await db.query(options.statement, options.args);
6758
},
6859

6960
/**
7061
* Asynchronous instert query.
7162
* @param {InsertOptions} options
63+
* @returns {Promise<void>}
7264
*/
73-
insert: async function(options) {
74-
dynamicAccess(options.filepath);
65+
insert: async function(options) {
66+
dynamicAccess(options.file);
7567
const sql = sqlstr.insertStr(options);
7668
await db.query(sql, options.values);
7769
},
7870

7971
/**
8072
* Asynchronous selection query.
8173
* @param {SelectionOptions} options
82-
* @returns
74+
* @returns {SelectionPromise}
8375
*/
8476
select: async function(options) {
85-
dynamicAccess(options.filepath);
77+
dynamicAccess(options.file);
8678
const { sql, args } = sqlstr.selectStr(options);
87-
return await db.query(sql, args);
79+
const data = await db.query(sql, args);
80+
return (data.length > 0 && options.first) ? data[0] : data;
8881
},
8982

9083
/**
9184
* Asynchronous update query.
9285
* @param {UpdateOptions} options
86+
* @returns {Promise<void>}
9387
*/
9488
update: async function(options) {
95-
dynamicAccess(options.filepath);
89+
dynamicAccess(options.file);
9690
const { sql, args } = sqlstr.updateStr(options);
9791
await db.query(sql, args);
9892
},
9993

10094
/**
10195
* Asynchronous delete query.
10296
* @param {DeleteOptions} options
97+
* @returns {Promise<void>}
10398
*/
10499
delete: async function(options) {
105-
dynamicAccess(options.filepath);
100+
dynamicAccess(options.file);
106101
const { sql, args } = sqlstr.deleteStr(options);
107102
await db.query(sql, args);
108103
},
109104

110105
/**
111-
* Synchronous query
106+
* Synchronous query.
112107
* @param {function} query
113108
* @param {string[]|BaseOptions} options
114-
* @returns {undefined|object}
109+
* @returns {QueryRetval}
115110
*/
116111
sync: function(query, options = []) {
117112
if (query.constructor.name !== 'AsyncFunction')
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
* Equals expression (lhs = rhs)
44
* @param {string} lhs
55
* @param {string} rhs
6-
* @returns
6+
* @returns {BooleanExpression}
77
*/
88
eq: function(lhs, rhs) {
99
return { lhs: lhs, operator: '=', rhs: rhs };
@@ -13,7 +13,7 @@ module.exports = {
1313
* Less than expression (lhs < rhs)
1414
* @param {string} lhs
1515
* @param {string} rhs
16-
* @returns
16+
* @returns {BooleanExpression}
1717
*/
1818
lt: function(lhs, rhs) {
1919
return { lhs: lhs, operator: '<', rhs: rhs };
@@ -23,7 +23,7 @@ module.exports = {
2323
* Greater than expression (lhs > rhs)
2424
* @param {string} lhs
2525
* @param {string} rhs
26-
* @returns
26+
* @returns {BooleanExpression}
2727
*/
2828
gt: function(lhs, rhs) {
2929
return { lhs: lhs, operator: '>', rhs: rhs };
@@ -33,7 +33,7 @@ module.exports = {
3333
* Less than or equal to expression (lhs <= rhs)
3434
* @param {string} lhs
3535
* @param {string} rhs
36-
* @returns
36+
* @returns {BooleanExpression}
3737
*/
3838
leq: function(lhs, rhs) {
3939
return { lhs: lhs, operator: '<=', rhs: rhs };
@@ -43,7 +43,7 @@ module.exports = {
4343
* Greater than or equal to expression (lhs >= rhs)
4444
* @param {string} lhs
4545
* @param {string} rhs
46-
* @returns
46+
* @returns {BooleanExpression}
4747
*/
4848
geq: function(lhs, rhs) {
4949
return { lhs: lhs, operator: '>=', rhs: rhs };

util/expressions/numeric.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module.exports = {
2+
/**
3+
* Returns a max expression [MAX(column_name)]
4+
* @param {string} column
5+
* @returns {string}
6+
*/
7+
max: function(column) {
8+
return `MAX(${column})`;
9+
},
10+
11+
/**
12+
* Returns a min expression [MIN(column_name)]
13+
* @param {string} column
14+
* @returns {string}
15+
*/
16+
min: function(column) {
17+
return `MIN(${column})`;
18+
},
19+
20+
/**
21+
* Returns a count expression [COUNT(column_name)]
22+
* @param {string} column
23+
* @returns {string}
24+
*/
25+
count: function(column) {
26+
return `COUNT(${column})`;
27+
},
28+
29+
/**
30+
* Returns an average expression [AVG(column_name)]
31+
* @param {string} column
32+
* @returns {string}
33+
*/
34+
avg: function(column) {
35+
return `AVG(${column})`;
36+
},
37+
38+
/**
39+
* Returns a sum expression [SUM(column_name)]
40+
* @param {string} column
41+
* @returns {string}
42+
*/
43+
sum: function(column) {
44+
return `SUM(${column})`;
45+
}
46+
}

util/operators/aggregate.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)