🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@prisma-next/sql-relational-core

Package Overview
Dependencies
Maintainers
4
Versions
952
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@prisma-next/sql-relational-core - npm Package Compare versions

Comparing version
0.14.0-dev.70
to
0.14.0-dev.71
+1309
dist/types-DtzFztRV.mjs
import { ifDefined } from "@prisma-next/utils/defined";
//#region src/ast/types.ts
function frozenArrayCopy(values) {
return Object.freeze([...values]);
}
function frozenOptionalRecordCopy(value) {
return value === void 0 ? void 0 : Object.freeze({ ...value });
}
function frozenRecordCopy(record) {
return Object.freeze({ ...record });
}
function frozenCodecRef(codec) {
const typeParams = codec.typeParams === void 0 ? void 0 : structuredClone(codec.typeParams);
const base = {
codecId: codec.codecId,
...ifDefined("typeParams", typeParams)
};
return Object.freeze(codec.many ? {
...base,
many: true
} : base);
}
function freezeRows(rows) {
return Object.freeze(rows.map((row) => Object.freeze({ ...row })));
}
function combineAll(folder, thunks) {
let result = folder.empty;
for (const thunk of thunks) {
if (folder.isAbsorbing?.(result)) return result;
result = folder.combine(result, thunk());
}
return result;
}
function rewriteComparable(value, rewriter) {
switch (value.kind) {
case "param-ref": return rewriter.paramRef ? rewriter.paramRef(value) : value;
case "prepared-param-ref": return rewriter.preparedParamRef ? rewriter.preparedParamRef(value) : value;
case "literal": return rewriter.literal ? rewriter.literal(value) : value;
case "list":
if (rewriter.list) return rewriter.list(value);
return value.rewrite(rewriter);
default: return value.rewrite(rewriter);
}
}
function foldComparable(value, folder) {
switch (value.kind) {
case "param-ref": return folder.paramRef ? folder.paramRef(value) : folder.empty;
case "prepared-param-ref": return folder.preparedParamRef ? folder.preparedParamRef(value) : folder.empty;
case "literal": return folder.literal ? folder.literal(value) : folder.empty;
case "list": return value.fold(folder);
default: return value.fold(folder);
}
}
function collectColumnRefsWith(node) {
return node.fold({
empty: [],
combine: (a, b) => [...a, ...b],
columnRef: (columnRef) => [columnRef],
select: (ast) => ast.collectColumnRefs()
});
}
function collectParamRefsWith(node) {
return node.fold({
empty: [],
combine: (a, b) => [...a, ...b],
paramRef: (paramRef) => [paramRef],
preparedParamRef: (paramRef) => [paramRef],
select: (ast) => ast.collectParamRefs()
});
}
function rewriteTableSource(table, rewriter) {
return rewriter.tableSource ? rewriter.tableSource(table) : table;
}
function rewriteProjectionItem(item, rewriter) {
const rewrittenExpr = item.expr.kind === "literal" ? rewriter.literal ? rewriter.literal(item.expr) : item.expr : item.expr.rewrite(rewriter);
return new ProjectionItem(item.alias, rewrittenExpr, item.codec);
}
function rewriteInsertValue(value, rewriter) {
switch (value.kind) {
case "param-ref": return rewriter.paramRef ? rewriteParamRefForInsert(value, rewriter) : value;
case "prepared-param-ref": return rewriter.preparedParamRef ? rewriter.preparedParamRef(value) : value;
case "column-ref": return rewriter.columnRef ? rewriteColumnRefForInsert(value, rewriter) : value;
case "default-value": return value;
case "raw-expr": return value;
}
}
function rewriteParamRefForInsert(value, rewriter) {
const rewritten = rewriter.paramRef ? rewriter.paramRef(value) : value;
return rewritten.kind === "param-ref" ? rewritten : value;
}
function rewriteColumnRefForInsert(value, rewriter) {
const rewritten = rewriter.columnRef ? rewriter.columnRef(value) : value;
return rewritten.kind === "column-ref" ? rewritten : value;
}
function rewriteInsertRow(row, rewriter) {
const result = {};
for (const [key, value] of Object.entries(row)) result[key] = rewriteInsertValue(value, rewriter);
return result;
}
function rewriteUpdateSet(set, rewriter) {
const result = {};
for (const [key, value] of Object.entries(set)) result[key] = value.rewrite(rewriter);
return result;
}
function rewriteLimitOffset(value, rewriter) {
if (value === void 0 || typeof value === "number") return value;
return value.rewrite(rewriter);
}
function rewriteOnConflict(onConflict, rewriter) {
const columns = onConflict.columns.map((columnRef) => {
const rewritten = rewriter.columnRef ? rewriter.columnRef(columnRef) : columnRef;
return rewritten.kind === "column-ref" ? rewritten : columnRef;
});
if (onConflict.action.kind === "do-nothing") return new InsertOnConflict(columns, new DoNothingConflictAction());
return new InsertOnConflict(columns, new DoUpdateSetConflictAction(rewriteUpdateSet(onConflict.action.set, rewriter)));
}
var AstNode = class {
freeze() {
Object.freeze(this);
}
};
var QueryAst = class extends AstNode {};
var FromSource = class extends AstNode {};
var Expression = class extends AstNode {
collectColumnRefs() {
return collectColumnRefsWith(this);
}
collectParamRefs() {
return collectParamRefsWith(this);
}
baseColumnRef() {
throw new Error(`${this.constructor.name} does not expose a base column reference`);
}
toExpr() {
return this;
}
not() {
return new NotExpr(this);
}
};
var TableSource = class TableSource extends FromSource {
kind = "table-source";
name;
alias;
/**
* Resolved storage namespace coordinate for this table, stamped when the
* table proxy constructs the AST. Renderers qualify via the namespace
* concretion's `qualifyTable()` using this id — never by re-resolving the
* bare table name at render time.
*/
namespaceId;
constructor(name, alias, namespaceId) {
super();
this.name = name;
this.alias = alias;
this.namespaceId = namespaceId;
}
static named(name, alias, namespaceId) {
const source = new TableSource(name, alias, namespaceId);
source.freeze();
return source;
}
rewrite(rewriter) {
return rewriter.tableSource ? rewriter.tableSource(this) : this;
}
toFromSource() {
return this;
}
};
var DerivedTableSource = class DerivedTableSource extends FromSource {
kind = "derived-table-source";
alias;
query;
constructor(alias, query) {
super();
this.alias = alias;
this.query = query;
this.freeze();
}
static as(alias, query) {
return new DerivedTableSource(alias, query);
}
rewrite(rewriter) {
return new DerivedTableSource(this.alias, this.query.rewrite(rewriter));
}
toFromSource() {
return this;
}
};
var FunctionSource = class FunctionSource extends FromSource {
kind = "function-source";
fn;
args;
alias;
constructor(fn, args, alias) {
super();
this.fn = fn;
this.args = frozenArrayCopy(args);
this.alias = alias;
this.freeze();
}
static of(fn, args, alias) {
return new FunctionSource(fn, args, alias);
}
rewrite(rewriter) {
const rewrittenArgs = this.args.map((arg) => rewriteComparable(arg, rewriter));
if (rewrittenArgs.every((arg, i) => arg === this.args[i])) return this;
return new FunctionSource(this.fn, rewrittenArgs, this.alias);
}
toFromSource() {
return this;
}
};
var ColumnRef = class ColumnRef extends Expression {
kind = "column-ref";
table;
column;
constructor(table, column) {
super();
this.table = table;
this.column = column;
this.freeze();
}
static of(table, column) {
return new ColumnRef(table, column);
}
accept(visitor) {
return visitor.columnRef(this);
}
rewrite(rewriter) {
return rewriter.columnRef ? rewriter.columnRef(this) : this;
}
fold(folder) {
return folder.columnRef ? folder.columnRef(this) : folder.empty;
}
baseColumnRef() {
return this;
}
};
var IdentifierRef = class IdentifierRef extends Expression {
kind = "identifier-ref";
name;
constructor(name) {
super();
this.name = name;
this.freeze();
}
static of(name) {
return new IdentifierRef(name);
}
accept(visitor) {
return visitor.identifierRef(this);
}
rewrite(rewriter) {
return rewriter.identifierRef ? rewriter.identifierRef(this) : this;
}
fold(folder) {
return folder.identifierRef ? folder.identifierRef(this) : folder.empty;
}
};
var ParamRef = class ParamRef extends Expression {
kind = "param-ref";
value;
name;
/**
* Codec identity carried by every column-bound `ParamRef`. The encode-side dispatch path materialises the per-instance codec through `contractCodecs.forCodecRef(codec)` — content-keyed memoisation on `(codecId, canonicalize(typeParams))` keeps repeated lookups for the same logical column on one shared {@link Codec}.
*
* `codec` may be `undefined` for `ParamRef`s constructed without a column-bound site (literals, transient builder state); the runtime treats those as untyped passthroughs.
*/
codec;
constructor(value, options) {
super();
this.value = value;
this.name = options?.name;
this.codec = options?.codec ? frozenCodecRef(options.codec) : void 0;
this.freeze();
}
static of(value, options) {
return new ParamRef(value, options);
}
accept(visitor) {
return visitor.param(this);
}
rewrite(rewriter) {
return rewriter.paramRef ? rewriter.paramRef(this) : this;
}
fold(folder) {
return folder.paramRef ? folder.paramRef(this) : folder.empty;
}
};
/**
* Bind-site placeholder: occupies the same positions as `ParamRef` in the
* AST, but carries no value — the value is supplied per-execute by the
* `PreparedStatement.execute(params)` caller and matched to this node by
* `name`.
*/
var PreparedParamRef = class PreparedParamRef extends Expression {
kind = "prepared-param-ref";
name;
codec;
constructor(name, codec) {
super();
this.name = name;
this.codec = frozenCodecRef(codec);
this.freeze();
}
static of(name, codec) {
return new PreparedParamRef(name, codec);
}
accept(visitor) {
return visitor.preparedParam(this);
}
rewrite(rewriter) {
return rewriter.preparedParamRef ? rewriter.preparedParamRef(this) : this;
}
fold(folder) {
return folder.preparedParamRef ? folder.preparedParamRef(this) : folder.empty;
}
};
var DefaultValueExpr = class extends AstNode {
kind = "default-value";
constructor() {
super();
this.freeze();
}
};
var LiteralExpr = class LiteralExpr extends Expression {
kind = "literal";
value;
constructor(value) {
super();
this.value = value;
this.freeze();
}
static of(value) {
return new LiteralExpr(value);
}
accept(visitor) {
return visitor.literal(this);
}
rewrite(rewriter) {
return rewriter.literal ? rewriter.literal(this) : this;
}
fold(folder) {
return folder.literal ? folder.literal(this) : folder.empty;
}
};
var SubqueryExpr = class SubqueryExpr extends Expression {
kind = "subquery";
query;
constructor(query) {
super();
this.query = query;
this.freeze();
}
static of(query) {
return new SubqueryExpr(query);
}
accept(visitor) {
return visitor.subquery(this);
}
rewrite(rewriter) {
const query = this.query.rewrite(rewriter);
return new SubqueryExpr(query);
}
fold(folder) {
return folder.select ? folder.select(this.query) : folder.empty;
}
};
var OperationExpr = class OperationExpr extends Expression {
kind = "operation";
method;
self;
args;
returns;
lowering;
constructor(options) {
super();
this.method = options.method;
this.self = options.self;
this.args = frozenArrayCopy(options.args ?? []);
this.returns = options.returns;
this.lowering = options.lowering;
this.freeze();
}
accept(visitor) {
return visitor.operation(this);
}
rewrite(rewriter) {
return new OperationExpr({
method: this.method,
self: this.self.rewrite(rewriter),
args: this.args.map((arg) => rewriteComparable(arg, rewriter)),
returns: this.returns,
lowering: this.lowering
});
}
fold(folder) {
return combineAll(folder, [() => this.self.fold(folder), ...this.args.map((arg) => () => foldComparable(arg, folder))]);
}
baseColumnRef() {
return this.self.baseColumnRef();
}
};
var RawExpr = class extends Expression {
kind = "raw-expr";
parts;
returns;
constructor(options) {
super();
this.parts = frozenArrayCopy(options.parts);
this.returns = options.returns;
this.freeze();
}
accept(visitor) {
return visitor.rawExpr(this);
}
rewrite(rewriter) {
return rewriter.rawExpr ? rewriter.rawExpr(this) : this;
}
fold(folder) {
if (folder.rawExpr) return folder.rawExpr(this);
return combineAll(folder, this.parts.filter((p) => typeof p !== "string").map((p) => () => p.fold(folder)));
}
};
var AggregateExpr = class AggregateExpr extends Expression {
kind = "aggregate";
fn;
expr;
constructor(fn, expr) {
super();
if (fn !== "count" && expr === void 0) throw new Error(`Aggregate function "${fn}" requires an expression`);
this.fn = fn;
this.expr = expr;
this.freeze();
}
static count(expr) {
return new AggregateExpr("count", expr);
}
static sum(expr) {
return new AggregateExpr("sum", expr);
}
static avg(expr) {
return new AggregateExpr("avg", expr);
}
static min(expr) {
return new AggregateExpr("min", expr);
}
static max(expr) {
return new AggregateExpr("max", expr);
}
accept(visitor) {
return visitor.aggregate(this);
}
rewrite(rewriter) {
return this.expr === void 0 ? this : new AggregateExpr(this.fn, this.expr.rewrite(rewriter));
}
fold(folder) {
return this.expr ? this.expr.fold(folder) : folder.empty;
}
};
/**
* Window function call: `fn(args) OVER (PARTITION BY ... ORDER BY ...)`.
*
* Both `partitionBy` and `orderBy` are optional; an empty `OVER ()`
* clause is legal SQL but rarely useful. For `ROW_NUMBER`, `RANK`, and
* `DENSE_RANK` the standard mandates an `ORDER BY` for deterministic
* results — callers are expected to provide one, but the AST does not
* enforce it.
*
* The `args` slot exists for future window function additions that take
* arguments (e.g. `COUNT(*) OVER`, `SUM(x) OVER`); `ROW_NUMBER` and the
* other ranking functions take no arguments.
*/
var WindowFuncExpr = class WindowFuncExpr extends Expression {
kind = "window-func";
fn;
args;
partitionBy;
orderBy;
constructor(options) {
super();
this.fn = options.fn;
this.args = options.args && options.args.length > 0 ? frozenArrayCopy(options.args) : [];
this.partitionBy = options.partitionBy && options.partitionBy.length > 0 ? frozenArrayCopy(options.partitionBy) : void 0;
this.orderBy = options.orderBy && options.orderBy.length > 0 ? frozenArrayCopy(options.orderBy) : void 0;
this.freeze();
}
static rowNumber(options) {
return new WindowFuncExpr({
fn: "row_number",
...options
});
}
accept(visitor) {
return visitor.windowFunc(this);
}
rewrite(rewriter) {
return new WindowFuncExpr({
fn: this.fn,
args: this.args.map((arg) => arg.rewrite(rewriter)),
...ifDefined("partitionBy", this.partitionBy?.map((expr) => expr.rewrite(rewriter))),
...ifDefined("orderBy", this.orderBy?.map((orderItem) => orderItem.rewrite(rewriter)))
});
}
fold(folder) {
return combineAll(folder, [
...this.args.map((arg) => () => arg.fold(folder)),
...(this.partitionBy ?? []).map((expr) => () => expr.fold(folder)),
...(this.orderBy ?? []).map((orderItem) => () => orderItem.expr.fold(folder))
]);
}
};
var JsonObjectExpr = class JsonObjectExpr extends Expression {
kind = "json-object";
entries;
constructor(entries) {
super();
this.entries = frozenArrayCopy(entries.map((entry) => Object.freeze({ ...entry })));
this.freeze();
}
static entry(key, value) {
return {
key,
value
};
}
static fromEntries(entries) {
return new JsonObjectExpr(entries);
}
accept(visitor) {
return visitor.jsonObject(this);
}
rewrite(rewriter) {
return new JsonObjectExpr(this.entries.map((entry) => ({
key: entry.key,
value: entry.value.kind === "literal" ? rewriter.literal ? rewriter.literal(entry.value) : entry.value : entry.value.rewrite(rewriter)
})));
}
fold(folder) {
return combineAll(folder, this.entries.map((entry) => () => entry.value.kind === "literal" ? folder.literal ? folder.literal(entry.value) : folder.empty : entry.value.fold(folder)));
}
};
var OrderByItem = class OrderByItem extends AstNode {
kind = "order-by-item";
expr;
dir;
constructor(expr, dir) {
super();
this.expr = expr;
this.dir = dir;
this.freeze();
}
static asc(expr) {
return new OrderByItem(expr, "asc");
}
static desc(expr) {
return new OrderByItem(expr, "desc");
}
rewrite(rewriter) {
return new OrderByItem(this.expr.rewrite(rewriter), this.dir);
}
/**
* A new frozen item with the sort direction flipped and `expr` unchanged.
* Integrations that own pagination (e.g. backward cursor pagination) use
* this to reverse a user's sort order without reaching into the AST.
*/
reverse() {
return new OrderByItem(this.expr, this.dir === "asc" ? "desc" : "asc");
}
};
var JsonArrayAggExpr = class JsonArrayAggExpr extends Expression {
kind = "json-array-agg";
expr;
onEmpty;
orderBy;
constructor(expr, onEmpty = "null", orderBy) {
super();
this.expr = expr;
this.onEmpty = onEmpty;
this.orderBy = orderBy && orderBy.length > 0 ? frozenArrayCopy(orderBy) : void 0;
this.freeze();
}
static of(expr, onEmpty = "null", orderBy) {
return new JsonArrayAggExpr(expr, onEmpty, orderBy);
}
accept(visitor) {
return visitor.jsonArrayAgg(this);
}
rewrite(rewriter) {
return new JsonArrayAggExpr(this.expr.rewrite(rewriter), this.onEmpty, this.orderBy?.map((orderItem) => orderItem.rewrite(rewriter)));
}
fold(folder) {
return combineAll(folder, [() => this.expr.fold(folder), ...(this.orderBy ?? []).map((orderItem) => () => orderItem.expr.fold(folder))]);
}
};
var ListExpression = class ListExpression extends Expression {
kind = "list";
values;
constructor(values) {
super();
this.values = frozenArrayCopy(values);
this.freeze();
}
static of(values) {
return new ListExpression(values);
}
static fromValues(values) {
return new ListExpression(values.map((value) => new LiteralExpr(value)));
}
accept(visitor) {
return visitor.list(this);
}
rewrite(rewriter) {
if (rewriter.list) return rewriter.list(this);
return new ListExpression(this.values.map((value) => value.rewrite(rewriter)));
}
fold(folder) {
if (folder.list) return folder.list(this);
return combineAll(folder, this.values.map((value) => () => value.fold(folder)));
}
};
var BinaryExpr = class BinaryExpr extends Expression {
kind = "binary";
op;
left;
right;
constructor(op, left, right) {
super();
this.op = op;
this.left = left;
this.right = right;
this.freeze();
}
static eq(left, right) {
return new BinaryExpr("eq", left, right);
}
static neq(left, right) {
return new BinaryExpr("neq", left, right);
}
static gt(left, right) {
return new BinaryExpr("gt", left, right);
}
static lt(left, right) {
return new BinaryExpr("lt", left, right);
}
static gte(left, right) {
return new BinaryExpr("gte", left, right);
}
static lte(left, right) {
return new BinaryExpr("lte", left, right);
}
static like(left, right) {
return new BinaryExpr("like", left, right);
}
static in(left, right) {
return new BinaryExpr("in", left, right);
}
static notIn(left, right) {
return new BinaryExpr("notIn", left, right);
}
accept(visitor) {
return visitor.binary(this);
}
rewrite(rewriter) {
return new BinaryExpr(this.op, rewriteComparable(this.left, rewriter), rewriteComparable(this.right, rewriter));
}
fold(folder) {
return combineAll(folder, [() => foldComparable(this.left, folder), () => foldComparable(this.right, folder)]);
}
};
var AndExpr = class AndExpr extends Expression {
kind = "and";
exprs;
constructor(exprs) {
super();
this.exprs = frozenArrayCopy(exprs);
this.freeze();
}
static of(exprs) {
return new AndExpr(exprs);
}
static true() {
return new AndExpr([]);
}
accept(visitor) {
return visitor.and(this);
}
rewrite(rewriter) {
return new AndExpr(this.exprs.map((expr) => expr.rewrite(rewriter)));
}
fold(folder) {
return combineAll(folder, this.exprs.map((expr) => () => expr.fold(folder)));
}
};
var OrExpr = class OrExpr extends Expression {
kind = "or";
exprs;
constructor(exprs) {
super();
this.exprs = frozenArrayCopy(exprs);
this.freeze();
}
static of(exprs) {
return new OrExpr(exprs);
}
static false() {
return new OrExpr([]);
}
accept(visitor) {
return visitor.or(this);
}
rewrite(rewriter) {
return new OrExpr(this.exprs.map((expr) => expr.rewrite(rewriter)));
}
fold(folder) {
return combineAll(folder, this.exprs.map((expr) => () => expr.fold(folder)));
}
};
var ExistsExpr = class ExistsExpr extends Expression {
kind = "exists";
notExists;
subquery;
constructor(subquery, notExists = false) {
super();
this.notExists = notExists;
this.subquery = subquery;
this.freeze();
}
static exists(subquery) {
return new ExistsExpr(subquery, false);
}
static notExists(subquery) {
return new ExistsExpr(subquery, true);
}
accept(visitor) {
return visitor.exists(this);
}
rewrite(rewriter) {
return new ExistsExpr(this.subquery.rewrite(rewriter), this.notExists);
}
fold(folder) {
return folder.select ? folder.select(this.subquery) : folder.empty;
}
};
var NullCheckExpr = class NullCheckExpr extends Expression {
kind = "null-check";
expr;
isNull;
constructor(expr, isNull) {
super();
this.expr = expr;
this.isNull = isNull;
this.freeze();
}
static isNull(expr) {
return new NullCheckExpr(expr, true);
}
static isNotNull(expr) {
return new NullCheckExpr(expr, false);
}
accept(visitor) {
return visitor.nullCheck(this);
}
rewrite(rewriter) {
return new NullCheckExpr(this.expr.rewrite(rewriter), this.isNull);
}
fold(folder) {
return this.expr.fold(folder);
}
};
var NotExpr = class NotExpr extends Expression {
kind = "not";
expr;
constructor(expr) {
super();
this.expr = expr;
this.freeze();
}
toWhereExpr() {
return this;
}
accept(visitor) {
return visitor.not(this);
}
rewrite(rewriter) {
return new NotExpr(this.expr.rewrite(rewriter));
}
fold(folder) {
return this.expr.fold(folder);
}
};
var EqColJoinOn = class EqColJoinOn extends AstNode {
kind = "eq-col-join-on";
left;
right;
constructor(left, right) {
super();
this.left = left;
this.right = right;
this.freeze();
}
static of(left, right) {
return new EqColJoinOn(left, right);
}
rewrite(rewriter) {
return rewriter.eqColJoinOn ? rewriter.eqColJoinOn(this) : this;
}
};
var JoinAst = class JoinAst extends AstNode {
kind = "join";
joinType;
source;
lateral;
on;
constructor(joinType, source, on, lateral = false) {
super();
this.joinType = joinType;
this.source = source;
this.lateral = lateral;
this.on = on;
this.freeze();
}
static inner(source, on, lateral = false) {
return new JoinAst("inner", source, on, lateral);
}
static left(source, on, lateral = false) {
return new JoinAst("left", source, on, lateral);
}
static right(source, on, lateral = false) {
return new JoinAst("right", source, on, lateral);
}
static full(source, on, lateral = false) {
return new JoinAst("full", source, on, lateral);
}
rewrite(rewriter) {
return new JoinAst(this.joinType, this.source.rewrite(rewriter), this.on.kind === "eq-col-join-on" ? this.on.rewrite(rewriter) : this.on.rewrite(rewriter), this.lateral);
}
};
var ProjectionItem = class ProjectionItem extends AstNode {
kind = "projection-item";
alias;
expr;
/**
* Codec identity for the projected cell. Decode-side dispatch resolves the per-instance codec through `contractCodecs.forCodecRef(codec)` — content-keyed memoisation collapses repeated lookups for the same logical column onto one shared {@link Codec}.
*
* Stays `undefined` for non-column-bound projections (computed expressions, subqueries, raw aliases) whose decoded type the runtime cannot infer from a single contract column.
*/
codec;
constructor(alias, expr, codec) {
super();
this.alias = alias;
this.expr = expr;
this.codec = codec ? frozenCodecRef(codec) : void 0;
this.freeze();
}
static of(alias, expr, codec) {
return new ProjectionItem(alias, expr, codec);
}
withCodec(codec) {
return new ProjectionItem(this.alias, this.expr, codec);
}
};
var SelectAst = class SelectAst extends QueryAst {
kind = "select";
from;
joins;
projection;
where;
orderBy;
distinct;
distinctOn;
groupBy;
having;
limit;
offset;
selectAllIntent;
constructor(options) {
super();
this.from = options.from;
this.joins = options.joins && options.joins.length > 0 ? frozenArrayCopy(options.joins) : void 0;
this.projection = frozenArrayCopy(options.projection);
this.where = options.where;
this.orderBy = options.orderBy && options.orderBy.length > 0 ? frozenArrayCopy(options.orderBy) : void 0;
this.distinct = options.distinct;
this.distinctOn = options.distinctOn && options.distinctOn.length > 0 ? frozenArrayCopy(options.distinctOn) : void 0;
this.groupBy = options.groupBy && options.groupBy.length > 0 ? frozenArrayCopy(options.groupBy) : void 0;
this.having = options.having;
this.limit = options.limit;
this.offset = options.offset;
this.selectAllIntent = frozenOptionalRecordCopy(options.selectAllIntent);
this.freeze();
}
static from(from) {
return new SelectAst({
from,
joins: void 0,
projection: [],
where: void 0,
orderBy: void 0,
distinct: void 0,
distinctOn: void 0,
groupBy: void 0,
having: void 0,
limit: void 0,
offset: void 0,
selectAllIntent: void 0
});
}
static noFrom() {
return new SelectAst({
joins: void 0,
projection: [],
where: void 0,
orderBy: void 0,
distinct: void 0,
distinctOn: void 0,
groupBy: void 0,
having: void 0,
limit: void 0,
offset: void 0,
selectAllIntent: void 0
});
}
toOptions() {
return {
...this.from !== void 0 ? { from: this.from } : {},
joins: this.joins,
projection: this.projection,
where: this.where,
orderBy: this.orderBy,
distinct: this.distinct,
distinctOn: this.distinctOn,
groupBy: this.groupBy,
having: this.having,
limit: this.limit,
offset: this.offset,
selectAllIntent: this.selectAllIntent
};
}
withFrom(from) {
return new SelectAst({
...this.toOptions(),
from
});
}
withJoins(joins) {
return new SelectAst({
...this.toOptions(),
joins: joins.length > 0 ? joins : void 0
});
}
withProjection(projection) {
return new SelectAst({
...this.toOptions(),
projection
});
}
addProjection(alias, expr) {
return new SelectAst({
...this.toOptions(),
projection: [...this.projection, new ProjectionItem(alias, expr)]
});
}
withWhere(where) {
return new SelectAst({
...this.toOptions(),
where
});
}
withOrderBy(orderBy) {
return new SelectAst({
...this.toOptions(),
orderBy: orderBy.length > 0 ? orderBy : void 0
});
}
withDistinct(enabled = true) {
return new SelectAst({
...this.toOptions(),
distinct: enabled ? true : void 0
});
}
withDistinctOn(distinctOn) {
return new SelectAst({
...this.toOptions(),
distinctOn: distinctOn.length > 0 ? distinctOn : void 0
});
}
withGroupBy(groupBy) {
return new SelectAst({
...this.toOptions(),
groupBy: groupBy.length > 0 ? groupBy : void 0
});
}
withHaving(having) {
return new SelectAst({
...this.toOptions(),
having
});
}
withLimit(limit) {
return new SelectAst({
...this.toOptions(),
limit
});
}
withOffset(offset) {
return new SelectAst({
...this.toOptions(),
offset
});
}
withSelectAllIntent(selectAllIntent) {
return new SelectAst({
...this.toOptions(),
selectAllIntent
});
}
rewrite(rewriter) {
const rewrittenFrom = this.from?.rewrite(rewriter);
const rewritten = new SelectAst({
...rewrittenFrom !== void 0 ? { from: rewrittenFrom } : {},
joins: this.joins?.map((join) => join.rewrite(rewriter)),
projection: this.projection.map((projection) => new ProjectionItem(projection.alias, projection.expr.kind === "literal" ? rewriter.literal ? rewriter.literal(projection.expr) : projection.expr : projection.expr.rewrite(rewriter), projection.codec)),
where: this.where?.rewrite(rewriter),
orderBy: this.orderBy?.map((orderItem) => orderItem.rewrite(rewriter)),
distinct: this.distinct,
distinctOn: this.distinctOn?.map((expr) => expr.rewrite(rewriter)),
groupBy: this.groupBy?.map((expr) => expr.rewrite(rewriter)),
having: this.having?.rewrite(rewriter),
limit: rewriteLimitOffset(this.limit, rewriter),
offset: rewriteLimitOffset(this.offset, rewriter),
selectAllIntent: this.selectAllIntent
});
return rewriter.select ? rewriter.select(rewritten) : rewritten;
}
collectColumnRefs() {
const refs = [];
const pushRefs = (columns) => {
refs.push(...columns);
};
if (this.from?.kind === "derived-table-source") pushRefs(this.from.query.collectColumnRefs());
else if (this.from?.kind === "function-source") for (const arg of this.from.args) pushRefs(arg.collectColumnRefs());
for (const projection of this.projection) if (!(projection.expr.kind === "literal")) pushRefs(projection.expr.collectColumnRefs());
if (this.where) pushRefs(this.where.collectColumnRefs());
if (this.having) pushRefs(this.having.collectColumnRefs());
for (const orderItem of this.orderBy ?? []) pushRefs(orderItem.expr.collectColumnRefs());
for (const expr of this.distinctOn ?? []) pushRefs(expr.collectColumnRefs());
for (const expr of this.groupBy ?? []) pushRefs(expr.collectColumnRefs());
for (const join of this.joins ?? []) {
if (join.source.kind === "derived-table-source") pushRefs(join.source.query.collectColumnRefs());
else if (join.source.kind === "function-source") for (const arg of join.source.args) pushRefs(arg.collectColumnRefs());
if (join.on.kind === "eq-col-join-on") refs.push(join.on.left, join.on.right);
else pushRefs(join.on.collectColumnRefs());
}
if (typeof this.limit === "object") pushRefs(this.limit.collectColumnRefs());
if (typeof this.offset === "object") pushRefs(this.offset.collectColumnRefs());
return refs;
}
collectParamRefs() {
const refs = [];
const pushRefs = (params) => {
refs.push(...params);
};
if (this.from?.kind === "derived-table-source") pushRefs(this.from.query.collectParamRefs());
else if (this.from?.kind === "function-source") for (const arg of this.from.args) pushRefs(arg.collectParamRefs());
for (const projection of this.projection) if (!(projection.expr.kind === "literal")) pushRefs(projection.expr.collectParamRefs());
if (this.where) pushRefs(this.where.collectParamRefs());
if (this.having) pushRefs(this.having.collectParamRefs());
for (const orderItem of this.orderBy ?? []) pushRefs(orderItem.expr.collectParamRefs());
for (const expr of this.distinctOn ?? []) pushRefs(expr.collectParamRefs());
for (const expr of this.groupBy ?? []) pushRefs(expr.collectParamRefs());
for (const join of this.joins ?? []) {
if (join.source.kind === "derived-table-source") pushRefs(join.source.query.collectParamRefs());
else if (join.source.kind === "function-source") for (const arg of join.source.args) pushRefs(arg.collectParamRefs());
if (!(join.on.kind === "eq-col-join-on")) pushRefs(join.on.collectParamRefs());
}
if (typeof this.limit === "object") pushRefs(this.limit.collectParamRefs());
if (typeof this.offset === "object") pushRefs(this.offset.collectParamRefs());
return refs;
}
toQueryAst() {
return this;
}
};
var InsertOnConflictAction = class extends AstNode {};
var DoNothingConflictAction = class extends InsertOnConflictAction {
kind = "do-nothing";
constructor() {
super();
this.freeze();
}
toInsertOnConflictAction() {
return this;
}
};
var DoUpdateSetConflictAction = class extends InsertOnConflictAction {
kind = "do-update-set";
set;
constructor(set) {
super();
this.set = frozenRecordCopy(set);
this.freeze();
}
toInsertOnConflictAction() {
return this;
}
};
var InsertOnConflict = class InsertOnConflict extends AstNode {
kind = "insert-on-conflict";
columns;
action;
constructor(columns, action) {
super();
this.columns = frozenArrayCopy(columns);
this.action = action;
this.freeze();
}
static on(columns) {
return new InsertOnConflict(columns, new DoNothingConflictAction());
}
doNothing() {
return new InsertOnConflict(this.columns, new DoNothingConflictAction());
}
doUpdateSet(set) {
return new InsertOnConflict(this.columns, new DoUpdateSetConflictAction(set));
}
};
var InsertAst = class InsertAst extends QueryAst {
kind = "insert";
table;
rows;
onConflict;
returning;
constructor(table, rows = [{}], onConflict, returning) {
super();
this.table = table;
this.rows = freezeRows(rows);
this.onConflict = onConflict;
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
this.freeze();
}
static into(table) {
return new InsertAst(table);
}
withRows(rows) {
return new InsertAst(this.table, rows.map((row) => ({ ...row })), this.onConflict, this.returning);
}
withReturning(returning) {
return new InsertAst(this.table, this.rows.map((row) => ({ ...row })), this.onConflict, returning);
}
withOnConflict(onConflict) {
return new InsertAst(this.table, this.rows.map((row) => ({ ...row })), onConflict, this.returning);
}
rewrite(rewriter) {
return new InsertAst(rewriteTableSource(this.table, rewriter), this.rows.map((row) => rewriteInsertRow(row, rewriter)), this.onConflict ? rewriteOnConflict(this.onConflict, rewriter) : void 0, this.returning?.map((item) => rewriteProjectionItem(item, rewriter)));
}
collectParamRefs() {
const refs = [];
for (const row of this.rows) for (const value of Object.values(row)) if (value.kind === "param-ref" || value.kind === "prepared-param-ref") refs.push(value);
else if (value.kind === "raw-expr") refs.push(...value.collectParamRefs());
if (this.onConflict?.action.kind === "do-update-set") {
for (const value of Object.values(this.onConflict.action.set)) if (value.kind === "param-ref" || value.kind === "prepared-param-ref") refs.push(value);
}
for (const item of this.returning ?? []) if (item.expr.kind !== "literal") refs.push(...item.expr.collectParamRefs());
return refs;
}
toQueryAst() {
return this;
}
};
var UpdateAst = class UpdateAst extends QueryAst {
kind = "update";
table;
set;
where;
returning;
constructor(table, set = {}, where, returning) {
super();
this.table = table;
this.set = frozenRecordCopy(set);
this.where = where;
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
this.freeze();
}
static table(table) {
return new UpdateAst(table);
}
withSet(set) {
return new UpdateAst(this.table, set, this.where, this.returning);
}
withWhere(where) {
return new UpdateAst(this.table, this.set, where, this.returning);
}
withReturning(returning) {
return new UpdateAst(this.table, this.set, this.where, returning);
}
rewrite(rewriter) {
return new UpdateAst(rewriteTableSource(this.table, rewriter), rewriteUpdateSet(this.set, rewriter), this.where?.rewrite(rewriter), this.returning?.map((item) => rewriteProjectionItem(item, rewriter)));
}
collectParamRefs() {
const refs = [];
for (const value of Object.values(this.set)) refs.push(...value.collectParamRefs());
if (this.where) refs.push(...this.where.collectParamRefs());
for (const item of this.returning ?? []) if (item.expr.kind !== "literal") refs.push(...item.expr.collectParamRefs());
return refs;
}
toQueryAst() {
return this;
}
};
var DeleteAst = class DeleteAst extends QueryAst {
kind = "delete";
table;
where;
returning;
constructor(table, where, returning) {
super();
this.table = table;
this.where = where;
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
this.freeze();
}
static from(table) {
return new DeleteAst(table);
}
withWhere(where) {
return new DeleteAst(this.table, where, this.returning);
}
withReturning(returning) {
return new DeleteAst(this.table, this.where, returning);
}
rewrite(rewriter) {
return new DeleteAst(rewriteTableSource(this.table, rewriter), this.where?.rewrite(rewriter), this.returning?.map((item) => rewriteProjectionItem(item, rewriter)));
}
collectParamRefs() {
const refs = [];
if (this.where) refs.push(...this.where.collectParamRefs());
for (const item of this.returning ?? []) if (item.expr.kind !== "literal") refs.push(...item.expr.collectParamRefs());
return refs;
}
toQueryAst() {
return this;
}
};
/**
* Raw-SQL query AST node carrying interpolated parameter / expression nodes
* embedded inside literal SQL fragments.
*
* `fragments` and `args` are interleaved during lowering:
* `fragments[0] + lower(args[0]) + fragments[1] + ... + fragments[n]`.
* Construction enforces `fragments.length === args.length + 1`.
*
* Extends {@link QueryAst} (whole-query AST, not a sub-expression).
* Construction does not validate that each arg is a `ParamRef` /
* `AnyExpression`: the type system already rejects bare values because
* `args` is typed `readonly AnyExpression[]`. The user-facing `raw\`...\``
* factory (separate `sql-raw-factory` component) layers stricter
* type-level rejection on top of this AST node.
*/
var RawSqlExpr = class RawSqlExpr extends QueryAst {
kind = "raw-sql";
fragments;
args;
constructor(fragments, args) {
super();
if (fragments.length !== args.length + 1) throw new Error(`RawSqlExpr: fragments.length must equal args.length + 1 (got fragments=${fragments.length}, args=${args.length})`);
this.fragments = Object.freeze([...fragments]);
this.args = Object.freeze([...args]);
this.freeze();
}
static of(fragments, args) {
return new RawSqlExpr(fragments, args);
}
collectParamRefs() {
const refs = [];
for (const arg of this.args) if (arg.kind === "param-ref") refs.push(arg);
else refs.push(...arg.collectParamRefs());
return refs;
}
toQueryAst() {
return this;
}
};
const queryAstKinds = /* @__PURE__ */ new Set([
"select",
"insert",
"update",
"delete",
"raw-sql"
]);
const whereExprKinds = /* @__PURE__ */ new Set([
"binary",
"and",
"or",
"exists",
"null-check",
"not"
]);
function isQueryAst(value) {
return typeof value === "object" && value !== null && "kind" in value && queryAstKinds.has(value.kind);
}
function isWhereExpr(value) {
return typeof value === "object" && value !== null && "kind" in value && whereExprKinds.has(value.kind);
}
//#endregion
export { RawSqlExpr as A, OperationExpr as C, PreparedParamRef as D, ParamRef as E, WindowFuncExpr as F, isQueryAst as I, isWhereExpr as L, SubqueryExpr as M, TableSource as N, ProjectionItem as O, UpdateAst as P, queryAstKinds as R, NullCheckExpr as S, OrderByItem as T, JsonArrayAggExpr as _, DefaultValueExpr as a, LiteralExpr as b, DoNothingConflictAction as c, ExistsExpr as d, FunctionSource as f, JoinAst as g, InsertOnConflict as h, ColumnRef as i, SelectAst as j, RawExpr as k, DoUpdateSetConflictAction as l, InsertAst as m, AndExpr as n, DeleteAst as o, IdentifierRef as p, BinaryExpr as r, DerivedTableSource as s, AggregateExpr as t, EqColJoinOn as u, JsonObjectExpr as v, OrExpr as w, NotExpr as x, ListExpression as y, whereExprKinds as z };
//# sourceMappingURL=types-DtzFztRV.mjs.map

