Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added functionality to updates

  • Loading branch information...
commit b574cfc99afe54e53314c413b0abb87ed000fe63 1 parent 3f52ff2
chrisdew authored
View
4 lib/row.js
@@ -18,7 +18,9 @@ function Row(fields, values) {
util.inherits(Row, events.EventEmitter);
Row.prototype.update = function(pairs) {
+ console.log("Pairs",pairs);
for (var i in pairs) {
this[pairs[i].field] = pairs[i].expr;
}
-}
+ this._version++;
+}
View
32 lib/table.js
@@ -65,6 +65,36 @@ Table.prototype.insert = function(spec, callback) {
var r = new row.Row(spec.fields, spec.values);
this.rowsByPk[pk] = r;
if (callback) callback(null, "1 row inserted.");
- this.emit('delta', {op:'insert',row:r});
+ this.emit('delta', {op:'insert',table:this.name,row:r});
return this;
}
+
+Table.prototype.update = function(spec) {
+ console.log("Table::update", spec);
+ var count = 0;
+
+ var evaluate = createFn(spec.where.expr);
+
+ for (var pk in this.rowsByPk) {
+ var r = this.rowsByPk[pk];
+ if (evaluate(r)) {
+ count++;
+ r.update(spec.set);
+ this.emit('delta', {op:'update',table:this.name,pk:pk,set:spec.set});
+ }
+ }
+}
+
+function createFn(expr) {
+ console.log("createFn", expr);
+ if (expr.fn === 'equal') {
+ return createEqual(expr.args);
+ }
+}
+
+function createEqual(args) {
+ console.log("createEqual", args);
+ if (args[0].field && (typeof args[1] === 'string' || typeof args[1] === 'number')) {
+ return function(r) { return r[args[0].field] === args[1]; };
+ }
+}
View
9 test/row-test.js
@@ -12,5 +12,14 @@ describe('Row', function() {
assert.deepEqual("EventEmitter",
new row.Row().constructor.super_.name);
});
+ var r = new row.Row(['id','foo'],[1,"hello"]);
+ it('should be a row with some fields', function() {
+ assert.deepEqual({"_version":1,"id":1,"foo":"hello"},r);
+ });
+ it('should update the value of foo to "world"', function() {
+ r.update([{"field":"foo","expr":"world"}]);
+ assert.deepEqual({"_version":2,"id":1,"foo":"world"},r);
+ });
+
});
View
33 test/table-test.js
@@ -13,12 +13,13 @@ describe('Table', function() {
new table.Table().constructor.super_.name);
});
+ var bar;
it('should insert a record', function(done) {
- var bar = new table.Table({name:'bar',
- fields:[{field:'id',type:'integer',pk:true,ai:true},
- {field:'foo',type: 'varchar'}]});
- bar.on('delta', function(delta) {
- assert.deepEqual({"op":"insert","row":{"_version":1,"id":1,"foo":"hello"}}, delta);
+ bar = new table.Table({name:'bar',
+ fields:[{field:'id',type:'integer',pk:true,ai:true},
+ {field:'foo',type: 'varchar'}]});
+ bar.once('delta', function(delta) {
+ assert.deepEqual({"op":"insert","table":"bar","row":{"_version":1,"id":1,"foo":"hello"}}, delta);
done();
});
bar.insert({"table":"bar","fields":["id","foo"],"values":[1,"hello"]});
@@ -28,5 +29,27 @@ describe('Table', function() {
assert.deepEqual(2,
bar.nextPk);
});
+
+ it('should insert a second record', function(done) {
+ bar.once('delta', function(delta) {
+ assert.deepEqual({"op":"insert","table":"bar","row":{"_version":1,"id":2,"foo":"hello2"}}, delta);
+ done();
+ });
+ bar.insert({"table":"bar","fields":["id","foo"],"values":[2,"hello2"]});
+ assert.deepEqual({'1':{id:1,foo:'hello',_version:1},
+ "2":{"_version":1,"id":2,"foo":"hello2"}},
+ bar.rowsByPk);
+ assert.deepEqual(3,
+ bar.nextPk);
+ });
+
+ it('sjould update a record', function(done) {
+ bar.once('delta', function(delta) {
+ assert.deepEqual({"op":"update","table":"bar","pk":"1","set":[{"field":"foo","expr":"hello"}]}, delta);
+ done();
+ });
+ bar.update({"table":"bar","set":[{"field":"foo","expr":"hello"}],"where":{"expr":{"fn":"equal","args":[{field:"id"},1]}}});
+ });
+
});
Please sign in to comment.
Something went wrong with that request. Please try again.