mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-12 09:18:14 +08:00
feat(TGIndex): 完成数据库的数据写入
This commit is contained in:
76
src/App.vue
76
src/App.vue
@@ -15,9 +15,11 @@
|
||||
import TSidebar from "./components/t-sidebar.vue";
|
||||
import useAppStore from "./store/modules/app";
|
||||
import { TGAppDataList } from "./data";
|
||||
import { getDataList } from "./data/init";
|
||||
import { fs } from "@tauri-apps/api";
|
||||
import { BaseDirectory } from "@tauri-apps/api/fs";
|
||||
import { onMounted } from "vue";
|
||||
import { InitTGData, DeleteTGData, WriteTGData } from "./utils/TGIndex";
|
||||
|
||||
const appStore = useAppStore();
|
||||
|
||||
@@ -25,42 +27,44 @@ onMounted(async () => {
|
||||
await checkLoad();
|
||||
});
|
||||
async function checkLoad() {
|
||||
if (!appStore.loading) {
|
||||
try {
|
||||
await fs.readDir(`${appStore.dataPath.app}`);
|
||||
} catch (e) {
|
||||
await fs.createDir("appData", { dir: BaseDirectory.AppLocalData });
|
||||
}
|
||||
try {
|
||||
await fs.readDir(`${appStore.dataPath.user}`);
|
||||
} catch (e) {
|
||||
await fs.createDir("userData", { dir: BaseDirectory.AppLocalData });
|
||||
}
|
||||
try {
|
||||
await fs.readDir(`${appStore.dataPath.merge}`);
|
||||
} catch (e) {
|
||||
await fs.createDir("mergeData", { dir: BaseDirectory.AppLocalData });
|
||||
}
|
||||
try {
|
||||
await fs.readDir(`${appStore.dataPath.temp}`);
|
||||
} catch (e) {
|
||||
await fs.createDir("tempData", { dir: BaseDirectory.AppLocalData });
|
||||
}
|
||||
console.log("检测到数据未加载,开始加载数据...");
|
||||
TGAppDataList.AppData.map(async item => {
|
||||
await fs.writeFile(
|
||||
`${appStore.dataPath.app}\\${item.name}`,
|
||||
JSON.stringify(item.data, null, 4)
|
||||
);
|
||||
});
|
||||
TGAppDataList.MergeData.map(async item => {
|
||||
await fs.writeFile(
|
||||
`${appStore.dataPath.merge}\\${item.name}`,
|
||||
JSON.stringify(item.data, null, 4)
|
||||
);
|
||||
});
|
||||
appStore.loading = true;
|
||||
console.log("数据加载完成!");
|
||||
if (appStore.loading) {
|
||||
console.log("数据已加载!");
|
||||
return;
|
||||
}
|
||||
await DeleteTGData();
|
||||
await createDataDir();
|
||||
await writeData();
|
||||
await writeIndex();
|
||||
appStore.loading = true;
|
||||
console.log("数据加载完成!");
|
||||
}
|
||||
// 创建数据文件夹
|
||||
async function createDataDir() {
|
||||
console.log("开始创建数据文件夹...");
|
||||
await fs.createDir("appData", { dir: BaseDirectory.AppLocalData, recursive: true });
|
||||
await fs.createDir("userData", { dir: BaseDirectory.AppLocalData, recursive: true });
|
||||
await fs.createDir("mergeData", { dir: BaseDirectory.AppLocalData, recursive: true });
|
||||
await fs.createDir("tempData", { dir: BaseDirectory.AppLocalData, recursive: true });
|
||||
console.log("数据文件夹创建完成!");
|
||||
}
|
||||
// 将数据写入文件夹
|
||||
async function writeData() {
|
||||
console.log("开始写入数据...");
|
||||
TGAppDataList.AppData.map(async item => {
|
||||
await fs.writeFile(`${appStore.dataPath.app}\\${item.name}`, JSON.stringify(item.data));
|
||||
});
|
||||
TGAppDataList.MergeData.map(async item => {
|
||||
await fs.writeFile(`${appStore.dataPath.merge}\\${item.name}`, JSON.stringify(item.data));
|
||||
});
|
||||
console.log("数据写入完成!");
|
||||
}
|
||||
// 写入 IndexedDB
|
||||
async function writeIndex() {
|
||||
console.log("开始写入 IndexedDB...");
|
||||
await InitTGData();
|
||||
getDataList.map(async item => {
|
||||
await WriteTGData(item.name, item.data);
|
||||
});
|
||||
console.log("IndexedDB 写入完成!");
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -30,5 +30,5 @@ export const AppDataList = [
|
||||
export const AppData = {
|
||||
achievements: achievements as Achievement[],
|
||||
achievementSeries: achievementSeries as AchievementSeries[],
|
||||
nameCards: nameCards as unknown as Map<NameCard>,
|
||||
nameCards: nameCards as unknown as Map<NameCard[]>,
|
||||
};
|
||||
|
||||
31
src/data/init/achievementSeries.ts
Normal file
31
src/data/init/achievementSeries.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @file data init achievementSeries
|
||||
* @description data init achievementSeries
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha
|
||||
*/
|
||||
import { MergeData } from "../merge";
|
||||
import { SeriesMap } from "../../interface/Achievements";
|
||||
import { Map } from "../../interface/Base";
|
||||
|
||||
/**
|
||||
* @description 成就系列表参数
|
||||
* @since Alpha
|
||||
*/
|
||||
export const Config = {
|
||||
storeName: "AchievementSeries",
|
||||
keyPath: "id",
|
||||
indexes: ["order", "name", "version", "card", "total_count", "complete_count"],
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 成就系列数据
|
||||
* @since Alpha
|
||||
* @return {SeriesMap[]}
|
||||
*/
|
||||
export function getData() {
|
||||
const data: Map<SeriesMap> = MergeData.achievementSeries;
|
||||
return Object.keys(data).map(key => {
|
||||
return data[Number(key)];
|
||||
});
|
||||
}
|
||||
32
src/data/init/achievements.ts
Normal file
32
src/data/init/achievements.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* @file data init achievement
|
||||
* @description data init achievement
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha
|
||||
*/
|
||||
|
||||
import { MergeData } from "../merge";
|
||||
import { AchievementMap } from "../../interface/Achievements";
|
||||
import { Map } from "../../interface/Base";
|
||||
|
||||
/**
|
||||
* @description 成就表参数
|
||||
* @since Alpha
|
||||
*/
|
||||
export const Config = {
|
||||
storeName: "Achievement",
|
||||
keyPath: "id",
|
||||
indexes: ["name", "description", "series", "order", "reward", "version"],
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 成就数据
|
||||
* @since Alpha
|
||||
* @return {AchievementMap[]}
|
||||
*/
|
||||
export function getData() {
|
||||
const data: Map<AchievementMap> = MergeData.achievements;
|
||||
return Object.keys(data).map(key => {
|
||||
return data[Number(key)];
|
||||
});
|
||||
}
|
||||
26
src/data/init/index.ts
Normal file
26
src/data/init/index.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @file data init index
|
||||
* @description data init index
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha
|
||||
*/
|
||||
import { Config as NameCardConfig, getData as getNameCardData } from "./nameCard";
|
||||
import { Config as AchievementConfig, getData as getAchievementData } from "./achievements";
|
||||
import { Config as SeriesConfig, getData as getSeriesData } from "./achievementSeries";
|
||||
|
||||
export const ConfigList = [NameCardConfig, AchievementConfig, SeriesConfig];
|
||||
|
||||
export const getDataList = [
|
||||
{
|
||||
name: "NameCard",
|
||||
data: getNameCardData(),
|
||||
},
|
||||
{
|
||||
name: "Achievement",
|
||||
data: getAchievementData(),
|
||||
},
|
||||
{
|
||||
name: "AchievementSeries",
|
||||
data: getSeriesData(),
|
||||
},
|
||||
];
|
||||
37
src/data/init/nameCard.ts
Normal file
37
src/data/init/nameCard.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* @file data init nameCard
|
||||
* @description data init nameCard
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha
|
||||
*/
|
||||
|
||||
import { AppData } from "../app";
|
||||
import { NameCard } from "../../interface/NameCard";
|
||||
import { Map } from "../../interface/Base";
|
||||
|
||||
/**
|
||||
* @description 名片表参数
|
||||
* @since Alpha
|
||||
*/
|
||||
export const Config = {
|
||||
storeName: "NameCard",
|
||||
keyPath: "name",
|
||||
indexes: ["type"],
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 名片数据
|
||||
* @since Alpha
|
||||
* @return {NameCard[]}
|
||||
*/
|
||||
export function getData() {
|
||||
const data: Map<NameCard[]> = AppData.nameCards;
|
||||
let result: NameCard[] = [];
|
||||
Object.keys(data).map(key => {
|
||||
const cards: NameCard[] = data[Number(key)];
|
||||
cards.map(card => {
|
||||
result.push(card);
|
||||
});
|
||||
});
|
||||
return result;
|
||||
}
|
||||
@@ -5,186 +5,57 @@
|
||||
* @since Alpha
|
||||
*/
|
||||
|
||||
// 一些常量
|
||||
const DB_NAME = "TGData";
|
||||
const DB_VERSION = 1;
|
||||
// 创建数据库
|
||||
export const TGDatabases = window.indexedDB.open(DB_NAME, DB_VERSION);
|
||||
import { ConfigList } from "../data/init";
|
||||
|
||||
// 数据库参数
|
||||
export const DB_NAME = "TGData";
|
||||
export const DB_VERSION = 1;
|
||||
|
||||
/**
|
||||
* @description 创建数据表
|
||||
* @param {string} name 表名
|
||||
* @param {string} keyPath 主键
|
||||
* @param {Array<string>} indexs 索引
|
||||
* @param {boolean} autoIncrement 是否自增
|
||||
* @returns {Promise<IDBObjectStore>} 返回数据表
|
||||
* @description 初始化数据库
|
||||
* @description 只会在第一次打开游戏时执行
|
||||
* @since Alpha
|
||||
*/
|
||||
export const createTable = (
|
||||
name: string,
|
||||
keyPath: string,
|
||||
indexs: Array<string>,
|
||||
autoIncrement: boolean
|
||||
): Promise<IDBObjectStore> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
TGDatabases.onsuccess = () => {
|
||||
const db = TGDatabases.result;
|
||||
const store = db.createObjectStore(name, {
|
||||
keyPath: keyPath,
|
||||
autoIncrement: autoIncrement,
|
||||
export async function InitTGData() {
|
||||
const request = window.indexedDB.open(DB_NAME, DB_VERSION);
|
||||
request.onupgradeneeded = event => {
|
||||
const db = request.result;
|
||||
// 创建表
|
||||
ConfigList.forEach(config => {
|
||||
const store = db.createObjectStore(config.storeName, {
|
||||
keyPath: config.keyPath,
|
||||
});
|
||||
indexs.forEach(index => {
|
||||
config.indexes.forEach(index => {
|
||||
store.createIndex(index, index, { unique: false });
|
||||
});
|
||||
resolve(store);
|
||||
};
|
||||
TGDatabases.onerror = () => {
|
||||
reject(TGDatabases.error);
|
||||
};
|
||||
});
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 删除数据表
|
||||
* @param {string} name 表名
|
||||
* @returns {Promise<void>} 返回空
|
||||
* @description 删除数据库
|
||||
* @since Alpha
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
export const deleteTable = (name: string): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
TGDatabases.onsuccess = () => {
|
||||
const db = TGDatabases.result;
|
||||
db.deleteObjectStore(name);
|
||||
resolve();
|
||||
};
|
||||
TGDatabases.onerror = () => {
|
||||
reject(TGDatabases.error);
|
||||
};
|
||||
});
|
||||
};
|
||||
export async function DeleteTGData() {
|
||||
await window.indexedDB.deleteDatabase(DB_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 添加数据
|
||||
* @param {string} name 表名
|
||||
* @param {any} data 数据
|
||||
* @returns {Promise<void>} 返回空
|
||||
* @description 向数据库中写入数据
|
||||
* @since Alpha
|
||||
* @param {string} storeName 表名
|
||||
* @param {any[]} data 数据
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
export const addData = (name: string, data: any): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
TGDatabases.onsuccess = () => {
|
||||
const db = TGDatabases.result;
|
||||
const store = db.transaction(name, "readwrite").objectStore(name);
|
||||
store.add(data);
|
||||
resolve();
|
||||
};
|
||||
TGDatabases.onerror = () => {
|
||||
reject(TGDatabases.error);
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 删除数据
|
||||
* @param {string} name 表名
|
||||
* @param {any} data 数据
|
||||
* @returns {Promise<void>} 返回空
|
||||
*/
|
||||
export const deleteData = (name: string, data: any): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
TGDatabases.onsuccess = () => {
|
||||
const db = TGDatabases.result;
|
||||
const store = db.transaction(name, "readwrite").objectStore(name);
|
||||
store.delete(data);
|
||||
resolve();
|
||||
};
|
||||
TGDatabases.onerror = () => {
|
||||
reject(TGDatabases.error);
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 修改数据
|
||||
* @param {string} name 表名
|
||||
* @param {any} data 数据
|
||||
* @returns {Promise<void>} 返回空
|
||||
*/
|
||||
export const updateData = (name: string, data: any): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
TGDatabases.onsuccess = () => {
|
||||
const db = TGDatabases.result;
|
||||
const store = db.transaction(name, "readwrite").objectStore(name);
|
||||
store.put(data);
|
||||
resolve();
|
||||
};
|
||||
TGDatabases.onerror = () => {
|
||||
reject(TGDatabases.error);
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 查询数据
|
||||
* @param {string} name 表名
|
||||
* @param {any} data 数据
|
||||
* @returns {Promise<any>} 返回数据
|
||||
*/
|
||||
export const getData = (name: string, data: any): Promise<any> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
TGDatabases.onsuccess = () => {
|
||||
const db = TGDatabases.result;
|
||||
const store = db.transaction(name, "readwrite").objectStore(name);
|
||||
const request = store.get(data);
|
||||
request.onsuccess = () => {
|
||||
resolve(request.result);
|
||||
};
|
||||
request.onerror = () => {
|
||||
reject(request.error);
|
||||
};
|
||||
};
|
||||
TGDatabases.onerror = () => {
|
||||
reject(TGDatabases.error);
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 查询所有数据
|
||||
* @param {string} name 表名
|
||||
* @returns {Promise<Array<any>>} 返回数据
|
||||
*/
|
||||
export const getAllData = (name: string): Promise<Array<any>> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
TGDatabases.onsuccess = () => {
|
||||
const db = TGDatabases.result;
|
||||
const store = db.transaction(name, "readwrite").objectStore(name);
|
||||
const request = store.getAll();
|
||||
request.onsuccess = () => {
|
||||
resolve(request.result);
|
||||
};
|
||||
request.onerror = () => {
|
||||
reject(request.error);
|
||||
};
|
||||
};
|
||||
TGDatabases.onerror = () => {
|
||||
reject(TGDatabases.error);
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @description 清空数据表
|
||||
* @param {string} name 表名
|
||||
* @returns {Promise<void>} 返回空
|
||||
*/
|
||||
export const clearTable = (name: string): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
TGDatabases.onsuccess = () => {
|
||||
const db = TGDatabases.result;
|
||||
const store = db.transaction(name, "readwrite").objectStore(name);
|
||||
store.clear();
|
||||
resolve();
|
||||
};
|
||||
TGDatabases.onerror = () => {
|
||||
reject(TGDatabases.error);
|
||||
};
|
||||
});
|
||||
};
|
||||
export async function WriteTGData(storeName: string, data: any[]) {
|
||||
const request = window.indexedDB.open(DB_NAME, DB_VERSION);
|
||||
request.onsuccess = event => {
|
||||
const db = request.result;
|
||||
const transaction = db.transaction(storeName, "readwrite");
|
||||
const store = transaction.objectStore(storeName);
|
||||
data.forEach(item => {
|
||||
store.put(item);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user