@koishijs/core
Advanced tools
Comparing version 4.17.3 to 4.17.4
@@ -14,7 +14,13 @@ import * as utils from '@koishijs/utils'; | ||
export interface Context { | ||
database: DatabaseService; | ||
model: DatabaseService; | ||
[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[]>; | ||
} | ||
export interface Tables extends Database.Tables { | ||
user: User; | ||
binding: Binding; | ||
channel: Channel; | ||
} | ||
export interface User { | ||
@@ -62,9 +68,5 @@ id: number; | ||
} | ||
export interface Tables { | ||
user: User; | ||
binding: Binding; | ||
channel: Channel; | ||
} | ||
export class DatabaseService extends Database<Tables, Context> { | ||
constructor(ctx: Context); | ||
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>>; | ||
@@ -559,6 +561,6 @@ setUser(platform: string, pid: string, data: Update<User>): Promise<Driver.WriteResult>; | ||
sendQueued(content: Fragment, delay?: number): Promise<string[]>; | ||
getChannel<K extends Channel.Field = never>(id?: string, fields?: K[]): Promise<Pick<Channel, "id" | "platform" | K>>; | ||
getChannel<K extends Channel.Field = never>(id?: string, fields?: K[]): Promise<any>; | ||
_observeChannelLike<K extends Channel.Field = never>(channelId: string, fields?: Iterable<K>): Promise<Channel.Observed<keyof Channel>>; | ||
observeChannel<T extends Channel.Field = never>(fields: Iterable<T>): Promise<Channel.Observed<T | G>>; | ||
getUser<K extends User.Field = never>(userId?: string, fields?: K[]): Promise<Pick<User, K>>; | ||
getUser<K extends User.Field = never>(userId?: string, fields?: K[]): Promise<any>; | ||
observeUser<T extends User.Field = never>(fields: Iterable<T>): Promise<User.Observed<T | U>>; | ||
@@ -565,0 +567,0 @@ withScope(scope: string, callback: () => Awaitable<string>): Promise<string>; |
{ | ||
"name": "@koishijs/core", | ||
"description": "Core Features for Koishi", | ||
"version": "4.17.3", | ||
"version": "4.17.4", | ||
"main": "lib/index.cjs", | ||
@@ -35,9 +35,9 @@ "module": "lib/index.mjs", | ||
"@koishijs/i18n-utils": "^1.0.1", | ||
"@koishijs/utils": "^7.1.2", | ||
"@satorijs/core": "^3.6.5", | ||
"@koishijs/utils": "^7.2.1", | ||
"@satorijs/core": "^3.6.6", | ||
"cordis": "^3.13.4", | ||
"cosmokit": "^1.5.2", | ||
"cosmokit": "^1.6.2", | ||
"fastest-levenshtein": "^1.0.16", | ||
"minato": "^3.0.2" | ||
"minato": "^3.1.1" | ||
} | ||
} |
@@ -13,4 +13,5 @@ import * as utils from '@koishijs/utils' | ||
interface Context { | ||
database: DatabaseService | ||
model: DatabaseService | ||
[Database.Tables]: Tables | ||
database: DatabaseService<this> | ||
model: DatabaseService<this> | ||
broadcast(content: Fragment, forced?: boolean): Promise<string[]> | ||
@@ -21,2 +22,8 @@ broadcast(channels: readonly string[], content: Fragment, forced?: boolean): Promise<string[]> | ||
export interface Tables extends Database.Tables { | ||
user: User | ||
binding: Binding | ||
channel: Channel | ||
} | ||
export interface User { | ||
@@ -71,13 +78,16 @@ id: number | ||
export interface Tables { | ||
user: User | ||
binding: Binding | ||
channel: Channel | ||
} | ||
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> | ||
} | ||
export class DatabaseService extends Database<Tables, Context> { | ||
constructor(ctx: Context) { | ||
constructor(ctx: C) { | ||
super(ctx) | ||
this.extend('user', { | ||
this.self.extend('user', { | ||
id: 'unsigned(8)', | ||
@@ -94,3 +104,3 @@ name: { type: 'string', length: 255 }, | ||
this.extend('binding', { | ||
this.self.extend('binding', { | ||
aid: 'unsigned(8)', | ||
@@ -104,3 +114,3 @@ bid: 'unsigned(8)', | ||
this.extend('channel', { | ||
this.self.extend('channel', { | ||
id: 'string(255)', | ||
@@ -120,3 +130,3 @@ platform: 'string(255)', | ||
if (platform in this.tables.user.fields) return | ||
this.migrate('user', { [platform]: 'string(255)' }, async (db) => { | ||
this.self.migrate('user', { [platform]: 'string(255)' }, async (db) => { | ||
const users = await db.get('user', { [platform]: { $exists: true } }, ['id', platform as never]) | ||
@@ -134,5 +144,5 @@ 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.get('binding', { platform, pid }, ['aid']) | ||
const [binding] = await this.self.get('binding', { platform, pid }, ['aid']) | ||
if (!binding) return | ||
const [user] = await this.get('user', { id: binding.aid }, modifier) | ||
const [user] = await this.self.get('user', { id: binding.aid }, modifier) | ||
return user | ||
@@ -142,10 +152,10 @@ } | ||
async setUser(platform: string, pid: string, data: Update<User>) { | ||
const [binding] = await this.get('binding', { platform, pid }, ['aid']) | ||
const [binding] = await this.self.get('binding', { platform, pid }, ['aid']) | ||
if (!binding) throw new Error('user not found') | ||
return this.set('user', binding.aid, data) | ||
return this.self.set('user', binding.aid, data) | ||
} | ||
async createUser(platform: string, pid: string, data: Partial<User>) { | ||
const user = await this.create('user', data) | ||
await this.create('binding', { aid: user.id, bid: user.id, pid, platform }) | ||
const user = await this.self.create('user', data) | ||
await this.self.create('binding', { aid: user.id, bid: user.id, pid, platform }) | ||
return user | ||
@@ -157,3 +167,3 @@ } | ||
async getChannel(platform: string, id: MaybeArray<string>, modifier?: Driver.Cursor<Channel.Field>) { | ||
const data = await this.get('channel', { platform, id }, modifier) | ||
const data = await this.self.get('channel', { platform, id }, modifier) | ||
if (Array.isArray(id)) return data | ||
@@ -175,3 +185,3 @@ if (data[0]) Object.assign(data[0], { platform, id }) | ||
async getAssignedChannels(fields?: Channel.Field[], selfIdMap: Dict<string[]> = this.getSelfIds()) { | ||
return this.get('channel', { | ||
return this.self.get('channel', { | ||
$or: Object.entries(selfIdMap).map(([platform, assignee]) => ({ platform, assignee })), | ||
@@ -182,7 +192,7 @@ }, fields) | ||
setChannel(platform: string, id: string, data: Update<Channel>) { | ||
return this.set('channel', { platform, id }, data) | ||
return this.self.set('channel', { platform, id }, data) | ||
} | ||
createChannel(platform: string, id: string, data: Partial<Channel>) { | ||
return this.create('channel', { platform, id, ...data }) | ||
return this.self.create('channel', { platform, id, ...data }) | ||
} | ||
@@ -189,0 +199,0 @@ |
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
464407
8735
Updated@koishijs/utils@^7.2.1
Updated@satorijs/core@^3.6.6
Updatedcosmokit@^1.6.2
Updatedminato@^3.1.1