This is a template project for creating SvelteKit applications with tRPC integration and Prisma for database access, providing a type-safe way to build full-stack applications.
# Clone this repository
git clone https://github.com/your-username/sveltekit-trpc-template.git my-project
# Navigate to project directory
cd my-project
# Remove the existing git history (optional)
rm -rf .git
# Initialize a new git repository (optional)
git init
# Install all dependencies
npm install
# Generate the Prisma client
npx prisma generate
# Create the database and run migrations
npx prisma migrate dev --name init
# Start development server
npm run dev
The application will be available at http://localhost:5173.
npm run prisma:studio
to open a visual database browsernpm run prisma:generate
after schema changesnpm run prisma:migrate:dev
to create new migrationsnpm run prisma:migrate:reset
to reset the databaseprisma/
- Contains Prisma schema and migrationssrc/lib/server/prisma.ts
- Exports the Prisma client instancesrc/lib/server/trpc/
- Contains all server-side tRPC codecontext.ts
- Defines the context passed to tRPC procedurestrpc.ts
- Sets up the tRPC instancerouter.ts
- Defines the tRPC routes and proceduressrc/lib/trpc/
- Contains client-side tRPC codeclient.ts
- Configures the tRPC clientsrc/lib/components/
- Reusable UI componentssrc/routes/api/trpc/[...trpc]/+server.ts
- The SvelteKit endpoint that handles tRPC requestssrc/routes/
- SvelteKit routes and pagessrc/lib/server/trpc/router.ts
myNewProcedure: publicProcedure
.input(
z.object({
// Define your input schema here using Zod
parameter: z.string()
})
)
.query(async ({ input, ctx }) => {
// Your procedure logic here
// Access the database with ctx.prisma
return {
result: `Processed: ${input.parameter}`
};
});
// In a Svelte component
import { trpc } from '$lib/trpc';
const result = await trpc.myNewProcedure.query({ parameter: 'test' });
prisma/schema.prisma
file to add your new modelnpm run prisma:migrate:dev
to create a migrationctx.prisma
clientBuild the application for production:
npm run build
The build output will be in the build
directory, which can be deployed to your preferred hosting service.
To add authentication:
src/lib/server/trpc/context.ts
src/lib/server/trpc/trpc.ts