generated from tailored/router-db-template
add db module
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user