Added relations to redis models

This commit is contained in:
2024-08-15 18:49:42 -04:00
parent 9cf9c76c12
commit c1cfa8bb82
2 changed files with 49 additions and 9 deletions
+29 -9
View File
@@ -21,22 +21,37 @@ function processKeys(map, data, partial){
for(let key of Object.keys(map)){ for(let key of Object.keys(map)){
// Do not require "isRequired" fields for partial validation, useful for
// updates.
if(!map[key].always && partial && !data.hasOwnProperty(key)) continue; if(!map[key].always && partial && !data.hasOwnProperty(key)) continue;
// Make sure required keys are present
if(!partial && map[key].isRequired && !data.hasOwnProperty(key)){ if(!partial && map[key].isRequired && !data.hasOwnProperty(key)){
errors.push({key, message:`${key} is required.`}); errors.push({key, message:`${key} is required.`});
continue; continue;
} }
// Remove undefined keys unless they have a default option or are a
// relation
if(data[key] === undefined){
console.log('undefined key:', key, data[key], map[key], map[key].default);
if(!map[key].default){
if(map[key].model && !map[key].type) continue;
continue;
}
}
// Check the type of the key
if(data.hasOwnProperty(key) && map[key].type && typeof(data[key]) !== map[key].type){ if(data.hasOwnProperty(key) && map[key].type && typeof(data[key]) !== map[key].type){
errors.push({key, message:`${key} is not ${map[key].type} type.`}); errors.push({key, message:`${key} is not ${map[key].type} type.`});
continue; continue;
} }
// console.log(key, data[key], map[key].default, data.hasOwnProperty(key) && data[key] !== undefined ? data[key] : returnOrCall(map[key].default)) // Add the key to the process object to be returned and set any default
// if the key is blank
out[key] = data.hasOwnProperty(key) && data[key] !== undefined ? data[key] : returnOrCall(map[key].default); out[key] = data.hasOwnProperty(key) && data[key] !== undefined ? data[key] : returnOrCall(map[key].default);
// Check for type specific validations, ie: string length
if(data.hasOwnProperty(key) && process_type[map[key].type]){ if(data.hasOwnProperty(key) && process_type[map[key].type]){
let typeError = process_type[map[key].type](map[key], data[key]); let typeError = process_type[map[key].type](map[key], data[key]);
if(typeError){ if(typeError){
@@ -47,8 +62,9 @@ function processKeys(map, data, partial){
} }
} }
// Check for errors, throw validation error if any
if(errors.length !== 0){ if(errors.length !== 0){
throw new ObjectValidateError(errors); throw ObjectValidateError(errors);
return {__errors__: errors}; return {__errors__: errors};
} }
@@ -56,6 +72,7 @@ function processKeys(map, data, partial){
} }
function parseFromString(map, data){ function parseFromString(map, data){
// Use the key maps data type to return string values to native
let types = { let types = {
boolean: function(value){ return value === 'false' ? false : true }, boolean: function(value){ return value === 'false' ? false : true },
number: Number, number: Number,
@@ -80,11 +97,14 @@ function parseToString(data){
return (types[typeof(data)] || String)(data); return (types[typeof(data)] || String)(data);
} }
function ObjectValidateError(message){ function ObjectValidateError(keys, message){
this.name = 'ObjectValidateError'; let error = new Error('ObjectValidateError')
this.message = (message || {}); error.name = "ObjectValidateError"
this.keys = (message || {}) error.message = message || `Invalid Keys: ${message}`
this.status = 422; error.keys = (keys || {});
error.status = 422;
return error
} }
ObjectValidateError.prototype = Error.prototype; ObjectValidateError.prototype = Error.prototype;
+20
View File
@@ -29,6 +29,22 @@ class QueryHelper{
} }
class Table{ class Table{
static errors = {
ObjectValidateError: objValidate.ObjectValidateError,
EntryNameUsed: ()=>{
let error = new Error('EntryNameUsed');
error.name = 'EntryNameUsed';
error.message = `${this.prototype.constructor.name}:${data[this._key]} already exists`;
error.keys = [{
key: this._key,
message: `${this.prototype.constructor.name}:${data[this._key]} already exists`
}]
error.status = 409;
return error;
}
}
static redisClient = client; static redisClient = client;
static models = {} static models = {}
@@ -139,6 +155,10 @@ class Table{
return out; return out;
} }
static findall(...args){
return this.listDetail(...args);
}
static async create(data){ static async create(data){
// Add a entry to this redis table. // Add a entry to this redis table.
try{ try{