Skip to content

Commit

Permalink
feat(mysql): add initialization for chess/monitor/rss/schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 9, 2020
1 parent 0632f0e commit f4cbdae
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/plugin-chess/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Context, Group } from 'koishi-core'
import { Context, extendDatabase, Group } from 'koishi-core'
import { isInteger } from 'koishi-utils'
import { State, MoveResult, StateData } from './state'
import MysqlDatabase from 'koishi-plugin-mysql/dist/database'
import * as go from './go'
import * as gomoku from './gomoku'
import * as othello from './othello'

extendDatabase<typeof MysqlDatabase>('koishi-plugin-mysql', ({ tables }) => {
tables.group.chess = `JSON NULL DEFAULT NULL`
})

interface Rule {
placement?: 'grid' | 'cross'
create?: (this: State) => string | void
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-monitor/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ extendDatabase<typeof MysqlDatabase>('koishi-plugin-mysql', {
},
})

extendDatabase<typeof MysqlDatabase>('koishi-plugin-mysql', ({ listFields }) => {
extendDatabase<typeof MysqlDatabase>('koishi-plugin-mysql', ({ listFields, tables }) => {
listFields.push('subscribe.names')
tables.group.subscribe = `JSON NULL DEFAULT NULL`
})

extendDatabase<typeof MongoDatabase>('koishi-plugin-mongo', {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-mysql/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class MysqlDatabase {
async start() {
this.pool = createPool(this.config)
const tables = await this.select('information_schema.tables', ['TABLE_NAME'], 'TABLE_SCHEMA = ?', [this.config.database])
const names = new Set<TableType>(tables.map(data => data.TABLE_NAME))
const names = new Set(tables.map(data => data.TABLE_NAME))
for (const name of Object.keys(MysqlDatabase.tables) as TableType[]) {
if (names.has(name)) return
const table = MysqlDatabase.tables[name]
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-rss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ Group.extend(() => ({
rss: [],
}))

extendDatabase<typeof MysqlDatabase>('koishi-plugin-mysql', ({ listFields }) => {
extendDatabase<typeof MysqlDatabase>('koishi-plugin-mysql', ({ listFields, tables }) => {
listFields.push('group.rss')
tables.group.rss = `TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci'`
})

export interface Config {
Expand Down
13 changes: 13 additions & 0 deletions packages/plugin-schedule/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ export interface Schedule {
session: Session
}

extendDatabase(MysqlDatabase, (Database) => {
Object.assign(Database.tables.schedule = [
'PRIMARY KEY (`id`) USING BTREE',
], {
id: `INT(10) UNSIGNED NOT NULL AUTO_INCREMENT`,
assignee: `BIGINT(20) NOT NULL DEFAULT '0'`,
time: `TIMESTAMP NULL DEFAULT NULL`,
interval: `BIGINT(20) UNSIGNED NOT NULL DEFAULT '0'`,
command: `MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_general_ci'`,
session: `JSON NOT NULL`,
})
})

extendDatabase<typeof MysqlDatabase>('koishi-plugin-mysql', {
createSchedule(time, interval, command, session) {
return this.create('schedule', { time, assignee: session.selfId, interval, command, session })
Expand Down

0 comments on commit f4cbdae

Please sign in to comment.