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_bars
$ npm install @xyluet/objection-table-name
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_details
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.
MIT