Comparing version 3.2.0 to 3.3.0
@@ -6,5 +6,5 @@ import { Extract, Dict, Awaitable, MaybeArray, Intersect } from 'cosmokit'; | ||
interface FieldExpr<T = any> { | ||
$or?: FieldQuery<T>[]; | ||
$and?: FieldQuery<T>[]; | ||
$not?: FieldQuery<T>; | ||
$or?: Field<T>[]; | ||
$and?: Field<T>[]; | ||
$not?: Field<T>; | ||
$exists?: boolean; | ||
@@ -19,3 +19,3 @@ $in?: Extract<T, Indexable, T[]>; | ||
$lte?: Extract<T, Comparable>; | ||
$el?: T extends (infer U)[] ? FieldQuery<U> : never; | ||
$el?: T extends (infer U)[] ? Field<U> : never; | ||
$size?: Extract<T, any[], number>; | ||
@@ -37,5 +37,5 @@ $regex?: Extract<T, string, string | RegExp>; | ||
type Shorthand<T = any> = Extract<T, Comparable> | Extract<T, Indexable, T[]> | Extract<T, string, RegExp>; | ||
type FieldQuery<T = any> = FieldExpr<T> | Shorthand<T>; | ||
type Field<T = any> = FieldExpr<T> | Shorthand<T>; | ||
type Expr<T = any> = LogicalExpr<T> & { | ||
[K in keyof T]?: null | FieldQuery<T[K]>; | ||
[K in keyof T]?: null | Field<T[K]>; | ||
}; | ||
@@ -118,3 +118,4 @@ } | ||
evaluate(): Eval.Expr<S[], boolean>; | ||
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>; | ||
@@ -298,2 +299,3 @@ } | ||
export function fromTerm<T>(value: Eval.Term<T>): Type<T>; | ||
export function fromTerms(values: Eval.Term<any>[], initial?: Type): Type; | ||
export function isType(value: any): value is Type; | ||
@@ -366,5 +368,6 @@ export function isArray(type: Type): boolean | undefined; | ||
regex<A extends boolean>(x: Term<string, A>, y: Term<string, A> | Term<RegExp, A>): Expr<boolean, A>; | ||
and: Multi<boolean, boolean>; | ||
or: Multi<boolean, boolean>; | ||
not: Unary<boolean, boolean>; | ||
and: Multi<boolean, boolean> & Multi<number, number> & Multi<bigint, bigint>; | ||
or: Multi<boolean, boolean> & Multi<number, number> & Multi<bigint, bigint>; | ||
not: Unary<boolean, boolean> & Unary<number, number> & Unary<bigint, bigint>; | ||
xor: Multi<boolean, boolean> & Multi<number, number> & Multi<bigint, bigint>; | ||
literal<T>(value: T, type?: Type<T> | Field.Type<T> | Field.NewType<T> | string): Expr<T, false>; | ||
@@ -413,3 +416,3 @@ number: Unary<any, number>; | ||
} | ||
export type Indexable = string | number; | ||
export type Indexable = string | number | bigint; | ||
export type Comparable = string | number | boolean | bigint | Date; | ||
@@ -482,2 +485,3 @@ type FlatWrap<S, A extends 0[], P extends string> = { | ||
join<X extends Join2.Input<S>>(tables: X, callback?: Join2.Predicate<S, X>, optional?: Dict<boolean, Keys<X>>): Selection<Join2.Output<S, X>>; | ||
get<K extends Keys<S>>(table: K, query: Query<S[K]>): Promise<S[K][]>; | ||
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>[]>; | ||
@@ -484,0 +488,0 @@ eval<K extends Keys<S>, T>(table: K, expr: Selection.Callback<S[K], T, true>, query?: Query<S[K]>): Promise<T>; |
{ | ||
"name": "minato", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"description": "Type Driven Database Framework", | ||
@@ -24,2 +24,6 @@ "type": "module", | ||
"author": "Shigma <shigma10826@gmail.com>", | ||
"contributors": [ | ||
"Shigma <shigma10826@gmail.com>", | ||
"Hieuzest <girkirin@hotmail.com>" | ||
], | ||
"license": "MIT", | ||
@@ -43,8 +47,26 @@ "repository": { | ||
"mongo", | ||
"postgres" | ||
"postgres", | ||
"cordis", | ||
"plugin" | ||
], | ||
"cordis": { | ||
"ecosystem": { | ||
"pattern": [ | ||
"@minatojs/driver-*", | ||
"@minatojs/plugin-*", | ||
"minato-plugin-*" | ||
] | ||
}, | ||
"service": { | ||
"implements": [ | ||
"model" | ||
] | ||
} | ||
}, | ||
"peerDependencies": { | ||
"cordis": "^3.15.0" | ||
}, | ||
"dependencies": { | ||
"cordis": "^3.14.0", | ||
"cosmokit": "^1.6.2" | ||
} | ||
} |
@@ -247,3 +247,3 @@ import { defineProperty, Dict, makeArray, mapValues, MaybeArray, omit } from 'cosmokit' | ||
} | ||
const sels = mapValues(tables, (t: TableLike<S>) => { | ||
let sels = mapValues(tables, (t: TableLike<S>) => { | ||
return typeof t === 'string' ? this.select(t) : t | ||
@@ -254,2 +254,6 @@ }) | ||
if (drivers.size !== 1) throw new Error('cannot join tables from different drivers') | ||
if (Object.keys(sels).length === 2 && (optional?.[0] || optional?.[Object.keys(sels)[0]])) { | ||
if (optional[1] || optional[Object.keys(sels)[1]]) throw new Error('full join is not supported') | ||
sels = Object.fromEntries(Object.entries(sels).reverse()) | ||
} | ||
const sel = new Selection([...drivers][0], sels) | ||
@@ -266,2 +270,4 @@ if (Array.isArray(oldTables)) { | ||
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>( | ||
@@ -271,4 +277,6 @@ table: 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 | ||
} | ||
@@ -275,0 +283,0 @@ |
@@ -96,7 +96,15 @@ import { Awaitable, Dict, mapValues, remove } from 'cosmokit' | ||
if (table instanceof Selection) { | ||
if (!table.args[0].fields) return table.model | ||
if (!table.args[0].fields && (typeof table.table === 'string' || table.table instanceof Selection)) { | ||
return table.model | ||
} | ||
const model = new Model('temp') | ||
model.fields = mapValues(table.args[0].fields, (expr, key) => ({ | ||
type: Type.fromTerm(expr), | ||
})) | ||
if (table.args[0].fields) { | ||
model.fields = mapValues(table.args[0].fields, (expr) => ({ | ||
type: Type.fromTerm(expr), | ||
})) | ||
} else { | ||
model.fields = mapValues(table.model.fields, (field) => ({ | ||
type: Type.fromField(field), | ||
})) | ||
} | ||
return model | ||
@@ -103,0 +111,0 @@ } |
@@ -114,6 +114,7 @@ import { defineProperty, isNullable, mapValues } from 'cosmokit' | ||
// logical | ||
and: Multi<boolean, boolean> | ||
or: Multi<boolean, boolean> | ||
not: Unary<boolean, boolean> | ||
// logical / bitwise | ||
and: Multi<boolean, boolean> & Multi<number, number> & Multi<bigint, bigint> | ||
or: Multi<boolean, boolean> & Multi<number, number> & Multi<bigint, bigint> | ||
not: Unary<boolean, boolean> & Unary<number, number> & Unary<bigint, bigint> | ||
xor: Multi<boolean, boolean> & Multi<number, number> & Multi<bigint, bigint> | ||
@@ -219,6 +220,27 @@ // typecast | ||
// logical | ||
Eval.and = multary('and', (args, data) => args.every(arg => executeEval(data, arg)), Type.Boolean) | ||
Eval.or = multary('or', (args, data) => args.some(arg => executeEval(data, arg)), Type.Boolean) | ||
Eval.not = unary('not', (value, data) => !executeEval(data, value), Type.Boolean) | ||
// logical / bitwise | ||
Eval.and = multary('and', (args, data) => { | ||
const type = Type.fromTerms(args, Type.Boolean) | ||
if (Field.boolean.includes(type.type)) return args.every(arg => executeEval(data, arg)) | ||
else if (Field.number.includes(type.type)) return args.map(arg => executeEval(data, arg)).reduce((prev, curr) => prev & curr) | ||
else if (type.type === 'bigint') return args.map(arg => BigInt(executeEval(data, arg) ?? 0)).reduce((prev, curr) => prev & curr) | ||
}, (...args) => Type.fromTerms(args, Type.Boolean)) | ||
Eval.or = multary('or', (args, data) => { | ||
const type = Type.fromTerms(args, Type.Boolean) | ||
if (Field.boolean.includes(type.type)) return args.some(arg => executeEval(data, arg)) | ||
else if (Field.number.includes(type.type)) return args.map(arg => executeEval(data, arg)).reduce((prev, curr) => prev | curr) | ||
else if (type.type === 'bigint') return args.map(arg => BigInt(executeEval(data, arg) ?? 0)).reduce((prev, curr) => prev | curr) | ||
}, (...args) => Type.fromTerms(args, Type.Boolean)) | ||
Eval.not = unary('not', (value, data) => { | ||
const type = Type.fromTerms([value], Type.Boolean) | ||
if (Field.boolean.includes(type.type)) return !executeEval(data, value) | ||
else if (Field.number.includes(type.type)) return ~executeEval(data, value) as any | ||
else if (type.type === 'bigint') return ~BigInt(executeEval(data, value) ?? 0) | ||
}, (value) => Type.fromTerms([value], Type.Boolean)) | ||
Eval.xor = multary('xor', (args, data) => { | ||
const type = Type.fromTerms(args, Type.Boolean) | ||
if (Field.boolean.includes(type.type)) return args.map(arg => executeEval(data, arg)).reduce((prev, curr) => prev !== curr) | ||
else if (Field.number.includes(type.type)) return args.map(arg => executeEval(data, arg)).reduce((prev, curr) => prev ^ curr) | ||
else if (type.type === 'bigint') return args.map(arg => BigInt(executeEval(data, arg) ?? 0)).reduce((prev, curr) => prev ^ curr) | ||
}, (...args) => Type.fromTerms(args, Type.Boolean)) | ||
@@ -225,0 +247,0 @@ // typecast |
@@ -30,3 +30,7 @@ import { Database } from './database.ts' | ||
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> {} | ||
@@ -33,0 +37,0 @@ } |
@@ -11,5 +11,5 @@ import { Extract, isNullable } from 'cosmokit' | ||
// logical | ||
$or?: FieldQuery<T>[] | ||
$and?: FieldQuery<T>[] | ||
$not?: FieldQuery<T> | ||
$or?: Field<T>[] | ||
$and?: Field<T>[] | ||
$not?: Field<T> | ||
@@ -32,3 +32,3 @@ // existence | ||
// list | ||
$el?: T extends (infer U)[] ? FieldQuery<U> : never | ||
$el?: T extends (infer U)[] ? Field<U> : never | ||
$size?: Extract<T, any[], number> | ||
@@ -60,6 +60,6 @@ | ||
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]> | ||
} | ||
@@ -108,3 +108,3 @@ } | ||
function executeFieldQuery(query: Query.FieldQuery, data: any) { | ||
function executeFieldQuery(query: Query.Field, data: any) { | ||
// shorthand syntax | ||
@@ -111,0 +111,0 @@ if (Array.isArray(query)) { |
@@ -1,2 +0,2 @@ | ||
import { defineProperty, Dict, filterKeys, mapValues } from 'cosmokit' | ||
import { defineProperty, Dict, filterKeys } from 'cosmokit' | ||
import { Driver } from './driver.ts' | ||
@@ -122,3 +122,10 @@ import { Eval, executeEval } from './eval.ts' | ||
} else { | ||
return mapValues(fields, field => this.resolveField(field)) | ||
const entries = Object.entries(fields).flatMap(([key, field]) => { | ||
const expr = this.resolveField(field) | ||
if (expr['$object']) { | ||
return Object.entries(expr['$object']).map(([key2, expr2]) => [`${key}.${key2}`, expr2]) | ||
} | ||
return [[key, expr]] | ||
}) | ||
return Object.fromEntries(entries) | ||
} | ||
@@ -254,3 +261,4 @@ } | ||
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> | ||
@@ -257,0 +265,0 @@ async execute(cursor?: any) { |
@@ -68,2 +68,6 @@ import { Binary, defineProperty, isNullable, mapValues } from 'cosmokit' | ||
export function fromTerms(values: Eval.Term<any>[], initial?: Type): Type { | ||
return values.map(fromTerm).find((type) => type.type !== 'expr') ?? initial ?? fromField('expr') | ||
} | ||
export function isType(value: any): value is Type { | ||
@@ -70,0 +74,0 @@ return value?.[kType] === true |
@@ -32,3 +32,3 @@ import { Intersect } from 'cosmokit' | ||
export type Indexable = string | number | ||
export type Indexable = string | number | bigint | ||
export type Comparable = string | number | boolean | bigint | Date | ||
@@ -35,0 +35,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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
280418
4877
- Removedcordis@^3.14.0