|
|
|
|
@@ -2,10 +2,23 @@ import { Database, type Changes } from "bun:sqlite";
|
|
|
|
|
import _ from "lodash";
|
|
|
|
|
import { Ok, Err, Result, None, Some, Option } from "ts-results-es";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
import { fun } from "./common";
|
|
|
|
|
|
|
|
|
|
const db = new Database("lab.sqlite", { strict: true })
|
|
|
|
|
initDB(db);
|
|
|
|
|
|
|
|
|
|
// Error Type
|
|
|
|
|
export const BOARD_ERR_WRONG_TYPE = "Wrong type"
|
|
|
|
|
export const BOARD_ERR_NO_BOARDS = "No such Boards"
|
|
|
|
|
export const BOARD_ERR_ID_CONFLICT = "ID conflict"
|
|
|
|
|
export const BOARD_ERR_NAME_CONFLICT = "Name conflict in one room"
|
|
|
|
|
|
|
|
|
|
export type BoardErrorType = (
|
|
|
|
|
typeof BOARD_ERR_WRONG_TYPE |
|
|
|
|
|
typeof BOARD_ERR_NO_BOARDS |
|
|
|
|
|
typeof BOARD_ERR_ID_CONFLICT |
|
|
|
|
|
typeof BOARD_ERR_NAME_CONFLICT
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const boardSchema = z.object({
|
|
|
|
|
id: z.number().nonnegative(),
|
|
|
|
|
@@ -21,11 +34,20 @@ const boardSchema = z.object({
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export type Board = z.infer<typeof boardSchema>
|
|
|
|
|
export type BoardColumn = keyof Board
|
|
|
|
|
|
|
|
|
|
export function isBoard(obj: any): obj is Board {
|
|
|
|
|
return boardSchema.safeParse(obj).success
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isBoardArray(obj: any): obj is Array<Board> {
|
|
|
|
|
return boardSchema.array().safeParse(obj).success
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isBoardColumn(obj: any): obj is BoardColumn {
|
|
|
|
|
return boardSchema.keyof().safeParse(obj).success
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const userSchema = z.object({
|
|
|
|
|
id: z.number().nonnegative(),
|
|
|
|
|
name: z.string(),
|
|
|
|
|
@@ -36,11 +58,19 @@ const userSchema = z.object({
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export type User = z.infer<typeof userSchema>
|
|
|
|
|
export type UserColumn = keyof User
|
|
|
|
|
|
|
|
|
|
export function isUser(obj: any): obj is User {
|
|
|
|
|
return userSchema.safeParse(obj).success
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isUserArray(obj: any): obj is Array<User> {
|
|
|
|
|
return userSchema.array().safeParse(obj).success
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isUserColumn(obj: any): obj is UserColumn {
|
|
|
|
|
return userSchema.keyof().safeParse(obj).success
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function initDB(db: Database) {
|
|
|
|
|
const tables = allTables()
|
|
|
|
|
@@ -93,9 +123,9 @@ export function tableColumnName(table: string): Array<string> {
|
|
|
|
|
|
|
|
|
|
export namespace BoardTable {
|
|
|
|
|
|
|
|
|
|
export function add(board: Board): Result<Changes, "Wrong type" | "ID conflict" | "Name conflict in one room"> {
|
|
|
|
|
export function add(board: Board): Result<Changes, BoardErrorType> {
|
|
|
|
|
if (!isBoard(board)) {
|
|
|
|
|
return new Err("Wrong type")
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure no id conflict
|
|
|
|
|
@@ -105,9 +135,9 @@ export namespace BoardTable {
|
|
|
|
|
} else {
|
|
|
|
|
const retID = includes(board.id)
|
|
|
|
|
if (retID.isOk()) {
|
|
|
|
|
if (retID.value) return new Err("ID conflict")
|
|
|
|
|
if (retID.value) return new Err(BOARD_ERR_ID_CONFLICT)
|
|
|
|
|
} else {
|
|
|
|
|
return new Err("Wrong type")
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -116,7 +146,7 @@ export namespace BoardTable {
|
|
|
|
|
const retName = includes(board.name, board.room)
|
|
|
|
|
if (retName.isOk()) {
|
|
|
|
|
if (retName.value) {
|
|
|
|
|
return new Err("Name conflict in one room")
|
|
|
|
|
return new Err(BOARD_ERR_NAME_CONFLICT)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return new Err("Wrong type")
|
|
|
|
|
@@ -126,7 +156,7 @@ export namespace BoardTable {
|
|
|
|
|
const query = db.query(`
|
|
|
|
|
INSERT INTO Boards VALUES
|
|
|
|
|
(${board.id},
|
|
|
|
|
'${board.name},
|
|
|
|
|
'${board.name}',
|
|
|
|
|
'${board.room}',
|
|
|
|
|
'${board.ipv4}',
|
|
|
|
|
'${_.isUndefined(board.ipv6) ? "NULL" : board.ipv6}',
|
|
|
|
|
@@ -136,12 +166,14 @@ export namespace BoardTable {
|
|
|
|
|
return Ok(query.run())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function addFromArray(array: Array<Board>): Result<Array<Changes>, "Wrong type"> {
|
|
|
|
|
export function addFromArray(array: Array<Board>)
|
|
|
|
|
: Result<Array<Changes>, BoardErrorType> {
|
|
|
|
|
|
|
|
|
|
let arrayChanges: Array<Changes> = []
|
|
|
|
|
for (const item of array) {
|
|
|
|
|
const ret = add(item)
|
|
|
|
|
if (ret.isErr()) {
|
|
|
|
|
return new Err("Wrong type")
|
|
|
|
|
return ret
|
|
|
|
|
} else {
|
|
|
|
|
arrayChanges.push(ret.value)
|
|
|
|
|
}
|
|
|
|
|
@@ -150,12 +182,16 @@ export namespace BoardTable {
|
|
|
|
|
return new Ok(arrayChanges)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function all() {
|
|
|
|
|
export function all(): Option<Array<Board>> {
|
|
|
|
|
const query = db.query(`SELECT * FROM Boards`)
|
|
|
|
|
const boards = query.all()
|
|
|
|
|
|
|
|
|
|
const ret = query.all()
|
|
|
|
|
query.finalize()
|
|
|
|
|
return boards
|
|
|
|
|
|
|
|
|
|
if (isBoardArray(ret)) {
|
|
|
|
|
return Some(ret)
|
|
|
|
|
} else {
|
|
|
|
|
return None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function countAll(): number {
|
|
|
|
|
@@ -173,10 +209,10 @@ export namespace BoardTable {
|
|
|
|
|
return query.values()[0][0] as number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function remove(name: string, room: string): Result<Board, "Wrong Type" | "No such Board">
|
|
|
|
|
export function remove(id: number): Result<Board, "Wrong Type" | "No such Board">
|
|
|
|
|
export function remove(board: Board): Result<Board, "Wrong Type" | "No such Board">
|
|
|
|
|
export function remove(arg1: any, arg2?: any): Result<Board, "Wrong Type" | "No such Board"> {
|
|
|
|
|
export function remove(name: string, room: string): Result<Board, BoardErrorType>
|
|
|
|
|
export function remove(id: number): Result<Board, BoardErrorType>
|
|
|
|
|
export function remove(board: Board): Result<Board, BoardErrorType>
|
|
|
|
|
export function remove(arg1: any, arg2?: any): Result<Board, BoardErrorType> {
|
|
|
|
|
let retBoard
|
|
|
|
|
let condition: string
|
|
|
|
|
if (isBoard(arg1)) {
|
|
|
|
|
@@ -189,7 +225,7 @@ export namespace BoardTable {
|
|
|
|
|
retBoard = _.cloneDeep(retBoard.value.value)
|
|
|
|
|
condition = `id=${arg1}`
|
|
|
|
|
} else {
|
|
|
|
|
return new Err("No such Board")
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (_.isString(arg1) && _.isString(arg2)) {
|
|
|
|
|
@@ -198,11 +234,11 @@ export namespace BoardTable {
|
|
|
|
|
retBoard = _.cloneDeep(retBoard.value.value)
|
|
|
|
|
condition = `name=${arg1}`
|
|
|
|
|
} else {
|
|
|
|
|
return new Err("No such Board")
|
|
|
|
|
return new Err(BOARD_ERR_NO_BOARDS)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return new Err("Wrong Type")
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const query = db.query(`DELETE FROM Boards WHERE ${condition}`)
|
|
|
|
|
@@ -211,9 +247,90 @@ export namespace BoardTable {
|
|
|
|
|
return new Ok(retBoard)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function find(name: string, room: string): Result<Option<Board>, "Wrong type">
|
|
|
|
|
export function find(id: number): Result<Option<Board>, "Wrong type">
|
|
|
|
|
export function find(arg1: any, arg2?: any): Result<Option<Board>, "Wrong type"> {
|
|
|
|
|
export function removeAll(): Option<Array<Board>> {
|
|
|
|
|
const array = all()
|
|
|
|
|
const query = db.query(`DELETE FROM Boards`)
|
|
|
|
|
query.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (array.isSome()) {
|
|
|
|
|
return new Some(array.value)
|
|
|
|
|
} else {
|
|
|
|
|
return None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function removeByCondition(condition: string): Result<Array<Board>, BoardErrorType> {
|
|
|
|
|
const rsArr = findByCondition(condition)
|
|
|
|
|
if (rsArr.isNone()) {
|
|
|
|
|
return new Err(BOARD_ERR_NO_BOARDS)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const query = db.query(`DELETE FROM Boards WHERE ${condition}`)
|
|
|
|
|
query.run()
|
|
|
|
|
|
|
|
|
|
return new Ok(rsArr.value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function removeByValue(columnName: string, val: string | Array<string>)
|
|
|
|
|
: Result<Array<Board>, BoardErrorType> {
|
|
|
|
|
|
|
|
|
|
if (!isBoardColumn(columnName)) {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let condition: string
|
|
|
|
|
if (_.isString(val)) {
|
|
|
|
|
const retCond = fun.sqlConditionFromString(columnName, val, "OR")
|
|
|
|
|
if (retCond.isSome()) {
|
|
|
|
|
condition = retCond.value
|
|
|
|
|
} else {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
} else if (fun.isStringArray(val)) {
|
|
|
|
|
const retCond = fun.sqlConditionFromArray(columnName, val, "OR")
|
|
|
|
|
if (retCond.isSome()) {
|
|
|
|
|
condition = retCond.value
|
|
|
|
|
} else {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return removeByCondition(condition)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function removeByName(name: string | Array<string>)
|
|
|
|
|
: Result<Array<Board>, BoardErrorType> {
|
|
|
|
|
return removeByValue("name", name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function removeByRoom(room: string | Array<string>)
|
|
|
|
|
: Result<Array<Board>, BoardErrorType> {
|
|
|
|
|
|
|
|
|
|
return removeByValue("room", room)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function rooms(): Option<Array<string>> {
|
|
|
|
|
const query = db.query(`SELECT DISTINCT room FROM Boards`)
|
|
|
|
|
let rooms: Array<string> = []
|
|
|
|
|
const retVal = query.values()
|
|
|
|
|
|
|
|
|
|
if (retVal.length > 0) {
|
|
|
|
|
for (const item of retVal) {
|
|
|
|
|
rooms.push(item[0] as string)
|
|
|
|
|
}
|
|
|
|
|
return new Some(rooms)
|
|
|
|
|
} else {
|
|
|
|
|
return None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function find(name: string, room: string): Result<Option<Board>, BoardErrorType>
|
|
|
|
|
export function find(id: number): Result<Option<Board>, BoardErrorType>
|
|
|
|
|
export function find(arg1: any, arg2?: any): Result<Option<Board>, BoardErrorType> {
|
|
|
|
|
let condition: string
|
|
|
|
|
if (_.isNumber(arg1)) {
|
|
|
|
|
condition = `id=${arg1}`
|
|
|
|
|
@@ -222,7 +339,7 @@ export namespace BoardTable {
|
|
|
|
|
condition = `name='${arg1}' AND room='${arg2}'`
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return new Err("Wrong type")
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const query = db.query(`SELECT * FROM Boards WHERE ${condition}`)
|
|
|
|
|
@@ -236,9 +353,71 @@ export namespace BoardTable {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function includes(name: string, room?: string): Result<boolean, "Wrong type">
|
|
|
|
|
export function includes(id: number): Result<boolean, "Wrong type">
|
|
|
|
|
export function includes(arg1: any, arg2?: any): Result<boolean, "Wrong type"> {
|
|
|
|
|
export function findByName(name: string | Array<string>): Result<Option<Array<Board>>, BoardErrorType> {
|
|
|
|
|
let condition: string
|
|
|
|
|
if (_.isString(name)) {
|
|
|
|
|
const retCond = fun.sqlConditionFromString("name", name, "OR")
|
|
|
|
|
if (retCond.isSome()) {
|
|
|
|
|
condition = retCond.value
|
|
|
|
|
} else {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
} else if (fun.isStringArray(name)) {
|
|
|
|
|
const retCond = fun.sqlConditionFromArray("name", name, "OR")
|
|
|
|
|
if (retCond.isSome()) {
|
|
|
|
|
condition = retCond.value
|
|
|
|
|
} else {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Ok(findByCondition(condition))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function findByCondition(condition: string): Option<Array<Board>> {
|
|
|
|
|
const query = db.query(`SELECT * FROM Boards WHERE ${condition}`)
|
|
|
|
|
const ret = query.all()
|
|
|
|
|
if (isBoardArray(ret)) {
|
|
|
|
|
return new Some(ret)
|
|
|
|
|
} else {
|
|
|
|
|
return None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function findByValue(columnName: string, val: string | Array<string>)
|
|
|
|
|
: Result<Option<Array<Board>>, BoardErrorType> {
|
|
|
|
|
|
|
|
|
|
if (!isBoardColumn(columnName)) {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let condition: string
|
|
|
|
|
if (_.isString(name)) {
|
|
|
|
|
const retCond = fun.sqlConditionFromString("name", name, "OR")
|
|
|
|
|
if (retCond.isSome()) {
|
|
|
|
|
condition = retCond.value
|
|
|
|
|
} else {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
} else if (fun.isStringArray(name)) {
|
|
|
|
|
const retCond = fun.sqlConditionFromArray("name", name, "OR")
|
|
|
|
|
if (retCond.isSome()) {
|
|
|
|
|
condition = retCond.value
|
|
|
|
|
} else {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Ok(findByCondition(condition))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function includes(name: string, room?: string): Result<boolean, BoardErrorType>
|
|
|
|
|
export function includes(id: number): Result<boolean, BoardErrorType>
|
|
|
|
|
export function includes(arg1: any, arg2?: any): Result<boolean, BoardErrorType> {
|
|
|
|
|
let condition: string
|
|
|
|
|
if (_.isUndefined(arg2)) {
|
|
|
|
|
if (_.isNumber(arg1)) {
|
|
|
|
|
@@ -248,13 +427,13 @@ export namespace BoardTable {
|
|
|
|
|
condition = `name='${arg1}'`
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return new Err("Wrong type")
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (_.isString(arg1) && _.isString(arg2)) {
|
|
|
|
|
condition = `name='${arg1} AND room=${arg2}'`
|
|
|
|
|
} else {
|
|
|
|
|
return new Err("Wrong type")
|
|
|
|
|
return new Err(BOARD_ERR_WRONG_TYPE)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|