Skip to content

Commit

Permalink
feat(minato): enhance typings, add overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 6, 2024
1 parent 538b748 commit 728b4d8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
8 changes: 6 additions & 2 deletions packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,16 @@ export class Database<S = {}, N = {}, C extends Context = Context> extends Servi
return this.select(sel)
}

async get<K extends Keys<S>>(table: K, query: Query<S[K]>): Promise<S[K][]>

async get<K extends Keys<S>, P extends FlatKeys<S[K]> = any>(
table: K,
query: Query<S[K]>,
cursor?: Driver.Cursor<P>,
): Promise<FlatPick<S[K], P>[]> {
return this.select(table, query).execute(cursor)
): Promise<FlatPick<S[K], P>[]>

async get<K extends Keys<S>>(table: K, query: Query<S[K]>, cursor?: any) {
return this.select(table, query).execute(cursor) as any
}

async eval<K extends Keys<S>, T>(table: K, expr: Selection.Callback<S[K], T, true>, query?: Query<S[K]>): Promise<T> {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ declare module 'cordis' {
namespace Context {
const Minato: unique symbol
const Database: unique symbol
// https://github.com/typescript-eslint/typescript-eslint/issues/6720
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Minato<C extends Context = Context> {}
// https://github.com/typescript-eslint/typescript-eslint/issues/6720
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Database<C extends Context = Context> {}
}
}
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export type Query<T = any> = Query.Expr<Flatten<T>> | Query.Shorthand<Indexable>
export namespace Query {
export interface FieldExpr<T = any> {
// logical
$or?: FieldQuery<T>[]
$and?: FieldQuery<T>[]
$not?: FieldQuery<T>
$or?: Field<T>[]
$and?: Field<T>[]
$not?: Field<T>

// existence
$exists?: boolean
Expand All @@ -28,7 +28,7 @@ export namespace Query {
$lte?: Extract<T, Comparable>

// list
$el?: T extends (infer U)[] ? FieldQuery<U> : never
$el?: T extends (infer U)[] ? Field<U> : never
$size?: Extract<T, any[], number>

// regexp
Expand All @@ -55,10 +55,10 @@ export namespace Query {
| Extract<T, Indexable, T[]>
| Extract<T, string, RegExp>

export type FieldQuery<T = any> = FieldExpr<T> | Shorthand<T>
export type Field<T = any> = FieldExpr<T> | Shorthand<T>

export type Expr<T = any> = LogicalExpr<T> & {
[K in keyof T]?: null | FieldQuery<T[K]>
[K in keyof T]?: null | Field<T[K]>
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ const queryOperators: QueryOperators = {
$size: (query, data) => data.length === query,
}

function executeFieldQuery(query: Query.FieldQuery, data: any) {
function executeFieldQuery(query: Query.Field, data: any) {
// shorthand syntax
if (Array.isArray(query)) {
return query.includes(data)
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ export class Selection<S = any> extends Executable<S, S[]> {
return Eval.exec(selection._action('eval', expr))
}

execute<K extends FlatKeys<S> = any>(cursor?: Driver.Cursor<K>): Promise<Extract<FlatPick<S, K>, S>[]>
execute(): Promise<S[]>
execute<K extends FlatKeys<S> = any>(cursor?: Driver.Cursor<K>): Promise<FlatPick<S, K>[]>
execute<T>(callback: Selection.Callback<S, T, true>): Promise<T>
async execute(cursor?: any) {
if (typeof cursor === 'function') {
Expand Down

0 comments on commit 728b4d8

Please sign in to comment.