Set DB interface

This commit is contained in:
jasinco 2025-04-27 17:14:16 +08:00
parent 0745298257
commit 1d358a9ea9
6 changed files with 58 additions and 14 deletions

View file

@ -2,3 +2,5 @@ fake:
POSTGRES_URL="postgres://test:test@192.168.50.14:5432/posts" bun run ./tools/gen_fake.ts POSTGRES_URL="postgres://test:test@192.168.50.14:5432/posts" bun run ./tools/gen_fake.ts
fetch_post: fetch_post:
POSTGRES_URL="postgres://test:test@192.168.50.14:5432/posts" bun run ./tools/post_db.ts POSTGRES_URL="postgres://test:test@192.168.50.14:5432/posts" bun run ./tools/post_db.ts
create_schema:
POSTGRES_URL="postgres://test:test@192.168.50.14:5432/posts" bun run ./tools/create_schema.ts

15
src/app/new_post/route.ts Normal file
View file

@ -0,0 +1,15 @@
import { insert_post, NewPost } from "@/db";
import { Blob } from "buffer";
import type { NextRequest } from "next/server";
export async function POST(req: NextRequest) {
let form = await req.formData();
let text = form.get("content")?.toString()
if (!text) {
return Response.error()
}
let request: NewPost = { content: text, image: [] };
let [id, hash] = await insert_post(request);
return Response.json({ id: id, hash: hash })
}

View file

@ -0,0 +1,6 @@
import { NextRequest } from "next/server";
export function Post(req: NextRequest) {
}

View file

@ -1,4 +1,4 @@
import { env, ReservedSQL, sql } from 'bun' import { env, ReservedSQL, CryptoHasher, sql } from 'bun'
import { MIMEType } from 'util'; import { MIMEType } from 'util';
export enum MultiMediaType { export enum MultiMediaType {
@ -39,20 +39,40 @@ const SQLPostCast2Post = (obj: SQLPostCast) => {
return x return x
} }
export interface Cursor {
hash: string, export interface NewPost {
post_time: string, image: Blob[]
content: string,
} }
export async function insert_post(post: NewPost): Promise<[number, string]> {
let post_hash = new CryptoHasher("sha256");
post_hash.update(post.content);
post_hash.update(Date.now().toString())
let populated = post_hash.digest("base64");
let [{ id, hash }] = await sql`INSERT INTO niming.posts (post, hash) VALUES (${post.content}, ${populated}) RETURNING id, hash`
return [id, hash]
}
export async function init_db() {
await sql.begin(async sql => {
await sql`CREATE SCHEMA niming`
await sql`CREATE TABLE niming.posts (id SERIAL PRIMARY KEY, hash char(44) UNIQUE, post VARCHAR(500), post_time TIMESTAMPTZ DEFAULT now())`
await sql`CREATE TABLE niming.images (id INTEGER PRIMARY KEY REFERENCES niming.posts (id), fileid integer[] )`
})
}
export class PostFetcher { export class PostFetcher {
conn: Promise<ReservedSQL>; conn: Promise<ReservedSQL>;
constructor() { constructor() {
this.conn = sql.reserve(); this.conn = sql.reserve();
} }
async init() { async init() {
await this.conn.then(async e => { await this.conn.then(async e => {
await e`DECLARE post_ptr CURSOR WITH HOLD FOR SELECT niming.posts.*, niming.images.file_sequence await e`DECLARE post_ptr CURSOR WITH HOLD FOR SELECT niming.posts.*, niming.images.fileid
AS images FROM niming.posts AS images FROM niming.posts
LEFT JOIN niming.images ON niming.images.hash=niming.posts.hash ORDER BY niming.posts.post_time DESC, niming.posts.hash ASC` LEFT JOIN niming.images ON niming.images.hash=niming.posts.hash ORDER BY niming.posts.post_time DESC, niming.posts.hash ASC`
}).catch(err => { }).catch(err => {

3
tools/create_schema.ts Normal file
View file

@ -0,0 +1,3 @@
import { init_db } from "@/db";
init_db()

View file

@ -1,15 +1,13 @@
import { CryptoHasher, sql } from "bun"; import { insert_post, NewPost } from '@/db';
import { retrieve_post, setup_func } from "@/db";
import { faker } from '@faker-js/faker'; import { faker } from '@faker-js/faker';
for (let i = 0; i < 100; i++) {
for (let i = 0; i < 100; i++) {
let text = faker.string.alpha(20); let text = faker.string.alpha(20);
let hash = new CryptoHasher("sha256") let x: NewPost = { image: [], content: text };
hash.update(text) let [p_id, p_hash] = await insert_post(x)
hash.update(Date.now().toString()) console.log(p_id, p_hash)
let dg = hash.digest("hex")
console.log(dg)
const _ = await sql`INSERT INTO niming.posts (hash, post,post_time) VALUES (${dg},${text}, now())`
} }