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

Implement new wasp/server/jobs API #1702

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@
"./server/email/core/types": "./dist/server/email/core/types.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/auth/email/utils": "./dist/server/auth/email/utils.js",
{=! Parts are used by users and documented (types), other parts are used by the framework code (entities). =}
"./jobs/*": "./dist/jobs/*.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/jobs/*": "./dist/server/jobs/*.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
{=! Todo(filip): This export becomes problematic once we start supporting different executors =}
"./jobs/pgBoss/types": "./dist/jobs/pgBoss/types.js",
"./server/jobs/pgBoss/types": "./dist/server/jobs/pgBoss/types.js",
{=! Used by users, documented. =}
"./router": "./dist/router/index.js",
{=! Used by users, documented. =}
Expand Down Expand Up @@ -118,7 +118,8 @@
"./server/auth": "./dist/server/auth/index.js",
"./server/crud": "./dist/server/crud/index.js",
"./client/crud": "./dist/client/crud/index.js",
"./server/email": "./dist/server/email/index.js"
"./server/email": "./dist/server/email/index.js",
"./server/jobs": "./dist/server/jobs/index.js"
},
{=!
TypeScript doesn't care about the redirects we define above in "exports" field; those
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { prisma } from 'wasp/server'
import type { JSONValue, JSONObject } from 'wasp/server/_types/serialization'
import { type JobFn } from '{= jobExecutorTypesImportPath =}'

{=! Used in framework code, shouldn't be public =}
// PRIVATE API
export const entities = {
{=# entities =}
{= name =}: prisma.{= prismaIdentifier =},
{=/ entities =}
};

{=! Used by users, should be public =}
// PUBLIC API
export type {= typeName =}<Input extends JSONObject, Output extends JSONValue | void> = JobFn<Input, Output, typeof entities>
5 changes: 5 additions & 0 deletions waspc/data/Generator/templates/sdk/server/jobs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{={= =}=}}

{=# jobs =}
export type { {= typeName =} } from './{= jobName =}'
{=/ jobs =}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PrismaDelegate } from 'wasp/server/_types'
import type { JSONValue, JSONObject } from 'wasp/server/_types/serialization'

// PRIVATE API
export type JobFn<
Input extends JSONObject,
Output extends JSONValue | void,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { pgBossStarted } from './pgBoss.js'
import { Job, SubmittedJob } from '../job.js'
import type { JSONValue, JSONObject } from 'wasp/server/_types/serialization'
import { PrismaDelegate } from 'wasp/server/_types'
import type { JobFn } from 'wasp/jobs/pgBoss/types'
import type { JobFn } from 'wasp/server/jobs/pgBoss/types'

export const PG_BOSS_EXECUTOR_NAME = Symbol('PgBoss')

Expand Down
104 changes: 0 additions & 104 deletions waspc/examples/todo-typescript/.wasp/out/sdk/wasp/package.json

This file was deleted.

9 changes: 4 additions & 5 deletions waspc/examples/todo-typescript/src/jobs/print.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { PrintTimeAndNumberOfTasks } from "wasp/jobs/PrintTimeAndNumberOfTasks";

import { PrintTimeAndNumberOfTasks } from 'wasp/server/jobs'
export const printTimeAndNumberOfTasks: PrintTimeAndNumberOfTasks<
{},
void
> = async (data, context) => {
const count = await context.entities.Task.count();
console.log(Date.now(), "Number of tasks:", count);
};
const count = await context.entities.Task.count()
console.log(Date.now(), 'Number of tasks:', count)
}
6 changes: 3 additions & 3 deletions waspc/src/Wasp/Generator/SdkGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ import Wasp.Generator.SdkGenerator.Client.AuthG (genNewClientAuth)
import Wasp.Generator.SdkGenerator.Client.CrudG (genNewClientCrudApi)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Generator.SdkGenerator.CrudG (genCrud)
import Wasp.Generator.SdkGenerator.JobGenerator (genJobTypes)
import Wasp.Generator.SdkGenerator.RouterGenerator (genRouter)
import Wasp.Generator.SdkGenerator.RpcGenerator (genRpc)
import Wasp.Generator.SdkGenerator.Server.AuthG (genNewServerApi)
import Wasp.Generator.SdkGenerator.Server.CrudG (genNewServerCrudApi)
import Wasp.Generator.SdkGenerator.Server.EmailSenderG (depsRequiredByEmail, genNewEmailSenderApi)
import Wasp.Generator.SdkGenerator.Server.JobGenerator (genNewJobsApi)
import Wasp.Generator.SdkGenerator.ServerApiG (genServerApi)
import Wasp.Generator.SdkGenerator.ServerOpsGenerator (genOperations)
import Wasp.Generator.SdkGenerator.WebSocketGenerator (depsRequiredByWebSockets, genWebSockets)
Expand Down Expand Up @@ -95,7 +95,7 @@ genSdkReal spec =
genFileCopy [relfile|types/index.ts|],
genFileCopy [relfile|test/vitest/helpers.tsx|],
genFileCopy [relfile|test/index.ts|],
genFileCopy [relfile|jobs/pgBoss/types.ts|],
genFileCopy [relfile|server/jobs/pgBoss/types.ts|],
genServerConfigFile spec,
genTsConfigJson,
genServerUtils spec,
Expand All @@ -108,7 +108,6 @@ genSdkReal spec =
<++> genExternalCodeDir (AS.externalCodeFiles spec)
<++> genEntitiesAndServerTypesDirs spec
<++> genCrud spec
<++> genJobTypes spec
<++> genServerApi spec
<++> genWebSockets spec
<++> genRouter spec
Expand All @@ -120,6 +119,7 @@ genSdkReal spec =
<++> genNewServerCrudApi spec
<++> genNewClientCrudApi spec
<++> genNewEmailSenderApi spec
<++> genNewJobsApi spec
where
genFileCopy = return . C.mkTmplFd

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module Wasp.Generator.SdkGenerator.JobGenerator (genJobTypes, getImportPathForJobName, getJobExecutorTypesImportPath) where
module Wasp.Generator.SdkGenerator.Server.JobGenerator
( genNewJobsApi,
getImportPathForJobName,
getJobExecutorTypesImportPath,
)
where

import Data.Aeson (object, (.=))
import Data.Maybe (fromJust)
Expand All @@ -16,20 +21,37 @@ import Wasp.Generator.SdkGenerator.Common (makeSdkImportPath)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Util

genJobTypes :: AppSpec -> Generator [FileDraft]
genJobTypes spec = case getJobs spec of
[] -> return []
jobs -> return $ map genJobType jobs
genNewJobsApi :: AppSpec -> Generator [FileDraft]
genNewJobsApi spec =
case getJobs spec of
[] -> return []
jobs ->
sequence
[ genIndexTs jobs
]
<++> mapM genJobType jobs

genIndexTs :: [(String, Job)] -> Generator FileDraft
genIndexTs jobs = return $ C.mkTmplFdWithData tmplFile tmplData
where
tmplFile = [relfile|server/jobs/index.ts|]
tmplData = object ["jobs" .= map getJobTmplData jobs]
getJobTmplData (jobName, _) =
object
[ "typeName" .= toUpperFirst jobName,
"jobName" .= jobName
]

genJobType :: (String, Job) -> FileDraft
genJobType :: (String, Job) -> Generator FileDraft
genJobType (jobName, job) =
C.mkTmplFdWithDstAndData
tmplFile
dstFile
$ Just tmplData
return $
C.mkTmplFdWithDstAndData
tmplFile
dstFile
$ Just tmplData
where
tmplFile = [relfile|jobs/_jobTypes.ts|]
dstFile = [reldir|jobs|] </> fromJust (SP.parseRelFile $ jobName ++ ".ts")
tmplFile = [relfile|server/jobs/_jobTypes.ts|]
dstFile = [reldir|server/jobs|] </> fromJust (SP.parseRelFile $ jobName ++ ".ts")
tmplData =
object
[ "typeName" .= toUpperFirst jobName,
Expand All @@ -40,9 +62,9 @@ genJobType (jobName, job) =
jobExecutorTypesImportPath = getJobExecutorTypesImportPath (J.executor job)

getImportPathForJobName :: String -> Path Posix (Rel d) File'
getImportPathForJobName jobName = makeSdkImportPath $ [reldirP|jobs|] </> fromJust (SP.parseRelFileP jobName)
getImportPathForJobName jobName = makeSdkImportPath $ [reldirP|server/jobs|] </> fromJust (SP.parseRelFileP jobName)

-- | We are importing relevant types per executor e.g. JobFn, this functions maps
-- the executor to the import path of the relevant types.
getJobExecutorTypesImportPath :: JobExecutor -> Path Posix (Rel r) File'
getJobExecutorTypesImportPath PgBoss = makeSdkImportPath [relfileP|jobs/pgBoss/types|]
getJobExecutorTypesImportPath PgBoss = makeSdkImportPath [relfileP|server/jobs/pgBoss/types|]
2 changes: 1 addition & 1 deletion waspc/src/Wasp/Generator/ServerGenerator/JobGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import Wasp.AppSpec.Util (isPgBossJobExecutorUsed)
import Wasp.Generator.Common (ServerRootDir)
import Wasp.Generator.FileDraft (FileDraft)
import Wasp.Generator.Monad (Generator)
import Wasp.Generator.SdkGenerator.JobGenerator (getImportPathForJobName, getJobExecutorTypesImportPath)
import Wasp.Generator.SdkGenerator.Server.JobGenerator (getImportPathForJobName, getJobExecutorTypesImportPath)
import Wasp.Generator.ServerGenerator.Common
( ServerTemplatesDir,
srcDirInServerTemplatesDir,
Expand Down
2 changes: 1 addition & 1 deletion waspc/waspc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,12 @@ library
Wasp.Generator.SdkGenerator.Common
Wasp.Generator.SdkGenerator.CrudG
Wasp.Generator.SdkGenerator.EmailSender.Providers
Wasp.Generator.SdkGenerator.JobGenerator
Wasp.Generator.SdkGenerator.RouterGenerator
Wasp.Generator.SdkGenerator.RpcGenerator
Wasp.Generator.SdkGenerator.Server.AuthG
Wasp.Generator.SdkGenerator.Server.CrudG
Wasp.Generator.SdkGenerator.Server.EmailSenderG
Wasp.Generator.SdkGenerator.Server.JobGenerator
Wasp.Generator.SdkGenerator.ServerApiG
Wasp.Generator.SdkGenerator.ServerOpsGenerator
Wasp.Generator.SdkGenerator.WebSocketGenerator
Expand Down
Loading