Skip to content

Commit

Permalink
Cleanup Templates (#711)
Browse files Browse the repository at this point in the history
- Consolidate to four templates in this repository: one basic template
for each ORM DBOS supports.
- Also include two more complex sample apps whose source code is in
https://github.com/dbos-inc/dbos-demo-apps, one using Express and one
using Next.js.
- Use Express in the `hello-knex` app so the programming guide can use
it as a base. May move other templates to Express later.
- Remove OpenAPI generation, as the feature was rarely used, redundant
with many popular open-source OpenAPI generators, and increasingly
difficult to maintain.
  • Loading branch information
kraftp authored Jan 15, 2025
1 parent c0a1308 commit 6a8ee92
Show file tree
Hide file tree
Showing 155 changed files with 243 additions and 4,204 deletions.
1 change: 0 additions & 1 deletion .github/workflows/publish_npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
packages/create,
packages/dbos-cloud,
packages/dbos-compiler,
packages/dbos-openapi,
packages/dbos-sqs,
packages/dbos-kafkajs,
]
Expand Down
78 changes: 4 additions & 74 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
"knex": "3.1.0",
"koa": "^2.15.0",
"lodash": "4.17.21",
"openapi-types": "12.1.3",
"pg": "8.11.3",
"pg-protocol": "^1.6.1",
"reflect-metadata": "^0.2.2",
Expand Down
7 changes: 3 additions & 4 deletions packages/create/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ program
throw new Error(`Unexpected arguments: ${command.args.join(',')}; Did you forget '--'?`);
}
let {appName, template} = options;
if (appName || template) {
appName = appName || 'dbos-hello-app';
template = template || 'hello';
if (template) {
appName = template;
}
else {
const templates = listTemplates();
Expand All @@ -41,7 +40,7 @@ program
appName = await input(
{
message: 'What is the application/directory name to create?',
default: template, // Default to the template name
default: appName || template,
validate: isValidApplicationName,
});
}
Expand Down
23 changes: 23 additions & 0 deletions packages/create/templates/dbos-drizzle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# DBOS Hello with Drizzle

This is a [DBOS app](https://docs.dbos.dev/) bootstrapped with `npx @dbos-inc/create` and using [Drizzle](https://docs.dbos.dev/typescript/tutorials/orms/using-drizzle).

## Getting Started

Run these commands to build your app, set up its database, then launch it:

```bash
npm run build
npx dbos migrate
npm run start
```

To see that it's working, visit this URL in your browser: [`http://localhost:3000/`](http://localhost:3000/).

Congratulations! You just launched a DBOS application.

## Next Steps

- To add more functionality to this application, modify `src/operations.ts`, then rebuild and restart it. Alternatively, `npm run dev` uses `nodemon` to automatically rebuild and restart the app when source files change, using instructions specified in `nodemon.json`.
- For a detailed tutorial, check out the [programming guide](https://docs.dbos.dev/typescript/programming-guide).
- To learn more about DBOS, take a look at [the documentation](https://docs.dbos.dev/) or [source code](https://github.com/dbos-inc/dbos-transact-ts).
7 changes: 7 additions & 0 deletions packages/create/templates/dbos-drizzle/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"watch": ["src/"],
"ext": "ts,json",
"ignore": ["src/**/*.test.ts"],
"exec": "npm run build && npm run start"
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "dbos-hello-drizzle",
"name": "dbos-drizzle",
"version": "0.0.1",
"scripts": {
"build": "tsc",
Expand Down
23 changes: 23 additions & 0 deletions packages/create/templates/dbos-knex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# DBOS Hello

This is a [DBOS app](https://docs.dbos.dev/) bootstrapped with `npx @dbos-inc/create` and using [Knex](https://docs.dbos.dev/typescript/tutorials/orms/using-knex).

## Getting Started

Run these commands to build your app, set up its database, then launch it:

```bash
npm run build
npx dbos migrate
npm run start
```

To see that it's working, visit this URL in your browser: [`http://localhost:3000/`](http://localhost:3000/).

Congratulations! You just launched a DBOS application.

## Next Steps

- To add more functionality to this application, modify `src/main.ts`, then rebuild and restart it. Alternatively, `npm run dev` uses `nodemon` to automatically rebuild and restart the app when source files change, using instructions specified in `nodemon.json`.
- For a detailed tutorial, check out the [programming guide](https://docs.dbos.dev/typescript/programming-guide).
- To learn more about DBOS, take a look at [the documentation](https://docs.dbos.dev/) or [source code](https://github.com/dbos-inc/dbos-transact-ts).
7 changes: 7 additions & 0 deletions packages/create/templates/dbos-knex/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"watch": ["src/"],
"ext": "ts,json",
"ignore": ["src/**/*.test.ts"],
"exec": "npm run build && npm run start"
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "dbos-hello",
"name": "dbos-knex",
"version": "0.0.1",
"scripts": {
"build": "tsc",
"test": "npx dbos rollback && npx dbos migrate && jest",
"test": "npx dbos migrate && jest",
"lint": "eslint src",
"lint-fix": "eslint --fix src",
"dev": "nodemon",
"start": "npx dbos start"
},
Expand All @@ -22,6 +21,7 @@
},
"dependencies": {
"@dbos-inc/dbos-sdk": "file:../../../..",
"express": "^4.21.2",
"knex": "3.1.0"
}
}
94 changes: 94 additions & 0 deletions packages/create/templates/dbos-knex/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Welcome to DBOS!

// This is a sample "Hello" app built with DBOS and Knex.
// It greets visitors and keeps track of how many times each visitor has been greeted.

import express, { Request, Response } from 'express';
import { DBOS } from "@dbos-inc/dbos-sdk";

export interface dbos_hello {
name: string;
greet_count: number;
}

export class Hello {
// This transaction uses DBOS and Knex to perform database operations.
// It retrieves and increments the number of times a user has been greeted.
@DBOS.transaction()
static async helloTransaction(user: string) {
const query = "INSERT INTO dbos_hello (name, greet_count) VALUES (?, 1) ON CONFLICT (name) DO UPDATE SET greet_count = dbos_hello.greet_count + 1 RETURNING greet_count;";
const { rows } = (await DBOS.knexClient.raw(query, [user])) as { rows: dbos_hello[] };
const greet_count = rows[0].greet_count;
const greeting = `Hello, ${user}! You have been greeted ${greet_count} times.`;
return makeHTML(greeting);
}
}

// Let's create an HTML + CSS Readme for our app.
function readme() {
return makeHTML(
`Visit the route <code class="bg-gray-100 px-1 rounded">/greeting/{name}</code> to be greeted!<br>
For example, visit <code class="bg-gray-100 px-1 rounded"><a href="/greeting/Mike" class="text-blue-600 hover:underline">/greeting/Mike</a></code><br>
The counter increments with each page visit.`
);
}

function makeHTML(message: string) {
const page = `
<!DOCTYPE html>
<html lang="en">
<head>
<title>DBOS Template App</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="font-sans text-gray-800 p-6 max-w-2xl mx-auto">
<h1 class="text-3xl font-semibold mb-4">Welcome to DBOS!</h1>
<p class="mt-8 mb-8">` + message + `</p>
<p class="mb-2">
To learn how to run this app yourself, visit our
<a href="https://docs.dbos.dev/quickstart?language=typescript" class="text-blue-600 hover:underline">Quickstart</a>.
</p><p class="mb-2">
Then, to learn how to build crashproof apps, continue to our
<a href="https://docs.dbos.dev/typescript/programming-guide" class="text-blue-600 hover:underline">Programming Guide</a>.<br>
</p>
</body>
</html>`;
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() {
await DBOS.launch({expressApp: app});
const 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);
}
23 changes: 23 additions & 0 deletions packages/create/templates/dbos-prisma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# DBOS Hello with Prisma

This is a [DBOS app](https://docs.dbos.dev/) bootstrapped with `npx @dbos-inc/create` and using [Prisma](https://docs.dbos.dev/typescript/tutorials/orms/using-prisma).

## Getting Started

Run these commands to build your app, set up its database, then launch it:

```bash
npm run build
npx dbos migrate
npm run start
```

To see that it's working, visit this URL in your browser: [`http://localhost:3000/`](http://localhost:3000/).

Congratulations! You just launched a DBOS application.

## Next Steps

- To add more functionality to this application, modify `src/operations.ts`, then rebuild and restart it. Alternatively, `npm run dev` uses `nodemon` to automatically rebuild and restart the app when source files change, using instructions specified in `nodemon.json`.
- For a detailed tutorial, check out the [programming guide](https://docs.dbos.dev/typescript/programming-guide).
- To learn more about DBOS, take a look at [the documentation](https://docs.dbos.dev/) or [source code](https://github.com/dbos-inc/dbos-transact-ts).
Loading

0 comments on commit 6a8ee92

Please sign in to comment.