kern-orm 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +65 -2
- package/dist/expression-Bc-s_0qJ.d.mts +2091 -0
- package/dist/index-Chr-UK_t.d.mts +134 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +3359 -0
- package/dist/testing.d.mts +2 -0
- package/dist/testing.mjs +34 -0
- package/dist/text-4Xs09pIf.mjs +413 -0
- package/dist/types.d.mts +213 -0
- package/dist/types.mjs +254 -0
- package/package.json +50 -4
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { Cn as Operand, Dn as AnyTable, K as SchemaContext, L as NoRelations, Sn as ComparisonSignature, St as FilteredStateRows, Tt as TableOrQuery, Wn as ResultRows, X as UsableRelation, Z as UsedRelation, a as UntypedExpression, er as Type, gn as EqualityOperator, gr as GenericOperator, hn as SelectQueryState, hr as OperatorDefinitions, mn as QueryStateOf, mt as MergeSelection, on as OneRow, pt as HelperFields, r as Expression, t as AnyExpression, vr as OperatorSignature, w as SelectQueryKind, wn as OperandValue, wt as SelectQuery, xn as BooleanResult, xt as ChangeState, yn as InSignature } from "./expression-Bc-s_0qJ.mjs";
|
|
2
|
+
/** Operators of ordered types: those of `equalityOperators`, and `lt`, `lte`, `gt`, and `gte` comparing with operands of the expression type. */
|
|
3
|
+
declare const orderingOperators: {
|
|
4
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
5
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
6
|
+
in: EqualityOperator<InSignature>;
|
|
7
|
+
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
8
|
+
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
9
|
+
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
10
|
+
gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
11
|
+
};
|
|
12
|
+
/** Signature of pattern matching: patterns stay strings even when the text values are transformed. */
|
|
13
|
+
interface PatternSignature extends OperatorSignature {
|
|
14
|
+
readonly method: <V extends Operand<string>>(pattern: V) => Expression<BooleanResult<this["T"] | OperandValue<V>>>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Operators of text types: pattern matching with `LIKE` and `ILIKE`.
|
|
18
|
+
*
|
|
19
|
+
* A string pattern is sent as it is; `param(type, pattern)` encodes it with a type.
|
|
20
|
+
*/
|
|
21
|
+
declare const textOperators: {
|
|
22
|
+
like: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
|
|
23
|
+
ilike: GenericOperator<PatternSignature, AnyExpression<string | null, unknown>>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* State of the query that the callback of a helper of table `T` with schema context `C` receives: the fields of the query the helper is applied to, and all its rows.
|
|
27
|
+
*
|
|
28
|
+
* It is guaranteed, so that the state that the callback returns tells whether the callback ends the guarantee of the query the helper is applied to.
|
|
29
|
+
*/
|
|
30
|
+
interface HelperState<T extends AnyTable, C extends SchemaContext> extends QueryStateOf<T, C> {
|
|
31
|
+
readonly result: HelperFields<never>;
|
|
32
|
+
readonly input: never;
|
|
33
|
+
readonly rows: "many";
|
|
34
|
+
readonly value: false;
|
|
35
|
+
readonly uses: never;
|
|
36
|
+
readonly guaranteed: true;
|
|
37
|
+
readonly aggregate: false;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Effect of a helper: the fields it selects, its rows mode on a query of many rows, `one` after `take` or `required` and `nullable` or `optional` after the method of the same name, the relations it selects records of, and whether it keeps the guarantee of the row of a query of a required relation.
|
|
41
|
+
*
|
|
42
|
+
* `reads` are the relations of the schema of the helper among them, which its callback may read from the references of the query it is applied to, and `key` is the key of its table there.
|
|
43
|
+
*/
|
|
44
|
+
interface HelperEffect {
|
|
45
|
+
readonly fields: object;
|
|
46
|
+
readonly rows: ResultRows;
|
|
47
|
+
readonly uses: UsedRelation;
|
|
48
|
+
readonly reads: UsedRelation;
|
|
49
|
+
readonly key: string;
|
|
50
|
+
readonly guaranteed: boolean;
|
|
51
|
+
}
|
|
52
|
+
/** Effect of a helper whose callback returns a query of state `S`, for each state of a union. */
|
|
53
|
+
type ReadEffect<S extends SelectQueryState> = S extends SelectQueryState ? {
|
|
54
|
+
readonly fields: { [K in keyof S["result"] as K extends keyof HelperFields ? never : K]: S["result"][K]; };
|
|
55
|
+
readonly rows: S["rows"];
|
|
56
|
+
readonly uses: S["uses"];
|
|
57
|
+
readonly reads: Extract<UsableRelation<S["schema"]["relations"]>, S["uses"]>;
|
|
58
|
+
readonly key: S["schema"]["key"];
|
|
59
|
+
readonly guaranteed: S["guaranteed"];
|
|
60
|
+
} : never;
|
|
61
|
+
/** Error of a helper that a query does not accept, or whose callback returns a query it does not accept. */
|
|
62
|
+
interface HelperError<Message extends string> {
|
|
63
|
+
readonly error: Message;
|
|
64
|
+
}
|
|
65
|
+
/** `unknown` when every result state retains the input marker and returns records, or the error. */
|
|
66
|
+
type CheckResult<S extends SelectQueryState> = [S] extends [{
|
|
67
|
+
readonly result: HelperFields;
|
|
68
|
+
readonly value: false;
|
|
69
|
+
}] ? unknown : HelperError<"A helper can filter, select fields, and take one row, but cannot clear the selection or use get, pluck, or aggregates">;
|
|
70
|
+
/** State of a query over table `T` that accepts a helper of effect `E`. */
|
|
71
|
+
type HelperInput<T extends AnyTable, E extends HelperEffect> = QueryStateOf<T, ReadsContext<E>> & ValueCheck<E>;
|
|
72
|
+
/** Constraint of `value` of a query that accepts a helper of effect `E`. */
|
|
73
|
+
type ValueCheck<E extends HelperEffect> = [E extends HelperEffect ? keyof E["fields"] : never] extends [never] ? [Extract<E["rows"], "nullable">] extends [never] ? unknown : RecordRows<"A helper cannot apply nullable to a query made by get, pluck, or an aggregate"> : RecordRows<"A helper cannot select fields on a query made by get, pluck, or an aggregate">;
|
|
74
|
+
/** Constraint of a query whose rows are records, which a query of values fails with error `Message`. */
|
|
75
|
+
interface RecordRows<Message extends string> {
|
|
76
|
+
readonly value: false | HelperError<Message>;
|
|
77
|
+
}
|
|
78
|
+
/** Schema context of a query that has the relations of its schema that a helper of effect `E` may read. */
|
|
79
|
+
type ReadsContext<E extends HelperEffect> = [E["reads"]] extends [never] ? SchemaContext : SchemaContext<{ readonly [K in E["reads"]["key"]]: { readonly [N in Extract<E["reads"], {
|
|
80
|
+
readonly key: K;
|
|
81
|
+
}>["name"]]: Extract<E["reads"], {
|
|
82
|
+
readonly key: K;
|
|
83
|
+
readonly name: N;
|
|
84
|
+
}>["relation"]; }; }, E["key"]>;
|
|
85
|
+
/** State of a query of state `S` after a helper of effect `E`. */
|
|
86
|
+
type ApplyEffect<S extends SelectQueryState, E extends HelperEffect> = ChangeState<S, {
|
|
87
|
+
result: [keyof E["fields"]] extends [never] ? S["result"] : MergeSelection<S["result"], E["fields"]>;
|
|
88
|
+
rows: E["rows"] extends "many" ? EffectRows<S, E> : E["rows"] extends "one" ? OneRow<EffectRows<S, E>> : E["rows"];
|
|
89
|
+
uses: S["uses"] | E["uses"];
|
|
90
|
+
guaranteed: E["guaranteed"] extends true ? S["guaranteed"] : false;
|
|
91
|
+
}>;
|
|
92
|
+
/** Rows mode of a query of state `S` after a helper of effect `E` that keeps the rows, before `take`: relaxed as after a filter when the helper ends the guarantee. */
|
|
93
|
+
type EffectRows<S extends SelectQueryState, E extends HelperEffect> = E["guaranteed"] extends true ? S["rows"] : FilteredStateRows<S>;
|
|
94
|
+
/**
|
|
95
|
+
* Helper of `defineHelper` for queries over table `T`: it applies effect `E` to the query it receives with arguments `A` and returns a query of the same kind.
|
|
96
|
+
*
|
|
97
|
+
* Selected fields replace earlier fields under the same keys; a query made by `get`, `pluck`, or an aggregate method does not accept a helper that selects fields.
|
|
98
|
+
*/
|
|
99
|
+
type Helper<T extends AnyTable, E extends HelperEffect, A extends unknown[]> = <S extends HelperInput<T, E>, Kind extends SelectQueryKind>(query: SelectQuery<S, Kind>, ...args: A) => E extends HelperEffect ? SelectQuery<ApplyEffect<S, E>, Kind> : never;
|
|
100
|
+
/**
|
|
101
|
+
* Defines a reusable helper of a table that filters, selects fields, and applies `take`, `optional`, `nullable`, and `required`, used with `use` or called with a query.
|
|
102
|
+
*
|
|
103
|
+
* `helper` receives the query it is applied to and any further arguments, and returns the query with its clauses.
|
|
104
|
+
* Defined with a query of a schema, such as `schema.form`, it can select the relations of the table in that schema, and applies to queries whose schema has them.
|
|
105
|
+
* The helper returns a query of the same kind as the one it is applied to, bound to the same `Db` if any.
|
|
106
|
+
*/
|
|
107
|
+
declare function defineHelper<T extends AnyTable, R extends SelectQuery<any, SelectQueryKind>, A extends unknown[], C extends SchemaContext = NoRelations>(table: TableOrQuery<T, C>, helper: (query: SelectQuery<HelperState<T, C>, SelectQueryKind>, ...args: A) => R & CheckResult<R["types"]>): Helper<T, ReadEffect<R["types"]>, A>;
|
|
108
|
+
/** Tagged template building a SQL fragment of values `T` that accepts compared values `I`. */
|
|
109
|
+
type SqlTemplate<T, O extends OperatorDefinitions, I = T> = (strings: TemplateStringsArray, ...values: unknown[]) => Expression<T, O, I>;
|
|
110
|
+
/**
|
|
111
|
+
* Builds a SQL fragment.
|
|
112
|
+
*
|
|
113
|
+
* Interpolated expressions such as column references stay structured, a query of a table without clauses, such as `db.form`, becomes the name of the table, and other values become query parameters.
|
|
114
|
+
* The type argument declares the TypeScript type of the fragment and does not convert values at runtime, so the fragment cannot be selected; the fragment has `isNull` and the operators of `equalityOperators` only.
|
|
115
|
+
*/
|
|
116
|
+
declare function sql<T = unknown>(strings: TemplateStringsArray, ...values: unknown[]): UntypedExpression<T>;
|
|
117
|
+
/**
|
|
118
|
+
* Creates a template building SQL fragments of `type`.
|
|
119
|
+
*
|
|
120
|
+
* The fragments have the operators of the type and encode compared values with it; the type adds no SQL cast.
|
|
121
|
+
*/
|
|
122
|
+
declare function sql<Output, Input, O extends OperatorDefinitions>(type: Type<Output, Input, O, any>): SqlTemplate<Output, O, Input>;
|
|
123
|
+
/**
|
|
124
|
+
* Creates an expression of a query parameter encoded with a type, for a value interpolated into `sql` or compared on its own.
|
|
125
|
+
*
|
|
126
|
+
* With a type, the parameter is cast to it, such as `$1::integer`, so that PostgreSQL applies the operators of the type even where nothing else in the statement determines it.
|
|
127
|
+
* The cast omits type parameters, such as `$1::numeric` for `numeric(10,2)`, so that their precision, scale, or length do not constrain the value.
|
|
128
|
+
*
|
|
129
|
+
* With an expression, such as `param(self, other)` in an operator definition, the type of the expression encodes the value, and the parameter is not cast: PostgreSQL types it by the expression it is used with.
|
|
130
|
+
* `value` may then also be an expression, such as an operand of a generic operator, which is returned as it is, with its own type and nullability.
|
|
131
|
+
*/
|
|
132
|
+
declare function param<Output, Input, O extends OperatorDefinitions>(type: Type<Output, Input, O, any>, value: Input): Expression<Output, O, Input>;
|
|
133
|
+
declare function param<E extends AnyExpression, V extends (E extends AnyExpression<infer T, infer I> ? Operand<T, I> : never)>(type: E, value: V): V extends AnyExpression ? V : E;
|
|
134
|
+
export { HelperEffect as a, textOperators as c, Helper as i, orderingOperators as l, param as n, defineHelper as o, sql as r, PatternSignature as s, SqlTemplate as t };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { $ as one, $n as TextValue, $t as and, A as SqlQueryState, An as ColumnOperators, At as UpdateValues, B as RelationDeclaration, Bn as NotFoundError, Bt as orderBy, C as AnyQuery, Cn as Operand, Ct as ResultOf, D as FieldTypes, Dn as AnyTable, Dt as ColumnValue, E as DecodedFields, En as CompileOptions, Et as from, F as DeclaredRelations, Fn as Table, Ft as OrderTerms, G as RequiredMember, Gn as Connection, Gt as WhereCondition, H as RelationKey, Hn as ResultDecodeError, Ht as NotAKey, I as MergeRelations, In as TableKeys, It as Ordering, J as TableContext, Jn as SqlStatement, Jt as defineFilter, K as SchemaContext, Kn as Driver, Kt as WhereFilter, L as NoRelations, Ln as TableOptions, Lt as SelectedRefs, M as InitialMutationState, Mn as ColumnValues, Mt as OrderDirection, N as MutationQuery, Nn as KeyColumns, Nt as OrderKey, O as InitialSqlQueryState, On as AnyTableKeys, Ot as InsertValues, P as MutationQueryState, Pn as Selectable, Pt as OrderTerm, Q as many, Qn as BaseType, Qt as Predicate, R as OneRelationKeys, Rn as ToColumns, Rt as asc, Sn as ComparisonSignature, St as FilteredStateRows, T as AnySqlQuery, Tn as compare, U as RelationKeys, Un as ResultMode, Ut as PrimaryKeyValues, V as RelationDeclarations, Vn as RequiredRelationError, Vt as KeyCheck, W as RelationKind, Wn as ResultRows, Wt as EqValue, X as UsableRelation, Xn as createStatement, Xt as Condition, Y as TableRelations, Yn as TransactionFailure, Yt as CombinedCondition, Z as UsedRelation, Zn as AnyType, Zt as ConditionOperand, _n as EqualityOperators, _r as GenericOperatorImplementation, _t as SelectionResult, a as UntypedExpression, an as FilteredRows, ar as TypeTransform, at as Tables, bn as equalityOperators, bt as BoundSelectQuery, c as Db, cn as QueryResult, cr as Column, ct as RecordQuery, d as DbQueryState, dn as EmptyRecord, dr as ToColumn, dt as ExpressionInput, en as not, er as Type, et as AnySchema, f as Database, fn as InitialQueryState, fr as BoundOperators, ft as ExpressionValue, gn as EqualityOperator, gr as GenericOperator, gt as Selection, h as TransactionCoordinator, hn as SelectQueryState, hr as OperatorDefinitions, ht as NestedQuery, i as TypedExpression, in as RelationRefs, ir as TypeOutput, it as SchemaTables, j as AnyMutationQuery, jn as ColumnShape, jt as OrderColumns, k as SqlQuery, kn as ColumnDefinitions, kt as UpdateSet, l as DbMutationState, ln as ResultByRows, lr as ColumnAttributes, lt as RecordQueryState, m as createDatabase, mn as QueryStateOf, mr as NoOperators, mt as MergeSelection, n as BaseOperators, nn as ColumnRefs, nr as TypeOperators, nt as SchemaPart, o as AnyDb, on as OneRow, or as defineType, ot as defineSchema, p as DatabaseOptions, pn as QueryState, pr as MergeOperators, q as SchemaRelations, qn as DriverResult, qt as WhereValues, r as Expression, rn as QueryContext, rr as TypeOptions, rt as SchemaPartRelations, s as BindOptions, sn as OneValueRows, sr as AnyColumn, st as AnyRecordQuery, t as AnyExpression, tn as or, tr as TypeInput, tt as Schema, u as DbOf, un as ValueRows, ur as KeyKind, ut as SelectionState, vn as HasEquality, vr as OperatorSignature, vt as SelectionUses, w as SelectQueryKind, wn as OperandValue, wt as SelectQuery, xn as BooleanResult, yn as InSignature, yr as defineGenericOperator, yt as AnySelectQuery, z as Relation, zn as defineTable, zt as desc } from "./expression-Bc-s_0qJ.mjs";
|
|
2
|
+
import { a as HelperEffect, c as textOperators, i as Helper, l as orderingOperators, n as param, o as defineHelper, r as sql, s as PatternSignature, t as SqlTemplate } from "./index-Chr-UK_t.mjs";
|
|
3
|
+
export { type AnyColumn, type AnyDb, type AnyExpression, type AnyMutationQuery, type AnyQuery, type AnyRecordQuery, type AnySchema, type AnySelectQuery, type AnySqlQuery, type AnyTable, type AnyTableKeys, type AnyType, type BaseOperators, type BaseType, type BindOptions, type BooleanResult, type BoundOperators, type BoundSelectQuery, type Column, type ColumnAttributes, type ColumnDefinitions, type ColumnOperators, type ColumnRefs, type ColumnShape, type ColumnValue, type ColumnValues, type CombinedCondition, type ComparisonSignature, type CompileOptions, type Condition, type ConditionOperand, type Connection, type Database, type DatabaseOptions, type Db, type DbMutationState, type DbOf, type DbQueryState, type DeclaredRelations, type DecodedFields, type Driver, type DriverResult, type EmptyRecord, type EqValue, type EqualityOperator, type EqualityOperators, type Expression, type ExpressionInput, type ExpressionValue, type FieldTypes, type FilteredRows, type FilteredStateRows, type GenericOperator, type GenericOperatorImplementation, type HasEquality, type Helper, type HelperEffect, type InSignature, type InitialMutationState, type InitialQueryState, type InitialSqlQueryState, type InsertValues, type KeyCheck, type KeyColumns, type KeyKind, type MergeOperators, type MergeRelations, type MergeSelection, type MutationQuery, type MutationQueryState, type NestedQuery, type NoOperators, type NoRelations, type NotAKey, NotFoundError, type OneRelationKeys, type OneRow, type OneValueRows, type Operand, type OperandValue, type OperatorDefinitions, type OperatorSignature, type OrderColumns, type OrderDirection, type OrderKey, type OrderTerm, type OrderTerms, type Ordering, type PatternSignature, type Predicate, type PrimaryKeyValues, type QueryContext, type QueryResult, type QueryState, type QueryStateOf, type RecordQuery, type RecordQueryState, type Relation, type RelationDeclaration, type RelationDeclarations, type RelationKey, type RelationKeys, type RelationKind, type RelationRefs, type RequiredMember, RequiredRelationError, type ResultByRows, ResultDecodeError, type ResultMode, type ResultOf, type ResultRows, type Schema, type SchemaContext, type SchemaPart, type SchemaPartRelations, type SchemaRelations, type SchemaTables, type SelectQuery, type SelectQueryKind, type SelectQueryState, type Selectable, type SelectedRefs, type Selection, type SelectionResult, type SelectionState, type SelectionUses, type SqlQuery, type SqlQueryState, type SqlStatement, type SqlTemplate, type Table, type TableContext, type TableKeys, type TableOptions, type TableRelations, type Tables, type TextValue, type ToColumn, type ToColumns, type TransactionCoordinator, type TransactionFailure, type Type, type TypeInput, type TypeOperators, type TypeOptions, type TypeOutput, type TypeTransform, type TypedExpression, type UntypedExpression, type UpdateSet, type UpdateValues, type UsableRelation, type UsedRelation, type ValueRows, type WhereCondition, type WhereFilter, type WhereValues, and, asc, compare, createDatabase, createStatement, defineFilter, defineGenericOperator, defineHelper, defineSchema, defineTable, defineType, desc, equalityOperators, from, many, not, one, or, orderBy, orderingOperators, param, sql, textOperators };
|