Comparing version 3.1.0 to 3.1.1
@@ -98,3 +98,3 @@ import { Extract, Dict, Awaitable, MaybeArray, Intersect } from 'cosmokit'; | ||
tables: Dict<Model>; | ||
constructor(driver: Driver, table: string | Selection | Dict<Selection.Immutable>, query?: Query); | ||
constructor(driver: Driver<any>, table: string | Selection | Dict<Selection.Immutable>, query?: Query); | ||
where(query: Query<S>): this; | ||
@@ -151,4 +151,4 @@ limit(limit: number): this; | ||
} | ||
export abstract class Driver<T = any> { | ||
ctx: Context; | ||
export abstract class Driver<T = any, C extends Context = Context> { | ||
ctx: C; | ||
config: T; | ||
@@ -169,6 +169,6 @@ static inject: string[]; | ||
abstract withTransaction(callback: (driver: this) => Promise<void>): Promise<void>; | ||
database: Database; | ||
database: Database<C>; | ||
logger: Logger; | ||
types: Dict<Driver.Transformer>; | ||
constructor(ctx: Context, config: T); | ||
constructor(ctx: C, config: T); | ||
model<S = any>(table: string | Selection.Immutable | Dict<string | Selection.Immutable>): Model<S>; | ||
@@ -189,3 +189,3 @@ migrate(name: string, hooks: MigrationHooks): Promise<void>; | ||
export interface Field<T = any> { | ||
type: Type<T>; | ||
type: Field.Type<T> | Type<T>; | ||
deftype?: Field.Type<T>; | ||
@@ -232,3 +232,3 @@ length?: number; | ||
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]>; | ||
}; | ||
@@ -247,3 +247,3 @@ export type Extension<O = any, N = any> = MapField<Flatten<O>, N>; | ||
export namespace Model { | ||
type Migration = (database: Database) => Promise<void>; | ||
type Migration<D = any> = (database: D) => Promise<void>; | ||
interface Config<O = {}> { | ||
@@ -264,3 +264,3 @@ callback?: Migration; | ||
fields: Field.Config<S>; | ||
migrations: Map<Model.Migration, string[]>; | ||
migrations: Map<Model.Migration<any>, string[]>; | ||
private type; | ||
@@ -280,3 +280,3 @@ constructor(name: string); | ||
[Type.kType]?: true; | ||
type: Field.Type<T> | Keys<N, T> | Field.NewType<T>; | ||
type: Field.Type<T>; | ||
inner?: T extends (infer I)[] ? Type<I, N> : Field.Type<T> extends 'json' ? { | ||
@@ -298,3 +298,3 @@ [key in keyof T]: Type<T[key], N>; | ||
export function fromPrimitive<T>(value: T): Type<T>; | ||
export function fromField<T, N>(field: Type | Field<T> | Field.Type<T> | Keys<N, T> | Field.NewType<T>): Type<T, N>; | ||
export function fromField<T, N>(field: any): Type<T, N>; | ||
export function fromTerm<T>(value: Eval.Term<T>): Type<T>; | ||
@@ -450,9 +450,15 @@ export function isType(value: any): value is Type; | ||
} | ||
export class Database<S = any, N = any, C extends Context = Context> extends Service<undefined, C> { | ||
export namespace Database { | ||
interface Tables { | ||
} | ||
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> { | ||
static [Service.provide]: string; | ||
static [Service.immediate]: boolean; | ||
tables: { | ||
[K in Keys<S>]: Model<S[K]>; | ||
}; | ||
drivers: Record<keyof any, Driver>; | ||
static readonly Tables: unique symbol; | ||
static readonly Types: unique symbol; | ||
tables: any; | ||
drivers: Record<keyof any, any>; | ||
types: Dict<Field.Transform>; | ||
@@ -471,5 +477,4 @@ migrating: boolean; | ||
private parseField; | ||
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<S>(field: Field.Definition<S, N> | Field.Transform<S, any, N>): Field.NewType<S>; | ||
migrate<K extends Keys<S>>(name: K, fields: Field.Extension<S[K], N>, callback: Model.Migration): void; | ||
define(name: any, field?: any): any; | ||
migrate<K extends Keys<S>>(name: K, fields: Field.Extension<S[K], N>, callback: Model.Migration<this>): void; | ||
select<T>(table: Selection<T>, query?: Query<T>): Selection<T>; | ||
@@ -506,11 +511,9 @@ select<T extends Keys<S>>(table: T, query?: Query<S[T]>): Selection<S[T]>; | ||
interface Context { | ||
database: Database; | ||
model: Database; | ||
[Database.Tables]: Database.Tables; | ||
[Database.Types]: Database.Types; | ||
database: Database<this>; | ||
model: Database<this>; | ||
} | ||
} | ||
export interface Tables { | ||
} | ||
export interface Types { | ||
} | ||
export { Logger, Schema, Schema as z } from 'cordis'; | ||
export default Database; |
{ | ||
"name": "minato", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "Type Driven Database Framework", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -51,8 +51,21 @@ import { Dict, Intersect, makeArray, mapValues, MaybeArray, omit, valueMap } from 'cosmokit' | ||
export class Database<S = any, N = any, C extends Context = Context> extends Service<undefined, C> { | ||
export namespace Database { | ||
export interface Tables {} | ||
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> { | ||
static [Service.provide] = 'model' | ||
static [Service.immediate] = true | ||
static readonly Tables = Symbol('minato.tables') | ||
static readonly Types = Symbol('minato.types') | ||
public tables: { [K in Keys<S>]: Model<S[K]> } = Object.create(null) | ||
public drivers: Record<keyof any, Driver> = Object.create(null) | ||
// { [K in Keys<S>]: Model<S[K]> } | ||
public tables: any = Object.create(null) | ||
public drivers: Record<keyof any, any> = Object.create(null) | ||
public types: Dict<Field.Transform> = Object.create(null) | ||
@@ -83,3 +96,3 @@ public migrating = false | ||
private getDriver(table: any) { | ||
private getDriver(table: any): Driver<any, C> { | ||
// const model: Model = this.tables[name] | ||
@@ -194,4 +207,9 @@ // if (model.driver) return this.drivers[model.driver] | ||
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<S>(field: Field.Definition<S, N> | Field.Transform<S, any, N>): Field.NewType<S> | ||
// 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<T>(field: Field.Definition<T, N> | Field.Transform<T, any, N>): Field.NewType<T> | ||
define(name: any, field?: any) { | ||
@@ -218,3 +236,7 @@ if (typeof name === 'object') { | ||
migrate<K extends Keys<S>>(name: K, fields: Field.Extension<S[K], N>, callback: Model.Migration) { | ||
migrate<K extends Keys<S>>( | ||
name: K, | ||
fields: Field.Extension<S[K], N>, | ||
callback: Model.Migration<this>, | ||
) { | ||
this.extend(name, fields, { callback }) | ||
@@ -229,4 +251,14 @@ } | ||
join<const U extends Join1.Input<S>>(tables: U, callback?: Join1.Predicate<S, U>, optional?: boolean[]): Selection<Join1.Output<S, U>> | ||
join<const U extends Join2.Input<S>>(tables: U, callback?: Join2.Predicate<S, U>, optional?: Dict<boolean, Keys<U>>): Selection<Join2.Output<S, U>> | ||
join<const U extends Join1.Input<S>>( | ||
tables: U, | ||
callback?: Join1.Predicate<S, U>, | ||
optional?: boolean[], | ||
): Selection<Join1.Output<S, U>> | ||
join<const U extends Join2.Input<S>>( | ||
tables: U, | ||
callback?: Join2.Predicate<S, U>, | ||
optional?: Dict<boolean, Keys<U>>, | ||
): Selection<Join2.Output<S, U>> | ||
join(tables: any, query?: any, optional?: any) { | ||
@@ -241,3 +273,5 @@ if (Array.isArray(tables)) { | ||
} else { | ||
const sel = new Selection(this.getDriver(Object.values(tables)[0]), valueMap(tables, (t: TableLike<S>) => typeof t === 'string' ? this.select(t) : t)) | ||
const sel = new Selection(this.getDriver(Object.values(tables)[0]), valueMap(tables, (t: TableLike<S>) => { | ||
return typeof t === 'string' ? this.select(t) : t | ||
})) | ||
if (typeof query === 'function') { | ||
@@ -251,3 +285,7 @@ sel.args[0].having = Eval.and(query(sel.row)) | ||
async get<T extends Keys<S>, K extends Keys<S[T]>>(table: T, query: Query<S[T]>, cursor?: Driver.Cursor<K>): Promise<Pick<S[T], K>[]> { | ||
async get<T extends Keys<S>, K extends Keys<S[T]>>( | ||
table: T, | ||
query: Query<S[T]>, | ||
cursor?: Driver.Cursor<K>, | ||
): Promise<Pick<S[T], K>[]> { | ||
return this.select(table, query).execute(cursor) | ||
@@ -260,3 +298,7 @@ } | ||
async set<T extends Keys<S>>(table: T, query: Query<S[T]>, update: Row.Computed<S[T], Update<S[T]>>): Promise<Driver.WriteResult> { | ||
async set<T extends Keys<S>>( | ||
table: T, | ||
query: Query<S[T]>, | ||
update: Row.Computed<S[T], Update<S[T]>>, | ||
): Promise<Driver.WriteResult> { | ||
const sel = this.select(table, query) | ||
@@ -263,0 +305,0 @@ if (typeof update === 'function') update = update(sel.row) |
@@ -47,3 +47,3 @@ import { Awaitable, Dict, valueMap } from 'cosmokit' | ||
export abstract class Driver<T = any> { | ||
export abstract class Driver<T = any, C extends Context = Context> { | ||
static inject = ['model'] | ||
@@ -65,7 +65,7 @@ | ||
public database: Database | ||
public database: Database<C> | ||
public logger: Logger | ||
public types: Dict<Driver.Transformer> = Object.create(null) | ||
constructor(public ctx: Context, public config: T) { | ||
constructor(public ctx: C, public config: T) { | ||
this.database = ctx.model | ||
@@ -72,0 +72,0 @@ this.logger = ctx.logger(this.constructor.name) |
@@ -19,13 +19,11 @@ import { Database } from './database.ts' | ||
interface Context { | ||
database: Database | ||
model: Database | ||
[Database.Tables]: Database.Tables | ||
[Database.Types]: Database.Types | ||
database: Database<this> | ||
model: Database<this> | ||
} | ||
} | ||
export interface Tables {} | ||
export interface Types {} | ||
export { Logger, Schema, Schema as z } from 'cordis' | ||
export default Database |
import { Binary, clone, isNullable, makeArray, MaybeArray, valueMap } from 'cosmokit' | ||
import { Database } from './database.ts' | ||
import { Eval, isEvalExpr } from './eval.ts' | ||
@@ -7,2 +6,3 @@ import { Flatten, Keys, unravel } from './utils.ts' | ||
import { Driver } from './driver.ts' | ||
import { Selection } from './selection.ts' | ||
@@ -13,3 +13,4 @@ export const Primary = Symbol('Primary') | ||
export interface Field<T = any> { | ||
type: Type<T> | ||
// FIXME Type<T> | ||
type: Field.Type<T> | Type<T> | ||
deftype?: Field.Type<T> | ||
@@ -80,4 +81,7 @@ length?: number | ||
// 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]?: 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]> | ||
} | ||
@@ -140,3 +144,3 @@ | ||
export namespace Model { | ||
export type Migration = (database: Database) => Promise<void> | ||
export type Migration<D = any> = (database: D) => Promise<void> | ||
@@ -143,0 +147,0 @@ export interface Config<O = {}> { |
@@ -163,3 +163,3 @@ import { defineProperty, Dict, filterKeys, valueMap } from 'cosmokit' | ||
constructor(driver: Driver, table: string | Selection | Dict<Selection.Immutable>, query?: Query) { | ||
constructor(driver: Driver<any>, table: string | Selection | Dict<Selection.Immutable>, query?: Query) { | ||
super(driver, { | ||
@@ -166,0 +166,0 @@ type: 'get', |
import { Binary, defineProperty, isNullable, mapValues } from 'cosmokit' | ||
import { Field } from './model.ts' | ||
import { Eval, isEvalExpr } from './eval.ts' | ||
import { Keys } from './utils.ts' | ||
// import { Keys } from './utils.ts' | ||
export interface Type<T = any, N = any> { | ||
[Type.kType]?: true | ||
type: Field.Type<T> | Keys<N, T> | Field.NewType<T> | ||
// FIXME | ||
type: Field.Type<T> // | Keys<N, T> | Field.NewType<T> | ||
inner?: T extends (infer I)[] ? Type<I, N> : Field.Type<T> extends 'json' ? { [key in keyof T]: Type<T[key], N> } : never | ||
@@ -52,5 +53,6 @@ array?: boolean | ||
export function fromField<T, N>(field: Type | Field<T> | Field.Type<T> | Keys<N, T> | Field.NewType<T>): Type<T, N> { | ||
// FIXME: Type | Field<T> | Field.Type<T> | Keys<N, T> | Field.NewType<T> | ||
export function fromField<T, N>(field: any): Type<T, N> { | ||
if (isType(field)) return field | ||
if (typeof field === 'string') return defineProperty({ type: field }, kType, true) | ||
if (typeof field === 'string') return defineProperty({ type: field }, kType, true) as never | ||
else if (field.type) return field.type | ||
@@ -57,0 +59,0 @@ else if (field.expr?.[kType]) return field.expr[kType] |
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
253915
4483