Skip to content

Commit

Permalink
feat: add Prisma support and generate schema
Browse files Browse the repository at this point in the history
- Added Prisma dependency to package.json
- Copied prisma/schema.prisma file in Dockerfile
- Added "migrate" script to package.json for running Prisma migrations
- Created prisma/schema.prisma file
- Created a prisma.ts file
  • Loading branch information
schettn committed Nov 2, 2023
1 parent ee7de2f commit 5079d58
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
FROM --platform=linux/amd64 node:18.8.0-alpine

LABEL description="This container serves as an entry point for our future Snek Function projects."
LABEL org.opencontainers.image.source="https://github.com/snek-functions/mailpress"
LABEL org.opencontainers.image.source="https://github.com/snek-functions/mailpresss"
LABEL maintainer="[email protected]"

WORKDIR /app

COPY .sf/ ./.sf
COPY templates/ ./templates
COPY package.json .
COPY templates/ ./templates
# Copy prisma files
COPY prisma/schema.prisma ./prisma/schema.prisma

RUN yarn install --production

RUN npx prisma generate

CMD ["sh", "-c", "yarn sf-server"]

EXPOSE 3000
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
}
},
"dependencies": {
"@devoxa/prisma-relay-cursor-connection": "^3.1.0",
"@prisma/client": "^5.2.0",
"@snek-at/function": "*",
"@snek-at/function-cli": "*",
"@snek-at/function-server": "*",
"@snek-functions/jwt": "*",
"dotenv": "^16.0.3",
"html-minifier": "^4.0.0",
"twig": "^1.16.0"
"twig": "^1.16.0",
"prisma": "^5.2.0"
},
"devDependencies": {
"@types/html-minifier": "^4.0.2",
Expand All @@ -44,7 +47,11 @@
"scripts": {
"develop": "yarn sf develop",
"build": "yarn sf build",
"migrate": "prisma migrate dev",
"generate-clients": "yarn snek-query generate https://services.snek.at/iam/graphql --output ./src/clients/iam && yarn snek-query generate https://services.snek.at/mailer/graphql --output ./src/clients/mailer",
"generate-clients-dev": "yarn snek-query generate http://localhost:4050/graphql --output ./src/clients/iam && yarn snek-query generate http://localhost:4020/graphql --output ./src/clients/mailer"
},
"prisma": {
"schema": "prisma/schema.prisma"
}
}
Empty file added prisma/schema.prisma
Empty file.
48 changes: 48 additions & 0 deletions src/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { logger } from "@snek-at/function";

import type { PrismaClient as PrismaClientType } from "@prisma/client";
import { createRequire } from "module";

const require = createRequire(import.meta.url);
const { PrismaClient } = require("@prisma/client") as {
PrismaClient: typeof PrismaClientType;
};

export const prisma = new PrismaClient({
log: [
{
level: "info",
emit: "event",
},
{
level: "query",
emit: "event",
},
{
level: "warn",
emit: "event",
},
{
level: "error",
emit: "event",
},
],
});

prisma.$on("info", (e) => {
logger.debug(`[${e.timestamp}] [DEBUG] [Prisma] ${e.target}: ${e.message}`);
});

prisma.$on("query", (e) => {
logger.debug(
`[${e.timestamp}] [DEBUG] [Prisma] ${e.target}: ${e.query} (params: ${e.params}, duration: ${e.duration}ms)`
);
});

prisma.$on("warn", (e) => {
logger.warn(`[${e.timestamp}] [WARNING] [Prisma] ${e.target}: ${e.message}`);
});

prisma.$on("error", (e) => {
logger.error(`[${e.timestamp}] [ERROR] [Prisma] ${e.target}: ${e.message}`);
});

0 comments on commit 5079d58

Please sign in to comment.