Skip to content

Commit ac168f9

Browse files
committed
chore: wire up app + api + db
1 parent 7529b33 commit ac168f9

9 files changed

+822
-9
lines changed

db/schema.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { text, integer, sqliteTable } from 'drizzle-orm/sqlite-core'
2+
import { sql } from 'drizzle-orm'
3+
4+
export const Specs = sqliteTable('Specs', {
5+
id: integer('id').primaryKey(),
6+
parentId: integer('parent_id'),
7+
content: text('content').notNull(),
8+
createdAt: text('created_at')
9+
.notNull()
10+
.default(sql`CURRENT_TIMESTAMP`),
11+
})

drizzle.config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from 'drizzle-kit'
2+
3+
export default defineConfig({
4+
schema: './db/schema.ts',
5+
driver: 'd1',
6+
verbose: true,
7+
strict: true,
8+
out: './migrations',
9+
})

functions/api/share.ts

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1+
import { drizzle } from 'drizzle-orm/d1'
2+
import { Specs } from '../../db/schema'
3+
14
export interface Env {
25
// If you set another name in wrangler.toml as the value for 'binding',
36
// replace "DB" with the variable name you defined.
47
DB: D1Database
58
}
69

710
export async function onRequest(context) {
8-
// If you did not use `DB` as your binding name, change it here
9-
const { results } = await context.env.DB.prepare(
10-
'SELECT * FROM Customers WHERE CompanyName = ?',
11-
)
12-
.bind('Bs Beverages')
13-
.all()
14-
15-
return Response.json(results)
11+
const db = drizzle(context.env.DB)
12+
13+
// Create a new entry
14+
if (context.request.method === 'POST') {
15+
// TODO: Validation
16+
const data = await context.request.json()
17+
18+
const result = await db
19+
.insert(Specs)
20+
.values({
21+
content: data.content,
22+
})
23+
.returning()
24+
25+
return Response.json(result)
26+
}
27+
28+
return new Response('Method not allowed, try POST.', {
29+
status: 405,
30+
})
31+
32+
// const result = await db.select().from(Specs).all()
33+
1634
// console.log('json', await context.request.json())
1735

1836
// const data = {

migrations/meta/0000_snapshot.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"version": "5",
3+
"dialect": "sqlite",
4+
"id": "88ea7ba3-9671-4d8a-b3cb-7672e6efeec5",
5+
"prevId": "00000000-0000-0000-0000-000000000000",
6+
"tables": {
7+
"Specs": {
8+
"name": "Specs",
9+
"columns": {
10+
"id": {
11+
"name": "id",
12+
"type": "integer",
13+
"primaryKey": true,
14+
"notNull": true,
15+
"autoincrement": false
16+
},
17+
"parent_id": {
18+
"name": "parent_id",
19+
"type": "integer",
20+
"primaryKey": false,
21+
"notNull": false,
22+
"autoincrement": false
23+
},
24+
"content": {
25+
"name": "content",
26+
"type": "text",
27+
"primaryKey": false,
28+
"notNull": true,
29+
"autoincrement": false
30+
},
31+
"created_at": {
32+
"name": "created_at",
33+
"type": "text",
34+
"primaryKey": false,
35+
"notNull": true,
36+
"autoincrement": false,
37+
"default": "DATETIME('now')"
38+
}
39+
},
40+
"indexes": {},
41+
"foreignKeys": {},
42+
"compositePrimaryKeys": {},
43+
"uniqueConstraints": {}
44+
}
45+
},
46+
"enums": {},
47+
"_meta": {
48+
"schemas": {},
49+
"tables": {},
50+
"columns": {}
51+
}
52+
}

