yippee C and R from db works well

This commit is contained in:
2026-01-12 18:16:32 +13:00
parent 8794bd6aea
commit ea173dcc52
9 changed files with 57 additions and 183 deletions

View File

@@ -3,8 +3,6 @@ import { defineConfig } from 'astro/config';
import tailwindcss from '@tailwindcss/vite'; import tailwindcss from '@tailwindcss/vite';
import db from '@astrojs/db';
import node from '@astrojs/node'; import node from '@astrojs/node';
// https://astro.build/config // https://astro.build/config
@@ -14,8 +12,6 @@ export default defineConfig({
plugins: [tailwindcss()] plugins: [tailwindcss()]
}, },
integrations: [db()],
adapter: node({ adapter: node({
mode: 'standalone' mode: 'standalone'
}) })

11
drizzle.config.ts Normal file
View File

@@ -0,0 +1,11 @@
import 'dotenv/config';
import { defineConfig } from 'drizzle-kit';
export default defineConfig({
out: './drizzle',
schema: './src/db/schema.ts',
dialect: 'postgresql',
dbCredentials: {
url: process.env.DATABASE_URL!,
},
});

View File

@@ -9,7 +9,6 @@
"astro": "astro" "astro": "astro"
}, },
"dependencies": { "dependencies": {
"@astrojs/db": "^0.18.3",
"@astrojs/node": "^9.5.1", "@astrojs/node": "^9.5.1",
"@tailwindcss/vite": "^4.1.18", "@tailwindcss/vite": "^4.1.18",
"astro": "^5.16.8", "astro": "^5.16.8",

191
pnpm-lock.yaml generated
View File

@@ -8,9 +8,6 @@ importers:
.: .:
dependencies: dependencies:
'@astrojs/db':
specifier: ^0.18.3
version: 0.18.3(@types/pg@8.16.0)(pg@8.16.3)
'@astrojs/node': '@astrojs/node':
specifier: ^9.5.1 specifier: ^9.5.1
version: 9.5.1(astro@5.16.8(@types/node@25.0.6)(jiti@2.6.1)(lightningcss@1.30.2)(rollup@4.55.1)(tsx@4.21.0)(typescript@5.9.3)) version: 9.5.1(astro@5.16.8(@types/node@25.0.6)(jiti@2.6.1)(lightningcss@1.30.2)(rollup@4.55.1)(tsx@4.21.0)(typescript@5.9.3))
@@ -51,9 +48,6 @@ packages:
'@astrojs/compiler@2.13.0': '@astrojs/compiler@2.13.0':
resolution: {integrity: sha512-mqVORhUJViA28fwHYaWmsXSzLO9osbdZ5ImUfxBarqsYdMlPbqAqGJCxsNzvppp1BEzc1mJNjOVvQqeDN8Vspw==} resolution: {integrity: sha512-mqVORhUJViA28fwHYaWmsXSzLO9osbdZ5ImUfxBarqsYdMlPbqAqGJCxsNzvppp1BEzc1mJNjOVvQqeDN8Vspw==}
'@astrojs/db@0.18.3':
resolution: {integrity: sha512-iTK50jUgyj25oa/JiXSN1/IVp5kTmPuioLlve06LE8/HzWGv3JpVgCKIV9HHf3kOVi1HV/uauXnyWzkB+yHLSQ==}
'@astrojs/internal-helpers@0.7.5': '@astrojs/internal-helpers@0.7.5':
resolution: {integrity: sha512-vreGnYSSKhAjFJCWAwe/CNhONvoc5lokxtRoZims+0wa3KbHBdPHSSthJsKxPd8d/aic6lWKpRTYGY/hsgK6EA==} resolution: {integrity: sha512-vreGnYSSKhAjFJCWAwe/CNhONvoc5lokxtRoZims+0wa3KbHBdPHSSthJsKxPd8d/aic6lWKpRTYGY/hsgK6EA==}
@@ -1205,10 +1199,6 @@ packages:
decode-named-character-reference@1.2.0: decode-named-character-reference@1.2.0:
resolution: {integrity: sha512-c6fcElNV6ShtZXmsgNgFFV5tVX2PaV4g+MOAkb8eXHvn6sryJBrZa9r0zV6+dtTyoCKxtDy5tyQ5ZwQuidtd+Q==} resolution: {integrity: sha512-c6fcElNV6ShtZXmsgNgFFV5tVX2PaV4g+MOAkb8eXHvn6sryJBrZa9r0zV6+dtTyoCKxtDy5tyQ5ZwQuidtd+Q==}
deep-diff@1.0.2:
resolution: {integrity: sha512-aWS3UIVH+NPGCD1kki+DCU9Dua032iSsO43LqQpcs4R3+dVv7tX0qBGjiVHJHjplsoUM2XRO/KB92glqc68awg==}
deprecated: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
defu@6.1.4: defu@6.1.4:
resolution: {integrity: sha512-mEQCMmwJu317oSz8CwdIOdwf3xMif1ttiM8LTufzc3g6kR+9Pe236twL8j3IYT1F7GfRgGcW6MWxzZjLIkuHIg==} resolution: {integrity: sha512-mEQCMmwJu317oSz8CwdIOdwf3xMif1ttiM8LTufzc3g6kR+9Pe236twL8j3IYT1F7GfRgGcW6MWxzZjLIkuHIg==}
@@ -1269,95 +1259,6 @@ packages:
resolution: {integrity: sha512-O9EC/miwdnRDY10qRxM8P3Pg8hXe3LyU4ZipReKOgTwn4OqANmftj8XJz1UPUAS6NMHf0E2htjsbQujUTkncCg==} resolution: {integrity: sha512-O9EC/miwdnRDY10qRxM8P3Pg8hXe3LyU4ZipReKOgTwn4OqANmftj8XJz1UPUAS6NMHf0E2htjsbQujUTkncCg==}
hasBin: true hasBin: true
drizzle-orm@0.42.0:
resolution: {integrity: sha512-pS8nNJm2kBNZwrOjTHJfdKkaU+KuUQmV/vk5D57NojDq4FG+0uAYGMulXtYT///HfgsMF0hnFFvu1ezI3OwOkg==}
peerDependencies:
'@aws-sdk/client-rds-data': '>=3'
'@cloudflare/workers-types': '>=4'
'@electric-sql/pglite': '>=0.2.0'
'@libsql/client': '>=0.10.0'
'@libsql/client-wasm': '>=0.10.0'
'@neondatabase/serverless': '>=0.10.0'
'@op-engineering/op-sqlite': '>=2'
'@opentelemetry/api': ^1.4.1
'@planetscale/database': '>=1.13'
'@prisma/client': '*'
'@tidbcloud/serverless': '*'
'@types/better-sqlite3': '*'
'@types/pg': '*'
'@types/sql.js': '*'
'@vercel/postgres': '>=0.8.0'
'@xata.io/client': '*'
better-sqlite3: '>=7'
bun-types: '*'
expo-sqlite: '>=14.0.0'
gel: '>=2'
knex: '*'
kysely: '*'
mysql2: '>=2'
pg: '>=8'
postgres: '>=3'
prisma: '*'
sql.js: '>=1'
sqlite3: '>=5'
peerDependenciesMeta:
'@aws-sdk/client-rds-data':
optional: true
'@cloudflare/workers-types':
optional: true
'@electric-sql/pglite':
optional: true
'@libsql/client':
optional: true
'@libsql/client-wasm':
optional: true
'@neondatabase/serverless':
optional: true
'@op-engineering/op-sqlite':
optional: true
'@opentelemetry/api':
optional: true
'@planetscale/database':
optional: true
'@prisma/client':
optional: true
'@tidbcloud/serverless':
optional: true
'@types/better-sqlite3':
optional: true
'@types/pg':
optional: true
'@types/sql.js':
optional: true
'@vercel/postgres':
optional: true
'@xata.io/client':
optional: true
better-sqlite3:
optional: true
bun-types:
optional: true
expo-sqlite:
optional: true
gel:
optional: true
knex:
optional: true
kysely:
optional: true
mysql2:
optional: true
pg:
optional: true
postgres:
optional: true
prisma:
optional: true
sql.js:
optional: true
sqlite3:
optional: true
drizzle-orm@0.45.1: drizzle-orm@0.45.1:
resolution: {integrity: sha512-Te0FOdKIistGNPMq2jscdqngBRfBpC8uMFVwqjf6gtTVJHIQ/dosgV/CLBU2N4ZJBsXL5savCba9b0YJskKdcA==} resolution: {integrity: sha512-Te0FOdKIistGNPMq2jscdqngBRfBpC8uMFVwqjf6gtTVJHIQ/dosgV/CLBU2N4ZJBsXL5savCba9b0YJskKdcA==}
peerDependencies: peerDependencies:
@@ -1919,11 +1820,6 @@ packages:
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
hasBin: true hasBin: true
nanoid@5.1.6:
resolution: {integrity: sha512-c7+7RQ+dMB5dPwwCp4ee1/iV/q2P6aK1mTZcfr1BTuVlyW9hJYiMPybJCcnBlQtuSmTIWNeazm/zqNoZSSElBg==}
engines: {node: ^18 || >=20}
hasBin: true
neotraverse@0.6.18: neotraverse@0.6.18:
resolution: {integrity: sha512-Z4SmBUweYa09+o6pG+eASabEpP6QkQ70yHj351pQoEXIs8uHbaU2DWVmzBANKgflPa47A50PtB2+NgRpQvr7vA==} resolution: {integrity: sha512-Z4SmBUweYa09+o6pG+eASabEpP6QkQ70yHj351pQoEXIs8uHbaU2DWVmzBANKgflPa47A50PtB2+NgRpQvr7vA==}
engines: {node: '>= 10'} engines: {node: '>= 10'}
@@ -2524,47 +2420,6 @@ snapshots:
'@astrojs/compiler@2.13.0': {} '@astrojs/compiler@2.13.0': {}
'@astrojs/db@0.18.3(@types/pg@8.16.0)(pg@8.16.3)':
dependencies:
'@libsql/client': 0.15.15
deep-diff: 1.0.2
drizzle-orm: 0.42.0(@libsql/client@0.15.15)(@types/pg@8.16.0)(pg@8.16.3)
nanoid: 5.1.6
piccolore: 0.1.3
prompts: 2.4.2
yargs-parser: 21.1.1
zod: 3.25.76
transitivePeerDependencies:
- '@aws-sdk/client-rds-data'
- '@cloudflare/workers-types'
- '@electric-sql/pglite'
- '@libsql/client-wasm'
- '@neondatabase/serverless'
- '@op-engineering/op-sqlite'
- '@opentelemetry/api'
- '@planetscale/database'
- '@prisma/client'
- '@tidbcloud/serverless'
- '@types/better-sqlite3'
- '@types/pg'
- '@types/sql.js'
- '@vercel/postgres'
- '@xata.io/client'
- better-sqlite3
- bufferutil
- bun-types
- expo-sqlite
- gel
- knex
- kysely
- mysql2
- pg
- postgres
- prisma
- sql.js
- sqlite3
- utf-8-validate
'@astrojs/internal-helpers@0.7.5': {} '@astrojs/internal-helpers@0.7.5': {}
'@astrojs/markdown-remark@6.3.10': '@astrojs/markdown-remark@6.3.10':
@@ -3000,10 +2855,12 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- bufferutil - bufferutil
- utf-8-validate - utf-8-validate
optional: true
'@libsql/core@0.15.15': '@libsql/core@0.15.15':
dependencies: dependencies:
js-base64: 3.7.8 js-base64: 3.7.8
optional: true
'@libsql/darwin-arm64@0.5.22': '@libsql/darwin-arm64@0.5.22':
optional: true optional: true
@@ -3020,8 +2877,10 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- bufferutil - bufferutil
- utf-8-validate - utf-8-validate
optional: true
'@libsql/isomorphic-fetch@0.3.1': {} '@libsql/isomorphic-fetch@0.3.1':
optional: true
'@libsql/isomorphic-ws@0.1.5': '@libsql/isomorphic-ws@0.1.5':
dependencies: dependencies:
@@ -3030,6 +2889,7 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- bufferutil - bufferutil
- utf-8-validate - utf-8-validate
optional: true
'@libsql/linux-arm-gnueabihf@0.5.22': '@libsql/linux-arm-gnueabihf@0.5.22':
optional: true optional: true
@@ -3052,7 +2912,8 @@ snapshots:
'@libsql/win32-x64-msvc@0.5.22': '@libsql/win32-x64-msvc@0.5.22':
optional: true optional: true
'@neon-rs/load@0.0.4': {} '@neon-rs/load@0.0.4':
optional: true
'@oslojs/encoding@1.1.0': {} '@oslojs/encoding@1.1.0': {}
@@ -3275,6 +3136,7 @@ snapshots:
'@types/ws@8.18.1': '@types/ws@8.18.1':
dependencies: dependencies:
'@types/node': 25.0.6 '@types/node': 25.0.6
optional: true
'@ungap/structured-clone@1.3.0': {} '@ungap/structured-clone@1.3.0': {}
@@ -3486,7 +3348,8 @@ snapshots:
dependencies: dependencies:
css-tree: 2.2.1 css-tree: 2.2.1
data-uri-to-buffer@4.0.1: {} data-uri-to-buffer@4.0.1:
optional: true
debug@4.4.3: debug@4.4.3:
dependencies: dependencies:
@@ -3496,8 +3359,6 @@ snapshots:
dependencies: dependencies:
character-entities: 2.0.2 character-entities: 2.0.2
deep-diff@1.0.2: {}
defu@6.1.4: {} defu@6.1.4: {}
depd@2.0.0: {} depd@2.0.0: {}
@@ -3506,7 +3367,8 @@ snapshots:
destr@2.0.5: {} destr@2.0.5: {}
detect-libc@2.0.2: {} detect-libc@2.0.2:
optional: true
detect-libc@2.1.2: {} detect-libc@2.1.2: {}
@@ -3553,12 +3415,6 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
drizzle-orm@0.42.0(@libsql/client@0.15.15)(@types/pg@8.16.0)(pg@8.16.3):
optionalDependencies:
'@libsql/client': 0.15.15
'@types/pg': 8.16.0
pg: 8.16.3
drizzle-orm@0.45.1(@libsql/client@0.15.15)(@types/pg@8.16.0)(pg@8.16.3): drizzle-orm@0.45.1(@libsql/client@0.15.15)(@types/pg@8.16.0)(pg@8.16.3):
optionalDependencies: optionalDependencies:
'@libsql/client': 0.15.15 '@libsql/client': 0.15.15
@@ -3702,6 +3558,7 @@ snapshots:
dependencies: dependencies:
node-domexception: 1.0.0 node-domexception: 1.0.0
web-streams-polyfill: 3.3.3 web-streams-polyfill: 3.3.3
optional: true
flattie@1.1.1: {} flattie@1.1.1: {}
@@ -3716,6 +3573,7 @@ snapshots:
formdata-polyfill@4.0.10: formdata-polyfill@4.0.10:
dependencies: dependencies:
fetch-blob: 3.2.0 fetch-blob: 3.2.0
optional: true
fresh@2.0.0: {} fresh@2.0.0: {}
@@ -3867,7 +3725,8 @@ snapshots:
jiti@2.6.1: {} jiti@2.6.1: {}
js-base64@3.7.8: {} js-base64@3.7.8:
optional: true
js-yaml@4.1.1: js-yaml@4.1.1:
dependencies: dependencies:
@@ -3889,6 +3748,7 @@ snapshots:
'@libsql/linux-x64-gnu': 0.5.22 '@libsql/linux-x64-gnu': 0.5.22
'@libsql/linux-x64-musl': 0.5.22 '@libsql/linux-x64-musl': 0.5.22
'@libsql/win32-x64-msvc': 0.5.22 '@libsql/win32-x64-msvc': 0.5.22
optional: true
lightningcss-android-arm64@1.30.2: lightningcss-android-arm64@1.30.2:
optional: true optional: true
@@ -4288,15 +4148,14 @@ snapshots:
nanoid@3.3.11: {} nanoid@3.3.11: {}
nanoid@5.1.6: {}
neotraverse@0.6.18: {} neotraverse@0.6.18: {}
nlcst-to-string@4.0.0: nlcst-to-string@4.0.0:
dependencies: dependencies:
'@types/nlcst': 2.0.3 '@types/nlcst': 2.0.3
node-domexception@1.0.0: {} node-domexception@1.0.0:
optional: true
node-fetch-native@1.6.7: {} node-fetch-native@1.6.7: {}
@@ -4305,6 +4164,7 @@ snapshots:
data-uri-to-buffer: 4.0.1 data-uri-to-buffer: 4.0.1
fetch-blob: 3.2.0 fetch-blob: 3.2.0
formdata-polyfill: 4.0.10 formdata-polyfill: 4.0.10
optional: true
node-mock-http@1.0.4: {} node-mock-http@1.0.4: {}
@@ -4423,7 +4283,8 @@ snapshots:
prismjs@1.30.0: {} prismjs@1.30.0: {}
promise-limit@2.7.0: {} promise-limit@2.7.0:
optional: true
prompts@2.4.2: prompts@2.4.2:
dependencies: dependencies:
@@ -4856,7 +4717,8 @@ snapshots:
web-namespaces@2.0.1: {} web-namespaces@2.0.1: {}
web-streams-polyfill@3.3.3: {} web-streams-polyfill@3.3.3:
optional: true
which-pm-runs@1.1.0: {} which-pm-runs@1.1.0: {}
@@ -4870,7 +4732,8 @@ snapshots:
string-width: 7.2.0 string-width: 7.2.0
strip-ansi: 7.1.2 strip-ansi: 7.1.2
ws@8.19.0: {} ws@8.19.0:
optional: true
xtend@4.0.2: {} xtend@4.0.2: {}

View File

@@ -1,6 +1,6 @@
import { integer, pgTable, varchar, date, json } from "drizzle-orm/pg-core"; import { integer, pgTable, varchar, date, json } from "drizzle-orm/pg-core";
export const usersTable = pgTable("users", { export const entryTable = pgTable("entries", {
id: integer().primaryKey().generatedAlwaysAsIdentity(), id: integer().primaryKey().generatedAlwaysAsIdentity(),
data: date().defaultNow(), data: date().defaultNow(),
location: json(), location: json(),

View File

@@ -1,7 +1,8 @@
import { db, Entry } from "astro:db"; import { db } from "../../../utils/db"
import { entryTable } from "../../../db/schema"
export async function GET() { export async function GET() {
const entries = await db.select().from(Entry) const entries = await db.select().from(entryTable)
return new Response(JSON.stringify(entries)) return new Response(JSON.stringify(entries))
} }

View File

@@ -1,4 +1,6 @@
import { db, Entry } from "astro:db"; import { db } from "../../../utils/db"
import { entryTable } from "../../../db/schema"
import { httpResponse } from "../../../utils/response";
export async function POST({ request }) { export async function POST({ request }) {
@@ -6,18 +8,15 @@ export async function POST({ request }) {
try { try {
const body = await request.json(); const body = await request.json();
const entry: typeof entryTable.$inferInsert = body
await db.insert(Entry).values(body) await db.insert(entryTable).values(entry)
} catch(e) { } catch(e) {
return new Response( return httpResponse({ "error": `Malformed JSON (${e})` }, 400)
JSON.stringify({
"error": `Malformed JSON (${e})`
}),{ status: 400 }
)
} }
return new Response(null, { status: 200 }); return httpResponse(null, 200);
} }
return new Response(null, { status: 400 }); return httpResponse(null, 400);
} }

View File

@@ -1,4 +1,4 @@
import 'dotenv/config'; import 'dotenv/config';
import { drizzle } from 'drizzle-orm/node-postgres'; import { drizzle } from 'drizzle-orm/node-postgres';
const db = drizzle(process.env.DATABASE_URL!); export const db = drizzle(process.env.DATABASE_URL!);

5
src/utils/response.ts Normal file
View File

@@ -0,0 +1,5 @@
export function httpResponse(data: object | null, code: number) {
return new Response(JSON.stringify(data), {
status: code
})
}