Skip to content

Commit

Permalink
feat: added a store of records
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb committed Mar 10, 2023
1 parent ec6c1e8 commit 404d391
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/sqls/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { configDir } from '@tauri-apps/api/path'
import Database from 'tauri-plugin-sql-api'
import { dialogErrorMessage } from '@/utils'
import type { HistoryRecord } from '@/types'
import type { HistoryRecord, RecordData } from '@/types'

const dbFile = import.meta.env.DEV ? 'sql.dev.db' : 'sql.db'
const db = await Database.load(
Expand Down Expand Up @@ -52,14 +52,16 @@ export const initSQL = () => {
* 查找的 sql 语句
*/
export const selectSQL = async () => {
return await executeSQL(`SELECT * FROM ${tableName} ORDER BY id;`)
return (await executeSQL(
`SELECT * FROM ${tableName} ORDER BY id;`
)) as HistoryRecord[]
}

/**
* 添加的 sql 语句
* @param data 聊天内容
*/
export const insertSQL = async (data: any[]) => {
export const insertSQL = async (data: RecordData[]) => {
await executeSQL(
`INSERT INTO ${tableName} (data) VALUES ('${JSON.stringify(data)}');`
)
Expand Down
34 changes: 34 additions & 0 deletions src/stores/record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { selectSQL, insertSQL, updateSQL, deleteSQL } from '@/sqls'
import type { HistoryRecord, RecordData } from '@/types'

export const useRecordStore = defineStore('recordStore', () => {
const currentRecord = ref<number>()

const recordList = ref<HistoryRecord[]>([])

const getRecord = async () => {
recordList.value = await selectSQL()
}

const addRecord = async (data: RecordData[]) => {
await insertSQL(data)

getRecord()
}

const updateRecord = async (id: number, payload: HistoryRecord) => {
await updateSQL(id, payload)

getRecord()
}

const deleteRecord = async (id?: number) => {
await deleteSQL(id)

getRecord()
}

onMounted(getRecord)

return { currentRecord, recordList, addRecord, updateRecord, deleteRecord }
})
7 changes: 6 additions & 1 deletion src/types/shared.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export interface RecordData {
role: string
content: string
}

export interface HistoryRecord {
id?: number
title?: string
data?: any[]
data?: RecordData[]
time?: string
}
1 change: 0 additions & 1 deletion src/utils/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* 判断数据是否为 null 或者 undefined
* @param value 数据
* @returns 判断结果
*/
export const isNil = (value: any) => value == null
4 changes: 4 additions & 0 deletions src/utils/tauri.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { message } from '@tauri-apps/api/dialog'

/**
* 错误弹框
* @param errorMessage 错误信息
*/
export const dialogErrorMessage = (errorMessage: string) => {
message(errorMessage, {
title: import.meta.env.VITE_APP_NAME,
Expand Down

0 comments on commit 404d391

Please sign in to comment.