add more functions for database, but not finish yet
This commit is contained in:
10
server/database.test.ts
Normal file
10
server/database.test.ts
Normal 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)
|
||||
|
||||
})
|
@@ -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}'`)
|
||||
|
||||
|
@@ -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")
|
||||
}
|
||||
|
@@ -1,5 +0,0 @@
|
||||
import { allTables } from "./database";
|
||||
|
||||
|
||||
const tables = allTables();
|
||||
console.log(tables);
|
Reference in New Issue
Block a user