Permalink
Browse files

feat: add some functions in model

  • Loading branch information...
XadillaX committed Oct 15, 2016
1 parent 694676e commit 4ed5864fb700a35e93ebcfac3da2cd0a9ea0f1b2
Showing with 29 additions and 0 deletions.
  1. +29 −0 lib/model.js
@@ -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) {
@@ -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);
}
@@ -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];
@@ -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.