Set DB interface
This commit is contained in:
		
							parent
							
								
									0745298257
								
							
						
					
					
						commit
						1d358a9ea9
					
				
					 6 changed files with 58 additions and 14 deletions
				
			
		
							
								
								
									
										2
									
								
								justfile
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								justfile
									
										
									
									
									
								
							|  | @ -2,3 +2,5 @@ fake: | |||
|   POSTGRES_URL="postgres://test:test@192.168.50.14:5432/posts" bun run ./tools/gen_fake.ts | ||||
| fetch_post: | ||||
|   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
									
								
							
							
						
						
									
										15
									
								
								src/app/new_post/route.ts
									
										
									
									
									
										Normal 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 }) | ||||
| } | ||||
							
								
								
									
										6
									
								
								src/app/new_user/route.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								src/app/new_user/route.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,6 @@ | |||
| import { NextRequest } from "next/server"; | ||||
| 
 | ||||
| export function Post(req: NextRequest) { | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										30
									
								
								src/db.ts
									
										
									
									
									
								
							
							
						
						
									
										30
									
								
								src/db.ts
									
										
									
									
									
								
							|  | @ -1,4 +1,4 @@ | |||
| import { env, ReservedSQL, sql } from 'bun' | ||||
| import { env, ReservedSQL, CryptoHasher, sql } from 'bun' | ||||
| import { MIMEType } from 'util'; | ||||
| 
 | ||||
| export enum MultiMediaType { | ||||
|  | @ -39,20 +39,40 @@ const SQLPostCast2Post = (obj: SQLPostCast) => { | |||
|   return x | ||||
| } | ||||
| 
 | ||||
| export interface Cursor { | ||||
|   hash: string, | ||||
|   post_time: string, | ||||
| 
 | ||||
| export interface NewPost { | ||||
|   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 { | ||||
|   conn: Promise<ReservedSQL>; | ||||
| 
 | ||||
|   constructor() { | ||||
|     this.conn = sql.reserve(); | ||||
|   } | ||||
| 
 | ||||
|   async init() { | ||||
|     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  | ||||
|         LEFT JOIN niming.images ON niming.images.hash=niming.posts.hash ORDER BY niming.posts.post_time DESC, niming.posts.hash ASC` | ||||
|     }).catch(err => { | ||||
|  |  | |||
							
								
								
									
										3
									
								
								tools/create_schema.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								tools/create_schema.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | |||
| import { init_db } from "@/db"; | ||||
| 
 | ||||
| init_db() | ||||
|  | @ -1,15 +1,13 @@ | |||
| import { CryptoHasher, sql } from "bun"; | ||||
| import { retrieve_post, setup_func } from "@/db"; | ||||
| import { insert_post, NewPost } from '@/db'; | ||||
| 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 hash = new CryptoHasher("sha256") | ||||
|   hash.update(text) | ||||
|   hash.update(Date.now().toString()) | ||||
|   let dg = hash.digest("hex") | ||||
|   console.log(dg) | ||||
|   const _ = await sql`INSERT INTO niming.posts (hash, post,post_time) VALUES (${dg},${text}, now())` | ||||
|   let x: NewPost = { image: [], content: text }; | ||||
|   let [p_id, p_hash] = await insert_post(x) | ||||
|   console.log(p_id, p_hash) | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue