objection-table-name

CircleCI codecov Version Downloads Docs

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.

📝 License

Copyright © 2022 Jane Jeon me@janejeon.dev.
This project is LGPL-3.0 licensed.

TL;DR: you are free to import and use this library “as-is” in your code, without needing to make your code source-available or to license it under the same license as this library; however, if you do change this library and you distribute it (directly or as part of your code consuming this library), please do contribute back any improvements for this library and this library alone.