Socket
Socket
Sign inDemoInstall

ormius

Package Overview
Dependencies
13
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.3 to 0.0.4

cli/migrate.js

40

index.js

@@ -1,30 +0,12 @@

const fs = require('fs')
const mysql = require('mysql')
const { Orm } = require('./lib/ormius')
const { TYPES, RELATION_TYPES } = require('./lib/types')
const { Model } = require('./lib/model')
const { Migration } = require('./lib/migration')
class Orm {
constructor(configFile, params = {}) {
if (!configFile) {
throw new Error('The config file is required')
}
const { deferConnection } = params
this.config = JSON.parse(fs.readFileSync(configFile, 'utf8'))
this.connection = mysql.createConnection(this.config)
if (!deferConnection) {
this.connect()
}
}
connect() {
const self = this
this.connection.connect(function(err) {
if (err) throw err;
console.log('connected as id ' + self.connection.threadId);
})
}
close() {
this.connection.end()
}
}
module.exports.Orm = Orm
module.exports = {
Orm,
Model,
TYPES,
RELATION_TYPES,
Migration
}
const { Query } = require('./query')
class Model {
#query
#model
#connection
#query
#model
#connection
constructor(modelName, model, ormius, values = {}) {
this.modelName = modelName
this.#connection = ormius.connection ? ormius.connection : ormius
this.#model = model
this.#query = new Query(this.modelName, this.#connection, this.#model)
this.values = values
}
constructor(modelName, model, ormius, values = {}) {
this.modelName = modelName
this.#connection = ormius.connection ? ormius.connection : ormius
this.#model = model
this.#query = new Query(this.modelName, this.#connection, this.#model)
this.values = values
}
setValues(values) {
this.values = values
return this
}
setValues(values) {
this.values = values
return this
}
findBy = (column, condition) => {
this.#query.findBy(column, condition)
return this
}
findBy = (column, condition) => {
this.#query.findBy(column, condition)
return this
}
where = (column, condition) => {
this.#query.where(column, condition)
return this
}
where = (column, condition) => {
this.#query.where(column, condition)
return this
}
updateBy = (newValues, conditions = this.values) => {
this.#query.updateBy(conditions, newValues)
return this
}
updateBy = (newValues, conditions = this.values) => {
this.#query.updateBy(conditions, newValues)
return this
}
select = (fields) => {
this.#query.select(fields)
return this
}
select = (fields) => {
this.#query.select(fields)
return this
}
create = (fields) => {
this.#query.create(fields)
return this
}
create = (fields) => {
this.#query.create(fields)
return this
}
execute = async () => {
const result = await this.#query.execute()
this.#query.clean()
if (Array.isArray(result)) {
return result.map(oneResult => {
return new this.constructor(this.#connection).setValues(oneResult)
})
} else {
this.values = result
return this
execute = async () => {
const result = await this.#query.execute()
this.#query.clean()
if (Array.isArray(result)) {
return result.map(oneResult => {
return new this.constructor(this.#connection).setValues(oneResult)
})
} else {
this.values = result
return this
}
}
}
}
module.exports = {
Model
Model
}
const { TYPES, RELATION_TYPES } = require('./types')
class Query {
constructor(modelName, connection, model) {
this.modelName = modelName
this.connection = connection
this.currentQuery = ''
this.conditions = []
this.model = model
this.selectColumns = this.allowedColumns()
this.selectColumnsIds = []
this.joins = []
}
constructor(modelName, connection, model) {
this.modelName = modelName
this.connection = connection
this.currentQuery = ''
this.conditions = []
this.model = model
this.selectColumns = this.allowedColumns()
this.selectColumnsIds = []
this.joins = []
}
clean () {
this.currentQuery = ''
this.conditions = []
this.selectColumns = this.allowedColumns()
this.selectColumnsIds = []
this.returnNumber = null
this.joins = []
}
clean () {
this.currentQuery = ''
this.conditions = []
this.selectColumns = this.allowedColumns()
this.selectColumnsIds = []
this.returnNumber = null
this.joins = []
}
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:
const { model, modelName } = this.model[column].parent.class
selectQuery = `SELECT ??, ${this.allowedColumns(model, modelName).join(',')} FROM ?? LEFT JOIN ?? ON ??=?? WHERE ??=?`
currentCondition = [this.selectColumns, this.modelName, modelName,
`${modelName}.${this.model[column].parent.attribute}`,
`${this.modelName}.${this.model[column].from}`,
`${modelName}.${this.model[column].parent.attribute}`, condition]
break
default:
throw new Error(`Not supported type ${this.model[column].type}`)
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}`)
}
if (this.currentQuery !== '') {
this.currentQuery = `${this.currentQuery} AND ${selectQuery}`
} else {
this.currentQuery = selectQuery
}
this.selectColumnsIds.push(this.conditions.length)
this.conditions = [...this.conditions, ...currentCondition]
console.log(this.currentQuery)
console.log(this.conditions)
return this
}
if (this.currentQuery !== '') {
this.currentQuery = `${this.currentQuery} AND ${selectQuery}`
} else {
this.currentQuery = selectQuery
findBy (column, condition) {
this.returnNumber = 0
this.where(column, condition)
this.currentQuery = `${this.currentQuery} LIMIT 1`
return this
}
this.selectColumnsIds.push(this.conditions.length)
this.conditions = [...this.conditions, ...currentCondition]
console.log(this.currentQuery)
console.log(this.conditions)
return this
}
findBy (column, condition) {
this.returnNumber = 0
this.where(column, condition)
this.currentQuery = `${this.currentQuery} LIMIT 1`
return this
}
updateBy (conditions, values) {
this.currentQuery = 'UPDATE ?? SET ? WHERE ?'
this.conditions = [this.modelName, values, conditions]
return this
}
updateBy (conditions, values) {
this.currentQuery = 'UPDATE ?? SET ? WHERE ?'
this.conditions = [this.modelName, values, conditions]
return this
}
select(fields) {
this.selectColumns = this.allowedColumns(fields)
this.selectColumnsIds.forEach(id => {
this.conditions[id] = this.selectColumns
})
console.log(this.currentQuery)
return this
}
select(fields) {
this.selectColumns = this.allowedColumns(fields)
this.selectColumnsIds.forEach(id => {
this.conditions[id] = this.selectColumns
})
console.log(this.currentQuery)
return this
}
allowedColumns (fields, modelName = this.modelName) {
const otherModel = modelName !== this.modelName
let currentFields
if (otherModel) {
currentFields = Object.keys(fields)
} else {
currentFields = fields || Object.keys(this.model)
}
const model = (otherModel) ? fields : this.model
return currentFields.filter(field => {
// otherModel ? `${modelName}.${field} as ${modelName}_${field}` :
return !RELATION_TYPES.includes(model[field].type)
}).map(field => {
if (otherModel) {
this.joins.push(`${modelName}_${field}`)
}
return otherModel ? `${modelName}.${field} as ${modelName}_${field}` : `${modelName}.${field}`
})
}
allowedColumns (fields, modelName = this.modelName) {
const otherModel = modelName !== this.modelName
let currentFields
if (otherModel) {
currentFields = Object.keys(fields)
} else {
currentFields = fields || Object.keys(this.model)
create (fields) {
this.clean()
const filteredFields = {}
Object.keys(fields).filter(filter => this.allowedColumns().includes(filter)).forEach(field => {
filteredFields[field] = fields[field]
})
this.currentQuery = 'INSERT INTO ?? SET ?'
this.conditions = [this.modelName, filteredFields]
console.log(this.currentQuery)
return this
}
const model = (otherModel) ? fields : this.model
return currentFields.filter(field => {
// otherModel ? `${modelName}.${field} as ${modelName}_${field}` :
return !RELATION_TYPES.includes(model[field].type)
}).map(field => {
if (otherModel) {
this.joins.push(`${modelName}_${field}`)
}
return otherModel ? `${modelName}.${field} as ${modelName}_${field}` : `${modelName}.${field}`
})
}
create (fields) {
this.clean()
const filteredFields = {}
Object.keys(fields).filter(filter => this.allowedColumns().includes(filter)).forEach(field => {
filteredFields[field] = fields[field]
})
this.currentQuery = 'INSERT INTO ?? SET ?'
this.conditions = [this.modelName, filteredFields]
console.log(this.currentQuery)
return this
}
async execute() {
if (this.currentQuery === '' || !this.currentQuery) {
throw new Error('Query not formed')
}
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) {
if (error) {
reject(error)
async execute() {
if (this.currentQuery === '' || !this.currentQuery) {
throw new Error('Query not formed')
}
const filtered = results.map(result => {
return Object.keys(result).reduce((obj, key) => {
if(!self.joins.includes(key)) {
obj[key] = result[key]
return obj
} else {
const [object, identity] = key.split('_')
if (!obj[object]) {
obj[object] = {}
}
obj[object][identity] = result[key]
return obj
}
}, {})
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) {
if (error) {
reject(error)
}
const filtered = results.map(result => {
return Object.keys(result).reduce((obj, key) => {
if(!self.joins.includes(key)) {
obj[key] = result[key]
return obj
} else {
const [object, identity] = key.split('_')
if (!obj[object]) {
obj[object] = {}
}
obj[object][identity] = result[key]
return obj
}
}, {})
})
if (self.returnNumber === 0) {
resolve(filtered[0])
} else {
resolve(filtered)
}
})
})
if (self.returnNumber === 0) {
resolve(filtered[0])
} else {
resolve(filtered)
}
})
})
}
}
}
module.exports = {
Query
Query
}
const TYPES = {
INT: 'int',
STRING: 'string',
BELONGS_TO: 'belongs_to',
HAS_MANY: 'has_many'
INT: 'int',
STRING: 'string',
BELONGS_TO: 'belongs_to',
HAS_MANY: 'has_many'
}

@@ -11,4 +11,4 @@

module.exports = {
TYPES,
RELATION_TYPES
TYPES,
RELATION_TYPES
}
{
"name": "ormius",
"version": "0.0.3",
"version": "0.0.4",
"main": "index.js",

@@ -8,3 +8,4 @@ "author": "Adele Bendayan",

"scripts": {
"example": "nodemon examples/example.js"
"example": "nodemon examples/example.js",
"migrate": "node cli/migrate.js"
},

@@ -14,3 +15,4 @@ "files": [

"index.js",
"lib/"
"lib/",
"cli/"
],

@@ -27,8 +29,10 @@ "repository": {

"dependencies": {
"fs": "0.0.1-security",
"mysql": "2.18.1"
"arg": "^5.0.1",
"fs": "^0.0.1-security",
"mysql": "^2.18.1"
},
"devDependencies": {
"nodemon": "2.0.15"
"eslint": "^8.10.0",
"nodemon": "^2.0.15"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc