0

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());
}
someone
  • 57
  • 4

1 Answers1

1

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.

Yevgeniy
  • 2,614
  • 1
  • 19
  • 26
  • Thanks! I don't know why this wouldn't work for me earlier :) Do you have any tip on how to fix the problem with the race condition? – someone Sep 10 '20 at 08:47
  • 1
    you are welcome. Instead of exporting `collection`, you can export a Promise providing you with `collection` once it is ready. You already do something similar using `db.listDatabases().then(...)`. Take a look at [this example](https://stackoverflow.com/a/49033851/804521) on how Promise works. – Yevgeniy Sep 10 '20 at 09:17