Permalink
Browse files

feat: delete in yukari

  • Loading branch information...
XadillaX committed Oct 11, 2016
1 parent 5aafa1c commit 2d30750ba90d13008be3eb93d069600d3c39760a
Showing with 45 additions and 0 deletions.
  1. +45 −0 lib/yukari.js
@@ -174,6 +174,42 @@ class Yukari {
});
}

delete(callback) {
if(callback === undefined) callback = _emptyCallback;

if(this.$source === "new") {
return callback(new Error("You must call this function via a new Yukari object."));
}

let pk;
if(this.$model.primaryKeys.length) {
// if it has primary key(s), use primary key(s) to query
pk = this.$model.primaryKeys.reduce((pks, field) => {
if(this.$origData[field.name]) {
pks[field.name] = this.$origData[field.name].data;
}
return pks;
}, {});
} else {
// if no primary key, use all original data to query
pk = Object.keys(this.$origData).reduce((pks, key) => {
pks[key] = this.$origData[key].data;
return pks;
}, {});
}

this.$model.where(pk).limit("0, 1").delete(function(err, result, sql) {
if(err) return callback(err);
if(!result) return callback(new Error("unknown error."));
if(!result.affectedRows) {
return callback(new Error("Out-dated yukari object."));
}

this.$source = "delete";
return callback(undefined, true, sql);
});
}

insert(callback) {
if(callback === undefined) callback = _emptyCallback;

@@ -280,6 +316,15 @@ class Yukari {
});
}

save(callback) {
if(undefined === callback) callback = _emptyCallback;
if(this.$source === "new") {
return this.insert(callback);
} else {
return this.update(callback);
}
}

toJSON(old) {
const obj = old ? this.$origData : this;
const result = {};

0 comments on commit 2d30750

Please sign in to comment.