Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 2234839/b9b3fa6bd20a49b4da884357ce6f9523 to your computer and use it in GitHub Desktop.
Save 2234839/b9b3fa6bd20a49b4da884357ce6f9523 to your computer and use it in GitHub Desktop.
使用prisma studio链接任何server
app.post(api_path, async (c) => {
const req = await c.req.json();
const [payload, resHelper] = reqParser(req);
const r = await execClientRequest(payload.data)
return c.json(resHelper(r, null));
});
interface queries {
schemaHash: string;
modelName: "Proxy_api";
operation: "count";
args?: {
take: 100;
skip: 0;
select: { id: true; Wallet_id: true; Wallet: true; createdAt: true; updatedAt: true };
};
}
interface payload {
data:
| {
schemaHash: string;
operation: "$transaction";
queries: queries[];
}
| queries;
}
async function execClientRequest(op: payload["data"]) {
if (op.operation == "$transaction") {
const { queries } = op;
const actions = queries.map(execQueries);
const r = await prisma.$transaction(actions);
return r;
} else if (["findMany", "count", "update"].includes(op.operation)) {
return execQueries(op);
}
function execQueries(op: queries) {
return prisma[toLowerCase(op.modelName) as /** 类型欺骗 */ "wallet"][op.operation](op.args);
}
}
function toLowerCase(str: string): string {
return str.toLowerCase();
}
function reqParser(req: any) {
const { requestId, action, payload } = req;
return [
payload as payload,
(data: any, error: any) => {
return {
requestId,
channel: "-prisma",
action,
payload: { error, data },
};
},
] as const;
}
// 请求拦截.js
(() => {
const rqwFetch = globalThis.fetch;
globalThis.fetch = async (...arg) => {
const [url, init] = arg;
const payload = JSON.parse(init.body);
if (
url.toString().endsWith("/api") &&
payload.channel === "prisma" &&
payload.action === "clientRequest"
) {
return rqwFetch("https://shenzilong.cn/studio_server/api", init);
} else {
return rqwFetch(...arg);
}
};
})();
@2234839
Copy link
Author

2234839 commented Dec 24, 2023

Using prisma_studio to connect to any server

When I successfully use prisma in serverless, it is only natural that I want prisma studio to connect to my server in serverless.

When I searched the internet for "prisma studio remote sqlite", I did not find a suitable solution.

There are others who have the same idea and question: https://community.fly.io/t/expose-sqlite-db-to-external-orm-connections/8300

So I decided to implement it myself, thanks to the simplicity of the API interface used by prisma studio, it took less time than I imagined (maybe an hour?).

https://shenzilong.cn/%E5%B7%A5%E5%85%B7/prisma/%E5%9C%A8serverless%E4%B8%AD%E4%BD%BF%E7%94%A8prisma.html#20231223132619-pult0qj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment