@koishijs/core
Advanced tools
Comparing version 4.17.4 to 4.17.5
import * as utils from '@koishijs/utils'; | ||
import * as minato from 'minato'; | ||
import * as satori from '@satorijs/core'; | ||
import * as cordis from 'cordis'; | ||
import { Dict, Awaitable, Promisify } from 'cosmokit'; | ||
import { Database, Driver, Update, Eval } from 'minato'; | ||
import { Dict, MaybeArray, Awaitable, Promisify } from 'cosmokit'; | ||
import { Driver, FlatKeys, FlatPick, Update, Eval } from 'minato'; | ||
import { Fragment, h, Schema, Universal, Bot, Quester } from '@satorijs/core'; | ||
@@ -10,13 +11,23 @@ import { LocaleTree } from '@koishijs/i18n-utils'; | ||
import { version } from '../package.json'; | ||
export interface Events { | ||
'model'(name: keyof Tables): void; | ||
} | ||
export interface Context { | ||
[Database.Tables]: Tables; | ||
database: DatabaseService<this>; | ||
model: DatabaseService<this>; | ||
broadcast(content: Fragment, forced?: boolean): Promise<string[]>; | ||
broadcast(channels: readonly string[], content: Fragment, forced?: boolean): Promise<string[]>; | ||
[minato.Types]: Types; | ||
[minato.Tables]: Tables; | ||
[Context.Database]: Context.Database<this>; | ||
} | ||
export interface Tables extends Database.Tables { | ||
export namespace Context { | ||
interface Database<C extends Context = Context> { | ||
getUser<K extends FlatKeys<User>>(platform: string, pid: string, modifier?: Driver.Cursor<K>): Promise<FlatPick<User, K>>; | ||
setUser(platform: string, pid: string, data: Update<User>): Promise<void>; | ||
createUser(platform: string, pid: string, data: Partial<User>): Promise<User>; | ||
getChannel<K extends FlatKeys<Channel>>(platform: string, id: MaybeArray<string>, modifier?: Driver.Cursor<K>): Promise<FlatPick<Channel, K> | FlatPick<Channel, K>[]>; | ||
getAssignedChannels<K extends Channel.Field>(fields?: K[], selfIdMap?: Dict<string[]>): Promise<Pick<Channel, K>[]>; | ||
setChannel(platform: string, id: string, data: Update<Channel>): Promise<void>; | ||
createChannel(platform: string, id: string, data: Partial<Channel>): Promise<Channel>; | ||
broadcast(content: Fragment, forced?: boolean): Promise<string[]>; | ||
broadcast(channels: readonly string[], content: Fragment, forced?: boolean): Promise<string[]>; | ||
} | ||
} | ||
export interface Types extends minato.Types { | ||
} | ||
export interface Tables extends minato.Tables { | ||
user: User; | ||
@@ -68,10 +79,10 @@ binding: Binding; | ||
} | ||
export class DatabaseService<C extends Context = Context, S extends C[typeof Database.Tables] = C[typeof Database.Tables], N extends C[typeof Database.Types] = C[typeof Database.Types]> extends Database<C, S, N> { | ||
private get self(); | ||
constructor(ctx: C); | ||
getUser<K extends User.Field>(platform: string, pid: string, modifier?: Driver.Cursor<K>): Promise<Pick<User, K>>; | ||
export class KoishiDatabase { | ||
ctx: Context; | ||
constructor(ctx: Context); | ||
getUser<K extends FlatKeys<User>>(platform: string, pid: string, modifier?: Driver.Cursor<K>): Promise<FlatPick<User, K>>; | ||
setUser(platform: string, pid: string, data: Update<User>): Promise<Driver.WriteResult>; | ||
createUser(platform: string, pid: string, data: Partial<User>): Promise<User>; | ||
getChannel<K extends Channel.Field>(platform: string, id: string, modifier?: Driver.Cursor<K>): Promise<Pick<Channel, K | 'id' | 'platform'>>; | ||
getChannel<K extends Channel.Field>(platform: string, ids: string[], modifier?: Driver.Cursor<K>): Promise<Pick<Channel, K>[]>; | ||
getChannel<K extends FlatKeys<Channel>>(platform: string, id: string, modifier?: Driver.Cursor<K>): Promise<FlatPick<Channel, K | 'id' | 'platform'>>; | ||
getChannel<K extends FlatKeys<Channel>>(platform: string, ids: string[], modifier?: Driver.Cursor<K>): Promise<FlatPick<Channel, K>[]>; | ||
getSelfIds(platforms?: string[]): Dict<string[]>; | ||
@@ -537,4 +548,2 @@ getAssignedChannels<K extends Channel.Field>(fields?: K[], selfIdMap?: Dict<string[]>): Promise<Pick<Channel, K>[]>; | ||
export type FieldCollector<T extends keyof Tables, K = keyof Tables[T], A extends any[] = any[], O extends {} = {}> = Iterable<K> | ((argv: Argv<never, never, A, O>, fields: Set<keyof Tables[T]>) => void); | ||
export interface Session extends Context.Associate<'session'> { | ||
} | ||
export class Session<U extends User.Field = never, G extends Channel.Field = never, C extends Context = Context> extends satori.Session<C> { | ||
@@ -699,3 +708,2 @@ static shadow: symbol; | ||
export namespace Context { | ||
type Associate<P extends string, C extends Context = Context> = satori.Context.Associate<P, C>; | ||
interface Config extends Config.Basic, Config.Advanced { | ||
@@ -702,0 +710,0 @@ i18n?: I18n.Config; |
{ | ||
"name": "@koishijs/core", | ||
"description": "Core Features for Koishi", | ||
"version": "4.17.4", | ||
"version": "4.17.5", | ||
"main": "lib/index.cjs", | ||
@@ -36,8 +36,8 @@ "module": "lib/index.mjs", | ||
"@koishijs/utils": "^7.2.1", | ||
"@satorijs/core": "^3.6.6", | ||
"cordis": "^3.13.4", | ||
"@satorijs/core": "^3.7.0", | ||
"cordis": "^3.14.0", | ||
"cosmokit": "^1.6.2", | ||
"fastest-levenshtein": "^1.0.16", | ||
"minato": "^3.1.1" | ||
"minato": "^3.2.0" | ||
} | ||
} |
@@ -13,3 +13,3 @@ import { defineProperty, Promisify, Time } from 'cosmokit' | ||
import { Permissions } from './permission' | ||
import { DatabaseService } from './database' | ||
import { KoishiDatabase } from './database' | ||
@@ -53,3 +53,2 @@ export type EffectScope = cordis.EffectScope<Context> | ||
super(config) | ||
this.mixin('model', ['getSelfIds', 'broadcast']) | ||
this.mixin('$processor', ['match', 'middleware']) | ||
@@ -69,3 +68,3 @@ this.mixin('$filter', [ | ||
this.provide('$commander', new Commander(this, this.config), true) | ||
this.plugin(DatabaseService) | ||
this.provide('koishi.database', new KoishiDatabase(this), true) | ||
} | ||
@@ -121,4 +120,2 @@ | ||
export namespace Context { | ||
export type Associate<P extends string, C extends Context = Context> = satori.Context.Associate<P, C> | ||
export interface Config extends Config.Basic, Config.Advanced { | ||
@@ -125,0 +122,0 @@ i18n?: I18n.Config |
import * as utils from '@koishijs/utils' | ||
import { Dict, MaybeArray } from 'cosmokit' | ||
import { Database, Driver, Update } from 'minato' | ||
import { Driver, FlatKeys, FlatPick, Update } from 'minato' | ||
import * as minato from 'minato' | ||
import { Fragment, Universal } from '@satorijs/core' | ||
@@ -8,16 +9,26 @@ import { Context } from './context' | ||
declare module './context' { | ||
interface Events { | ||
'model'(name: keyof Tables): void | ||
interface Context { | ||
[minato.Types]: Types | ||
[minato.Tables]: Tables | ||
[Context.Database]: Context.Database<this> | ||
} | ||
interface Context { | ||
[Database.Tables]: Tables | ||
database: DatabaseService<this> | ||
model: DatabaseService<this> | ||
broadcast(content: Fragment, forced?: boolean): Promise<string[]> | ||
broadcast(channels: readonly string[], content: Fragment, forced?: boolean): Promise<string[]> | ||
namespace Context { | ||
interface Database<C extends Context = Context> { | ||
getUser<K extends FlatKeys<User>>(platform: string, pid: string, modifier?: Driver.Cursor<K>): Promise<FlatPick<User, K>> | ||
setUser(platform: string, pid: string, data: Update<User>): Promise<void> | ||
createUser(platform: string, pid: string, data: Partial<User>): Promise<User> | ||
getChannel<K extends FlatKeys<Channel>>(platform: string, id: MaybeArray<string>, modifier?: Driver.Cursor<K>): Promise<FlatPick<Channel, K> | FlatPick<Channel, K>[]> | ||
getAssignedChannels<K extends Channel.Field>(fields?: K[], selfIdMap?: Dict<string[]>): Promise<Pick<Channel, K>[]> | ||
setChannel(platform: string, id: string, data: Update<Channel>): Promise<void> | ||
createChannel(platform: string, id: string, data: Partial<Channel>): Promise<Channel> | ||
broadcast(content: Fragment, forced?: boolean): Promise<string[]> | ||
broadcast(channels: readonly string[], content: Fragment, forced?: boolean): Promise<string[]> | ||
} | ||
} | ||
} | ||
export interface Tables extends Database.Tables { | ||
export interface Types extends minato.Types {} | ||
export interface Tables extends minato.Tables { | ||
user: User | ||
@@ -77,16 +88,21 @@ binding: Binding | ||
export class DatabaseService< | ||
C extends Context = Context, | ||
S extends C[typeof Database.Tables] = C[typeof Database.Tables], | ||
N extends C[typeof Database.Types] = C[typeof Database.Types], | ||
> extends Database<C, S, N> { | ||
// workaround typescript | ||
private get self() { | ||
return this as any as DatabaseService<Context> | ||
} | ||
// Do not set "database" inject for this service. | ||
export class KoishiDatabase { | ||
constructor(public ctx: Context) { | ||
ctx.plugin(minato.Database) | ||
constructor(ctx: C) { | ||
super(ctx) | ||
ctx.set('koishi.database', this) | ||
this.self.extend('user', { | ||
ctx.mixin('koishi.database', { | ||
getUser: 'database.getUser', | ||
setUser: 'database.setUser', | ||
createUser: 'database.createUser', | ||
getChannel: 'database.getChannel', | ||
getAssignedChannels: 'database.getAssignedChannels', | ||
setChannel: 'database.setChannel', | ||
createChannel: 'database.createChannel', | ||
broadcast: 'broadcast', | ||
}) | ||
ctx.model.extend('user', { | ||
id: 'unsigned(8)', | ||
@@ -103,3 +119,3 @@ name: { type: 'string', length: 255 }, | ||
this.self.extend('binding', { | ||
ctx.model.extend('binding', { | ||
aid: 'unsigned(8)', | ||
@@ -113,3 +129,3 @@ bid: 'unsigned(8)', | ||
this.self.extend('channel', { | ||
ctx.model.extend('channel', { | ||
id: 'string(255)', | ||
@@ -128,4 +144,4 @@ platform: 'string(255)', | ||
ctx.on('login-added', ({ platform }) => { | ||
if (platform in this.tables.user.fields) return | ||
this.self.migrate('user', { [platform]: 'string(255)' }, async (db) => { | ||
if (platform in ctx.model.tables.user.fields) return | ||
ctx.model.migrate('user', { [platform]: 'string(255)' }, async (db) => { | ||
const users = await db.get('user', { [platform]: { $exists: true } }, ['id', platform as never]) | ||
@@ -142,6 +158,6 @@ await db.upsert('binding', users.filter(u => u[platform]).map((user) => ({ | ||
async getUser<K extends User.Field>(platform: string, pid: string, modifier?: Driver.Cursor<K>): Promise<Pick<User, K>> { | ||
const [binding] = await this.self.get('binding', { platform, pid }, ['aid']) | ||
async getUser<K extends FlatKeys<User>>(platform: string, pid: string, modifier?: Driver.Cursor<K>): Promise<FlatPick<User, K>> { | ||
const [binding] = await this.ctx.database.get('binding', { platform, pid }, ['aid']) | ||
if (!binding) return | ||
const [user] = await this.self.get('user', { id: binding.aid }, modifier) | ||
const [user] = await this.ctx.database.get('user', { id: binding.aid }, modifier) | ||
return user | ||
@@ -151,17 +167,17 @@ } | ||
async setUser(platform: string, pid: string, data: Update<User>) { | ||
const [binding] = await this.self.get('binding', { platform, pid }, ['aid']) | ||
const [binding] = await this.ctx.database.get('binding', { platform, pid }, ['aid']) | ||
if (!binding) throw new Error('user not found') | ||
return this.self.set('user', binding.aid, data) | ||
return this.ctx.database.set('user', binding.aid, data) | ||
} | ||
async createUser(platform: string, pid: string, data: Partial<User>) { | ||
const user = await this.self.create('user', data) | ||
await this.self.create('binding', { aid: user.id, bid: user.id, pid, platform }) | ||
const user = await this.ctx.database.create('user', data) | ||
await this.ctx.database.create('binding', { aid: user.id, bid: user.id, pid, platform }) | ||
return user | ||
} | ||
getChannel<K extends Channel.Field>(platform: string, id: string, modifier?: Driver.Cursor<K>): Promise<Pick<Channel, K | 'id' | 'platform'>> | ||
getChannel<K extends Channel.Field>(platform: string, ids: string[], modifier?: Driver.Cursor<K>): Promise<Pick<Channel, K>[]> | ||
getChannel<K extends FlatKeys<Channel>>(platform: string, id: string, modifier?: Driver.Cursor<K>): Promise<FlatPick<Channel, K | 'id' | 'platform'>> | ||
getChannel<K extends FlatKeys<Channel>>(platform: string, ids: string[], modifier?: Driver.Cursor<K>): Promise<FlatPick<Channel, K>[]> | ||
async getChannel(platform: string, id: MaybeArray<string>, modifier?: Driver.Cursor<Channel.Field>) { | ||
const data = await this.self.get('channel', { platform, id }, modifier) | ||
const data = await this.ctx.database.get('channel', { platform, id }, modifier) | ||
if (Array.isArray(id)) return data | ||
@@ -183,3 +199,3 @@ if (data[0]) Object.assign(data[0], { platform, id }) | ||
async getAssignedChannels(fields?: Channel.Field[], selfIdMap: Dict<string[]> = this.getSelfIds()) { | ||
return this.self.get('channel', { | ||
return this.ctx.database.get('channel', { | ||
$or: Object.entries(selfIdMap).map(([platform, assignee]) => ({ platform, assignee })), | ||
@@ -190,7 +206,7 @@ }, fields) | ||
setChannel(platform: string, id: string, data: Update<Channel>) { | ||
return this.self.set('channel', { platform, id }, data) | ||
return this.ctx.database.set('channel', { platform, id }, data) | ||
} | ||
createChannel(platform: string, id: string, data: Partial<Channel>) { | ||
return this.self.create('channel', { platform, id, ...data }) | ||
return this.ctx.database.create('channel', { platform, id, ...data }) | ||
} | ||
@@ -197,0 +213,0 @@ |
@@ -17,2 +17,4 @@ // @ts-ignore | ||
export { Tables, Types } from './database' | ||
export { version } |
@@ -82,3 +82,9 @@ import { Dict, remove } from 'cosmokit' | ||
constructor(public ctx: Context) {} | ||
constructor(public ctx: Context) { | ||
this.extend('intercept.http', Schema.object({ | ||
timeout: Schema.natural().role('ms').description('等待连接建立的最长时间。'), | ||
proxyAgent: Schema.string().description('使用的代理服务器地址。'), | ||
keepAlive: Schema.boolean().description('是否保持连接。'), | ||
})) | ||
} | ||
@@ -85,0 +91,0 @@ extend(name: string, schema: Schema, order = 0) { |
@@ -59,4 +59,2 @@ import { observe } from '@koishijs/utils' | ||
export interface Session extends Context.Associate<'session'> {} | ||
export class Session<U extends User.Field = never, G extends Channel.Field = never, C extends Context = Context> extends satori.Session<C> { | ||
@@ -63,0 +61,0 @@ static shadow = Symbol.for('session.shadow') |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
468612
8785
Updated@satorijs/core@^3.7.0
Updatedcordis@^3.14.0
Updatedminato@^3.2.0