typeorm-naming-strategy 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/snake-naming.strategy.d.ts +11 -0
- package/dist/snake-naming.strategy.js +38 -0
- package/dist/snake-naming.strategy.js.map +1 -0
- package/package.json +72 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
+
|
|
5
|
+
## 1.0.0 (2021-12-17)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* :beer: fixed eslint run ([14c919e](https://github.com/chantouchsek/typeorm-naming-strategy/commit/14c919e4899b482b51dfff0e418ab97db1021e8d))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Chantouch Sek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Typeorm naming strategy
|
|
2
|
+
|
|
3
|
+
[](https://github.com/chantouchsek/typeorm-naming-strategy/actions/workflows/test.yml)
|
|
4
|
+
[](https://npmjs.com/package/typeorm-naming-strategy)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://npmjs.com/package/typeorm-naming-strategy)
|
|
7
|
+
[](https://npmjs.com/package/typeorm-naming-strategy)
|
|
8
|
+
|
|
9
|
+
This package provides a few (one, at the moment) useful custom naming strategies. It alliterates the name of columns, relations, and other fields in the database.
|
|
10
|
+
|
|
11
|
+
For example, using the snake strategy, if you have a model like this:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
class User {
|
|
15
|
+
@Column()
|
|
16
|
+
createdAt;
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
In the DB the `createdAt` field will be `created_at`
|
|
21
|
+
|
|
22
|
+
## Naming strategies available
|
|
23
|
+
|
|
24
|
+
- Snake
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
It's available as an [npm package](https://www.npmjs.com/package/typeorm-naming-strategy)
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm install typeorm-naming-strategy --save
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or using yarn
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
yarn add typeorm-naming-strategy
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createConnection } from 'typeorm';
|
|
44
|
+
// import { SnakeNamingStrategy } from 'typeorm-naming-strategy';
|
|
45
|
+
import SnakeNamingStrategy from 'typeorm-naming-strategy';
|
|
46
|
+
|
|
47
|
+
await createConnection({
|
|
48
|
+
...
|
|
49
|
+
namingStrategy: new SnakeNamingStrategy(), // Here you'r using the strategy!
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Alternatively you can use it in combination with a `ormconfig.js`
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
// Use require instead of import
|
|
57
|
+
// const SnakeNamingStrategy = require("typeorm-naming-strategy").SnakeNamingStrategy
|
|
58
|
+
const SnakeNamingStrategy = require("typeorm-naming-strategy")
|
|
59
|
+
|
|
60
|
+
module.exports = {
|
|
61
|
+
...
|
|
62
|
+
namingStrategy: new SnakeNamingStrategy(),
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Or you can use it in combination with a `ormconfig.ts`
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import SnakeNamingStrategy from 'typeorm-naming-strategy';
|
|
70
|
+
|
|
71
|
+
module.exports = {
|
|
72
|
+
...
|
|
73
|
+
namingStrategy: new SnakeNamingStrategy(),
|
|
74
|
+
}
|
|
75
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SnakeNamingStrategy = void 0;
|
|
4
|
+
const snake_naming_strategy_1 = require("./snake-naming.strategy");
|
|
5
|
+
Object.defineProperty(exports, "SnakeNamingStrategy", { enumerable: true, get: function () { return snake_naming_strategy_1.SnakeNamingStrategy; } });
|
|
6
|
+
exports.default = snake_naming_strategy_1.SnakeNamingStrategy;
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mEAA8D;AAErD,oGAFA,2CAAmB,OAEA;AAE5B,kBAAe,2CAAmB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DefaultNamingStrategy, NamingStrategyInterface } from 'typeorm';
|
|
2
|
+
export declare class SnakeNamingStrategy extends DefaultNamingStrategy implements NamingStrategyInterface {
|
|
3
|
+
tableName(className: string, customName: string): string;
|
|
4
|
+
columnName(propertyName: string, customName: string, embeddedPrefixes: string[]): string;
|
|
5
|
+
relationName(propertyName: string): string;
|
|
6
|
+
joinColumnName(relationName: string, referencedColumnName: string): string;
|
|
7
|
+
joinTableName(firstTableName: string, secondTableName: string, firstPropertyName: string): string;
|
|
8
|
+
joinTableColumnName(tableName: string, propertyName: string, columnName?: string): string;
|
|
9
|
+
classTableInheritanceParentColumnName(parentTableName: string, parentTableIdPropertyName: string): string;
|
|
10
|
+
eagerJoinRelationAlias(alias: string, propertyPath: string): string;
|
|
11
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SnakeNamingStrategy = void 0;
|
|
4
|
+
const typeorm_1 = require("typeorm");
|
|
5
|
+
const StringUtils_1 = require("typeorm/util/StringUtils");
|
|
6
|
+
class SnakeNamingStrategy extends typeorm_1.DefaultNamingStrategy {
|
|
7
|
+
tableName(className, customName) {
|
|
8
|
+
return customName ? customName : (0, StringUtils_1.snakeCase)(className);
|
|
9
|
+
}
|
|
10
|
+
columnName(propertyName, customName, embeddedPrefixes) {
|
|
11
|
+
return ((0, StringUtils_1.snakeCase)(embeddedPrefixes.concat('').join('_')) +
|
|
12
|
+
(customName ? customName : (0, StringUtils_1.snakeCase)(propertyName)));
|
|
13
|
+
}
|
|
14
|
+
relationName(propertyName) {
|
|
15
|
+
return (0, StringUtils_1.snakeCase)(propertyName);
|
|
16
|
+
}
|
|
17
|
+
joinColumnName(relationName, referencedColumnName) {
|
|
18
|
+
return (0, StringUtils_1.snakeCase)(relationName + '_' + referencedColumnName);
|
|
19
|
+
}
|
|
20
|
+
joinTableName(firstTableName, secondTableName, firstPropertyName) {
|
|
21
|
+
return (0, StringUtils_1.snakeCase)(firstTableName +
|
|
22
|
+
'_' +
|
|
23
|
+
firstPropertyName.replace(/\./gi, '_') +
|
|
24
|
+
'_' +
|
|
25
|
+
secondTableName);
|
|
26
|
+
}
|
|
27
|
+
joinTableColumnName(tableName, propertyName, columnName) {
|
|
28
|
+
return (0, StringUtils_1.snakeCase)(tableName + '_' + (columnName ? columnName : propertyName));
|
|
29
|
+
}
|
|
30
|
+
classTableInheritanceParentColumnName(parentTableName, parentTableIdPropertyName) {
|
|
31
|
+
return (0, StringUtils_1.snakeCase)(`${parentTableName}_${parentTableIdPropertyName}`);
|
|
32
|
+
}
|
|
33
|
+
eagerJoinRelationAlias(alias, propertyPath) {
|
|
34
|
+
return alias + '_' + propertyPath.replace('.', '_');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.SnakeNamingStrategy = SnakeNamingStrategy;
|
|
38
|
+
//# sourceMappingURL=snake-naming.strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snake-naming.strategy.js","sourceRoot":"","sources":["../src/snake-naming.strategy.ts"],"names":[],"mappings":";;;AAAA,qCAAyE;AACzE,0DAAqD;AAErD,MAAa,mBACX,SAAQ,+BAAqB;IAG7B,SAAS,CAAC,SAAiB,EAAE,UAAkB;QAC7C,OAAO,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,IAAA,uBAAS,EAAC,SAAS,CAAC,CAAC;IACxD,CAAC;IAED,UAAU,CACR,YAAoB,EACpB,UAAk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typeorm-naming-strategy",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Custom naming strategies for typeorm",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": "^12.20.0 || >=14.13.0"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"typeorm",
|
|
15
|
+
"naming",
|
|
16
|
+
"strategy",
|
|
17
|
+
"node",
|
|
18
|
+
"orm",
|
|
19
|
+
"naming strategy",
|
|
20
|
+
"snake strategy",
|
|
21
|
+
"typeorm snake",
|
|
22
|
+
"naming strategies"
|
|
23
|
+
],
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/chantouchsek/typeorm-naming-strategy"
|
|
27
|
+
},
|
|
28
|
+
"author": {
|
|
29
|
+
"name": "Chantouch Sek",
|
|
30
|
+
"email": "chantouchsek.cs83@gmail.com",
|
|
31
|
+
"url": "https://chantouch.me"
|
|
32
|
+
},
|
|
33
|
+
"lint-staged": {
|
|
34
|
+
"*.{ts,js}": "eslint"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "rimraf dist && tsc",
|
|
39
|
+
"test": "jest --coverage --passWithNoTests --bail",
|
|
40
|
+
"prepublish": "yarn build",
|
|
41
|
+
"prepare": "husky install",
|
|
42
|
+
"lint": "eslint --ext \".ts,.js,.vue\" --ignore-path .gitignore .",
|
|
43
|
+
"standard-version": "standard-version",
|
|
44
|
+
"release": "standard-version && git push --follow-tags origin main && yarn publish"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@commitlint/cli": "^15.0.0",
|
|
48
|
+
"@commitlint/config-conventional": "^15.0.0",
|
|
49
|
+
"@types/jest": "^27.0.3",
|
|
50
|
+
"@types/node": "^17.0.0",
|
|
51
|
+
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
|
52
|
+
"@typescript-eslint/parser": "^4.31.1",
|
|
53
|
+
"eslint": "^7.32.0",
|
|
54
|
+
"eslint-config-prettier": "^8.3.0",
|
|
55
|
+
"eslint-plugin-import": "^2.24.2",
|
|
56
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
57
|
+
"eslint-plugin-promise": "^5.1.0",
|
|
58
|
+
"husky": "^7.0.4",
|
|
59
|
+
"jest": "^27.4.5",
|
|
60
|
+
"lint-staged": "^12.1.2",
|
|
61
|
+
"prettier": "^2.5.1",
|
|
62
|
+
"rimraf": "^3.0.2",
|
|
63
|
+
"standard-version": "^9.3.2",
|
|
64
|
+
"ts-jest": "^27.1.2",
|
|
65
|
+
"tslint": "^6.1.3",
|
|
66
|
+
"typeorm": "^0.2.41",
|
|
67
|
+
"typescript": "^4.5.4"
|
|
68
|
+
},
|
|
69
|
+
"peerDependencies": {
|
|
70
|
+
"typeorm": "^0.2.41"
|
|
71
|
+
}
|
|
72
|
+
}
|