Sorry, the diff of this file is too big to display

+1
-1
import { a as ForeignKeyConstraint, c as PrimaryKeyConstraint, i as DdlNode, l as UniqueConstraint, n as DdlColumn, o as FunctionColumnDefault, r as DdlColumnDefault, s as LiteralColumnDefault, t as CheckExpressionConstraint, u as isDdlNode } from "../ddl-types-DFKQr_qQ.mjs";
import { A as RawSqlExpr, C as OperationExpr, D as PreparedParamRef, E as ParamRef, F as WindowFuncExpr, I as isQueryAst, L as isWhereExpr, M as SubqueryExpr, N as TableSource, O as ProjectionItem, P as UpdateAst, R as queryAstKinds, S as NullCheckExpr, T as OrderByItem, _ as JsonArrayAggExpr, a as DefaultValueExpr, b as LiteralExpr, c as DoNothingConflictAction, d as ExistsExpr, f as FunctionSource, g as JoinAst, h as InsertOnConflict, i as ColumnRef, j as SelectAst, k as RawExpr, l as DoUpdateSetConflictAction, m as InsertAst, n as AndExpr, o as DeleteAst, p as IdentifierRef, r as BinaryExpr, s as DerivedTableSource, t as AggregateExpr, u as EqColJoinOn, v as JsonObjectExpr, w as OrExpr, x as NotExpr, y as ListExpression, z as whereExprKinds } from "../types-bqJC9IQ2.mjs";
import { A as RawSqlExpr, C as OperationExpr, D as PreparedParamRef, E as ParamRef, F as WindowFuncExpr, I as isQueryAst, L as isWhereExpr, M as SubqueryExpr, N as TableSource, O as ProjectionItem, P as UpdateAst, R as queryAstKinds, S as NullCheckExpr, T as OrderByItem, _ as JsonArrayAggExpr, a as DefaultValueExpr, b as LiteralExpr, c as DoNothingConflictAction, d as ExistsExpr, f as FunctionSource, g as JoinAst, h as InsertOnConflict, i as ColumnRef, j as SelectAst, k as RawExpr, l as DoUpdateSetConflictAction, m as InsertAst, n as AndExpr, o as DeleteAst, p as IdentifierRef, r as BinaryExpr, s as DerivedTableSource, t as AggregateExpr, u as EqColJoinOn, v as JsonObjectExpr, w as OrExpr, x as NotExpr, y as ListExpression, z as whereExprKinds } from "../types-DtzFztRV.mjs";
import { n as compact, t as collectOrderedParamRefs } from "../util-DQQgv2j1.mjs";

