Parcourir la source

账本的CDUR

john il y a 8 mois
Parent
commit
5a6d0614ea

+ 68 - 0
node_expores/db/book.js

@@ -0,0 +1,68 @@
+import connection from "./base.js";
+
+export async function books_insert({
+  book_name = '',
+  author_id = '',
+  create_time = '',
+  update_time = '',
+}) {
+  return new Promise(async (resolve, reject) => {
+    try {
+      const sql = `
+        INSERT INTO book (book_name, author_id, create_time, update_time)
+        VALUES (?, ?, ?, ?)
+      `;
+      const values = [book_name, author_id, create_time, update_time];
+      // 直接接收 execute 返回的内容
+      connection.execute(sql, values, (result, fields) => {
+        return resolve(fields.insertId);
+      });
+    } catch (err) {
+      // console.error("Error inserting data:", err);
+      // throw err;
+      return resolve(false);
+    }
+  })
+}
+
+// 查询账本信息
+export function getBookById(book_id) {
+  return new Promise((resolve, reject) => {
+    connection.query(`SELECT * FROM book WHERE id = ? AND is_del=0`, [book_id], (err, rows) => {
+      if (err) {
+        resolve(false); // 如果存在记录,则返回 true,否则返回 false
+      } else {
+        resolve(rows.length > 0 ?  rows[0]: false); // 如果存在记录,则返回 true,否则返回 false
+      }
+    });
+  });
+}
+
+// 删除账本
+export function delBookById(book_id) {
+  return new Promise((resolve, reject) => {
+    connection.query(`update book SET is_del = 1 WHERE id = ?`, [book_id], (err, rows) => {
+      if (err) {
+        resolve(false); // 如果存在记录,则返回 true,否则返回 false
+      } else {
+        resolve(true); // 如果存在记录,则返回 true,否则返回 false
+      }
+    });
+  });
+}
+
+
+
+// 是否存在重复的账本数据
+export function ishaveBookById(book_id) {
+  return new Promise((resolve, reject) => {
+    connection.query(`SELECT * FROM book WHERE id = ?`, [book_id], (err, rows) => {
+      if (err) {
+        // reject(err);
+        resolve(false); // 如果存在记录,则返回 true,否则返回 false
+      } else {
+        resolve(rows.length > 0); // 如果存在记录,则返回 true,否则返回 false
+      }
+    });
+  });
+}

+ 1 - 25
node_expores/db/update.sql

@@ -1,25 +1 @@
--- 更新 files 增加软删除数据
-ALTER TABLE cashbook.files ADD is_del INT DEFAULT 0 NOT NULL;
-
--- 更新 authors 增加新字段
-ALTER TABLE cashbook.authors ADD user_id varchar(300) NOT NULL;
-ALTER TABLE cashbook.authors ADD CONSTRAINT authors_unique_2 UNIQUE KEY (user_id);
-ALTER TABLE cashbook.authors ADD create_time TIMESTAMP NOT NULL;
-ALTER TABLE cashbook.authors ADD update_time TIMESTAMP NOT NULL;
-
--- cashbook.record_files definition
-CREATE TABLE `record_files` (
-  `id` int NOT NULL AUTO_INCREMENT,
-  `file_id` varchar(300) NOT NULL,
-  `record_id` varchar(300) NOT NULL,
-  `book_id` varchar(300) NOT NULL,
-  `user_id` varchar(300) NOT NULL,
-  `create_time` timestamp NOT NULL,
-  `update_time` timestamp NOT NULL,
-  PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-
-ALTER TABLE cashbook.types ADD user_id varchar(300) NOT NULL;
-
-ALTER TABLE cashbook.types MODIFY COLUMN record_id int NULL;
-ALTER TABLE cashbook.record MODIFY COLUMN author_id VARCHAR(300) NOT NULL;
+ALTER TABLE cashbook.book ADD is_del INT DEFAULT 0 NOT NULL;

+ 10 - 10
node_expores/environment/index.js

@@ -1,19 +1,19 @@
 function dbInfo() {
   // 根据需要更新db的数据配置
-  // return {
-  //   host: "localhost",
-  //   port: 3306,
-  //   user: "root",
-  //   password: "12345678",
-  //   database: "cashbook",
-  // };
   return {
-    host: "192.168.2.103",
-    port: 6806,
+    host: "localhost",
+    port: 3306,
     user: "root",
-    password: "admin",
+    password: "12345678",
     database: "cashbook",
   };
+  // return {
+  //   host: "192.168.2.103",
+  //   port: 6806,
+  //   user: "root",
+  //   password: "admin",
+  //   database: "cashbook",
+  // };
 }
 
 

+ 17 - 0
node_expores/types/db.d.ts

@@ -0,0 +1,17 @@
+declare module '#db' {
+  // 在这里定义模块的类型
+  // 例如,如果模块导出一个连接数据库的函数,可以这样定义:
+  export function connect(connectionString: string): Promise<void>;
+
+  // 如果模块导出一个数据库配置对象
+  export interface DatabaseConfig {
+    host: string;
+    port: number;
+    username: string;
+    password: string;
+  }
+
+  // 如果模块有默认导出
+  const dbConfig: DatabaseConfig;
+  export default dbConfig;
+}

+ 22 - 0
node_expores/types/environment.d.ts

@@ -0,0 +1,22 @@
+declare module '#environment' {
+  // 在这里定义模块的类型,比如:
+  export interface aesInfoConfig {
+    key: string;
+    iv: string;
+  }
+
+  export interface DbInfoConfig {
+    host: string,
+    port: number,
+    user: string,
+    password: string,
+    database: string,
+  }
+
+  // const config: Config;
+  export default  {
+    dbInfo: () => DbInfoConfig,
+    aes_info: () => aesInfoConfig,
+    privateKey: string
+  };
+}

+ 15 - 0
node_expores/types/utils.d.ts

@@ -0,0 +1,15 @@
+declare module '#utils' {
+  // 在这里定义模块的类型
+  // 例如,如果模块导出一个函数,可以这样定义:
+  export function someUtilityFunction(arg: string): number;
+
+  // 如果模块导出多个内容,可以使用命名导出
+  export interface UtilityOptions {
+    optionA: string;
+    optionB: number;
+  }
+
+  // 如果模块有默认导出
+  const defaultExport: UtilityOptions;
+  export default defaultExport;
+}