Skip to content

Commit

Permalink
push to dev
Browse files Browse the repository at this point in the history
  • Loading branch information
chaidhat committed Oct 5, 2024
1 parent 419c5cd commit 99ce305
Show file tree
Hide file tree
Showing 17 changed files with 3,007 additions and 829 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added lib/.DS_Store
Binary file not shown.
46 changes: 0 additions & 46 deletions lib/helper.js

This file was deleted.

84 changes: 76 additions & 8 deletions lib/checker.js → lib/orm-checker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ormSql = require("./sql");
const ormSql = require("./orm-sql");
const ormHelper = require("./orm-helper");

var databaseTables = []
var areAllTablesValid = false;
Expand All @@ -8,17 +9,24 @@ function getValidity() {
return areAllTablesValid;
}

module.exports.findTable = findTable;
function findTable(tableName) {
// TODO: make O(1). not O(N)
return databaseTables.find((dt) => dt.tableName === tableName);
}

// call only ONCE per table on init
module.exports.validateTable = validateTable;
async function validateTable(table) {
// this is done at compiletime
module.exports.pushTable = pushTable;
async function pushTable(table) {
if (areAllTablesValid) {
areAllTablesValid = false;
}
databaseTables.push(table);
}

module.exports.removeTableFromValidation = removeTableFromValidation;
async function removeTableFromValidation(table) {
module.exports.popTable = popTable;
async function popTable(table) {
for (let i = 0; i < databaseTables.length; i++) {
if (databaseTables[i].tableName === table.tableName) {
databaseTables.splice(i, 1);
Expand All @@ -27,13 +35,16 @@ async function removeTableFromValidation(table) {
}
}

// call after ALL tables are init
// this is called for when a sqlQuery is made.
// this is done at runtime
// NOTE: sqlQueries are NOT made for table definees nor inits.
module.exports.validateAllTables = validateAllTables;
async function validateAllTables() {
await _checkTableDbConsistency();
_checkTableNames();
_checkTableDuplicates();
_checkTablesForCycles();
_checkTableProperties();
areAllTablesValid = true;
}

Expand All @@ -57,6 +68,7 @@ function _checkTableNames() {
}
}


async function _checkTableDbConsistency() {
for (let i = 0; i < databaseTables.length; i++) {
const databaseTable = databaseTables[i];
Expand Down Expand Up @@ -98,15 +110,21 @@ async function _checkTableDbConsistency() {
for (let j = 0; j < databaseTable.properties.length; j++) {
const column = databaseTable.properties[j];
if (!columns.includes(column.name)) {
console.log(`orm fatal: cannot find schema '${databaseTable.tableName}.${column.name}' in database.`);
if (ormHelper.isTokenPrimitive(column.type)) {
// primitives require row in db
console.log(`orm fatal: cannot find schema row '${databaseTable.tableName}.${column.name}' in database.`);
}
} else {
const index = columns.indexOf(column.name);
if (index > -1) { // only splice array when item is found
columns.splice(index, 1); // 2nd parameter means remove one item only
}
}
if (column.type !== dataTypeMap[column.name]) {
console.log(`orm fatal: '${databaseTable.tableName}.${column.name}' has type '${column.type}' but in db it has type '${dataTypeMap[column.name]}'`);
if (ormHelper.isTokenPrimitive(column.type)) {
// primitives require correct type in db
console.log(`orm fatal: '${databaseTable.tableName}.${column.name}' has type '${column.type}' but in db it has type '${dataTypeMap[column.name]}'`);
}
}
}

Expand Down Expand Up @@ -150,4 +168,54 @@ function _checkTablesForCycles() {
}
}
}
}

function _checkTableProperties() {
databaseTables.forEach((databaseTable) => {
databaseTable.properties.forEach((property) => {
const propertyType = ormHelper.parseType(property.type);

if (ormHelper.isTokenPrimitive(propertyType.dataType)) {
if (propertyType.dataType === "varchar") {
if (propertyType.precision === null) {
throw `orm fatal: type error '${property.name}': varchar precision is required`;
}
} else {
if (propertyType.precision !== null) {
throw `orm fatal: type error '${property.name}': unexpected precision value (only varchars can have precision)`;
}
}
if (propertyType.isArray) {
throw `orm fatal: type error '${property.name}': array of primitives not supported yet!`;
// TODO: implement
}
return; // OK
}

const dependentDatabaseTable = databaseTables.find((dt) => dt.tableName === propertyType.dataType);
if (dependentDatabaseTable !== undefined) {
if (propertyType.precision !== null) {
throw `orm fatal: type error '${property.name}': unexpected precision value (only varchars can have precision)`;
}
if (propertyType.isArray) {
// 1-M connection
// assert that that database contains a key with the id of this table
// i.e.,
// table A { int tableAId, int a, tableBId b }, then
// table B MUST contain tableAId to form a 1-M connection.
const dependentDatabaseTableProperty = dependentDatabaseTable.properties.find((p) => p.name === databaseTable.tableKeyName);
if (dependentDatabaseTableProperty === undefined) {
throw `orm fatal: type error '${property.name}': 1-M fail: table '${dependentDatabaseTable.tableName}' must contain 'int ${databaseTable.tableKeyName}' as property`;
}
} else {
// 1-1 connection
throw `orm fatal: type error '${property.name}': 1-1 fail: non-primitives as non-arrays are not supported yet!`;
// TODO: implement
}
return; // OK
}
throw `orm fatal: type error '${property.name}': unknown type`;

});
});
}
Loading

0 comments on commit 99ce305

Please sign in to comment.