在 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);
    });