Skip to content

Commit

Permalink
feat(mongo): support config schema
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 8, 2024
1 parent 228fff5 commit 25ed14e
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 53 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"dep": "yakumo upgrade",
"pub": "yakumo publish",
"lint": "eslint packages --ext=ts --cache",
"test": "yakumo mocha -r esbuild-register -t 10000",
"test": "yakumo mocha -r esbuild-register -r yml-register -t 10000",
"test:text": "shx rm -rf coverage && c8 -r text yarn test",
"test:json": "shx rm -rf coverage && c8 -r json yarn test",
"test:html": "shx rm -rf coverage && c8 -r html yarn test"
Expand All @@ -36,6 +36,7 @@
"yakumo": "^1.0.0-beta.6",
"yakumo-esbuild": "^1.0.0-beta.2",
"yakumo-mocha": "^1.0.0-beta.2",
"yakumo-tsc": "^1.0.0-beta.3"
"yakumo-tsc": "^1.0.0-beta.3",
"yml-register": "^1.1.0"
}
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"postgres"
],
"dependencies": {
"cordis": "^3.8.5",
"cordis": "^3.9.0",
"cosmokit": "^1.5.2"
}
}
2 changes: 1 addition & 1 deletion packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Database<S = any> extends Service {

private stashed = new Set<string>()

constructor(ctx = new Context()) {
constructor(ctx?: Context) {
super(ctx, 'model', true)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export abstract class Driver<C = any> {

constructor(public ctx: Context, public config: C) {
this.database = ctx.model
this.logger = ctx.logger(this['contructor'].name)
this.logger = ctx.logger(this.constructor.name)

ctx.on('ready', async () => {
await this.start()
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ declare module 'cordis' {

export interface Tables {}

export { Logger, Schema, Schema as z } from 'cordis'

export default Database
67 changes: 44 additions & 23 deletions packages/mongo/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
import { BSONType, ClientSession, Collection, Db, IndexDescription, MongoClient, MongoClientOptions, MongoError } from 'mongodb'
import { Dict, isNullable, makeArray, mapValues, noop, omit, pick } from 'cosmokit'
import { Driver, Eval, executeUpdate, Query, RuntimeError, Selection } from 'minato'
import { Driver, Eval, executeUpdate, Query, RuntimeError, Selection, z } from 'minato'
import { URLSearchParams } from 'url'
import { Transformer } from './utils'

const tempKey = '__temp_minato_mongo__'

export namespace MongoDriver {
export interface Config extends MongoClientOptions {
username?: string
password?: string
protocol?: string
host?: string
port?: number
/** database name */
database?: string
/** default auth database */
authDatabase?: string
connectOptions?: ConstructorParameters<typeof URLSearchParams>[0]
/** connection string (will overwrite all configs except 'name') */
uri?: string
/**
* store single primary key in `_id` field to enhance index performance
* @default false
*/
optimizeIndex?: boolean
}
}

export class MongoDriver extends Driver<MongoDriver.Config> {
static name = 'mongo'

Expand Down Expand Up @@ -491,4 +469,47 @@ See https://www.mongodb.com/docs/manual/tutorial/convert-standalone-to-replica-s
}
}

export namespace MongoDriver {
export interface Config extends MongoClientOptions {
username?: string
password?: string
protocol?: string
host?: string
port?: number
/** database name */
database?: string
/** default auth database */
authDatabase?: string
connectOptions?: ConstructorParameters<typeof URLSearchParams>[0]
/** connection string (will overwrite all configs except 'name') */
uri?: string
/**
* store single primary key in `_id` field to enhance index performance
* @default false
*/
optimizeIndex?: boolean
}

export const Config: z<Config> = z.object({
protocol: z.string().default('mongodb'),
host: z.string().default('localhost'),
port: z.natural().max(65535),
username: z.string(),
password: z.string().role('secret'),
database: z.string().default('koishi'),
writeConcern: z.object({
w: z.union([
z.const(undefined),
z.number().required(),
z.const('majority').required(),
]),
wtimeoutMS: z.number(),
journal: z.boolean(),
}),
}).i18n({
'en-US': require('./locales/en-US'),
'zh-CN': require('./locales/zh-CN'),
})
}

export default MongoDriver
16 changes: 16 additions & 0 deletions packages/mongo/src/locales/en-US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
protocol: The protocol to use.
host: The host to connect to.
port: The port number to be connected.
username: The username used for authentication.
password: The password used for authentication.
database: The name of the database we want to use.
writeConcern:
$description: Write Concern
w:
$description: The write concern.
$value:
- Default
- Custom
- Majority
wtimeoutMS: The write concern timeout.
journal: The journal write concern.
16 changes: 16 additions & 0 deletions packages/mongo/src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
protocol: 要使用的协议名。
host: 要连接到的主机名。
port: 要连接到的端口号。
username: 要使用的用户名。
password: 要使用的密码。
database: 要访问的数据库名。
writeConcern:
$description: Write Concern
w:
$description: The write concern.
$value:
- Default
- Custom
- Majority
wtimeoutMS: The write concern timeout.
journal: The journal write concern.
51 changes: 46 additions & 5 deletions packages/mysql/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createPool, format } from '@vlasky/mysql'
import type { OkPacket, Pool, PoolConfig, PoolConnection } from 'mysql'
import { Dict, difference, makeArray, pick, Time } from 'cosmokit'
import { Driver, Eval, executeUpdate, Field, isEvalExpr, Model, randomId, RuntimeError, Selection } from 'minato'
import { Driver, Eval, executeUpdate, Field, isEvalExpr, Model, randomId, RuntimeError, Selection, z } from 'minato'
import { Builder, escapeId, isBracketed } from '@minatojs/sql-utils'

declare module 'mysql' {
Expand Down Expand Up @@ -255,10 +255,6 @@ class MySQLBuilder extends Builder {
}
}

export namespace MySQLDriver {
export interface Config extends PoolConfig {}
}

export class MySQLDriver extends Driver<MySQLDriver.Config> {
static name = 'mysql'

Expand Down Expand Up @@ -680,4 +676,49 @@ INSERT INTO mtt VALUES(json_extract(j, concat('$[', i, ']'))); SET i=i+1; END WH
}
}

export namespace MySQLDriver {
export interface Config extends PoolConfig {}

export const Config: z<Config> = z.intersect([
z.object({
host: z.string().default('localhost'),
port: z.natural().max(65535).default(3306),
user: z.string().default('root'),
password: z.string().role('secret'),
database: z.string().default('koishi'),
}),
z.object({
ssl: z.union([
z.const(undefined),
z.object({
ca: z.string(),
cert: z.string(),
sigalgs: z.string(),
ciphers: z.string(),
clientCertEngine: z.string(),
crl: z.string(),
dhparam: z.string(),
ecdhCurve: z.string(),
honorCipherOrder: z.boolean(),
key: z.string(),
privateKeyEngine: z.string(),
privateKeyIdentifier: z.string(),
maxVersion: z.string(),
minVersion: z.string(),
passphrase: z.string(),
pfx: z.string(),
rejectUnauthorized: z.boolean(),
secureOptions: z.natural(),
secureProtocol: z.string(),
sessionIdContext: z.string(),
sessionTimeout: z.number(),
}),
]) as any,
}),
]).i18n({
'en-US': require('./locales/en-US'),
'zh-CN': require('./locales/zh-CN'),
})
}

export default MySQLDriver
12 changes: 12 additions & 0 deletions packages/mysql/src/locales/en-US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
host: The hostname of the database you are connecting to.
port: The port number to connect to.
user: The MySQL user to authenticate as.
password: The password of that MySQL user.
database: Name of the database to use for this connection.

ssl:
$description: SSL options.
$value:
- Default
- $description: Custom
rejectUnauthorized: Reject clients with invalid certificates.
12 changes: 12 additions & 0 deletions packages/mysql/src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
host: 要连接到的主机名。
port: 要连接到的端口号。
user: 要使用的用户名。
password: 要使用的密码。
database: 要访问的数据库名。

ssl:
$description: SSL 高级选项。
$value:
- 默认值
- $description: 自定义
rejectUnauthorized: 拒绝使用无效证书的客户端。
35 changes: 23 additions & 12 deletions packages/postgres/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import postgres from 'postgres'
import { Dict, difference, isNullable, makeArray, pick, Time } from 'cosmokit'
import { Driver, Eval, executeUpdate, Field, isEvalExpr, Model, randomId, Selection } from 'minato'
import { Driver, Eval, executeUpdate, Field, isEvalExpr, Model, randomId, Selection, z } from 'minato'
import { Builder, isBracketed } from '@minatojs/sql-utils'

const timeRegex = /(\d+):(\d+):(\d+)/
Expand Down Expand Up @@ -440,16 +440,6 @@ class PostgresBuilder extends Builder {
}
}

export namespace PostgresDriver {
export interface Config<T extends Record<string, postgres.PostgresType> = {}> extends postgres.Options<T> {
host: string
port: number
username: string
password: string
database: string
}
}

export class PostgresDriver extends Driver<PostgresDriver.Config> {
static name = 'postgres'

Expand All @@ -463,7 +453,7 @@ export class PostgresDriver extends Driver<PostgresDriver.Config> {
async start() {
this.postgres = postgres({
onnotice: () => { },
debug(_, query, parameters) {
debug: (_, query, parameters) => {
this.logger.debug(`> %s` + (parameters.length ? `\nparameters: %o` : ``), query, parameters.length ? parameters : '')
},
transform: {
Expand Down Expand Up @@ -785,4 +775,25 @@ export class PostgresDriver extends Driver<PostgresDriver.Config> {
}
}

export namespace PostgresDriver {
export interface Config<T extends Record<string, postgres.PostgresType> = {}> extends postgres.Options<T> {
host: string
port: number
user: string
password: string
database: string
}

export const Config: z<Config> = z.object({
host: z.string().default('localhost'),
port: z.natural().max(65535).default(5432),
user: z.string().default('root'),
password: z.string().role('secret'),
database: z.string().default('koishi'),
}).i18n({
'en-US': require('./locales/en-US'),
'zh-CN': require('./locales/zh-CN'),
})
}

export default PostgresDriver
5 changes: 5 additions & 0 deletions packages/postgres/src/locales/en-US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
host: The hostname of the database you are connecting to.
port: The port number to connect to.
user: The MySQL user to authenticate as.
password: The password of that MySQL user.
database: Name of the database to use for this connection.
5 changes: 5 additions & 0 deletions packages/postgres/src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
host: 要连接到的主机名。
port: 要连接到的端口号。
username: 要使用的用户名。
password: 要使用的密码。
database: 要访问的数据库名。
2 changes: 1 addition & 1 deletion packages/postgres/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('@minatojs/driver-postgres', () => {
await database.connect(PostgresDriver, {
host: 'localhost',
port: 5432,
username: 'koishi',
user: 'koishi',
password: 'koishi@114514',
database: 'test'
})
Expand Down
Loading

0 comments on commit 25ed14e

Please sign in to comment.