Created
June 15, 2022 14:38
-
-
Save teknixstuff/1eece720dc817ce135606624955ede4c to your computer and use it in GitHub Desktop.
JS database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Database(...cols) { | |
const arrayEquals = (a1,a2)=>a1.every(e=>a2.includes(e))&&a2.every(e=>a1.includes(e)); | |
var databaseRaw = []; | |
var database = new Proxy(databaseRaw,{ | |
set(obj, prop, val) { | |
if (!(Number.isInteger(Number(prop)) && Number(prop) > 0)) { | |
return Reflect.set(obj, prop, val); | |
} | |
if (typeof val !== 'object') { | |
throw new TypeError('Database entry must be an object.'); | |
} | |
if (!arrayEquals(Object.keys(val),database.cols)) { | |
throw new TypeError('Database entry must have keys that match the database\'s columns.'); | |
} | |
return Reflect.set(obj, prop, val); | |
} | |
}); | |
Object.defineProperty(database,'raw',{ | |
configurable: false, | |
enumerable: false, | |
writable: false, | |
value: databaseRaw | |
}); | |
Object.defineProperty(database,'cols',{ | |
configurable: false, | |
enumerable: false, | |
writable: false, | |
value: Object.freeze(cols) | |
}); | |
return database; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment