Skip to content

Instantly share code, notes, and snippets.

@chemzqm
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemzqm/9048224 to your computer and use it in GitHub Desktop.
Save chemzqm/9048224 to your computer and use it in GitHub Desktop.
var co = require('co');
var r = require('co-rethinkdb');
var conn;
r.getConnection = function* () {
return conn || (conn = yield r.connect({
host: 'chemzqm.me',
port: 28015,
db: 'blog',
authKey: 'formv!ne'
}))
}
var table = 'posts';
function* init() {
yield r.dbCreate('blog');
yield r.tableCreate('posts');
}
function* insert() {
var items = [];
for (var i = 0; i < 10; i++) {
items.push({
id: i,
name: 'item-' + i,
createAt: Date.now(),
updateAt: Date.now()
})
}
var res = yield r.table(table).insert(items);
return res;
}
function* clear() {
return yield r.table(table).delete();
}
function* findById() {
return yield r.table(table).get('095aeb05-e8e8-435d-b9ab-713f16483476');
}
function* findAll() {
var cursor = yield r.table(table);
//需要重新绑定cursor
return yield cursor.toArray.bind(cursor);
}
function* removeById() {
return yield r.table(table).get('095aeb05-e8e8-435d-b9ab-713f16483476').delete();
}
function* removeByName () {
return yield r.table(table).filter(r.row('name').eq('item-2')).delete();
}
function* update() {
return yield r.table(table).get('585a0785-0faf-426b-8e73-d138fca70f05').update({
name : 'item999'
})
}
function* range() {
return r.table(table).between(0, 1);
}
function* page() {
return yield r.table(table).orderBy('id').slice(3,6);
}
function* count() {
return yield r.table(table).count();
}
co(function* () {
yield clear;
yield insert;
yield r.table(table).get(0).delete();
//var arr = yield page;
//var res = yield range;
var c = yield count;
console.log(c);
conn.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment