扫一扫分享
Rebridge是一个透明的Javascript-Redis桥。您可以使用它来创建自动同步到Redis数据库的JavaScript对象。
安装
npm install rebridge
同步,非阻塞使用
const Rebridge = require("rebridge");
const redis = require("redis");
const client = redis.createClient();
const db = new Rebridge(client, {
mode: "deasync"
});
db.users = [];
db.users.push({
username: "johndoe",
email: "johndoe@domain.com"
});
db.users.push({
username: "foobar",
email: "foobar@domain.com"
});
db.users.push({
username: "CapacitorSet",
email: "CapacitorSet@users.noreply.github.com"
});
console.log("Users:", db.users._value); // Prints the list of users
const [me] = db.users.filter(user => user.username === "CapacitorSet");
console.log("Me:", me); // Prints [{username: "CapacitorSet", email: "..."}]
client.quit();
异步使用
const Rebridge = require("rebridge");
const redis = require("redis");
const client = redis.createClient();
const db = new Rebridge(client);
db.users.set([])
.then(() => Promise.all([
db.users.push({
username: "johndoe",
email: "johndoe@domain.com"
}),
db.users.push({
username: "foobar",
email: "foobar@domain.com"
}),
db.users.push({
username: "CapacitorSet",
email: "CapacitorSet@users.noreply.github.com"
})
]))
.then(() => db.users._promise)
.then(arr => console.log("Users:", arr)) // Prints the list of users
.then(() => db.users.filter(user => user.username === "CapacitorSet"))
.then(([me]) => console.log("Me:", me)) // Prints [{username: "CapacitorSet", email: "..."}]
.then(() => client.quit())
.catch(err => console.log("An error occurred:", err));
Rebridge使用ES6代理对象,因此至少需要Node 6。
手机预览