Skip to content

Commit ee7d876

Browse files
committed
refactoring
1 parent 735bb6c commit ee7d876

File tree

4 files changed

+171
-75
lines changed

4 files changed

+171
-75
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,46 @@
22

33
[![NPM](https://nodei.co/npm/breadfruit.png?compact=true)](https://nodei.co/npm/breadfruit/)
44

5-
Not really bread. Not really fruit. Just like this package.
5+
Not really bread. Not really fruit. Just like this package. Some simple helpers on top of knex.
6+
7+
## create an instance of breadfruit
8+
9+
```javascript
10+
const config = {
11+
client: 'postgresql',
12+
connection: 'postgres://postgres@localhost:5432/someDatabase',
13+
pool: { min: 1, max: 7 }
14+
};
15+
16+
const bread = require('breadfruit')(config);
17+
```
18+
19+
## Browse, Read, Edit, Add, Delete, Raw
20+
21+
```javascript
22+
const {browse, read, edit, add, del, raw} = require('breadfruit')(config);
23+
24+
//get an array of users, by table, columns, and a filter
25+
const users = await browse('users', ['username', 'user_id'], {active: true});
26+
27+
28+
//get a single user by table, columns, and a filter
29+
const user = await read('users', ['username', 'first_name'], {user_id: 1337});
30+
31+
32+
//edit a user by table, returned columns, updated values, and a filter
33+
const updatedUser = await edit('users', ['username', 'first_name'], {first_name: 'Howard'}, {user_id: 1337});
34+
35+
36+
//add a new user by table, returned columns, and user data
37+
const newUser = await add('users', ['user_id'], {first_name: 'Howard', username: 'howitzer'});
38+
39+
40+
//delete a user by table and a filter
41+
const deleteCount = await del('users', {user_id: 1337});
42+
43+
44+
//perform a raw query
45+
const rows = await raw('select * from users');
46+
47+
```

index.js

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,81 @@
1-
let knex;
1+
const knexConstructor = require('knex');
22

33
function connect(settings) {
4-
knex = require('knex')(settings);
5-
}
4+
const knex = knexConstructor(settings);
5+
const defaultOptions = {
6+
dateField: 'created_at',
7+
dbApi: knex,
8+
limit: 1000,
9+
offset: 0,
10+
sortOrder: 'ASC',
11+
};
612

7-
const defaultOptions = {
8-
dateField: 'created_at',
9-
dbApi: knex,
10-
limit: 1000,
11-
offset: 0,
12-
sortOrder: 'ASC',
13-
};
13+
return {
14+
browse(table, fields, filter, options = {}) {
15+
const dbApi = options.dbApi || defaultOptions.dbApi || knex;
16+
const limit = options.limit || defaultOptions.limit;
17+
const offset = options.offset || defaultOptions.offset;
18+
const dateField = options.dateField || defaultOptions.dateField;
19+
const sortOrder = options.sortOrder || defaultOptions.sortOrder;
1420

15-
module.exports = {
16-
connect,
17-
browse(table, fields, filter, options = {}) {
18-
const dbApi = options.dbApi || defaultOptions.dbApi || knex;
19-
const limit = options.limit || defaultOptions.limit;
20-
const offset = options.offset || defaultOptions.offset;
21-
const dateField = options.dateField || defaultOptions.dateField;
22-
const sortOrder = options.sortOrder || defaultOptions.sortOrder;
21+
let query = dbApi(table)
22+
.where(filter)
23+
.select(fields)
24+
.limit(limit)
25+
.offset(offset);
2326

24-
let query = dbApi(table)
25-
.where(filter)
26-
.select(fields)
27-
.limit(limit)
28-
.offset(offset);
27+
if (options.search_start_date && options.search_end_date) {
28+
query = query
29+
.whereBetween(dateField, [options.search_start_date, options.search_end_date]);
30+
}
2931

30-
if (options.search_start_date && options.search_end_date) {
31-
query = query
32-
.whereBetween(dateField, [options.search_start_date, options.search_end_date]);
33-
}
32+
if (options.orderBy) {
33+
query = query
34+
.orderBy(options.orderBy, sortOrder);
35+
}
3436

35-
if (options.orderBy) {
36-
query = query
37-
.orderBy(options.orderBy, sortOrder);
37+
return query;
38+
},
39+
read(table, fields, filter, options = {}) {
40+
const dbApi = options.dbApi || knex;
41+
return dbApi(table)
42+
.where(filter)
43+
.select(fields)
44+
.then(([row]) => {
45+
return row;
46+
});
47+
},
48+
add(table, fields, data, options = {}) {
49+
const dbApi = options.dbApi || knex;
50+
return dbApi(table)
51+
.returning(fields)
52+
.insert(data)
53+
.then(([row]) => {
54+
return row;
55+
});
56+
},
57+
edit(table, fields, data, filter, options = {}) {
58+
const dbApi = options.dbApi || knex;
59+
return dbApi(table)
60+
.where(filter)
61+
.returning(fields)
62+
.update(data)
63+
.then(([row]) => {
64+
return row;
65+
});
66+
},
67+
del(table, filter, options = {}) {
68+
const dbApi = options.dbApi || knex;
69+
return dbApi(table)
70+
.where(filter)
71+
.del();
72+
},
73+
raw(sql, options = {}) {
74+
const dbApi = options.dbApi || knex;
75+
return dbApi.raw(sql, options)
76+
.then(res => res.rows);
3877
}
78+
};
79+
}
3980

40-
41-
return query;
42-
},
43-
read(table, fields, filter, options = {}) {
44-
const dbApi = options.dbApi || knex;
45-
return dbApi(table)
46-
.where(filter)
47-
.select(fields)
48-
.then(([row]) => {
49-
return row;
50-
});
51-
},
52-
add(table, fields, data, options = {}) {
53-
const dbApi = options.dbApi || knex;
54-
return dbApi(table)
55-
.returning(fields)
56-
.insert(data)
57-
.then(([row]) => {
58-
return row;
59-
});
60-
},
61-
edit(table, fields, data, filter, options = {}) {
62-
const dbApi = options.dbApi || knex;
63-
return dbApi(table)
64-
.where(filter)
65-
.returning(fields)
66-
.update(data)
67-
.then(([row]) => {
68-
return row;
69-
});
70-
},
71-
del(table, filter, options = {}) {
72-
const dbApi = options.dbApi || knex;
73-
return dbApi(table)
74-
.where(filter)
75-
.del();
76-
},
77-
raw(sql, options = {}) {
78-
const dbApi = options.dbApi || knex;
79-
return dbApi.raw(sql, options)
80-
.then(res => res.rows);
81-
}
82-
};
81+
module.exports = connect;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "breadfruit",
3-
"version": "0.5.1",
3+
"version": "2.0.0",
44
"description": "Boilerplate sql query system for Node.js using Knex",
55
"main": "index.js",
66
"scripts": {

test/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,59 @@ describe('breadfruit', function(){
1313
it('be awesome', function(done){
1414
done();
1515
});
16+
17+
it('should create an instance of the api', function(done) {
18+
const api = breadfruit({client: 'pg'});
19+
api.should.be.an('object');
20+
done();
21+
});
22+
23+
it('should fail to browse without a real connection', function(done) {
24+
const api = breadfruit({client: 'pg'});
25+
api.browse('tableName', [], {})
26+
.catch((error) => {
27+
done();
28+
});
29+
});
30+
31+
it('should fail to read without a real connection', function(done) {
32+
const api = breadfruit({client: 'pg'});
33+
api.read('tableName', [], {})
34+
.catch((error) => {
35+
done();
36+
});
37+
});
38+
39+
it('should fail to add without a real connection', function(done) {
40+
const api = breadfruit({client: 'pg'});
41+
api.add('tableName', [], {})
42+
.catch((error) => {
43+
done();
44+
});
45+
});
46+
47+
it('should fail to edit without a real connection', function(done) {
48+
const api = breadfruit({client: 'pg'});
49+
api.edit('tableName', [], {})
50+
.catch((error) => {
51+
done();
52+
});
53+
});
54+
55+
it('should fail to delete without a real connection', function(done) {
56+
const api = breadfruit({client: 'pg'});
57+
api.del('tableName', [], {})
58+
.catch((error) => {
59+
done();
60+
});
61+
});
62+
63+
it('should fail to do raw query without a real connection', function(done) {
64+
const api = breadfruit({client: 'pg'});
65+
api.raw('select NOW()', {})
66+
.catch((error) => {
67+
done();
68+
});
69+
});
70+
1671
});

0 commit comments

Comments
 (0)