javascriptnode.jsarangodbarangojs

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


Solution

  • just do exactly what you proposed. move the upper part of your code to db.js and expose dband 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.