Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | 132x 132x 1x 394x 4785x 3256x 1529x 2120x 1529x 2120x 2120x 2120x 721x 715x 715x 6x 881x 7783x 656x 11094x 124x 7861x 11605x 3256x 3256x 11605x 11594x 1x 2121x 1154x 967x 967x 2121x 268x 268x 268x 12748x 12748x 12748x | import type { VirtualType } from '../data-types/virtual-type.ts';
import { SqlExpression } from '../expressions/methods.ts';
import { SqlAst, type ExprNode, type OrderNode } from '../sql/ast.ts';
import { OrderDirection } from '../sql/order.ts';
import type { WhereCompiler } from '../where/where.ts';
import type { AttributeColumns } from './attribute-columns.ts';
import type { ModelDefinition } from './model-definition.ts';
import type { AttributeSpec, FindAttributeOptions, Order, OrderItem } from './select-options.ts';
const ast = new SqlAst();
const invalidOrder = (): never => {
throw new Error('Order must be type of array or instance of a valid sequelize method.');
};
/**
* Converts the attribute and expression lists of finders into SELECT expressions: `attributes`,
* `order`, `group` and `distinctOn` items, including `'jsonAttr.a.b'` paths.
*/
export class SelectTargets {
readonly #columns: AttributeColumns;
/**
* @param columns - Attribute to column mapping.
*/
constructor(columns: AttributeColumns) {
this.#columns = columns;
}
/**
* Selected columns for the `attributes` option.
*
* @param definition - Model definition.
* @param alias - Table alias.
* @param ctx - Where compiler of the model.
* @param attributes - `attributes` option.
* @returns Column expressions.
*/
columns(
definition: ModelDefinition,
alias: string,
ctx: WhereCompiler,
attributes: FindAttributeOptions | undefined,
): readonly ExprNode[] {
if (attributes === undefined || !Array.isArray(attributes)) {
return this.#specColumns(definition, alias, ctx, (attributes ?? {}) as AttributeSpec);
}
return SelectTargets.#withDependencies(definition, attributes).map((item) =>
this.#attributeItem(definition, alias, ctx, item),
);
}
// A VIRTUAL attribute has no column: selecting it selects the attributes it depends on (v6).
static #withDependencies(
definition: ModelDefinition,
attributes: readonly unknown[],
): readonly (string | readonly [unknown, string])[] {
const list = (attributes as readonly (string | readonly [unknown, string])[]).flatMap(
(item) => {
const type = typeof item === 'string' ? definition.attributes.get(item)?.type : undefined;
return type?.isVirtual === true ? [...((type as VirtualType).dependencies ?? [])] : [item];
},
);
return list.filter((item, index) => typeof item !== 'string' || list.indexOf(item) === index);
}
/**
* One `order` item.
*
* @param definition - Model definition.
* @param alias - Table alias.
* @param ctx - Where compiler of the model.
* @param item - Attribute name, `[target, direction]` or expression.
* @returns Order node.
*/
orderItem(
definition: ModelDefinition,
alias: string,
ctx: WhereCompiler,
item: unknown,
): OrderNode {
if (Array.isArray(item)) {
const [target, direction]: readonly unknown[] = item;
return {
expr: this.target(definition, alias, ctx, target),
...(direction === undefined ? {} : OrderDirection.parse(direction)),
};
}
return { expr: this.target(definition, alias, ctx, item) };
}
/**
* Expression of an attribute name, `'jsonAttr.a.b'` path, `'table.column'` or expression.
*
* @param definition - Model definition.
* @param alias - Table alias.
* @param ctx - Where compiler of the model.
* @param target - Attribute name or expression.
* @returns Expression.
*/
target(
definition: ModelDefinition,
alias: string,
ctx: WhereCompiler,
target: unknown,
): ExprNode {
return typeof target === 'string'
? definition.attributes.has(target)
? ast.ident(alias, definition.attributes.get(target)?.field ?? target)
: (this.#jsonPathOrder(definition, alias, target) ?? ast.ident(...target.split('.')))
: ctx.toExpr(target);
}
/**
* `ORDER BY` items of the `order` option.
*
* @param definition - Model definition.
* @param alias - Table alias.
* @param ctx - Where compiler of the model.
* @param order - `order` option.
* @returns Order nodes.
* @throws Error for an order that is neither a list nor an expression.
*/
orderBy(
definition: ModelDefinition,
alias: string,
ctx: WhereCompiler,
order: Order | readonly OrderItem[] | undefined | null,
): readonly OrderNode[] {
return order === undefined || order === null
? []
: Array.isArray(order)
? order.map((item: unknown) => this.orderItem(definition, alias, ctx, item))
: order instanceof SqlExpression
? [{ expr: ctx.toExpr(order) }]
: invalidOrder();
}
/**
* Expressions of a `group` or `distinctOn` option (one item or a list).
*
* @param definition - Model definition.
* @param alias - Table alias.
* @param ctx - Where compiler of the model.
* @param items - Attribute names or expressions.
* @returns Expressions.
*/
list(
definition: ModelDefinition,
alias: string,
ctx: WhereCompiler,
items: unknown,
): readonly ExprNode[] {
return items === undefined
? []
: (Array.isArray(items) ? items : [items]).map((item: unknown) =>
this.target(definition, alias, ctx, item),
);
}
/**
* Expressions of the `distinctOn` option.
*
* @param definition - Model definition.
* @param alias - Table alias.
* @param ctx - Where compiler of the model.
* @param items - Attribute names or expressions.
* @returns Expressions.
*/
distinctOn(
definition: ModelDefinition,
alias: string,
ctx: WhereCompiler,
items: readonly unknown[] | undefined,
): readonly ExprNode[] {
return (items ?? []).map((item) => this.target(definition, alias, ctx, item));
}
#specColumns(
definition: ModelDefinition,
alias: string,
ctx: WhereCompiler,
spec: AttributeSpec,
): readonly ExprNode[] {
const all = this.#columns.stored(definition).map((attribute) => attribute.fieldName);
const excluded = new Set(spec.exclude ?? []);
return [
...all
.filter((name) => !excluded.has(name))
.map((name) => SelectTargets.#columnFor(definition, alias, name)),
...(spec.include ?? []).map((item) => this.#attributeItem(definition, alias, ctx, item)),
];
}
#attributeItem(
definition: ModelDefinition,
alias: string,
ctx: WhereCompiler,
item: string | readonly [unknown, string],
): ExprNode {
if (typeof item === 'string') {
return SelectTargets.#columnFor(definition, alias, item);
}
const [source, name] = item;
const expr =
typeof source === 'string'
? ast.ident(alias, definition.attributes.get(source)?.field ?? source)
: ctx.toExpr(source);
return { kind: 'alias', expr, alias: name };
}
/** `'jsonAttr.a.b'` orders by the jsonb value at that path (numbers sort numerically). */
#jsonPathOrder(definition: ModelDefinition, alias: string, target: string): ExprNode | undefined {
const [name = '', ...path] = target.split('.');
const attribute = definition.attributes.get(name);
return attribute === undefined ||
path.length === 0 ||
(attribute.type.key !== 'JSON' && attribute.type.key !== 'JSONB')
? undefined
: ast.binary(
'#>',
attribute.type.key === 'JSON'
? { kind: 'cast', expr: ast.ident(alias, attribute.field), type: 'JSONB' }
: ast.ident(alias, attribute.field),
ast.param(path),
);
}
static #columnFor(definition: ModelDefinition, alias: string, name: string): ExprNode {
const attribute = definition.attributes.get(name);
const column = ast.ident(alias, attribute?.field ?? name);
return attribute === undefined || attribute.field === name
? column
: { kind: 'alias', expr: column, alias: name };
}
}
|