Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
# ZenStack V3 Quick Start Project

The project demonstrates a minimum TypeScript application that uses ZenStack with Sql.js (a WASM port of SQLite).
The project demonstrates a minimum TypeScript application that uses ZenStack with Sql.js (a WASM port of SQLite) and Express.js as a backend server.

## Setup

To run the project:

- `npm install`
- `npm run generate`
- `npm run dev`

The server will start on `http://localhost:3000`.

## API Endpoints

- `GET /` - Welcome message
- `GET /api/users` - Get all users with their posts
- `GET /api/posts` - Get all posts with author information
- `GET /api/posts/:id` - Get a specific post by ID with author information

## Example Usage

```bash
# Get all users
curl http://localhost:3000/api/users

# Get all posts
curl http://localhost:3000/api/posts

# Get a specific post
curl http://localhost:3000/api/posts/1
```
72 changes: 71 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,78 @@
import express from 'express';
import { createClient } from './db';
import { createUsersAndPosts } from './utils';

async function main() {
const db = await createClient();


// Seed database with sample data
await createUsersAndPosts(db);

const app = express();
const port = process.env.PORT || 3000;

// Middleware
app.use(express.json());

// Routes
app.get('/', (req, res) => {
res.json({ message: 'Welcome to ZenStack Express Server!' });
});

// Get all users
app.get('/api/users', async (req, res) => {
try {
const users = await db.user.findMany({
include: { posts: true }
});
res.json(users);
} catch (error) {
console.error('Error fetching users:', error);
res.status(500).json({ error: 'Failed to fetch users' });
}
});

// Get all posts
app.get('/api/posts', async (req, res) => {
try {
const posts = await db.post.findMany({
include: { author: true }
});
res.json(posts);
} catch (error) {
console.error('Error fetching posts:', error);
res.status(500).json({ error: 'Failed to fetch posts' });
}
});

// Get post by id
app.get('/api/posts/:id', async (req, res) => {
try {
const id = parseInt(req.params.id);
if (isNaN(id) || id < 1) {
res.status(400).json({ error: 'Invalid post ID' });
return;
}

const post = await db.post.findUnique({
where: { id },
include: { author: true }
});
if (!post) {
res.status(404).json({ error: 'Post not found' });
} else {
res.json(post);
}
} catch (error) {
console.error('Error fetching post:', error);
res.status(500).json({ error: 'Failed to fetch post' });
}
});

// Start server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
}

main();
Loading