Skip to content

Commit

Permalink
added postgres support in index.js and created boilerplate file struc…
Browse files Browse the repository at this point in the history
…ture for create, send, and verify
  • Loading branch information
iangeckeler committed Jul 8, 2019
1 parent fd61334 commit f274582
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 23 deletions.
73 changes: 69 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,57 @@ const create = require("./functions/create.js");
const send = require("./functions/send.js");
const verify = require("./functions/verify.js");

// import mongoose functions
const mongooseCreate = require("./mongoose/create");
const mongooseSend = require("./mongoose/send");
const mongooseVerify = require("./mongoose/verify");

const userSchema = require("./mongoose/userSchema");

//import postgres functions
const generatePool = require("./postgres/configure");
const createTable = require("./postgres/createtable");
const postgresCreate = require("./postgres/create");
const postgresSend = require("./postgres/send");
const postgresVerify = require("./postgres/verify");

const connect = (AccSID, AuthToken, mongoURI = null) => {
return new Client(AccSID, AuthToken, mongoURI);
};

// EXAMPLE CONFIG OBJECT
// options = {
// appName: "",
// connectionURI: null,
// isPostgres: null
// }
class Client {
constructor(AccSID, AuthToken, mongoURI = null) {
constructor(
AccSID,
AuthToken,
options = {
appName: "",
connectionURI: null,
isPostgres: false
}
) {
if (typeof options === "string")
throw new Error(
"Options config must be an object, as specified in the documentation."
);
if (!options.hasOwnProperty("isPostgres")) {
options.isPostgres = false;
}
if (!options.hasOwnProperty("appName")) {
options.appName = "";
}
this.AccSID = AccSID;
this.AuthToken = AuthToken;
this.client = twilio(this.AccSID, this.AuthToken);
this.users = {};

if (mongoURI !== null) {
if (options.connectionURI !== null && options.isPostgres === false) {
mongoose
.connect(mongoURI)
.connect(options.connectionURI)
.then(db => {
console.log("Two Factor successfully connected to Mongo");
})
Expand All @@ -35,6 +66,40 @@ class Client {
this.create = mongooseCreate;
this.send = mongooseSend;
this.verify = mongooseVerify;
} else if (options.connectionURI !== null && options.isPostgres === true) {
// connect the database and assign a reference to it to our client object
const pgPool = generatePool(options.connectionURI);
let tableCreated = false;
this.pgConnect = function() {
return new Promise((resolve, reject) => {
// connection using created pool
pgPool.connect(function(err, database, done) {
if (err) reject(new Error("Error connecting to Postgres Pool."));
if (!database) {
throw new Error("Could not find Database at Connection URI.");
}
// if table not created yet, create it and modify closure to reflect
if (tableCreated === false) {
database
.query(createTable)
.then(res => {
resolve(database, done);
tableCreated = true;
})
.catch(e =>
reject(new Error("Error connecting to Postgres Pool."))
);
} else {
resolve(database, done);
}
});
});
};

// assign the postgres functions to our client object
this.create = postgresCreate;
this.send = postgresSend;
this.verify = postgresVerify;
} else {
this.create = create;
this.send = send;
Expand Down
Loading

0 comments on commit f274582

Please sign in to comment.