@xyluet/objection-table-name
v2.0.0
Published
Plugin that automatically set tableName using class name to objection.js
Downloads
9
Maintainers
Readme
objection-table-name
What To Solve
class FooBar extends Model {
static get tableName() {
return `foo_bar`; // you type it on every model
}
}
what about:
class FooBar extends Model { }
console.log(FooBar.tableName);
// => foo_bar
Installation
$ npm install @xyluet/objection-table-name
Usage
const { Model } = require(`objection`);
const TableNamer = require(`@xyluet/objection-table-name`);
// The common way is:
// - make this is as your base class
// BaseModel.js
class BaseModel extends TableNamer()(Model) { }
// TransactionDetail.js
class TransactionDetail extends BaseModel { }
console.log(TransactionDetail.tableName);
// => transaction_detail
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(TransactionDetail.tableName);
// => Foo_Bar
Lodash provides some already defined caseMappers. You can use it too.
License
MIT