@@ -4,0 +4,0 @@ import { CodecDescriptorImpl, CodecImpl, column, voidParamsSchema } from "@prisma-next/framework-components/codec";

import { a as ForeignKeyConstraint, c as PrimaryKeyConstraint, l as UniqueConstraint, n as DdlColumn, o as FunctionColumnDefault, s as LiteralColumnDefault, t as CheckExpressionConstraint } from "../ddl-types-DFKQr_qQ.mjs";
import { C as OperationExpr, E as ParamRef, N as TableSource, O as ProjectionItem, P as UpdateAst, S as NullCheckExpr, T as OrderByItem, b as LiteralExpr, d as ExistsExpr, g as JoinAst, h as InsertOnConflict, i as ColumnRef, j as SelectAst, k as RawExpr, m as InsertAst, n as AndExpr, p as IdentifierRef, r as BinaryExpr, t as AggregateExpr, w as OrExpr } from "../types-bqJC9IQ2.mjs";
import { C as OperationExpr, E as ParamRef, N as TableSource, O as ProjectionItem, P as UpdateAst, S as NullCheckExpr, T as OrderByItem, b as LiteralExpr, d as ExistsExpr, g as JoinAst, h as InsertOnConflict, i as ColumnRef, j as SelectAst, k as RawExpr, m as InsertAst, n as AndExpr, p as IdentifierRef, r as BinaryExpr, t as AggregateExpr, w as OrExpr } from "../types-DtzFztRV.mjs";
import { blindCast } from "@prisma-next/utils/casts";

