// Welcome to DBOS! // This is the Quickstart TypeORM template app. It greets visitors, counting how many total greetings were made. // To learn how to run this app, visit the TypeORM tutorial: https://docs.dbos.dev/tutorials/using-typeorm import express, { Request, Response } from 'express'; import { DBOS } from '@dbos-inc/dbos-sdk'; import { DBOSHello } from '../entities/DBOSHello'; import { TypeOrmDataSource } from '@dbos-inc/typeorm-datasource'; 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_typeorm'}`, }; const dataSource = TypeOrmDataSource.createFromConfig('app-db', config, [DBOSHello]); export class Hello { // This transaction uses DBOS and TypeORM to perform database operations. @dataSource.transaction() static async helloTransaction(name: string) { const greeting = `Hello, ${name}!`; let entity = new DBOSHello(); entity.greeting = greeting; entity = await dataSource.entityManager.save(entity); const greeting_note = `Greeting ${entity.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-typeorm', 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); }