简体   繁体   中英

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? 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:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM