diff --git a/jsdoc/Database.html b/jsdoc/Database.html index 68330f0..da415ce 100644 --- a/jsdoc/Database.html +++ b/jsdoc/Database.html @@ -204,13 +204,13 @@
diff --git a/jsdoc/Model.html b/jsdoc/Model.html index e83d91a..2a43bcc 100644 --- a/jsdoc/Model.html +++ b/jsdoc/Model.html @@ -28,7 +28,7 @@
-

Model()

+

Model(name, properties, dummy)

The Model class is used to create a model instance.

@@ -45,7 +45,7 @@ -

new Model()

+

new Model(name, properties, dummy)

@@ -60,6 +60,101 @@ +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + +

The name of the model

properties + + +object + + + +

The properties of the model

dummy + + +boolean + + + +

Whether or not the model is a dummy model

+ + @@ -93,7 +188,7 @@
Source:
@@ -155,13 +250,13 @@
diff --git a/jsdoc/entity-relationships.js.html b/jsdoc/entity-relationships.js.html index 2e9a9c1..6ef4075 100644 --- a/jsdoc/entity-relationships.js.html +++ b/jsdoc/entity-relationships.js.html @@ -28,9 +28,9 @@
const Logger = require('../../logger.js');
 const DataTypes = require('./types.js');
+const DataConstraints = require('./relationships_constraints.js');
 const Model = require('./model.js');
 
-
 /**
  * In order to keep the models dynamic and flexible, we need to be able to add
  * new models to the database in whatever order the developer wants too. Without
@@ -50,28 +50,133 @@ class PSQLObjectRelation {
         Logger.Database('ORM Loading...');
         this.connection = psqlConnection;;
         this.models = [];
+        // dummyModels are for models that have requested a model that doesn't exist
+        // the model that doesn't exist will be added here, and once it is added, the
+        // dummy dependancy will be resolved
+        this.dummyModels = [];
     }
 
+    /**
+     * @function model
+     * @description Gets a model from the database stash
+     * @param {string} modelName - The name of the target model
+     */
     model(modelName) {
-        return { }
+        // TODO: Resolve the dependancy if it dosen't exist and make a note of it
+        if (!this.models[modelName]) { 
+            Logger.Database(`Model ${modelName} does not exist, adding to dummyModels`);
+
+            if (this.dummyModels[modelName]) {
+                return this.dummyModels[modelName];
+            }
+
+            const dummy = new Model(modelName, {}, true);
+            this.dummyModels[modelName] = dummy;
+            return dummy;
+        }
+        return this.models[modelName];
     }
 
-    addModel(name, model, constraints) {
+    /**
+     * @function addModel
+     * @description Adds a model to the database stash
+     * @param {string} name 
+     * @param {object} model
+     */
+    addModel(name, model) {
         Logger.Database(`ORM Adding ${name}`);
 
+        if (this.models[name]) {
+            Logger.Error(`Redefinition of model ${name}, ignoring.`);
+            return;
+        }
+
         const keys = Object.keys(model);
-        this.models[name] = new Model;
 
+        /**
+         * Make sure that every property has a type and a conatraints array
+         * If not, add it
+         * The structure should always look like:
+         * property: {
+         *   type: DataTypes.VARCHAR(50),
+         *   constraints: [ DataConstraints.PRIMARY_KEY, DataConstraints.NOT_NULL ]
+         * }
+         */
+        keys.forEach(key => {
+            if (typeof model[key] != 'object') {
+                const type = model[key];
+                model[key] = {
+                    type: type,
+                    constraints: []
+                };
+            }
+            if (!model[key].constraints) {
+                model[key].constraints = [];
+            }
+        });
+
+        this.models[name] = new Model(name, model);
     }
 
+    /**
+     * @function resolveDependants
+     * @description Resolves all the dependancies for the models that have been added where properties weren't available when they were added
+     */
+    // TODO: Make this more maintainable
     resolveDepends() {
-        
+        for (const dummyModelName in this.dummyModels)
+        {
+            const dummyModel = this.dummyModels[dummyModelName];
+
+            if (!this.models[dummyModel.name]) {
+                Logger.Error(`Model ${dummyModel.name} does not exist, cannot resolve dependancies`);
+                continue;
+            }
+
+            for (const propertyName in dummyModel.properties)
+            {
+                const property = dummyModel.properties[propertyName];
+
+                if (property.type !== DataTypes.INHERET) {
+                    continue;
+                }
+
+                if (property.referers.length === 0 || property.referers[0] === '') {
+                    Logger.Error(`Model ${dummyModel.name} has a property ${propertyName} with no referers`);
+                    continue;
+                }
+
+                for (const referer of property.referers) {
+                    if (!this.models[referer]) {
+                        Logger.Error(`Model ${dummyModel.name} has a property ${propertyName} with a referer ${referer} that does not exist`);
+                        continue;
+                    }
+                    
+                    // TODO: Make more generic
+                    const relaventConstraints = this.models[referer].properties[dummyModelName].constraints;
+                    for (const constraint in relaventConstraints) {
+                        if (typeof relaventConstraints[constraint] === 'object') {
+                            this.models[referer].properties[dummyModelName].constraints[constraint] = DataConstraints.FOREIGN_KEY_REF(dummyModel.name);
+                            Logger.Database(`Model ${referer} has a property ${dummyModel.name} which was resolved to a foreign key`);
+                            break;
+                        }
+                    }
+
+                    this.dummyModels.splice(this.dummyModels.indexOf(dummyModel), 1);
+                }
+            }
+        }
     }
 
-    // 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();
+        console.log(this.models['lego_brick']);
     }
 }
 
