Try prisma in Netlify.

This commit is contained in:
Dillon Kearns 2023-01-26 16:07:40 -08:00
parent 6eb4bff4bd
commit 93408eb17b
8 changed files with 319 additions and 5 deletions

View File

@ -0,0 +1,136 @@
module Route.Users exposing (ActionData, Data, route, RouteParams, Msg, Model)
{-|
@docs ActionData, Data, route, RouteParams, Msg, Model
-}
import BackendTask
import BackendTask.Custom
import Effect
import ErrorPage
import FatalError
import Head
import Html
import Json.Decode as Decode
import Json.Encode as Encode
import Pages.Msg
import Pages.PageUrl
import Path
import Platform.Sub
import RouteBuilder
import Server.Request
import Server.Response
import Shared
import View
type alias Model =
{}
type Msg
= NoOp
type alias RouteParams =
{}
route : RouteBuilder.StatefulRoute RouteParams Data ActionData Model Msg
route =
RouteBuilder.buildWithLocalState
{ view = view
, init = init
, update = update
, subscriptions = subscriptions
}
(RouteBuilder.serverRender { data = data, action = action, head = head })
init :
Maybe Pages.PageUrl.PageUrl
-> Shared.Model
-> RouteBuilder.StaticPayload Data ActionData RouteParams
-> ( Model, Effect.Effect Msg )
init pageUrl sharedModel app =
( {}, Effect.none )
update :
Pages.PageUrl.PageUrl
-> Shared.Model
-> RouteBuilder.StaticPayload Data ActionData RouteParams
-> Msg
-> Model
-> ( Model, Effect.Effect msg )
update pageUrl sharedModel app msg model =
case msg of
NoOp ->
( model, Effect.none )
subscriptions :
Maybe Pages.PageUrl.PageUrl
-> RouteParams
-> Path.Path
-> Shared.Model
-> Model
-> Sub Msg
subscriptions maybePageUrl routeParams path sharedModel model =
Platform.Sub.none
type alias Data =
{ users : List String
}
type alias ActionData =
{}
data :
RouteParams
-> Server.Request.Parser (BackendTask.BackendTask FatalError.FatalError (Server.Response.Response Data ErrorPage.ErrorPage))
data routeParams =
Server.Request.succeed
(BackendTask.Custom.run "users"
Encode.null
(Decode.list (Decode.field "" Decode.string))
|> BackendTask.allowFatal
|> BackendTask.map
(\users ->
Server.Response.render
{ users = users
}
)
)
head : RouteBuilder.StaticPayload Data ActionData RouteParams -> List Head.Tag
head app =
[]
view :
Maybe Pages.PageUrl.PageUrl
-> Shared.Model
-> Model
-> RouteBuilder.StaticPayload Data ActionData RouteParams
-> View.View (Pages.Msg.Msg Msg)
view maybeUrl sharedModel model app =
{ title = "Users"
, body =
[ Html.h2 [] [ Html.text "Users" ]
, Html.text (app.data.users |> String.join ", ")
]
}
action :
RouteParams
-> Server.Request.Parser (BackendTask.BackendTask FatalError.FatalError (Server.Response.Response ActionData ErrorPage.ErrorPage))
action routeParams =
Server.Request.succeed (BackendTask.succeed (Server.Response.render {}))

View File

