-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from baethon/method-proxies
Method proxies (closes #8)
- Loading branch information
Showing
21 changed files
with
419 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,131 @@ | ||
const pluralize = require('pluralize') | ||
const snakeCase = require('lodash.snakecase') | ||
const QueryBuilder = require('./query-builder') | ||
const { toScope } = require('./utils') | ||
const { KexError } = require('./errors') | ||
|
||
const getTableName = (modelName, { tableName }) => { | ||
return tableName || snakeCase(pluralize.plural(modelName)) | ||
} | ||
|
||
/** @typedef { import('./kex') } Kex */ | ||
/** @typedef { import('./kex').ModelOptions } ModelOptions */ | ||
/** @typedef { import('./query-builder').QueryBuilder } QueryBuilder */ | ||
/** @typedef { import('./plugins/soft-deletes').SoftDeleteOptions } SoftDeleteOptions */ | ||
/** @typedef { import('./relations/relation') } Relation */ | ||
/** @typedef { import('./query-builder').Scope } Scope */ | ||
|
||
/** | ||
* @typedef {Object} Model | ||
* @property {QueryBuilder} QueryBuilder | ||
* @property {Function} query create new query | ||
* @property {String} name name of the model | ||
* @property {Kex} kex | ||
* @property {String} tableName | ||
* @property {String} primaryKey | ||
* @property {ModelOptions} options | ||
* @typedef {Object} ModelOptions | ||
* @property {String} [tableName] | ||
* @property {String} [primaryKey=id] | ||
* @property {Boolean | SoftDeleteOptions} [softDeletes=false] | ||
* @property {Object.<String, Object>} [relations] | ||
* @property {PluginFactory[]} [plugins] | ||
* @property {Object.<String,Scope>} [scopes] | ||
* @property {Object.<String,Scope>} [globalScopes] | ||
* @property {Object.<String,Relation>} [relations] | ||
*/ | ||
|
||
/** | ||
* Create the model object | ||
* | ||
* @param {Kex} kex | ||
* @param {String} name | ||
* @param {ModelOptions} options | ||
* @return {Model} | ||
* @typedef {Object} ExtendOptions | ||
* @property {String} methodName | ||
* @property {Function} fn | ||
* @property {Boolean} [queryProxy=false] should the function | ||
* be proxied to the QueryBuilder? | ||
*/ | ||
const createModel = (kex, name, options) => { | ||
const tableName = getTableName(name, options) | ||
const builder = QueryBuilder.createChildClass(tableName, options) | ||
|
||
return { | ||
get QueryBuilder () { | ||
return builder | ||
}, | ||
|
||
get name () { | ||
return name | ||
}, | ||
|
||
get kex () { | ||
return kex | ||
}, | ||
|
||
get tableName () { | ||
return tableName | ||
}, | ||
|
||
get primaryKey () { | ||
return options.primaryKey || 'id' | ||
}, | ||
|
||
get options () { | ||
return options | ||
}, | ||
|
||
query () { | ||
const { knex } = this.kex | ||
return this.QueryBuilder.create(knex.client) | ||
|
||
const proxyQueryMethods = [ | ||
'where', | ||
'insert', | ||
'returning' | ||
] | ||
|
||
class Model { | ||
/** | ||
* @param {import('./kex')} kex | ||
* @param {String} name | ||
* @param {ModelOptions} options | ||
*/ | ||
constructor (kex, name, options = {}) { | ||
this.name = name | ||
this.kex = kex | ||
this.options = options | ||
this.QueryBuilder = QueryBuilder.createChildClass(this) | ||
this.booted = false | ||
} | ||
|
||
get tableName () { | ||
const { tableName } = this.options | ||
return tableName || snakeCase(pluralize.plural(this.name)) | ||
} | ||
|
||
get primaryKey () { | ||
const { primaryKey } = this.options | ||
return primaryKey || 'id' | ||
} | ||
|
||
query () { | ||
this.bootIfNotBooted() | ||
|
||
const { knex } = this.kex | ||
return this.QueryBuilder.create(knex.client) | ||
} | ||
|
||
/** | ||
* @param {ExtendOptions} options | ||
*/ | ||
extend (options) { | ||
const { methodName, fn, queryProxy = false } = options | ||
|
||
if (this[methodName]) { | ||
throw new KexError(`Can't overwrite method [${methodName}] in ${this.name} model`) | ||
} | ||
|
||
if (queryProxy) { | ||
this.QueryBuilder.extend({ methodName, fn }) | ||
this[methodName] = (...args) => { | ||
const query = this.query() | ||
return query[methodName](...args) | ||
} | ||
} else { | ||
this[methodName] = (...args) => { | ||
return fn.call(this, ...args) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @callback PluginFactory | ||
* @param {Model} Model | ||
* @param {Object} options | ||
*/ | ||
addScope (name, fn) { | ||
this.extend({ | ||
methodName: name, | ||
fn: toScope(fn), | ||
queryProxy: true | ||
}) | ||
|
||
/** | ||
* Apply the list of plugins to the Model | ||
* | ||
* @param {PluginFactory[]} plugins | ||
* @param {Model} Model | ||
* @param {Object} options | ||
* @return {Model} | ||
*/ | ||
const applyPlugins = function (plugins, Model, options) { | ||
plugins.forEach(fn => { | ||
fn(Model, options) | ||
}) | ||
return this | ||
} | ||
|
||
return Model | ||
/** | ||
* @private | ||
*/ | ||
bootIfNotBooted () { | ||
if (this.booted) { | ||
return | ||
} | ||
|
||
const { | ||
scopes = {}, | ||
globalScopes = {} | ||
} = this.options | ||
|
||
Object.entries(scopes) | ||
.forEach(([name, fn]) => this.addScope(name, fn)) | ||
|
||
Object.entries(globalScopes) | ||
.forEach(([name, fn]) => this.QueryBuilder.addGlobalScope(name, fn)) | ||
|
||
this.booted = true | ||
} | ||
} | ||
|
||
module.exports = { createModel, applyPlugins } | ||
proxyQueryMethods.forEach(name => { | ||
Model.prototype[name] = function (...args) { | ||
const query = this.query() | ||
return query[name](...args) | ||
} | ||
}) | ||
|
||
module.exports = Model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,31 @@ | ||
const { ModelNotFound } = require('../errors') | ||
|
||
/** | ||
* @param {import('../model').Model} Model | ||
* @param {import('../model')} Model | ||
*/ | ||
module.exports = (Model) => { | ||
const { primaryKey } = Model | ||
|
||
Model.find = function (id) { | ||
return this.query() | ||
.where(primaryKey, id) | ||
.first() | ||
} | ||
Model.extend({ | ||
methodName: 'find', | ||
fn (id) { | ||
return this.query() | ||
.where(primaryKey, id) | ||
.first() | ||
} | ||
}) | ||
|
||
Model.findOrFail = function (id) { | ||
return this.find(id) | ||
.then(model => { | ||
if (!model) { | ||
throw ModelNotFound.findOrFail(Model, { primaryKey, value: id }) | ||
} | ||
Model.extend({ | ||
methodName: 'findOrFail', | ||
fn (id) { | ||
return this.find(id) | ||
.then(model => { | ||
if (!model) { | ||
throw ModelNotFound.findOrFail(Model, { primaryKey, value: id }) | ||
} | ||
|
||
return model | ||
}) | ||
} | ||
return model | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
const { ModelNotFound } = require('../errors') | ||
|
||
/** | ||
* @param {import('../model').Model} Model | ||
* @param {import('../kex').ModelOptions} options | ||
* @param {import('../model')} Model | ||
*/ | ||
module.exports = (Model, options) => { | ||
Model.QueryBuilder.prototype.firstOrFail = function (columns) { | ||
return this.first(columns) | ||
.then(model => { | ||
if (!model) { | ||
throw ModelNotFound.firstOrFail(Model) | ||
} | ||
module.exports = (Model) => { | ||
Model.QueryBuilder.extend({ | ||
methodName: 'firstOrFail', | ||
async fn (columns) { | ||
const model = await this.first(columns) | ||
|
||
return model | ||
}) | ||
} | ||
if (!model) { | ||
throw ModelNotFound.firstOrFail(Model) | ||
} | ||
|
||
return model | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.