Convert backend code to ES modules

Bundling code now with Rollup for its flat bundling and tree shaking
support. Front end code still goes through Webpack for browser support.
This commit is contained in:
Zaid Abdulla committed 2019-02-04 23:47:54 -08:00
1 parent 824639976f
commit 1e41c72d60
62 files changed
+718 -898

No files matched your search

+10 -18
View File
@@ -18,7 +18,8 @@
*/
"use strict";
const config = require("./config");
import { MongoClient } from "mongodb";
import * as config from "./config";
const MAX_CACHE_TTL = config.get("MAX_CACHE_TTL");
@@ -26,11 +27,10 @@ let mongoClient;
let mongoCollection;
let mongoTimeOffset = 0;
function connect(callback) {
const mongodb = require("mongodb");
export function connect(callback) {
const MONGODB_CONNECTION_URL = config.get("MONGODB_CONNECTION_URL");
mongodb.MongoClient.connect(
MongoClient.connect(
MONGODB_CONNECTION_URL,
{ useNewUrlParser: true },
(err, client) => {
@@ -51,11 +51,11 @@ function connect(callback) {
);
}
function disconnect() {
export function disconnect() {
if (mongoClient) mongoClient.close();
}
function get(key, callback) {
export function get(key, callback) {
const expire = new Date(Date.now() - mongoTimeOffset);
if (Array.isArray(key)) {
mongoCollection.find({ _id: { $in: key } }).toArray((err, res) => {
@@ -83,13 +83,13 @@ function get(key, callback) {
}
}
function del(key, callback) {
export function del(key, callback) {
if (Array.isArray(key))
mongoCollection.deleteMany({ _id: { $in: key } }, callback);
else mongoCollection.deleteOne({ _id: key }, callback);
}
function set(key, value, ttl, callback) {
export function set(key, value, ttl, callback) {
if (!callback && typeof v === "function") {
callback = ttl;
ttl = null;
@@ -106,7 +106,7 @@ function set(key, value, ttl, callback) {
);
}
function pop(key, callback) {
export function pop(key, callback) {
mongoCollection.findOneAndDelete({ _id: key }, (err, res) => {
if (err || !res["value"]) return void callback(err);
@@ -117,7 +117,7 @@ function pop(key, callback) {
});
}
function lock(lockName, ttl, callback) {
export function lock(lockName, ttl, callback) {
const token = Math.random()
.toString(36)
.slice(2);
@@ -159,11 +159,3 @@ function lock(lockName, ttl, callback) {
}
);
}
exports.connect = connect;
exports.disconnect = disconnect;
exports.get = get;
exports.set = set;
exports.del = del;
exports.pop = pop;
exports.lock = lock;