conditions.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import { type AnyColumn, Column, type GetColumnData } from "../../column.js";
  2. import { Placeholder, SQL, type SQLChunk, type SQLWrapper } from "../sql.js";
  3. export declare function bindIfParam(value: unknown, column: SQLWrapper): SQLChunk;
  4. export interface BinaryOperator {
  5. <TColumn extends Column>(left: TColumn, right: GetColumnData<TColumn, 'raw'> | SQLWrapper): SQL;
  6. <T>(left: SQL.Aliased<T>, right: T | SQLWrapper): SQL;
  7. <T extends SQLWrapper>(left: Exclude<T, SQL.Aliased | Column>, right: unknown): SQL;
  8. }
  9. /**
  10. * Test that two values are equal.
  11. *
  12. * Remember that the SQL standard dictates that
  13. * two NULL values are not equal, so if you want to test
  14. * whether a value is null, you may want to use
  15. * `isNull` instead.
  16. *
  17. * ## Examples
  18. *
  19. * ```ts
  20. * // Select cars made by Ford
  21. * db.select().from(cars)
  22. * .where(eq(cars.make, 'Ford'))
  23. * ```
  24. *
  25. * @see isNull for a way to test equality to NULL.
  26. */
  27. export declare const eq: BinaryOperator;
  28. /**
  29. * Test that two values are not equal.
  30. *
  31. * Remember that the SQL standard dictates that
  32. * two NULL values are not equal, so if you want to test
  33. * whether a value is not null, you may want to use
  34. * `isNotNull` instead.
  35. *
  36. * ## Examples
  37. *
  38. * ```ts
  39. * // Select cars not made by Ford
  40. * db.select().from(cars)
  41. * .where(ne(cars.make, 'Ford'))
  42. * ```
  43. *
  44. * @see isNotNull for a way to test whether a value is not null.
  45. */
  46. export declare const ne: BinaryOperator;
  47. /**
  48. * Combine a list of conditions with the `and` operator. Conditions
  49. * that are equal `undefined` are automatically ignored.
  50. *
  51. * ## Examples
  52. *
  53. * ```ts
  54. * db.select().from(cars)
  55. * .where(
  56. * and(
  57. * eq(cars.make, 'Volvo'),
  58. * eq(cars.year, 1950),
  59. * )
  60. * )
  61. * ```
  62. */
  63. export declare function and(...conditions: (SQLWrapper | undefined)[]): SQL | undefined;
  64. /**
  65. * Combine a list of conditions with the `or` operator. Conditions
  66. * that are equal `undefined` are automatically ignored.
  67. *
  68. * ## Examples
  69. *
  70. * ```ts
  71. * db.select().from(cars)
  72. * .where(
  73. * or(
  74. * eq(cars.make, 'GM'),
  75. * eq(cars.make, 'Ford'),
  76. * )
  77. * )
  78. * ```
  79. */
  80. export declare function or(...conditions: (SQLWrapper | undefined)[]): SQL | undefined;
  81. /**
  82. * Negate the meaning of an expression using the `not` keyword.
  83. *
  84. * ## Examples
  85. *
  86. * ```ts
  87. * // Select cars _not_ made by GM or Ford.
  88. * db.select().from(cars)
  89. * .where(not(inArray(cars.make, ['GM', 'Ford'])))
  90. * ```
  91. */
  92. export declare function not(condition: SQLWrapper): SQL;
  93. /**
  94. * Test that the first expression passed is greater than
  95. * the second expression.
  96. *
  97. * ## Examples
  98. *
  99. * ```ts
  100. * // Select cars made after 2000.
  101. * db.select().from(cars)
  102. * .where(gt(cars.year, 2000))
  103. * ```
  104. *
  105. * @see gte for greater-than-or-equal
  106. */
  107. export declare const gt: BinaryOperator;
  108. /**
  109. * Test that the first expression passed is greater than
  110. * or equal to the second expression. Use `gt` to
  111. * test whether an expression is strictly greater
  112. * than another.
  113. *
  114. * ## Examples
  115. *
  116. * ```ts
  117. * // Select cars made on or after 2000.
  118. * db.select().from(cars)
  119. * .where(gte(cars.year, 2000))
  120. * ```
  121. *
  122. * @see gt for a strictly greater-than condition
  123. */
  124. export declare const gte: BinaryOperator;
  125. /**
  126. * Test that the first expression passed is less than
  127. * the second expression.
  128. *
  129. * ## Examples
  130. *
  131. * ```ts
  132. * // Select cars made before 2000.
  133. * db.select().from(cars)
  134. * .where(lt(cars.year, 2000))
  135. * ```
  136. *
  137. * @see lte for less-than-or-equal
  138. */
  139. export declare const lt: BinaryOperator;
  140. /**
  141. * Test that the first expression passed is less than
  142. * or equal to the second expression.
  143. *
  144. * ## Examples
  145. *
  146. * ```ts
  147. * // Select cars made before 2000.
  148. * db.select().from(cars)
  149. * .where(lte(cars.year, 2000))
  150. * ```
  151. *
  152. * @see lt for a strictly less-than condition
  153. */
  154. export declare const lte: BinaryOperator;
  155. /**
  156. * Test whether the first parameter, a column or expression,
  157. * has a value from a list passed as the second argument.
  158. *
  159. * ## Examples
  160. *
  161. * ```ts
  162. * // Select cars made by Ford or GM.
  163. * db.select().from(cars)
  164. * .where(inArray(cars.make, ['Ford', 'GM']))
  165. * ```
  166. *
  167. * @see notInArray for the inverse of this test
  168. */
  169. export declare function inArray<T>(column: SQL.Aliased<T>, values: (T | Placeholder)[] | SQLWrapper): SQL;
  170. export declare function inArray<TColumn extends Column>(column: TColumn, values: ReadonlyArray<GetColumnData<TColumn, 'raw'> | Placeholder> | SQLWrapper): SQL;
  171. export declare function inArray<T extends SQLWrapper>(column: Exclude<T, SQL.Aliased | Column>, values: ReadonlyArray<unknown | Placeholder> | SQLWrapper): SQL;
  172. /**
  173. * Test whether the first parameter, a column or expression,
  174. * has a value that is not present in a list passed as the
  175. * second argument.
  176. *
  177. * ## Examples
  178. *
  179. * ```ts
  180. * // Select cars made by any company except Ford or GM.
  181. * db.select().from(cars)
  182. * .where(notInArray(cars.make, ['Ford', 'GM']))
  183. * ```
  184. *
  185. * @see inArray for the inverse of this test
  186. */
  187. export declare function notInArray<T>(column: SQL.Aliased<T>, values: (T | Placeholder)[] | SQLWrapper): SQL;
  188. export declare function notInArray<TColumn extends Column>(column: TColumn, values: (GetColumnData<TColumn, 'raw'> | Placeholder)[] | SQLWrapper): SQL;
  189. export declare function notInArray<T extends SQLWrapper>(column: Exclude<T, SQL.Aliased | Column>, values: (unknown | Placeholder)[] | SQLWrapper): SQL;
  190. /**
  191. * Test whether an expression is NULL. By the SQL standard,
  192. * NULL is neither equal nor not equal to itself, so
  193. * it's recommended to use `isNull` and `notIsNull` for
  194. * comparisons to NULL.
  195. *
  196. * ## Examples
  197. *
  198. * ```ts
  199. * // Select cars that have no discontinuedAt date.
  200. * db.select().from(cars)
  201. * .where(isNull(cars.discontinuedAt))
  202. * ```
  203. *
  204. * @see isNotNull for the inverse of this test
  205. */
  206. export declare function isNull(value: SQLWrapper): SQL;
  207. /**
  208. * Test whether an expression is not NULL. By the SQL standard,
  209. * NULL is neither equal nor not equal to itself, so
  210. * it's recommended to use `isNull` and `notIsNull` for
  211. * comparisons to NULL.
  212. *
  213. * ## Examples
  214. *
  215. * ```ts
  216. * // Select cars that have been discontinued.
  217. * db.select().from(cars)
  218. * .where(isNotNull(cars.discontinuedAt))
  219. * ```
  220. *
  221. * @see isNull for the inverse of this test
  222. */
  223. export declare function isNotNull(value: SQLWrapper): SQL;
  224. /**
  225. * Test whether a subquery evaluates to have any rows.
  226. *
  227. * ## Examples
  228. *
  229. * ```ts
  230. * // Users whose `homeCity` column has a match in a cities
  231. * // table.
  232. * db
  233. * .select()
  234. * .from(users)
  235. * .where(
  236. * exists(db.select()
  237. * .from(cities)
  238. * .where(eq(users.homeCity, cities.id))),
  239. * );
  240. * ```
  241. *
  242. * @see notExists for the inverse of this test
  243. */
  244. export declare function exists(subquery: SQLWrapper): SQL;
  245. /**
  246. * Test whether a subquery doesn't include any result
  247. * rows.
  248. *
  249. * ## Examples
  250. *
  251. * ```ts
  252. * // Users whose `homeCity` column doesn't match
  253. * // a row in the cities table.
  254. * db
  255. * .select()
  256. * .from(users)
  257. * .where(
  258. * notExists(db.select()
  259. * .from(cities)
  260. * .where(eq(users.homeCity, cities.id))),
  261. * );
  262. * ```
  263. *
  264. * @see exists for the inverse of this test
  265. */
  266. export declare function notExists(subquery: SQLWrapper): SQL;
  267. /**
  268. * Test whether an expression is between two values. This
  269. * is an easier way to express range tests, which would be
  270. * expressed mathematically as `x <= a <= y` but in SQL
  271. * would have to be like `a >= x AND a <= y`.
  272. *
  273. * Between is inclusive of the endpoints: if `column`
  274. * is equal to `min` or `max`, it will be TRUE.
  275. *
  276. * ## Examples
  277. *
  278. * ```ts
  279. * // Select cars made between 1990 and 2000
  280. * db.select().from(cars)
  281. * .where(between(cars.year, 1990, 2000))
  282. * ```
  283. *
  284. * @see notBetween for the inverse of this test
  285. */
  286. export declare function between<T>(column: SQL.Aliased, min: T | SQLWrapper, max: T | SQLWrapper): SQL;
  287. export declare function between<TColumn extends AnyColumn>(column: TColumn, min: GetColumnData<TColumn, 'raw'> | SQLWrapper, max: GetColumnData<TColumn, 'raw'> | SQLWrapper): SQL;
  288. export declare function between<T extends SQLWrapper>(column: Exclude<T, SQL.Aliased | Column>, min: unknown, max: unknown): SQL;
  289. /**
  290. * Test whether an expression is not between two values.
  291. *
  292. * This, like `between`, includes its endpoints, so if
  293. * the `column` is equal to `min` or `max`, in this case
  294. * it will evaluate to FALSE.
  295. *
  296. * ## Examples
  297. *
  298. * ```ts
  299. * // Exclude cars made in the 1970s
  300. * db.select().from(cars)
  301. * .where(notBetween(cars.year, 1970, 1979))
  302. * ```
  303. *
  304. * @see between for the inverse of this test
  305. */
  306. export declare function notBetween<T>(column: SQL.Aliased, min: T | SQLWrapper, max: T | SQLWrapper): SQL;
  307. export declare function notBetween<TColumn extends AnyColumn>(column: TColumn, min: GetColumnData<TColumn, 'raw'> | SQLWrapper, max: GetColumnData<TColumn, 'raw'> | SQLWrapper): SQL;
  308. export declare function notBetween<T extends SQLWrapper>(column: Exclude<T, SQL.Aliased | Column>, min: unknown, max: unknown): SQL;
  309. /**
  310. * Compare a column to a pattern, which can include `%` and `_`
  311. * characters to match multiple variations. Including `%`
  312. * in the pattern matches zero or more characters, and including
  313. * `_` will match a single character.
  314. *
  315. * ## Examples
  316. *
  317. * ```ts
  318. * // Select all cars with 'Turbo' in their names.
  319. * db.select().from(cars)
  320. * .where(like(cars.name, '%Turbo%'))
  321. * ```
  322. *
  323. * @see ilike for a case-insensitive version of this condition
  324. */
  325. export declare function like(column: Column | SQL.Aliased | SQL, value: string | SQLWrapper): SQL;
  326. /**
  327. * The inverse of like - this tests that a given column
  328. * does not match a pattern, which can include `%` and `_`
  329. * characters to match multiple variations. Including `%`
  330. * in the pattern matches zero or more characters, and including
  331. * `_` will match a single character.
  332. *
  333. * ## Examples
  334. *
  335. * ```ts
  336. * // Select all cars that don't have "ROver" in their name.
  337. * db.select().from(cars)
  338. * .where(notLike(cars.name, '%Rover%'))
  339. * ```
  340. *
  341. * @see like for the inverse condition
  342. * @see notIlike for a case-insensitive version of this condition
  343. */
  344. export declare function notLike(column: Column | SQL.Aliased | SQL, value: string | SQLWrapper): SQL;
  345. /**
  346. * Case-insensitively compare a column to a pattern,
  347. * which can include `%` and `_`
  348. * characters to match multiple variations. Including `%`
  349. * in the pattern matches zero or more characters, and including
  350. * `_` will match a single character.
  351. *
  352. * Unlike like, this performs a case-insensitive comparison.
  353. *
  354. * ## Examples
  355. *
  356. * ```ts
  357. * // Select all cars with 'Turbo' in their names.
  358. * db.select().from(cars)
  359. * .where(ilike(cars.name, '%Turbo%'))
  360. * ```
  361. *
  362. * @see like for a case-sensitive version of this condition
  363. */
  364. export declare function ilike(column: Column | SQL.Aliased | SQL, value: string | SQLWrapper): SQL;
  365. /**
  366. * The inverse of ilike - this case-insensitively tests that a given column
  367. * does not match a pattern, which can include `%` and `_`
  368. * characters to match multiple variations. Including `%`
  369. * in the pattern matches zero or more characters, and including
  370. * `_` will match a single character.
  371. *
  372. * ## Examples
  373. *
  374. * ```ts
  375. * // Select all cars that don't have "Rover" in their name.
  376. * db.select().from(cars)
  377. * .where(notLike(cars.name, '%Rover%'))
  378. * ```
  379. *
  380. * @see ilike for the inverse condition
  381. * @see notLike for a case-sensitive version of this condition
  382. */
  383. export declare function notIlike(column: Column | SQL.Aliased | SQL, value: string | SQLWrapper): SQL;
  384. /**
  385. * Test that a column or expression contains all elements of
  386. * the list passed as the second argument.
  387. *
  388. * ## Throws
  389. *
  390. * The argument passed in the second array can't be empty:
  391. * if an empty is provided, this method will throw.
  392. *
  393. * ## Examples
  394. *
  395. * ```ts
  396. * // Select posts where its tags contain "Typescript" and "ORM".
  397. * db.select().from(posts)
  398. * .where(arrayContains(posts.tags, ['Typescript', 'ORM']))
  399. * ```
  400. *
  401. * @see arrayContained to find if an array contains all elements of a column or expression
  402. * @see arrayOverlaps to find if a column or expression contains any elements of an array
  403. */
  404. export declare function arrayContains<T>(column: SQL.Aliased<T>, values: (T | Placeholder) | SQLWrapper): SQL;
  405. export declare function arrayContains<TColumn extends Column>(column: TColumn, values: (GetColumnData<TColumn, 'raw'> | Placeholder) | SQLWrapper): SQL;
  406. export declare function arrayContains<T extends SQLWrapper>(column: Exclude<T, SQL.Aliased | Column>, values: (unknown | Placeholder)[] | SQLWrapper): SQL;
  407. /**
  408. * Test that the list passed as the second argument contains
  409. * all elements of a column or expression.
  410. *
  411. * ## Throws
  412. *
  413. * The argument passed in the second array can't be empty:
  414. * if an empty is provided, this method will throw.
  415. *
  416. * ## Examples
  417. *
  418. * ```ts
  419. * // Select posts where its tags contain "Typescript", "ORM" or both,
  420. * // but filtering posts that have additional tags.
  421. * db.select().from(posts)
  422. * .where(arrayContained(posts.tags, ['Typescript', 'ORM']))
  423. * ```
  424. *
  425. * @see arrayContains to find if a column or expression contains all elements of an array
  426. * @see arrayOverlaps to find if a column or expression contains any elements of an array
  427. */
  428. export declare function arrayContained<T>(column: SQL.Aliased<T>, values: (T | Placeholder) | SQLWrapper): SQL;
  429. export declare function arrayContained<TColumn extends Column>(column: TColumn, values: (GetColumnData<TColumn, 'raw'> | Placeholder) | SQLWrapper): SQL;
  430. export declare function arrayContained<T extends SQLWrapper>(column: Exclude<T, SQL.Aliased | Column>, values: (unknown | Placeholder)[] | SQLWrapper): SQL;
  431. /**
  432. * Test that a column or expression contains any elements of
  433. * the list passed as the second argument.
  434. *
  435. * ## Throws
  436. *
  437. * The argument passed in the second array can't be empty:
  438. * if an empty is provided, this method will throw.
  439. *
  440. * ## Examples
  441. *
  442. * ```ts
  443. * // Select posts where its tags contain "Typescript", "ORM" or both.
  444. * db.select().from(posts)
  445. * .where(arrayOverlaps(posts.tags, ['Typescript', 'ORM']))
  446. * ```
  447. *
  448. * @see arrayContains to find if a column or expression contains all elements of an array
  449. * @see arrayContained to find if an array contains all elements of a column or expression
  450. */
  451. export declare function arrayOverlaps<T>(column: SQL.Aliased<T>, values: (T | Placeholder) | SQLWrapper): SQL;
  452. export declare function arrayOverlaps<TColumn extends Column>(column: TColumn, values: (GetColumnData<TColumn, 'raw'> | Placeholder) | SQLWrapper): SQL;
  453. export declare function arrayOverlaps<T extends SQLWrapper>(column: Exclude<T, SQL.Aliased | Column>, values: (unknown | Placeholder)[] | SQLWrapper): SQL;