Skip to content

Commit eb1d910

Browse files
Add in findById and deleteById
1 parent 91fe03b commit eb1d910

4 files changed

Lines changed: 79 additions & 9 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ This was built primarily to see how far I could get in an afternoon / evening in
88
npm install pg-orm --save
99
```
1010

11+
Note that this will automatically use your database.json file to build the pg link, a la:
12+
13+
```
14+
{
15+
"dev": {
16+
"driver": "pg",
17+
"user": "skoobi",
18+
"password": "something",
19+
"host": "localhost",
20+
"database": "myDB",
21+
"schema": "myDBSchema"
22+
}
23+
}
24+
```
25+
1126
### Creating your models
1227

1328
The ORM essentially acts like a factory that pops out objects based on a simple DSL. Here's an example definition:

libs/base.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function dbCall(stmnt, values) {
3333

3434
// ---------------------------------------
3535

36+
// This should probably be in another file...
3637
var Base = function() {
3738
this.tableName = null;
3839
this.tableProperties = null;
@@ -46,6 +47,7 @@ Base.prototype.create = function(data) {
4647
_propertyValues = [],
4748
_cnt = 1;
4849

50+
// check the incoming data to ensure that it's legit mapped to the db:
4951
for (var i in data) {
5052
if (this.tableProperties.hasOwnProperty(i)) {
5153
_propertyNames.push(i);
@@ -97,10 +99,46 @@ Base.prototype.update = function() {
9799
dbCall(stmnt, _propertyValues).then(function(data) {
98100
defer.resolve(data);
99101
});
100-
102+
101103
return defer.promise;
102104
}; // end update
103105

106+
Base.prototype.findById = function(id) {
107+
var _this = this;
108+
var stmnt = 'SELECT * FROM ' + _this.tableName + ' WHERE id = $1';
109+
110+
var defer = Q.defer();
111+
dbCall(stmnt, [ id ]).then(function(data) {
112+
if (data.rows.length < 1) {
113+
defer.resolve(data);
114+
}
115+
116+
// build a new object from the result:
117+
var newObject = new Base();
118+
newObject.tableProperties = _this.tableProperties;
119+
newObject.tableName = _this.tableName;
120+
121+
for(var j in newObject.tableProperties) {
122+
newObject[j] = data.rows[0][j];
123+
}
124+
defer.resolve(newObject);
125+
});
126+
127+
return defer.promise;
128+
}; // end findById
129+
130+
Base.prototype.deleteById = function(id) {
131+
var _this = this;
132+
var stmnt = 'DELETE FROM ' + _this.tableName + ' WHERE id=$1';
133+
134+
var defer = Q.defer();
135+
dbCall(stmnt, [ id ]).then(function(data) {
136+
defer.resolve(data);
137+
});
138+
139+
return defer.promise;
140+
}; // end deleteById
141+
104142
// ---------------------------------------
105143

106144
module.exports.build = function(obj) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pg-orm",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "A simple node ORM for postgres",
55
"homepage": "http://skoobistudios.com",
66
"author": {

test/base.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,45 @@ var testBase = base.build({
1717
}
1818
});
1919

20-
describe('base node module', function () {
20+
describe('crud operators', function () {
2121
it('should should build an object', function () {
2222
assert(testBase.tableName == 'test', 'named object should be named');
2323
});
2424

25-
it('should be able to create a new object', function(done) {
26-
testBase.create({ 'name': 'Ronald' }).then(function(testObject) {
27-
assert(testObject.name == 'Ronald', 'Name should be ronald');
25+
it('should be able to create an object', function(done) {
26+
testBase.create({ 'name': 'Create' }).then(function(testObject) {
27+
assert(testObject.name == 'Create', 'Name should be Create');
2828
assert(typeof testObject.id != 'undefined', 'ID should not be undefined after insert');
2929
assert(testObject.prototype == testBase.prototype, 'Protos don\'t match');
3030
done();
3131
});
3232
});
3333

3434
it('should be able to update an existing object', function(done) {
35-
this.timeout(5000);
36-
testBase.create({ 'name': 'Ronald' }).then(function(testObject) {
35+
testBase.create({ 'name': 'Update' }).then(function(testObject) {
3736
testObject.name = 'Mike';
3837
testObject.update().then(function() {
39-
assert(testObject.name == 'Mike', 'Should update to new name');
38+
assert(testObject.name == 'Mike', 'Should update to new name of Mike');
4039
done();
4140
});
4241
});
4342
});
43+
44+
it('should be able to find an existing object', function(done) {
45+
testBase.create({ 'name': 'Find' }).then(function(testObject) {
46+
testBase.findById(testObject.id).then(function(newTestObject) {
47+
assert(testObject.name == 'Find', 'Name should be Find after find');
48+
done();
49+
});
50+
});
51+
});
52+
53+
it('should be able to delete an existing object', function(done) {
54+
testBase.create({ 'name': 'Delete' }).then(function(testObject) {
55+
var _id = testObject.id;
56+
testObject.deleteById(_id).then(function(data) {
57+
done();
58+
})
59+
});
60+
});
4461
});

0 commit comments

Comments
 (0)