@ -1,6 +1,19 @@
import kleur from "kleur";
import something from './something.ts'
kleur.enabled = true;
import something from "./something.ts";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export async function users() {
try {
const users = await prisma.user.findMany({
include: { profile: true },
});
return users;
} catch (error) {
console.trace(error);
return ["PRISMA ERROR"];
}
}
export async function environmentVariable(name) {
const result = process.env[name];

View File

@ -8,6 +8,9 @@
"name": "elm-pages-example",
"version": "1.0.0",
"license": "BSD-3",
"dependencies": {
"@prisma/client": "^4.9.0"
},
"devDependencies": {
"@dillonkearns/elm-graphql": "^4.2.3",
"@netlify/functions": "^0.7.2",
@ -18,6 +21,7 @@
"elm-tailwind-modules": "^0.3.2",
"elm-tooling": "^1.3.0",
"postcss": "^8.4.5",
"prisma": "^4.9.0",
"tailwindcss": "^2.2.19"
}
},
@ -589,6 +593,38 @@
"node": ">= 8"
}
},
"node_modules/@prisma/client": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.9.0.tgz",
"integrity": "sha512-bz6QARw54sWcbyR1lLnF2QHvRW5R/Jxnbbmwh3u+969vUKXtBkXgSgjDA85nji31ZBlf7+FrHDy5x+5ydGyQDg==",
"hasInstallScript": true,
"dependencies": {
"@prisma/engines-version": "4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5"
},
"engines": {
"node": ">=14.17"
},
"peerDependencies": {
"prisma": "*"
},
"peerDependenciesMeta": {
"prisma": {
"optional": true
}
}
},
"node_modules/@prisma/engines": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.9.0.tgz",
"integrity": "sha512-t1pt0Gsp+HcgPJrHFc+d/ZSAaKKWar2G/iakrE07yeKPNavDP3iVKPpfXP22OTCHZUWf7OelwKJxQgKAm5hkgw==",
"devOptional": true,
"hasInstallScript": true
},
"node_modules/@prisma/engines-version": {
"version": "4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5",
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5.tgz",
"integrity": "sha512-M16aibbxi/FhW7z1sJCX8u+0DriyQYY5AyeTH7plQm9MLnURoiyn3CZBqAyIoQ+Z1pS77usCIibYJWSgleBMBA=="
},
"node_modules/@sindresorhus/is": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-2.1.1.tgz",
@ -4898,6 +4934,23 @@
"node": ">= 0.8"
}
},
"node_modules/prisma": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/prisma/-/prisma-4.9.0.tgz",
"integrity": "sha512-bS96oZ5oDFXYgoF2l7PJ3Mp1wWWfLOo8B/jAfbA2Pn0Wm5Z/owBHzaMQKS3i1CzVBDWWPVnOohmbJmjvkcHS5w==",
"devOptional": true,
"hasInstallScript": true,
"dependencies": {
"@prisma/engines": "4.9.0"
},
"bin": {
"prisma": "build/index.js",
"prisma2": "build/index.js"
},
"engines": {
"node": ">=14.17"
}
},
"node_modules/prompts": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz",
@ -6603,6 +6656,25 @@
"fastq": "^1.6.0"
}
},
"@prisma/client": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.9.0.tgz",
"integrity": "sha512-bz6QARw54sWcbyR1lLnF2QHvRW5R/Jxnbbmwh3u+969vUKXtBkXgSgjDA85nji31ZBlf7+FrHDy5x+5ydGyQDg==",
"requires": {
"@prisma/engines-version": "4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5"
}
},
"@prisma/engines": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.9.0.tgz",
"integrity": "sha512-t1pt0Gsp+HcgPJrHFc+d/ZSAaKKWar2G/iakrE07yeKPNavDP3iVKPpfXP22OTCHZUWf7OelwKJxQgKAm5hkgw==",
"devOptional": true
},
"@prisma/engines-version": {
"version": "4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5",
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5.tgz",
"integrity": "sha512-M16aibbxi/FhW7z1sJCX8u+0DriyQYY5AyeTH7plQm9MLnURoiyn3CZBqAyIoQ+Z1pS77usCIibYJWSgleBMBA=="
},
"@sindresorhus/is": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-2.1.1.tgz",
@ -9733,6 +9805,15 @@
"integrity": "sha1-t+PqQkNaTJsnWdmeDyAesZWALuE=",
"dev": true
},
"prisma": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/prisma/-/prisma-4.9.0.tgz",
"integrity": "sha512-bS96oZ5oDFXYgoF2l7PJ3Mp1wWWfLOo8B/jAfbA2Pn0Wm5Z/owBHzaMQKS3i1CzVBDWWPVnOohmbJmjvkcHS5w==",
"devOptional": true,
"requires": {
"@prisma/engines": "4.9.0"
}
},
"prompts": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz",

View File

@ -21,6 +21,10 @@
"elm-tailwind-modules": "^0.3.2",
"elm-tooling": "^1.3.0",
"postcss": "^8.4.5",
"prisma": "^4.9.0",
"tailwindcss": "^2.2.19"
},
"dependencies": {
"@prisma/client": "^4.9.0"
}
}
}

View File

@ -0,0 +1,41 @@
-- CreateTable
CREATE TABLE "Post" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"title" TEXT NOT NULL,
"content" TEXT,
"published" BOOLEAN NOT NULL DEFAULT false,
"authorId" INTEGER NOT NULL,
PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Profile" (
"id" SERIAL NOT NULL,
"bio" TEXT,
"userId" INTEGER NOT NULL,
PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"email" TEXT NOT NULL,
"name" TEXT,
PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "Profile.userId_unique" ON "Profile"("userId");
-- CreateIndex
CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");
-- AddForeignKey
ALTER TABLE "Post" ADD FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Profile" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"

View File

@ -0,0 +1,34 @@
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "rhel-openssl-1.0.x"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id]) // renamed from `User` -> `author`
authorId Int // relation scalar field
}
model Profile {
id Int @id @default(autoincrement())
bio String?
userId Int @unique // relation scalar field
user User @relation(fields: [userId], references: [id]) // renamed from `User` -> `user`
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[] // renamed from `Post` -> `posts`
profile Profile? // renamed from `User` -> `profile`
}

View File

@ -150,13 +150,15 @@ export async function run(options) {
.build({
entryPoints: ["./custom-backend-task"],
platform: "node",
outfile: ".elm-pages/compiled-ports/custom-backend-task.js",
outfile: ".elm-pages/compiled-ports/custom-backend-task.mjs",
assetNames: "[name]-[hash]",
chunkNames: "chunks/[name]-[hash]",
outExtension: { ".js": ".js" },
metafile: true,
bundle: true,
watch: false,
format: "esm",
external: ["@prisma/client"],
logLevel: "silent",
})
.then((result) => {
@ -625,7 +627,7 @@ async function runAdapter(adaptFn, processedIndexTemplate) {
apiRoutePatterns: JSON.parse(
await fsPromises.readFile("./dist/api-patterns.json", "utf-8")
),
portsFilePath: "./.elm-pages/compiled-ports/custom-backend-task.js",
portsFilePath: "./.elm-pages/compiled-ports/custom-backend-task.mjs",
htmlTemplate: processedIndexTemplate,
});
console.log("Success - Adapter script complete");