Skip to content

Commit

Permalink
feat(orm): support decimal field type
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 14, 2021
1 parent a0a920f commit 2209a20
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/koishi-core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ export interface Field<T = any> {
length?: number
nullable?: boolean
initial?: T
precision?: number
scale?: number
}

export namespace Field {
export const numberTypes: Type[] = ['integer', 'unsigned', 'float', 'double']
export const numberTypes: Type[] = ['integer', 'unsigned', 'float', 'double', 'decimal']
export const stringTypes: Type[] = ['char', 'string', 'text']
export const dateTypes: Type[] = ['timestamp', 'date', 'time']
export const objectTypes: Type[] = ['list', 'json']

export type Type<T = any> =
| T extends number ? 'integer' | 'unsigned' | 'float' | 'double'
| T extends number ? 'integer' | 'unsigned' | 'float' | 'double' | 'decimal'
: T extends string ? 'char' | 'string' | 'text'
: T extends Date ? 'timestamp' | 'date' | 'time'
: T extends any[] ? 'list' | 'json'
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-mysql/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function escape(value: any, table?: TableType, field?: string) {
return mysqlEscape(stringify(value, table, field))
}

function getTypeDefinition({ type, length }: Field) {
function getTypeDefinition({ type, length, precision, scale }: Field) {
switch (type) {
case 'float':
case 'double':
Expand All @@ -47,6 +47,7 @@ function getTypeDefinition({ type, length }: Field) {
case 'timestamp': return type
case 'integer': return `int(${length || 10})`
case 'unsigned': return `int(${length || 10}) unsigned`
case 'decimal': return `int(${precision}, ${scale}) unsigned`
case 'string': return `varchar(${length || 65536})`
case 'list': return `varchar(${length || 65536})`
case 'json': return `varchar(${length || 65536})`
Expand Down

0 comments on commit 2209a20

Please sign in to comment.