Skip to content

Commit

Permalink
feat: add some functions in model
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX committed Oct 15, 2016
1 parent 694676e commit 4ed5864
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const EventEmitter = require("eventemitter2").EventEmitter2;

const Field = require("./field");
const Query = require("./query");
const Yukari = require("./yukari");

class ToshihikoModel extends EventEmitter {
constructor(collectionName, toshihiko, schema, options) {
Expand Down Expand Up @@ -119,6 +120,12 @@ class ToshihikoModel extends EventEmitter {
return this.parent;
}

build(fields) {
const yukari = new Yukari(this, "new");
yukari.buildNewRow(fields);
return yukari;
}

where(condition) {
return (new Query(this)).where(condition);
}
Expand Down Expand Up @@ -173,6 +180,10 @@ class ToshihikoModel extends EventEmitter {
return (new Query(this)).delete(callback);
}

execute(sql, format, callback) {
return (new Query(this)).execute(sql, format, callback);
}

convertColumnToName(object) {
if(typeof object === "string") {
return this.columnToName[object];
Expand All @@ -195,6 +206,24 @@ class ToshihikoModel extends EventEmitter {

return undefined;
}

/**
* be compatible with 0.x
*/
getPrimaryKeysName() {
if(!this.primaryKeys.length) return [];
if(this.primaryKeys.length === 1) return this.primaryKeys[0].name;
return this.primaryKeys.map(pri => pri.name);
}

/**
* be compatible with 0.x
*/
getPrimaryKeysColumn() {
if(!this.primaryKeys.length) return [];
if(this.primaryKeys.length === 1) return this.primaryKeys[0].column;
return this.primaryKeys.map(pri => pri.column);
}
}

module.exports = ToshihikoModel;

0 comments on commit 4ed5864

Please sign in to comment.