Skip to content

Commit

Permalink
add prisma template
Browse files Browse the repository at this point in the history
  • Loading branch information
schettn committed Jul 31, 2024
1 parent 0287689 commit f98fee8
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package-lock.json
# Environment
.env
.env.*
!.env.template

# pylon (build directory)
.pylon
Expand Down
30 changes: 29 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,28 @@ WORKDIR /usr/src/pylon
# install dependencies into temp directory
# this will cache them and speed up future builds
FROM base AS install
ARG NODE_VERSION=20
RUN apt update \
&& apt install -y curl
RUN curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n \
&& bash n $NODE_VERSION \
&& rm n \
&& npm install -g n

RUN mkdir -p /temp/dev
COPY package.json bun.lockb /temp/dev/
COPY prisma /temp/dev/prisma

RUN cd /temp/dev && bun install --frozen-lockfile
RUN cd /temp/dev && bun prisma generate

# install with --production (exclude devDependencies)
RUN mkdir -p /temp/prod
COPY package.json bun.lockb /temp/prod/
COPY prisma /temp/prod/prisma

RUN cd /temp/prod && bun install --frozen-lockfile --production
RUN cd /temp/prod && bun prisma generate

# copy node_modules from temp directory
# then copy all (non-ignored) project files into the image
Expand All @@ -35,13 +49,27 @@ RUN mkdir -p .pylon
# RUN bun test
RUN bun run pylon build

# Deploy prisma schema (create dev.db)
RUN bun prisma migrate deploy

# copy production dependencies and source code into final image
FROM base AS release
RUN apt-get update -y && apt-get install -y openssl
COPY --from=install /temp/prod/node_modules node_modules
COPY --from=prerelease /usr/src/pylon/.pylon .pylon
COPY --from=prerelease /usr/src/pylon/package.json .
COPY --from=prerelease /usr/src/pylon/prisma prisma

# Change ownership of the parent directory to the bun user
RUN chown -R bun:bun /usr/src/pylon/prisma/db

# Ensure proper permissions for the parent directory
RUN chmod -R 755 /usr/src/pylon/prisma/db

# run the app
USER bun
EXPOSE 3000/tcp
ENTRYPOINT [ "bun", "run", "./node_modules/.bin/pylon-server" ]
ENTRYPOINT [ "/usr/local/bin/bun", "run", "./node_modules/.bin/pylon-server" ]


VOLUME [ "/usr/src/pylon/prisma/db" ]
Binary file modified bun.lockb
Binary file not shown.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"@getcronit/pylon": "*",
"@getcronit/pylon-builder": "*",
"@getcronit/pylon-server": "*",
"bun-types": "^1.1.18"
"@prisma/client": "^5.17.0",
"bun-types": "^1.1.21"
},
"devDependencies": {
"commitizen": "^4.2.5",
"commitizen": "^4.3.0",
"@getcronit/pylon-cli": "*"
},
"scripts": {
Expand Down
31 changes: 31 additions & 0 deletions prisma/migrations/20240731120552_initial/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- CreateTable
CREATE TABLE "Post" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"content" TEXT,
"published" BOOLEAN NOT NULL DEFAULT false,
"authorId" INTEGER NOT NULL,
CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION
);

-- CreateTable
CREATE TABLE "Profile" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"bio" TEXT,
"userId" INTEGER NOT NULL,
CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION
);

-- CreateTable
CREATE TABLE "User" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT,
"email" TEXT NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "Profile_userId_key" ON "Profile"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
39 changes: 39 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "sqlite"
url = "file:./db/dev.db"
}

model Post {
id Int @id @default(autoincrement())
title String
createdAt DateTime @default(now())
content String?
published Boolean @default(false)
authorId Int
User User @relation(fields: [authorId], references: [id], onDelete: NoAction, onUpdate: NoAction)
}

model Profile {
id Int @id @default(autoincrement())
bio String?
userId Int @unique
User User @relation(fields: [userId], references: [id], onDelete: NoAction, onUpdate: NoAction)
}

model User {
id Int @id @default(autoincrement())
name String?
email String @unique
Post Post[]
Profile Profile?
}
3 changes: 3 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PrismaClient } from "@prisma/client";

export const prisma = new PrismaClient();
19 changes: 10 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {defineService, logger, PylonAPI} from '@getcronit/pylon'
import { defineService, logger, PylonAPI } from "@getcronit/pylon";
import { prisma } from "./client";

export default defineService({
Query: {
hello() {
return 'Hello, World!'
}
}
})
posts: async () => {
return await prisma.post.findMany();
},
},
});

export const configureApp: PylonAPI['configureApp'] = app => {
logger.info('Configuring app')
}
export const configureApp: PylonAPI["configureApp"] = (app) => {
logger.info("Configuring app");
};

0 comments on commit f98fee8

Please sign in to comment.