diff --git a/ormconfig.ts b/ormconfig.ts index cf6470b5d..b04d0a936 100644 --- a/ormconfig.ts +++ b/ormconfig.ts @@ -1,49 +1,2 @@ -import { join } from 'path' -import fs from 'fs-extra' -import getAppDataPath from 'appdata-path' - -import ConnectionEntity from './src/database/models/ConnectionEntity' -import MessageEntity from './src/database/models/MessageEntity' -import SubscriptionEntity from './src/database/models/SubscriptionEntity' -import ScriptEntity from './src/database/models/ScriptEntity' -import SettingEntity from './src/database/models/SettingEntity' -import CollectionEntity from './src/database/models/CollectionEntity' -import HistoryMessageHeaderEntity from './src/database/models/HistoryMessageHeaderEntity' -import HistoryMessagePayloadEntity from './src/database/models/HistoryMessagePayloadEntity' - -const STORE_PATH = getAppDataPath('MQTTX') - -try { - if (!fs.pathExistsSync(STORE_PATH)) { - fs.mkdirpSync(STORE_PATH) - } -} catch (err) { - console.error(err) -} - -const config = { - type: 'sqlite', - name: 'MQTTX', - driver: 'sqlite', - synchronize: false, - logging: process.env.NODE_ENV !== 'production', - entities: [ - ConnectionEntity, - MessageEntity, - SubscriptionEntity, - ScriptEntity, - SettingEntity, - CollectionEntity, - HistoryMessageHeaderEntity, - HistoryMessagePayloadEntity, - ], - database: join(STORE_PATH, 'MQTTX.db'), - migrations: [join(__dirname, './src/database/migration/**/*{.ts,.js}')], - migrationsTableName: 'temp_migration_table', - cli: { - migrationsDir: join(__dirname, './src/database/migration'), - subscribersDir: join(__dirname, './src/database/subscriber'), - entitiesDir: join(__dirname, './src/database/models'), - }, -} -export = config +const ORMConfig = require('./src/database/database.config') +module.exports = ORMConfig.default diff --git a/package.json b/package.json index 7d62a48b0..2487b7d67 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,6 @@ "description": "MQTT desktop client", "author": "EMQ X Team ", "scripts": { - "db:log": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm schema:log -c MQTTX -f ormconfig.ts", - "db:migration:show": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:show -c MQTTX -f ormconfig.ts", - "db:migration:generate": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:generate -f ormconfig.ts -c MQTTX --pretty", - "db:migration:run": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:run -c MQTTX -f ormconfig.ts", - "db:migration:revert": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:revert -f ormconfig.ts -c MQTTX", - "db:migration:sync": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm schema:sync -f ormconfig.ts -c MQTTX", - "db:diagram": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm-uml ormconfig.ts -c MQTTX -d ./src/database/database.png", "version": "yarn run gen:version && git add -A .", "serve": "vue-cli-service serve", "build": "vue-cli-service build", @@ -25,7 +18,14 @@ "format": "prettier --write \"src/**/*.ts\" \"src/**/*.vue\" \"src/**/*.scss\"", "lint": "eslint --fix --ext .ts,.vue src", "test:e2e": "vue-cli-service test:e2e", - "test:unit": "vue-cli-service test:unit" + "test:unit": "vue-cli-service test:unit", + "db:log": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm schema:log -c MQTTX -f ormconfig.ts", + "db:migration:show": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:show -c MQTTX -f ormconfig.ts", + "db:migration:generate": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:generate -f ormconfig.ts -c MQTTX --pretty", + "db:migration:run": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:run -c MQTTX -f ormconfig.ts", + "db:migration:revert": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm migration:revert -f ormconfig.ts -c MQTTX", + "db:migration:sync": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm schema:sync -f ormconfig.ts -c MQTTX", + "db:diagram": "TS_NODE_PROJECT=tsconfig.commonjs.json ts-node ./node_modules/.bin/typeorm-uml ormconfig.ts -c MQTTX -d ./src/database/database.png" }, "main": "background.js", "dependencies": { diff --git a/src/database/database.config.ts b/src/database/database.config.ts new file mode 100644 index 000000000..730cae4e6 --- /dev/null +++ b/src/database/database.config.ts @@ -0,0 +1,49 @@ +import { join } from 'path' +import fs from 'fs-extra' +import { getAppDataPath } from 'appdata-path' +const STORE_PATH = getAppDataPath('MQTTX') +import ConnectionEntity from './models/ConnectionEntity' +import MessageEntity from './models/MessageEntity' +import SubscriptionEntity from './models/SubscriptionEntity' +import ScriptEntity from './models/ScriptEntity' +import SettingEntity from './models/SettingEntity' +import CollectionEntity from './models/CollectionEntity' +import HistoryMessageHeaderEntity from './models/HistoryMessageHeaderEntity' +import HistoryMessagePayloadEntity from './models/HistoryMessagePayloadEntity' +import { ConnectionOptions } from 'typeorm' + +try { + if (!fs.pathExistsSync(STORE_PATH)) { + fs.mkdirpSync(STORE_PATH) + } +} catch (err) { + console.error(err) +} + +const ORMConfig = { + type: 'sqlite', + name: 'MQTTX', + driver: 'sqlite', + synchronize: false, + logging: process.env.NODE_ENV !== 'production', + database: join(STORE_PATH, 'MQTTX.db'), + migrations: ['database/migration/*{.ts,.js}'], + migrationsTableName: 'temp_migration_table', + migrationsRun: true, + entities: [ + ConnectionEntity, + MessageEntity, + SubscriptionEntity, + ScriptEntity, + SettingEntity, + CollectionEntity, + HistoryMessageHeaderEntity, + HistoryMessagePayloadEntity, + ], + cli: { + migrationsDir: 'src/database/migration', + subscribersDir: 'src/database/subscriber', + entitiesDir: 'src/database/models', + }, +} as ConnectionOptions +export default ORMConfig diff --git a/src/database/database.png b/src/database/database.png deleted file mode 100644 index 3ad193a9a..000000000 Binary files a/src/database/database.png and /dev/null differ diff --git a/src/database/models/ConnectionEntity.ts b/src/database/models/ConnectionEntity.ts index b66126f37..1ef8a486a 100644 --- a/src/database/models/ConnectionEntity.ts +++ b/src/database/models/ConnectionEntity.ts @@ -53,15 +53,9 @@ export default class ConnectionEntity { @Column({ type: 'varchar' }) mqttVersion?: string - @Column({ type: 'number' }) + @Column({ type: 'integer' }) unreadMessageCount?: number - // @Column() - // client!: string//TODO - - // @Column() - // will!: string //TODO - @Column({ type: 'boolean' }) clientIdWithTime!: boolean diff --git a/src/database/models/SettingEntity.ts b/src/database/models/SettingEntity.ts index 649ff3516..4fd8f9e01 100644 --- a/src/database/models/SettingEntity.ts +++ b/src/database/models/SettingEntity.ts @@ -1,4 +1,4 @@ -import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm' +import { Entity, PrimaryGeneratedColumn, Column, UpdateDateColumn, CreateDateColumn } from 'typeorm' @Entity('SettingEntity') export default class SettingEntity { @@ -15,7 +15,4 @@ export default class SettingEntity { // 0: text 1: number 2: boolean @Column({ type: 'integer' }) settingType!: number - - @Column({ type: 'timestamp' }) - updateTime!: string } diff --git a/src/database/useSqlite.ts b/src/database/useSqlite.ts index 4bdf23044..49bddd391 100644 --- a/src/database/useSqlite.ts +++ b/src/database/useSqlite.ts @@ -1,9 +1,9 @@ -import { createConnection, EntityTarget, getConnectionOptions } from 'typeorm' +import { createConnection, EntityTarget } from 'typeorm' +import ORMConfig from './database.config' export default function useSqlite() { const insert = async (data: T, model: EntityTarget): Promise => { - const config = await getConnectionOptions('MQTTX') - const db = await createConnection(config) + const db = await createConnection(ORMConfig) const repository = db.getRepository(model) return repository.save(data) }