add more functions for database, but not finish yet

This commit is contained in:
SikongJueluo 2025-03-23 17:25:47 +08:00
parent 12caccd2ca
commit 482d359c98
No known key found for this signature in database
6 changed files with 91 additions and 19 deletions

View File

@ -11,6 +11,7 @@
"pinia": "^3.0.1",
"trpc-bun-adapter": "^1.2.2",
"ts-log": "^2.2.7",
"ts-results-es": "^5.0.1",
"vue": "^3.5.13",
"vue-router": "4",
"zod": "^3.24.2",
@ -527,6 +528,8 @@
"ts-log": ["ts-log@2.2.7", "", {}, "sha512-320x5Ggei84AxzlXp91QkIGSw5wgaLT6GeAH0KsqDmRZdVWW2OiSeVvElVoatk3f7nicwXlElXsoFkARiGE2yg=="],
"ts-results-es": ["ts-results-es@5.0.1", "", {}, "sha512-HjX/7HxQe2bXkbp8pHTjy4Ir9eHIDnDDsLDphhGqy6I9iZ/vD4QXWEIlrVRZsEX+kS2jIiiF/mnl0nKnPTiYFw=="],
"typescript": ["typescript@5.7.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw=="],
"undici-types": ["undici-types@6.20.0", "", {}, "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="],

View File

@ -20,6 +20,7 @@
"pinia": "^3.0.1",
"trpc-bun-adapter": "^1.2.2",
"ts-log": "^2.2.7",
"ts-results-es": "^5.0.1",
"vue": "^3.5.13",
"vue-router": "4",
"zod": "^3.24.2"

10
server/database.test.ts Normal file
View File

@ -0,0 +1,10 @@
import { test, expect } from "bun:test"
import * as db from "./database.ts"
test("DataBase", () => {
const allTables = db.allTables()
expect(allTables).toBeArray()
expect(allTables).toEqual(["Users", "Boards"])
expect(db.boardsNum()).toBe(0)
})

View File

@ -1,5 +1,6 @@
import { Database } from "bun:sqlite";
import { isNumber, isString } from "lodash";
import _ from "lodash";
import { Ok, Err, Result, None, Some, Option } from "ts-results-es";
import { z } from "zod";
const db = new Database("lab.sqlite", { strict: true })
@ -7,7 +8,7 @@ initDB();
const boardSchema = z.object({
id: z.number(),
id: z.number().nonnegative(),
name: z.string(),
room: z.string(),
ipv4: z.string().ip({ version: "v4" }),
@ -26,7 +27,7 @@ export function isBoard(obj: any): obj is Board {
}
const userSchema = z.object({
id: z.number(),
id: z.number().nonnegative(),
name: z.string(),
password: z.string(),
boardID: z.number(),
@ -90,6 +91,61 @@ export function tableColumnName(table: string): Array<string> {
return columnName
}
export function addBoard(board: Board): Result<Board, "Wrong params type"> {
if (!isBoard(board)) {
return new Err("Wrong params type")
}
if (board.id == 0) {
const idNum = boardsNum()
board.id = idNum
} else {
}
const query = db.query(`
INSERT INTO Boards VALUES
(${board.id},
'${board.name},
'${board.room}',
'${board.ipv4}',
'${typeof board.ipv6 === "undefined" ? "NULL" : board.ipv6}',
${board.port});
`)
query.run()
return Ok(board)
}
export function boardsNum(): number {
const query = db.query(`SELECT COUNT(*) FROM Boards`)
return query.values()[0][0] as number
}
export function deleteBoard(name?: string): Result<Board, "Wrong Type" | "No such Board">
export function deleteBoard(id?: number): Result<Board, "Wrong Type" | "No such Board">
export function deleteBoard(board?: Board): Result<Board, "Wrong Type" | "No such Board">
export function deleteBoard(arg: any): Result<Board, "Wrong Type" | "No such Board"> {
let retBoard
let condition: string
if (isBoard(arg)) {
retBoard = arg
} else if (_.isNumber(arg)) {
retBoard = findBoard(arg)
} else if (_.isString(arg)) {
retBoard = findBoard(arg)
} else {
return new Err("Wrong Type")
}
const query = db.query(`DELETE FROM Boards WHERE id=${arg.id}`)
return new Ok(retBoard)
}
export function allBoards() {
const query = db.query(`SELECT * FROM Boards`)
const boards = query.all()
@ -98,26 +154,33 @@ export function allBoards() {
return boards
}
export function findBoard(id?: number | string): Board {
export function findBoard(name?: string): Result<Option<Board>, "Wrong type">
export function findBoard(id?: number): Result<Option<Board>, "Wrong type">
export function findBoard(arg: any): Result<Option<Board>, "Wrong type"> {
let condition: string
if (isNumber(id)) {
condition = `id=${id}`
} else if (isString(id)) {
condition = `name='${id}'`
if (_.isNumber(arg)) {
condition = `arg=${arg}`
} else if (_.isString(arg)) {
condition = `name='${arg}'`
} else {
throw new Error("Failure: Wrong type when find board")
return new Err("Wrong type")
}
const query = db.query(`SELECT * FROM Boards WHERE ${condition}`)
const spRet = boardSchema.safeParse(query.get())
if (spRet.success) {
return spRet.data
return new Ok(Some(spRet.data))
} else {
throw new Error(`Not Found ${id} FPGA Board`)
return new Ok(None)
}
}
export function findUser(name: string): User {
const query = db.query(`SELECT * FROM Users WHERE name='${name}'`)

View File

@ -1,5 +1,5 @@
import type { Board } from "./database";
import { boardSchema, findBoard } from "./database";
import { findBoard, isBoard } from "./database";
import { numberToBytes } from "./common";
import { z } from "zod";
import _ from "lodash";
@ -124,13 +124,13 @@ export class Equipment {
constructor(name?: string)
constructor(board?: Board)
constructor(arg1: any) {
if (boardSchema.safeParse(arg1).success) {
if (isBoard(arg1)) {
this.board = arg1
} else if (typeof arg1 === "string") {
try {
const board = findBoard(arg1)
this.board = boardSchema.parse(board)
this.board = board
} catch (error) {
throw new Error("Equipment Construction Failure")
}

View File

@ -1,5 +0,0 @@
import { allTables } from "./database";
const tables = allTables();
console.log(tables);