207 lines
7.1 KiB
HTML
207 lines
7.1 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: entity-relationships.js</title>
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="main">
|
|
|
|
<h1 class="page-title">Source: entity-relationships.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source linenums"><code>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
|
|
* them worrying about dependancies and such, we can just add them to the database
|
|
* in the order they are defined. This class will handle that for us. As well as
|
|
* keeping track of the models that have been added to the database.
|
|
*/
|
|
|
|
|
|
/**
|
|
* @class Database
|
|
* @classdesc The Database class is used to create a database instance.
|
|
* @param {object} connection - An active instance of a psql database connection
|
|
*/
|
|
class PSQLObjectRelation {
|
|
constructor(psqlConnection) {
|
|
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) {
|
|
// 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];
|
|
}
|
|
|
|
/**
|
|
* @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);
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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']);
|
|
}
|
|
}
|
|
|
|
module.exports = PSQLObjectRelation;
|
|
</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Database.html">Database</a></li><li><a href="Model.html">Model</a></li></ul><h3>Global</h3><ul><li><a href="global.html#addModel">addModel</a></li><li><a href="global.html#model">model</a></li><li><a href="global.html#property">property</a></li><li><a href="global.html#resolveDependants">resolveDependants</a></li><li><a href="global.html#syncModels">syncModels</a></li></ul>
|
|
</nav>
|
|
|
|
<br class="clear">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.10</a> on Sat Feb 12 2022 03:04:16 GMT+0000 (Greenwich Mean Time)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|