rado 1.2.0 → 1.3.0-preview.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/README.md +163 -257
  2. package/dist/compat.d.ts +3 -0
  3. package/dist/core/Builder.d.ts +16 -9
  4. package/dist/core/Builder.js +52 -4
  5. package/dist/core/Column.d.ts +38 -25
  6. package/dist/core/Column.js +56 -27
  7. package/dist/core/Constraint.d.ts +2 -1
  8. package/dist/core/Constraint.js +2 -1
  9. package/dist/core/Database.d.ts +5 -4
  10. package/dist/core/Database.js +14 -13
  11. package/dist/core/Emitter.d.ts +2 -2
  12. package/dist/core/Index.d.ts +1 -1
  13. package/dist/core/Internal.d.ts +19 -2
  14. package/dist/core/Internal.js +29 -11
  15. package/dist/core/Param.js +2 -0
  16. package/dist/core/Queries.d.ts +1 -0
  17. package/dist/core/Queries.js +11 -1
  18. package/dist/core/Resolver.js +2 -2
  19. package/dist/core/Schema.d.ts +1 -3
  20. package/dist/core/Selection.d.ts +9 -4
  21. package/dist/core/Selection.js +94 -5
  22. package/dist/core/Sql.d.ts +4 -3
  23. package/dist/core/Sql.js +8 -2
  24. package/dist/core/Table.d.ts +18 -11
  25. package/dist/core/Table.js +45 -25
  26. package/dist/core/View.d.ts +34 -0
  27. package/dist/core/View.js +153 -0
  28. package/dist/core/Virtual.d.ts +7 -2
  29. package/dist/core/Virtual.js +14 -10
  30. package/dist/core/expr/Conditions.d.ts +7 -7
  31. package/dist/core/expr/Include.d.ts +2 -2
  32. package/dist/core/expr/Include.js +1 -1
  33. package/dist/core/expr/Input.d.ts +1 -1
  34. package/dist/core/expr/Input.js +5 -1
  35. package/dist/core/query/CTE.d.ts +3 -5
  36. package/dist/core/query/CTE.js +1 -13
  37. package/dist/core/query/Delete.d.ts +10 -10
  38. package/dist/core/query/Delete.js +2 -2
  39. package/dist/core/query/Insert.d.ts +6 -5
  40. package/dist/core/query/Insert.js +22 -9
  41. package/dist/core/query/Query.d.ts +26 -11
  42. package/dist/core/query/Select.d.ts +41 -8
  43. package/dist/core/query/Select.js +235 -25
  44. package/dist/core/query/Shared.d.ts +2 -1
  45. package/dist/core/query/Shared.js +8 -5
  46. package/dist/core/query/Update.d.ts +9 -9
  47. package/dist/core/query/Update.js +24 -12
  48. package/dist/driver/better-sqlite3.js +4 -0
  49. package/dist/driver/bun-sqlite.js +3 -0
  50. package/dist/driver/d1.js +3 -0
  51. package/dist/driver/libsql.js +4 -0
  52. package/dist/driver/mysql2.js +5 -0
  53. package/dist/driver/pg.d.ts +17 -1
  54. package/dist/driver/pg.js +10 -1
  55. package/dist/driver/pglite.js +21 -6
  56. package/dist/driver/sql.js.js +4 -0
  57. package/dist/index.d.ts +1 -0
  58. package/dist/index.js +1 -0
  59. package/dist/mysql/columns.d.ts +30 -30
  60. package/dist/mysql/columns.js +25 -24
  61. package/dist/mysql/dialect.js +6 -2
  62. package/dist/mysql/diff.js +1 -1
  63. package/dist/mysql.d.ts +1 -0
  64. package/dist/mysql.js +2 -0
  65. package/dist/postgres/columns.d.ts +103 -33
  66. package/dist/postgres/columns.js +317 -35
  67. package/dist/postgres/diff.js +3 -3
  68. package/dist/postgres/enum.d.ts +17 -0
  69. package/dist/postgres/enum.js +64 -0
  70. package/dist/postgres/schema.d.ts +19 -0
  71. package/dist/postgres/schema.js +47 -0
  72. package/dist/postgres.d.ts +3 -1
  73. package/dist/postgres.js +9 -2
  74. package/dist/sqlite/columns.d.ts +29 -14
  75. package/dist/sqlite/columns.js +41 -9
  76. package/dist/sqlite/dialect.js +3 -1
  77. package/dist/sqlite/diff.js +1 -1
  78. package/dist/sqlite/functions.d.ts +1 -1
  79. package/dist/sqlite/functions.js +1 -1
  80. package/dist/sqlite.d.ts +1 -0
  81. package/dist/sqlite.js +3 -6
  82. package/dist/universal/columns.d.ts +8 -8
  83. package/dist/universal/columns.js +43 -26
  84. package/dist/universal/functions.d.ts +1 -1
  85. package/dist/universal/functions.js +1 -1
  86. package/package.json +54 -37
@@ -1,8 +1,17 @@
1
1
  // src/core/Virtual.ts
2
+ import { Field } from "./expr/Field.js";
2
3
  import { getSql, hasSql, internalTarget } from "./Internal.js";
3
4
  import { sql } from "./Sql.js";
