Skip to content

Commit

Permalink
Update packages/docs/docs/100-getting-started/express.md
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
jimezesinachi and coderabbitai[bot] authored Oct 28, 2024
1 parent 7a98821 commit 0f23b7b
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/docs/docs/100-getting-started/express.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ The `QueryEngine` compiles SynthQL queries into plain SQL and sends them to the
// src/queryEngine.ts
import { QueryEngine } from '@synthql/backend';

// Ensure DATABASE_URL is set in your .env file:
// DATABASE_URL=postgresql://user:password@localhost:5432/dbname
if (!process.env.DATABASE_URL) {
throw new Error('DATABASE_URL environment variable is required');
}

export const queryEngine = new QueryEngine({
url: process.env.DATABASE_URL,
});
Expand All @@ -88,14 +94,35 @@ export const queryEngine = new QueryEngine({
```ts
// src/index.ts
import express from 'express';
import cors from 'cors';
import { createExpressSynthqlHandler } from '@synthql/handler-express';
import { queryEngine } from './queryEngine';

const app = express();

// Enable CORS for your client application
app.use(cors({
origin: process.env.CLIENT_URL || 'http://localhost:3000'
}));

const expressSynthqlRequestHandler = createExpressSynthqlHandler(queryEngine);

app.post('/synthql', async (req, res) => {
return await expressSynthqlRequestHandler(req, res);
app.post('/synthql', async (req, res, next) => {
try {
return await expressSynthqlRequestHandler(req, res);
} catch (error) {
next(error);
}
});

// Error handling middleware
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({
error: process.env.NODE_ENV === 'production'
? 'Internal server error'
: err.message
});
});

app.listen(3000);
Expand Down

0 comments on commit 0f23b7b

Please sign in to comment.