Comparing version 0.0.6 to 0.0.7
@@ -12,2 +12,2 @@ const { Orm } = require('./lib/ormius') | ||
Migration | ||
} | ||
} |
@@ -14,6 +14,24 @@ class Migration { | ||
markAsRan() { | ||
this.#connection.query('INSERT INTO migrations (migration) VALUES (?)', this.migrationId, (error) => { | ||
if (error) { | ||
console.log('error', error) | ||
throw error | ||
} | ||
}) | ||
} | ||
addColumn(columnName, columnType) { | ||
console.log('add column', this.modelName, columnName, columnType) | ||
this.#connection.query(`ALTER TABLE ${this.modelName} ADD ${columnName} ${columnType}`, (error) => { | ||
if (error) { | ||
console.log('error', error) | ||
throw error | ||
} | ||
}) | ||
} | ||
createTable(newTable) { | ||
console.log(newTable) | ||
const self = this | ||
this.#connection.query(`CREATE TABLE ${this.modelName} (${newTable.map(({ type, name }) => `${name} ${type}`).join(',')})`, function (error) { | ||
this.#connection.query(`CREATE TABLE ${this.modelName} (${newTable.map(({ type, name }) => `${name} ${type}`).join(',')})`, (error) => { | ||
if (error) { | ||
@@ -23,8 +41,2 @@ console.log('error', error) | ||
} | ||
self.#connection.query('INSERT INTO migrations (migration) VALUES (?)', [self.migrationId], function (error) { | ||
if (error) { | ||
console.log('error', error) | ||
throw error | ||
} | ||
}) | ||
}) | ||
@@ -36,2 +48,2 @@ } | ||
Migration | ||
} | ||
} |
@@ -46,7 +46,8 @@ const { Query } = require('./query') | ||
execute = async () => { | ||
execute = async() => { | ||
const result = await this.#query.execute() | ||
this.#query.clean() | ||
if (Array.isArray(result)) { | ||
return result.map(oneResult => { | ||
return result.map((oneResult) => { | ||
return new this.constructor(this.#connection).setValues(oneResult) | ||
@@ -63,2 +64,2 @@ }) | ||
Model | ||
} | ||
} |
@@ -10,2 +10,3 @@ const fs = require('fs') | ||
const { deferConnection } = params | ||
this.config = JSON.parse(fs.readFileSync(configFile, 'utf8')) | ||
@@ -20,5 +21,8 @@ this.connection = mysql.createConnection(this.config) | ||
const self = this | ||
this.connection.connect(function(err) { | ||
if (err) throw err | ||
console.log('connected as id ' + self.connection.threadId) | ||
this.connection.connect((err) => { | ||
if (err) { | ||
throw err | ||
} | ||
console.log(`connected as id ${ self.connection.threadId}`) | ||
}) | ||
@@ -32,2 +36,2 @@ } | ||
module.exports.Orm = Orm | ||
module.exports.Orm = Orm |
@@ -15,3 +15,3 @@ const { TYPES, RELATION_TYPES } = require('./types') | ||
clean () { | ||
clean() { | ||
this.currentQuery = '' | ||
@@ -25,20 +25,22 @@ this.conditions = [] | ||
where (column, condition) { | ||
where(column, condition) { | ||
let selectQuery | ||
let currentCondition | ||
switch (this.model[column].type) { | ||
case TYPES.STRING: | ||
case TYPES.INT: | ||
selectQuery = 'SELECT ?? FROM ?? WHERE ??=?' | ||
currentCondition = [this.selectColumns, this.modelName, column, condition] | ||
break | ||
case TYPES.BELONGS_TO: | ||
selectQuery = `SELECT ??, ${this.allowedColumns(this.model[column].parent.class.model, this.model[column].parent.class.modelName).join(',')} FROM ?? LEFT JOIN ?? ON ??=?? WHERE ??=?` | ||
currentCondition = [this.selectColumns, this.modelName, this.model[column].parent.class.modelName, | ||
`${this.model[column].parent.class.modelName}.${this.model[column].parent.attribute}`, | ||
`${this.modelName}.${this.model[column].from}`, | ||
`${this.model[column].parent.class.modelName}.${this.model[column].parent.attribute}`, condition] | ||
break | ||
default: | ||
throw new Error(`Not supported type ${this.model[column].type}`) | ||
case TYPES.STRING: | ||
case TYPES.INT: | ||
case TYPES.BOOLEAN: | ||
selectQuery = 'SELECT ?? FROM ?? WHERE ??=?' | ||
currentCondition = [this.selectColumns, this.modelName, column, condition] | ||
break | ||
case TYPES.BELONGS_TO: | ||
selectQuery = `SELECT ??, ${this.allowedColumns(this.model[column].parent.class.model, this.model[column].parent.class.modelName).join(',')} FROM ?? LEFT JOIN ?? ON ??=?? WHERE ??=?` | ||
currentCondition = [this.selectColumns, this.modelName, this.model[column].parent.class.modelName, | ||
`${this.model[column].parent.class.modelName}.${this.model[column].parent.attribute}`, | ||
`${this.modelName}.${this.model[column].from}`, | ||
`${this.model[column].parent.class.modelName}.${this.model[column].parent.attribute}`, condition] | ||
break | ||
default: | ||
throw new Error(`Not supported type ${this.model[column].type}`) | ||
} | ||
@@ -57,3 +59,3 @@ if (this.currentQuery !== '') { | ||
findBy (column, condition) { | ||
findBy(column, condition) { | ||
this.returnNumber = 0 | ||
@@ -65,3 +67,3 @@ this.where(column, condition) | ||
updateBy (conditions, values) { | ||
updateBy(conditions, values) { | ||
this.currentQuery = 'UPDATE ?? SET ? WHERE ?' | ||
@@ -74,3 +76,3 @@ this.conditions = [this.modelName, values, conditions] | ||
this.selectColumns = this.allowedColumns(fields) | ||
this.selectColumnsIds.forEach(id => { | ||
this.selectColumnsIds.forEach((id) => { | ||
this.conditions[id] = this.selectColumns | ||
@@ -82,5 +84,6 @@ }) | ||
allowedColumns (fields, modelName = this.modelName) { | ||
allowedColumns(fields, modelName = this.modelName) { | ||
const otherModel = modelName !== this.modelName | ||
let currentFields | ||
if (otherModel) { | ||
@@ -92,6 +95,7 @@ currentFields = Object.keys(fields) | ||
const model = (otherModel) ? fields : this.model | ||
return currentFields.filter(field => { | ||
return currentFields.filter((field) => { | ||
// otherModel ? `${modelName}.${field} as ${modelName}_${field}` : | ||
return !RELATION_TYPES.includes(model[field].type) | ||
}).map(field => { | ||
}).map((field) => { | ||
if (otherModel) { | ||
@@ -104,6 +108,7 @@ this.joins.push(`${modelName}_${field}`) | ||
create (fields) { | ||
create(fields) { | ||
this.clean() | ||
const filteredFields = {} | ||
Object.keys(fields).filter(filter => this.allowedColumns().includes(filter)).forEach(field => { | ||
Object.keys(fields).filter((filter) => this.allowedColumns().includes(filter)).forEach((field) => { | ||
filteredFields[field] = fields[field] | ||
@@ -122,11 +127,12 @@ }) | ||
const self = this | ||
console.log('executed query', this.currentQuery, this.conditions) | ||
return new Promise(function(resolve, reject) { | ||
self.connection.query(self.currentQuery, self.conditions, function (error, results) { | ||
return new Promise((resolve, reject) => { | ||
self.connection.query(self.currentQuery, self.conditions, (error, results) => { | ||
if (error) { | ||
reject(error) | ||
} | ||
const filtered = results.map(result => { | ||
const filtered = results.map((result) => { | ||
return Object.keys(result).reduce((obj, key) => { | ||
if(!self.joins.includes(key)) { | ||
if (!self.joins.includes(key)) { | ||
obj[key] = result[key] | ||
@@ -136,2 +142,3 @@ return obj | ||
const [object, identity] = key.split('_') | ||
if (!obj[object]) { | ||
@@ -145,2 +152,3 @@ obj[object] = {} | ||
}) | ||
if (self.returnNumber === 0) { | ||
@@ -158,2 +166,2 @@ resolve(filtered[0]) | ||
Query | ||
} | ||
} |
const TYPES = { | ||
INT: 'int', | ||
STRING: 'string', | ||
BOOLEAN: 'boolean', | ||
BELONGS_TO: 'belongs_to', | ||
@@ -13,2 +14,2 @@ HAS_MANY: 'has_many' | ||
RELATION_TYPES | ||
} | ||
} |
{ | ||
"name": "ormius", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "author": "Adele Bendayan", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10421
281