Skip to content

Commit

Permalink
feat(core): drop remove api in favor of orm
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 23, 2021
1 parent b211cd8 commit 10d1570
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 50 deletions.
2 changes: 0 additions & 2 deletions packages/koishi-core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,13 @@ export interface Database {
getUser<K extends User.Field, T extends User.Index>(type: T, ids: readonly string[], fields?: readonly K[]): Promise<Pick<User, K | T>[]>
setUser<T extends User.Index>(type: T, id: string, data: Partial<User>): Promise<void>
createUser<T extends User.Index>(type: T, id: string, data: Partial<User>): Promise<void>
removeUser<T extends User.Index>(type: T, id: string): Promise<void>

getChannel<K extends Channel.Field>(type: Platform, id: string, fields?: readonly K[]): Promise<Pick<Channel, K | 'id'>>
getChannel<K extends Channel.Field>(type: Platform, ids: readonly string[], fields?: readonly K[]): Promise<Pick<Channel, K | 'id'>[]>
getChannel<K extends Channel.Field>(type: Platform, id: MaybeArray<string>, fields?: readonly K[]): Promise<any>
getAssignedChannels<K extends Channel.Field>(fields?: readonly K[], assignMap?: Record<string, readonly string[]>): Promise<Pick<Channel, K>[]>
setChannel(type: Platform, id: string, data: Partial<Channel>): Promise<void>
createChannel(type: Platform, id: string, data: Partial<Channel>): Promise<void>
removeChannel(type: Platform, id: string): Promise<void>
}

type Methods<S, T> = {
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-test-utils/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function testDatabase(app: App) {
authority: 2,
})

await db.removeUser('mock', 'A')
await db.remove('user', { mock: ['A'] })
await expect(db.getUser('mock', ['A'])).eventually.to.deep.equal([])
})

Expand All @@ -51,7 +51,7 @@ export function testDatabase(app: App) {
await expect(db.getAssignedChannels(null)).eventually.to.have.length(2)
await expect(db.getAssignedChannels(null, { mock: ['321'] })).eventually.to.have.length(1)

await db.removeChannel('mock', 'A')
await db.remove('channel', { id: ['mock:A'] })
await expect(db.getChannel('mock', ['A'])).eventually.to.deep.equal([])
})

Expand Down
12 changes: 0 additions & 12 deletions packages/koishi-test-utils/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ Database.extend(MemoryDatabase, {
Object.assign(table[index], clone(data))
},

async removeUser(type, id) {
const table = this.$table('user')
const index = table.findIndex(row => row[type] === id)
if (index >= 0) table.splice(index, 1)
},

async createUser(type, id, data) {
const table = this.$table('user')
const index = table.findIndex(row => row[type] === id)
Expand Down Expand Up @@ -136,12 +130,6 @@ Database.extend(MemoryDatabase, {
Object.assign(table[index], clone(data))
},

async removeChannel(type, id) {
const table = this.$table('channel')
const index = table.findIndex(row => row.id === `${type}:${id}`)
if (index >= 0) table.splice(index, 1)
},

async createChannel(type, id, data) {
const table = this.$table('channel')
const index = table.findIndex(row => row.id === `${type}:${id}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-common/src/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export default function apply(ctx: Context, config: AdminConfig = {}) {
}

async function bind(user: User.Observed<never>, platform: Platform, userId: string) {
await ctx.database.removeUser(platform, userId)
await ctx.database.remove('user', { [platform]: [userId] })
ctx.app._userCache[platform].set(userId, user)
user[platform] = userId as never
await user._update()
Expand Down
8 changes: 0 additions & 8 deletions packages/plugin-mongo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ Database.extend(MongoDatabase, {
await this.setUser(type, id, data)
},

async removeUser(type, id) {
await this.user.deleteOne({ [type]: id })
},

async getChannel(type, pid, fields = Channel.fields) {
if (Array.isArray(pid)) {
if (fields && !fields.length) return pid.map(id => ({ id: `${type}:${id}` }))
Expand All @@ -184,10 +180,6 @@ Database.extend(MongoDatabase, {
return channels.map(channel => ({ ...pick(Channel.create(channel.type, channel.pid), fields), ...channel, id: `${channel.type}:${channel.pid}` }))
},

async removeChannel(type, pid) {
await this.channel.deleteOne({ type, pid })
},

async setChannel(type, pid, data) {
await this.channel.updateOne({ type, pid }, { $set: data }, { upsert: true })
},
Expand Down
34 changes: 17 additions & 17 deletions packages/plugin-mysql/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface MysqlDatabase extends Database {}

export function escape(value: any, table?: TableType, field?: string) {
const type = MysqlDatabase.tables[table]?.[field]
return mysqlEscape(typeof type === 'object' ? type.toString(value) : value)
return mysqlEscape(typeof type === 'object' ? type.stringify(value) : value)
}

class MysqlDatabase {
Expand All @@ -48,7 +48,7 @@ class MysqlDatabase {
typeCast: (field, next) => {
const type = MysqlDatabase.tables[field.packet.orgTable]?.[field.packet.orgName]
if (typeof type === 'object') {
return type.valueOf(field)
return type.parse(field)
}
if (field.type === 'BIT') {
return Boolean(field.buffer()?.readUInt8(0))
Expand Down Expand Up @@ -204,8 +204,8 @@ namespace MysqlDatabase {

export interface Domain<T = any> {
definition: string
toString(value: T): string
valueOf(source: FieldInfo): T
parse(source: FieldInfo): T
stringify(value: T): string
}

export namespace Domain {
Expand All @@ -216,38 +216,38 @@ namespace MysqlDatabase {
export class String implements Domain<string> {
constructor(public definition = 'TEXT') {}

toString(value: any) {
return value
parse(field: FieldInfo) {
return field.string()
}

valueOf(field: FieldInfo) {
return field.string()
stringify(value: any) {
return value
}
}

export class Array implements Domain<string[]> {
constructor(public definition = 'TEXT') {}

toString(value: string[]) {
return value.join(',')
}

valueOf(field: FieldInfo) {
parse(field: FieldInfo) {
const source = field.string()
return source ? source.split(',') : []
}

stringify(value: string[]) {
return value.join(',')
}
}

export class Json implements Domain {
// mysql does not support text column with default value
constructor(public definition = 'text', private defaultValue?: any) {}

toString(value: any) {
return JSON.stringify(value)
parse(field: FieldInfo) {
return JSON.parse(field.string()) || this.defaultValue
}

valueOf(field: FieldInfo) {
return JSON.parse(field.string()) || this.defaultValue
stringify(value: any) {
return JSON.stringify(value)
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions packages/plugin-mysql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ Database.extend(MysqlDatabase, {
return data && { ...data, [type]: id }
},

async removeUser(type, id) {
await this.query('DELETE FROM `user` WHERE ?? = ?', [type, id])
},

async createUser(type, id, data) {
data[type] = id
const newKeys = Object.keys(data)
Expand Down Expand Up @@ -124,10 +120,6 @@ Database.extend(MysqlDatabase, {
}).join(' OR '))
},

async removeChannel(type, pid) {
await this.query('DELETE FROM `channel` WHERE `id` = ?', [`${type}:${pid}`])
},

async createChannel(type, pid, data) {
data.id = `${type}:${pid}`
const newKeys = Object.keys(data)
Expand Down

0 comments on commit 10d1570

Please sign in to comment.