create-surf-app 1.0.0-alpha.33 → 1.0.0-alpha.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -69,27 +69,43 @@ exports.users = pgTable("users", {
|
|
|
69
69
|
|
|
70
70
|
Tables are auto-created on startup and when `schema.js` changes (file watcher).
|
|
71
71
|
|
|
72
|
-
Query the database in routes
|
|
72
|
+
Query the database in routes with `dbQuery(sql, params)` from `@surf-ai/sdk/db`. Drizzle ORM is **only** used to declare the schema — there is no Drizzle client, no `req.db` middleware, and no direct connection pool. `dbQuery` returns a pg-style result `{ rows, rowCount, fields }`, so destructure `rows`:
|
|
73
73
|
|
|
74
74
|
```js
|
|
75
75
|
const { dbQuery } = require("@surf-ai/sdk/db");
|
|
76
76
|
|
|
77
77
|
router.get("/", async (req, res) => {
|
|
78
|
-
const rows = await dbQuery(
|
|
78
|
+
const { rows } = await dbQuery(
|
|
79
|
+
"SELECT * FROM users ORDER BY created_at DESC"
|
|
80
|
+
);
|
|
79
81
|
res.json(rows);
|
|
80
82
|
});
|
|
81
83
|
|
|
82
84
|
router.post("/", async (req, res) => {
|
|
83
85
|
const { name } = req.body;
|
|
84
|
-
const
|
|
86
|
+
const { rows } = await dbQuery(
|
|
85
87
|
"INSERT INTO users (name) VALUES ($1) RETURNING *",
|
|
86
88
|
[name]
|
|
87
89
|
);
|
|
88
|
-
res.json(
|
|
90
|
+
res.json(rows[0]);
|
|
89
91
|
});
|
|
90
92
|
```
|
|
91
93
|
|
|
92
|
-
|
|
94
|
+
### Environment variables
|
|
95
|
+
|
|
96
|
+
Only variables prefixed with `VITE_` are exposed to the Vite frontend (`import.meta.env.VITE_FOO`). To use a plain env var (e.g. `APP_TITLE`) in the UI, read it in a backend route and fetch it from the frontend:
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
// backend/routes/config.js
|
|
100
|
+
const express = require("express");
|
|
101
|
+
const router = express.Router();
|
|
102
|
+
|
|
103
|
+
router.get("/", (_req, res) => {
|
|
104
|
+
res.json({ title: process.env.APP_TITLE || "App" });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
module.exports = router;
|
|
108
|
+
```
|
|
93
109
|
|
|
94
110
|
## Do NOT modify
|
|
95
111
|
|
|
@@ -72,27 +72,31 @@ export const users = pgTable("users", {
|
|
|
72
72
|
|
|
73
73
|
Tables are auto-synced on server start and when `db/schema.ts` changes in dev mode.
|
|
74
74
|
|
|
75
|
-
Query the database in API routes using
|
|
75
|
+
Query the database in API routes using the `db()` helper re-exported from `@/db`. Drizzle ORM is **only** used to declare the schema — there is no Drizzle client and no direct connection pool. `db(sql, params)` returns a pg-style result `{ rows, rowCount, fields }`, so destructure `rows`:
|
|
76
76
|
|
|
77
77
|
```ts
|
|
78
|
-
import {
|
|
78
|
+
import { db } from "@/db";
|
|
79
79
|
|
|
80
80
|
export async function GET() {
|
|
81
|
-
const rows = await
|
|
81
|
+
const { rows } = await db(
|
|
82
|
+
"SELECT * FROM users ORDER BY created_at DESC"
|
|
83
|
+
);
|
|
82
84
|
return Response.json(rows);
|
|
83
85
|
}
|
|
84
86
|
|
|
85
87
|
export async function POST(request: Request) {
|
|
86
88
|
const { name } = await request.json();
|
|
87
|
-
const
|
|
89
|
+
const { rows } = await db(
|
|
88
90
|
"INSERT INTO users (name) VALUES ($1) RETURNING *",
|
|
89
91
|
[name]
|
|
90
92
|
);
|
|
91
|
-
return Response.json(
|
|
93
|
+
return Response.json(rows[0]);
|
|
92
94
|
}
|
|
93
95
|
```
|
|
94
96
|
|
|
95
|
-
|
|
97
|
+
### Environment variables
|
|
98
|
+
|
|
99
|
+
Only variables prefixed with `NEXT_PUBLIC_` are exposed to the browser. To use a plain env var (e.g. `APP_TITLE`) in a client component, either read it in a server component / route handler and pass it down as a prop, or expose it through a backend route.
|
|
96
100
|
|
|
97
101
|
## Do NOT modify
|
|
98
102
|
|
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
//
|
|
4
4
|
// Usage in route handlers:
|
|
5
5
|
// import { db } from '@/db'
|
|
6
|
-
// const
|
|
6
|
+
// const { rows } = await db('SELECT * FROM users')
|
|
7
7
|
|
|
8
8
|
export { dbQuery as db, dbProvision, dbTables, dbTableSchema, dbStatus } from '@surf-ai/sdk/db'
|