migrations/meta/0001_snapshot.json

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"version": "5",
3+
"dialect": "sqlite",
4+
"id": "74278783-0df3-42f9-88c5-434d63d7f212",
5+
"prevId": "88ea7ba3-9671-4d8a-b3cb-7672e6efeec5",
6+
"tables": {
7+
"Specs": {
8+
"name": "Specs",
9+
"columns": {
10+
"id": {
11+
"name": "id",
12+
"type": "integer",
13+
"primaryKey": true,
14+
"notNull": true,
15+
"autoincrement": false
16+
},
17+
"parent_id": {
18+
"name": "parent_id",
19+
"type": "integer",
20+
"primaryKey": false,
21+
"notNull": false,
22+
"autoincrement": false
23+
},
24+
"content": {
25+
"name": "content",
26+
"type": "text",
27+
"primaryKey": false,
28+
"notNull": true,
29+
"autoincrement": false
30+
}
31+
},
32+
"indexes": {},
33+
"foreignKeys": {},
34+
"compositePrimaryKeys": {},
35+
"uniqueConstraints": {}
36+
}
37+
},
38+
"enums": {},
39+
"_meta": {
40+
"schemas": {},
41+
"tables": {},
42+
"columns": {}
43+
}
44+
}

migrations/meta/0002_snapshot.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"version": "5",
3+
"dialect": "sqlite",
4+
"id": "9605af17-a1b9-4179-adb3-d05af5e0fc04",
5+
"prevId": "74278783-0df3-42f9-88c5-434d63d7f212",
6+
"tables": {
7+
"Specs": {
8+
"name": "Specs",
9+
"columns": {
10+
"id": {
11+
"name": "id",
12+
"type": "integer",
13+
"primaryKey": true,
14+
"notNull": true,
15+
"autoincrement": false
16+
},
17+
"parent_id": {
18+
"name": "parent_id",
19+
"type": "integer",
20+
"primaryKey": false,
21+
"notNull": false,
22+
"autoincrement": false
23+
},
24+
"content": {
25+
"name": "content",
26+
"type": "text",
27+
"primaryKey": false,
28+
"notNull": true,
29+
"autoincrement": false
30+
},
31+
"created_at": {
32+
"name": "created_at",
33+
"type": "text",
34+
"primaryKey": false,
35+
"notNull": true,
36+
"autoincrement": false,
37+
"default": "CURRENT_TIMESTAMP"
38+
}
39+
},
40+
"indexes": {},
41+
"foreignKeys": {},
42+
"compositePrimaryKeys": {},
43+
"uniqueConstraints": {}
44+
}
45+
},
46+
"enums": {},
47+
"_meta": {
48+
"schemas": {},
49+
"tables": {},
50+
"columns": {}
51+
}
52+
}

migrations/meta/_journal.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"version": "5",
3+
"dialect": "sqlite",
4+
"entries": [
5+
{
6+
"idx": 0,
7+
"version": "5",
8+
"when": 1705278292306,
9+
"tag": "0000_dizzy_living_lightning",
10+
"breakpoints": true
11+
},
12+
{
13+
"idx": 1,
14+
"version": "5",
15+
"when": 1705278416497,
16+
"tag": "0001_windy_dark_beast",
17+
"breakpoints": true
18+
},
19+
{
20+
"idx": 2,
21+
"version": "5",
22+
"when": 1705278468137,
23+
"tag": "0002_fast_amphibian",
24+
"breakpoints": true
25+
}
26+
]
27+
}

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@
88
"build": "vue-tsc && vite build",
99
"frontend": "vite",
1010
"backend": "wrangler pages dev ./",
11-
"preview": "vite preview"
11+
"preview": "vite preview",
12+
"db:generate": "drizzle-kit generate:sqlite --schema=./db/schema.ts --out=./migrations",
13+
"db:up": "drizzle-kit up:sqlite"
1214
},
1315
"dependencies": {
1416
"@scalar/api-reference": "^1.12.1",
17+
"drizzle-orm": "^0.29.3",
1518
"monaco-editor": "^0.45.0",
1619
"vue": "^3.3.11"
1720
},
1821
"devDependencies": {
1922
"@vitejs/plugin-vue": "^4.5.2",
2023
"concurrently": "^8.2.2",
24+
"drizzle-kit": "^0.20.12",
2125
"eslint": "^8.56.0",
2226
"eslint-config-prettier": "^9.1.0",
2327
"eslint-plugin-prettier-vue": "^5.0.0",

0 commit comments

Comments
 (0)