Permalink
Browse files

feat: field class finished

  • Loading branch information...
XadillaX committed Aug 9, 2016
1 parent f0494a0 commit 3cd305bb85a51d3cdfc70a90e45f74155284ce7e
Showing with 245 additions and 9 deletions.
  1. +1 −0 lib/adapters/mysql.js
  2. +81 −0 lib/field.js
  3. +7 −7 lib/field_type/index.js
  4. +85 −0 lib/model.js
  5. +4 −2 package.json
  6. +59 −0 test/field.js
  7. +8 −0 test/toshihiko.js
@@ -118,6 +118,7 @@ class MySQLAdapter extends Adapter {
sql = this.format(sql, params);
}

debug(`executing sql 【${sql}`);
conn.query(sql, function(err, rows) {
conn.release();
callback(err, rows);
@@ -0,0 +1,81 @@
/**
* XadillaX created at 2016-08-09 13:04:53 With ♥
*
* Copyright (c) 2016 Souche.com, all rights
* reserved.
*/
"use strict";

const debug = require("debug")("toshihiko:field");
const otrans = require("otrans");

const FieldType = require("./field_type");

class ToshihikoField {
constructor(options) {
if(!options.name) {
throw new Error("no field name specified.");
}

Object.defineProperty(this, "options", {
configurable: false,
writable: false,
enumerable: false,
value: otrans.toCamel(options)
});

options = this.options;

let type = options.type;
if(!type || typeof type.restore !== "function" || typeof type.parse !== "function") {
type = FieldType.String;
}

let validators = [];
if(typeof options.validators === "function") {
options.validators = [ options.validators ];
}
if(Array.isArray(options.validators)) {
validators = options.validators;
}

Object.defineProperties(this, {
name: { value: options.name, enumerable: true },
column: { value: options.column || options.name, enumerable: true },
type: { value: type, enumerable: true },
validators: { value: validators, enumerable: true },
allowNull: { value: !!options.allowNull, enumerable: true },

primaryKey: { value: !!options.primaryKey, enumerable: true },
autoIncrement: {
value: (options.autoIncrement !== undefined && !!options.autoIncrement),
enumerable: true
},

default: {
value: (options.defaultValue === undefined) ? type.defaultValue : options.defaultValue,
enumerable: true
},

equal: {
value: (typeof type.equal === "function") ? type.equal.bind(type) : FieldType.$equal
}
});

debug(`${options.name} created.`, this);
}

get defaultValue() {
return this.default;
}

restore(value) {
return this.type.restore(value);
}

parse(value) {
return this.type.parse(value);
}
}

module.exports = ToshihikoField;
@@ -14,14 +14,14 @@ var Integer = require("./integer");
var Json = require("./json");

module.exports = {
String : _String,
Boolean : _Boolean,
Integer : Integer,
Float : Float,
Json : Json,
Datetime : Datetime,
String: _String,
Boolean: _Boolean,
Integer: Integer,
Float: Float,
Json: Json,
Datetime: Datetime,

$equal : function(a, b) {
$equal: function(a, b) {
return a === b;
}
};
@@ -0,0 +1,85 @@
/**
* XadillaX created at 2016-08-09 11:53:15 With ♥
*
* Copyright (c) 2016 Souche.com, all rights
* reserved.
*/
"use strict";

const EventEmitter = require("eventemitter2").EventEmitter2;

const Field = require("./field");

class ToshihikoModel extends EventEmitter {
constructor(collectionName, toshihiko, schema, options) {
this.ai = null;

Object.defineProperties(this, {
primaryKeys: {
enumerable: true,
value: []
},
name: {
enumerable: true,
value: collectionName
},
parent: {
value: toshihiko
},
originalSchema: {
value: schema
},
schema: {
enumerable: true,
value: schema.map(options => {
const field = new Field(options);
if(field.primaryKey) {
this.primaryKeys.push(field);
}

if(field.autoIncrement) this.ai = field;

return field;
})
},
options: {
value: options || {}
}
});

if(!this.primaryKeys.length) {
this.emit("log", `!!! WARNING: YOU'D BETTER ADD PRIMARY KEY(S) IN MODEL ${this.name} !!!`);
}

// specify cache
const Toshihiko = require("./toshihiko");
if(this.options.cache) {
// create cache object if specified
Object.defineProperty(this, "cache", {
enumerable: true,
value: Toshihiko.createCache(this.options.cache)
});
} else if(this.parent.cache && this.options.cache === undefined) {
// use global cache object if NOT specified
Object.defineProperty(this, "cache", {
enumerable: true,
value: this.parent.cache
});
} else {
// use NO cache
Object.defineProperty(this, "cache", {
enumerable: true,
value: null
});
}
}

/**
* be compatible with 0.x
*/
get toshihiko() {
return this.parent;
}
}

module.exports = ToshihikoModel;
@@ -11,7 +11,10 @@
"config.util": "^1.20.2-1",
"debug": "^2.2.0",
"eventemitter2": "^2.1.0",
"lodash": "4.14.1"
"fbbk-json": "^1.0.0",
"lodash": "4.14.1",
"moment": "^2.14.1",
"otrans": "^1.0.0"
},
"devDependencies": {
"conventional-changelog-cli": "1.2.0",
@@ -24,7 +27,6 @@
"mocha": "3.0.1",
"mysql": "^2.11.1",
"mysql2": "^1.0.0-rc.9",
"otrans": "^1.0.0",
"should": "10.0.0",
"sync-runner": "^0.1.5",
"validate-commit-msg": "2.6.1"
@@ -0,0 +1,59 @@
/**
* XadillaX created at 2016-08-09 13:20:53 With ♥
*
* Copyright (c) 2016 Souche.com, all rights
* reserved.
*/
"use strict";

const Field = require("../lib/field");
const Type = require("../lib/field_type");

describe("🐣 field", function() {
describe("create", function() {
it("should create json object", function() {
const field = new Field({
name: "foo",
column: "bar",
type: Type.Json,
primaryKey: true,
autoIncrement: true
});

field.name.should.equal("foo");
field.column.should.equal("bar");
field.type.should.deepEqual(Type.Json);
field.validators.should.deepEqual([]);
field.allowNull.should.equal(false);
field.primaryKey.should.equal(true);
field.autoIncrement.should.equal(true);
field.defaultValue.should.deepEqual({});
});

it("should have some default options", function() {
const field = new Field({
name: "foo"
});

field.name.should.equal("foo");
field.column.should.equal("foo");
field.type.should.deepEqual(Type.String);
field.validators.should.deepEqual([]);
field.allowNull.should.equal(false);
field.primaryKey.should.equal(false);
field.autoIncrement.should.equal(false);
field.defaultValue.should.equal("");
});

it("should have validator(s)", function() {
const v1 = function() {};
const v2 = function() {};

let field = new Field({ name: "foo", validators: v1 });
field.validators[0].should.equal(v1);
field = new Field({ name: "foo", validators: [ v2, v1 ] });
field.validators[0].should.equal(v2);
field.validators[1].should.equal(v1);
});
});
});
@@ -0,0 +1,8 @@
/**
* XadillaX created at 2016-08-09 10:05:39 With ♥
*
* Copyright (c) 2016 Souche.com, all rights
* reserved.
*/
"use strict";

0 comments on commit 3cd305b

Please sign in to comment.