Skip to content

Commit

Permalink
Merge pull request #12 from richardo2016/dev
Browse files Browse the repository at this point in the history
Support Use Original Request in model's `functions`
  • Loading branch information
xicilion authored Apr 28, 2018
2 parents 33fb041 + c76ba27 commit 2fc136c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const mq = require('mq');
const classes = require('./classes');
const Db = require('./db');
const setupDb = require('./db');
const diagram = require('./utils/diagram');

class App extends mq.Routing {
constructor(url, opts) {
super();

this.db = Db(this, url, opts);
this.db = setupDb(this, url, opts);
classes.bind(this);
this.diagram = diagram;
}
Expand Down
9 changes: 5 additions & 4 deletions lib/classes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ exports.bind = (app) => {

var _req = {
session: req.session,
query: req.query.toJSON()
query: req.query.toJSON(),
request: req
};

var where = _req.query.where;
Expand Down Expand Up @@ -106,8 +107,8 @@ exports.bind = (app) => {
_extend.bind(_, app);

app.post('/:classname/:func', (req, classname, func) => {
_(req, classname, (req, db, cls, data) => {
if (!check_acl(req.session, func, cls.ACL))
_(req, classname, (_req, db, cls, data) => {
if (!check_acl(_req.session, func, cls.ACL))
return err_info(4030001, {}, cls.cid);

const f = cls.functions[func];
Expand All @@ -118,7 +119,7 @@ exports.bind = (app) => {
}, cls.cid);

try {
return f(req, data);
return f(_req, data);
} catch (e) {
console.error(e.stack);
return err_info(5000002, {
Expand Down
22 changes: 11 additions & 11 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = (app, url, opts) => {
var _define = db.define;
var cls_id = 1;

db.define = function (name, properties, opts) {
db.define = function (name, properties, orm_define_opts) {
var old_properties = properties;

if (use_uuid)
Expand All @@ -46,7 +46,7 @@ module.exports = (app, url, opts) => {
properties.updatedAt.type = 'date';
properties.updatedAt.time = true; //change the field type to datetime in MySQL

var m = _define.call(this, name, properties, opts);
var m = _define.call(this, name, properties, orm_define_opts);
m.cid = cls_id++;

Object.defineProperty(m, 'model_name', {
Expand All @@ -56,15 +56,15 @@ module.exports = (app, url, opts) => {
var _beforeCreate;
var _beforeSave;

if (opts !== undefined) {
if (opts.hooks !== undefined) {
_beforeCreate = opts.hooks.beforeCreate;
_beforeSave = opts.hooks.beforeSave;
if (orm_define_opts !== undefined) {
if (orm_define_opts.hooks !== undefined) {
_beforeCreate = orm_define_opts.hooks.beforeCreate;
_beforeSave = orm_define_opts.hooks.beforeSave;
}

m.functions = opts.functions;
m.ACL = opts.ACL;
m.OACL = opts.OACL;
m.functions = orm_define_opts.functions;
m.ACL = orm_define_opts.ACL;
m.OACL = orm_define_opts.OACL;
}

if (m.ACL === undefined)
Expand Down Expand Up @@ -112,13 +112,13 @@ module.exports = (app, url, opts) => {
m.extends = {};

var _hasOne = m.hasOne;
m.hasOne = function (name, model, opts) {
m.hasOne = function (name, model, orm_hasOne_opts) {
m.extends[name] = {
type: 'hasOne',
model: model
};

if (opts !== undefined && opts.reversed)
if (orm_hasOne_opts !== undefined && orm_hasOne_opts.reversed)
m.extends[name].reversed = true;

return _hasOne.apply(this, slice.call(arguments));
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/diagram.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = function () {
var Viz = require('viz.js');
var Viz = require('viz.js');

module.exports = function () {
var models = [];
var exts = [];

Expand Down
1 change: 1 addition & 0 deletions todo.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
* 重写用例,用例独立运行, uuid
- 修正 uuid 模式下, 关联模型用 id 关联时, 关联字段在 mysql 中分配长度过小的问题.

* reverse 测试
- hasOne 测试
Expand Down

0 comments on commit 2fc136c

Please sign in to comment.