Skip to content

Commit

Permalink
fix: hook ObjectId for mongo
Browse files Browse the repository at this point in the history
Hieuzest committed Feb 21, 2024
1 parent 3a2e5bf commit bc79193
Showing 2 changed files with 17 additions and 15 deletions.
14 changes: 7 additions & 7 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.3",
"version": "2.6.6",
"main": "lib/index.cjs",
"types": "lib/index.d.ts",
"exports": {
@@ -52,19 +52,19 @@
}
},
"peerDependencies": {
"@koishijs/plugin-console": "^5.22.0",
"@koishijs/plugin-console": "^5.22.3",
"koishi": "^4.16.0"
},
"devDependencies": {
"@koishijs/client": "^5.22.0",
"@koishijs/plugin-console": "^5.22.0",
"@types/node": "^20.10.2",
"@koishijs/client": "^5.22.3",
"@koishijs/plugin-console": "^5.22.3",
"@types/node": "^20.10.5",
"esbuild": "^0.18.20",
"esbuild-register": "^3.5.0",
"koishi": "^4.16.0",
"typescript": "^5.3.2"
"typescript": "^5.3.3"
},
"dependencies": {
"@koishijs/console": "^5.22.0"
"@koishijs/console": "^5.22.3"
}
}
18 changes: 10 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ declare module '@koishijs/console' {
export interface TableInfo extends Driver.TableStats, Model.Config<any> {
fields: Field.Config
primary: string[]
PrimaryConstructor: (value: any) => void
hookObjectId: boolean
}

export interface DatabaseInfo extends Driver.Stats {
@@ -41,9 +41,13 @@ class DatabaseProvider extends DataService<DatabaseInfo> {
this.ctx.console.addListener(`database/${name}`, async (...args) => {
const callargs: any[] = args.map(deserialize)
if (['set', 'remove'].includes(name)) {
const table = (await this.get()).tables[callargs[0]]
if (table.PrimaryConstructor) {
callargs[1][table.primary[0]] = new table.PrimaryConstructor(callargs[1][table.primary[0]])
const table = (await this.get()).tables[callargs[0]]!
if (table.hookObjectId) {
callargs[1] = {
$expr: {
$eq: ['$_id', { $toObjectId: callargs[1][table.primary[0]] }],
},
}
}
}
const result = await (this.ctx.database[name] as any)(...callargs)
@@ -91,10 +95,8 @@ 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]
}
if (result.tables[name].fields[result.tables[name].primary[0]]?.type === 'primary') {
const record = await this.ctx.database.select(name as any).limit(1).execute()
result.tables[name].PrimaryConstructor = record[0]?.[result.tables[name].primary[0]]?.constructor
}
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)
}))
result.tables = Object.fromEntries(Object.entries(result.tables).sort(([a], [b]) => a.localeCompare(b)))
return result

0 comments on commit bc79193

Please sign in to comment.