From 11803707269d10054aaedc4b3db1f1a044196824 Mon Sep 17 00:00:00 2001 From: Mihovil Ilakovac Date: Tue, 30 Jan 2024 20:05:31 +0100 Subject: [PATCH] Implement new wasp/server/jobs API --- .../data/Generator/templates/sdk/package.json | 9 +- .../sdk/{ => server}/jobs/_jobTypes.ts | 4 +- .../templates/sdk/server/jobs/index.ts | 5 + .../sdk/{ => server}/jobs/pgBoss/types.ts | 1 + .../server/src/jobs/core/pgBoss/pgBossJob.ts | 2 +- .../.wasp/out/sdk/wasp/package.json | 104 ------------------ .../todo-typescript/src/jobs/print.ts | 9 +- waspc/src/Wasp/Generator/SdkGenerator.hs | 6 +- .../SdkGenerator/{ => Server}/JobGenerator.hs | 50 ++++++--- .../Generator/ServerGenerator/JobGenerator.hs | 2 +- waspc/waspc.cabal | 2 +- 11 files changed, 59 insertions(+), 135 deletions(-) rename waspc/data/Generator/templates/sdk/{ => server}/jobs/_jobTypes.ts (82%) create mode 100644 waspc/data/Generator/templates/sdk/server/jobs/index.ts rename waspc/data/Generator/templates/sdk/{ => server}/jobs/pgBoss/types.ts (95%) delete mode 100644 waspc/examples/todo-typescript/.wasp/out/sdk/wasp/package.json rename waspc/src/Wasp/Generator/SdkGenerator/{ => Server}/JobGenerator.hs (54%) diff --git a/waspc/data/Generator/templates/sdk/package.json b/waspc/data/Generator/templates/sdk/package.json index bf9db83f49..c7477e7a26 100644 --- a/waspc/data/Generator/templates/sdk/package.json +++ b/waspc/data/Generator/templates/sdk/package.json @@ -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. =} @@ -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 diff --git a/waspc/data/Generator/templates/sdk/jobs/_jobTypes.ts b/waspc/data/Generator/templates/sdk/server/jobs/_jobTypes.ts similarity index 82% rename from waspc/data/Generator/templates/sdk/jobs/_jobTypes.ts rename to waspc/data/Generator/templates/sdk/server/jobs/_jobTypes.ts index 6c601a650e..87660de6f1 100644 --- a/waspc/data/Generator/templates/sdk/jobs/_jobTypes.ts +++ b/waspc/data/Generator/templates/sdk/server/jobs/_jobTypes.ts @@ -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 =} = JobFn diff --git a/waspc/data/Generator/templates/sdk/server/jobs/index.ts b/waspc/data/Generator/templates/sdk/server/jobs/index.ts new file mode 100644 index 0000000000..296811e957 --- /dev/null +++ b/waspc/data/Generator/templates/sdk/server/jobs/index.ts @@ -0,0 +1,5 @@ +{{={= =}=}} + +{=# jobs =} +export type { {= typeName =} } from './{= jobName =}' +{=/ jobs =} diff --git a/waspc/data/Generator/templates/sdk/jobs/pgBoss/types.ts b/waspc/data/Generator/templates/sdk/server/jobs/pgBoss/types.ts similarity index 95% rename from waspc/data/Generator/templates/sdk/jobs/pgBoss/types.ts rename to waspc/data/Generator/templates/sdk/server/jobs/pgBoss/types.ts index 3e9e685bc5..4ebb34b61b 100644 --- a/waspc/data/Generator/templates/sdk/jobs/pgBoss/types.ts +++ b/waspc/data/Generator/templates/sdk/server/jobs/pgBoss/types.ts @@ -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, diff --git a/waspc/data/Generator/templates/server/src/jobs/core/pgBoss/pgBossJob.ts b/waspc/data/Generator/templates/server/src/jobs/core/pgBoss/pgBossJob.ts index 34b36ae470..c4742bbb97 100644 --- a/waspc/data/Generator/templates/server/src/jobs/core/pgBoss/pgBossJob.ts +++ b/waspc/data/Generator/templates/server/src/jobs/core/pgBoss/pgBossJob.ts @@ -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') diff --git a/waspc/examples/todo-typescript/.wasp/out/sdk/wasp/package.json b/waspc/examples/todo-typescript/.wasp/out/sdk/wasp/package.json deleted file mode 100644 index 10857f880c..0000000000 --- a/waspc/examples/todo-typescript/.wasp/out/sdk/wasp/package.json +++ /dev/null @@ -1,104 +0,0 @@ -{ - "name": "wasp", - "version": "1.0.0", - "private": true, - "type": "module", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "types": "tsc --declaration --emitDeclarationOnly --stripInternal --declarationDir dist" - }, - "exports": { - "./core/config": "./dist/core/config.js", - "./core/stitches.config": "./dist/core/stitches.config.js", - "./core/storage": "./dist/core/storage.js", - "./core/auth": "./dist/core/auth.js", - "./rpc": "./dist/rpc/index.js", - "./rpc/queries": "./dist/rpc/queries/index.js", - "./rpc/queries/core": "./dist/rpc/queries/core.js", - "./rpc/actions": "./dist/rpc/actions/index.js", - "./rpc/actions/core": "./dist/rpc/actions/core.js", - "./rpc/queryClient": "./dist/rpc/queryClient.js", - "./types": "./dist/types/index.js", - "./auth/helpers/user": "./dist/auth/helpers/user.js", - "./auth/session": "./dist/auth/session.js", - "./auth/providers/types": "./dist/auth/providers/types.js", - "./auth/utils": "./dist/auth/utils.js", - "./auth/password": "./dist/auth/password.js", - "./auth/jwt": "./dist/auth/jwt.js", - "./auth/validation": "./dist/auth/validation.js", - "./auth/pages/createAuthRequiredPage": "./dist/auth/pages/createAuthRequiredPage.jsx", - "./api/events": "./dist/api/events.js", - "./operations": "./dist/operations/index.js", - "./ext-src/*": "./dist/ext-src/*.js", - "./operations/*": "./dist/operations/*", - "./universal/url": "./dist/universal/url.js", - "./universal/types": "./dist/universal/types.js", - "./universal/validators": "./dist/universal/validators.js", - "./server/utils": "./dist/server/utils.js", - "./server/actions": "./dist/server/actions/index.js", - "./server/queries": "./dist/server/queries/index.js", - "./server/auth/email": "./dist/server/auth/email/index.js", - "./test": "./dist/test/index.js", - "./test/*": "./dist/test/*.js", - "./server/crud/*": "./dist/server/crud/*", - "./server/email/core/types": "./dist/server/email/core/types.js", - "./server/auth/email/utils": "./dist/server/auth/email/utils.js", - "./jobs/*": "./dist/jobs/*.js", - "./jobs/pgBoss/types": "./dist/jobs/pgBoss/types.js", - "./router": "./dist/router/index.js", - "./server/webSocket": "./dist/server/webSocket/index.js", - "./webSocket": "./dist/webSocket/index.js", - "./webSocket/WebSocketProvider": "./dist/webSocket/WebSocketProvider.jsx", - - "./server/types": "./dist/server/types/index.js", - "./server/middleware": "./dist/server/middleware/index.js", - - "./server": "./dist/server/index.js", - "./server/api": "./dist/server/api/index.js", - "./client/api": "./dist/api/index.js", - "./auth": "./dist/auth/index.js", - "./client/auth": "./dist/client/auth/index.js", - "./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" - }, - "typesVersions": { - "*": { - "client/api": ["api/index.ts"] - } - }, - "license": "ISC", - "include": [ - "src/**/*" - ], - "dependencies": {"@prisma/client": "4.16.2", - "prisma": "4.16.2", - "@tanstack/react-query": "^4.29.0", - "axios": "^1.4.0", - "express": "~4.18.1", - "jsonwebtoken": "^8.5.1", - "mitt": "3.0.0", - "react": "^18.2.0", - "lodash.merge": "^4.6.2", - "react-router-dom": "^5.3.3", - "react-hook-form": "^7.45.4", - "secure-password": "^4.0.0", - "superjson": "^1.12.2", - "@types/express-serve-static-core": "^4.17.13", - "@stitches/react": "^1.2.8", - "lucia": "^3.0.0-beta.14", - "@lucia-auth/adapter-prisma": "^4.0.0-beta.9", - "socket.io": "^4.6.1", - "socket.io-client": "^4.6.1", - "@socket.io/component-emitter": "^4.0.0", - "vitest": "^1.2.1", - "@vitest/ui": "^1.2.1", - "jsdom": "^21.1.1", - "@testing-library/react": "^14.1.2", - "@testing-library/jest-dom": "^6.3.0", - "msw": "^1.1.0" -}, - "devDependencies": {"@tsconfig/node18": "latest" -} -} diff --git a/waspc/examples/todo-typescript/src/jobs/print.ts b/waspc/examples/todo-typescript/src/jobs/print.ts index a98941b9f3..4b9d79145b 100644 --- a/waspc/examples/todo-typescript/src/jobs/print.ts +++ b/waspc/examples/todo-typescript/src/jobs/print.ts @@ -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) +} diff --git a/waspc/src/Wasp/Generator/SdkGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator.hs index 3bd94f8d2e..d0556bb7cc 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator.hs @@ -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) @@ -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, @@ -108,7 +108,6 @@ genSdkReal spec = <++> genExternalCodeDir (AS.externalCodeFiles spec) <++> genEntitiesAndServerTypesDirs spec <++> genCrud spec - <++> genJobTypes spec <++> genServerApi spec <++> genWebSockets spec <++> genRouter spec @@ -120,6 +119,7 @@ genSdkReal spec = <++> genNewServerCrudApi spec <++> genNewClientCrudApi spec <++> genNewEmailSenderApi spec + <++> genNewJobsApi spec where genFileCopy = return . C.mkTmplFd diff --git a/waspc/src/Wasp/Generator/SdkGenerator/JobGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator/Server/JobGenerator.hs similarity index 54% rename from waspc/src/Wasp/Generator/SdkGenerator/JobGenerator.hs rename to waspc/src/Wasp/Generator/SdkGenerator/Server/JobGenerator.hs index 96ea4ae074..48699c9495 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator/JobGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator/Server/JobGenerator.hs @@ -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) @@ -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, @@ -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|] diff --git a/waspc/src/Wasp/Generator/ServerGenerator/JobGenerator.hs b/waspc/src/Wasp/Generator/ServerGenerator/JobGenerator.hs index 17de8feedc..3b98ba177f 100644 --- a/waspc/src/Wasp/Generator/ServerGenerator/JobGenerator.hs +++ b/waspc/src/Wasp/Generator/ServerGenerator/JobGenerator.hs @@ -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, diff --git a/waspc/waspc.cabal b/waspc/waspc.cabal index a15a13f4dc..03063900a8 100644 --- a/waspc/waspc.cabal +++ b/waspc/waspc.cabal @@ -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