Skip to content

Instantly share code, notes, and snippets.

@Starstalker-awe
Created July 14, 2022 15:45
Show Gist options
  • Save Starstalker-awe/b34487d111a544fac956c99c2e7b132a to your computer and use it in GitHub Desktop.
Save Starstalker-awe/b34487d111a544fac956c99c2e7b132a to your computer and use it in GitHub Desktop.
Database Management using JSON...
{
"User": [
{
"id": "literally whatever you want the id to be",
"created": "2022-07-13T23:15:34.720Z",
"username": "Just a user!",
"password": "123#$%^&*()"
}
],
"Page": [
{
"id":"literally whatever you want the id to be again",
"created": "2022-07-13T23:15:34.720Z",
"page_name": "Home",
"html_filename": "index.html",
"homepage": true,
"created_increment": 1
}
]
}
import {argon2i} from 'argon2-ffi';
import * as crypto from 'crypto';
import {DB as _DB, ObjectManager, QuerySet, qsetProx} from 'main.js';
let DB = _DB;
function User({...kw} = {}){
User.objects = qsetProx(DB).User;
Object.assign(this, new ObjectManager("User", kw));
this.username = kw.username;
this.password = this.passHashed ? kw.password : argon2i.hash(kw.password, crypto.randomBytes(32));
this.passHashed = true;
this._schema = {
username: String,
password: String
}
}
function Page({...kw} = {}){
Page.objects = qsetProx(DB).Page;
const ObjMan = new ObjectManager("Page", kw);
Object.assign(this, ObjMan);
this.page_name = kw.page_name;
this.html_filename = kw.html_filename;
this.homepage = kw.homepage;
this.created_increment = kw.created_increment ?? Page.objects.count() + 1
this._schema = {
page_name: String,
html_filename: String,
homepage: Boolean,
created_increment: Number
}
this.save = function save(){
if(this.homepage){Page.objects.update({homepage: false})};
return ObjMan.save();
}
}
const models = {User, Page};
DB = Object.keys(models).reduce((a,k)=>(a[k] = new QuerySet(DB[k].map(async e=>await new models[k](e))), a), {});
import * as fs from 'fs';
import * as uuid from 'uuid';
import {dirname, join} from 'path';
import {fileURLToPath} from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
let DB = JSON.parse(fs.readFileSync(join(__dirname, 'db.json')));
function qsetProx(obj){return new Proxy(obj, {get(t, p){return new QuerySet(t[p])}})}
function QuerySet(...objs){
this.objs = objs.flat();
this.count = function count(){return this.objs.length}
this.add = function add(...os){this.objs.push(...os); return this}
this.remove = function remove(...os){os.map(e=>this.objs.map(o=>o.id).indexOf(e.id)).forEach(i=>this.objs.splice(i, 1)); return this}
this.filter = function filter(ffunc, ...args){ffunc ??= (obj, id)=>obj.id === id; return new QuerySet(this.objs.filter(object=>ffunc(object, ...args)))}
this.get = function get(filter, ...args){return this.filter(filter, ...args).objs[0]}
this.update = function update({...kw} = {}){this.objs.map(e=>Object.assign(e, kw)).forEach(e=>e.save()); return this}
}
function ObjectManager(dbcol, kw){
this._typeof = dbcol;
this.id = kw.id ?? uuid.v4();
this.created = kw.created ?? null;
this.modified = kw.modified ?? null;
this.update = function update({...kw} = {}){
Object.assign(this, kw);
return this.save();
}
this.save = function save(io=null){
this.created ??= new Date(), this.modified = new Date();
return ((io = DB[this._typeof].map(e=>e.id).indexOf(this.id)) > -1 ? DB[this._typeof][io] = JSON.stringify(this) : (DB[this._typeof].push(JSON.stringify(this))), this);
}
}
export {ObjectManager, QuerySet, qsetProx, DB};
@Starstalker-awe
Copy link
Author

Just as a note, _schema has no purpose other than readability...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment