【发布时间】:2014-10-12 05:18:57
【问题描述】:
我是 ORM 技术的新手,并且在 mysql 中使用 light-orm,我想知道如何使用 light-orm 将任何新行插入到数据库中,或者向我推荐 nodejs 和 mysql 的最佳 ORM。
提前致谢。
【问题讨论】:
我是 ORM 技术的新手,并且在 mysql 中使用 light-orm,我想知道如何使用 light-orm 将任何新行插入到数据库中,或者向我推荐 nodejs 和 mysql 的最佳 ORM。
提前致谢。
【问题讨论】:
完全有效的问题。在没有 ORM 经验的情况下,我试图找到答案。这花了我 30 分钟,为了找到它,我不得不深入研究源代码本身,因为文档中没有任何信息。 (之后会提出拉取请求)。
model.create(function(err, model) {});
这很好,但是我们如何获得你问的model 对象?
这是Collection object中可用方法的列表:
/**
* Create model
* @param data Attributes, that will be setted during construction
*/
createModel(data: {});
/**
* Create model
* @param {boolean} add True, if created model should be added to models list
*/
createModel(add: boolean);
/**
* Create model
* @param data Attributes, that will be setted during construction
* @param {boolean} add True, if created model should be added to models list
*/
createModel(data: {}, add: boolean);
createModel(options?: any, add?: boolean) : Model {
假设您有一个表 users,其中包含字段:login, password。
简单示例(未测试)应如下所示:
var mysql = require('mysql'),
lightOrm = require('light-orm');
lightOrm.driver = mysql.createConnection(require('./connection.json'));
lightOrm.driver.connect();
var UsersCollection = new lightOrm.Collection('users');
var model = UsersCollection.createModel({login: "mylogin", password: "mypass"}, true);
model.create(); // Write the changes to database
总体而言,light-orm 看起来不错,如果您觉得舒服 - 请继续使用它。
【讨论】:
model.create(cb);。
model.create();?