1 回答
TA贡献1834条经验 获得超8个赞
完全按照你的建议去做。将代码的上半部分移动到db.js并公开db和collection使用exports:
数据库.js:
const {
Database
} = require("arangojs");
var db = new Database({
url: "http://localhost:8529"
});
const database_name = "cool_database";
db.useBasicAuth("username", "password123");
db.listDatabases()
.then(names => {
if (names.indexOf(database_name) > -1) {
db.useDatabase(database_name);
db.get();
} else {
db.createDatabase(database_name)
.then(() => {
db.useDatabase(database_name);
db.collection("my-collection").create();
});
}
});
exports.collection = db.collection("my-collection");
exports.db = db;
索引.js:
const {
db,
collection
} = require("./db");
const getJobFromQueue = () => {
return db.query({
query: "FOR el IN @@collection FILTER DATE_TIMESTAMP(el.email.sendAfter) < DATE_NOW() AND el.status != 'processed' AND el.status != 'failed' SORT el.email.sendAfter LIMIT 1 RETURN el",
bindVars: {
"@collection": "my-collection"
}
})
.then(cursor => cursor.all());
}
警告:
请记住,您的代码中存在潜在的竞争条件。在它们被初始化之前,您可能最终会使用dband 。collection
添加回答
举报