在前端使用 ctx.database 一致地操作数据库。
虽然这可能并不是好的实践,但总之趁着 v5 还在梦梦的肚子里先摸着玩。
Example
Backend
import { Context, Schema } from "koishi";
import { resolve } from "path";
import {} from "@koishijs/plugin-console";
export const name = "baz";
export const inject = ["console", "database-console"];
export interface Config {}
export const Config: Schema<Config> = Schema.object({});
export function apply(ctx: Context) {
ctx.console.addEntry({
dev: resolve(__dirname, "../client/index.ts"),
prod: resolve(__dirname, "../dist"),
});
}
Frontend
import { Context } from "@koishijs/client";
import type {} from "koishi-plugin-database-console";
export default (ctx: Context) => {
ctx.inject(["database"], async (ctx) => {
console.log(await ctx.database.get("user", { id: 0 }));
});
};
In Component
<script lang="ts" setup>
import { Context, useContext } from "@koishijs/client";
const ctx: Context = useContext();
ctx.get("database")?.get("user", { id: 0 }).then(console.log);
</script>