Skip to content

Commit ca255f3

Browse files
committed
update serverlessdb examples
Signed-off-by: karthik2804 <[email protected]>
1 parent f9e520f commit ca255f3

File tree

20 files changed

+3937
-166
lines changed

20 files changed

+3937
-166
lines changed

examples/serverless_db/neondb/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ node_modules
22
dist
33
target
44
.spin/
5-
dist.js
5+
build/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KNITWIT_SOURCE=./config/knitwit.json

examples/serverless_db/neondb/README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
This example demonstrates how to connect to Neon Database using the Neon Serverless SDK, execute a SQL query, and handle responses using the Spin SDK.
44

5-
## Install Dependencies
6-
Install the necessary npm packages:
7-
8-
```bash
9-
npm install
10-
```
11-
125
## Setup the Example
136

147
1. **Setup Neon Database**

examples/serverless_db/neondb/package-lock.json

Lines changed: 3800 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/serverless_db/neondb/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -n spin-http -o target/neondb.wasm",
7+
"build": "knitwit --out-dir build/wit/knitwit --out-world combined && npx webpack --mode=production && npx mkdirp dist && npx j2w -i build/bundle.js -d build/wit/knitwit -n combined -o dist/neondb.wasm",
88
"test": "echo \"Error: no test specified\" && exit 1"
99
},
1010
"keywords": [],
@@ -15,10 +15,12 @@
1515
"ts-loader": "^9.4.1",
1616
"typescript": "^4.8.4",
1717
"webpack": "^5.74.0",
18-
"webpack-cli": "^4.10.0"
18+
"webpack-cli": "^4.10.0",
19+
"@fermyon/knitwit": "0.3.0"
1920
},
2021
"dependencies": {
21-
"@fermyon/spin-sdk": "^2.0.0",
22+
"@fermyon/spin-sdk": "^3.0.0-rc1",
23+
"itty-router": "^5.0.18",
2224
"@neondatabase/serverless": "^0.9.3"
2325
}
2426
}

examples/serverless_db/neondb/spin.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ route = "/..."
1111
component = "neondb"
1212

1313
[component.neondb]
14-
source = "target/neondb.wasm"
14+
source = "dist/neondb.wasm"
1515
exclude_files = ["**/node_modules"]
16-
allowed_outbound_hosts = ["https://*:*"]
16+
allowed_outbound_hosts = ["https://*.aws.neon.tech"]
1717
[component.neondb.build]
18-
command = "npm run build"
19-
watch = ["src/**/*.ts", "package.json"]
18+
command = ["npm install", "npm run build"]
19+
watch = ["src/**/*.ts"]
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
import { ResponseBuilder } from '@fermyon/spin-sdk';
1+
// https://itty.dev/itty-router/routers/autorouter
2+
import { AutoRouter } from 'itty-router';
23
import { neon } from '@neondatabase/serverless';
34

4-
export async function handler(_req: Request, res: ResponseBuilder) {
5-
try {
6-
const sql = neon('<neon-database-endpoint>');
7-
const posts = await sql('SELECT * FROM posts');
8-
res.send(JSON.stringify(posts, null, 2));
9-
} catch (e: any) {
10-
res.status(500);
11-
res.send(`error: ${e}`);
12-
}
13-
}
5+
let router = AutoRouter();
6+
7+
// Route ordering matters, the first route that matches will be used
8+
// Any route that does not return will be treated as a middleware
9+
// Any unmatched route will return a 404
10+
router
11+
.get("/", async () => {
12+
try {
13+
const sql = neon('<neon-database-endpoint>');
14+
const posts = await sql('SELECT * FROM posts');
15+
return new Response(JSON.stringify(posts, null, 2), { headers: { 'Content-Type': 'application/json' } });
16+
} catch (e: any) {
17+
return new Response(`error: ${e}`, { status: 500 });
18+
}
19+
})
20+
21+
//@ts-ignore
22+
addEventListener('fetch', async (event: FetchEvent) => {
23+
event.respondWith(router.fetch(event.request));
24+
});

examples/serverless_db/neondb/src/spin.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
const path = require('path');
2-
const SpinSdkPlugin = require('@fermyon/spin-sdk/plugins/webpack');
2+
const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack")
33

44
module.exports = {
5-
entry: './src/spin.ts',
6-
experiments: {
7-
outputModule: true,
8-
},
9-
module: {
10-
rules: [
11-
{
12-
test: /\.tsx?$/,
13-
use: 'ts-loader',
14-
exclude: /node_modules/,
15-
},
5+
entry: './src/index.ts',
6+
experiments: {
7+
outputModule: true,
8+
},
9+
module: {
10+
rules: [
11+
{
12+
test: /\.tsx?$/,
13+
use: 'ts-loader',
14+
exclude: /node_modules/,
15+
},
16+
],
17+
},
18+
resolve: {
19+
extensions: ['.tsx', '.ts', '.js'],
20+
},
21+
output: {
22+
path: path.resolve(__dirname, './build'),
23+
filename: 'bundle.js',
24+
module: true,
25+
library: {
26+
type: "module",
27+
}
28+
},
29+
plugins: [
30+
new SpinSdkPlugin()
1631
],
17-
},
18-
resolve: {
19-
extensions: ['.tsx', '.ts', '.js'],
20-
},
21-
output: {
22-
path: path.resolve(__dirname, './'),
23-
filename: 'dist.js',
24-
module: true,
25-
library: {
26-
type: 'module',
32+
optimization: {
33+
minimize: false
2734
},
28-
},
29-
plugins: [new SpinSdkPlugin()],
30-
optimization: {
31-
minimize: false,
32-
},
33-
};
35+
};

0 commit comments

Comments
 (0)