Ver código fonte

Change database path

jerryliao 1 ano atrás
pai
commit
877c25c4f5
1 arquivos alterados com 29 adições e 19 exclusões
  1. 29 19
      utils/db.ts

+ 29 - 19
utils/db.ts

@@ -1,7 +1,7 @@
 import { DB } from "$sqlite/mod.ts";
 
 function prepareDB(tableName: string) {
-  const db = new DB("data/postdown.db");
+  const db = new DB("/data/postdown.db");
   switch (tableName) {
     case "User":
       db.execute(`
@@ -47,15 +47,17 @@ export function find(
   tableName: string,
   queryObject: { [key: string]: string | number | boolean },
   targetKeys: string[] = [],
-  limit?: number
+  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}` : ""}`
+    } 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);
@@ -70,7 +72,7 @@ export function find(
 
 export function insert(
   tableName: string,
-  userInsertObject: { [key: string]: string | number | boolean }
+  userInsertObject: { [key: string]: string | number | boolean },
 ) {
   const db = prepareDB(tableName);
   const insertObject = {
@@ -78,11 +80,15 @@ export function insert(
     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(", ")})`
+    `INSERT INTO ${tableName.toLowerCase()} (${
+      Object.keys(insertObject).join(
+        ", ",
+      )
+    }) VALUES (${
+      Object.keys(insertObject)
+        .map((key) => `:${key}`)
+        .join(", ")
+    })`,
   );
   try {
     insertQuery.all(insertObject);
@@ -99,7 +105,7 @@ export function insert(
 export function update(
   tableName: string,
   id: number | string,
-  userUpdateObject: { [key: string]: string | number | boolean }
+  userUpdateObject: { [key: string]: string | number | boolean },
 ) {
   const db = prepareDB(tableName);
   const updateObject = {
@@ -107,9 +113,11 @@ export function update(
     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`
+    `UPDATE ${tableName.toLowerCase()} SET ${
+      Object.keys(updateObject)
+        .map((updateKey) => `${updateKey} = :${updateKey}`)
+        .join(", ")
+    } WHERE id = :id`,
   );
   try {
     updateQuery.all({ ...updateObject, id });
@@ -125,13 +133,15 @@ export function update(
 
 export function del(
   tableName: string,
-  queryObject: { [key: string]: string | number | boolean }
+  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 ")}`
+    `DELETE FROM ${tableName.toLowerCase()} WHERE ${
+      Object.keys(queryObject)
+        .map((queryKey) => `${queryKey} = :${queryKey}`)
+        .join(" AND ")
+    }`,
   );
   try {
     deleteQuery.all(queryObject);