Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bringing Prisma entities to the frontend #962

Merged
merged 63 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
4eb5fce
Support typing backend queries
sodic Jan 10, 2023
c418d43
Implement types for actions and extract entities
sodic Jan 12, 2023
b9f68e4
Merge remote-tracking branch 'origin/main' into filip-types-for-queri…
sodic Jan 19, 2023
2d45fb6
Add Prisma entities to the frontend
sodic Jan 20, 2023
3441e73
Add frontend types to internal todoApp
sodic Jan 20, 2023
7e9a8a4
Merge remote-tracking branch 'origin/main' into filip-frontend-entities
sodic Jan 30, 2023
994c64a
Undo moving for easier review
sodic Jan 30, 2023
807f58c
Revert back to generating a query.js file
sodic Jan 30, 2023
917299a
Rename buildEntityData
sodic Jan 30, 2023
693a9c3
Remove solved todo in schema template
sodic Feb 1, 2023
acdea9a
Fix docs in method
sodic Feb 1, 2023
8464fec
Adding uninstall command (#953)
infomiho Feb 1, 2023
adf2ac9
Use StrongPath instead of FilePath
sodic Feb 1, 2023
e7a3386
Fix review feedback
sodic Feb 1, 2023
b3a9c99
Merge remote-tracking branch 'origin/main' into filip-frontend-entities
sodic Feb 2, 2023
9fed4c9
Move prisma client generation messages
sodic Feb 2, 2023
3c0e9fd
Generalize FileDraft functions
sodic Feb 3, 2023
38c7b55
Generate prisma clients using ENV
sodic Feb 3, 2023
7714d56
Fix schema checksum check
sodic Feb 4, 2023
7b1b08b
Small refactor in db generator
sodic Feb 6, 2023
acc3e0f
Run prisma generate from server root dir
sodic Feb 6, 2023
46ca1a9
Fix types for useAction
sodic Feb 6, 2023
d714717
Fix type error for useAction
sodic Feb 7, 2023
7b17987
Fix schema generation in Dockerfile
sodic Feb 7, 2023
30fe6fc
Refactor passing env vars to prisma schema
sodic Feb 7, 2023
1ce7aae
Fix useAction types
sodic Feb 8, 2023
c75cd3c
Replace Prelude readFile with SP readFile
sodic Feb 8, 2023
fba3e46
Add comment for prisma/client in web app
sodic Feb 8, 2023
b51b631
Replace Prelude writeFile with SP writeFile
sodic Feb 8, 2023
5cba399
Replace do and if with ifM
sodic Feb 8, 2023
4ccbe72
Rename readProjectTelemetryFile
sodic Feb 8, 2023
7cc9541
Refactor readOrCreateUserSignatureFile
sodic Feb 8, 2023
43516f5
Remove redundant comment
sodic Feb 8, 2023
f6dc618
Fix typo in variable name
sodic Feb 8, 2023
f82922a
Simulate unions with a type class
sodic Feb 8, 2023
f49ecf8
Further improve strongpath types
sodic Feb 8, 2023
ccd3548
Generate prisma clients after migration
sodic Feb 10, 2023
d8750a8
Change ModuleRootDir to ComponentRootDir
sodic Feb 10, 2023
77ad8a7
Remove solved todo
sodic Feb 10, 2023
eff0595
Merge remote-tracking branch 'origin/main' into filip-frontend-entities
sodic Feb 10, 2023
c01f0af
Improve naming
sodic Feb 10, 2023
d817192
Remove redundant env variable
sodic Feb 10, 2023
1c73bf6
Improve formatting
sodic Feb 10, 2023
beaaabe
Merge remote-tracking branch 'origin/main' into filip-frontend-entities
sodic Feb 10, 2023
1435a70
Fix errors after merging
sodic Feb 10, 2023
5a3b4f8
Change local function name
sodic Feb 10, 2023
8b78054
Rename local function again
sodic Feb 10, 2023
533ef8a
Update changelog
sodic Feb 13, 2023
0b3eafb
Fix error type in useQuery
sodic Feb 13, 2023
c754861
Rename Component to AppComponent
sodic Feb 13, 2023
ddf6113
Refactor DbGenerator
sodic Feb 13, 2023
6f61e31
Explain Abs paths in SP helpers
sodic Feb 13, 2023
95a747b
Update e2e tests
sodic Feb 13, 2023
89a8e53
Fix formatting
sodic Feb 13, 2023
a70c86c
Change signature for doesFileExist
sodic Feb 13, 2023
8c74e6b
Change signature for SP functions
sodic Feb 13, 2023
2eb4c9d
Remove redundant do block
sodic Feb 13, 2023
c376d78
Fix formatting
sodic Feb 13, 2023
105f5c6
Reorder functions
sodic Feb 13, 2023
bddc967
Rename module to appComponent in functions
sodic Feb 13, 2023
5b089c3
Rename module to component in functions
sodic Feb 13, 2023
3fcdd53
Rename telemetry cache function
sodic Feb 13, 2023
5c92849
Fix formatting
sodic Feb 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion waspc/data/Generator/templates/db/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ datasource db {
generator client {
sodic marked this conversation as resolved.
Show resolved Hide resolved
provider = "prisma-client-js"
{=! TODO(matija): this shouldn't be hardcoded, generator should provide this path. =}
Martinsos marked this conversation as resolved.
Show resolved Hide resolved
output = "../server/node_modules/.prisma/client"
output = "{= clientOutputDir =}"
}

{=# modelSchemas =}
Expand Down
18 changes: 18 additions & 0 deletions waspc/data/Generator/templates/react-app/src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{={= =}=}}
sodic marked this conversation as resolved.
Show resolved Hide resolved
shayneczyzewski marked this conversation as resolved.
Show resolved Hide resolved
import {
{=# entities =}
{= name =},
{=/ entities =}
} from '@prisma/client'

export type {
{=# entities =}
{= name =},
{=/ entities =}
} from '@prisma/client'

export type WaspEntity =
{=# entities =}
| {= name =}
{=/ entities =}
| never
13 changes: 3 additions & 10 deletions waspc/examples/todoApp/src/client/Todo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import createTask from '@wasp/actions/createTask.js'
import updateTaskIsDone from '@wasp/actions/updateTaskIsDone.js'
import deleteCompletedTasks from '@wasp/actions/deleteCompletedTasks.js'
import toggleAllTasks from '@wasp/actions/toggleAllTasks.js'

// Copied from Prisma
type Task = {
id: number
description: string
isDone: boolean
userId: number
}
import { Task } from '@wasp/entities'

type GetTasksError = { message: string }

Expand Down Expand Up @@ -91,7 +84,7 @@ const Tasks = ({ tasks }: { tasks: NonEmptyArray<Task> }) => {
<div>
<table className='border-separate border-spacing-2'>
<tbody>
{tasks.map((task, idx) => <Task task={task} key={idx} />)}
{tasks.map((task, idx) => <TaskView task={task} key={idx} />)}
</tbody>
</table>
</div>
Expand All @@ -100,7 +93,7 @@ const Tasks = ({ tasks }: { tasks: NonEmptyArray<Task> }) => {

type UpdateTaskIsDonePayload = Pick<Task, "id" | "isDone">

const Task = ({ task }: { task: Task }) => {
const TaskView = ({ task }: { task: Task }) => {
const updateTaskIsDoneOptimistically = useAction<UpdateTaskIsDonePayload, void, Task[]>(updateTaskIsDone, {
optimisticUpdates: [{
getQuerySpecifier: () => [getTasks],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react'
import { Link } from 'react-router-dom'
import { User } from '@wasp/entities'

export const ProfilePage = ({ user }) => {
export const ProfilePage = ({ username }: User) => {
return (
<>
<div>I am Profile page for { user.username }!</div>
<div>I am Profile page for { username }!</div>
<br />
<Link to='/'>Go to dashboard</Link>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ import { useAction } from '@wasp/actions'
import updateTaskIsDone from '@wasp/actions/updateTaskIsDone'
import getTask from '@wasp/queries/getTask.js'
import getTasks from '@wasp/queries/getTasks.js'
import { Task } from '@wasp/entities'

const Todo = (props) => {
const Todo = (props: any) => {
const taskId = parseInt(props.match.params.id)
const { data: task, isFetching, error } = useQuery(getTask, { id: taskId })
const { data: task, isFetching, error } = useQuery<unknown, Task, Task | Task[]>(getTask, { id: taskId })
sodic marked this conversation as resolved.
Show resolved Hide resolved

const updateTaskIsDoneOptimistically = useAction(updateTaskIsDone, {
optimisticUpdates: [
{
getQuerySpecifier: () => [getTask, { id: taskId }],
// This query's cache should should never be emtpy
updateQuery: ({ isDone }, oldTask) => ({ ...oldTask, isDone }),
updateQuery: ({ isDone }: Pick<Task, "id" | "isDone">, oldTask: Task) => ({ ...oldTask, isDone }),
},
{
getQuerySpecifier: () => [getTasks],
updateQuery: (updatedTask, oldTasks) => {
updateQuery: (updatedTask: Task, oldTasks: Task[]) => {
if (oldTasks === undefined) {
// cache is empty
return [updatedTask]
Expand Down
2 changes: 0 additions & 2 deletions waspc/examples/todoApp/src/server/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import HttpError from '@wasp/core/HttpError.js'
import { Task } from '@wasp/entities/'
import { AuthenticatedAction } from '@wasp/types'
import { getSomeResource } from './serverSetup.js'
import {
CreateTask,
Expand Down
19 changes: 19 additions & 0 deletions waspc/src/Wasp/Generator/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ module Wasp.Generator.Common
nodeVersionRange,
npmVersionRange,
prismaVersion,
makeJsonWithEntityNameAndPrismaIdentifier,
entityNameToPrismaIdentifier,
)
where

import Data.Aeson (KeyValue ((.=)), object)
import qualified Data.Aeson as Aeson
import Data.Char (toLower)
import qualified Wasp.SemanticVersion as SV

-- | Directory where the whole web app project (client, server, ...) is generated.
Expand Down Expand Up @@ -34,3 +39,17 @@ npmVersionRange = SV.Range [SV.backwardsCompatibleWith latestLTSVersion]

prismaVersion :: SV.Version
prismaVersion = SV.Version 4 5 0

makeJsonWithEntityNameAndPrismaIdentifier :: String -> Aeson.Value
makeJsonWithEntityNameAndPrismaIdentifier name =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
makeJsonWithEntityNameAndPrismaIdentifier name =
makeJsonWithEntityNameAndPrismaIdentifier entityName =

Copy link
Contributor Author

@sodic sodic Feb 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change in #982 to minimize conflicts.

object
[ "name" .= name,
"prismaIdentifier" .= entityNameToPrismaIdentifier name
]
Martinsos marked this conversation as resolved.
Show resolved Hide resolved

-- | Takes a Wasp Entity name (like `SomeTask` from `entity SomeTask {...}`) and
-- converts it into a corresponding Prisma identifier (like `prisma.someTask`).
Martinsos marked this conversation as resolved.
Show resolved Hide resolved
-- This is what Prisma implicitly does when translating `model` declarations to
-- client SDK identifiers. Useful when creating `context.entities` JS objects in Wasp templates.
entityNameToPrismaIdentifier :: String -> String
entityNameToPrismaIdentifier entityName = toLower (head entityName) : tail entityName
44 changes: 32 additions & 12 deletions waspc/src/Wasp/Generator/DbGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ where

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally rearranged the functions to go with our conventions, but it made the diff too unclear. I'll shuffle them around in a different PR.

import Data.Aeson (object, (.=))
import Data.Maybe (fromMaybe, maybeToList)
import StrongPath (Abs, Dir, Path', (</>))
import StrongPath (Abs, Dir, File', Path', Rel, (</>))
import qualified StrongPath as SP
import StrongPath.TH (reldir)
import System.Directory (doesFileExist)
import Wasp.AppSpec (AppSpec, getEntities)
import qualified Wasp.AppSpec as AS
Expand All @@ -21,13 +22,17 @@ import qualified Wasp.AppSpec.Entity as AS.Entity
import Wasp.AppSpec.Valid (getApp)
import Wasp.Generator.Common (ProjectRootDir)
import Wasp.Generator.DbGenerator.Common
( dbMigrationsDirInDbRootDir,
( DbRootDir,
clientDbSchemaFileInProjectRootDir,
dbMigrationsDirInDbRootDir,
dbRootDirInProjectRootDir,
dbSchemaChecksumOnLastDbConcurrenceFileProjectRootDir,
dbSchemaChecksumOnLastGenerateFileProjectRootDir,
dbSchemaFileInDbTemplatesDir,
dbSchemaFileInProjectRootDir,
dbTemplatesDirInTemplatesDir,
serverDbSchemaFileInProjectRootDir,
serverRootDirFromDbRootDir,
webAppRootDirFromDbRootDir,
)
import qualified Wasp.Generator.DbGenerator.Operations as DbOps
import Wasp.Generator.FileDraft (FileDraft, createCopyDirFileDraft, createTemplateFileDraft)
Expand All @@ -43,10 +48,24 @@ import Wasp.Util (checksumFromFilePath, hexToString, ifM, (<:>))

genDb :: AppSpec -> Generator [FileDraft]
genDb spec =
genPrismaSchema spec <:> (maybeToList <$> genMigrationsDir spec)
genServerPrismaSchema spec
<:> genClientPrismaSchema spec
<:> (maybeToList <$> genMigrationsDir spec)

genPrismaSchema :: AppSpec -> Generator FileDraft
genPrismaSchema spec = do
genClientPrismaSchema :: AppSpec -> Generator FileDraft
genClientPrismaSchema =
genPrismaSchema webAppRootDirFromDbRootDir clientDbSchemaFileInProjectRootDir

genServerPrismaSchema :: AppSpec -> Generator FileDraft
genServerPrismaSchema =
genPrismaSchema serverRootDirFromDbRootDir serverDbSchemaFileInProjectRootDir

genPrismaSchema ::
Path' (Rel DbRootDir) (Dir a) ->
Path' (Rel ProjectRootDir) File' ->
AppSpec ->
Generator FileDraft
genPrismaSchema moduleRootDir destinationFile spec = do
(datasourceProvider, datasourceUrl) <- case dbSystem of
AS.Db.PostgreSQL -> return ("postgresql", "env(\"DATABASE_URL\")")
AS.Db.SQLite ->
Expand All @@ -58,14 +77,15 @@ genPrismaSchema spec = do
object
[ "modelSchemas" .= map entityToPslModelSchema (AS.getDecls @AS.Entity.Entity spec),
"datasourceProvider" .= (datasourceProvider :: String),
"datasourceUrl" .= (datasourceUrl :: String)
"datasourceUrl" .= (datasourceUrl :: String),
"clientOutputDir" .= SP.fromRelDir clientOutputDir
]

return $ createTemplateFileDraft dstPath tmplSrcPath (Just templateData)
return $ createTemplateFileDraft destinationFile tmplSrcPath (Just templateData)
where
dstPath = dbSchemaFileInProjectRootDir
tmplSrcPath = dbTemplatesDirInTemplatesDir </> dbSchemaFileInDbTemplatesDir
dbSystem = fromMaybe AS.Db.SQLite (AS.Db.system =<< AS.App.db (snd $ getApp spec))
dbSystem = fromMaybe AS.Db.SQLite $ AS.Db.system =<< AS.App.db (snd $ getApp spec)
clientOutputDir = moduleRootDir </> [reldir|node_modules/.prisma/client|]

entityToPslModelSchema :: (String, AS.Entity.Entity) -> String
entityToPslModelSchema (entityName, entity) =
Expand Down Expand Up @@ -116,7 +136,7 @@ warnIfDbNeedsMigration spec projectRootDir = do
then warnProjectDiffersFromDb projectRootDir
else return Nothing
where
dbSchemaFp = SP.fromAbsFile $ projectRootDir </> dbSchemaFileInProjectRootDir
dbSchemaFp = SP.fromAbsFile $ projectRootDir </> serverDbSchemaFileInProjectRootDir
dbSchemaChecksumFp = SP.fromAbsFile $ projectRootDir </> dbSchemaChecksumOnLastDbConcurrenceFileProjectRootDir
entitiesExist = not . null $ getEntities spec

Expand Down Expand Up @@ -155,7 +175,7 @@ genPrismaClient spec projectRootDir = do
where
wasCurrentSchemaAlreadyGenerated :: IO Bool
wasCurrentSchemaAlreadyGenerated = do
let dbSchemaFp = SP.fromAbsFile $ projectRootDir SP.</> dbSchemaFileInProjectRootDir
let dbSchemaFp = SP.fromAbsFile $ projectRootDir SP.</> serverDbSchemaFileInProjectRootDir
let dbSchemaChecksumFp = SP.fromAbsFile $ projectRootDir SP.</> dbSchemaChecksumOnLastGenerateFileProjectRootDir

dbSchemaChecksumFileExists <- doesFileExist dbSchemaChecksumFp
Expand Down
30 changes: 22 additions & 8 deletions waspc/src/Wasp/Generator/DbGenerator/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ module Wasp.Generator.DbGenerator.Common
dbSchemaChecksumOnLastDbConcurrenceFileProjectRootDir,
dbSchemaChecksumOnLastGenerateFileProjectRootDir,
dbSchemaFileInDbTemplatesDir,
dbSchemaFileInProjectRootDir,
dbTemplatesDirInTemplatesDir,
defaultMigrateArgs,
getOnLastDbConcurrenceChecksumFileRefreshAction,
MigrateArgs (..),
RefreshOnLastDbConcurrenceChecksumFile (..),
DbRootDir,
serverRootDirFromDbRootDir,
webAppRootDirFromDbRootDir,
serverDbSchemaFileInProjectRootDir,
clientDbSchemaFileInProjectRootDir
)
where

import StrongPath (Dir, File, File', Path', Rel, reldir, relfile, (</>))
import qualified StrongPath as SP
import Wasp.Common (DbMigrationsDir)
import Wasp.Generator.Common (ProjectRootDir)
import Wasp.Generator.ServerGenerator.Common (ServerRootDir)
import Wasp.Generator.Templates (TemplatesDir)

data DbRootDir
Expand All @@ -33,6 +37,12 @@ data DbSchemaChecksumOnLastDbConcurrenceFile
-- to know if we need to regenerate schema.prisma during web app generation or not.
data DbSchemaChecksumOnLastGenerateFile

serverRootDirFromDbRootDir :: Path' (Rel DbRootDir) (Dir ServerRootDir)
serverRootDirFromDbRootDir = [reldir|../server|]

webAppRootDirFromDbRootDir :: Path' (Rel DbRootDir) (Dir ServerRootDir)
webAppRootDirFromDbRootDir = [reldir|../web-app|]

dbRootDirInProjectRootDir :: Path' (Rel ProjectRootDir) (Dir DbRootDir)
dbRootDirInProjectRootDir = [reldir|db|]

Expand All @@ -42,13 +52,17 @@ dbTemplatesDirInTemplatesDir = [reldir|db|]
dbSchemaFileInDbTemplatesDir :: Path' (Rel DbTemplatesDir) File'
dbSchemaFileInDbTemplatesDir = [relfile|schema.prisma|]

dbSchemaFileInDbRootDir :: Path' (Rel DbRootDir) File'
-- Generated schema file will be in the same relative location as the
-- template file within templates dir.
dbSchemaFileInDbRootDir = SP.castRel dbSchemaFileInDbTemplatesDir
clientDbSchemaFileInDbRootDir :: Path' (Rel DbRootDir) File'
clientDbSchemaFileInDbRootDir = [relfile|schema.client.prisma|]

serverDbSchemaFileInDbRootDir :: Path' (Rel DbRootDir) File'
serverDbSchemaFileInDbRootDir = [relfile|schema.server.prisma|]

clientDbSchemaFileInProjectRootDir :: Path' (Rel ProjectRootDir) File'
clientDbSchemaFileInProjectRootDir = dbRootDirInProjectRootDir </> clientDbSchemaFileInDbRootDir

dbSchemaFileInProjectRootDir :: Path' (Rel ProjectRootDir) File'
dbSchemaFileInProjectRootDir = dbRootDirInProjectRootDir </> dbSchemaFileInDbRootDir
serverDbSchemaFileInProjectRootDir :: Path' (Rel ProjectRootDir) File'
serverDbSchemaFileInProjectRootDir = dbRootDirInProjectRootDir </> serverDbSchemaFileInDbRootDir

dbMigrationsDirInDbRootDir :: Path' (Rel DbRootDir) (Dir DbMigrationsDir)
dbMigrationsDirInDbRootDir = [reldir|migrations|]
Expand Down
50 changes: 29 additions & 21 deletions waspc/src/Wasp/Generator/DbGenerator/Jobs.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Wasp.Generator.DbGenerator.Jobs
( migrateDev,
migrateDiff,
generatePrismaClient,
generatePrismaClientForClient,
generatePrismaClientForServer,
runStudio,
migrateStatus,
asPrismaCliArgs,
Expand All @@ -10,26 +11,18 @@ where

import StrongPath (Abs, Dir, File', Path', Rel, (</>))
import qualified StrongPath as SP
import StrongPath.TH (relfile)
import qualified System.Info
import Wasp.Generator.Common (ProjectRootDir)
import Wasp.Generator.DbGenerator.Common (MigrateArgs (..), dbSchemaFileInProjectRootDir)
import Wasp.Generator.DbGenerator.Common (MigrateArgs (..), clientDbSchemaFileInProjectRootDir, serverDbSchemaFileInProjectRootDir)
import qualified Wasp.Generator.Job as J
import Wasp.Generator.Job.Process (runNodeCommandAsJob)
import Wasp.Generator.ServerGenerator.Common (serverRootDirInProjectRootDir)

-- | NOTE: The expectation is that `npm install` was already executed
-- such that we can use the locally installed package.
-- This assumption is ok since it happens during compilation now.
prismaInServerNodeModules :: Path' (Rel ProjectRootDir) File'
prismaInServerNodeModules = serverRootDirInProjectRootDir </> [SP.relfile|./node_modules/.bin/prisma|]

absPrismaExecutableFp :: Path' Abs (Dir ProjectRootDir) -> FilePath
absPrismaExecutableFp projectDir = SP.toFilePath $ projectDir </> prismaInServerNodeModules

migrateDev :: Path' Abs (Dir ProjectRootDir) -> MigrateArgs -> J.Job
migrateDev projectDir migrateArgs = do
let serverDir = projectDir </> serverRootDirInProjectRootDir
let schemaFile = projectDir </> dbSchemaFileInProjectRootDir
let schemaFile = projectDir </> serverDbSchemaFileInProjectRootDir

-- NOTE(matija): We are running this command from server's root dir since that is where
-- Prisma packages (cli and client) are currently installed.
Expand Down Expand Up @@ -63,7 +56,7 @@ asPrismaCliArgs migrateArgs = do
migrateDiff :: Path' Abs (Dir ProjectRootDir) -> J.Job
migrateDiff projectDir = do
let serverDir = projectDir </> serverRootDirInProjectRootDir
let schemaFileFp = SP.toFilePath $ projectDir </> dbSchemaFileInProjectRootDir
let schemaFileFp = SP.toFilePath $ projectDir </> serverDbSchemaFileInProjectRootDir
let prismaMigrateDiffCmdArgs =
[ "migrate",
"diff",
Expand All @@ -84,7 +77,7 @@ migrateDiff projectDir = do
migrateStatus :: Path' Abs (Dir ProjectRootDir) -> J.Job
migrateStatus projectDir = do
let serverDir = projectDir </> serverRootDirInProjectRootDir
let schemaFileFp = SP.toFilePath $ projectDir </> dbSchemaFileInProjectRootDir
let schemaFileFp = SP.toFilePath $ projectDir </> serverDbSchemaFileInProjectRootDir
let prismaMigrateDiffCmdArgs =
[ "migrate",
"status",
Expand All @@ -98,15 +91,30 @@ migrateStatus projectDir = do
runStudio :: Path' Abs (Dir ProjectRootDir) -> J.Job
runStudio projectDir = do
let serverDir = projectDir </> serverRootDirInProjectRootDir
let schemaFile = projectDir </> dbSchemaFileInProjectRootDir
let schemaFile = projectDir </> serverDbSchemaFileInProjectRootDir
let prismaStudioCmdArgs = ["studio", "--schema", SP.toFilePath schemaFile]

runNodeCommandAsJob serverDir (absPrismaExecutableFp projectDir) prismaStudioCmdArgs J.Db

generatePrismaClient :: Path' Abs (Dir ProjectRootDir) -> J.Job
generatePrismaClient projectDir = do
let serverDir = projectDir </> serverRootDirInProjectRootDir
let schemaFile = projectDir </> dbSchemaFileInProjectRootDir
let prismaGenerateCmdArgs = ["generate", "--schema", SP.toFilePath schemaFile]
generatePrismaClientForServer :: Path' Abs (Dir ProjectRootDir) -> J.Job
generatePrismaClientForServer projectDir =
generatePrismaClient projectDir serverDbSchemaFileInProjectRootDir

runNodeCommandAsJob serverDir (absPrismaExecutableFp projectDir) prismaGenerateCmdArgs J.Db
generatePrismaClientForClient :: Path' Abs (Dir ProjectRootDir) -> J.Job
generatePrismaClientForClient projectDir =
generatePrismaClient projectDir clientDbSchemaFileInProjectRootDir

generatePrismaClient :: Path' Abs (Dir ProjectRootDir) -> Path' (Rel ProjectRootDir) File' -> J.Job
generatePrismaClient projectDir dbSchemaFile =
runNodeCommandAsJob projectDir prismaExecutable prismaGenerateCmdArgs J.Db
where
prismaExecutable = absPrismaExecutableFp projectDir
prismaGenerateCmdArgs = ["generate", "--schema", SP.toFilePath schemaFile]
schemaFile = projectDir </> dbSchemaFile

-- | NOTE: The expectation is that `npm install` was already executed
-- such that we can use the locally installed package.
-- This assumption is ok since it happens during compilation now.
absPrismaExecutableFp :: Path' Abs (Dir ProjectRootDir) -> FilePath
Martinsos marked this conversation as resolved.
Show resolved Hide resolved
absPrismaExecutableFp projectDir =
SP.fromAbsFile $ projectDir </> serverRootDirInProjectRootDir </> [relfile|./node_modules/.bin/prisma|]
Loading