objection-table-name
v2.0.2
Published
ActiveRecord-style objection.js plugin to auto-fill tableName
Downloads
1,946
Maintainers
Readme
objection-table-name
Enjoy objection-tablename? Check out my other objection plugins: objection-hashid and objection-authorize!
What To Solve
class NodeModule extends Model {
static get tableName() {
return 'nodeModules' // you type it on every model
}
}
what about:
class NodeModule extends Model {}
console.log(NodeModule.tableName)
// => nodeModules
Installation
$ npm install objection-table-name
Usage
const { Model } = require('objection')
const tableName = require('objection-table-name')
// The common way is:
// - make this is as your base class
// BaseModel.js
class BaseModel extends tableName()(Model) {}
// TransactionDetail.js
class TransactionDetail extends BaseModel {}
console.log(TransactionDetail.tableName)
// => transactionDetails
You can define your own mapper
function upperFirst([s, ...rest]) {
return [s.toUpperCase(), ...rest].join('')
}
class BaseModel extends TableNamer({
caseMapper: upperFirst
})(Model) {}
class foo_Bar extends BaseModel {}
console.log(foo_Bar.tableName)
// => Foo_Bar
Lodash provides some already defined caseMappers. You can use it too.