Skip to content

Commit

Permalink
feat: adapt minato v3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest committed Apr 12, 2024
1 parent bc79193 commit 513aeb1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 29 deletions.
16 changes: 10 additions & 6 deletions client/components/data-table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function getCellStyle({ column }) {
if (column.cellStyle) return column.cellStyle
for (const pref of config.value.dataview?.colors ?? []) {
if (!pref || !pref.types) continue
if (pref.types.includes(table.value?.fields?.[column.label]?.type)) {
if (pref.types.includes(table.value?.fields?.[column.label]?.deftype)) {
return column.cellStyle = { 'background-color': pref.color }
}
}
Expand Down Expand Up @@ -275,7 +275,7 @@ const columnInputAttr: ComputedRef<Dict<{
const dateAttrs = { clearable: false}
let type, step
switch (fieldConfig.type) {
switch (fieldConfig.deftype) {
case 'time':
return { ...o, [fName]: { is: 'el-time-picker', attrs: dateAttrs} }
case 'date':
Expand All @@ -301,7 +301,7 @@ const columnInputAttr: ComputedRef<Dict<{
const validate = (val) => {
if (fieldConfig.nullable === false && !val.length) return false
if (type.value === 'number') val = parseFloat(val)
switch (fieldConfig.type) {
switch (fieldConfig.deftype) {
case 'unsigned':
if (val < 0)
return false
Expand Down Expand Up @@ -332,7 +332,7 @@ function onSort(e) {
}
function renderCell(field: string, { row, column, $index }) {
const fType = table.value.fields[field].type
const fType = table.value.fields[field].deftype
const data = row[field]
switch (fType) {
case 'json':
Expand All @@ -349,14 +349,17 @@ function renderCell(field: string, { row, column, $index }) {
if (data instanceof Date)
return `${dateStr(data)} ${timeStr(data)}`
break
case 'binary':
return `<Binary len=${data}>`
}
return data
}
/** convert cell data to model value */
function toModelValue(field: string, data) {
const fType = table.value.fields[field].type
const fType = table.value.fields[field].deftype
switch (fType) {
case 'list':
case 'json':
return JSON.stringify(data)
case 'time':
Expand All @@ -371,7 +374,7 @@ function toModelValue(field: string, data) {
/** convert model value data to cell */
function fromModelValue(field: string, data) {
const fType = table.value.fields[field].type
const fType = table.value.fields[field].deftype
switch (fType) {
case 'unsigned':
case 'integer':
Expand Down Expand Up @@ -399,6 +402,7 @@ function onOuterCellClick(_row, _column, element) {
}
function onCellDblClick({ row, column, $index }) {
if (isCellChanged({ row, column, $index }, false)) return // Change record exists
if (table.value.fields[column.label].deftype === 'binary') return
if (state.changes[$index] === undefined)
state.changes[$index] = {}
state.changes[$index][column.label] = reactive({
Expand Down
1 change: 1 addition & 0 deletions client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const FieldType = [
'char', 'string', 'text',
'boolean',
'timestamp', 'date', 'time',
'binary',
'list', 'json',
'expr',
] as const
Expand Down
8 changes: 6 additions & 2 deletions client/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Database } from 'koishi'
import { message, send } from '@koishijs/client'
import { Binary, message, send } from '@koishijs/client'
import { Methods } from 'koishi-plugin-dataview'

export function serialize(obj: unknown): string {
if (Binary.is(obj)) return `"b${obj.byteLength}"`
if (obj instanceof Date) return `"d${obj.toJSON()}"`
return JSON.stringify(obj, (_, value) => {
if (Binary.is(value)) return `b${value.byteLength}`
if (typeof value === 'string') return 's' + value
if (typeof value === 'object') {
if (value instanceof Date) return 'd' + new Date(value).toJSON()
Expand All @@ -31,7 +33,9 @@ export function deserialize(str: string): any {
typeof v === 'string'
? v[0] === 's'
? v.slice(1)
: new Date(v.slice(1))
: v[0] === 'b'
? +v.slice(1)
: new Date(v.slice(1))
: v,
)
}
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-dataview",
"description": "View Database in Koishi Console",
"version": "2.6.6",
"version": "2.7.0",
"main": "lib/index.cjs",
"types": "lib/index.d.ts",
"exports": {
Expand Down Expand Up @@ -52,19 +52,19 @@
}
},
"peerDependencies": {
"@koishijs/plugin-console": "^5.22.3",
"koishi": "^4.16.0"
"@koishijs/plugin-console": "^5.28.2",
"koishi": "^4.17.4"
},
"devDependencies": {
"@koishijs/client": "^5.22.3",
"@koishijs/plugin-console": "^5.22.3",
"@types/node": "^20.10.5",
"esbuild": "^0.18.20",
"@koishijs/client": "^5.28.2",
"@koishijs/plugin-console": "^5.28.2",
"@types/node": "^20.12.7",
"esbuild": "^0.20.2",
"esbuild-register": "^3.5.0",
"koishi": "^4.16.0",
"typescript": "^5.3.3"
"koishi": "^4.17.4",
"typescript": "^5.4.5"
},
"dependencies": {
"@koishijs/console": "^5.22.3"
"@koishijs/console": "^5.28.2"
}
}
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clone, Context, Dict, Driver, Field, makeArray, Model, Schema } from 'koishi'
import { clone, Context, Dict, Driver, Field, makeArray, Model, Schema, Type } from 'koishi'
import { DataService } from '@koishijs/console'
import { resolve } from 'path'
import { deserialize, serialize } from './utils'
Expand All @@ -24,7 +24,7 @@ declare module '@koishijs/console' {
export interface TableInfo extends Driver.TableStats, Model.Config<any> {
fields: Field.Config
primary: string[]
hookObjectId: boolean
HookObjectId: (value: any) => void
}

export interface DatabaseInfo extends Driver.Stats {
Expand All @@ -42,12 +42,8 @@ class DatabaseProvider extends DataService<DatabaseInfo> {
const callargs: any[] = args.map(deserialize)
if (['set', 'remove'].includes(name)) {
const table = (await this.get()).tables[callargs[0]]!
if (table.hookObjectId) {
callargs[1] = {
$expr: {
$eq: ['$_id', { $toObjectId: callargs[1][table.primary[0]] }],
},
}
if (table.HookObjectId) {
callargs[1][table.primary[0]] = new table.HookObjectId(callargs[1][table.primary[0]])
}
}
const result = await (this.ctx.database[name] as any)(...callargs)
Expand Down Expand Up @@ -95,8 +91,11 @@ class DatabaseProvider extends DataService<DatabaseInfo> {
for (const [key, field] of Object.entries(result.tables[name].fields)) {
if (field.deprecated) delete result.tables[name].fields[key]
}
result.tables[name].hookObjectId = result.tables[name].fields[result.tables[name].primary[0]]?.type === 'primary'
&& ['mongo', 'MongoDriver'].includes(Object.values(this.ctx.database.drivers)[0].constructor.name)
if ((result.tables[name].fields[result.tables[name].primary[0]]?.type as Type)?.type === 'primary'
&& ['mongo', 'MongoDriver'].includes(Object.values(this.ctx.database.drivers)[0].constructor.name)) {
const record = await this.ctx.database.select(name as any).limit(1).execute()
result.tables[name].HookObjectId = record[0]?.[result.tables[name].primary[0]]?.constructor
}
}))
result.tables = Object.fromEntries(Object.entries(result.tables).sort(([a], [b]) => a.localeCompare(b)))
return result
Expand Down
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Binary } from 'koishi'

export function serialize(obj: unknown): string {
if (Binary.is(obj)) return `"b${obj.byteLength}"`
if (obj instanceof Date) return `"d${obj.toJSON()}"`
return JSON.stringify(obj, (_, value) => {
if (Binary.is(value)) return `b${value.byteLength}`
if (typeof value === 'string') return 's' + value
if (typeof value === 'object') {
if (value instanceof Date) return 'd' + new Date(value).toJSON()
Expand All @@ -27,7 +31,9 @@ export function deserialize(str: string): unknown {
typeof v === 'string'
? v[0] === 's'
? v.slice(1)
: new Date(v.slice(1))
: v[0] === 'b'
? undefined
: new Date(v.slice(1))
: v,
)
}

0 comments on commit 513aeb1

Please sign in to comment.