-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (52 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict'
const fp = require("fastify-plugin");
const StudioServer = require("./server.js");
const {
getConfig,
getDirectUrl,
resolveUrl,
} = require("@prisma/internals");
const path = require("path");
const fs = require("fs");
const { enginesVersion } = require('@prisma/engines')
const fastifyFormbody = require('@fastify/formbody');
const fastifySession = require('@fastify/session');
const fastifyCookie = require('@fastify/cookie');
const prismaPackageJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../prisma/package.json'), 'utf-8'))
/**
*
* @param {import("fastify").FastifyInstance} app
* @param {{schemaPath: string, auth: {enabled: boolean, default: {email: string, password: string}, secret: string}}} options
*/
async function PrismaStudioFastify(app, options) {
let { schemaPath, auth } = options;
const schema = await fs.promises.readFile(schemaPath, 'utf-8')
const config = await getConfig({ datamodel: schema, ignoreEnvVarErrors: true })
if (auth.enabled) {
app.register(fastifyFormbody);
app.register(fastifyCookie);
app.register(fastifySession, { secret: auth.secret });
}
const studio = new StudioServer({
schemaPath,
prismaClient: {
resolve: {
'@prisma/client': path.resolve(__dirname, '../prisma/prisma-client/index.js'),
},
directUrl: resolveUrl(getDirectUrl(config.datasources[0])),
},
versions: {
prisma: prismaPackageJson.version,
queryEngine: enginesVersion,
},
auth: auth.enabled ? auth.default : undefined,
})
await studio.start(app)
}
const plugin = fp(PrismaStudioFastify, {
name: 'prisma-studio-fastify',
fastify: '^4.0.x'
})
module.exports = plugin
module.exports.default = plugin
module.exports.fastifyOverview = plugin