@@ -86,13 +191,13 @@ module.exports = PSQLObjectRelation;
 
 
 
 
 
- Documentation generated by JSDoc 3.6.10 on Fri Feb 11 2022 15:12:15 GMT+0000 (Greenwich Mean Time) + Documentation generated by JSDoc 3.6.10 on Sat Feb 12 2022 03:04:16 GMT+0000 (Greenwich Mean Time)
diff --git a/jsdoc/global.html b/jsdoc/global.html new file mode 100644 index 0000000..aa0807b --- /dev/null +++ b/jsdoc/global.html @@ -0,0 +1,740 @@ + + + + + JSDoc: Global + + + + + + + + + + +
+ +

Global

+ + + + + + +
+ +
+ +

+ + +
+ +
+
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + +

Methods

+ + + + + + + +

addModel(name, model)

+ + + + + + +
+

Adds a model to the database stash

+
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + +
model + + +object + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

model(modelName)

+ + + + + + +
+

Gets a model from the database stash

+
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
modelName + + +string + + + +

The name of the target model

+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

property(name)

+ + + + + + +
+

Gets a property from the model

+
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + +

The name of the target property

+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

resolveDependants()

+ + + + + + +
+

Resolves all the dependancies for the models that have been added where properties weren't available when they were added

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

syncModels()

+ + + + + + +
+

Syncs the models to the database +ONLY run this after all models are properly added

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ +
+ Documentation generated by JSDoc 3.6.10 on Sat Feb 12 2022 03:04:16 GMT+0000 (Greenwich Mean Time) +
+ + + + + \ No newline at end of file diff --git a/jsdoc/index.html b/jsdoc/index.html index 27c4d42..3013822 100644 --- a/jsdoc/index.html +++ b/jsdoc/index.html @@ -50,13 +50,13 @@
- Documentation generated by JSDoc 3.6.10 on Fri Feb 11 2022 15:12:15 GMT+0000 (Greenwich Mean Time) + Documentation generated by JSDoc 3.6.10 on Sat Feb 12 2022 03:04:16 GMT+0000 (Greenwich Mean Time)
diff --git a/jsdoc/model.js.html b/jsdoc/model.js.html index 7ee763e..a6f4093 100644 --- a/jsdoc/model.js.html +++ b/jsdoc/model.js.html @@ -26,16 +26,49 @@
-

+            
const Logger = require('../../logger.js');
+const DataTypes = require('./types.js');
+
 /**
  * @class Model
  * @classdesc The Model class is used to create a model instance.
+ * @param {string} name - The name of the model
+ * @param {object} properties - The properties of the model
+ * @param {boolean} dummy - Whether or not the model is a dummy model
  */
 class Model {
+    constructor(name, properties, dummy = false) {
+        this.name = name;
+        this.properties = properties;
+        this.dummy = dummy;
 
-    constructor() {
+        if (dummy)
+            Logger.Database(`Model ${name} is dummy: ${dummy}`);
+        
+        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, referer = "") {
+        if (this.dummy)
+        {
+            if (this.properties[name]) {
+                this.properties[name].referers.push(referer);
+            }
+            this.properties[name] = {
+                type: DataTypes.INHERET,
+                referers: [referer],
+                constraints: [],
+                dummy: true
+            }
+            return "UNRESOVLED PROPERTY";
+        }
+        return this.property[name];
+    }
 }
 
 module.exports = Model;
@@ -49,13 +82,13 @@ module.exports = Model;
 
 
 
 
 
- Documentation generated by JSDoc 3.6.10 on Fri Feb 11 2022 15:12:15 GMT+0000 (Greenwich Mean Time) + Documentation generated by JSDoc 3.6.10 on Sat Feb 12 2022 03:04:16 GMT+0000 (Greenwich Mean Time)