Added relations to redis ORM

This commit is contained in:
2024-08-13 15:18:47 -04:00
parent 2a5ca3664a
commit b27e1b1617
20 changed files with 240 additions and 150 deletions
+69 -9
View File
@@ -5,14 +5,37 @@ const objValidate = require('../utils/object_validate');
const conf = require('../conf');
const client = createClient({});
client.connect()
client.connect();
function redisPrefix(key){
return `${conf.redis.prefix}${key}`;
}
class QueryHelper{
hisroty = []
constructor(orgin){
this.orgin = orgin
this.hisroty.push(orgin.constructor.name);
}
static isNotCycle(modleName, queryHelper){
if(queryHelper instanceof this){
if(queryHelper.hisroty.includes(modleName)){
return true;
}
queryHelper.hisroty.push(modleName)
}
}
}
class Table{
static redisClient = client;
static models = {}
static register = function(Model){
Model = Model || this;
this.models[Model.name] = Model;
}
constructor(data){
for(let key in data){
@@ -20,9 +43,8 @@ class Table{
}
}
static async get(index){
static async get(index, queryHelper){
try{
if(typeof index === 'object'){
index = index[this._key];
}
@@ -43,13 +65,37 @@ class Table{
// back to native values.
result = objValidate.parseFromString(this._keyMap, result);
return new this(result);
let instance = new this(result);
await instance.buildRelations(queryHelper);
return instance;
}catch(error){
throw error;
}
}
async buildRelations(queryHelper){
for(let [key, options] of Object.entries(this.constructor._keyMap)){
if(options.model){
let remoteModel = this.constructor.models[options.model]
try{
if(QueryHelper.isNotCycle(remoteModel.name, queryHelper)) continue;
if(options.rel === 'one'){
// console.log('relone:', this[key], queryHelper, remoteModel, await remoteModel.get(this[key], queryHelper || new QueryHelper(this)))
this[key] = await remoteModel.get(this[key] || this[options.localKey || this.constructor._key] , queryHelper || new QueryHelper(this))
}
if(options.rel === 'many'){
this[key] = await remoteModel.listDetail({
[options.remoteKey]: this[options.localKey || this.constructor._key],
},queryHelper || new QueryHelper(this))
}
}catch{}
}
}
}
static async exists(index){
if(typeof index === 'object'){
index = index[this._key];
@@ -73,12 +119,21 @@ class Table{
}
}
static async listDetail(){
static async listDetail(options, queryHelper){
// Return a list of the entries as instances.
let out = [];
for(let entry of await this.list()){
out.push(await this.get(entry));
let instance = await this.get(entry, arguments[arguments.length - 1]);
if(!options) out.push(instance);
let matchCount = 0;
for(let option in options){
if(instance[option] === options[option] && ++matchCount === Object.keys(options).length){
out.push(instance);
break;
}
}
}
return out;
@@ -213,8 +268,9 @@ class Table{
toJSON(){
let result = {};
for (const [key, keyProps] of Object.entries(this.constructor._keyMap)) {
if(!keyProps.isPrivate) result[key] = this[key];
for (const [key, value] of Object.entries(this)) {
if(this.constructor._keyMap[key] && this.constructor._keyMap[key].isPrivate) continue;
result[key] = value;
}
return result
@@ -222,6 +278,10 @@ class Table{
// return JSON.stringify(result);
}
toString(){
return this[this.constructor._key];
}
}