4
- import { Field } from "./expr/Field.js";
5
- function virtual(alias, source) {
5
+ function virtualFields(alias, source) {
6
+ return Object.fromEntries(
7
+ Object.entries(source).map(([key, value]) => {
8
+ if (value && typeof value === "object" && !hasSql(value))
9
+ return [key, virtualFields(alias, value)];
10
+ return [key, new Field(alias, key, getSql(value))];
11
+ })
12
+ );
13
+ }
14
+ function virtualTarget(alias, source) {
6
15
  const target = {
7
16
  [internalTarget]: sql.identifier(alias)
8
17
  };
@@ -14,16 +23,11 @@ function virtual(alias, source) {
14
23
  if (!name) throw new Error("Cannot alias a virtual field without a name");
15
24
  return Object.assign(new Field(alias, name, expr), target);
16
25
  }
17
- const aliased = Object.fromEntries(
18
- Object.entries(source).map(([key, value]) => {
19
- return [key, new Field(alias, key, getSql(value))];
20
- })
21
- );
22
26
  return {
23
- [internalTarget]: sql.identifier(alias),
24
- ...aliased
27
+ ...target,
28
+ ...virtualFields(alias, source)
25
29
  };
26
30
  }
27
31
  export {
28
- virtual
32
+ virtualTarget
29
33
  };
@@ -5,17 +5,17 @@ import { type Sql } from '../Sql.js';
5
5
  import { type Input } from './Input.js';
6
6
  export declare const eq: <T>(left: Input<T>, right: Input<T>) => Sql<boolean>;
7
7
  export declare const ne: <T>(left: Input<T>, right: Input<T>) => Sql<boolean>;
8
- export declare const gt: <T>(left: Input<T>, right: Input<T>) => Sql<boolean>;
9
- export declare const gte: <T>(left: Input<T>, right: Input<T>) => Sql<boolean>;
10
- export declare const lt: <T>(left: Input<T>, right: Input<T>) => Sql<boolean>;
11
- export declare const lte: <T>(left: Input<T>, right: Input<T>) => Sql<boolean>;
8
+ export declare const gt: (left: Input, right: Input) => Sql<boolean>;
9
+ export declare const gte: (left: Input, right: Input) => Sql<boolean>;
10
+ export declare const lt: (left: Input, right: Input) => Sql<boolean>;
11
+ export declare const lte: (left: Input, right: Input) => Sql<boolean>;
12
12
  export declare const like: (left: Input<string | null>, right: Input<string>) => Sql<boolean>;
13
13
  export declare const notLike: (left: Input<string | null>, right: Input<string>) => Sql<boolean>;
14
14
  export declare const ilike: (left: Input<string | null>, right: Input<string>) => Sql<boolean>;
15
15
  export declare const notILike: (left: Input<string | null>, right: Input<string>) => Sql<boolean>;
16
- export declare const arrayContains: <T>(left: Input<Array<T>>, right: Input<T>) => Sql<boolean>;
17
- export declare const arrayContained: <T>(left: Input<Array<T>>, right: Input<Array<T>>) => Sql<boolean>;
18
- export declare const arrayOverlaps: <T>(left: Input<Array<T>>, right: Input<Array<T>>) => Sql<boolean>;
16
+ export declare const arrayContains: <T>(left: Input<Array<T> | null>, right: Input<T | Array<T> | null>) => Sql<boolean>;
17
+ export declare const arrayContained: <T>(left: Input<Array<T> | null>, right: Input<Array<T> | null>) => Sql<boolean>;
18
+ export declare const arrayOverlaps: <T>(left: Input<Array<T> | null>, right: Input<Array<T> | null>) => Sql<boolean>;
19
19
  export declare function and(...conditions: Array<undefined | Input<boolean>>): Sql<boolean>;
20
20
  export declare function or(...conditions: Array<undefined | Input<boolean>>): Sql<boolean>;
21
21
  export declare function not(condition: Input<boolean>): Sql<boolean>;
@@ -1,10 +1,10 @@
1
1
  import { type HasSql, internalData, internalSql } from '../Internal.js';
2
2
  import type { QueryMeta } from '../MetaData.js';
3
3
  import type { QueryData } from '../Queries.js';
4
- import type { RowOfRecord } from '../Selection.js';
5
- import { type Sql } from '../Sql.js';
6
4
  import type { SelectQuery } from '../query/Query.js';
7
5
  import { type Select, type SelectBase } from '../query/Select.js';
6
+ import type { RowOfRecord } from '../Selection.js';
7
+ import { type Sql } from '../Sql.js';
8
8
  export type IncludeQuery = SelectQuery & {
9
9
  first: boolean;
10
10
  };
@@ -1,10 +1,10 @@
1
1
  // src/core/expr/Include.ts
2
2
  import { getData, internalData, internalSql } from "../Internal.js";
3
- import { sql } from "../Sql.js";
4
3
  import {
5
4
  querySelection,
6
5
  selectQuery
7
6
  } from "../query/Select.js";
7
+ import { sql } from "../Sql.js";
8
8
  import { jsonAggregateArray, jsonArray } from "./Json.js";
9
9
  var Include = class {
10
10
  [internalData];
@@ -3,4 +3,4 @@ import { type HasSql } from '../Internal.js';
3
3
  import { type Sql } from '../Sql.js';
4
4
  export type Input<T = unknown> = HasSql<T> | T;
5
5
  export declare function input<T>(value: Input<T>, maybeField?: Input<T>): Sql<T>;
6
- export declare function mapToColumn<T>({ mapToDriverValue }: ColumnData, expr: Input<T>): Sql<T>;
6
+ export declare function mapToColumn<T>({ mapToDriverValue, type }: ColumnData, expr: Input<T>): Sql<T>;
@@ -16,9 +16,13 @@ function input(value, maybeField) {
16
16
  const fieldSource = maybeField && typeof maybeField === "object" && hasField(maybeField) && getField(maybeField).source;
17
17
  return fieldSource && "mapToDriverValue" in fieldSource ? mapToColumn(fieldSource, value) : sql.value(value);
18
18
  }
19
- function mapToColumn({ mapToDriverValue }, expr) {
19
+ function mapToColumn({ mapToDriverValue, type }, expr) {
20
20
  const isObject = expr && typeof expr === "object";
21
21
  if (isObject && hasSql(expr)) return getSql(expr);
22
+ if (Array.isArray(expr) && type.kind === "array") {
23
+ if (!mapToDriverValue) return input(expr);
24
+ return input(mapToDriverValue(expr));
25
+ }
22
26
  return input(
23
27
  expr !== null && mapToDriverValue ? mapToDriverValue(expr) : expr
24
28
  );
@@ -1,8 +1,6 @@
1
- import { type HasQuery, type HasTarget } from '../Internal.js';
2
- import type { SelectionInput } from '../Selection.js';
1
+ import { type HasQuery } from '../Internal.js';
3
2
  import { type Sql } from '../Sql.js';
3
+ import type { VirtualTarget } from '../Virtual.js';
4
4
  import type { QueryBase } from './Query.js';
5
- import type { UnionBase } from './Select.js';
6
- export type CTE<Input = unknown> = Input & HasTarget & HasQuery;
7
- export declare function createCTE<Input extends SelectionInput>(cteName: string, query: UnionBase<Input>): CTE<Input>;
5
+ export type CTE<Input = unknown> = VirtualTarget<Input> & HasQuery;
8
6
  export declare function formatCTE(query: QueryBase): Sql | undefined;
@@ -1,17 +1,6 @@
1
1
  // src/core/query/CTE.ts
2
- import {
3
- getQuery,
4
- getSelection,
5
- getTarget,
6
- internalQuery
7
- } from "../Internal.js";
2
+ import { getQuery, getTarget } from "../Internal.js";
8
3
  import { sql } from "../Sql.js";
9
- function createCTE(cteName, query) {
10
- const fields = getSelection(query).makeVirtual(cteName);
11
- return Object.assign(fields, {
12
- [internalQuery]: getQuery(query).nameSelf(cteName)
13
- });
14
- }
15
4
  function formatCTE(query) {
16
5
  const isRecursive = query.withRecursive;
17
6
  const definitions = isRecursive ? query.withRecursive : query.with;
@@ -28,6 +17,5 @@ function formatCTE(query) {
28
17
  });
29
18
  }
30
19
  export {
31
- createCTE,
32
20
  formatCTE
33
21
  };
@@ -1,23 +1,23 @@
1
- import { type HasQuery, type HasSql, internalData, internalQuery, internalSelection } from '../Internal.js';
1
+ import type { Input as UserInput } from '../expr/Input.js';
2
+ import { type HasSql, internalData, internalQuery, internalSelection } from '../Internal.js';
2
3
  import type { IsPostgres, IsSqlite, QueryMeta } from '../MetaData.js';
3
4
  import { type QueryData, SingleQuery } from '../Queries.js';
4
5
  import { type Selection, type SelectionInput, type SelectionRow } from '../Selection.js';
5
6
  import { type Sql } from '../Sql.js';
6
- import type { TableDefinition, TableRow } from '../Table.js';
7
- import type { Input } from '../expr/Input.js';
7
+ import type { TableDefinition, TableFields } from '../Table.js';
8
8
  import type { DeleteQuery } from './Query.js';
9
- export declare class Delete<Result, Meta extends QueryMeta = QueryMeta> extends SingleQuery<Result, Meta> implements HasQuery<Result> {
9
+ export declare class Delete<Input, Meta extends QueryMeta = QueryMeta> extends SingleQuery<Array<SelectionRow<Input>>, Meta> {
10
10
  readonly [internalData]: QueryData<Meta> & DeleteQuery;
11
11
  readonly [internalSelection]?: Selection;
12
12
  constructor(data: QueryData<Meta> & DeleteQuery);
13
- get [internalQuery](): Sql<Result>;
14
- limit(limit: Input<number>): Delete<Result, Meta>;
15
- offset(offset: Input<number>): Delete<Result, Meta>;
16
- orderBy(...orderBy: Array<HasSql>): Delete<Result, Meta>;
13
+ get [internalQuery](): Sql<Array<SelectionRow<Input>>>;
14
+ limit(limit: UserInput<number>): Delete<Input, Meta>;
15
+ offset(offset: UserInput<number>): Delete<Input, Meta>;
16
+ orderBy(...orderBy: Array<HasSql>): Delete<Input, Meta>;
17
17
  }
18
18
  export declare class DeleteFrom<Definition extends TableDefinition, Meta extends QueryMeta> extends Delete<void, Meta> {
19
19
  where(...where: Array<HasSql<boolean> | undefined>): DeleteFrom<Definition, Meta>;
20
- returning(this: DeleteFrom<Definition, IsPostgres | IsSqlite>): Delete<Array<TableRow<Definition>>, Meta>;
21
- returning<Input extends SelectionInput>(this: DeleteFrom<Definition, IsPostgres | IsSqlite>, returning: Input): Delete<Array<SelectionRow<Input>>, Meta>;
20
+ returning(this: DeleteFrom<Definition, IsPostgres | IsSqlite>): Delete<TableFields<Definition>, Meta>;
21
+ returning<Input extends SelectionInput>(this: DeleteFrom<Definition, IsPostgres | IsSqlite>, returning: Input): Delete<Input, Meta>;
22
22
  }
23
23
  export declare function deleteQuery(query: DeleteQuery): Sql;
@@ -1,4 +1,5 @@
1
1
  // src/core/query/Delete.ts
2
+ import { and } from "../expr/Conditions.js";
2
3
  import {
3
4
  getData,
4
5
  getTable,
@@ -11,7 +12,6 @@ import {
11
12
  selection
12
13
  } from "../Selection.js";
13
14
  import { sql } from "../Sql.js";
14
- import { and } from "../expr/Conditions.js";
15
15
  import { formatCTE } from "./CTE.js";
16
16
  import { formatModifiers } from "./Shared.js";
17
17
  var Delete = class _Delete extends SingleQuery {
@@ -52,7 +52,7 @@ function deleteQuery(query) {
52
52
  return sql.query(
53
53
  formatCTE(query),
54
54
  {
55
- deleteFrom: sql.identifier(table.name),
55
+ deleteFrom: table.identifier(),
56
56
  where,
57
57
  returning: returning && selection(returning)
58
58
  },
@@ -3,17 +3,17 @@ import type { IsMysql, IsPostgres, IsSqlite, QueryMeta } from '../MetaData.js';
3
3
  import { type QueryData, SingleQuery } from '../Queries.js';
4
4
  import { type Selection, type SelectionInput, type SelectionRow } from '../Selection.js';
5
5
  import { type Sql } from '../Sql.js';
6
- import type { TableDefinition, TableInsert, TableRow } from '../Table.js';
6
+ import type { TableDefinition, TableFields, TableInsert, TableRow } from '../Table.js';
7
7
  import type { InsertQuery, OnConflict, OnConflictSet, OnConflictUpdate } from './Query.js';
8
- export declare class Insert<Result, Meta extends QueryMeta = QueryMeta> extends SingleQuery<Result, Meta> implements HasQuery<Result> {
8
+ export declare class Insert<Input, Meta extends QueryMeta = QueryMeta> extends SingleQuery<Array<SelectionRow<Input>>, Meta> implements HasQuery<Array<SelectionRow<Input>>> {
9
9
  readonly [internalData]: QueryData<Meta> & InsertQuery;
10
10
  readonly [internalSelection]?: Selection;
11
11
  constructor(data: QueryData<Meta> & InsertQuery);
12
- get [internalQuery](): Sql<Result>;
12
+ get [internalQuery](): Sql<Array<SelectionRow<Input>>>;
13
13
  }
14
14
  declare class InsertCanReturn<Definition extends TableDefinition, Meta extends QueryMeta> extends Insert<void, Meta> {
15
- returning<Meta extends IsPostgres | IsSqlite>(this: InsertCanReturn<Definition, Meta>): Insert<Array<TableRow<Definition>>, Meta>;
16
- returning<Input extends SelectionInput, Meta extends IsPostgres | IsSqlite>(this: InsertCanReturn<Definition, Meta>, returning: Input): Insert<Array<SelectionRow<Input>>, Meta>;
15
+ returning<Meta extends IsPostgres | IsSqlite>(this: InsertCanReturn<Definition, Meta>): Insert<TableFields<Definition>, Meta>;
16
+ returning<Input extends SelectionInput, Meta extends IsPostgres | IsSqlite>(this: InsertCanReturn<Definition, Meta>, returning: Input): Insert<Input, Meta>;
17
17
  }
18
18
  declare class InsertCanConflict<Definition extends TableDefinition, Meta extends QueryMeta> extends InsertCanReturn<Definition, Meta> {
19
19
  onConflictDoNothing<Meta extends IsPostgres | IsSqlite>(this: InsertCanConflict<Definition, Meta>, onConflictDoNothing?: OnConflict): InsertCanConflict<Definition, Meta>;
@@ -23,6 +23,7 @@ declare class InsertCanConflict<Definition extends TableDefinition, Meta extends
23
23
  export declare class InsertInto<Definition extends TableDefinition, Meta extends QueryMeta> {
24
24
  [internalData]: QueryData<Meta> & InsertQuery;
25
25
  constructor(data: QueryData<Meta> & InsertQuery);
26
+ overridingSystemValue(): InsertInto<Definition, Meta>;
26
27
  values(value: TableInsert<Definition>): InsertCanConflict<Definition, Meta>;
27
28
  values(values: Array<TableInsert<Definition>>): InsertCanConflict<Definition, Meta>;
28
29
  select(query: SingleQuery<Array<TableRow<Definition>>, Meta>): InsertCanConflict<Definition, Meta>;
@@ -1,4 +1,5 @@
1
1
  // src/core/query/Insert.ts
2
+ import { input, mapToColumn } from "../expr/Input.js";
2
3
  import {
3
4
  getData,
4
5
  getTable,
@@ -11,10 +12,8 @@ import {
11
12
  selection
12
13
  } from "../Selection.js";
13
14
  import { sql } from "../Sql.js";
14
- import { input, mapToColumn } from "../expr/Input.js";
15
15
  import { formatCTE } from "./CTE.js";
16
- import { selectQuery } from "./Select.js";
17
- import { formatModifiers } from "./Shared.js";
16
+ import { querySelection, selectQuery } from "./Select.js";
18
17
  var Insert = class extends SingleQuery {
19
18
  [internalData];
20
19
  constructor(data) {
@@ -58,11 +57,17 @@ var InsertCanConflict = class _InsertCanConflict extends InsertCanReturn {
58
57
  });
59
58
  }
60
59
  };
61
- var InsertInto = class {
60
+ var InsertInto = class _InsertInto {
62
61
  [internalData];
63
62
  constructor(data) {
64
63
  this[internalData] = data;
65
64
  }
65
+ overridingSystemValue() {
66
+ return new _InsertInto({
67
+ ...getData(this),
68
+ overridingSystemValue: true
69
+ });
70
+ }
66
71
  values(values) {
67
72
  return new InsertCanConflict({
68
73
  ...getData(this),
@@ -70,6 +75,15 @@ var InsertInto = class {
70
75
  });
71
76
  }
72
77
  select(query) {
78
+ const insert = getTable(getData(this).insert);
79
+ const expected = Object.keys(insert.columns);
80
+ const actual = querySelection(getData(query)).fieldNames();
81
+ if (expected.length !== actual.length)
82
+ throw new Error("Insert select fields must match table columns");
83
+ for (let i = 0; i < expected.length; i++) {
84
+ if (expected[i] !== actual[i])
85
+ throw new Error("Insert select fields must match table columns");
86
+ }
73
87
  return new InsertCanConflict({
74
88
  ...getData(this),
75
89
  ...getData(query)
@@ -147,21 +161,20 @@ function formatConflicts(on) {
147
161
  );
148
162
  }
149
163
  function insertQuery(query) {
150
- const { insert, values, select, returning } = query;
164
+ const { insert, values, select, returning, overridingSystemValue } = query;
151
165
  if (!values && !select) throw new Error("No values defined");
152
166
  const table = getTable(insert);
153
- const tableName = sql.identifier(table.name);
154
167
  const toInsert = values ? sql.query({
155
168
  values: formatValues(table, Array.isArray(values) ? values : [values])
156
169
  }) : selectQuery(query);
157
170
  const conflicts = query.on ? formatConflicts(query.on) : void 0;
158
171
  return sql.query(
159
172
  formatCTE(query),
160
- { insertInto: sql`${tableName} (${table.listColumns()})` },
173
+ { insertInto: sql`${table.identifier()} (${table.listColumns()})` },
174
+ { overridingSystemValue },
161
175
  toInsert,
162
176
  conflicts,
163
- returning && sql.query({ returning: selection(returning) }),
164
- formatModifiers(query)
177
+ returning && sql.query({ returning: selection(returning) })
165
178
  ).inlineFields(false);
166
179
  }
167
180
  export {
@@ -1,18 +1,26 @@
1
+ import type { Input } from '../expr/Input.js';
1
2
  import type { HasSql, HasTarget } from '../Internal.js';
2
3
  import type { MakeNullable, SelectionInput, SelectionRow } from '../Selection.js';
3
4
  import type { Sql } from '../Sql.js';
4
5
  import type { Table, TableDefinition, TableFields, TableInsert, TableUpdate } from '../Table.js';
5
6
  import type { Expand } from '../Types.js';
6
- import type { Input } from '../expr/Input.js';
7
7
  import type { CTE } from './CTE.js';
8
8
  export interface InnerJoin<Target> {
9
9
  innerJoin: Target;
10
10
  on: HasSql<boolean>;
11
11
  }
12
+ export interface InnerJoinLateral<Target> {
13
+ innerJoinLateral: Target;
14
+ on: HasSql<boolean>;
15
+ }
12
16
  export interface LeftJoin<Target> {
13
17
  leftJoin: Target;
14
18
  on: HasSql<boolean>;
15
19
  }
20
+ export interface LeftJoinLateral<Target> {
21
+ leftJoinLateral: Target;
22
+ on: HasSql<boolean>;
23
+ }
16
24
  export interface RightJoin<Target> {
17
25
  rightJoin: Target;
18
26
  on: HasSql<boolean>;
@@ -23,10 +31,14 @@ export interface FullJoin<Target> {
23
31
  }
24
32
  export interface CrossJoin<Target> {
25
33
  crossJoin: Target;
26
- on: HasSql<boolean>;
34
+ on?: undefined;
27
35
  }
28
- export type Join<Target = HasTarget | Sql> = InnerJoin<Target> | LeftJoin<Target> | RightJoin<Target> | FullJoin<Target> | CrossJoin<Target>;
29
- export type JoinOp = 'leftJoin' | 'rightJoin' | 'innerJoin' | 'fullJoin' | 'crossJoin';
36
+ export interface CrossJoinLateral<Target> {
37
+ crossJoinLateral: Target;
38
+ on?: undefined;
39
+ }
40
+ export type Join<Target = HasTarget | Sql> = InnerJoin<Target> | InnerJoinLateral<Target> | LeftJoin<Target> | LeftJoinLateral<Target> | RightJoin<Target> | FullJoin<Target> | CrossJoin<Target> | CrossJoinLateral<Target>;
41
+ export type JoinOp = 'leftJoin' | 'leftJoinLateral' | 'rightJoin' | 'innerJoin' | 'innerJoinLateral' | 'fullJoin' | 'crossJoin' | 'crossJoinLateral';
30
42
  export interface QueryBase {
31
43
  with?: Array<CTE>;
32
44
  withRecursive?: Array<CTE>;
@@ -47,6 +59,7 @@ export type FromGuard<Target = HasTarget | Sql> = Target | [Target, ...Array<Joi
47
59
  interface SelectionBase<Returning = SelectionInput> extends SelectBase<Returning> {
48
60
  select: Returning;
49
61
  from?: FromGuard;
62
+ for?: HasSql;
50
63
  }
51
64
  export interface SelectionQuery<Returning = SelectionInput> extends SelectionBase<Returning>, QueryBase, ResultModifiers {
52
65
  }
@@ -58,23 +71,23 @@ export interface FromQuery<Target> extends FromBase<Target>, QueryBase, ResultMo
58
71
  type FoldJoins<T extends Array<unknown>, Result> = T extends [
59
72
  infer Join,
60
73
  ...infer Joins
61
- ] ? FoldJoins<Joins, Join extends LeftJoin<Table<infer Definition, infer Name>> ? Result & MakeNullable<Record<Name, TableFields<Definition>>> : Join extends RightJoin<Table<infer Definition, infer Name>> ? MakeNullable<Result> & Record<Name, TableFields<Definition>> : Join extends InnerJoin<Table<infer Definition, infer Name>> ? Result & Record<Name, TableFields<Definition>> : Join extends FullJoin<Table<infer Definition, infer Name>> ? MakeNullable<Result> & MakeNullable<Record<Name, TableFields<Definition>>> : Result> : Result;
74
+ ] ? FoldJoins<Joins, Join extends LeftJoin<Table<infer Definition, infer Name>> ? Result & MakeNullable<Record<Name, TableFields<Definition>>> : Join extends LeftJoinLateral<Table<infer Definition, infer Name>> ? Result & MakeNullable<Record<Name, TableFields<Definition>>> : Join extends RightJoin<Table<infer Definition, infer Name>> ? MakeNullable<Result> & Record<Name, TableFields<Definition>> : Join extends InnerJoin<Table<infer Definition, infer Name>> ? Result & Record<Name, TableFields<Definition>> : Join extends InnerJoinLateral<Table<infer Definition, infer Name>> ? Result & Record<Name, TableFields<Definition>> : Join extends CrossJoin<Table<infer Definition, infer Name>> ? Result & Record<Name, TableFields<Definition>> : Join extends FullJoin<Table<infer Definition, infer Name>> ? MakeNullable<Result> & MakeNullable<Record<Name, TableFields<Definition>>> : Join extends CrossJoinLateral<Table<infer Definition, infer Name>> ? Result & Record<Name, TableFields<Definition>> : Result> : Result;
62
75
  export type FromRow<Target> = Target extends [
63
76
  Table<infer Definition, infer Name>,
64
77
  ...infer Joins
65
78
  ] ? Joins['length'] extends 0 ? TableFields<Definition> : Expand<FoldJoins<Joins, Record<Name, TableFields<Definition>>>> : SelectionRow<Target>;
66
79
  export type Union<Returning = SelectionInput> = {
67
- union: SelectQuery<Returning>;
80
+ union: SelectQuery<Returning> | UnionQuery<Returning>;
68
81
  } | {
69
- unionAll: SelectQuery<Returning>;
82
+ unionAll: SelectQuery<Returning> | UnionQuery<Returning>;
70
83
  } | {
71
- intersect: SelectQuery<Returning>;
84
+ intersect: SelectQuery<Returning> | UnionQuery<Returning>;
72
85
  } | {
73
- intersectAll: SelectQuery<Returning>;
86
+ intersectAll: SelectQuery<Returning> | UnionQuery<Returning>;
74
87
  } | {
75
- except: SelectQuery<Returning>;
88
+ except: SelectQuery<Returning> | UnionQuery<Returning>;
76
89
  } | {
77
- exceptAll: SelectQuery<Returning>;
90
+ exceptAll: SelectQuery<Returning> | UnionQuery<Returning>;
78
91
  };
79
92
  export type UnionOp = 'union' | 'unionAll' | 'intersect' | 'intersectAll' | 'except' | 'exceptAll';
80
93
  export type CompoundSelect<Returning = SelectionInput> = [
@@ -108,6 +121,7 @@ export interface InsertQuery<Returning = SelectionInput, Definition extends Tabl
108
121
  values?: TableInsert<Definition> | Array<TableInsert<Definition>>;
109
122
  returning?: Returning;
110
123
  on?: Array<Conflict<Definition>>;
124
+ overridingSystemValue?: boolean;
111
125
  }
112
126
  export interface DeleteQuery<Returning = SelectionInput, Definition extends TableDefinition = TableDefinition> extends QueryBase, ResultModifiers {
113
127
  delete: Table<Definition>;
@@ -117,6 +131,7 @@ export interface DeleteQuery<Returning = SelectionInput, Definition extends Tabl
117
131
  export interface UpdateQuery<Returning = SelectionInput, Definition extends TableDefinition = TableDefinition> extends QueryBase, ResultModifiers {
118
132
  update: Table<Definition>;
119
133
  set?: TableUpdate<Definition>;
134
+ from?: FromGuard;
120
135
  where?: HasSql<boolean>;
121
136
  returning?: Returning;
122
137
  }
@@ -1,3 +1,5 @@
1
+ import type { Field, StripFieldMeta } from '../expr/Field.js';
2
+ import type { Input as UserInput } from '../expr/Input.js';
1
3
  import { type HasQuery, type HasSelection, type HasSql, type HasTarget, internalData, internalQuery, internalSelection, internalSql } from '../Internal.js';
2
4
  import type { IsMysql, IsPostgres, QueryMeta } from '../MetaData.js';
3
5
  import { type QueryData, SingleQuery } from '../Queries.js';
@@ -5,9 +7,7 @@ import { type IsNullable, type MakeNullable, type Selection, type SelectionRecor
5
7
  import { Sql } from '../Sql.js';
6
8
  import type { Table, TableDefinition, TableFields } from '../Table.js';
7
9
  import type { Expand } from '../Types.js';
8
- import type { Field, StripFieldMeta } from '../expr/Field.js';
9
- import type { Input as UserInput } from '../expr/Input.js';
10
- import { type CTE } from './CTE.js';
10
+ import type { VirtualTarget } from '../Virtual.js';
11
11
  import type { CompoundSelect, SelectQuery, UnionQuery } from './Query.js';
12
12
  type UnionTarget<Input, Meta extends QueryMeta> = UnionBase<Input, Meta> | ((self: Input & HasTarget) => UnionBase<Input, Meta>);
13
13
  export declare class SelectFirst<Input, Meta extends QueryMeta = QueryMeta> extends SingleQuery<SelectionRow<Input>, Meta> implements HasQuery<SelectionRow<Input>> {
@@ -32,16 +32,25 @@ export declare abstract class UnionBase<Input, Meta extends QueryMeta = QueryMet
32
32
  except(target: UnionTarget<Input, Meta>): Union<Input, Meta>;
33
33
  exceptAll<Meta extends IsPostgres | IsMysql>(this: UnionBase<Input, Meta>, target: UnionTarget<Input, Meta>): Union<Input, Meta>;
34
34
  }
35
+ declare const forKeywords: readonly ["update", "no key update", "share", "key share"];
35
36
  export declare class Select<Input, Meta extends QueryMeta = QueryMeta> extends UnionBase<StripFieldMeta<Input>, Meta> implements HasSelection, SelectBase<Input, Meta>, HasQuery<Array<SelectionRow<Input>>> {
36
37
  #private;
37
38
  readonly [internalData]: QueryData<Meta> & SelectQuery;
38
39
  constructor(data: QueryData<Meta> & SelectQuery);
39
40
  from(from: HasTarget | Sql): Select<Input, Meta>;
41
+ for(keyword: (typeof forKeywords)[number], config?: {
42
+ of?: HasTarget | Array<HasTarget>;
43
+ noWait?: boolean;
44
+ skipLocked?: boolean;
45
+ }): Select<Input, Meta>;
40
46
  leftJoin(leftJoin: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
47
+ leftJoinLateral(leftJoinLateral: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
41
48
  rightJoin(rightJoin: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
42
49
  innerJoin(innerJoin: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
50
+ innerJoinLateral(innerJoinLateral: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
43
51
  fullJoin(fullJoin: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
44
- crossJoin(crossJoin: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
52
+ crossJoin(crossJoin: HasTarget | Sql): Select<Input, Meta>;
53
+ crossJoinLateral(crossJoinLateral: HasTarget | Sql): Select<Input, Meta>;
45
54
  where(...where: Array<HasSql<boolean> | undefined>): Select<Input, Meta>;
46
55
  groupBy(...groupBy: Array<HasSql>): Select<Input, Meta>;
47
56
  having(having: HasSql<boolean> | ((self: Input) => HasSql<boolean>)): Select<Input, Meta>;
@@ -54,8 +63,16 @@ export declare class Select<Input, Meta extends QueryMeta = QueryMeta> extends U
54
63
  get [internalQuery](): Sql<Array<SelectionRow<Input>>>;
55
64
  get [internalSql](): Sql<SelectionRow<Input>>;
56
65
  }
57
- export type SubQuery<Input, Name extends string = string> = Input & HasTarget<Name> & HasSelection;
66
+ export type SubQuery<Input, Name extends string = string> = RetypeSubQueryInput<Input, Name> & HasTarget<Name> & HasSelection;
67
+ type RetypeSubQueryInput<Input, TableName extends string> = Input extends HasSql<infer Value> ? Field<Value, TableName> : Input extends SelectionRecord ? Expand<{
68
+ [K in keyof Input]: RetypeSubQueryInput<Input[K], TableName>;
69
+ }> : Input;
58
70
  export interface SelectBase<Input, Meta extends QueryMeta = QueryMeta> extends UnionBase<StripFieldMeta<Input>, Meta>, HasSql<SelectionRow<Input>> {
71
+ for(keyword: (typeof forKeywords)[number], config?: {
72
+ of?: HasTarget | Array<HasTarget>;
73
+ noWait?: boolean;
74
+ skipLocked?: boolean;
75
+ }): Select<Input, Meta>;
59
76
  where(...where: Array<HasSql<boolean> | undefined>): Select<Input, Meta>;
60
77
  groupBy(...exprs: Array<HasSql>): Select<Input, Meta>;
61
78
  having(having: HasSql<boolean>): Select<Input, Meta>;
@@ -67,7 +84,7 @@ export interface SelectBase<Input, Meta extends QueryMeta = QueryMeta> extends U
67
84
  export interface WithoutSelection<Meta extends QueryMeta> {
68
85
  from<Definition extends TableDefinition, Name extends string>(from: Table<Definition, Name>): AllFrom<TableFields<Definition>, Meta, Record<Name, TableFields<Definition>>>;
69
86
  from<Input>(from: SubQuery<Input>): SelectionFrom<Input, Meta>;
70
- from<Input>(from: CTE<Input>): SelectionFrom<Input, Meta>;
87
+ from<Input>(from: VirtualTarget<Input>): SelectionFrom<Input, Meta>;
71
88
  }
72
89
  export interface WithSelection<Input, Meta extends QueryMeta> extends SelectBase<Input, Meta>, HasSql<SelectionRow<Input>> {
73
90
  from<Definition extends TableDefinition, Name extends string>(from: Table<Definition, Name>): SelectionFrom<Input, Meta>;
@@ -78,10 +95,18 @@ export interface WithSelection<Input, Meta extends QueryMeta> extends SelectBase
78
95
  export interface AllFrom<Input, Meta extends QueryMeta, Tables = Input> extends SelectBase<Input, Meta> {
79
96
  leftJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & MakeNullable<Record<Name, TableFields<Definition>>>>, Meta>;
80
97
  leftJoin<Input, Name extends string>(right: SubQuery<Input, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & MakeNullable<Record<Name, Input>>>, Meta>;
98
+ leftJoinLateral<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & MakeNullable<Record<Name, TableFields<Definition>>>>, Meta>;
99
+ leftJoinLateral<Input, Name extends string>(right: SubQuery<Input, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & MakeNullable<Record<Name, Input>>>, Meta>;
81
100
  rightJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<MakeNullable<Tables> & Record<Name, TableFields<Definition>>>, Meta>;
82
101
  rightJoin<Input, Name extends string>(right: SubQuery<Input, Name>, on: HasSql<boolean>): AllFrom<Expand<MakeNullable<Tables> & Record<Name, Input>>, Meta>;
83
102
  innerJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & Record<Name, TableFields<Definition>>>, Meta>;
84
103
  innerJoin<Input, Name extends string>(right: SubQuery<Input, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & Record<Name, Input>>, Meta>;
104
+ innerJoinLateral<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & Record<Name, TableFields<Definition>>>, Meta>;
105
+ innerJoinLateral<Input, Name extends string>(right: SubQuery<Input, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & Record<Name, Input>>, Meta>;
106
+ crossJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>): AllFrom<Expand<Tables & Record<Name, TableFields<Definition>>>, Meta>;
107
+ crossJoin<Input, Name extends string>(right: SubQuery<Input, Name>): AllFrom<Expand<Tables & Record<Name, Input>>, Meta>;
108
+ crossJoinLateral<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>): AllFrom<Expand<Tables & Record<Name, TableFields<Definition>>>, Meta>;
109
+ crossJoinLateral<Input, Name extends string>(right: SubQuery<Input, Name>): AllFrom<Expand<Tables & Record<Name, Input>>, Meta>;
85
110
  fullJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<MakeNullable<Tables> & MakeNullable<Record<Name, TableFields<Definition>>>>, Meta>;
86
111
  fullJoin<Input, Name extends string>(right: SubQuery<Input, Name>, on: HasSql<boolean>): AllFrom<Expand<MakeNullable<Tables> & MakeNullable<Record<Name, Input>>>, Meta>;
87
112
  }
@@ -90,14 +115,22 @@ type MarkFieldsAsNullable<Input, TableName extends string> = Expand<{
90
115
  }>;
91
116
  export interface SelectionFrom<Input, Meta extends QueryMeta> extends SelectBase<Input, Meta> {
92
117
  leftJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<MarkFieldsAsNullable<Input, Name>, Meta>;
118
+ leftJoin<Name extends string>(right: HasTarget<Name>, on: HasSql<boolean>): SelectionFrom<MarkFieldsAsNullable<Input, Name>, Meta>;
93
119
  leftJoin(right: HasTarget, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
120
+ leftJoinLateral<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<MarkFieldsAsNullable<Input, Name>, Meta>;
121
+ leftJoinLateral<Name extends string>(right: HasTarget<Name>, on: HasSql<boolean>): SelectionFrom<MarkFieldsAsNullable<Input, Name>, Meta>;
94
122
  rightJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
95
123
  rightJoin(right: HasTarget, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
96
124
  innerJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
97
125
  innerJoin(right: HasTarget, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
98
- crossJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
99
- crossJoin(right: HasTarget, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
126
+ innerJoinLateral<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
127
+ innerJoinLateral(right: HasTarget, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
128
+ crossJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>): SelectionFrom<Input, Meta>;
129
+ crossJoin(right: HasTarget): SelectionFrom<Input, Meta>;
130
+ crossJoinLateral<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>): SelectionFrom<Input, Meta>;
131
+ crossJoinLateral(right: HasTarget): SelectionFrom<Input, Meta>;
100
132
  fullJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
133
+ fullJoin<Name extends string>(right: HasTarget<Name>, on: HasSql<boolean>): SelectionFrom<MarkFieldsAsNullable<Input, Name>, Meta>;
101
134
  fullJoin(right: HasTarget, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
102
135
  }
103
136
  export declare function querySelection({ select, from }: SelectQuery): Selection;