@cheetah.js/cli 0.1.16 → 0.1.18
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/.eslintrc.js +33 -33
- package/.idea/cheetah-cli.iml +7 -7
- package/.idea/codeStyles/Project.xml +59 -0
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/modules.xml +7 -7
- package/.idea/php.xml +18 -18
- package/.idea/prettier.xml +6 -0
- package/.idea/vcs.xml +5 -5
- package/.prettierrc +4 -4
- package/LICENSE +673 -673
- package/README.md +26 -26
- package/cheetah.config.ts +15 -15
- package/package.json +44 -42
- package/.idea/git_toolbox_prj.xml +0 -15
- package/bun.lockb +0 -0
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -28
- package/dist/cli.js.map +0 -1
- package/dist/entities/cheetah-migration.entity.d.ts +0 -5
- package/dist/entities/cheetah-migration.entity.js +0 -32
- package/dist/entities/cheetah-migration.entity.js.map +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -19
- package/dist/index.js.map +0 -1
- package/dist/migrator/diff-calculator.d.ts +0 -20
- package/dist/migrator/diff-calculator.js +0 -339
- package/dist/migrator/diff-calculator.js.map +0 -1
- package/dist/migrator/migrator.d.ts +0 -84
- package/dist/migrator/migrator.js +0 -419
- package/dist/migrator/migrator.js.map +0 -1
- package/dist/run-cli.sh +0 -2
package/README.md
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
## Cheetah.js CLI
|
|
2
|
-
The CLI (Command Line Interface) for Cheetah.js.
|
|
3
|
-
|
|
4
|
-
### Instalação
|
|
5
|
-
To install, you can use npm or bun:
|
|
6
|
-
```bash
|
|
7
|
-
bun install @cheetahjs/cli
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
### Uso
|
|
11
|
-
Você pode usar a CLI com npx ou bunx.
|
|
12
|
-
|
|
13
|
-
### Gerar uma nova migração
|
|
14
|
-
Para gerar uma nova migração, use o comando
|
|
15
|
-
```bash
|
|
16
|
-
bunx cli migration:generate
|
|
17
|
-
```
|
|
18
|
-
Isso irá gerar um novo arquivo de migração com uma diferença.
|
|
19
|
-
|
|
20
|
-
### Executar todas as migrações pendentes
|
|
21
|
-
Para executar todas as migrações pendentes, use o comando
|
|
22
|
-
```bash
|
|
23
|
-
bunx cli migration:run
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
### Contribuição
|
|
1
|
+
## Cheetah.js CLI
|
|
2
|
+
The CLI (Command Line Interface) for Cheetah.js.
|
|
3
|
+
|
|
4
|
+
### Instalação
|
|
5
|
+
To install, you can use npm or bun:
|
|
6
|
+
```bash
|
|
7
|
+
bun install @cheetahjs/cli
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
### Uso
|
|
11
|
+
Você pode usar a CLI com npx ou bunx.
|
|
12
|
+
|
|
13
|
+
### Gerar uma nova migração
|
|
14
|
+
Para gerar uma nova migração, use o comando
|
|
15
|
+
```bash
|
|
16
|
+
bunx cli migration:generate
|
|
17
|
+
```
|
|
18
|
+
Isso irá gerar um novo arquivo de migração com uma diferença.
|
|
19
|
+
|
|
20
|
+
### Executar todas as migrações pendentes
|
|
21
|
+
Para executar todas as migrações pendentes, use o comando
|
|
22
|
+
```bash
|
|
23
|
+
bunx cli migration:run
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Contribuição
|
|
27
27
|
Contribuições são bem-vindas. Por favor, abra um problema ou faça um pull request com suas mudanças.
|
package/cheetah.config.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { ConnectionSettings, PgDriver } from '@cheetah.js/orm';
|
|
2
|
-
|
|
3
|
-
const config: ConnectionSettings = {
|
|
4
|
-
host: 'localhost',
|
|
5
|
-
port: 5432,
|
|
6
|
-
database: 'postgres',
|
|
7
|
-
username: 'postgres',
|
|
8
|
-
password: 'postgres',
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
10
|
-
// @ts-ignore
|
|
11
|
-
driver: PgDriver,
|
|
12
|
-
migrationPath: '/database/migration',
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export default config;
|
|
1
|
+
import { ConnectionSettings, PgDriver } from '@cheetah.js/orm';
|
|
2
|
+
|
|
3
|
+
const config: ConnectionSettings = {
|
|
4
|
+
host: 'localhost',
|
|
5
|
+
port: 5432,
|
|
6
|
+
database: 'postgres',
|
|
7
|
+
username: 'postgres',
|
|
8
|
+
password: 'postgres',
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
driver: PgDriver,
|
|
12
|
+
migrationPath: '/database/migration',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default config;
|
package/package.json
CHANGED
|
@@ -1,42 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@cheetah.js/cli",
|
|
3
|
-
"description": "Cheetah.js CLI - Generate migrations, models, controllers and more. (In development)",
|
|
4
|
-
"version": "0.1.
|
|
5
|
-
"module": "dist/index.js",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\""
|
|
10
|
-
},
|
|
11
|
-
"dependencies": {
|
|
12
|
-
"commander": "^11.1.0",
|
|
13
|
-
"knex": "^3.0.1",
|
|
14
|
-
"ts-node": "^10.9.1"
|
|
15
|
-
},
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"@typescript-eslint/eslint-plugin": "^6.12.0",
|
|
18
|
-
"@typescript-eslint/parser": "^6.12.0",
|
|
19
|
-
"bun-types": "latest",
|
|
20
|
-
"eslint": "^8.54.0",
|
|
21
|
-
"eslint-config-prettier": "^9.0.0",
|
|
22
|
-
"eslint-plugin-prettier": "^5.0.1",
|
|
23
|
-
"pg": "^8.11.3",
|
|
24
|
-
"prettier": "^3.1.0",
|
|
25
|
-
"reflect-metadata": "^0.1.13"
|
|
26
|
-
},
|
|
27
|
-
"bin":
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@cheetah.js/cli",
|
|
3
|
+
"description": "Cheetah.js CLI - Generate migrations, models, controllers and more. (In development)",
|
|
4
|
+
"version": "0.1.18",
|
|
5
|
+
"module": "dist/index.js",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\""
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"commander": "^11.1.0",
|
|
13
|
+
"knex": "^3.0.1",
|
|
14
|
+
"ts-node": "^10.9.1"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@typescript-eslint/eslint-plugin": "^6.12.0",
|
|
18
|
+
"@typescript-eslint/parser": "^6.12.0",
|
|
19
|
+
"bun-types": "latest",
|
|
20
|
+
"eslint": "^8.54.0",
|
|
21
|
+
"eslint-config-prettier": "^9.0.0",
|
|
22
|
+
"eslint-plugin-prettier": "^5.0.1",
|
|
23
|
+
"pg": "^8.11.3",
|
|
24
|
+
"prettier": "^3.1.0",
|
|
25
|
+
"reflect-metadata": "^0.1.13"
|
|
26
|
+
},
|
|
27
|
+
"bin": {
|
|
28
|
+
"cheetah": "./dist/run-cli.sh"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@cheetah.js/core": "^0.1.34",
|
|
32
|
+
"@cheetah.js/orm": "^0.1.42",
|
|
33
|
+
"typescript": "^5.0.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependenciesMeta": {
|
|
36
|
+
"pg": {
|
|
37
|
+
"optional": true
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"author": "Lucas Mota"
|
|
44
|
+
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<project version="4">
|
|
3
|
-
<component name="GitToolBoxProjectSettings">
|
|
4
|
-
<option name="commitMessageIssueKeyValidationOverride">
|
|
5
|
-
<BoolValueOverride>
|
|
6
|
-
<option name="enabled" value="true" />
|
|
7
|
-
</BoolValueOverride>
|
|
8
|
-
</option>
|
|
9
|
-
<option name="commitMessageValidationEnabledOverride">
|
|
10
|
-
<BoolValueOverride>
|
|
11
|
-
<option name="enabled" value="true" />
|
|
12
|
-
</BoolValueOverride>
|
|
13
|
-
</option>
|
|
14
|
-
</component>
|
|
15
|
-
</project>
|
package/bun.lockb
DELETED
|
Binary file
|
package/dist/cli.d.ts
DELETED
package/dist/cli.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
require("reflect-metadata");
|
|
5
|
-
const commander_1 = require("commander");
|
|
6
|
-
const migrator_1 = require("./migrator/migrator");
|
|
7
|
-
const program = new commander_1.Command();
|
|
8
|
-
program.name('[npx|bunx] cli').description('CLI to Cheetah.js ORM ');
|
|
9
|
-
program
|
|
10
|
-
.command('migration:generate')
|
|
11
|
-
.description('generate a new migration file with a diff')
|
|
12
|
-
.action(async (str, options) => {
|
|
13
|
-
const migrator = new migrator_1.Migrator();
|
|
14
|
-
migrator.useTsNode();
|
|
15
|
-
await migrator.generateMigration();
|
|
16
|
-
process.exit(0);
|
|
17
|
-
});
|
|
18
|
-
program
|
|
19
|
-
.command('migration:run')
|
|
20
|
-
.description('run all pending migrations')
|
|
21
|
-
.action(async (str, options) => {
|
|
22
|
-
const migrator = new migrator_1.Migrator();
|
|
23
|
-
migrator.useTsNode();
|
|
24
|
-
await migrator.migrate();
|
|
25
|
-
process.exit(0);
|
|
26
|
-
});
|
|
27
|
-
program.parse();
|
|
28
|
-
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;;AAEA,4BAA0B;AAC1B,yCAAoC;AACpC,kDAA+C;AAE/C,MAAM,OAAO,GAAG,IAAI,mBAAO,EAAE,CAAC;AAE9B,OAAO,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,WAAW,CAAC,wBAAwB,CAAC,CAAC;AAErE,OAAO;KACJ,OAAO,CAAC,oBAAoB,CAAC;KAC7B,WAAW,CAAC,2CAA2C,CAAC;KACxD,MAAM,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE;IAC7B,MAAM,QAAQ,GAAG,IAAI,mBAAQ,EAAE,CAAC;IAChC,QAAQ,CAAC,SAAS,EAAE,CAAC;IACrB,MAAM,QAAQ,CAAC,iBAAiB,EAAE,CAAC;IACnC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC;AAEL,OAAO;KACJ,OAAO,CAAC,eAAe,CAAC;KACxB,WAAW,CAAC,4BAA4B,CAAC;KACzC,MAAM,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE;IAC7B,MAAM,QAAQ,GAAG,IAAI,mBAAQ,EAAE,CAAC;IAChC,QAAQ,CAAC,SAAS,EAAE,CAAC;IACrB,MAAM,QAAQ,CAAC,OAAO,EAAE,CAAC;IACzB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC;AAEL,OAAO,CAAC,KAAK,EAAE,CAAC"}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.CheetahMigration = void 0;
|
|
13
|
-
const orm_1 = require("@cheetah.js/orm");
|
|
14
|
-
let CheetahMigration = class CheetahMigration extends orm_1.BaseEntity {
|
|
15
|
-
constructor() {
|
|
16
|
-
super(...arguments);
|
|
17
|
-
this.createdAt = new Date();
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
exports.CheetahMigration = CheetahMigration;
|
|
21
|
-
__decorate([
|
|
22
|
-
(0, orm_1.PrimaryKey)(),
|
|
23
|
-
__metadata("design:type", String)
|
|
24
|
-
], CheetahMigration.prototype, "migrationFile", void 0);
|
|
25
|
-
__decorate([
|
|
26
|
-
(0, orm_1.Property)(),
|
|
27
|
-
__metadata("design:type", Date)
|
|
28
|
-
], CheetahMigration.prototype, "createdAt", void 0);
|
|
29
|
-
exports.CheetahMigration = CheetahMigration = __decorate([
|
|
30
|
-
(0, orm_1.Entity)()
|
|
31
|
-
], CheetahMigration);
|
|
32
|
-
//# sourceMappingURL=cheetah-migration.entity.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cheetah-migration.entity.js","sourceRoot":"","sources":["../../src/entities/cheetah-migration.entity.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAA2E;AAGpE,IAAM,gBAAgB,GAAtB,MAAM,gBAAiB,SAAQ,gBAAU;IAAzC;;QAKE,cAAS,GAAS,IAAI,IAAI,EAAE,CAAC;IACtC,CAAC;CAAA,CAAA;AANY,4CAAgB;AAEpB;IADN,IAAA,gBAAU,GAAE;;uDACgB;AAGtB;IADN,IAAA,cAAQ,GAAE;8BACO,IAAI;mDAAc;2BALzB,gBAAgB;IAD5B,IAAA,YAAM,GAAE;GACI,gBAAgB,CAM5B"}
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
require("reflect-metadata");
|
|
18
|
-
__exportStar(require("./migrator/migrator"), exports);
|
|
19
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4BAA0B;AAE1B,sDAAoC"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { DriverInterface, EntityStorage } from '@cheetah.js/orm';
|
|
2
|
-
import { ColDiff, ColumnsInfo, SnapshotTable, TableDiff } from './migrator';
|
|
3
|
-
export declare class DiffCalculator {
|
|
4
|
-
private driver;
|
|
5
|
-
private entities;
|
|
6
|
-
constructor(entities: EntityStorage, driver: DriverInterface);
|
|
7
|
-
diff(snapshotBd: SnapshotTable[], snapshotEntities: SnapshotTable[]): TableDiff[];
|
|
8
|
-
private checkIndexes;
|
|
9
|
-
private createNewColumn;
|
|
10
|
-
private diffColumnType;
|
|
11
|
-
private diffColumnDefault;
|
|
12
|
-
private diffColumnPrimary;
|
|
13
|
-
private diffColumnUnique;
|
|
14
|
-
private diffForeignKey;
|
|
15
|
-
private diffColumnSql;
|
|
16
|
-
diffColumnPrecisionAndScale(bdCol: ColumnsInfo, entityCol: ColumnsInfo, colDiffs: ColDiff[]): void;
|
|
17
|
-
private diffColumnNullable;
|
|
18
|
-
private convertEntityTypeToSqlType;
|
|
19
|
-
private diffEnum;
|
|
20
|
-
}
|
|
@@ -1,339 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DiffCalculator = void 0;
|
|
4
|
-
const orm_1 = require("@cheetah.js/orm");
|
|
5
|
-
class DiffCalculator {
|
|
6
|
-
constructor(entities, driver) {
|
|
7
|
-
this.driver = driver;
|
|
8
|
-
this.entities = entities;
|
|
9
|
-
}
|
|
10
|
-
diff(snapshotBd, snapshotEntities) {
|
|
11
|
-
const diffs = [];
|
|
12
|
-
const bdTablesMap = new Map(snapshotBd.map((table) => [table.tableName, table]));
|
|
13
|
-
const entityTablesMap = new Map(snapshotEntities.map((table) => [table.tableName, table]));
|
|
14
|
-
const allTableNames = new Set([
|
|
15
|
-
...bdTablesMap.keys(),
|
|
16
|
-
...entityTablesMap.keys(),
|
|
17
|
-
]);
|
|
18
|
-
allTableNames.forEach((tableName) => {
|
|
19
|
-
const bdTable = bdTablesMap.get(tableName);
|
|
20
|
-
const entityTable = entityTablesMap.get(tableName);
|
|
21
|
-
if (!entityTable) {
|
|
22
|
-
diffs.push({
|
|
23
|
-
tableName,
|
|
24
|
-
colDiffs: [{ actionType: 'DELETE', colName: '*' }],
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
else if (!bdTable) {
|
|
28
|
-
const colDiffs = entityTable.columns.flatMap((c) => {
|
|
29
|
-
return this.createNewColumn(c, []);
|
|
30
|
-
});
|
|
31
|
-
diffs.push({
|
|
32
|
-
tableName,
|
|
33
|
-
newTable: true,
|
|
34
|
-
schema: entityTable.schema ?? 'public',
|
|
35
|
-
colDiffs,
|
|
36
|
-
});
|
|
37
|
-
this.checkIndexes(bdTable, entityTable, colDiffs);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
const colDiffs = [];
|
|
41
|
-
const bdColumnsMap = new Map(bdTable.columns.map((col) => [col.name, col]));
|
|
42
|
-
const entityColumnsMap = new Map(entityTable.columns.map((col) => [col.name, col]));
|
|
43
|
-
const allColumnNames = new Set([
|
|
44
|
-
...bdColumnsMap.keys(),
|
|
45
|
-
...entityColumnsMap.keys(),
|
|
46
|
-
]);
|
|
47
|
-
allColumnNames.forEach((colName) => {
|
|
48
|
-
const bdCol = bdColumnsMap.get(colName);
|
|
49
|
-
const entityCol = entityColumnsMap.get(colName);
|
|
50
|
-
if (!entityCol) {
|
|
51
|
-
colDiffs.push({
|
|
52
|
-
actionType: 'DELETE',
|
|
53
|
-
colName: bdCol.name,
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
else if (!bdCol) {
|
|
57
|
-
this.createNewColumn(entityCol, colDiffs);
|
|
58
|
-
}
|
|
59
|
-
else
|
|
60
|
-
this.diffColumnSql(bdCol, entityCol, colDiffs);
|
|
61
|
-
});
|
|
62
|
-
this.checkIndexes(bdTable, entityTable, colDiffs);
|
|
63
|
-
if (colDiffs.length > 0) {
|
|
64
|
-
diffs.push({
|
|
65
|
-
tableName: tableName,
|
|
66
|
-
schema: entityTable.schema ?? 'public',
|
|
67
|
-
colDiffs,
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
return diffs;
|
|
73
|
-
}
|
|
74
|
-
checkIndexes(bdTable, entityTable, colDiffs) {
|
|
75
|
-
if ((bdTable && bdTable.indexes) || (entityTable && entityTable.indexes)) {
|
|
76
|
-
if (!bdTable || !bdTable.indexes) {
|
|
77
|
-
colDiffs.push({
|
|
78
|
-
actionType: 'INDEX',
|
|
79
|
-
colName: '*',
|
|
80
|
-
indexTables: entityTable.indexes.map((index) => ({
|
|
81
|
-
name: index.indexName,
|
|
82
|
-
properties: index.columnName.split(','),
|
|
83
|
-
})),
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
if (!entityTable || !entityTable.indexes) {
|
|
87
|
-
colDiffs.push({
|
|
88
|
-
actionType: 'INDEX',
|
|
89
|
-
colName: '*',
|
|
90
|
-
indexTables: bdTable.indexes.map((index) => ({ name: index.indexName })),
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (bdTable && bdTable.indexes && entityTable && entityTable.indexes) {
|
|
95
|
-
const bdIndexesMap = new Map(bdTable.indexes.map((index) => [index.indexName, index]));
|
|
96
|
-
const entityIndexesMap = new Map(entityTable.indexes.map((index) => [index.indexName, index]));
|
|
97
|
-
const allIndexes = new Set([
|
|
98
|
-
...bdIndexesMap.keys(),
|
|
99
|
-
...entityIndexesMap.keys(),
|
|
100
|
-
]);
|
|
101
|
-
allIndexes.forEach((indexName) => {
|
|
102
|
-
const bdIndex = bdIndexesMap.get(indexName);
|
|
103
|
-
const entityIndex = entityIndexesMap.get(indexName);
|
|
104
|
-
if (!entityIndex) {
|
|
105
|
-
colDiffs.push({
|
|
106
|
-
actionType: 'INDEX',
|
|
107
|
-
colName: bdIndex.columnName,
|
|
108
|
-
indexTables: [{ name: indexName }],
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
else if (!bdIndex) {
|
|
112
|
-
colDiffs.push({
|
|
113
|
-
actionType: 'INDEX',
|
|
114
|
-
colName: entityIndex.columnName,
|
|
115
|
-
indexTables: [
|
|
116
|
-
{ name: indexName, properties: entityIndex.columnName.split(',') },
|
|
117
|
-
],
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
createNewColumn(entityCol, colDiffs) {
|
|
124
|
-
const colType = this.convertEntityTypeToSqlType(entityCol.type);
|
|
125
|
-
colDiffs.push({
|
|
126
|
-
actionType: 'CREATE',
|
|
127
|
-
colName: entityCol.name,
|
|
128
|
-
colType: colType.type,
|
|
129
|
-
colLength: entityCol.length ?? colType.len,
|
|
130
|
-
colChanges: {
|
|
131
|
-
autoIncrement: entityCol.autoIncrement,
|
|
132
|
-
default: entityCol.default,
|
|
133
|
-
primary: entityCol.primary,
|
|
134
|
-
unique: entityCol.unique,
|
|
135
|
-
nullable: entityCol.nullable,
|
|
136
|
-
enumItems: entityCol.enumItems,
|
|
137
|
-
foreignKeys: entityCol.foreignKeys ?? undefined,
|
|
138
|
-
precision: entityCol.precision ?? undefined,
|
|
139
|
-
scale: entityCol.scale ?? undefined,
|
|
140
|
-
},
|
|
141
|
-
});
|
|
142
|
-
return colDiffs;
|
|
143
|
-
}
|
|
144
|
-
diffColumnType(bdCol, entityCol, colDiffs) {
|
|
145
|
-
const isPostgres = this.driver instanceof orm_1.PgDriver;
|
|
146
|
-
if (bdCol.type === 'USER-DEFINED') {
|
|
147
|
-
bdCol.type = 'enum';
|
|
148
|
-
}
|
|
149
|
-
const colT = this.convertEntityTypeToSqlType(entityCol.type);
|
|
150
|
-
const colType = colT.type;
|
|
151
|
-
let length = entityCol.length ?? colT.len;
|
|
152
|
-
if (colType === 'integer' && isPostgres) {
|
|
153
|
-
length = 32;
|
|
154
|
-
}
|
|
155
|
-
if (bdCol.isDecimal && isPostgres) {
|
|
156
|
-
bdCol.type = 'decimal';
|
|
157
|
-
length = bdCol.precision ?? 0;
|
|
158
|
-
}
|
|
159
|
-
if (!bdCol.type.includes(colType) || bdCol.length !== length) {
|
|
160
|
-
colDiffs.push({
|
|
161
|
-
actionType: 'ALTER',
|
|
162
|
-
colName: entityCol.name,
|
|
163
|
-
colType: colType,
|
|
164
|
-
colLength: length,
|
|
165
|
-
colChanges: {
|
|
166
|
-
enumItems: entityCol.enumItems || undefined,
|
|
167
|
-
precision: entityCol.precision ?? undefined,
|
|
168
|
-
scale: entityCol.scale ?? undefined,
|
|
169
|
-
},
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
diffColumnDefault(bdCol, entityCol, colDiffs) {
|
|
174
|
-
if (bdCol.default !== (entityCol.default ?? null)) {
|
|
175
|
-
colDiffs.push({
|
|
176
|
-
actionType: 'ALTER',
|
|
177
|
-
colName: entityCol.name,
|
|
178
|
-
colChanges: { default: entityCol.default },
|
|
179
|
-
colLength: entityCol.length,
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
diffColumnPrimary(bdCol, entityCol, colDiffs) {
|
|
184
|
-
if (bdCol.primary !== (entityCol.primary ?? false)) {
|
|
185
|
-
colDiffs.push({
|
|
186
|
-
actionType: 'ALTER',
|
|
187
|
-
colName: entityCol.name,
|
|
188
|
-
colChanges: { primary: entityCol.primary },
|
|
189
|
-
colLength: entityCol.length,
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
diffColumnUnique(bdCol, entityCol, colDiffs) {
|
|
194
|
-
if (bdCol.unique !== (entityCol.unique ?? false)) {
|
|
195
|
-
if (bdCol.unique === false && entityCol.unique === undefined) {
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
if (entityCol.primary) {
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
colDiffs.push({
|
|
202
|
-
actionType: 'ALTER',
|
|
203
|
-
colName: entityCol.name,
|
|
204
|
-
colChanges: { unique: entityCol.unique || false },
|
|
205
|
-
colLength: entityCol.length,
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
diffForeignKey(bdCol, entityCol, colDiffs) {
|
|
210
|
-
if (bdCol.foreignKeys || entityCol.foreignKeys) {
|
|
211
|
-
const bdFKMap = new Map((bdCol.foreignKeys || []).map((fk) => [
|
|
212
|
-
`${fk.referencedTableName}.${fk.referencedColumnName}`,
|
|
213
|
-
fk,
|
|
214
|
-
]));
|
|
215
|
-
const entityFKMap = new Map((entityCol.foreignKeys || []).map((fk) => [
|
|
216
|
-
`${fk.referencedTableName}.${fk.referencedColumnName}`,
|
|
217
|
-
fk,
|
|
218
|
-
]));
|
|
219
|
-
const allFKs = new Set([...bdFKMap.keys(), ...entityFKMap.keys()]);
|
|
220
|
-
allFKs.forEach((fkName) => {
|
|
221
|
-
const bdFK = bdFKMap.get(fkName);
|
|
222
|
-
const entityFK = entityFKMap.get(fkName);
|
|
223
|
-
if (!entityFK) {
|
|
224
|
-
const fks = bdCol.foreignKeys?.filter((fk) => fk !== bdFK);
|
|
225
|
-
colDiffs.push({
|
|
226
|
-
actionType: 'ALTER',
|
|
227
|
-
colName: bdCol.name,
|
|
228
|
-
colChanges: {
|
|
229
|
-
foreignKeys: fks.length > 0 ? fks : [],
|
|
230
|
-
},
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
diffColumnSql(bdCol, entityCol, colDiffs) {
|
|
237
|
-
this.diffForeignKey(bdCol, entityCol, colDiffs);
|
|
238
|
-
this.diffEnum(bdCol, entityCol, colDiffs);
|
|
239
|
-
this.diffColumnType(bdCol, entityCol, colDiffs);
|
|
240
|
-
this.diffColumnDefault(bdCol, entityCol, colDiffs);
|
|
241
|
-
this.diffColumnPrimary(bdCol, entityCol, colDiffs);
|
|
242
|
-
this.diffColumnUnique(bdCol, entityCol, colDiffs);
|
|
243
|
-
this.diffColumnNullable(bdCol, entityCol, colDiffs);
|
|
244
|
-
this.diffColumnPrecisionAndScale(bdCol, entityCol, colDiffs);
|
|
245
|
-
return colDiffs;
|
|
246
|
-
}
|
|
247
|
-
diffColumnPrecisionAndScale(bdCol, entityCol, colDiffs) {
|
|
248
|
-
const bdPrecision = bdCol.precision ?? 0;
|
|
249
|
-
const entityPrecision = entityCol.precision ?? bdPrecision;
|
|
250
|
-
const bdScale = bdCol.scale ?? 0;
|
|
251
|
-
const entityScale = entityCol.scale ?? bdCol.scale;
|
|
252
|
-
if (bdCol.isDecimal &&
|
|
253
|
-
(bdPrecision !== entityPrecision ||
|
|
254
|
-
(bdCol.isDecimal && bdScale !== entityScale))) {
|
|
255
|
-
colDiffs.push({
|
|
256
|
-
actionType: 'ALTER',
|
|
257
|
-
colType: 'decimal',
|
|
258
|
-
colName: entityCol.name,
|
|
259
|
-
colChanges: {
|
|
260
|
-
precision: entityCol.precision ?? 0,
|
|
261
|
-
scale: entityCol.scale ?? 0,
|
|
262
|
-
},
|
|
263
|
-
colLength: entityCol.length,
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
diffColumnNullable(bdCol, entityCol, colDiffs) {
|
|
268
|
-
if (bdCol.nullable !== (entityCol.nullable ?? false)) {
|
|
269
|
-
colDiffs.push({
|
|
270
|
-
actionType: 'ALTER',
|
|
271
|
-
colName: entityCol.name,
|
|
272
|
-
colChanges: { nullable: entityCol.nullable ?? false },
|
|
273
|
-
colLength: entityCol.length,
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
convertEntityTypeToSqlType(entityType) {
|
|
278
|
-
switch (entityType) {
|
|
279
|
-
case 'Number':
|
|
280
|
-
case 'int':
|
|
281
|
-
return { type: 'integer', len: 32 };
|
|
282
|
-
case 'bigint':
|
|
283
|
-
return { type: 'bigint' };
|
|
284
|
-
case 'float':
|
|
285
|
-
case 'double':
|
|
286
|
-
case 'decimal':
|
|
287
|
-
return { type: 'decimal' };
|
|
288
|
-
case 'String':
|
|
289
|
-
case 'varchar':
|
|
290
|
-
return { type: 'character varying', len: 255 };
|
|
291
|
-
case 'Boolean':
|
|
292
|
-
return { type: 'boolean', len: null };
|
|
293
|
-
case 'Date':
|
|
294
|
-
return { type: 'timestamp' };
|
|
295
|
-
case 'Object':
|
|
296
|
-
return { type: 'json', len: null };
|
|
297
|
-
case 'uuid':
|
|
298
|
-
return { type: 'uuid', len: null };
|
|
299
|
-
case 'text':
|
|
300
|
-
return { type: 'text' };
|
|
301
|
-
case 'enum':
|
|
302
|
-
return { type: 'enum', len: null };
|
|
303
|
-
default:
|
|
304
|
-
return { type: 'character varying', len: 255 };
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
diffEnum(bdCol, entityCol, colDiffs) {
|
|
308
|
-
if (bdCol.enumItems || entityCol.enumItems) {
|
|
309
|
-
if (bdCol.enumItems && entityCol.enumItems) {
|
|
310
|
-
const allEnums = new Set([...bdCol.enumItems, ...entityCol.enumItems]);
|
|
311
|
-
const differences = [...allEnums].filter((x) => !bdCol.enumItems?.includes(x) || !entityCol.enumItems?.includes(x));
|
|
312
|
-
if (differences.length === 0) {
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
colDiffs.push({
|
|
316
|
-
actionType: 'ALTER',
|
|
317
|
-
colName: entityCol.name,
|
|
318
|
-
colType: 'enum',
|
|
319
|
-
colChanges: {
|
|
320
|
-
enumItems: entityCol.enumItems,
|
|
321
|
-
},
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
if (!entityCol.enumItems) {
|
|
325
|
-
}
|
|
326
|
-
else if (!bdCol.enumItems) {
|
|
327
|
-
colDiffs.push({
|
|
328
|
-
actionType: 'CREATE',
|
|
329
|
-
colName: entityCol.name,
|
|
330
|
-
colChanges: {
|
|
331
|
-
enumItems: entityCol.enumItems,
|
|
332
|
-
},
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
exports.DiffCalculator = DiffCalculator;
|
|
339
|
-
//# sourceMappingURL=diff-calculator.js.map
|