@sequelize/core
Version:
Sequelize is a promise-based Node.js ORM tool for Postgres, MySQL, MariaDB, SQLite, Microsoft SQL Server, Amazon Redshift, Snowflake’s Data Cloud, Db2, and IBM i. It features solid transaction support, relations, eager and lazy loading, read replication a
64 lines (63 loc) • 1.76 kB
TypeScript
import type { Expression } from '../sequelize.js';
import { BaseSqlExpression, SQL_IDENTIFIER } from './base-sql-expression.js';
/**
* Do not use me directly. Use {@link sql.jsonPath}.
*/
export declare class JsonPath extends BaseSqlExpression {
readonly expression: Expression;
readonly path: ReadonlyArray<string | number>;
protected readonly [SQL_IDENTIFIER]: 'jsonPath';
constructor(expression: Expression, path: ReadonlyArray<string | number>);
}
/**
* Use this to access nested properties in a JSON column.
* You can also use the dot notation with {@link sql.attribute}, but this works with any values, not just attributes.
*
* @param expression The expression to access the property on.
* @param path The path to the property. If a number is used, it will be treated as an array index, otherwise as a key.
*
* @example
* ```ts
* sql`${jsonPath('data', ['name'])} = '"John"'`
* ```
*
* will produce
*
* ```sql
* -- postgres
* "data"->'name' = '"John"'
* -- sqlite, mysql, mariadb
* JSON_EXTRACT("data", '$.name') = '"John"'
* ```
*
* @example
* ```ts
* // notice here that 0 is a number, not a string. It will be treated as an array index.
* sql`${jsonPath('array', [0])}`
* ```
*
* will produce
*
* ```sql
* -- postgres
* "array"->0
* -- sqlite, mysql, mariadb
* JSON_EXTRACT(`array`, '$[0]')
* ```
*
* @example
* ```ts
* // notice here that 0 is a string, not a number. It will be treated as an object key.
* sql`${jsonPath('object', ['0'])}`
* ```
*
* will produce
*
* ```sql
* -- postgres
* "object"->'0'
* -- sqlite, mysql, mariadb
* JSON_EXTRACT(`object`, '$.0')
* ```
*/
export declare function jsonPath(expression: Expression, path: ReadonlyArray<string | number>): JsonPath;