@@ -4,0 +4,0 @@ //#region src/contract-free/column.ts

@@ -1,2 +0,2 @@

import { C as OperationExpr, E as ParamRef, k as RawExpr } from "../types-bqJC9IQ2.mjs";
import { C as OperationExpr, E as ParamRef, k as RawExpr } from "../types-DtzFztRV.mjs";
import { runtimeError } from "@prisma-next/framework-components/runtime";

@@ -3,0 +3,0 @@ //#region src/expression.ts

import { a as ForeignKeyConstraint, c as PrimaryKeyConstraint, i as DdlNode, l as UniqueConstraint, n as DdlColumn, o as FunctionColumnDefault, r as DdlColumnDefault, s as LiteralColumnDefault, t as CheckExpressionConstraint, u as isDdlNode } from "./ddl-types-DFKQr_qQ.mjs";
import { A as RawSqlExpr, C as OperationExpr, D as PreparedParamRef, E as ParamRef, F as WindowFuncExpr, I as isQueryAst, L as isWhereExpr, M as SubqueryExpr, N as TableSource, O as ProjectionItem, P as UpdateAst, R as queryAstKinds, S as NullCheckExpr, T as OrderByItem, _ as JsonArrayAggExpr, a as DefaultValueExpr, b as LiteralExpr, c as DoNothingConflictAction, d as ExistsExpr, f as FunctionSource, g as JoinAst, h as InsertOnConflict, i as ColumnRef, j as SelectAst, k as RawExpr, l as DoUpdateSetConflictAction, m as InsertAst, n as AndExpr, o as DeleteAst, p as IdentifierRef, r as BinaryExpr, s as DerivedTableSource, t as AggregateExpr, u as EqColJoinOn, v as JsonObjectExpr, w as OrExpr, x as NotExpr, y as ListExpression, z as whereExprKinds } from "./types-bqJC9IQ2.mjs";
import { A as RawSqlExpr, C as OperationExpr, D as PreparedParamRef, E as ParamRef, F as WindowFuncExpr, I as isQueryAst, L as isWhereExpr, M as SubqueryExpr, N as TableSource, O as ProjectionItem, P as UpdateAst, R as queryAstKinds, S as NullCheckExpr, T as OrderByItem, _ as JsonArrayAggExpr, a as DefaultValueExpr, b as LiteralExpr, c as DoNothingConflictAction, d as ExistsExpr, f as FunctionSource, g as JoinAst, h as InsertOnConflict, i as ColumnRef, j as SelectAst, k as RawExpr, l as DoUpdateSetConflictAction, m as InsertAst, n as AndExpr, o as DeleteAst, p as IdentifierRef, r as BinaryExpr, s as DerivedTableSource, t as AggregateExpr, u as EqColJoinOn, v as JsonObjectExpr, w as OrExpr, x as NotExpr, y as ListExpression, z as whereExprKinds } from "./types-DtzFztRV.mjs";
import { n as compact, t as collectOrderedParamRefs } from "./util-DQQgv2j1.mjs";

