Move prisma client to top-level node_modules

This commit is contained in:
Filip Sodić 2023-12-18 17:57:14 +01:00
parent d8f9ee2c24
commit 52c547b3f2
8 changed files with 88 additions and 125 deletions

View File

@ -26,7 +26,7 @@ COPY server/ ./server/
RUN cd server && npm install RUN cd server && npm install
{=# usingPrisma =} {=# usingPrisma =}
COPY db/schema.prisma ./db/ COPY db/schema.prisma ./db/
RUN cd server && {= serverPrismaClientOutputDirEnv =} npx prisma generate --schema='{= dbSchemaFileFromServerDir =}' RUN cd server && npx prisma generate --schema='{= dbSchemaFileFromServerDir =}'
{=/ usingPrisma =} {=/ usingPrisma =}
# Building the server should come after Prisma generation. # Building the server should come after Prisma generation.
RUN cd server && npm run build RUN cd server && npm run build

View File

@ -10,7 +10,6 @@ datasource db {
generator client { generator client {
provider = "prisma-client-js" provider = "prisma-client-js"
output = {=& prismaClientOutputDir =}
{=# prismaPreviewFeatures =} {=# prismaPreviewFeatures =}
previewFeatures = {=& . =} previewFeatures = {=& . =}
{=/ prismaPreviewFeatures =} {=/ prismaPreviewFeatures =}

View File

@ -29,7 +29,6 @@ import Wasp.Generator.DbGenerator.Common
dbSchemaFileInDbTemplatesDir, dbSchemaFileInDbTemplatesDir,
dbSchemaFileInProjectRootDir, dbSchemaFileInProjectRootDir,
dbTemplatesDirInTemplatesDir, dbTemplatesDirInTemplatesDir,
prismaClientOutputDirEnvVar,
) )
import qualified Wasp.Generator.DbGenerator.Operations as DbOps import qualified Wasp.Generator.DbGenerator.Operations as DbOps
import Wasp.Generator.FileDraft (FileDraft, createCopyDirFileDraft, createTemplateFileDraft) import Wasp.Generator.FileDraft (FileDraft, createCopyDirFileDraft, createTemplateFileDraft)
@ -68,7 +67,6 @@ genPrismaSchema spec = do
[ "modelSchemas" .= map entityToPslModelSchema (AS.getDecls @AS.Entity.Entity spec), [ "modelSchemas" .= map entityToPslModelSchema (AS.getDecls @AS.Entity.Entity spec),
"datasourceProvider" .= datasourceProvider, "datasourceProvider" .= datasourceProvider,
"datasourceUrl" .= datasourceUrl, "datasourceUrl" .= datasourceUrl,
"prismaClientOutputDir" .= makeEnvVarField Wasp.Generator.DbGenerator.Common.prismaClientOutputDirEnvVar,
"prismaPreviewFeatures" .= prismaPreviewFeatures, "prismaPreviewFeatures" .= prismaPreviewFeatures,
"dbExtensions" .= dbExtensions "dbExtensions" .= dbExtensions
] ]
@ -95,7 +93,7 @@ genMigrationsDir spec = return $ createCopyDirFileDraft RemoveExistingDstDir gen
postWriteDbGeneratorActions :: AppSpec -> Path' Abs (Dir ProjectRootDir) -> IO ([GeneratorWarning], [GeneratorError]) postWriteDbGeneratorActions :: AppSpec -> Path' Abs (Dir ProjectRootDir) -> IO ([GeneratorWarning], [GeneratorError])
postWriteDbGeneratorActions spec dstDir = do postWriteDbGeneratorActions spec dstDir = do
dbGeneratorWarnings <- maybeToList <$> warnIfDbNeedsMigration spec dstDir dbGeneratorWarnings <- maybeToList <$> warnIfDbNeedsMigration spec dstDir
dbGeneratorErrors <- maybeToList <$> genPrismaClients spec dstDir dbGeneratorErrors <- maybeToList <$> generatePrismaClient spec dstDir
return (dbGeneratorWarnings, dbGeneratorErrors) return (dbGeneratorWarnings, dbGeneratorErrors)
-- | Checks if user needs to run `wasp db migrate-dev` due to changes in schema.prisma, and if so, returns a warning. -- | Checks if user needs to run `wasp db migrate-dev` due to changes in schema.prisma, and if so, returns a warning.
@ -173,12 +171,12 @@ warnProjectDiffersFromDb projectRootDir = do
"Wasp was unable to verify your database is up to date." "Wasp was unable to verify your database is up to date."
<> " Running `wasp db migrate-dev` may fix this and will provide more info." <> " Running `wasp db migrate-dev` may fix this and will provide more info."
genPrismaClients :: AppSpec -> Path' Abs (Dir ProjectRootDir) -> IO (Maybe GeneratorError) generatePrismaClient :: AppSpec -> Path' Abs (Dir ProjectRootDir) -> IO (Maybe GeneratorError)
genPrismaClients spec projectRootDir = generatePrismaClient spec projectRootDir =
ifM ifM
wasCurrentSchemaAlreadyGenerated wasCurrentSchemaAlreadyGenerated
(return Nothing) (return Nothing)
generatePrismaClientsIfEntitiesExist generatePrismaClientIfEntitiesExist
where where
wasCurrentSchemaAlreadyGenerated :: IO Bool wasCurrentSchemaAlreadyGenerated :: IO Bool
wasCurrentSchemaAlreadyGenerated = wasCurrentSchemaAlreadyGenerated =
@ -186,10 +184,10 @@ genPrismaClients spec projectRootDir =
projectRootDir projectRootDir
Wasp.Generator.DbGenerator.Common.dbSchemaChecksumOnLastGenerateFileProjectRootDir Wasp.Generator.DbGenerator.Common.dbSchemaChecksumOnLastGenerateFileProjectRootDir
generatePrismaClientsIfEntitiesExist :: IO (Maybe GeneratorError) generatePrismaClientIfEntitiesExist :: IO (Maybe GeneratorError)
generatePrismaClientsIfEntitiesExist generatePrismaClientIfEntitiesExist
| entitiesExist = | entitiesExist =
either (Just . GenericGeneratorError) (const Nothing) <$> DbOps.generatePrismaClients projectRootDir either (Just . GenericGeneratorError) (const Nothing) <$> DbOps.generatePrismaClient projectRootDir
| otherwise = return Nothing | otherwise = return Nothing
entitiesExist = not . null $ getEntities spec entitiesExist = not . null $ getEntities spec

View File

@ -1,8 +1,5 @@
module Wasp.Generator.DbGenerator.Common module Wasp.Generator.DbGenerator.Common
( dbMigrationsDirInDbRootDir, ( dbMigrationsDirInDbRootDir,
serverPrismaClientOutputDirEnv,
webAppPrismaClientOutputDirEnv,
prismaClientOutputDirInAppComponentDir,
dbSchemaFileFromAppComponentDir, dbSchemaFileFromAppComponentDir,
dbRootDirInProjectRootDir, dbRootDirInProjectRootDir,
dbSchemaChecksumOnLastDbConcurrenceFileProjectRootDir, dbSchemaChecksumOnLastDbConcurrenceFileProjectRootDir,
@ -19,15 +16,15 @@ module Wasp.Generator.DbGenerator.Common
serverRootDirFromDbRootDir, serverRootDirFromDbRootDir,
webAppRootDirFromDbRootDir, webAppRootDirFromDbRootDir,
dbSchemaFileInProjectRootDir, dbSchemaFileInProjectRootDir,
prismaClientOutputDirEnvVar, waspProjectDirFromProjectRootDir,
DbSchemaChecksumFile, DbSchemaChecksumFile,
) )
where where
import StrongPath (Dir, File, File', Path', Rel, reldir, relfile, (</>)) import StrongPath (Dir, File, File', Path', Rel, reldir, relfile, (</>))
import qualified StrongPath as SP
import Wasp.Generator.Common (AppComponentRootDir, DbRootDir, ProjectRootDir, ServerRootDir) import Wasp.Generator.Common (AppComponentRootDir, DbRootDir, ProjectRootDir, ServerRootDir)
import Wasp.Generator.Templates (TemplatesDir) import Wasp.Generator.Templates (TemplatesDir)
import Wasp.Project.Common (WaspProjectDir)
import Wasp.Project.Db.Migrations (DbMigrationsDir) import Wasp.Project.Db.Migrations (DbMigrationsDir)
data DbTemplatesDir data DbTemplatesDir
@ -93,21 +90,8 @@ dbSchemaChecksumOnLastGenerateFileInDbRootDir = [relfile|schema.prisma.wasp-gene
dbSchemaChecksumOnLastGenerateFileProjectRootDir :: Path' (Rel ProjectRootDir) (File DbSchemaChecksumOnLastGenerateFile) dbSchemaChecksumOnLastGenerateFileProjectRootDir :: Path' (Rel ProjectRootDir) (File DbSchemaChecksumOnLastGenerateFile)
dbSchemaChecksumOnLastGenerateFileProjectRootDir = dbRootDirInProjectRootDir </> dbSchemaChecksumOnLastGenerateFileInDbRootDir dbSchemaChecksumOnLastGenerateFileProjectRootDir = dbRootDirInProjectRootDir </> dbSchemaChecksumOnLastGenerateFileInDbRootDir
prismaClientOutputDirEnvVar :: String waspProjectDirFromProjectRootDir :: Path' (Rel ProjectRootDir) (Dir WaspProjectDir)
prismaClientOutputDirEnvVar = "PRISMA_CLIENT_OUTPUT_DIR" waspProjectDirFromProjectRootDir = [reldir|../../|]
prismaClientOutputDirInAppComponentDir :: AppComponentRootDir d => Path' (Rel d) (Dir ServerRootDir)
prismaClientOutputDirInAppComponentDir = [reldir|node_modules/.prisma/client|]
serverPrismaClientOutputDirEnv :: (String, String)
serverPrismaClientOutputDirEnv = appComponentPrismaClientOutputDirEnv serverRootDirFromDbRootDir
webAppPrismaClientOutputDirEnv :: (String, String)
webAppPrismaClientOutputDirEnv = appComponentPrismaClientOutputDirEnv webAppRootDirFromDbRootDir
appComponentPrismaClientOutputDirEnv :: AppComponentRootDir d => Path' (Rel DbRootDir) (Dir d) -> (String, String)
appComponentPrismaClientOutputDirEnv appComponentDirFromDbRootDir =
(prismaClientOutputDirEnvVar, SP.fromRelDir $ appComponentDirFromDbRootDir </> prismaClientOutputDirInAppComponentDir)
data MigrateArgs = MigrateArgs data MigrateArgs = MigrateArgs
{ _migrationName :: Maybe String, { _migrationName :: Maybe String,

View File

@ -13,24 +13,24 @@ module Wasp.Generator.DbGenerator.Jobs
) )
where where
import StrongPath (Abs, Dir, File, File', Path', (</>)) import StrongPath (Abs, Dir, File', Path', (</>))
import qualified StrongPath as SP import qualified StrongPath as SP
import StrongPath.TH (relfile) import StrongPath.TH (relfile)
import qualified System.Info import qualified System.Info
import Wasp.Generator.Common (ProjectRootDir) import Wasp.Generator.Common (ProjectRootDir)
import Wasp.Generator.DbGenerator.Common import Wasp.Generator.DbGenerator.Common
( MigrateArgs (..), ( MigrateArgs (..),
PrismaDbSchema,
dbSchemaFileInProjectRootDir, dbSchemaFileInProjectRootDir,
waspProjectDirFromProjectRootDir,
) )
import Wasp.Generator.Job (JobType)
import qualified Wasp.Generator.Job as J import qualified Wasp.Generator.Job as J
import Wasp.Generator.Job.Process (runNodeCommandAsJob, runNodeCommandAsJobWithExtraEnv) import Wasp.Generator.Job.Process (runNodeCommandAsJob, runNodeCommandAsJobWithExtraEnv)
import Wasp.Generator.ServerGenerator.Common (serverRootDirInProjectRootDir) import Wasp.Generator.ServerGenerator.Common (serverRootDirInProjectRootDir)
import Wasp.Generator.ServerGenerator.Db.Seed (dbSeedNameEnvVarName) import Wasp.Generator.ServerGenerator.Db.Seed (dbSeedNameEnvVarName)
import Wasp.Project.Common (WaspProjectDir)
migrateDev :: Path' Abs (Dir ProjectRootDir) -> MigrateArgs -> J.Job migrateDev :: Path' Abs (Dir ProjectRootDir) -> MigrateArgs -> J.Job
migrateDev projectDir migrateArgs = migrateDev projectRootDir migrateArgs =
-- NOTE(matija): We are running this command from server's root dir since that is where -- NOTE(matija): We are running this command from server's root dir since that is where
-- Prisma packages (cli and client) are currently installed. -- Prisma packages (cli and client) are currently installed.
-- NOTE(martin): `prisma migrate dev` refuses to execute when interactivity is needed if stdout is being piped, -- NOTE(martin): `prisma migrate dev` refuses to execute when interactivity is needed if stdout is being piped,
@ -40,8 +40,8 @@ migrateDev projectDir migrateArgs =
-- we are using `script` to trick Prisma into thinking it is running in TTY (interactively). -- we are using `script` to trick Prisma into thinking it is running in TTY (interactively).
runNodeCommandAsJob serverDir "script" scriptArgs J.Db runNodeCommandAsJob serverDir "script" scriptArgs J.Db
where where
serverDir = projectDir </> serverRootDirInProjectRootDir serverDir = projectRootDir </> serverRootDirInProjectRootDir
schemaFile = projectDir </> dbSchemaFileInProjectRootDir schemaFile = projectRootDir </> dbSchemaFileInProjectRootDir
scriptArgs = scriptArgs =
if System.Info.os == "darwin" if System.Info.os == "darwin"
@ -57,7 +57,7 @@ migrateDev projectDir migrateArgs =
-- * Linux - we are passing the command as one argument, so we MUST quote the paths. -- * Linux - we are passing the command as one argument, so we MUST quote the paths.
buildPrismaMigrateCmd :: (String -> String) -> [String] buildPrismaMigrateCmd :: (String -> String) -> [String]
buildPrismaMigrateCmd argQuoter = buildPrismaMigrateCmd argQuoter =
[ argQuoter $ absPrismaExecutableFp projectDir, [ argQuoter $ absPrismaExecutableFp (projectRootDir </> waspProjectDirFromProjectRootDir),
"migrate", "migrate",
"dev", "dev",
"--schema", "--schema",
@ -85,7 +85,9 @@ asPrismaCliArgs migrateArgs = do
-- Because of the --exit-code flag, it changes the exit code behavior -- Because of the --exit-code flag, it changes the exit code behavior
-- to signal if the diff is empty or not (Empty: 0, Error: 1, Not empty: 2) -- to signal if the diff is empty or not (Empty: 0, Error: 1, Not empty: 2)
migrateDiff :: Path' Abs (Dir ProjectRootDir) -> J.Job migrateDiff :: Path' Abs (Dir ProjectRootDir) -> J.Job
migrateDiff projectDir = runPrismaCommandAsDbJob projectDir $ \schema -> migrateDiff projectRootDir =
runPrismaCommandAsJob
projectRootDir
[ "migrate", [ "migrate",
"diff", "diff",
"--from-schema-datamodel", "--from-schema-datamodel",
@ -94,6 +96,8 @@ migrateDiff projectDir = runPrismaCommandAsDbJob projectDir $ \schema ->
SP.fromAbsFile schema, SP.fromAbsFile schema,
"--exit-code" "--exit-code"
] ]
where
schema = projectRootDir </> dbSchemaFileInProjectRootDir
-- | Checks to see if all migrations are applied to the DB. -- | Checks to see if all migrations are applied to the DB.
-- An exit code of 0 means we successfully verified all migrations are applied. -- An exit code of 0 means we successfully verified all migrations are applied.
@ -101,27 +105,34 @@ migrateDiff projectDir = runPrismaCommandAsDbJob projectDir $ \schema ->
-- or (b) there are pending migrations to apply. -- or (b) there are pending migrations to apply.
-- Therefore, this should be checked **after** a command that ensures connectivity. -- Therefore, this should be checked **after** a command that ensures connectivity.
migrateStatus :: Path' Abs (Dir ProjectRootDir) -> J.Job migrateStatus :: Path' Abs (Dir ProjectRootDir) -> J.Job
migrateStatus projectDir = runPrismaCommandAsDbJob projectDir $ \schema -> migrateStatus projectRootDir =
runPrismaCommandAsJob
projectRootDir
["migrate", "status", "--schema", SP.fromAbsFile schema] ["migrate", "status", "--schema", SP.fromAbsFile schema]
where
schema = projectRootDir </> dbSchemaFileInProjectRootDir
-- | Runs `prisma migrate reset`, which drops the tables (so schemas and data is lost) and then -- | Runs `prisma migrate reset`, which drops the tables (so schemas and data is lost) and then
-- reapplies all the migrations. -- reapplies all the migrations.
reset :: Path' Abs (Dir ProjectRootDir) -> J.Job reset :: Path' Abs (Dir ProjectRootDir) -> J.Job
reset projectDir = runPrismaCommandAsDbJob projectDir $ \schema -> reset projectRootDir =
runPrismaCommandAsJob
projectRootDir
-- NOTE(martin): We do "--skip-seed" here because I just think seeding happening automatically on -- NOTE(martin): We do "--skip-seed" here because I just think seeding happening automatically on
-- reset is too aggressive / confusing. -- reset is too aggressive / confusing.
["migrate", "reset", "--schema", SP.fromAbsFile schema, "--skip-generate", "--skip-seed"] ["migrate", "reset", "--schema", SP.fromAbsFile schema, "--skip-generate", "--skip-seed"]
where
schema = projectRootDir </> dbSchemaFileInProjectRootDir
-- | Runs `prisma db seed`, which executes the seeding script specified in package.json in -- | Runs `prisma db seed`, which executes the seeding script specified in package.json in
-- prisma.seed field. -- prisma.seed field.
seed :: Path' Abs (Dir ProjectRootDir) -> String -> J.Job seed :: Path' Abs (Dir ProjectRootDir) -> String -> J.Job
-- NOTE: Since v 0.3, Prisma doesn't use --schema parameter for `db seed`. -- NOTE: Since v 0.3, Prisma doesn't use --schema parameter for `db seed`.
seed projectDir seedName = seed projectRootDir seedName =
runPrismaCommandAsJobWithExtraEnv runPrismaCommandAsJobWithExtraEnv
J.Db
[(dbSeedNameEnvVarName, seedName)] [(dbSeedNameEnvVarName, seedName)]
projectDir projectRootDir
(const ["db", "seed"]) ["db", "seed"]
-- | Checks if the DB is running and connectable by running -- | Checks if the DB is running and connectable by running
-- `prisma db execute --stdin --schema <path to db schema>`. -- `prisma db execute --stdin --schema <path to db schema>`.
@ -130,56 +141,48 @@ seed projectDir seedName =
-- Since nothing is passed to stdin, `prisma db execute` just runs an empty -- Since nothing is passed to stdin, `prisma db execute` just runs an empty
-- SQL command, which works perfectly for checking if the database is running. -- SQL command, which works perfectly for checking if the database is running.
dbExecuteTest :: Path' Abs (Dir ProjectRootDir) -> J.Job dbExecuteTest :: Path' Abs (Dir ProjectRootDir) -> J.Job
dbExecuteTest projectDir = dbExecuteTest projectRootDir =
let absSchemaPath = projectDir </> dbSchemaFileInProjectRootDir let absSchemaPath = projectRootDir </> dbSchemaFileInProjectRootDir
in runPrismaCommandAsDbJob in runPrismaCommandAsJob
projectDir projectRootDir
(const ["db", "execute", "--stdin", "--schema", SP.fromAbsFile absSchemaPath]) ["db", "execute", "--stdin", "--schema", SP.fromAbsFile absSchemaPath]
-- | Runs `prisma studio` - Prisma's db inspector. -- | Runs `prisma studio` - Prisma's db inspector.
runStudio :: Path' Abs (Dir ProjectRootDir) -> J.Job runStudio :: Path' Abs (Dir ProjectRootDir) -> J.Job
runStudio projectDir = runPrismaCommandAsDbJob projectDir $ \schema -> runStudio projectRootDir =
["studio", "--schema", SP.fromAbsFile schema] runPrismaCommandAsJob projectRootDir ["studio", "--schema", SP.fromAbsFile schema]
generatePrismaClient :: Path' Abs (Dir ProjectRootDir) -> (String, String) -> JobType -> J.Job
generatePrismaClient projectDir prismaClientOutputDirEnv jobType =
runPrismaCommandAsJobWithExtraEnv jobType envVars projectDir $ \schema ->
["generate", "--schema", SP.fromAbsFile schema]
where where
envVars = [prismaClientOutputDirEnv] schema = projectRootDir </> dbSchemaFileInProjectRootDir
runPrismaCommandAsDbJob :: generatePrismaClient :: Path' Abs (Dir ProjectRootDir) -> J.Job
Path' Abs (Dir ProjectRootDir) -> generatePrismaClient projectRootDir =
(Path' Abs (File PrismaDbSchema) -> [String]) -> runPrismaCommandAsJob projectRootDir args
J.Job where
runPrismaCommandAsDbJob projectDir makeCmdArgs = args =
runPrismaCommandAsJob J.Db projectDir makeCmdArgs [ "generate",
"--schema",
SP.fromAbsFile $ projectRootDir </> dbSchemaFileInProjectRootDir
]
runPrismaCommandAsJob :: runPrismaCommandAsJob :: Path' Abs (Dir ProjectRootDir) -> [String] -> J.Job
JobType -> runPrismaCommandAsJob projectRootDir args =
Path' Abs (Dir ProjectRootDir) -> runPrismaCommandAsJobWithExtraEnv [] projectRootDir args
(Path' Abs (File PrismaDbSchema) -> [String]) ->
J.Job
runPrismaCommandAsJob jobType projectDir makeCmdArgs =
runPrismaCommandAsJobWithExtraEnv jobType [] projectDir makeCmdArgs
runPrismaCommandAsJobWithExtraEnv :: runPrismaCommandAsJobWithExtraEnv ::
JobType ->
[(String, String)] -> [(String, String)] ->
Path' Abs (Dir ProjectRootDir) -> Path' Abs (Dir ProjectRootDir) ->
(Path' Abs (File PrismaDbSchema) -> [String]) -> [String] ->
J.Job J.Job
runPrismaCommandAsJobWithExtraEnv jobType envVars projectDir makeCmdArgs = runPrismaCommandAsJobWithExtraEnv envVars projectRootDir args =
runNodeCommandAsJobWithExtraEnv envVars serverDir (absPrismaExecutableFp projectDir) (makeCmdArgs schemaFile) jobType runNodeCommandAsJobWithExtraEnv envVars waspProjectDir (absPrismaExecutableFp waspProjectDir) args J.Db
where where
serverDir = projectDir </> serverRootDirInProjectRootDir waspProjectDir = projectRootDir </> waspProjectDirFromProjectRootDir
schemaFile = projectDir </> dbSchemaFileInProjectRootDir
-- | NOTE: The expectation is that `npm install` was already executed -- | NOTE: The expectation is that `npm install` was already executed
-- such that we can use the locally installed package. -- such that we can use the locally installed package.
-- This assumption is ok since it happens during compilation now. -- This assumption is ok since it happens during compilation now.
absPrismaExecutableFp :: Path' Abs (Dir ProjectRootDir) -> FilePath absPrismaExecutableFp :: Path' Abs (Dir WaspProjectDir) -> FilePath
absPrismaExecutableFp projectDir = SP.fromAbsFile prismaExecutableAbs absPrismaExecutableFp waspProjectDir = SP.fromAbsFile prismaExecutableAbs
where where
prismaExecutableAbs :: Path' Abs File' prismaExecutableAbs :: Path' Abs File'
prismaExecutableAbs = projectDir </> serverRootDirInProjectRootDir </> [relfile|./node_modules/.bin/prisma|] prismaExecutableAbs = waspProjectDir </> [relfile|./node_modules/.bin/prisma|]

View File

@ -1,6 +1,6 @@
module Wasp.Generator.DbGenerator.Operations module Wasp.Generator.DbGenerator.Operations
( migrateDevAndCopyToSource, ( migrateDevAndCopyToSource,
generatePrismaClients, generatePrismaClient,
doesSchemaMatchDb, doesSchemaMatchDb,
writeDbSchemaChecksumToFile, writeDbSchemaChecksumToFile,
areAllMigrationsAppliedToDb, areAllMigrationsAppliedToDb,
@ -12,13 +12,10 @@ module Wasp.Generator.DbGenerator.Operations
) )
where where
import Control.Applicative (liftA2)
import Control.Concurrent (newChan) import Control.Concurrent (newChan)
import Control.Concurrent.Async (concurrently) import Control.Concurrent.Async (concurrently)
import Control.Monad (when)
import Control.Monad.Catch (catch) import Control.Monad.Catch (catch)
import Control.Monad.Extra (whenM) import Control.Monad.Extra (whenM)
import Data.Either (isRight)
import qualified Data.Text as T import qualified Data.Text as T
import qualified Path as P import qualified Path as P
import StrongPath (Abs, Dir, File, Path', Rel, (</>)) import StrongPath (Abs, Dir, File, Path', Rel, (</>))
@ -36,8 +33,6 @@ import Wasp.Generator.DbGenerator.Common
dbSchemaChecksumOnLastGenerateFileProjectRootDir, dbSchemaChecksumOnLastGenerateFileProjectRootDir,
dbSchemaFileInProjectRootDir, dbSchemaFileInProjectRootDir,
getOnLastDbConcurrenceChecksumFileRefreshAction, getOnLastDbConcurrenceChecksumFileRefreshAction,
serverPrismaClientOutputDirEnv,
webAppPrismaClientOutputDirEnv,
) )
import qualified Wasp.Generator.DbGenerator.Jobs as DbJobs import qualified Wasp.Generator.DbGenerator.Jobs as DbJobs
import Wasp.Generator.FileDraft.WriteableMonad (WriteableMonad (copyDirectoryRecursive, doesDirectoryExist)) import Wasp.Generator.FileDraft.WriteableMonad (WriteableMonad (copyDirectoryRecursive, doesDirectoryExist))
@ -184,31 +179,21 @@ isDbConnectionPossible DbConnectionSuccess = True
isDbConnectionPossible DbNotCreated = True isDbConnectionPossible DbNotCreated = True
isDbConnectionPossible _ = False isDbConnectionPossible _ = False
generatePrismaClients :: Path' Abs (Dir ProjectRootDir) -> IO (Either String ()) generatePrismaClient :: Path' Abs (Dir ProjectRootDir) -> IO (Either String ())
generatePrismaClients projectRootDir = do generatePrismaClient projectRootDir = do
generateResult <- liftA2 (>>) generatePrismaClientForServer generatePrismaClientForWebApp projectRootDir
when (isRight generateResult) updateDbSchemaChecksumOnLastGenerate
return generateResult
where
generatePrismaClientForServer = generatePrismaClient serverPrismaClientOutputDirEnv J.Server
generatePrismaClientForWebApp = generatePrismaClient webAppPrismaClientOutputDirEnv J.WebApp
updateDbSchemaChecksumOnLastGenerate =
writeDbSchemaChecksumToFile projectRootDir dbSchemaChecksumOnLastGenerateFileProjectRootDir
generatePrismaClient ::
(String, String) ->
J.JobType ->
Path' Abs (Dir ProjectRootDir) ->
IO (Either String ())
generatePrismaClient prismaClientOutputDirEnv jobType projectRootDir = do
chan <- newChan chan <- newChan
(_, exitCode) <- (_, exitCode) <-
concurrently concurrently
(readJobMessagesAndPrintThemPrefixed chan) (readJobMessagesAndPrintThemPrefixed chan)
(DbJobs.generatePrismaClient projectRootDir prismaClientOutputDirEnv jobType chan) (DbJobs.generatePrismaClient projectRootDir chan)
return $ case exitCode of case exitCode of
ExitSuccess -> Right () ExitFailure code -> return $ Left $ "Prisma client generation failed with exit code: " ++ show code
ExitFailure code -> Left $ "Prisma client generation failed with exit code: " ++ show code ExitSuccess -> do
updateDbSchemaChecksumOnLastGenerate
return $ Right ()
where
updateDbSchemaChecksumOnLastGenerate =
writeDbSchemaChecksumToFile projectRootDir dbSchemaChecksumOnLastGenerateFileProjectRootDir
-- | Checks `prisma migrate diff` exit code to determine if schema.prisma is -- | Checks `prisma migrate diff` exit code to determine if schema.prisma is
-- different than the DB. Returns Nothing on error as we do not know the current state. -- different than the DB. Returns Nothing on error as we do not know the current state.

View File

@ -23,7 +23,6 @@ import Wasp.Generator.Common
import Wasp.Generator.DbGenerator.Common import Wasp.Generator.DbGenerator.Common
( PrismaDbSchema, ( PrismaDbSchema,
dbSchemaFileFromAppComponentDir, dbSchemaFileFromAppComponentDir,
serverPrismaClientOutputDirEnv,
) )
import Wasp.Generator.FileDraft (FileDraft (..), createTemplateFileDraft) import Wasp.Generator.FileDraft (FileDraft (..), createTemplateFileDraft)
import qualified Wasp.Generator.FileDraft.TemplateFileDraft as TmplFD import qualified Wasp.Generator.FileDraft.TemplateFileDraft as TmplFD
@ -31,7 +30,6 @@ import Wasp.Generator.Monad (Generator, GeneratorError, runGenerator)
import Wasp.Generator.Templates (TemplatesDir, compileAndRenderTemplate) import Wasp.Generator.Templates (TemplatesDir, compileAndRenderTemplate)
import Wasp.Node.Version (latestMajorNodeVersion) import Wasp.Node.Version (latestMajorNodeVersion)
import qualified Wasp.SemanticVersion as SV import qualified Wasp.SemanticVersion as SV
import Wasp.Util (getEnvVarDefinition)
genDockerFiles :: AppSpec -> Generator [FileDraft] genDockerFiles :: AppSpec -> Generator [FileDraft]
genDockerFiles spec = sequence [genDockerfile spec, genDockerignore spec] genDockerFiles spec = sequence [genDockerfile spec, genDockerignore spec]
@ -47,7 +45,6 @@ genDockerfile spec = do
( Just $ ( Just $
object object
[ "usingPrisma" .= not (null $ AS.getDecls @AS.Entity.Entity spec), [ "usingPrisma" .= not (null $ AS.getDecls @AS.Entity.Entity spec),
"serverPrismaClientOutputDirEnv" .= getEnvVarDefinition serverPrismaClientOutputDirEnv,
"dbSchemaFileFromServerDir" .= SP.fromRelFile dbSchemaFileFromServerDir, "dbSchemaFileFromServerDir" .= SP.fromRelFile dbSchemaFileFromServerDir,
"nodeMajorVersion" .= show (SV.major latestMajorNodeVersion), "nodeMajorVersion" .= show (SV.major latestMajorNodeVersion),
"userDockerfile" .= fromMaybe "" (AS.userDockerfileContents spec) "userDockerfile" .= fromMaybe "" (AS.userDockerfileContents spec)

View File

@ -45,7 +45,6 @@ import Wasp.Env (envVarsToDotEnvContent)
import Wasp.Generator.Common import Wasp.Generator.Common
( ServerRootDir, ( ServerRootDir,
makeJsonWithEntityData, makeJsonWithEntityData,
prismaVersion,
) )
import Wasp.Generator.FileDraft (FileDraft, createTextFileDraft) import Wasp.Generator.FileDraft (FileDraft, createTextFileDraft)
import Wasp.Generator.Monad (Generator) import Wasp.Generator.Monad (Generator)
@ -152,7 +151,6 @@ npmDepsForWasp spec =
("cors", "^2.8.5"), ("cors", "^2.8.5"),
("express", "~4.18.1"), ("express", "~4.18.1"),
("morgan", "~1.10.0"), ("morgan", "~1.10.0"),
("@prisma/client", show prismaVersion),
("jsonwebtoken", "^8.5.1"), ("jsonwebtoken", "^8.5.1"),
-- NOTE: secure-password has a package.json override for sodium-native. -- NOTE: secure-password has a package.json override for sodium-native.
("secure-password", "^4.0.0"), ("secure-password", "^4.0.0"),
@ -172,7 +170,6 @@ npmDepsForWasp spec =
AS.Dependency.fromList AS.Dependency.fromList
[ ("nodemon", "^2.0.19"), [ ("nodemon", "^2.0.19"),
("standard", "^17.0.0"), ("standard", "^17.0.0"),
("prisma", show prismaVersion),
-- TODO: Allow users to choose whether they want to use TypeScript -- TODO: Allow users to choose whether they want to use TypeScript
-- in their projects and install these dependencies accordingly. -- in their projects and install these dependencies accordingly.
("typescript", "^5.1.0"), ("typescript", "^5.1.0"),