Skip to content
This repository has been archived by the owner on Nov 28, 2021. It is now read-only.

Plugin that automatically set tableName using class name to objection.js

License

Notifications You must be signed in to change notification settings

sunjayaali/objection-table-name

Repository files navigation

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_bars

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_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.

License

MIT

About

Plugin that automatically set tableName using class name to objection.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published