@@ -4,0 +4,0 @@ import { SQL_CHAR_CODEC_ID, SQL_FLOAT_CODEC_ID, SQL_INT_CODEC_ID, SQL_TEXT_CODEC_ID, SQL_TIMESTAMP_CODEC_ID, SQL_VARCHAR_CODEC_ID, SqlCharCodec, SqlCharDescriptor, SqlFloatCodec, SqlFloatDescriptor, SqlIntCodec, SqlIntDescriptor, SqlTextCodec, SqlTextDescriptor, SqlTimestampCodec, SqlTimestampDescriptor, SqlVarcharCodec, SqlVarcharDescriptor, sqlCharColumn, sqlCharDecode, sqlCharDescriptor, sqlCharEncode, sqlCharRenderOutputType, sqlFloatColumn, sqlFloatDecode, sqlFloatDescriptor, sqlFloatEncode, sqlIntColumn, sqlIntDecode, sqlIntDescriptor, sqlIntEncode, sqlTextColumn, sqlTextDecode, sqlTextDescriptor, sqlTextEncode, sqlTimestampColumn, sqlTimestampDecode, sqlTimestampDecodeJson, sqlTimestampDescriptor, sqlTimestampEncode, sqlTimestampEncodeJson, sqlTimestampRenderOutputType, sqlVarcharColumn, sqlVarcharDecode, sqlVarcharDescriptor, sqlVarcharEncode, sqlVarcharRenderOutputType } from "./exports/ast.mjs";

