Skip to content
Merged
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
41 changes: 40 additions & 1 deletion src/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Schema = require("./Schema");
const Error = require("./Error");
const fs = require("fs");
const Util = require("./Util");

const _ = require("lodash");
/**
* Quick mongodb wrapper
*/
Expand Down Expand Up @@ -36,6 +36,7 @@ class Database extends Base {
async set(key, value) {
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
if (!Util.isValue(value)) throw new Error("Invalid value specified!", "ValueError");
if(!key.includes('.')) {
let raw = await this.schema.findOne({
ID: key
});
Expand All @@ -57,6 +58,32 @@ class Database extends Base {
});
return raw.data;
}
} else {
const subkey = key.split('.').shift();
let raw = await this.schema.findOne({
ID: subkey
});
if(!raw) {
const setValue = _.set({}, key.split('.').slice(1).join('.'), value);
let data = new this.schema({
ID: subkey,
data: setValue
});
await data.save()
.catch(e => {
return this.emit("error", e);
});
return data.data;
} else {
const setValue = _.set(raw, key.split('.').slice(1).join('.'), value);
raw.data = setValue;
await raw.save()
.catch(e => {
return this.emit("error", e);
});
return raw.data;
}
}
}

/**
Expand Down Expand Up @@ -100,12 +127,24 @@ class Database extends Base {
*/
async get(key) {
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
if(!key.includes('.')) {
let get = await this.schema.findOne({ ID: key })
.catch(e => {
return this.emit("error", e);
});
if (!get) return null;
return get.data;
} else {
const subkey = key.split('.').shift();
let get = await this.schema.findOne({ ID: subkey })
.catch(e => {
return this.emit("error", e);
});
if (!get) return null;
const data = key.split('.').slice(1).reduce((a, b) => a[b], get.data)
if(!data) return null;
return data;
}
}

/**
Expand Down