mirror of
https://github.com/wasp-lang/wasp.git
synced 2024-11-23 19:29:17 +03:00
Implemented generation of prisma.schema. (#33)
This commit is contained in:
parent
bc5caef944
commit
06bb7b97a8
17
waspc/data/Generator/templates/db/prisma.schema
Normal file
17
waspc/data/Generator/templates/db/prisma.schema
Normal file
@ -0,0 +1,17 @@
|
||||
{{={= =}=}}
|
||||
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = "file:./dev.db"
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
{=# entities =}
|
||||
model {= name =} {
|
||||
{= pslModelSchema =}
|
||||
}
|
||||
|
||||
{=/ entities =}
|
@ -2,6 +2,19 @@ app todoApp {
|
||||
title: "ToDo App"
|
||||
}
|
||||
|
||||
entityPSL Project {=psl
|
||||
id Int @id @default(autoincrement())
|
||||
tasks Tasks[]
|
||||
psl=}
|
||||
|
||||
entityPSL Task {=psl
|
||||
id Int @id @default(autoincrement())
|
||||
description String
|
||||
isDone Boolean @default(false)
|
||||
project Project @relation(fields: [projectId], references: [id])
|
||||
projectId Int
|
||||
psl=}
|
||||
|
||||
route "/" -> page Main
|
||||
page Main {
|
||||
component: import Main from "@ext/pages/Main"
|
||||
@ -30,4 +43,4 @@ query getTasks {
|
||||
|
||||
action createTask {
|
||||
fn: import { createTask } from "@ext/actions.js"
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import CompileOptions (CompileOptions)
|
||||
import Wasp (Wasp)
|
||||
import Generator.WebAppGenerator (generateWebApp)
|
||||
import Generator.ServerGenerator (genServer)
|
||||
import Generator.DbGenerator (genDb)
|
||||
import Generator.FileDraft (FileDraft, write)
|
||||
import Generator.Common (ProjectRootDir)
|
||||
|
||||
@ -29,6 +30,7 @@ writeWebAppCode :: Wasp -> Path Abs (Dir ProjectRootDir) -> CompileOptions -> IO
|
||||
writeWebAppCode wasp dstDir compileOptions = do
|
||||
writeFileDrafts dstDir (generateWebApp wasp compileOptions)
|
||||
writeFileDrafts dstDir (genServer wasp compileOptions)
|
||||
writeFileDrafts dstDir (genDb wasp compileOptions)
|
||||
writeDotWaspInfo dstDir
|
||||
|
||||
-- | Writes file drafts while using given destination dir as root dir.
|
||||
|
48
waspc/src/Generator/DbGenerator.hs
Normal file
48
waspc/src/Generator/DbGenerator.hs
Normal file
@ -0,0 +1,48 @@
|
||||
module Generator.DbGenerator
|
||||
( genDb
|
||||
) where
|
||||
|
||||
import qualified Path as P
|
||||
import Data.Aeson ((.=), object)
|
||||
|
||||
import StrongPath (Path, Rel, File, Dir, (</>))
|
||||
import qualified StrongPath as SP
|
||||
import Wasp (Wasp)
|
||||
import qualified Wasp
|
||||
import Wasp.EntityPSL (EntityPSL)
|
||||
import CompileOptions (CompileOptions)
|
||||
import Generator.FileDraft (FileDraft, createTemplateFileDraft)
|
||||
import Generator.Common (ProjectRootDir)
|
||||
import Generator.Templates (TemplatesDir)
|
||||
|
||||
data DbRootDir
|
||||
data DbTemplatesDir
|
||||
|
||||
dbRootDirInProjectRootDir :: Path (Rel ProjectRootDir) (Dir DbRootDir)
|
||||
dbRootDirInProjectRootDir = SP.fromPathRelDir [P.reldir|db|]
|
||||
|
||||
dbTemplatesDirInTemplatesDir :: Path (Rel TemplatesDir) (Dir DbTemplatesDir)
|
||||
dbTemplatesDirInTemplatesDir = SP.fromPathRelDir [P.reldir|db|]
|
||||
|
||||
genDb :: Wasp -> CompileOptions -> [FileDraft]
|
||||
genDb wasp _ =
|
||||
[ genPrismaSchema $ Wasp.getPSLEntities wasp
|
||||
]
|
||||
|
||||
genPrismaSchema :: [EntityPSL] -> FileDraft
|
||||
genPrismaSchema entities = createTemplateFileDraft dstPath tmplSrcPath (Just templateData)
|
||||
where
|
||||
relSrcPath :: Path (Rel DbTemplatesDir) File
|
||||
relSrcPath = (SP.fromPathRelFile [P.relfile|prisma.schema|])
|
||||
|
||||
relDstPath :: Path (Rel DbRootDir) File
|
||||
-- Generated schema file will be in the same relative location as the
|
||||
-- template file within templates dir.
|
||||
relDstPath = SP.castRel relSrcPath
|
||||
|
||||
dstPath = dbRootDirInProjectRootDir </> relDstPath
|
||||
tmplSrcPath = dbTemplatesDirInTemplatesDir </> relSrcPath
|
||||
|
||||
templateData = object
|
||||
[ "entities" .= entities
|
||||
]
|
@ -66,7 +66,11 @@ copyTmplAsIs path = makeTemplateFD path (SP.castRel path) Nothing
|
||||
makeSimpleTemplateFD :: Path (Rel WebAppTemplatesDir) File -> Wasp -> FileDraft
|
||||
makeSimpleTemplateFD path wasp = makeTemplateFD path (SP.castRel path) (Just $ Aeson.toJSON wasp)
|
||||
|
||||
makeTemplateFD :: Path (Rel WebAppTemplatesDir) File -> Path (Rel WebAppRootDir) File -> Maybe Aeson.Value -> FileDraft
|
||||
makeTemplateFD
|
||||
:: Path (Rel WebAppTemplatesDir) File
|
||||
-> Path (Rel WebAppRootDir) File
|
||||
-> Maybe Aeson.Value
|
||||
-> FileDraft
|
||||
makeTemplateFD srcPathInWebAppTemplatesDir dstPathInWebAppRootDir tmplData =
|
||||
createTemplateFileDraft
|
||||
(webAppRootDirInProjectRootDir </> dstPathInWebAppRootDir)
|
||||
|
@ -65,8 +65,12 @@ reservedNames =
|
||||
, reservedNameApp
|
||||
, reservedNamePage
|
||||
, reservedNameRoute
|
||||
, reservedNameEntityPSL
|
||||
|
||||
-- TODO(matija): old entity stuff, to be removed.
|
||||
, reservedNameEntity
|
||||
, reservedNameEntityForm
|
||||
|
||||
, reservedNameQuery
|
||||
, reservedNameAction
|
||||
-- * Data types
|
||||
|
@ -12,6 +12,9 @@ module Wasp
|
||||
, getApp
|
||||
, setApp
|
||||
|
||||
, getPSLEntities
|
||||
|
||||
-- TODO(matija): Old Entity stuff, to be removed.
|
||||
, module Wasp.Entity
|
||||
, getEntities
|
||||
, addEntity
|
||||
@ -164,6 +167,11 @@ getActionByName wasp name = U.headSafe $ filter (\a -> Wasp.Action._name a == na
|
||||
|
||||
-- * Entities
|
||||
|
||||
getPSLEntities :: Wasp -> [Wasp.EntityPSL.EntityPSL]
|
||||
getPSLEntities wasp = [entityPSL | (WaspElementEntityPSL entityPSL) <- (waspElements wasp)]
|
||||
|
||||
|
||||
-- TODO(matija): old Entity stuff, to be removed
|
||||
getEntities :: Wasp -> [Entity]
|
||||
getEntities wasp = [entity | (WaspElementEntity entity) <- (waspElements wasp)]
|
||||
|
||||
|
@ -5,8 +5,15 @@ module Wasp.EntityPSL
|
||||
) where
|
||||
|
||||
import Data.Text (Text)
|
||||
import Data.Aeson (ToJSON(..), (.=), object)
|
||||
|
||||
data EntityPSL = EntityPSL
|
||||
{ _name :: !String
|
||||
, _pslModelSchema :: !Text -- ^ PSL stands for Prisma Schema Language.
|
||||
} deriving (Show, Eq)
|
||||
|
||||
instance ToJSON EntityPSL where
|
||||
toJSON entity = object
|
||||
[ "name" .= _name entity
|
||||
, "pslModelSchema" .= _pslModelSchema entity
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user