在 node.js 中开始思考
thinky 是 RethinkDB 的轻量级 node.js ORM。
首先,你需要在服务器上运行 RethinkDB。
然后将 thinky.io npm 包安装到你的项目中。
npm install --save thinky
现在将 thinky 导入到你的模型文件中。
const thinky = require('thinky)();
const type = thinky.type
接下来创建一个模型。
const User = thinky.createModel('User' {
email:type.string(),
password: type.string()
});
你现在可以创建并保存用户。
const user = new User({
email: 'test@email.com',
password: 'password'
});
user.save();
将为用户提供唯一 ID。
你可以将 promise 链接到 save 函数以使用生成的用户,或捕获错误。
user.save()
.then(function(result) {
console.log(result);
})
.catch(function(error) {
console.log(error);
});
使用过滤器等功能查找你的用户,并使用 promises 来使用结果。
User.filter({ email: 'test@email.com }).run()
.then(function(result) {
console.log(result);
})
.catch(function(error) {
console.log(error);
});
或者通过唯一 ID 搜索特定用户。
User.get(id)
.then(function(user) {
console.log(user);
})
.catch(function(error) {
console.log(error);
});