Skip to content

Commit

Permalink
fix(orm): use fallback data when inserting rows
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 14, 2021
1 parent 2209a20 commit 355853b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-mongo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Database.extend(MongoDatabase, {
async create(name, data: any) {
const meta = Tables.config[name]
const { primary, type = getFallbackType(meta) } = meta
const copy = { ...data }
const copy = { ...data, ...Tables.create(name) }
if (copy[primary]) {
copy['_id'] = copy[primary]
delete copy[primary]
Expand Down
13 changes: 1 addition & 12 deletions packages/plugin-mysql/src/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPool, Pool, PoolConfig, escape as mysqlEscape, escapeId, format, OkPacket, TypeCast } from 'mysql'
import { createPool, Pool, PoolConfig, escape as mysqlEscape, escapeId, format, TypeCast } from 'mysql'
import { App, Database, Field } from 'koishi-core'
import * as Koishi from 'koishi-core'
import { Logger } from 'koishi-utils'
Expand Down Expand Up @@ -225,17 +225,6 @@ class MysqlDatabase {
return this.query(sql, values)
}

async create<K extends TableType>(table: K, data: Partial<Tables[K]>): Promise<Tables[K]> {
const keys = Object.keys(data)
if (!keys.length) return
logger.debug(`[create] ${table}: ${data}`)
const header = await this.query<OkPacket>(
`INSERT INTO ?? (${this.joinKeys(keys)}) VALUES (${keys.map(() => '?').join(', ')})`,
[table, ...this.formatValues(table, data, keys)],
)
return { ...data, id: header.insertId } as any
}

async count<K extends TableType>(table: K, conditional?: string) {
const [{ 'COUNT(*)': count }] = await this.query(`SELECT COUNT(*) FROM ?? ${conditional ? 'WHERE ' + conditional : ''}`, [table])
return count as number
Expand Down
14 changes: 10 additions & 4 deletions packages/plugin-mysql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import MysqlDatabase, { Config, TableType } from './database'
import { User, Channel, Database, Context, Query } from 'koishi-core'
import { difference } from 'koishi-utils'
import { OkPacket, escapeId, escape } from 'mysql'
import * as Koishi from 'koishi-core'

export * from './database'
export default MysqlDatabase
Expand Down Expand Up @@ -138,8 +139,8 @@ Database.extend(MysqlDatabase, {
},

async create(table, data) {
data = { ...data, ...Koishi.Tables.create(table) }
const keys = Object.keys(data)
if (!keys.length) return
const header = await this.query<OkPacket>(
`INSERT INTO ?? (${this.joinKeys(keys)}) VALUES (${keys.map(() => '?').join(', ')})`,
[table, ...this.formatValues(table, data, keys)],
Expand All @@ -149,12 +150,17 @@ Database.extend(MysqlDatabase, {

async update(table, data) {
if (!data.length) return
data = data.map(item => ({ ...item, ...Koishi.Tables.create(table) }))
const keys = Object.keys(data[0])
const placeholder = `(${keys.map(() => '?').join(', ')})`
const update = keys.filter(key => key !== 'id').map((key) => {
key = escapeId(key)
return `${key} = VALUES(${key})`
}).join(', ')
await this.query(
`INSERT INTO ?? (${this.joinKeys(keys)}) VALUES ${data.map(() => placeholder).join(', ')}
ON DUPLICATE KEY UPDATE ${keys.filter(key => key !== 'id').map(key => `\`${key}\` = VALUES(\`${key}\`)`).join(', ')}`,
[table, ...[].concat(...data.map(data => this.formatValues(table, data, keys)))],
`INSERT INTO ${escapeId(table)} (${this.joinKeys(keys)}) VALUES ${data.map(() => placeholder).join(', ')}
ON DUPLICATE KEY UPDATE ${update}`,
[].concat(...data.map(data => this.formatValues(table, data, keys))),
)
},

Expand Down

0 comments on commit 355853b

Please sign in to comment.