// Welcome to DBOS! // This is the Quickstart Prisma template app. It greets visitors, counting how many total greetings were made. // To learn how to run this app, visit the Prisma tutorial: https://docs.dbos.dev/tutorials/using-prisma import express, { Request, Response } from 'express'; import { DBOS } from '@dbos-inc/dbos-sdk'; import { PrismaClient } from '@prisma/client'; import { PrismaDataSource } from '@dbos-inc/prisma-datasource'; process.env['DATABASE_URL'] = process.env['DBOS_DATABASE_URL'] || process.env['DATABASE_URL'] || `postgresql://${process.env.PGUSER || 'postgres'}:${process.env.PGPASSWORD || 'dbos'}@${process.env.PGHOST || 'localhost'}:${process.env.PGPORT || '5432'}/${process.env.PGDATABASE || 'dbos_prisma'}`; const prismaClient = new PrismaClient(); const dataSource = new PrismaDataSource('app-db', prismaClient); export class Hello { // This transaction uses DBOS and prisma to perform database operations. @dataSource.transaction() static async helloTransaction(name: string) { const greeting = `Hello, ${name}!`; const res = await dataSource.client.dbosHello.create({ data: { greeting: greeting, }, }); const greeting_note = `Greeting ${res.greeting_id}: ${greeting}`; return makeHTML(greeting_note); } } // Let's create an HTML + CSS Readme for our app. function readme() { return makeHTML( `Visit the route /greeting/{name} to be greeted!
For example, visit /greeting/Mike
The counter increments with each page visit.`, ); } function makeHTML(message: string) { const page = ` DBOS Template App

Welcome to DBOS!

` + message + `

To learn how to run this app yourself, visit our Quickstart.

Then, to learn how to build crashproof apps, continue to our Programming Guide.

`; return page; } // Let's create an HTTP server using Express.js export const app = express(); app.use(express.json()); // Serve the Readme from the root path app.get('/', (req: Request, res: Response) => { res.send(readme()); }); // Serve the transaction from the /greeting/:name path app.get('/greeting/:name', (req: Request, res: Response) => { const { name } = req.params; Hello.helloTransaction(name) .then((result) => res.send(result)) .catch((error) => { console.error(error); res.status(500).send('Internal Server Error'); }); }); // Finally, launch DBOS and start the server async function main() { DBOS.setConfig({ name: 'dbos-prisma', systemDatabaseUrl: process.env.DBOS_SYSTEM_DATABASE_URL, }); await DBOS.launch(); const PORT = parseInt(process.env.NODE_PORT || '3000'); const ENV = process.env.NODE_ENV || 'development'; app.listen(PORT, () => { console.log(`🚀 Server is running on http://localhost:${PORT}`); console.log(`🌟 Environment: ${ENV}`); }); } // Only start the server when this file is run directly from Node if (require.main === module) { main().catch(console.log); }