From 86a4035a85dc7901533453bc9805ddd5d1bfb072 Mon Sep 17 00:00:00 2001 From: Martin Sosic Date: Sat, 27 Jan 2024 14:34:40 +0100 Subject: [PATCH] [New SDK]: 'wasp/server'. --- .../data/Generator/templates/sdk/auth/jwt.ts | 2 +- .../Generator/templates/sdk/auth/lucia.ts | 2 +- .../Generator/templates/sdk/auth/session.ts | 2 +- .../Generator/templates/sdk/auth/utils.ts | 2 +- .../Generator/templates/sdk/dbSeed/types.ts | 2 +- .../Generator/templates/sdk/jobs/_jobTypes.ts | 2 +- .../data/Generator/templates/sdk/package.json | 12 +++++----- .../templates/sdk/server/_types/index.ts | 2 +- .../templates/sdk/server/actions/index.ts | 2 +- .../templates/sdk/server/auth/email/utils.ts | 2 +- .../Generator/templates/sdk/server/config.ts | 1 + .../templates/sdk/server/dbClient.ts | 5 +++-- .../Generator/templates/sdk/server/index.ts | 7 ++++++ .../templates/sdk/server/queries/index.ts | 2 +- .../templates/sdk/server/types/index.ts | 5 ++--- .../templates/sdk/server/webSocket/index.ts | 2 +- .../templates/server/src/actions/_action.ts | 2 +- .../src/auth/providers/oauth/createRouter.ts | 3 +-- .../server/src/auth/providers/oauth/init.ts | 2 +- .../templates/server/src/auth/utils.ts | 2 +- .../templates/server/src/crud/_operations.ts | 2 +- .../Generator/templates/server/src/dbSeed.ts | 8 +++---- .../server/src/jobs/core/pgBoss/pgBoss.ts | 2 +- .../server/src/middleware/globalMiddleware.ts | 2 +- .../templates/server/src/queries/_query.ts | 2 +- .../templates/server/src/routes/apis/index.ts | 2 +- .../Generator/templates/server/src/server.ts | 5 +++-- .../server/src/webSocket/initialization.ts | 3 +-- .../todo-typescript/src/setup/serverSetup.ts | 8 +++---- .../src/user/customEmailSending.ts | 18 ++++++++------- waspc/src/Wasp/Generator/SdkGenerator.hs | 1 + web/docs/data-model/backends.md | 22 +++++++++---------- web/docs/data-model/entities.md | 8 +++---- web/docs/project/server-config.md | 3 ++- 34 files changed, 78 insertions(+), 69 deletions(-) create mode 100644 waspc/data/Generator/templates/sdk/server/index.ts diff --git a/waspc/data/Generator/templates/sdk/auth/jwt.ts b/waspc/data/Generator/templates/sdk/auth/jwt.ts index b244990158..cb7f33725e 100644 --- a/waspc/data/Generator/templates/sdk/auth/jwt.ts +++ b/waspc/data/Generator/templates/sdk/auth/jwt.ts @@ -1,7 +1,7 @@ import jwt from 'jsonwebtoken' import util from 'util' -import config from 'wasp/server/config' +import { config } from 'wasp/server' const jwtSign = util.promisify(jwt.sign) const jwtVerify = util.promisify(jwt.verify) diff --git a/waspc/data/Generator/templates/sdk/auth/lucia.ts b/waspc/data/Generator/templates/sdk/auth/lucia.ts index 9d53af0a1c..eabf63523e 100644 --- a/waspc/data/Generator/templates/sdk/auth/lucia.ts +++ b/waspc/data/Generator/templates/sdk/auth/lucia.ts @@ -1,7 +1,7 @@ {{={= =}=}} import { Lucia } from "lucia"; import { PrismaAdapter } from "@lucia-auth/adapter-prisma"; -import prisma from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' import { type {= userEntityUpper =} } from "wasp/entities" const prismaAdapter = new PrismaAdapter( diff --git a/waspc/data/Generator/templates/sdk/auth/session.ts b/waspc/data/Generator/templates/sdk/auth/session.ts index b7ddebc3ea..487dca2046 100644 --- a/waspc/data/Generator/templates/sdk/auth/session.ts +++ b/waspc/data/Generator/templates/sdk/auth/session.ts @@ -11,7 +11,7 @@ import { deserializeAndSanitizeProviderData, } from "./utils.js"; -import prisma from 'wasp/server/dbClient'; +import { prisma } from 'wasp/server'; // Creates a new session for the `authId` in the database export async function createSession(authId: string): Promise { diff --git a/waspc/data/Generator/templates/sdk/auth/utils.ts b/waspc/data/Generator/templates/sdk/auth/utils.ts index 08b7798840..ca9f60628f 100644 --- a/waspc/data/Generator/templates/sdk/auth/utils.ts +++ b/waspc/data/Generator/templates/sdk/auth/utils.ts @@ -3,7 +3,7 @@ import { hashPassword } from './password.js' import { verify } from './jwt.js' import AuthError from 'wasp/core/AuthError' import HttpError from 'wasp/core/HttpError' -import prisma from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' import { sleep } from 'wasp/server/utils' import { type {= userEntityUpper =}, diff --git a/waspc/data/Generator/templates/sdk/dbSeed/types.ts b/waspc/data/Generator/templates/sdk/dbSeed/types.ts index fe0d5396a4..7a72bca7ef 100644 --- a/waspc/data/Generator/templates/sdk/dbSeed/types.ts +++ b/waspc/data/Generator/templates/sdk/dbSeed/types.ts @@ -1,3 +1,3 @@ import type { PrismaClient } from '@prisma/client' -export type DbSeedFn = (prismaClient: PrismaClient) => Promise +export type DbSeedFn = (prisma: PrismaClient) => Promise diff --git a/waspc/data/Generator/templates/sdk/jobs/_jobTypes.ts b/waspc/data/Generator/templates/sdk/jobs/_jobTypes.ts index ce67cbfc9c..6c601a650e 100644 --- a/waspc/data/Generator/templates/sdk/jobs/_jobTypes.ts +++ b/waspc/data/Generator/templates/sdk/jobs/_jobTypes.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import prisma from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' import type { JSONValue, JSONObject } from 'wasp/server/_types/serialization' import { type JobFn } from '{= jobExecutorTypesImportPath =}' diff --git a/waspc/data/Generator/templates/sdk/package.json b/waspc/data/Generator/templates/sdk/package.json index 8fb552438c..9a98d56f13 100644 --- a/waspc/data/Generator/templates/sdk/package.json +++ b/waspc/data/Generator/templates/sdk/package.json @@ -100,12 +100,6 @@ "./universal/types": "./dist/universal/types.js", {=! Used by our code, uncodumented (but accessible) for users. =} "./universal/validators": "./dist/universal/validators.js", - {=! Used by users and by our code, documented =} - "./server/dbClient": "./dist/server/dbClient.js", - {=! Used by users and by our code, documented. =} - "./server/config": "./dist/server/config.js", - {=! Used by users and by our code, documented. =} - "./server/types": "./dist/server/types/index.js", {=! Used by users and by our code, documented. =} "./server/middleware": "./dist/server/middleware/index.js", {=! Parts are used by users, documented. Parts are probably used by our code, undocumented (but accessible). =} @@ -144,7 +138,13 @@ {=! Used by our code, uncodumented (but accessible) for users. =} "./webSocket/WebSocketProvider": "./dist/webSocket/WebSocketProvider.jsx", + {=! Still needed, reconsider during refactoring =} + "./server/types": "./dist/server/types/index.js", + {=! ================= NEW API HERE =================== =} + {=! Public: { config, prisma, type ServerSetupFn } =} + {=! Private: [] =} + "./server": "./dist/server/index.js", {=! Public: { type MyEntity1, type MyEntity2, ... } =} {=! Private: [] =} "./server/api": "./dist/server/api/index.js", diff --git a/waspc/data/Generator/templates/sdk/server/_types/index.ts b/waspc/data/Generator/templates/sdk/server/_types/index.ts index 24d5011c91..c00ecce15e 100644 --- a/waspc/data/Generator/templates/sdk/server/_types/index.ts +++ b/waspc/data/Generator/templates/sdk/server/_types/index.ts @@ -2,7 +2,7 @@ import { type Expand } from 'wasp/universal/types'; import { type Request, type Response } from 'express' import { type ParamsDictionary as ExpressParams, type Query as ExpressQuery } from 'express-serve-static-core' -import prisma from "wasp/server/dbClient" +import { prisma } from 'wasp/server' {=# isAuthEnabled =} import { type {= userEntityName =}, diff --git a/waspc/data/Generator/templates/sdk/server/actions/index.ts b/waspc/data/Generator/templates/sdk/server/actions/index.ts index c1832fd6d6..a00094e8fe 100644 --- a/waspc/data/Generator/templates/sdk/server/actions/index.ts +++ b/waspc/data/Generator/templates/sdk/server/actions/index.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import prisma from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' {=! TODO: This template is exactly the same at the moment as one for queries, consider in the future if it is worth removing this duplication. =} diff --git a/waspc/data/Generator/templates/sdk/server/auth/email/utils.ts b/waspc/data/Generator/templates/sdk/server/auth/email/utils.ts index 168ff61262..005adfdd67 100644 --- a/waspc/data/Generator/templates/sdk/server/auth/email/utils.ts +++ b/waspc/data/Generator/templates/sdk/server/auth/email/utils.ts @@ -9,7 +9,7 @@ import { deserializeAndSanitizeProviderData, type EmailProviderData, } from 'wasp/auth/utils'; -import waspServerConfig from 'wasp/server/config'; +import { config as waspServerConfig } from 'wasp/server'; import { type {= userEntityUpper =}, type {= authEntityUpper =} } from 'wasp/entities' export async function createEmailVerificationLink( diff --git a/waspc/data/Generator/templates/sdk/server/config.ts b/waspc/data/Generator/templates/sdk/server/config.ts index 0ee4ca96f3..fcdc0b6667 100644 --- a/waspc/data/Generator/templates/sdk/server/config.ts +++ b/waspc/data/Generator/templates/sdk/server/config.ts @@ -54,6 +54,7 @@ const config: { } const resolvedConfig: Config = merge(config.all, config[env]) +// PUBLIC API export default resolvedConfig function getDevelopmentConfig(): EnvConfig { diff --git a/waspc/data/Generator/templates/sdk/server/dbClient.ts b/waspc/data/Generator/templates/sdk/server/dbClient.ts index 66e7801be3..cbd69d2f83 100644 --- a/waspc/data/Generator/templates/sdk/server/dbClient.ts +++ b/waspc/data/Generator/templates/sdk/server/dbClient.ts @@ -2,11 +2,12 @@ import Prisma from '@prisma/client' const createDbClient = () => { - const prismaClient = new Prisma.PrismaClient() + const prisma = new Prisma.PrismaClient() - return prismaClient + return prisma } const dbClient = createDbClient() +// PUBLIC API export default dbClient diff --git a/waspc/data/Generator/templates/sdk/server/index.ts b/waspc/data/Generator/templates/sdk/server/index.ts new file mode 100644 index 0000000000..8d527a331b --- /dev/null +++ b/waspc/data/Generator/templates/sdk/server/index.ts @@ -0,0 +1,7 @@ +// PUBLIC API +export { default as config } from './config.js' +// PUBLIC API +export { default as prisma } from './dbClient.js' +// PUBLIC API +export { type ServerSetupFn } from './types/index.js' + diff --git a/waspc/data/Generator/templates/sdk/server/queries/index.ts b/waspc/data/Generator/templates/sdk/server/queries/index.ts index 92cb7b7f4f..1c8cf33b05 100644 --- a/waspc/data/Generator/templates/sdk/server/queries/index.ts +++ b/waspc/data/Generator/templates/sdk/server/queries/index.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import prisma from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' {=! TODO: This template is exactly the same at the moment as one for actions, consider in the future if it is worth removing this duplication. =} diff --git a/waspc/data/Generator/templates/sdk/server/types/index.ts b/waspc/data/Generator/templates/sdk/server/types/index.ts index fda96a4528..1bbffc7555 100644 --- a/waspc/data/Generator/templates/sdk/server/types/index.ts +++ b/waspc/data/Generator/templates/sdk/server/types/index.ts @@ -1,12 +1,11 @@ import { type Application } from 'express' import { Server } from 'http' +// PUBLIC API export type ServerSetupFn = (context: ServerSetupFnContext) => Promise +// PRIVATE API (server) export type ServerSetupFnContext = { app: Application, server: Server, } - -export type { Application } from 'express' -export type { Server } from 'http' diff --git a/waspc/data/Generator/templates/sdk/server/webSocket/index.ts b/waspc/data/Generator/templates/sdk/server/webSocket/index.ts index 1074eda872..f40a15d019 100644 --- a/waspc/data/Generator/templates/sdk/server/webSocket/index.ts +++ b/waspc/data/Generator/templates/sdk/server/webSocket/index.ts @@ -3,7 +3,7 @@ import { Server } from 'socket.io' import { EventsMap, DefaultEventsMap } from '@socket.io/component-emitter' -import prisma from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' {=# isAuthEnabled =} import { type SanitizedUser } from 'wasp/server/_types/index.js' {=/ isAuthEnabled =} diff --git a/waspc/data/Generator/templates/server/src/actions/_action.ts b/waspc/data/Generator/templates/server/src/actions/_action.ts index 11b092cb1a..54435171e0 100644 --- a/waspc/data/Generator/templates/server/src/actions/_action.ts +++ b/waspc/data/Generator/templates/server/src/actions/_action.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import prisma from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' {=& jsFn.importStatement =} diff --git a/waspc/data/Generator/templates/server/src/auth/providers/oauth/createRouter.ts b/waspc/data/Generator/templates/server/src/auth/providers/oauth/createRouter.ts index a8032b717d..38a783304a 100644 --- a/waspc/data/Generator/templates/server/src/auth/providers/oauth/createRouter.ts +++ b/waspc/data/Generator/templates/server/src/auth/providers/oauth/createRouter.ts @@ -3,8 +3,7 @@ import { Router } from "express" import passport from "passport" -import prisma from 'wasp/server/dbClient' -import waspServerConfig from 'wasp/server/config' +import { prisma, config as waspServerConfig } from 'wasp/server' import { type ProviderName, type ProviderId, diff --git a/waspc/data/Generator/templates/server/src/auth/providers/oauth/init.ts b/waspc/data/Generator/templates/server/src/auth/providers/oauth/init.ts index a104eac299..15cb5631fc 100644 --- a/waspc/data/Generator/templates/server/src/auth/providers/oauth/init.ts +++ b/waspc/data/Generator/templates/server/src/auth/providers/oauth/init.ts @@ -1,6 +1,6 @@ import passport from "passport"; -import waspServerConfig from 'wasp/server/config'; +import { config as waspServerConfig } from 'wasp/server'; import type { InitData, ProviderConfig, RequestWithWasp, UserSignupFields } from "wasp/auth/providers/types"; import type { OAuthConfig, UserDefinedConfigFn } from "./types.js"; diff --git a/waspc/data/Generator/templates/server/src/auth/utils.ts b/waspc/data/Generator/templates/server/src/auth/utils.ts index d3ede94ce6..887997b5cb 100644 --- a/waspc/data/Generator/templates/server/src/auth/utils.ts +++ b/waspc/data/Generator/templates/server/src/auth/utils.ts @@ -3,7 +3,7 @@ import { hashPassword } from 'wasp/auth/password' import { verify } from 'wasp/auth/jwt' import AuthError from 'wasp/core/AuthError' import HttpError from 'wasp/core/HttpError' -import prisma from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' import { sleep } from 'wasp/server/utils' import { type {= userEntityUpper =}, diff --git a/waspc/data/Generator/templates/server/src/crud/_operations.ts b/waspc/data/Generator/templates/server/src/crud/_operations.ts index 31237b2dc9..14f9ddda2e 100644 --- a/waspc/data/Generator/templates/server/src/crud/_operations.ts +++ b/waspc/data/Generator/templates/server/src/crud/_operations.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import prisma from "wasp/server/dbClient"; +import { prisma } from 'wasp/server'; import type { Prisma } from "@prisma/client"; import type { diff --git a/waspc/data/Generator/templates/server/src/dbSeed.ts b/waspc/data/Generator/templates/server/src/dbSeed.ts index 204480f456..51378ed8f9 100644 --- a/waspc/data/Generator/templates/server/src/dbSeed.ts +++ b/waspc/data/Generator/templates/server/src/dbSeed.ts @@ -6,7 +6,7 @@ // TODO: Consider in the future moving it into a a separate project (maybe db/ ?), while still // maintaining access to logic from the server/ . -import prismaClient from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' import type { DbSeedFn } from 'wasp/dbSeed/types' {=# dbSeeds =} @@ -26,13 +26,13 @@ async function main() { } else { console.error('Name of the seed to run not specified!') } - await (seeds[nameOfSeedToRun] satisfies DbSeedFn)(prismaClient) + await (seeds[nameOfSeedToRun] satisfies DbSeedFn)(prisma) } main() - .then(async () => { await prismaClient.$disconnect() }) + .then(async () => { await prisma.$disconnect() }) .catch(async (e) => { console.error(e) - await prismaClient.$disconnect() + await prisma.$disconnect() process.exit(1) }) diff --git a/waspc/data/Generator/templates/server/src/jobs/core/pgBoss/pgBoss.ts b/waspc/data/Generator/templates/server/src/jobs/core/pgBoss/pgBoss.ts index b0c1622f66..85c6c90e75 100644 --- a/waspc/data/Generator/templates/server/src/jobs/core/pgBoss/pgBoss.ts +++ b/waspc/data/Generator/templates/server/src/jobs/core/pgBoss/pgBoss.ts @@ -1,5 +1,5 @@ import PgBoss from 'pg-boss' -import config from 'wasp/server/config' +import { config } from 'wasp/server' const boss = createPgBoss() diff --git a/waspc/data/Generator/templates/server/src/middleware/globalMiddleware.ts b/waspc/data/Generator/templates/server/src/middleware/globalMiddleware.ts index 7f576b1353..49690a9fbe 100644 --- a/waspc/data/Generator/templates/server/src/middleware/globalMiddleware.ts +++ b/waspc/data/Generator/templates/server/src/middleware/globalMiddleware.ts @@ -5,7 +5,7 @@ import logger from 'morgan' import cors from 'cors' import helmet from 'helmet' -import config from 'wasp/server/config' +import { config } from 'wasp/server' import type { MiddlewareConfig, MiddlewareConfigFn } from 'wasp/server/middleware' export type { MiddlewareConfig, MiddlewareConfigFn } from 'wasp/server/middleware' diff --git a/waspc/data/Generator/templates/server/src/queries/_query.ts b/waspc/data/Generator/templates/server/src/queries/_query.ts index 24118fd432..0b60b99a42 100644 --- a/waspc/data/Generator/templates/server/src/queries/_query.ts +++ b/waspc/data/Generator/templates/server/src/queries/_query.ts @@ -1,5 +1,5 @@ {{={= =}=}} -import prisma from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' {=& jsFn.importStatement =} diff --git a/waspc/data/Generator/templates/server/src/routes/apis/index.ts b/waspc/data/Generator/templates/server/src/routes/apis/index.ts index 3e87cd3643..6a1b1dcd00 100644 --- a/waspc/data/Generator/templates/server/src/routes/apis/index.ts +++ b/waspc/data/Generator/templates/server/src/routes/apis/index.ts @@ -1,6 +1,6 @@ {{={= =}=}} import express from 'express' -import prisma from 'wasp/server/dbClient' +import { prisma } from 'wasp/server' import { handleRejection } from 'wasp/server/utils' import { MiddlewareConfigFn, globalMiddlewareConfigForExpress } from '../../middleware/index.js' {=# isAuthEnabled =} diff --git a/waspc/data/Generator/templates/server/src/server.ts b/waspc/data/Generator/templates/server/src/server.ts index e4b16898b6..382be0c310 100644 --- a/waspc/data/Generator/templates/server/src/server.ts +++ b/waspc/data/Generator/templates/server/src/server.ts @@ -2,11 +2,12 @@ import http from 'http' import app from './app.js' -import config from 'wasp/server/config' +import { config } from 'wasp/server' {=# setupFn.isDefined =} {=& setupFn.importStatement =} -import { ServerSetupFn, ServerSetupFnContext } from 'wasp/server/types' +import { ServerSetupFn } from 'wasp/server' +import { ServerSetupFnContext } from 'wasp/server/types' {=/ setupFn.isDefined =} {=# isPgBossJobExecutorUsed =} diff --git a/waspc/data/Generator/templates/server/src/webSocket/initialization.ts b/waspc/data/Generator/templates/server/src/webSocket/initialization.ts index 28467910c1..8e87baa8e6 100644 --- a/waspc/data/Generator/templates/server/src/webSocket/initialization.ts +++ b/waspc/data/Generator/templates/server/src/webSocket/initialization.ts @@ -4,8 +4,7 @@ import http from 'http' import { Server, Socket } from 'socket.io' import type { ServerType } from 'wasp/server/webSocket' -import config from 'wasp/server/config' -import prisma from 'wasp/server/dbClient' +import { config, prisma } from 'wasp/server' {=# isAuthEnabled =} import { getSessionAndUserFromSessionId } from 'wasp/auth/session' diff --git a/waspc/examples/todo-typescript/src/setup/serverSetup.ts b/waspc/examples/todo-typescript/src/setup/serverSetup.ts index ddfc8ab036..1b49dcf081 100644 --- a/waspc/examples/todo-typescript/src/setup/serverSetup.ts +++ b/waspc/examples/todo-typescript/src/setup/serverSetup.ts @@ -1,9 +1,7 @@ -import express from 'express' +import express, { Application } from 'express' import cors from 'cors' import type { MiddlewareConfigFn } from 'wasp/server/middleware' -import config from 'wasp/server/config' -import type { Application, ServerSetupFn } from 'wasp/server/types' -import prismaClient from 'wasp/server/dbClient' +import { config, ServerSetupFn, prisma } from 'wasp/server' export const serverMiddlewareFn: MiddlewareConfigFn = (middlewareConfig) => { // Example of adding an extra domains to CORS. @@ -29,5 +27,5 @@ export const serverSetup: ServerSetupFn = async ({ app }: { app: Application}) = res.send('I am a custom route') }) console.log("I am a server setup function!"); - console.log("Executed raw prisma client call: ", await prismaClient.$executeRaw``); + console.log("Executed raw prisma client call: ", await prisma.$executeRaw``); } diff --git a/waspc/examples/todo-typescript/src/user/customEmailSending.ts b/waspc/examples/todo-typescript/src/user/customEmailSending.ts index ab6a729409..222031c34c 100644 --- a/waspc/examples/todo-typescript/src/user/customEmailSending.ts +++ b/waspc/examples/todo-typescript/src/user/customEmailSending.ts @@ -6,34 +6,36 @@ import { } from 'wasp/server/auth/email/utils' export async function send() { + const userEmail = 'mihovil@ilakovac.com' + const link = await createPasswordResetLink( - 'mihovil@ilakovac.com', + userEmail, '/password-reset' ) const secondLink = await createEmailVerificationLink( - 'mihovil@ilakovac.com', + userEmail, '/email-verify' ) // Send email verification email. - await sendEmailVerificationEmail('mihovil@ilakovac.com', { + await sendEmailVerificationEmail(userEmail, { from: { name: 'Wasp', - email: 'mihovil@ilakovac.com', + email: userEmail, }, - to: 'mihovil@ilakovac.com', + to: userEmail, subject: 'Email verification', text: 'Click on the link to verify your email. ' + secondLink, html: `Click here to verify your email.`, }) // Send password reset email. - await sendPasswordResetEmail('mihovil@ilakovac.com', { + await sendPasswordResetEmail(userEmail, { from: { name: 'Wasp', - email: 'mihovil@ilakovac.com', + email: userEmail, }, - to: 'mihovil@ilakovac.com', + to: userEmail, subject: 'Password reset', text: 'Click on the link to reset your password.' + link, html: `Click here to reset your password.`, diff --git a/waspc/src/Wasp/Generator/SdkGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator.hs index 1a975cc2d7..363a1ad111 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator.hs @@ -86,6 +86,7 @@ genSdkReal spec = genFileCopy [relfile|operations/index.ts|], -- Not migrated to TS yet genFileCopy [relfile|operations/updateHandlersMap.js|], + genFileCopy [relfile|server/index.ts|], genFileCopy [relfile|server/dbClient.ts|], genFileCopy [relfile|types/index.ts|], genFileCopy [relfile|dbSeed/types.ts|], diff --git a/web/docs/data-model/backends.md b/web/docs/data-model/backends.md index 672f2998cc..f450fa796a 100644 --- a/web/docs/data-model/backends.md +++ b/web/docs/data-model/backends.md @@ -144,7 +144,7 @@ app MyApp { -Each seed function must be an async function that takes one argument, `prismaClient`, which is a [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client/crud) instance used to interact with the database. +Each seed function must be an async function that takes one argument, `prisma`, which is a [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client/crud) instance used to interact with the database. This is the same Prisma Client instance that Wasp uses internally and thus includes all of the usual features (e.g., password hashing). Since a seed function falls under server-side code, it can import other server-side functions. This is convenient because you might want to seed the database using Actions. @@ -157,20 +157,20 @@ Here's an example of a seed function that imports an Action: ```js import { createTask } from "./actions.js"; -export const devSeedSimple = async (prismaClient) => { - const user = await createUser(prismaClient, { +export const devSeedSimple = async (prisma) => { + const user = await createUser(prisma, { username: "RiuTheDog", password: "bark1234", }); await createTask( { description: "Chase the cat" }, - { user, entities: { Task: prismaClient.task } } + { user, entities: { Task: prisma.task } } ); }; -async function createUser(prismaClient, data) { - const { password, ...newUser } = await prismaClient.user.create({ data }); +async function createUser(prisma, data) { + const { password, ...newUser } = await prisma.user.create({ data }); return newUser; } ``` @@ -185,23 +185,23 @@ import { PrismaClient } from "@prisma/client"; type SanitizedUser = Omit; -export const devSeedSimple = async (prismaClient: PrismaClient) => { - const user = await createUser(prismaClient, { +export const devSeedSimple = async (prisma: PrismaClient) => { + const user = await createUser(prisma, { username: "RiuTheDog", password: "bark1234", }); await createTask( { description: "Chase the cat", isDone: false }, - { user, entities: { Task: prismaClient.task } } + { user, entities: { Task: prisma.task } } ); }; async function createUser( - prismaClient: PrismaClient, + prisma: PrismaClient, data: Pick ): Promise { - const { password, ...newUser } = await prismaClient.user.create({ data }); + const { password, ...newUser } = await prisma.user.create({ data }); return newUser; } ``` diff --git a/web/docs/data-model/entities.md b/web/docs/data-model/entities.md index 23843da178..3af8063294 100644 --- a/web/docs/data-model/entities.md +++ b/web/docs/data-model/entities.md @@ -77,9 +77,9 @@ You can only use the Prisma Client in your Wasp server code. You can import it l ```js -import prismaClient from '@wasp/dbClient'` +import prisma from '@wasp/dbClient'` -prismaClient.task.create({ +prisma.task.create({ description: "Read the Entities doc", isDone: true // almost :) }) @@ -89,9 +89,9 @@ prismaClient.task.create({ ```ts -import prismaClient from '@wasp/dbClient'` +import prisma from '@wasp/dbClient'` -prismaClient.task.create({ +prisma.task.create({ description: "Read the Entities doc", isDone: true // almost :) }) diff --git a/web/docs/project/server-config.md b/web/docs/project/server-config.md index 10800d1f59..b690db4b34 100644 --- a/web/docs/project/server-config.md +++ b/web/docs/project/server-config.md @@ -72,7 +72,8 @@ function addCustomRoute(app) { ```ts title="src/server/myServerSetupCode.ts" -import { ServerSetupFn, Application } from '@wasp/types' +import { ServerSetupFn } from 'wasp/server' +import { Application } from 'express' export const mySetupFunction: ServerSetupFn = async ({ app }) => { addCustomRoute(app)