-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: karthik2804 <[email protected]>
- Loading branch information
1 parent
fbbf2c8
commit 2e4a417
Showing
11 changed files
with
2,462 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
dist | ||
target | ||
.spin/ | ||
dist.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Drizzle ORM Integration | ||
|
||
This example showcases how to use [Drizzle ORM](https://orm.drizzle.team/) to generate database queries and execute it against Spin's SQLite database. | ||
|
||
## Install Dependencies | ||
Install the necessary npm packages: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
## Building and Running the Example | ||
|
||
```bash | ||
spin build | ||
spin up --sqlite @migrations.sql | ||
``` | ||
|
||
Use e.g. curl -v http://127.0.0.1:3000/ to test the endpoint. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"version": 1, | ||
"project": { | ||
"worlds": [ | ||
"spin-http" | ||
] | ||
}, | ||
"packages": { | ||
"@fermyon/spin-sdk": { | ||
"witPath": "../../bin/wit", | ||
"world": "spin-imports" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER); | ||
INSERT OR REPLACE INTO users (id, name, age) VALUES (1, 'Dan', 30); | ||
INSERT OR REPLACE INTO users (id, name, age) VALUES (2, 'Alice', 25); |
2,268 changes: 2,268 additions & 0 deletions
2,268
examples/typescript/orm/drizzle/package-lock.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "drizzle", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -n spin-http -o target/drizzle.wasm", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"mkdirp": "^3.0.1", | ||
"ts-loader": "^9.4.1", | ||
"typescript": "^4.8.4", | ||
"webpack": "^5.74.0", | ||
"webpack-cli": "^4.10.0" | ||
}, | ||
"dependencies": { | ||
"@fermyon/spin-sdk": "^2.0.0-alpha.2", | ||
"drizzle-orm": "^0.31.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
spin_manifest_version = 2 | ||
|
||
[application] | ||
authors = ["karthik2804 <[email protected]>"] | ||
description = "" | ||
name = "drizzle" | ||
version = "0.1.0" | ||
|
||
[[trigger.http]] | ||
route = "/..." | ||
component = "drizzle" | ||
|
||
[component.drizzle] | ||
source = "target/drizzle.wasm" | ||
exclude_files = ["**/node_modules"] | ||
sqlite_databases = ["default"] | ||
[component.drizzle.build] | ||
command = "npm run build" | ||
watch = ["src/**/*.ts", "package.json"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ResponseBuilder, Sqlite } from "@fermyon/spin-sdk"; | ||
import { QueryBuilder } from "drizzle-orm/sqlite-core"; | ||
import { eq } from "drizzle-orm"; | ||
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core'; | ||
import { ParameterValue } from "@fermyon/spin-sdk/lib/sqlite"; | ||
|
||
const users = sqliteTable('users', { | ||
id: integer('id').primaryKey(), | ||
name: text('name').notNull(), | ||
age: integer('age'), | ||
}); | ||
|
||
// Need this as BigInt des not implement to JSON by default. | ||
(BigInt.prototype as any).toJSON = function () { | ||
return this.toString() | ||
} | ||
|
||
export async function handler(req: Request, res: ResponseBuilder) { | ||
|
||
// Use drizzle to generate the query | ||
const qb = new QueryBuilder(); | ||
const query = qb.select().from(users).where(eq(users.name, 'Dan')); | ||
const { sql, params } = query.toSQL(); | ||
|
||
// Use the generated query and parameters to execute against Spin's sqlite | ||
// database | ||
let sqlite = Sqlite.openDefault() | ||
try { | ||
|
||
let result = sqlite.execute(sql, params as ParameterValue[]) | ||
res.send(JSON.stringify(result, null, 2)); | ||
} catch (e: any) { | ||
console.log(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ResponseBuilder } from "@fermyon/spin-sdk"; | ||
import { handler } from "."; | ||
|
||
//@ts-ignore | ||
addEventListener('fetch', (event: FetchEvent) => { | ||
handleEvent(event); | ||
}); | ||
|
||
async function handleEvent(event: FetchEvent) { | ||
|
||
let resolve: any, reject: any; | ||
let responsePromise = new Promise(async (res, rej) => { | ||
resolve = res; | ||
reject = rej; | ||
}); | ||
//@ts-ignore | ||
event.respondWith(responsePromise); | ||
|
||
let res = new ResponseBuilder(resolve); | ||
|
||
await handler(event.request, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./dist/", | ||
"noImplicitAny": true, | ||
"module": "es6", | ||
"target": "es2020", | ||
"jsx": "react", | ||
"skipLibCheck": true, | ||
"lib": [ | ||
"ES2015", | ||
"WebWorker" | ||
], | ||
"allowJs": true, | ||
"strict": true, | ||
"noImplicitReturns": true, | ||
"moduleResolution": "node" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const path = require('path'); | ||
const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack") | ||
|
||
module.exports = { | ||
entry: './src/spin.ts', | ||
experiments: { | ||
outputModule: true, | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.tsx?$/, | ||
use: 'ts-loader', | ||
exclude: /node_modules/, | ||
}, | ||
], | ||
}, | ||
resolve: { | ||
extensions: ['.tsx', '.ts', '.js'], | ||
}, | ||
output: { | ||
path: path.resolve(__dirname, './'), | ||
filename: 'dist.js', | ||
module: true, | ||
library: { | ||
type: "module", | ||
} | ||
}, | ||
plugins: [ | ||
new SpinSdkPlugin() | ||
], | ||
optimization: { | ||
minimize: false | ||
}, | ||
}; |