{
"name": "@prisma-next/sql-relational-core",
"version": "0.14.0-dev.70",
"version": "0.14.0-dev.71",
"license": "Apache-2.0",

@@ -9,20 +9,20 @@ "type": "module",

"dependencies": {
"@prisma-next/contract": "0.14.0-dev.70",
"@prisma-next/framework-components": "0.14.0-dev.70",
"@prisma-next/operations": "0.14.0-dev.70",
"@prisma-next/sql-contract": "0.14.0-dev.70",
"@prisma-next/sql-operations": "0.14.0-dev.70",
"@prisma-next/utils": "0.14.0-dev.70",
"@prisma-next/contract": "0.14.0-dev.71",
"@prisma-next/framework-components": "0.14.0-dev.71",
"@prisma-next/operations": "0.14.0-dev.71",
"@prisma-next/sql-contract": "0.14.0-dev.71",
"@prisma-next/sql-operations": "0.14.0-dev.71",
"@prisma-next/utils": "0.14.0-dev.71",
"@standard-schema/spec": "^1.1.0",
"arktype": "^2.2.0",
"arktype": "^2.2.2",
"ts-toolbelt": "^9.6.0"
},
"devDependencies": {
"@prisma-next/sql-contract-ts": "0.14.0-dev.70",
"@prisma-next/test-utils": "0.14.0-dev.70",
"@prisma-next/tsconfig": "0.14.0-dev.70",
"@prisma-next/tsdown": "0.14.0-dev.70",
"tsdown": "0.22.1",
"@prisma-next/sql-contract-ts": "0.14.0-dev.71",
"@prisma-next/test-utils": "0.14.0-dev.71",
"@prisma-next/tsconfig": "0.14.0-dev.71",
"@prisma-next/tsdown": "0.14.0-dev.71",
"tsdown": "0.22.3",
"typescript": "5.9.3",
"vitest": "4.1.8"
"vitest": "4.1.10"
},

@@ -29,0 +29,0 @@ "peerDependencies": {

import { ifDefined } from "@prisma-next/utils/defined";
//#region src/ast/types.ts
function frozenArrayCopy(values) {
return Object.freeze([...values]);
}
function frozenOptionalRecordCopy(value) {
return value === void 0 ? void 0 : Object.freeze({ ...value });
}
function frozenRecordCopy(record) {
return Object.freeze({ ...record });
}
function frozenCodecRef(codec) {
const typeParams = codec.typeParams === void 0 ? void 0 : structuredClone(codec.typeParams);
const base = {
codecId: codec.codecId,
...ifDefined("typeParams", typeParams)
};
return Object.freeze(codec.many ? {
...base,
many: true
} : base);
}
function freezeRows(rows) {
return Object.freeze(rows.map((row) => Object.freeze({ ...row })));
}
function combineAll(folder, thunks) {
let result = folder.empty;
for (const thunk of thunks) {
if (folder.isAbsorbing?.(result)) return result;
result = folder.combine(result, thunk());
}
return result;
}
function rewriteComparable(value, rewriter) {
switch (value.kind) {
case "param-ref": return rewriter.paramRef ? rewriter.paramRef(value) : value;
case "prepared-param-ref": return rewriter.preparedParamRef ? rewriter.preparedParamRef(value) : value;
case "literal": return rewriter.literal ? rewriter.literal(value) : value;
case "list":
if (rewriter.list) return rewriter.list(value);
return value.rewrite(rewriter);
default: return value.rewrite(rewriter);
}
}
function foldComparable(value, folder) {
switch (value.kind) {
case "param-ref": return folder.paramRef ? folder.paramRef(value) : folder.empty;
case "prepared-param-ref": return folder.preparedParamRef ? folder.preparedParamRef(value) : folder.empty;
case "literal": return folder.literal ? folder.literal(value) : folder.empty;
case "list": return value.fold(folder);
default: return value.fold(folder);
}
}
function collectColumnRefsWith(node) {
return node.fold({
empty: [],
combine: (a, b) => [...a, ...b],
columnRef: (columnRef) => [columnRef],
select: (ast) => ast.collectColumnRefs()
});
}
function collectParamRefsWith(node) {
return node.fold({
empty: [],
combine: (a, b) => [...a, ...b],
paramRef: (paramRef) => [paramRef],
preparedParamRef: (paramRef) => [paramRef],
select: (ast) => ast.collectParamRefs()
});
}
function rewriteTableSource(table, rewriter) {
return rewriter.tableSource ? rewriter.tableSource(table) : table;
}
function rewriteProjectionItem(item, rewriter) {
const rewrittenExpr = item.expr.kind === "literal" ? rewriter.literal ? rewriter.literal(item.expr) : item.expr : item.expr.rewrite(rewriter);
return new ProjectionItem(item.alias, rewrittenExpr, item.codec);
}
function rewriteInsertValue(value, rewriter) {
switch (value.kind) {
case "param-ref": return rewriter.paramRef ? rewriteParamRefForInsert(value, rewriter) : value;
case "prepared-param-ref": return rewriter.preparedParamRef ? rewriter.preparedParamRef(value) : value;
case "column-ref": return rewriter.columnRef ? rewriteColumnRefForInsert(value, rewriter) : value;
case "default-value": return value;
case "raw-expr": return value;
}
}
function rewriteParamRefForInsert(value, rewriter) {
const rewritten = rewriter.paramRef ? rewriter.paramRef(value) : value;
return rewritten.kind === "param-ref" ? rewritten : value;
}
function rewriteColumnRefForInsert(value, rewriter) {
const rewritten = rewriter.columnRef ? rewriter.columnRef(value) : value;
return rewritten.kind === "column-ref" ? rewritten : value;
}
function rewriteInsertRow(row, rewriter) {
const result = {};
for (const [key, value] of Object.entries(row)) result[key] = rewriteInsertValue(value, rewriter);
return result;
}
function rewriteUpdateSet(set, rewriter) {
const result = {};
for (const [key, value] of Object.entries(set)) result[key] = value.rewrite(rewriter);
return result;
}
function rewriteLimitOffset(value, rewriter) {
if (value === void 0 || typeof value === "number") return value;
return value.rewrite(rewriter);
}
function rewriteOnConflict(onConflict, rewriter) {
const columns = onConflict.columns.map((columnRef) => {
const rewritten = rewriter.columnRef ? rewriter.columnRef(columnRef) : columnRef;
return rewritten.kind === "column-ref" ? rewritten : columnRef;
});
if (onConflict.action.kind === "do-nothing") return new InsertOnConflict(columns, new DoNothingConflictAction());
return new InsertOnConflict(columns, new DoUpdateSetConflictAction(rewriteUpdateSet(onConflict.action.set, rewriter)));
}
var AstNode = class {
freeze() {
Object.freeze(this);
}
};
var QueryAst = class extends AstNode {};
var FromSource = class extends AstNode {};
var Expression = class extends AstNode {
collectColumnRefs() {
return collectColumnRefsWith(this);
}
collectParamRefs() {
return collectParamRefsWith(this);
}
baseColumnRef() {
throw new Error(`${this.constructor.name} does not expose a base column reference`);
}
toExpr() {
return this;
}
not() {
return new NotExpr(this);
}
};
var TableSource = class TableSource extends FromSource {
kind = "table-source";
name;
alias;
/**
* Resolved storage namespace coordinate for this table, stamped when the
* table proxy constructs the AST. Renderers qualify via the namespace
* concretion's `qualifyTable()` using this id — never by re-resolving the
* bare table name at render time.
*/
namespaceId;
constructor(name, alias, namespaceId) {
super();
this.name = name;
this.alias = alias;
this.namespaceId = namespaceId;
}
static named(name, alias, namespaceId) {
const source = new TableSource(name, alias, namespaceId);
source.freeze();
return source;
}
rewrite(rewriter) {
return rewriter.tableSource ? rewriter.tableSource(this) : this;
}
toFromSource() {
return this;
}
};
var DerivedTableSource = class DerivedTableSource extends FromSource {
kind = "derived-table-source";
alias;
query;
constructor(alias, query) {
super();
this.alias = alias;
this.query = query;
this.freeze();
}
static as(alias, query) {
return new DerivedTableSource(alias, query);
}
rewrite(rewriter) {
return new DerivedTableSource(this.alias, this.query.rewrite(rewriter));
}
toFromSource() {
return this;
}
};
var FunctionSource = class FunctionSource extends FromSource {
kind = "function-source";
fn;
args;
alias;
constructor(fn, args, alias) {
super();
this.fn = fn;
this.args = frozenArrayCopy(args);
this.alias = alias;
this.freeze();
}
static of(fn, args, alias) {
return new FunctionSource(fn, args, alias);
}
rewrite(rewriter) {
const rewrittenArgs = this.args.map((arg) => rewriteComparable(arg, rewriter));
if (rewrittenArgs.every((arg, i) => arg === this.args[i])) return this;
return new FunctionSource(this.fn, rewrittenArgs, this.alias);
}
toFromSource() {
return this;
}
};
var ColumnRef = class ColumnRef extends Expression {
kind = "column-ref";
table;
column;
constructor(table, column) {
super();
this.table = table;
this.column = column;
this.freeze();
}
static of(table, column) {
return new ColumnRef(table, column);
}
accept(visitor) {
return visitor.columnRef(this);
}
rewrite(rewriter) {
return rewriter.columnRef ? rewriter.columnRef(this) : this;
}
fold(folder) {
return folder.columnRef ? folder.columnRef(this) : folder.empty;
}
baseColumnRef() {
return this;
}
};
var IdentifierRef = class IdentifierRef extends Expression {
kind = "identifier-ref";
name;
constructor(name) {
super();
this.name = name;
this.freeze();
}
static of(name) {
return new IdentifierRef(name);
}
accept(visitor) {
return visitor.identifierRef(this);
}
rewrite(rewriter) {
return rewriter.identifierRef ? rewriter.identifierRef(this) : this;
}
fold(folder) {
return folder.identifierRef ? folder.identifierRef(this) : folder.empty;
}
};
var ParamRef = class ParamRef extends Expression {
kind = "param-ref";
value;
name;
/**
* Codec identity carried by every column-bound `ParamRef`. The encode-side dispatch path materialises the per-instance codec through `contractCodecs.forCodecRef(codec)` — content-keyed memoisation on `(codecId, canonicalize(typeParams))` keeps repeated lookups for the same logical column on one shared {@link Codec}.
*
* `codec` may be `undefined` for `ParamRef`s constructed without a column-bound site (literals, transient builder state); the runtime treats those as untyped passthroughs.
*/
codec;
constructor(value, options) {
super();
this.value = value;
this.name = options?.name;
this.codec = options?.codec ? frozenCodecRef(options.codec) : void 0;
this.freeze();
}
static of(value, options) {
return new ParamRef(value, options);
}
accept(visitor) {
return visitor.param(this);
}
rewrite(rewriter) {
return rewriter.paramRef ? rewriter.paramRef(this) : this;
}
fold(folder) {
return folder.paramRef ? folder.paramRef(this) : folder.empty;
}
};
/**
* Bind-site placeholder: occupies the same positions as `ParamRef` in the
* AST, but carries no value — the value is supplied per-execute by the
* `PreparedStatement.execute(params)` caller and matched to this node by
* `name`.
*/
var PreparedParamRef = class PreparedParamRef extends Expression {
kind = "prepared-param-ref";
name;
codec;
constructor(name, codec) {
super();
this.name = name;
this.codec = frozenCodecRef(codec);
this.freeze();
}
static of(name, codec) {
return new PreparedParamRef(name, codec);
}
accept(visitor) {
return visitor.preparedParam(this);
}
rewrite(rewriter) {
return rewriter.preparedParamRef ? rewriter.preparedParamRef(this) : this;
}
fold(folder) {
return folder.preparedParamRef ? folder.preparedParamRef(this) : folder.empty;
}
};
var DefaultValueExpr = class extends AstNode {
kind = "default-value";
constructor() {
super();
this.freeze();
}
};
var LiteralExpr = class LiteralExpr extends Expression {
kind = "literal";
value;
constructor(value) {
super();
this.value = value;
this.freeze();
}
static of(value) {
return new LiteralExpr(value);
}
accept(visitor) {
return visitor.literal(this);
}
rewrite(rewriter) {
return rewriter.literal ? rewriter.literal(this) : this;
}
fold(folder) {
return folder.literal ? folder.literal(this) : folder.empty;
}
};
var SubqueryExpr = class SubqueryExpr extends Expression {
kind = "subquery";
query;
constructor(query) {
super();
this.query = query;
this.freeze();
}
static of(query) {
return new SubqueryExpr(query);
}
accept(visitor) {
return visitor.subquery(this);
}
rewrite(rewriter) {
return new SubqueryExpr(this.query.rewrite(rewriter));
}
fold(folder) {
return folder.select ? folder.select(this.query) : folder.empty;
}
};
var OperationExpr = class OperationExpr extends Expression {
kind = "operation";
method;
self;
args;
returns;
lowering;
constructor(options) {
super();
this.method = options.method;
this.self = options.self;
this.args = frozenArrayCopy(options.args ?? []);
this.returns = options.returns;
this.lowering = options.lowering;
this.freeze();
}
accept(visitor) {
return visitor.operation(this);
}
rewrite(rewriter) {
return new OperationExpr({
method: this.method,
self: this.self.rewrite(rewriter),
args: this.args.map((arg) => rewriteComparable(arg, rewriter)),
returns: this.returns,
lowering: this.lowering
});
}
fold(folder) {
return combineAll(folder, [() => this.self.fold(folder), ...this.args.map((arg) => () => foldComparable(arg, folder))]);
}
baseColumnRef() {
return this.self.baseColumnRef();
}
};
var RawExpr = class extends Expression {
kind = "raw-expr";
parts;
returns;
constructor(options) {
super();
this.parts = frozenArrayCopy(options.parts);
this.returns = options.returns;
this.freeze();
}
accept(visitor) {
return visitor.rawExpr(this);
}
rewrite(rewriter) {
return rewriter.rawExpr ? rewriter.rawExpr(this) : this;
}
fold(folder) {
if (folder.rawExpr) return folder.rawExpr(this);
return combineAll(folder, this.parts.filter((p) => typeof p !== "string").map((p) => () => p.fold(folder)));
}
};
var AggregateExpr = class AggregateExpr extends Expression {
kind = "aggregate";
fn;
expr;
constructor(fn, expr) {
super();
if (fn !== "count" && expr === void 0) throw new Error(`Aggregate function "${fn}" requires an expression`);
this.fn = fn;
this.expr = expr;
this.freeze();
}
static count(expr) {
return new AggregateExpr("count", expr);
}
static sum(expr) {
return new AggregateExpr("sum", expr);
}
static avg(expr) {
return new AggregateExpr("avg", expr);
}
static min(expr) {
return new AggregateExpr("min", expr);
}
static max(expr) {
return new AggregateExpr("max", expr);
}
accept(visitor) {
return visitor.aggregate(this);
}
rewrite(rewriter) {
return this.expr === void 0 ? this : new AggregateExpr(this.fn, this.expr.rewrite(rewriter));
}
fold(folder) {
return this.expr ? this.expr.fold(folder) : folder.empty;
}
};
/**
* Window function call: `fn(args) OVER (PARTITION BY ... ORDER BY ...)`.
*
* Both `partitionBy` and `orderBy` are optional; an empty `OVER ()`
* clause is legal SQL but rarely useful. For `ROW_NUMBER`, `RANK`, and
* `DENSE_RANK` the standard mandates an `ORDER BY` for deterministic
* results — callers are expected to provide one, but the AST does not
* enforce it.
*
* The `args` slot exists for future window function additions that take
* arguments (e.g. `COUNT(*) OVER`, `SUM(x) OVER`); `ROW_NUMBER` and the
* other ranking functions take no arguments.
*/
var WindowFuncExpr = class WindowFuncExpr extends Expression {
kind = "window-func";
fn;
args;
partitionBy;
orderBy;
constructor(options) {
super();
this.fn = options.fn;
this.args = options.args && options.args.length > 0 ? frozenArrayCopy(options.args) : [];
this.partitionBy = options.partitionBy && options.partitionBy.length > 0 ? frozenArrayCopy(options.partitionBy) : void 0;
this.orderBy = options.orderBy && options.orderBy.length > 0 ? frozenArrayCopy(options.orderBy) : void 0;
this.freeze();
}
static rowNumber(options) {
return new WindowFuncExpr({
fn: "row_number",
...options
});
}
accept(visitor) {
return visitor.windowFunc(this);
}
rewrite(rewriter) {
return new WindowFuncExpr({
fn: this.fn,
args: this.args.map((arg) => arg.rewrite(rewriter)),
...ifDefined("partitionBy", this.partitionBy?.map((expr) => expr.rewrite(rewriter))),
...ifDefined("orderBy", this.orderBy?.map((orderItem) => orderItem.rewrite(rewriter)))
});
}
fold(folder) {
return combineAll(folder, [
...this.args.map((arg) => () => arg.fold(folder)),
...(this.partitionBy ?? []).map((expr) => () => expr.fold(folder)),
...(this.orderBy ?? []).map((orderItem) => () => orderItem.expr.fold(folder))
]);
}
};
var JsonObjectExpr = class JsonObjectExpr extends Expression {
kind = "json-object";
entries;
constructor(entries) {
super();
this.entries = frozenArrayCopy(entries.map((entry) => Object.freeze({ ...entry })));
this.freeze();
}
static entry(key, value) {
return {
key,
value
};
}
static fromEntries(entries) {
return new JsonObjectExpr(entries);
}
accept(visitor) {
return visitor.jsonObject(this);
}
rewrite(rewriter) {
return new JsonObjectExpr(this.entries.map((entry) => ({
key: entry.key,
value: entry.value.kind === "literal" ? rewriter.literal ? rewriter.literal(entry.value) : entry.value : entry.value.rewrite(rewriter)
})));
}
fold(folder) {
return combineAll(folder, this.entries.map((entry) => () => entry.value.kind === "literal" ? folder.literal ? folder.literal(entry.value) : folder.empty : entry.value.fold(folder)));
}
};
var OrderByItem = class OrderByItem extends AstNode {
kind = "order-by-item";
expr;
dir;
constructor(expr, dir) {
super();
this.expr = expr;
this.dir = dir;
this.freeze();
}
static asc(expr) {
return new OrderByItem(expr, "asc");
}
static desc(expr) {
return new OrderByItem(expr, "desc");
}
rewrite(rewriter) {
return new OrderByItem(this.expr.rewrite(rewriter), this.dir);
}
/**
* A new frozen item with the sort direction flipped and `expr` unchanged.
* Integrations that own pagination (e.g. backward cursor pagination) use
* this to reverse a user's sort order without reaching into the AST.
*/
reverse() {
return new OrderByItem(this.expr, this.dir === "asc" ? "desc" : "asc");
}
};
var JsonArrayAggExpr = class JsonArrayAggExpr extends Expression {
kind = "json-array-agg";
expr;
onEmpty;
orderBy;
constructor(expr, onEmpty = "null", orderBy) {
super();
this.expr = expr;
this.onEmpty = onEmpty;
this.orderBy = orderBy && orderBy.length > 0 ? frozenArrayCopy(orderBy) : void 0;
this.freeze();
}
static of(expr, onEmpty = "null", orderBy) {
return new JsonArrayAggExpr(expr, onEmpty, orderBy);
}
accept(visitor) {
return visitor.jsonArrayAgg(this);
}
rewrite(rewriter) {
return new JsonArrayAggExpr(this.expr.rewrite(rewriter), this.onEmpty, this.orderBy?.map((orderItem) => orderItem.rewrite(rewriter)));
}
fold(folder) {
return combineAll(folder, [() => this.expr.fold(folder), ...(this.orderBy ?? []).map((orderItem) => () => orderItem.expr.fold(folder))]);
}
};
var ListExpression = class ListExpression extends Expression {
kind = "list";
values;
constructor(values) {
super();
this.values = frozenArrayCopy(values);
this.freeze();
}
static of(values) {
return new ListExpression(values);
}
static fromValues(values) {
return new ListExpression(values.map((value) => new LiteralExpr(value)));
}
accept(visitor) {
return visitor.list(this);
}
rewrite(rewriter) {
if (rewriter.list) return rewriter.list(this);
return new ListExpression(this.values.map((value) => value.rewrite(rewriter)));
}
fold(folder) {
if (folder.list) return folder.list(this);
return combineAll(folder, this.values.map((value) => () => value.fold(folder)));
}
};
var BinaryExpr = class BinaryExpr extends Expression {
kind = "binary";
op;
left;
right;
constructor(op, left, right) {
super();
this.op = op;
this.left = left;
this.right = right;
this.freeze();
}
static eq(left, right) {
return new BinaryExpr("eq", left, right);
}
static neq(left, right) {
return new BinaryExpr("neq", left, right);
}
static gt(left, right) {
return new BinaryExpr("gt", left, right);
}
static lt(left, right) {
return new BinaryExpr("lt", left, right);
}
static gte(left, right) {
return new BinaryExpr("gte", left, right);
}
static lte(left, right) {
return new BinaryExpr("lte", left, right);
}
static like(left, right) {
return new BinaryExpr("like", left, right);
}
static in(left, right) {
return new BinaryExpr("in", left, right);
}
static notIn(left, right) {
return new BinaryExpr("notIn", left, right);
}
accept(visitor) {
return visitor.binary(this);
}
rewrite(rewriter) {
return new BinaryExpr(this.op, rewriteComparable(this.left, rewriter), rewriteComparable(this.right, rewriter));
}
fold(folder) {
return combineAll(folder, [() => foldComparable(this.left, folder), () => foldComparable(this.right, folder)]);
}
};
var AndExpr = class AndExpr extends Expression {
kind = "and";
exprs;
constructor(exprs) {
super();
this.exprs = frozenArrayCopy(exprs);
this.freeze();
}
static of(exprs) {
return new AndExpr(exprs);
}
static true() {
return new AndExpr([]);
}
accept(visitor) {
return visitor.and(this);
}
rewrite(rewriter) {
return new AndExpr(this.exprs.map((expr) => expr.rewrite(rewriter)));
}
fold(folder) {
return combineAll(folder, this.exprs.map((expr) => () => expr.fold(folder)));
}
};
var OrExpr = class OrExpr extends Expression {
kind = "or";
exprs;
constructor(exprs) {
super();
this.exprs = frozenArrayCopy(exprs);
this.freeze();
}
static of(exprs) {
return new OrExpr(exprs);
}
static false() {
return new OrExpr([]);
}
accept(visitor) {
return visitor.or(this);
}
rewrite(rewriter) {
return new OrExpr(this.exprs.map((expr) => expr.rewrite(rewriter)));
}
fold(folder) {
return combineAll(folder, this.exprs.map((expr) => () => expr.fold(folder)));
}
};
var ExistsExpr = class ExistsExpr extends Expression {
kind = "exists";
notExists;
subquery;
constructor(subquery, notExists = false) {
super();
this.notExists = notExists;
this.subquery = subquery;
this.freeze();
}
static exists(subquery) {
return new ExistsExpr(subquery, false);
}
static notExists(subquery) {
return new ExistsExpr(subquery, true);
}
accept(visitor) {
return visitor.exists(this);
}
rewrite(rewriter) {
return new ExistsExpr(this.subquery.rewrite(rewriter), this.notExists);
}
fold(folder) {
return folder.select ? folder.select(this.subquery) : folder.empty;
}
};
var NullCheckExpr = class NullCheckExpr extends Expression {
kind = "null-check";
expr;
isNull;
constructor(expr, isNull) {
super();
this.expr = expr;
this.isNull = isNull;
this.freeze();
}
static isNull(expr) {
return new NullCheckExpr(expr, true);
}
static isNotNull(expr) {
return new NullCheckExpr(expr, false);
}
accept(visitor) {
return visitor.nullCheck(this);
}
rewrite(rewriter) {
return new NullCheckExpr(this.expr.rewrite(rewriter), this.isNull);
}
fold(folder) {
return this.expr.fold(folder);
}
};
var NotExpr = class NotExpr extends Expression {
kind = "not";
expr;
constructor(expr) {
super();
this.expr = expr;
this.freeze();
}
toWhereExpr() {
return this;
}
accept(visitor) {
return visitor.not(this);
}
rewrite(rewriter) {
return new NotExpr(this.expr.rewrite(rewriter));
}
fold(folder) {
return this.expr.fold(folder);
}
};
var EqColJoinOn = class EqColJoinOn extends AstNode {
kind = "eq-col-join-on";
left;
right;
constructor(left, right) {
super();
this.left = left;
this.right = right;
this.freeze();
}
static of(left, right) {
return new EqColJoinOn(left, right);
}
rewrite(rewriter) {
return rewriter.eqColJoinOn ? rewriter.eqColJoinOn(this) : this;
}
};
var JoinAst = class JoinAst extends AstNode {
kind = "join";
joinType;
source;
lateral;
on;
constructor(joinType, source, on, lateral = false) {
super();
this.joinType = joinType;
this.source = source;
this.lateral = lateral;
this.on = on;
this.freeze();
}
static inner(source, on, lateral = false) {
return new JoinAst("inner", source, on, lateral);
}
static left(source, on, lateral = false) {
return new JoinAst("left", source, on, lateral);
}
static right(source, on, lateral = false) {
return new JoinAst("right", source, on, lateral);
}
static full(source, on, lateral = false) {
return new JoinAst("full", source, on, lateral);
}
rewrite(rewriter) {
return new JoinAst(this.joinType, this.source.rewrite(rewriter), this.on.kind === "eq-col-join-on" ? this.on.rewrite(rewriter) : this.on.rewrite(rewriter), this.lateral);
}
};
var ProjectionItem = class ProjectionItem extends AstNode {
kind = "projection-item";
alias;
expr;
/**
* Codec identity for the projected cell. Decode-side dispatch resolves the per-instance codec through `contractCodecs.forCodecRef(codec)` — content-keyed memoisation collapses repeated lookups for the same logical column onto one shared {@link Codec}.
*
* Stays `undefined` for non-column-bound projections (computed expressions, subqueries, raw aliases) whose decoded type the runtime cannot infer from a single contract column.
*/
codec;
constructor(alias, expr, codec) {
super();
this.alias = alias;
this.expr = expr;
this.codec = codec ? frozenCodecRef(codec) : void 0;
this.freeze();
}
static of(alias, expr, codec) {
return new ProjectionItem(alias, expr, codec);
}
withCodec(codec) {
return new ProjectionItem(this.alias, this.expr, codec);
}
};
var SelectAst = class SelectAst extends QueryAst {
kind = "select";
from;
joins;
projection;
where;
orderBy;
distinct;
distinctOn;
groupBy;
having;
limit;
offset;
selectAllIntent;
constructor(options) {
super();
this.from = options.from;
this.joins = options.joins && options.joins.length > 0 ? frozenArrayCopy(options.joins) : void 0;
this.projection = frozenArrayCopy(options.projection);
this.where = options.where;
this.orderBy = options.orderBy && options.orderBy.length > 0 ? frozenArrayCopy(options.orderBy) : void 0;
this.distinct = options.distinct;
this.distinctOn = options.distinctOn && options.distinctOn.length > 0 ? frozenArrayCopy(options.distinctOn) : void 0;
this.groupBy = options.groupBy && options.groupBy.length > 0 ? frozenArrayCopy(options.groupBy) : void 0;
this.having = options.having;
this.limit = options.limit;
this.offset = options.offset;
this.selectAllIntent = frozenOptionalRecordCopy(options.selectAllIntent);
this.freeze();
}
static from(from) {
return new SelectAst({
from,
joins: void 0,
projection: [],
where: void 0,
orderBy: void 0,
distinct: void 0,
distinctOn: void 0,
groupBy: void 0,
having: void 0,
limit: void 0,
offset: void 0,
selectAllIntent: void 0
});
}
static noFrom() {
return new SelectAst({
joins: void 0,
projection: [],
where: void 0,
orderBy: void 0,
distinct: void 0,
distinctOn: void 0,
groupBy: void 0,
having: void 0,
limit: void 0,
offset: void 0,
selectAllIntent: void 0
});
}
toOptions() {
return {
...this.from !== void 0 ? { from: this.from } : {},
joins: this.joins,
projection: this.projection,
where: this.where,
orderBy: this.orderBy,
distinct: this.distinct,
distinctOn: this.distinctOn,
groupBy: this.groupBy,
having: this.having,
limit: this.limit,
offset: this.offset,
selectAllIntent: this.selectAllIntent
};
}
withFrom(from) {
return new SelectAst({
...this.toOptions(),
from
});
}
withJoins(joins) {
return new SelectAst({
...this.toOptions(),
joins: joins.length > 0 ? joins : void 0
});
}
withProjection(projection) {
return new SelectAst({
...this.toOptions(),
projection
});
}
addProjection(alias, expr) {
return new SelectAst({
...this.toOptions(),
projection: [...this.projection, new ProjectionItem(alias, expr)]
});
}
withWhere(where) {
return new SelectAst({
...this.toOptions(),
where
});
}
withOrderBy(orderBy) {
return new SelectAst({
...this.toOptions(),
orderBy: orderBy.length > 0 ? orderBy : void 0
});
}
withDistinct(enabled = true) {
return new SelectAst({
...this.toOptions(),
distinct: enabled ? true : void 0
});
}
withDistinctOn(distinctOn) {
return new SelectAst({
...this.toOptions(),
distinctOn: distinctOn.length > 0 ? distinctOn : void 0
});
}
withGroupBy(groupBy) {
return new SelectAst({
...this.toOptions(),
groupBy: groupBy.length > 0 ? groupBy : void 0
});
}
withHaving(having) {
return new SelectAst({
...this.toOptions(),
having
});
}
withLimit(limit) {
return new SelectAst({
...this.toOptions(),
limit
});
}
withOffset(offset) {
return new SelectAst({
...this.toOptions(),
offset
});
}
withSelectAllIntent(selectAllIntent) {
return new SelectAst({
...this.toOptions(),
selectAllIntent
});
}
rewrite(rewriter) {
const rewrittenFrom = this.from?.rewrite(rewriter);
const rewritten = new SelectAst({
...rewrittenFrom !== void 0 ? { from: rewrittenFrom } : {},
joins: this.joins?.map((join) => join.rewrite(rewriter)),
projection: this.projection.map((projection) => new ProjectionItem(projection.alias, projection.expr.kind === "literal" ? rewriter.literal ? rewriter.literal(projection.expr) : projection.expr : projection.expr.rewrite(rewriter), projection.codec)),
where: this.where?.rewrite(rewriter),
orderBy: this.orderBy?.map((orderItem) => orderItem.rewrite(rewriter)),
distinct: this.distinct,
distinctOn: this.distinctOn?.map((expr) => expr.rewrite(rewriter)),
groupBy: this.groupBy?.map((expr) => expr.rewrite(rewriter)),
having: this.having?.rewrite(rewriter),
limit: rewriteLimitOffset(this.limit, rewriter),
offset: rewriteLimitOffset(this.offset, rewriter),
selectAllIntent: this.selectAllIntent
});
return rewriter.select ? rewriter.select(rewritten) : rewritten;
}
collectColumnRefs() {
const refs = [];
const pushRefs = (columns) => {
refs.push(...columns);
};
if (this.from?.kind === "derived-table-source") pushRefs(this.from.query.collectColumnRefs());
else if (this.from?.kind === "function-source") for (const arg of this.from.args) pushRefs(arg.collectColumnRefs());
for (const projection of this.projection) if (!(projection.expr.kind === "literal")) pushRefs(projection.expr.collectColumnRefs());
if (this.where) pushRefs(this.where.collectColumnRefs());
if (this.having) pushRefs(this.having.collectColumnRefs());
for (const orderItem of this.orderBy ?? []) pushRefs(orderItem.expr.collectColumnRefs());
for (const expr of this.distinctOn ?? []) pushRefs(expr.collectColumnRefs());
for (const expr of this.groupBy ?? []) pushRefs(expr.collectColumnRefs());
for (const join of this.joins ?? []) {
if (join.source.kind === "derived-table-source") pushRefs(join.source.query.collectColumnRefs());
else if (join.source.kind === "function-source") for (const arg of join.source.args) pushRefs(arg.collectColumnRefs());
if (join.on.kind === "eq-col-join-on") refs.push(join.on.left, join.on.right);
else pushRefs(join.on.collectColumnRefs());
}
if (typeof this.limit === "object") pushRefs(this.limit.collectColumnRefs());
if (typeof this.offset === "object") pushRefs(this.offset.collectColumnRefs());
return refs;
}
collectParamRefs() {
const refs = [];
const pushRefs = (params) => {
refs.push(...params);
};
if (this.from?.kind === "derived-table-source") pushRefs(this.from.query.collectParamRefs());
else if (this.from?.kind === "function-source") for (const arg of this.from.args) pushRefs(arg.collectParamRefs());
for (const projection of this.projection) if (!(projection.expr.kind === "literal")) pushRefs(projection.expr.collectParamRefs());
if (this.where) pushRefs(this.where.collectParamRefs());
if (this.having) pushRefs(this.having.collectParamRefs());
for (const orderItem of this.orderBy ?? []) pushRefs(orderItem.expr.collectParamRefs());
for (const expr of this.distinctOn ?? []) pushRefs(expr.collectParamRefs());
for (const expr of this.groupBy ?? []) pushRefs(expr.collectParamRefs());
for (const join of this.joins ?? []) {
if (join.source.kind === "derived-table-source") pushRefs(join.source.query.collectParamRefs());
else if (join.source.kind === "function-source") for (const arg of join.source.args) pushRefs(arg.collectParamRefs());
if (!(join.on.kind === "eq-col-join-on")) pushRefs(join.on.collectParamRefs());
}
if (typeof this.limit === "object") pushRefs(this.limit.collectParamRefs());
if (typeof this.offset === "object") pushRefs(this.offset.collectParamRefs());
return refs;
}
toQueryAst() {
return this;
}
};
var InsertOnConflictAction = class extends AstNode {};
var DoNothingConflictAction = class extends InsertOnConflictAction {
kind = "do-nothing";
constructor() {
super();
this.freeze();
}
toInsertOnConflictAction() {
return this;
}
};
var DoUpdateSetConflictAction = class extends InsertOnConflictAction {
kind = "do-update-set";
set;
constructor(set) {
super();
this.set = frozenRecordCopy(set);
this.freeze();
}
toInsertOnConflictAction() {
return this;
}
};
var InsertOnConflict = class InsertOnConflict extends AstNode {
kind = "insert-on-conflict";
columns;
action;
constructor(columns, action) {
super();
this.columns = frozenArrayCopy(columns);
this.action = action;
this.freeze();
}
static on(columns) {
return new InsertOnConflict(columns, new DoNothingConflictAction());
}
doNothing() {
return new InsertOnConflict(this.columns, new DoNothingConflictAction());
}
doUpdateSet(set) {
return new InsertOnConflict(this.columns, new DoUpdateSetConflictAction(set));
}
};
var InsertAst = class InsertAst extends QueryAst {
kind = "insert";
table;
rows;
onConflict;
returning;
constructor(table, rows = [{}], onConflict, returning) {
super();
this.table = table;
this.rows = freezeRows(rows);
this.onConflict = onConflict;
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
this.freeze();
}
static into(table) {
return new InsertAst(table);
}
withRows(rows) {
return new InsertAst(this.table, rows.map((row) => ({ ...row })), this.onConflict, this.returning);
}
withReturning(returning) {
return new InsertAst(this.table, this.rows.map((row) => ({ ...row })), this.onConflict, returning);
}
withOnConflict(onConflict) {
return new InsertAst(this.table, this.rows.map((row) => ({ ...row })), onConflict, this.returning);
}
rewrite(rewriter) {
return new InsertAst(rewriteTableSource(this.table, rewriter), this.rows.map((row) => rewriteInsertRow(row, rewriter)), this.onConflict ? rewriteOnConflict(this.onConflict, rewriter) : void 0, this.returning?.map((item) => rewriteProjectionItem(item, rewriter)));
}
collectParamRefs() {
const refs = [];
for (const row of this.rows) for (const value of Object.values(row)) if (value.kind === "param-ref" || value.kind === "prepared-param-ref") refs.push(value);
else if (value.kind === "raw-expr") refs.push(...value.collectParamRefs());
if (this.onConflict?.action.kind === "do-update-set") {
for (const value of Object.values(this.onConflict.action.set)) if (value.kind === "param-ref" || value.kind === "prepared-param-ref") refs.push(value);
}
for (const item of this.returning ?? []) if (item.expr.kind !== "literal") refs.push(...item.expr.collectParamRefs());
return refs;
}
toQueryAst() {
return this;
}
};
var UpdateAst = class UpdateAst extends QueryAst {
kind = "update";
table;
set;
where;
returning;
constructor(table, set = {}, where, returning) {
super();
this.table = table;
this.set = frozenRecordCopy(set);
this.where = where;
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
this.freeze();
}
static table(table) {
return new UpdateAst(table);
}
withSet(set) {
return new UpdateAst(this.table, set, this.where, this.returning);
}
withWhere(where) {
return new UpdateAst(this.table, this.set, where, this.returning);
}
withReturning(returning) {
return new UpdateAst(this.table, this.set, this.where, returning);
}
rewrite(rewriter) {
return new UpdateAst(rewriteTableSource(this.table, rewriter), rewriteUpdateSet(this.set, rewriter), this.where?.rewrite(rewriter), this.returning?.map((item) => rewriteProjectionItem(item, rewriter)));
}
collectParamRefs() {
const refs = [];
for (const value of Object.values(this.set)) refs.push(...value.collectParamRefs());
if (this.where) refs.push(...this.where.collectParamRefs());
for (const item of this.returning ?? []) if (item.expr.kind !== "literal") refs.push(...item.expr.collectParamRefs());
return refs;
}
toQueryAst() {
return this;
}
};
var DeleteAst = class DeleteAst extends QueryAst {
kind = "delete";
table;
where;
returning;
constructor(table, where, returning) {
super();
this.table = table;
this.where = where;
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
this.freeze();
}
static from(table) {
return new DeleteAst(table);
}
withWhere(where) {
return new DeleteAst(this.table, where, this.returning);
}
withReturning(returning) {
return new DeleteAst(this.table, this.where, returning);
}
rewrite(rewriter) {
return new DeleteAst(rewriteTableSource(this.table, rewriter), this.where?.rewrite(rewriter), this.returning?.map((item) => rewriteProjectionItem(item, rewriter)));
}
collectParamRefs() {
const refs = [];
if (this.where) refs.push(...this.where.collectParamRefs());
for (const item of this.returning ?? []) if (item.expr.kind !== "literal") refs.push(...item.expr.collectParamRefs());
return refs;
}
toQueryAst() {
return this;
}
};
/**
* Raw-SQL query AST node carrying interpolated parameter / expression nodes
* embedded inside literal SQL fragments.
*
* `fragments` and `args` are interleaved during lowering:
* `fragments[0] + lower(args[0]) + fragments[1] + ... + fragments[n]`.
* Construction enforces `fragments.length === args.length + 1`.
*
* Extends {@link QueryAst} (whole-query AST, not a sub-expression).
* Construction does not validate that each arg is a `ParamRef` /
* `AnyExpression`: the type system already rejects bare values because
* `args` is typed `readonly AnyExpression[]`. The user-facing `raw\`...\``
* factory (separate `sql-raw-factory` component) layers stricter
* type-level rejection on top of this AST node.
*/
var RawSqlExpr = class RawSqlExpr extends QueryAst {
kind = "raw-sql";
fragments;
args;
constructor(fragments, args) {
super();
if (fragments.length !== args.length + 1) throw new Error(`RawSqlExpr: fragments.length must equal args.length + 1 (got fragments=${fragments.length}, args=${args.length})`);
this.fragments = Object.freeze([...fragments]);
this.args = Object.freeze([...args]);
this.freeze();
}
static of(fragments, args) {
return new RawSqlExpr(fragments, args);
}
collectParamRefs() {
const refs = [];
for (const arg of this.args) if (arg.kind === "param-ref") refs.push(arg);
else refs.push(...arg.collectParamRefs());
return refs;
}
toQueryAst() {
return this;
}
};
const queryAstKinds = new Set([
"select",
"insert",
"update",
"delete",
"raw-sql"
]);
const whereExprKinds = new Set([
"binary",
"and",
"or",
"exists",
"null-check",
"not"
]);
function isQueryAst(value) {
return typeof value === "object" && value !== null && "kind" in value && queryAstKinds.has(value.kind);
}
function isWhereExpr(value) {
return typeof value === "object" && value !== null && "kind" in value && whereExprKinds.has(value.kind);
}
//#endregion
export { RawSqlExpr as A, OperationExpr as C, PreparedParamRef as D, ParamRef as E, WindowFuncExpr as F, isQueryAst as I, isWhereExpr as L, SubqueryExpr as M, TableSource as N, ProjectionItem as O, UpdateAst as P, queryAstKinds as R, NullCheckExpr as S, OrderByItem as T, JsonArrayAggExpr as _, DefaultValueExpr as a, LiteralExpr as b, DoNothingConflictAction as c, ExistsExpr as d, FunctionSource as f, JoinAst as g, InsertOnConflict as h, ColumnRef as i, SelectAst as j, RawExpr as k, DoUpdateSetConflictAction as l, InsertAst as m, AndExpr as n, DeleteAst as o, IdentifierRef as p, BinaryExpr as r, DerivedTableSource as s, AggregateExpr as t, EqColJoinOn as u, JsonObjectExpr as v, OrExpr as w, NotExpr as x, ListExpression as y, whereExprKinds as z };
//# sourceMappingURL=types-bqJC9IQ2.mjs.map

Sorry, the diff of this file is too big to display