| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- import { DB } from "$sqlite/mod.ts";
- function prepareDB(tableName: string) {
- const db = new DB("data/postdown.db");
- switch (tableName) {
- case "User":
- db.execute(`
- CREATE TABLE IF NOT EXISTS user (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- name VARCHAR(128),
- email VARCHAR(128),
- password TEXT,
- created DATETIME DEFAULT CURRENT_TIMESTAMP,
- updated DATETIME,
- UNIQUE(name)
- )
- `);
- break;
- case "Token":
- db.execute(`
- CREATE TABLE IF NOT EXISTS token (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- user_id INTEGER,
- token VARCHAR(128),
- created DATETIME DEFAULT CURRENT_TIMESTAMP,
- updated DATETIME
- )
- `);
- break;
- case "Post":
- db.execute(`
- CREATE TABLE IF NOT EXISTS post (
- id VARCHAR(64) PRIMARY KEY,
- user_id INTEGER,
- title VARCHAR(256),
- content TEXT,
- shared BOOLEAN,
- created DATETIME DEFAULT CURRENT_TIMESTAMP,
- updated DATETIME
- )
- `);
- }
- return db;
- }
- export function find(
- tableName: string,
- queryObject: { [key: string]: string | number | boolean },
- targetKeys: string[] = [],
- limit?: number
- ) {
- const db = prepareDB(tableName);
- const findQuery = db.prepareQuery(
- `SELECT ${
- targetKeys.length > 0 ? targetKeys.join(", ") : "*"
- } FROM ${tableName.toLowerCase()} WHERE ${Object.keys(queryObject)
- .map((queryKey) => `${queryKey} = :${queryKey}`)
- .join(" AND ")} ORDER BY updated DESC ${limit ? ` LIMIT ${limit}` : ""}`
- );
- try {
- return findQuery.all(queryObject);
- } catch (e) {
- console.error("Find error:", e);
- return [];
- } finally {
- findQuery.finalize();
- db.close();
- }
- }
- export function insert(
- tableName: string,
- userInsertObject: { [key: string]: string | number | boolean }
- ) {
- const db = prepareDB(tableName);
- const insertObject = {
- ...userInsertObject,
- updated: new Date().toISOString().slice(0, 19).replace("T", " "),
- };
- const insertQuery = db.prepareQuery(
- `INSERT INTO ${tableName.toLowerCase()} (${Object.keys(insertObject).join(
- ", "
- )}) VALUES (${Object.keys(insertObject)
- .map((key) => `:${key}`)
- .join(", ")})`
- );
- try {
- insertQuery.all(insertObject);
- return find(tableName, userInsertObject, ["id"], 1);
- } catch (e) {
- console.error("Insert error:", e);
- return [];
- } finally {
- insertQuery.finalize();
- db.close();
- }
- }
- export function update(
- tableName: string,
- id: number | string,
- userUpdateObject: { [key: string]: string | number | boolean }
- ) {
- const db = prepareDB(tableName);
- const updateObject = {
- ...userUpdateObject,
- updated: new Date().toISOString().slice(0, 19).replace("T", " "),
- };
- const updateQuery = db.prepareQuery(
- `UPDATE ${tableName.toLowerCase()} SET ${Object.keys(updateObject)
- .map((updateKey) => `${updateKey} = :${updateKey}`)
- .join(", ")} WHERE id = :id`
- );
- try {
- updateQuery.all({ ...updateObject, id });
- return find(tableName, userUpdateObject, ["id"], 1);
- } catch (e) {
- console.error("Insert error:", e);
- return [];
- } finally {
- updateQuery.finalize();
- db.close();
- }
- }
- export function del(
- tableName: string,
- queryObject: { [key: string]: string | number | boolean }
- ) {
- const db = prepareDB(tableName);
- const deleteQuery = db.prepareQuery(
- `DELETE FROM ${tableName.toLowerCase()} WHERE ${Object.keys(queryObject)
- .map((queryKey) => `${queryKey} = :${queryKey}`)
- .join(" AND ")}`
- );
- try {
- return deleteQuery.all(queryObject);
- } catch (e) {
- console.error("Insert error:", e);
- return [];
- } finally {
- deleteQuery.finalize();
- db.close();
- }
- }
|