// Welcome to DBOS! // This is the Quickstart Drizzle template app. It greets visitors, counting how many total greetings were made. // To learn how to run this app, visit the Drizzle tutorial: https://docs.dbos.dev/tutorials/using-drizzle import express, { Request, Response } from 'express'; import { DBOS } from '@dbos-inc/dbos-sdk'; import { dbosHello } from './schema'; import { DrizzleDataSource } from '@dbos-inc/drizzle-datasource'; import { NodePgDatabase } from 'drizzle-orm/node-postgres'; export const config = { connectionString: process.env.DBOS_DATABASE_URL || `postgresql://${process.env.PGUSER || 'postgres'}:${process.env.PGPASSWORD || 'dbos'}@${process.env.PGHOST || 'localhost'}:${process.env.PGPORT || '5432'}/${process.env.PGDATABASE || 'dbos_drizzle'}`, }; const drizzleds = new DrizzleDataSource('app-db', config); export class Hello { // This transaction uses DBOS and drizzle to perform database operations. @drizzleds.transaction() static async helloTransaction(user: string) { const greeting = `Hello, ${user}!`; const greetings_output = await drizzleds.client .insert(dbosHello) .values({ greeting }) .returning({ greet_count: dbosHello.greet_count }); const greeting_message = `${greeting} We have made ${greetings_output[0].greet_count} greetings.`; return makeHTML(greeting_message); } } // 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-drizzle', 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); }