From d45119595fd843354e89fb199730837308b06d76 Mon Sep 17 00:00:00 2001 From: Shigma Date: Thu, 8 Feb 2024 21:10:30 +0800 Subject: [PATCH] feat(mongo): remove some default values --- packages/mongo/src/index.ts | 4 ++-- packages/mongo/tests/migration.spec.ts | 19 +++++++++++-------- packages/mysql/src/index.ts | 2 +- packages/postgres/src/index.ts | 2 +- packages/postgres/tests/index.spec.ts | 2 +- packages/sqlite/src/index.ts | 24 ++++++++++-------------- packages/tests/tsconfig.json | 4 ++++ tsconfig.base.json | 4 ++++ 8 files changed, 34 insertions(+), 27 deletions(-) diff --git a/packages/mongo/src/index.ts b/packages/mongo/src/index.ts index 9e2c0d7e..70fb47e2 100644 --- a/packages/mongo/src/index.ts +++ b/packages/mongo/src/index.ts @@ -47,7 +47,7 @@ export class MongoDriver extends Driver { } stop() { - return this.client.close() + return this.client?.close() } /** @@ -496,7 +496,7 @@ export namespace MongoDriver { port: z.natural().max(65535), username: z.string(), password: z.string().role('secret'), - database: z.string().default('koishi'), + database: z.string().required(), writeConcern: z.object({ w: z.union([ z.const(undefined), diff --git a/packages/mongo/tests/migration.spec.ts b/packages/mongo/tests/migration.spec.ts index eaa461a6..f51ebc79 100644 --- a/packages/mongo/tests/migration.spec.ts +++ b/packages/mongo/tests/migration.spec.ts @@ -1,5 +1,5 @@ import { Database, Primary } from 'minato' -import Logger from 'reggol' +import { Context, ForkScope, Logger } from 'cordis' import { expect } from 'chai' import { } from 'chai-shape' import MongoDriver from '@minatojs/driver-mongo' @@ -36,21 +36,25 @@ interface Tables { } describe('@minatojs/driver-mongo/migrate-virtualKey', () => { - const database: Database = new Database() + const ctx = new Context() + ctx.plugin(Database) + + const database = ctx.model as Database + let fork: ForkScope const resetConfig = async (optimizeIndex: boolean) => { - await database.stopAll() - await database.connect(MongoDriver, { + fork?.dispose() + fork = ctx.plugin(MongoDriver, { host: 'localhost', port: 27017, database: 'test', - optimizeIndex: optimizeIndex, + optimizeIndex, }) } beforeEach(async () => { logger.level = 3 - await database.connect(MongoDriver, { + fork = ctx.plugin(MongoDriver, { host: 'localhost', port: 27017, database: 'test', @@ -59,8 +63,7 @@ describe('@minatojs/driver-mongo/migrate-virtualKey', () => { }) afterEach(async () => { - await database.dropAll() - await database.stopAll() + fork?.dispose() logger.level = 2 }) diff --git a/packages/mysql/src/index.ts b/packages/mysql/src/index.ts index f680b574..fb89b9e1 100644 --- a/packages/mysql/src/index.ts +++ b/packages/mysql/src/index.ts @@ -685,7 +685,7 @@ export namespace MySQLDriver { port: z.natural().max(65535).default(3306), user: z.string().default('root'), password: z.string().role('secret'), - database: z.string().default('koishi'), + database: z.string().required(), }), z.object({ ssl: z.union([ diff --git a/packages/postgres/src/index.ts b/packages/postgres/src/index.ts index 67f58928..7824dfc1 100644 --- a/packages/postgres/src/index.ts +++ b/packages/postgres/src/index.ts @@ -789,7 +789,7 @@ export namespace PostgresDriver { port: z.natural().max(65535).default(5432), user: z.string().default('root'), password: z.string().role('secret'), - database: z.string().default('koishi'), + database: z.string().required(), }).i18n({ 'en-US': require('./locales/en-US'), 'zh-CN': require('./locales/zh-CN'), diff --git a/packages/postgres/tests/index.spec.ts b/packages/postgres/tests/index.spec.ts index 3646ce68..6ca9a1c0 100644 --- a/packages/postgres/tests/index.spec.ts +++ b/packages/postgres/tests/index.spec.ts @@ -15,7 +15,7 @@ describe('@minatojs/driver-postgres', () => { port: 5432, user: 'koishi', password: 'koishi@114514', - database: 'test' + database: 'test', }) }) diff --git a/packages/sqlite/src/index.ts b/packages/sqlite/src/index.ts index d6ac81e4..5ed04510 100644 --- a/packages/sqlite/src/index.ts +++ b/packages/sqlite/src/index.ts @@ -1,10 +1,11 @@ import { clone, deepEqual, Dict, difference, isNullable, makeArray } from 'cosmokit' import { Driver, Eval, executeUpdate, Field, Model, randomId, Selection, z } from 'minato' import { Builder, escapeId } from '@minatojs/sql-utils' -import { promises as fs } from 'fs' +import { resolve } from 'node:path' +import { readFile, writeFile } from 'node:fs/promises' import init from '@minatojs/sql.js' -import { dirname, join } from 'path' -import { mkdir, rename, stat } from 'fs/promises' +import enUS from './locales/en-US.yml' +import zhCN from './locales/zh-CN.yml' function getTypeDef({ type }: Field) { switch (type) { @@ -249,13 +250,7 @@ export class SQLiteDriver extends Driver { async start() { if (this.config.path !== ':memory:') { - this.config.path = join(this.ctx.baseDir, this.config.path) - const oldPath = join(this.ctx.baseDir, '.koishi.db') - if (await stat(oldPath).catch(() => null)) { - this.logger.info('migrating to data directory') - await mkdir(dirname(this.config.path), { recursive: true }) - await rename(oldPath, this.config.path) - } + this.config.path = resolve(this.ctx.baseDir, this.config.path) } const isBrowser = process.env.KOISHI_ENV === 'browser' const sqlite = await init({ @@ -269,7 +264,7 @@ export class SQLiteDriver extends Driver { console.log(this.config.path) this.db = new sqlite.Database(this.config.path) } else { - const buffer = await fs.readFile(this.config.path).catch(() => null) + const buffer = await readFile(this.config.path).catch(() => null) this.db = new sqlite.Database(this.config.path, buffer) if (isBrowser) { window.addEventListener('beforeunload', this.beforeUnload = () => { @@ -326,7 +321,7 @@ export class SQLiteDriver extends Driver { #export() { const data = this.db.export() - fs.writeFile(this.config.path, data) + return writeFile(this.config.path, data) } #run(sql: string, params: any = [], callback?: () => any) { @@ -469,9 +464,10 @@ export namespace SQLiteDriver { } export const Config: z = z.object({ - path: z.string().role('path').default('data/koishi.db'), + path: z.string().role('path').required(), }).i18n({ - // 'zh-CN': zhCN, + 'en-US': enUS, + 'zh-CN': zhCN, }) } diff --git a/packages/tests/tsconfig.json b/packages/tests/tsconfig.json index 74ac2c8d..d2545e1c 100644 --- a/packages/tests/tsconfig.json +++ b/packages/tests/tsconfig.json @@ -3,6 +3,10 @@ "compilerOptions": { "outDir": "lib", "rootDir": "src", + "types": [ + "@types/mocha", + "@types/node", + ], }, "include": [ "src", diff --git a/tsconfig.base.json b/tsconfig.base.json index 69259013..6d20efa9 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -14,5 +14,9 @@ "noImplicitAny": false, "noImplicitThis": false, "strictFunctionTypes": false, + "types": [ + "@types/node", + "yml-register/types", + ], }, }