Skip to content

Commit

Permalink
Slight mysql cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Nov 10, 2016
1 parent 7e420d3 commit 7815a73
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
91 changes: 54 additions & 37 deletions modules/orm/adapters/mysql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,14 @@ import Builder from './builder'
import { getTableName } from '../../../global/get-name'


/*
Proxy object that returns item from resulting query
or will check for a relationship on the model
and return a promise.
ex
result.id -> returns `id` on the result Object
result.users
-> returns users if extists on the object.
otherwise, checks for `users` function on the
model and returns the related query promise
*/

const relatable = (result, model) => new Proxy(result, {
get(target, name) {
if(name in target) return target[name]
if(getTableName(name) in target) return target[getTableName(name)]

let instance = new model(result)
if(name in instance) return instance[name]().result()
}
})
class MysqlAdapter {

export default {
/*
Generic Adapter Methods (these should be in every adapter)
select, create, queryBuilder, getJoins, makeRelatable
/*
Builds the mysql query, used query builder and root model class
*/
select({ model, select, where, limit, joins = [] }) {
Expand All @@ -42,10 +24,10 @@ export default {
if(error) return reject(error)

if(joins.length > 0) results = this.mergeInJoins(results)
resolve(relatable(limit === 1 ? results[0] : results, model))
resolve(this.makeRelatable(limit === 1 ? results[0] : results, model))
})
})
},
}

/*
create a row in the database
Expand All @@ -54,20 +36,61 @@ export default {
return new Promise((resolve, reject) => {
connection.query(`INSERT INTO ${model.tableName()} SET ?`, data, (error, result) => {
if(error) return reject(error)
resolve(relatable({
resolve(this.makeRelatable({
id: result.insertId,
...data
}, model))
})
})
},
}

/*
returns a new query builder instance
*/
queryBuilder(options) {
return new Builder(options)
},
}

/*
creates join query from any model realtionships
used on eager loads
*/
getJoins(joins) {
return joins.map(join => ` INNER JOIN \`${join.includeTable}\` ON ${join.localField} = ${join.remoteField}`)
}


/*
Proxy object that returns item from resulting query
or will check for a relationship on the model
and return a promise.
ex
result.id -> returns `id` on the result Object
result.users
-> returns users if extists on the object.
otherwise, checks for `users` function on the
model and returns the related query promise
*/

makeRelatable(result, model) {
return new Proxy(result, {
get(target, name) {
if(name in target) return target[name]
if(getTableName(name) in target) return target[getTableName(name)]

let instance = new model(result)
if(name in instance) return instance[name]().result()
}
})
}

/*
MYSQL SPECIFIC METHODS
*/


/*
Joins nested tables for when eager loading a relationship
Expand All @@ -91,13 +114,7 @@ export default {
})
return newResult
})
},

/*
creates join query from any model realtionships
used on eager loads
*/
getJoins(joins) {
return joins.map(join => ` INNER JOIN \`${join.includeTable}\` ON ${join.localField} = ${join.remoteField}`)
}
}

export default new MysqlAdapter()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "relation",
"version": "0.0.11",
"version": "0.1.0",
"description": "",
"bin": {
"relation": "dist/cli/index.js"
Expand Down

0 comments on commit 7815a73

Please sign in to comment.