diff --git a/src/database/psql-entity-framework/entity-relationships.js b/src/database/psql-entity-framework/entity-relationships.js index c107ad1..28613ad 100644 --- a/src/database/psql-entity-framework/entity-relationships.js +++ b/src/database/psql-entity-framework/entity-relationships.js @@ -1,7 +1,7 @@ const Logger = require('../../logger.js'); const DataTypes = require('./types.js'); +const DataConstraints = require('./relationships_constraints.js'); const Model = require('./model.js'); -const { DataConstraints } = require('../database.js'); /** * In order to keep the models dynamic and flexible, we need to be able to add @@ -90,25 +90,50 @@ class PSQLObjectRelation { * @description Resolves all the dependancies for the models that have been added where properties weren't available when they were added */ resolveDepends() { - console.log(this.models) - for (const model in this.models) { - console.log(model) - // for (const property of model.properties) { - - // for (const constraint of property.constraints) { - // if (typeof constraint === 'object') { - // console.log('') - // if (constraint.fk.ref === DataTypes.INHERET) - // Logger.Debug(`Model ${model.name} has a dependancy on ${property.inherit}`); - // } - // } - - // } - } + for (const dummyModelI in this.dummyModels) + { + const dummyModel = this.dummyModels[dummyModelI]; + console.log(dummyModel); + + if (!this.models[dummyModel.name]) { + Logger.Error(`Model ${dummyModel.name} does not exist, cannot resolve dependancies`); + continue; + } + + const referenceModel = this.models[dummyModel.name]; + console.log(referenceModel); + + } + + + // for (const modelName in this.models) { + // const model = this.models[modelName]; + + // for (const propertyName in model.properties) { + // const property = model.properties[propertyName]; + + // for (const constraint of property.constraints) { + + // // If the constraint is a foreign key, resolve it + // if (constraint.fk) { + // const references = constraint.fk.ref; + // // Ignore if no resolution is needed + // if (references !== DataTypes.INHERET) continue; + + // Logger.Debug(`Resolving dependancy for ${modelName}.${propertyName}, references ${references}`); + // const fkModel = this.models[constraint.fk.ref]; + // } + // } + // } + // } } - // ONLY run this after all models are properly added + /** + * @function syncModels + * @description Syncs the models to the database + * ONLY run this after all models are properly added + */ async syncModels() { Logger.Database('ORM Syncing...'); this.resolveDepends(); diff --git a/src/database/psql-entity-framework/model.js b/src/database/psql-entity-framework/model.js index 76d4bab..023efb8 100644 --- a/src/database/psql-entity-framework/model.js +++ b/src/database/psql-entity-framework/model.js @@ -20,6 +20,11 @@ class Model { Logger.Database(`Model ${name} created, with properties: ${JSON.stringify(properties)}`); } + /** + * @function property + * @description Gets a property from the model + * @param {string} name - The name of the target property + */ property(name) { if (this.dummy) { diff --git a/src/logger.js b/src/logger.js index fa40b72..4fa3c90 100644 --- a/src/logger.js +++ b/src/logger.js @@ -41,7 +41,7 @@ module.exports.DEBUG_LOGS = 1; module.exports.INFO_LOGS = 2; module.exports.WARN_LOGS = 3; -module.exports.Middleware = function(origin, message) { +module.exports.Middleware = function(origin, ...message) { let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [MIDDLEWARE: ${origin}] ${message} \n`); if (LogLevel > 0) return; @@ -49,7 +49,7 @@ module.exports.Middleware = function(origin, message) { + clc.yellow(`MIDDLEWARE: ${origin}`) + '] ' + message); } -module.exports.Database = function(message) { +module.exports.Database = function(...message) { let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [POSTGRES: SQL] ${message} \n`); if (LogLevel > 0) return; @@ -57,7 +57,7 @@ module.exports.Database = function(message) { + clc.magentaBright(`POSTGRES: SQL`) + '] ' + message); } -module.exports.Debug = function(message) { +module.exports.Debug = function(...message) { let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [DEBUG] ${message} \n`); if (LogLevel > 1) return; @@ -72,7 +72,7 @@ module.exports.Ready = function() { + clc.rainbow('READY') + ']'); } -module.exports.Info = function(message) { +module.exports.Info = function(...message) { let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [INFO] ${message} \n`); if (LogLevel > 2) return; @@ -80,7 +80,7 @@ module.exports.Info = function(message) { + clc.green('INFO') + '] ' + message); } -module.exports.Warn = function(message) { +module.exports.Warn = function(...message) { let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [WARN] ${message} \n`); if (LogLevel > 3) return; @@ -88,14 +88,14 @@ module.exports.Warn = function(message) { + clc.yellow('WARN') + '] ' + message); } -module.exports.Error = function(message) { +module.exports.Error = function(...message) { let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [ERROR] ${message} \n`); console.error('[' + d.toLocaleString() + '] [' + clc.red('ERROR') + '] ' + message); } -module.exports.Panic = function(message) { +module.exports.Panic = function(...message) { let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [PANIC] ${message} \n`); console.error('[' + d.toLocaleString() + '] [' diff --git a/src/models/lego-brick.js b/src/models/lego-brick.js index 6f2377d..9ac55fe 100644 --- a/src/models/lego-brick.js +++ b/src/models/lego-brick.js @@ -12,7 +12,7 @@ async function init() { type: DataTypes.INTEGER, constraints: [ DataConstraints.FOREIGN_KEY_REF(ORM.model('catagory').property('id')) ] }, - date_released: DataTypes.DATE, + date_released: DataTypes.TIMESTAMP, dimenions_x: DataTypes.DECIMAL, dimenions_y: DataTypes.DECIMAL, dimenions_z: DataTypes.DECIMAL,