add db module

This commit is contained in:
2025-05-03 04:48:12 +08:00
parent ee483aa87e
commit c2a0623482
32 changed files with 28860 additions and 109 deletions

View File

@@ -1,4 +1,8 @@
import { XhsClient } from '@/libs/xhs.ts';
import { Sequelize } from 'sequelize';
import { createSequelize } from '@/services/xhs-db/db.ts';
import path from 'node:path';
import fs from 'node:fs';
type XhsClientOptions = {
key: string;
@@ -6,12 +10,14 @@ type XhsClientOptions = {
signConfig?: {
signUrl: string;
};
initDB?: boolean;
[key: string]: any;
};
type XhsClientMap = {
client: XhsClient;
key: string;
options: XhsClientOptions;
db: Sequelize;
};
type XhsServicesOptions = {
root?: string;
@@ -29,9 +35,34 @@ export class XhsServices {
}
const client = new XhsClient({ cookie });
client.signConfig = signConfig;
this.map.set(key, { client, key, options });
const storagePath = options.storage || `db-sqlite/xhs-${key}.db`;
const storage = path.resolve(storagePath);
const dir = path.dirname(storage);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
let isNew = options?.initDB ?? false;
if (!fs.existsSync(storage) || !isNew) {
isNew = true;
}
const db = createSequelize({ storage: storage });
const xhsClientMap = {
client,
key,
options,
db,
};
if (isNew) {
this.initDb(xhsClientMap);
}
this.map.set(key, xhsClientMap);
return client;
}
async initDb(xhsClientMap: XhsClientMap) {
//
}
createRoot(options: Partial<XhsClientOptions>) {
options.key = options.key || this.root;
return this.createClient(options as XhsClientOptions);