Skip to content

Commit

Permalink
fix(db): fix dataconfig model import issue
Browse files Browse the repository at this point in the history
  • Loading branch information
oceanlvr authored and ysfscream committed Jul 27, 2021
1 parent aa0b36e commit f90628d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 71 deletions.
51 changes: 2 additions & 49 deletions ormconfig.ts
Original file line number Diff line number Diff line change
@@ -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
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@
"description": "MQTT desktop client",
"author": "EMQ X Team <[email protected]>",
"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",
Expand All @@ -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": {
Expand Down
49 changes: 49 additions & 0 deletions src/database/database.config.ts
Original file line number Diff line number Diff line change
@@ -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
Binary file removed src/database/database.png
Binary file not shown.
8 changes: 1 addition & 7 deletions src/database/models/ConnectionEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions src/database/models/SettingEntity.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -15,7 +15,4 @@ export default class SettingEntity {
// 0: text 1: number 2: boolean
@Column({ type: 'integer' })
settingType!: number

@Column({ type: 'timestamp' })
updateTime!: string
}
6 changes: 3 additions & 3 deletions src/database/useSqlite.ts
Original file line number Diff line number Diff line change
@@ -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 <T>(data: T, model: EntityTarget<T>): Promise<T> => {
const config = await getConnectionOptions('MQTTX')
const db = await createConnection(config)
const db = await createConnection(ORMConfig)
const repository = db.getRepository(model)
return repository.save(data)
}
Expand Down

0 comments on commit f90628d

Please sign in to comment.