Wondering what’s next for npm?Check out our public roadmap! »

    objection-table-name

    1.2.3 • Public • Published

    objection-table-name

    CircleCI codecov NPM Downloads install size David Known Vulnerabilities Dependabot Status Docs Standard Prettier

    Note about Objection.js

    This library is tested to work with both Objection.js v1 and v2!

    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.

    Install

    npm i objection-table-name

    DownloadsWeekly Downloads

    20

    Version

    1.2.3

    License

    LGPL-3.0

    Unpacked Size

    15.7 kB

    Total Files

    11

    Last publish

    Collaborators

    • avatar