Skip to content

Commit f755c0e

Browse files
oceanlvrysfscream
authored andcommitted
feat(typeorm): add typeorm CLI and refactor typeorm config
1 parent 75cd8c5 commit f755c0e

File tree

7 files changed

+311
-39
lines changed

7 files changed

+311
-39
lines changed

ormconfig.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { join } from 'path'
2+
import fs from 'fs-extra'
3+
import Connections from './src/database/models/Connections'
4+
import getAppDataPath from 'appdata-path'
5+
6+
const STORE_PATH = getAppDataPath('MQTTX')
7+
8+
try {
9+
if (!fs.pathExistsSync(STORE_PATH)) {
10+
fs.mkdirpSync(STORE_PATH)
11+
}
12+
} catch (err) {
13+
console.error(err)
14+
}
15+
16+
const config = {
17+
type: 'sqlite',
18+
name: 'MQTTX',
19+
driver: 'sqlite',
20+
synchronize: false,
21+
entities: [Connections],
22+
database: join(STORE_PATH, 'MQTTX.db'),
23+
migrations: [join(__dirname, './src/database/migration/**/*{.ts,.js}')],
24+
migrationsTableName: 'temp_migration_table',
25+
cli: {
26+
migrationsDir: join(__dirname, './src/database/migration'),
27+
subscribersDir: join(__dirname, './src/database/subscriber'),
28+
entitiesDir: join(__dirname, './src/database/models'),
29+
},
30+
}
31+
export = config

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
"description": "MQTT desktop client",
55
"author": "EMQ X Team <[email protected]>",
66
"scripts": {
7+
"db:log": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm schema:log -c MQTTX -f ormconfig.ts",
8+
"db:migration:show": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:show -c MQTTX -f ormconfig.ts",
9+
"db:migration:generate": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:generate -f ormconfig.ts -c MQTTX --pretty",
10+
"db:migration:run": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:run -c MQTTX -f ormconfig.ts",
11+
"db:migration:revert": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:revert -f ormconfig.ts -c MQTTX",
12+
"db:migration:sync": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm schema:sync -f ormconfig.ts -c MQTTX",
13+
"db:diagram": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm-uml ormconfig.ts -c MQTTX -d ./src/database/database.png",
714
"version": "yarn run gen:version && git add -A .",
815
"serve": "vue-cli-service serve",
916
"build": "vue-cli-service build",
@@ -22,6 +29,7 @@
2229
},
2330
"main": "background.js",
2431
"dependencies": {
32+
"appdata-path": "^1.0.0",
2533
"axios": "^0.21.1",
2634
"chart.js": "^2.9.4",
2735
"core-js": "^2.6.11",
@@ -82,6 +90,7 @@
8290
"prettier": "^2.0.5",
8391
"sass": "~1.32.13",
8492
"sass-loader": "^8.0.2",
93+
"typeorm-uml": "^1.6.4",
8594
"typescript": "^3.7.4",
8695
"vue-cli-plugin-electron-builder": "^2.0.0",
8796
"vue-template-compiler": "^2.6.12"
@@ -102,4 +111,4 @@
102111
},
103112
"license": "Apache",
104113
"repository": "https://github.com/emqx/MQTTX"
105-
}
114+
}

src/database/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Database Migration
2+
3+
```shell
4+
# To Show Migration SQL queries
5+
# Check what sql queries db:sync is going to run use
6+
yarn run db:log
7+
8+
# To show all migrations and whether they've been run or not
9+
yarn run db:migration:show
10+
11+
# Automatic migration generation creates a new migration file and writes all sql queries that must be executed to update the database.
12+
yarn run db:migration:generate -n ${migration_name}
13+
14+
# generate a ER database diagram
15+
yarn run db:diagram
16+
```
17+
18+
dangerous CLI, do some effect in database.
19+
20+
```shell
21+
# Dangerous, Development Only!
22+
# sync current models to database
23+
yarn run db:migration:sync
24+
25+
# To execute all pending migrations | To revert the most recently executed migration
26+
yarn run db:migration:[run|revert]
27+
```
28+
29+
If your table models is not stable, and there are development mode without any data. you could just use:
30+
31+
1. `yarn run db:log` check CLI sync execute SQL queries
32+
2. `yarn run db:diagram` get a latest ER diagram
33+
3. `db:migration:sync` sync models to database
34+
35+
Otherwise, Please follow bellow steps and make sure what you do. Or there is a high risk to lost data.
36+
37+
1. `yarn run db:log` check CLI sync execute SQL queries
38+
2. `db:migration:generate -n {migration_name}` Automatic migration generate
39+
3. `db:migration:run` make pending migrations executed. NOTICE: you should execute this code before the APP open since the table model changed.
40+
4. `db:diagram` get a latest ER diagram.
41+
42+
Please use `db:migration:revert` to revert changes.
43+
44+
executing `db:migration:run`, `db:migration:sync` carefully.
45+
46+
Reference:
47+
48+
- [typeorm migrations](https://github.com/typeorm/typeorm/blob/master/docs/migrations.md#creating-a-new-migration)
49+
- [typeorm CLI](https://github.com/typeorm/typeorm/blob/master/docs/using-cli.md#installing-cli)

src/database/ormconfig.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/database/useSqlite.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { ConnectionOptions, createConnection, EntityTarget } from 'typeorm'
1+
import { createConnection, EntityTarget, getConnectionOptions } from 'typeorm'
22

3-
export default function useSqlite(config: ConnectionOptions) {
3+
export default function useSqlite() {
44
const insert = async <T>(data: T, model: EntityTarget<T>): Promise<T> => {
5+
const config = await getConnectionOptions('MQTTX')
56
const db = await createConnection(config)
67
const repository = db.getRepository(model)
78
return repository.save(data)

tsconfig.commonjs.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "commonjs"
5+
}
6+
}

0 commit comments

Comments
 (0)