简体   繁体   English

如何移动代码以从我的文件中设置 ut DB 和集合并只需要它?

[英]How to move the code to set ut DB and collection out from my file and just requre it?

So, let's say I have this code that works perfectly.所以,假设我有这个完美运行的代码。

 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(); }); } }); const collection = db.collection("my-collection"); 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()); }

But I want to move the top code out to another file and just require db and collection, how do I make that work?但是我想将最上面的代码移到另一个文件中,只需要 db 和集合,我该如何工作? Have been struggling to make it work for too long now.长期以来一直在努力使其工作。

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

just do exactly what you proposed.只是完全按照你的建议去做。 move the upper part of your code to db.js and expose db and collection using exports :将代码的上半部分移动到db.js并使用exports公开dbcollection

db.js:数据库.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;

index.js:索引.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());
}

WARNING :警告

keep in mind, there is a potential race condition in your code.请记住,您的代码中存在潜在的竞争条件。 you may end up using db and collection , before they hat been initialized.在初始化之前,您可能最终会使用dbcollection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需求文件中的Fauxton错误 - Fauxton errors in requre file 如何将此函数调用移出我的HTML代码 - How to move this function call out of my HTML code 如何使用当前代码为图像按钮设置活动 state 而不是仅聚焦 state? - How do I set the active state for image buttons instead of just focused state with my current code? 如何将用于搜索的内联脚本移到单独的 js 文件中? - How can I move the inline scripts for my search out into a separate js file? 如何将代码移出主 server.js 文件,但在应用程序启动时仍使用它? - How to move code out of the main server.js file, but still use it when the app starts? 如何防止我的 while 循环只打印出一个结果? - How can I prevent my while loop from printing out just one result? db.collection(...).set 不是 function - db.collection(…).set is not a function 如何实施自己的javascript代码(广告代码),以像buysellads一样从我的广告服务器中获取广告 - How to implement my own javascript code (ad tag code) to get ads from my ad server just like buysellads JScript 如何从我的 SQL 数据库中提取以节省重复我的代码? - How can JScript pull from my SQL DB to save repeating my code? 如何将带有Servlet的JavaScript代码移动到外部文件中? - How to move JavaScript code with Servlets into external file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM