Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/easydb.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"databases": {
"default": {
"uuid": "6091c45a-2ca9-4808-bb8d-1dbf42122818",
"token": "5285d22c-9d1c-4c3a-8eee-ad67e92f1e24"
},
"alternate": {
"uuid": "9785cd23-6a42-4659-8518-713a99c77abb",
"token": "750f1876-f8e4-45fe-9f97-87805b893980"
}
},
"url": "https://app.easydb.io"
}
16 changes: 10 additions & 6 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
const easyDB = require("easydb-io");
// import uuid from "uuid/v4";

let db = easyDB({
database: "e9189a59-68b3-4cfb-892d-3527c0cf6bac",
token: "64eea1c5f3d84401b554f60dda46491d"
// url: "http://localhost:8080"
});
// let db = easyDB({
// database: "e9189a59-68b3-4cfb-892d-3527c0cf6bac",
// token: "64eea1c5f3d84401b554f60dda46491d"
// // url: "http://localhost:8080"
// });

const runner = async () => {
//while (true) {
// db.Put(uuid(), { [uuid()]: uuid() });
// db.delete("e22975eb-1506-4881-b1ea-efaad7d44b8d");
const data = await db.list((err, data) => console.log(data));
//let db = easyDB.connect("alternate");
// console.log("IT IS", db.connect);
// db.put("hello", "world");
//db.put("hello", "Jake");
const data = await easyDB.list((err, data) => console.log(data));
console.log(data);
//}
};
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
"name": "easydb-io",
"version": "2.0.0",
"description": "EasyDB JavaScript Client",
"main": "bundle.js",
"main": "./src/index.js",
"scripts": {
"build": "rollup -c"
},
"author": "jakecooper",
"license": "MIT",
"dependencies": {
"axios": "^0.19.0"
"axios": "^0.19.0",
"fs": "0.0.1-security",
"uuid": "^3.3.3"
},
"repository": {
"type": "git",
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default {
format: "umd",
globals: {
axios: "Axios"
}
},
name: "easydb"
},
external: ["axios"] // <-- suppresses the warning
};
172 changes: 124 additions & 48 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
import { get, post, delete as del } from "axios";
import {
get as AxiosGet,
post as AxiosPost,
delete as AxiosDelete
} from "axios";
import fs from "fs";
import uuid from "uuid/v4";

const BASE_URL = "https://app.easydb.io";
const CONFIG_FILE = "./easydb.config.json";

const isNode = () => {
return typeof process !== "undefined" && process.release.name === "node";
};

if (isNode && !fs.existsSync(CONFIG_FILE)) {
fs.writeFileSync(
CONFIG_FILE,
JSON.stringify({ uuid: uuid(), token: uuid(), url: BASE_URL }, null, 2)
);
}

const coherseCallback = (axiosPromise, callback) => {
let p = new Promise((resolve, reject) => {
Expand All @@ -22,56 +42,112 @@ const coherseCallback = (axiosPromise, callback) => {
return callback ? null : p;
};

const EasyDB = config => {
let { database, token, url = `https://app.easydb.io` } = config;
let DEFAULT_DB;

return {
get: (key, cb) => {
return coherseCallback(
get(`${url}/database/${database}/${key}`, {
headers: {
token
}
}),
cb
);
},
put: (key, value, cb) => {
return coherseCallback(
post(
`${url}/database/${database}/${key}`,
{
value
},
{
headers: {
"Content-Type": "application/json",
token
}
}
),
cb
);
},
delete: (key, cb) => {
return coherseCallback(
del(`${url}/database/${database}/${key}`, {
if (isNode && fs.existsSync(CONFIG_FILE)) {
let data = JSON.parse(fs.readFileSync(CONFIG_FILE));
let { uuid, token } = data.databases.default;
DEFAULT_DB = { uuid, token, url: data.url };
}

const instanceActions = {
get: (DB, key, cb) => {
return coherseCallback(
AxiosGet(`${DB.url}/database/${DB.uuid}/${key}`, {
headers: {
token: DB.token
}
}),
cb
);
},
put: (DB, key, value, cb) => {
return coherseCallback(
AxiosPost(
`${DB.url}/database/${DB.uuid}/${key}`,
{
value
},
{
headers: {
token
"Content-Type": "application/json",
token: DB.token
}
}),
cb
);
},
list: cb => {
return coherseCallback(
get(`${url}/database/${database}`, {
headers: { token }
}),
cb
);
}
),
cb
);
},
delete: (DB, key, cb) => {
return coherseCallback(
AxiosDelete(`${DB.url}/database/${DB.uuid}/${key}`, {
headers: {
token: DB.token
}
}),
cb
);
},
list: (DB, cb) => {
return coherseCallback(
AxiosGet(`${DB.url}/database/${DB.uuid}`, {
headers: { token: DB.token }
}),
cb
);
}
};

const constructActions = database => {
if (!database) {
throw "Database uninitialized. Init, connect, or use an easydb.config.json";
}
return Object.keys(instanceActions).reduce((acc, action) => {
acc[action] = (...params) => {
instanceActions[action](database, ...params);
};
return acc;
}, {});
};

const connect = params => {
let uuid, token;
let configData = JSON.parse(fs.readFileSync(CONFIG_FILE));

if (typeof params === "string" || params == null) {
// Initialize from config
if (!params) {
// Nothing provided, connect to default
params = "default";
}
if (!configData.databases[params]) {
let validChoices = Object.keys(configData.databases)
.map(s => ` - ${s}`)
.join("\n");
throw `Could not find database ${params} in easydb.config.json\n Valid databases are: \n${validChoices}`;
}
};
let database = configData.databases[params];
uuid = database.uuid;
token = database.token;
} else if (typeof params === "object") {
// Initialize from object
uuid = params.uuid;
token = params.token;
} else {
throw "Invalid parameters passed to connect(Object | String)";
}
if (!uuid || !token) {
throw "uuid and token must be provided";
}
let currentDB = { uuid, token, url: configData.url };
return constructActions(currentDB);
};

module.exports = EasyDB;
let actions = {
connect,
...constructActions(DEFAULT_DB)
};

console.log(actions);

module.exports = actions;