Skip to content

Commit

Permalink
fix(core): fix typing issues, support database generics
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 15, 2024
1 parent eb26c8e commit 40b5647
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 22 deletions.
17 changes: 6 additions & 11 deletions packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ export namespace Database {
export interface Types {}
}

export class Database<
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 Service<undefined, C> {
export class Database<S = {}, N = {}, C extends Context = Context> extends Service<undefined, C> {
static [Service.provide] = 'model'
static [Service.immediate] = true
static readonly Tables = Symbol('minato.tables')
Expand Down Expand Up @@ -202,13 +198,12 @@ export class Database<
return type
}

// FIXME
// define<K extends Exclude<Keys<N>, Field.Type | 'object' | 'array'>>(
// name: K,
// field: Field.Definition<N[K], N> | Field.Transform<N[K], any, N>,
// ): K
define<K extends Exclude<Keys<N>, Field.Type | 'object' | 'array'>>(
name: K,
field: Field.Definition<N[K], N> | Field.Transform<N[K], any, N>,
): K

// define<T>(field: Field.Definition<T, N> | Field.Transform<T, any, N>): Field.NewType<T>
define<T>(field: Field.Definition<T, N> | Field.Transform<T, any, N>): Field.NewType<T>
define(name: any, field?: any) {
if (typeof name === 'object') {
field = name
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 @@ -61,7 +61,7 @@ export abstract class Driver<T = any, C extends Context = Context> {
abstract upsert(sel: Selection.Mutable, data: any[], keys: string[]): Promise<Driver.WriteResult>
abstract withTransaction(callback: (driver: this) => Promise<void>): Promise<void>

public database: Database<C>
public database: Database<any, any, C>
public logger: Logger
public types: Dict<Driver.Transformer> = Object.create(null)

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ declare module 'cordis' {
interface Context {
[Database.Tables]: Database.Tables
[Database.Types]: Database.Types
database: Database<this>
model: Database<this>
database: Database<this[typeof Database.Tables], this[typeof Database.Types], this>
model: Database<this[typeof Database.Tables], this[typeof Database.Types], this>
}
}

Expand Down
9 changes: 2 additions & 7 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import { Eval, isEvalExpr } from './eval.ts'
import { Flatten, Keys, unravel } from './utils.ts'
import { Type } from './type.ts'
import { Driver } from './driver.ts'
import { Selection } from './selection.ts'

export const Primary = Symbol('Primary')
export type Primary = (string | number) & { [Primary]: true }

export interface Field<T = any> {
// FIXME Type<T>
type: Field.Type<T> | Type<T>
type: Type<T>
deftype?: Field.Type<T>
length?: number
nullable?: boolean
Expand Down Expand Up @@ -77,11 +75,8 @@ export namespace Field {
type: Type<T> | Field<T>['type']
} & Omit<Field<T>, 'type'>

// FIXME
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type MapField<O = any, N = any> = {
// [K in keyof O]?: Literal<O[K], N> | Definition<O[K], N> | Transform<O[K], any, N>
[K in keyof O]?: Field<O[K]> | Shorthand<Type<O[K]>> | Selection.Callback<O, O[K]>
[K in keyof O]?: Literal<O[K], N> | Definition<O[K], N> | Transform<O[K], any, N>
}

export type Extension<O = any, N = any> = MapField<Flatten<O>, N>
Expand Down
2 changes: 1 addition & 1 deletion packages/mysql/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@minatojs/driver-mysql",
"version": "3.1.0",
"version": "3.1.1",
"description": "MySQL Driver for Minato",
"type": "module",
"main": "lib/index.cjs",
Expand Down

0 comments on commit 40b5647

Please sign in to comment.