Skip to content

Commit

Permalink
feat(mongo): remove some default values
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 8, 2024
1 parent 25ed14e commit d451195
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/mongo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class MongoDriver extends Driver<MongoDriver.Config> {
}

stop() {
return this.client.close()
return this.client?.close()
}

/**
Expand Down Expand Up @@ -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),
Expand Down
19 changes: 11 additions & 8 deletions packages/mongo/tests/migration.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -36,21 +36,25 @@ interface Tables {
}

describe('@minatojs/driver-mongo/migrate-virtualKey', () => {
const database: Database<Tables> = new Database()
const ctx = new Context()
ctx.plugin(Database)

const database = ctx.model as Database<Tables>
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',
Expand All @@ -59,8 +63,7 @@ describe('@minatojs/driver-mongo/migrate-virtualKey', () => {
})

afterEach(async () => {
await database.dropAll()
await database.stopAll()
fork?.dispose()
logger.level = 2
})

Expand Down
2 changes: 1 addition & 1 deletion packages/mysql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
2 changes: 1 addition & 1 deletion packages/postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion packages/postgres/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('@minatojs/driver-postgres', () => {
port: 5432,
user: 'koishi',
password: 'koishi@114514',
database: 'test'
database: 'test',
})
})

Expand Down
24 changes: 10 additions & 14 deletions packages/sqlite/src/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -249,13 +250,7 @@ export class SQLiteDriver extends Driver<SQLiteDriver.Config> {

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({
Expand All @@ -269,7 +264,7 @@ export class SQLiteDriver extends Driver<SQLiteDriver.Config> {
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 = () => {
Expand Down Expand Up @@ -326,7 +321,7 @@ export class SQLiteDriver extends Driver<SQLiteDriver.Config> {

#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) {
Expand Down Expand Up @@ -469,9 +464,10 @@ export namespace SQLiteDriver {
}

export const Config: z<Config> = 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,
})
}

Expand Down
4 changes: 4 additions & 0 deletions packages/tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"types": [
"@types/mocha",
"@types/node",
],
},
"include": [
"src",
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"noImplicitAny": false,
"noImplicitThis": false,
"strictFunctionTypes": false,
"types": [
"@types/node",
"yml-register/types",
],
},
}

0 comments on commit d451195

Please sign in to comment.