1 | # objection-table-name
|
2 |
|
3 | [![CircleCI](https://circleci.com/gh/JaneJeon/objection-table-name.svg?style=shield)](https://circleci.com/gh/JaneJeon/objection-table-name)
|
4 | [![codecov](https://codecov.io/gh/JaneJeon/objection-table-name/branch/master/graph/badge.svg)](https://codecov.io/gh/JaneJeon/objection-table-name)
|
5 | [![NPM](https://img.shields.io/npm/v/objection-table-name)](https://www.npmjs.com/package/objection-table-name)
|
6 | [![Downloads](https://img.shields.io/npm/dt/objection-table-name)](https://www.npmjs.com/package/objection-table-name)
|
7 | [![install size](https://packagephobia.now.sh/badge?p=objection-table-name)](https://packagephobia.now.sh/result?p=objection-table-name)
|
8 | [![David](https://img.shields.io/david/JaneJeon/objection-table-name)](https://david-dm.org/JaneJeon/objection-table-name)
|
9 | [![Known Vulnerabilities](https://snyk.io//test/github/JaneJeon/objection-table-name/badge.svg?targetFile=package.json)](https://snyk.io//test/github/JaneJeon/objection-table-name?targetFile=package.json)
|
10 | [![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=JaneJeon/objection-table-name)](https://dependabot.com)
|
11 | [![Docs](https://img.shields.io/badge/docs-github-blue)](https://janejeon.github.io/objection-table-name)
|
12 | [![Standard](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
|
13 | [![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
|
14 |
|
15 | > Enjoy objection-tablename? Check out my other objection plugins: [objection-hashid](https://github.com/JaneJeon/objection-hashid) and [objection-authorize](https://github.com/JaneJeon/objection-authorize)!
|
16 |
|
17 | ## Note about Objection.js
|
18 |
|
19 | This library is tested to work with _both_ Objection.js v1 and v2!
|
20 |
|
21 | ## What To Solve
|
22 |
|
23 | ```js
|
24 | class NodeModule extends Model {
|
25 | static get tableName() {
|
26 | return 'nodeModules' // you type it on every model
|
27 | }
|
28 | }
|
29 | ```
|
30 |
|
31 | what about:
|
32 |
|
33 | ```js
|
34 | class NodeModule extends Model {}
|
35 | console.log(NodeModule.tableName)
|
36 | // => nodeModules
|
37 | ```
|
38 |
|
39 | ## Installation
|
40 |
|
41 | `$ npm install objection-table-name`
|
42 |
|
43 | ## Usage
|
44 |
|
45 | ```js
|
46 | const { Model } = require('objection')
|
47 | const tableName = require('objection-table-name')
|
48 |
|
49 | // The common way is:
|
50 | // - make this is as your base class
|
51 |
|
52 | // BaseModel.js
|
53 | class BaseModel extends tableName()(Model) {}
|
54 |
|
55 | // TransactionDetail.js
|
56 | class TransactionDetail extends BaseModel {}
|
57 | console.log(TransactionDetail.tableName)
|
58 | // => transactionDetails
|
59 | ```
|
60 |
|
61 | You can define your own mapper
|
62 |
|
63 | ```js
|
64 | function upperFirst([s, ...rest]) {
|
65 | return [s.toUpperCase(), ...rest].join('')
|
66 | }
|
67 |
|
68 | class BaseModel extends TableNamer({
|
69 | caseMapper: upperFirst
|
70 | })(Model) {}
|
71 |
|
72 | class foo_Bar extends BaseModel {}
|
73 | console.log(foo_Bar.tableName)
|
74 | // => Foo_Bar
|
75 | ```
|
76 |
|
77 | [Lodash](https://lodash.com/docs/) provides some already defined caseMappers. You can use it too.
|