rado 1.0.15 → 1.2.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.
- package/dist/compat.d.ts +3 -3
- package/dist/compat.js +2 -0
- package/dist/core/Builder.d.ts +14 -9
- package/dist/core/Builder.js +24 -32
- package/dist/core/Column.d.ts +13 -4
- package/dist/core/Column.js +43 -8
- package/dist/core/Constraint.d.ts +16 -5
- package/dist/core/Constraint.js +17 -6
- package/dist/core/Database.d.ts +11 -4
- package/dist/core/Database.js +20 -4
- package/dist/core/Dialect.js +2 -2
- package/dist/core/Driver.d.ts +3 -3
- package/dist/core/Emitter.d.ts +7 -29
- package/dist/core/Emitter.js +31 -139
- package/dist/core/Index.d.ts +1 -1
- package/dist/core/Internal.d.ts +5 -4
- package/dist/core/Queries.d.ts +46 -0
- package/dist/core/{Query.js → Queries.js} +10 -8
- package/dist/core/Resolver.d.ts +2 -2
- package/dist/core/Selection.d.ts +7 -10
- package/dist/core/Selection.js +15 -13
- package/dist/core/Sql.d.ts +8 -29
- package/dist/core/Sql.js +67 -118
- package/dist/core/Table.d.ts +7 -3
- package/dist/core/Table.js +24 -8
- package/dist/core/Virtual.d.ts +1 -1
- package/dist/core/Virtual.js +15 -14
- package/dist/core/expr/Conditions.d.ts +4 -4
- package/dist/core/expr/Conditions.js +18 -8
- package/dist/core/expr/Field.d.ts +8 -6
- package/dist/core/expr/Field.js +3 -3
- package/dist/core/expr/Include.d.ts +10 -7
- package/dist/core/expr/Include.js +29 -12
- package/dist/core/expr/Input.d.ts +3 -1
- package/dist/core/expr/Input.js +24 -7
- package/dist/core/expr/Json.js +2 -2
- package/dist/core/query/CTE.d.ts +8 -0
- package/dist/core/query/CTE.js +33 -0
- package/dist/core/query/Delete.d.ts +14 -13
- package/dist/core/query/Delete.js +32 -6
- package/dist/core/query/Insert.d.ts +17 -36
- package/dist/core/query/Insert.js +121 -72
- package/dist/core/query/Query.d.ts +124 -0
- package/dist/core/query/Select.d.ts +74 -33
- package/dist/core/query/Select.js +255 -57
- package/dist/core/query/Shared.d.ts +3 -0
- package/dist/core/query/Shared.js +23 -0
- package/dist/core/query/Update.d.ts +13 -13
- package/dist/core/query/Update.js +51 -22
- package/dist/driver/d1.d.ts +2 -2
- package/dist/driver/libsql.d.ts +2 -2
- package/dist/driver/mysql2.d.ts +2 -2
- package/dist/driver/pg.d.ts +2 -2
- package/dist/driver/pglite.d.ts +2 -2
- package/dist/index.d.ts +7 -7
- package/dist/index.js +7 -7
- package/dist/mysql/columns.d.ts +37 -37
- package/dist/mysql/columns.js +32 -15
- package/dist/mysql/dialect.js +6 -7
- package/dist/mysql/diff.js +2 -1
- package/dist/mysql.d.ts +3 -2
- package/dist/mysql.js +11 -2
- package/dist/postgres/columns.d.ts +13 -13
- package/dist/postgres/columns.js +15 -6
- package/dist/postgres/dialect.js +6 -7
- package/dist/postgres/diff.js +2 -1
- package/dist/postgres.d.ts +3 -2
- package/dist/postgres.js +11 -2
- package/dist/sqlite/columns.d.ts +16 -1
- package/dist/sqlite/columns.js +36 -3
- package/dist/sqlite/dialect.js +6 -6
- package/dist/sqlite/diff.js +3 -5
- package/dist/sqlite.d.ts +3 -2
- package/dist/sqlite.js +11 -2
- package/dist/universal/functions.js +4 -1
- package/package.json +4 -4
- package/dist/core/Join.d.ts +0 -1
- package/dist/core/Query.d.ts +0 -46
- package/dist/core/query/Union.d.ts +0 -35
- package/dist/core/query/Union.js +0 -107
- /package/dist/core/{Join.js → query/Query.js} +0 -0
package/dist/compat.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { HasSql } from './core/Internal.js';
|
|
2
2
|
import type { Sql } from './core/Sql.js';
|
|
3
|
-
import type { Table, TableDefinition, TableFields
|
|
4
|
-
export type
|
|
5
|
-
export type InferInsertModel<T> = T extends Table<infer Definition> ? TableInsert<Definition> : never;
|
|
3
|
+
import type { Table, TableDefinition, TableFields } from './core/Table.js';
|
|
4
|
+
export type { InsertRow as InferInsertModel, SelectRow as InferSelectModel } from './core/Table.js';
|
|
6
5
|
export type SQL<T = unknown> = Sql<T>;
|
|
7
6
|
export type SQLWrapper<T = unknown> = HasSql<T>;
|
|
8
7
|
export declare function getTableColumns<Definition extends TableDefinition>(table: Table<Definition>): TableFields<Definition>;
|
|
8
|
+
export { Rollback as TransactionRollbackError } from './core/Database.js';
|
package/dist/compat.js
CHANGED
package/dist/core/Builder.d.ts
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type HasSql, internalData } from './Internal.js';
|
|
2
2
|
import type { IsPostgres, QueryMeta } from './MetaData.js';
|
|
3
|
-
import type { QueryData } from './
|
|
4
|
-
import {
|
|
3
|
+
import type { QueryData, SingleQuery } from './Queries.js';
|
|
4
|
+
import type { SelectionInput, SelectionRow } from './Selection.js';
|
|
5
5
|
import type { Table, TableDefinition } from './Table.js';
|
|
6
|
+
import { type CTE } from './query/CTE.js';
|
|
6
7
|
import { DeleteFrom } from './query/Delete.js';
|
|
7
8
|
import { InsertInto } from './query/Insert.js';
|
|
8
|
-
import type {
|
|
9
|
-
import type { UnionBase } from './query/
|
|
9
|
+
import type { DeleteQuery, FromGuard, FromQuery, FromRow, InsertQuery, QueryBase, SelectionQuery, UpdateQuery } from './query/Query.js';
|
|
10
|
+
import type { UnionBase, WithSelection, WithoutSelection } from './query/Select.js';
|
|
10
11
|
import { UpdateTable } from './query/Update.js';
|
|
11
12
|
declare class BuilderBase<Meta extends QueryMeta> {
|
|
12
|
-
readonly [internalData]: QueryData<Meta
|
|
13
|
-
constructor(data?: QueryData<Meta>);
|
|
13
|
+
readonly [internalData]: QueryData<Meta> & QueryBase;
|
|
14
|
+
constructor(data?: QueryData<Meta> & QueryBase);
|
|
15
|
+
$query<Returning extends SelectionInput>(select: SelectionQuery<Returning>): SingleQuery<SelectionRow<Returning>, Meta>;
|
|
16
|
+
$query<const From extends FromGuard>(from: FromQuery<From>): SingleQuery<FromRow<From>, Meta>;
|
|
17
|
+
$query<Returning extends SelectionInput, Definition extends TableDefinition>(insert: InsertQuery<Returning, Definition>): SingleQuery<SelectionRow<Returning>, Meta>;
|
|
18
|
+
$query<Returning extends SelectionInput, Definition extends TableDefinition>(remove: DeleteQuery<Returning, Definition>): SingleQuery<SelectionRow<Returning>, Meta>;
|
|
19
|
+
$query<Returning extends SelectionInput, Definition extends TableDefinition>(update: UpdateQuery<Returning, Definition>): SingleQuery<SelectionRow<Returning>, Meta>;
|
|
14
20
|
select(): WithoutSelection<Meta>;
|
|
15
|
-
select<Input extends SelectionInput>(
|
|
21
|
+
select<Input extends SelectionInput>(select: Input): WithSelection<Input, Meta>;
|
|
16
22
|
selectDistinct(): WithoutSelection<Meta>;
|
|
17
23
|
selectDistinct<Input extends SelectionInput>(selection: Input): WithSelection<Input, Meta>;
|
|
18
24
|
selectDistinctOn(this: Builder<IsPostgres>, columns: Array<HasSql>): WithoutSelection<Meta>;
|
|
@@ -21,7 +27,6 @@ declare class BuilderBase<Meta extends QueryMeta> {
|
|
|
21
27
|
insert<Definition extends TableDefinition>(into: Table<Definition>): InsertInto<Definition, Meta>;
|
|
22
28
|
delete<Definition extends TableDefinition>(from: Table<Definition>): DeleteFrom<Definition, Meta>;
|
|
23
29
|
}
|
|
24
|
-
export type CTE<Input = unknown> = Input & HasTarget & HasQuery;
|
|
25
30
|
export declare class Builder<Meta extends QueryMeta> extends BuilderBase<Meta> {
|
|
26
31
|
$with(cteName: string): {
|
|
27
32
|
as<Input extends SelectionInput>(query: UnionBase<Input, Meta>): CTE<Input>;
|
package/dist/core/Builder.js
CHANGED
|
@@ -1,73 +1,65 @@
|
|
|
1
1
|
// src/core/Builder.ts
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
internalData,
|
|
7
|
-
internalQuery
|
|
8
|
-
} from "./Internal.js";
|
|
9
|
-
import { selection } from "./Selection.js";
|
|
10
|
-
import { DeleteFrom } from "./query/Delete.js";
|
|
11
|
-
import { InsertInto } from "./query/Insert.js";
|
|
2
|
+
import { getData, internalData } from "./Internal.js";
|
|
3
|
+
import { createCTE } from "./query/CTE.js";
|
|
4
|
+
import { Delete, DeleteFrom } from "./query/Delete.js";
|
|
5
|
+
import { Insert, InsertInto } from "./query/Insert.js";
|
|
12
6
|
import { Select } from "./query/Select.js";
|
|
13
|
-
import { UpdateTable } from "./query/Update.js";
|
|
7
|
+
import { Update, UpdateTable } from "./query/Update.js";
|
|
14
8
|
var BuilderBase = class {
|
|
15
9
|
[internalData];
|
|
16
10
|
constructor(data = {}) {
|
|
17
11
|
this[internalData] = data;
|
|
18
12
|
}
|
|
19
|
-
|
|
13
|
+
$query(query) {
|
|
14
|
+
const data = { ...getData(this), ...query };
|
|
15
|
+
if ("delete" in query) return new Delete(data);
|
|
16
|
+
if ("insert" in query) return new Insert(data);
|
|
17
|
+
if ("update" in query) return new Update(data);
|
|
18
|
+
return new Select(data);
|
|
19
|
+
}
|
|
20
|
+
select(select) {
|
|
20
21
|
return new Select({
|
|
21
22
|
...getData(this),
|
|
22
|
-
select
|
|
23
|
+
select
|
|
23
24
|
});
|
|
24
25
|
}
|
|
25
|
-
selectDistinct(
|
|
26
|
+
selectDistinct(select) {
|
|
26
27
|
return new Select({
|
|
27
28
|
...getData(this),
|
|
28
|
-
select
|
|
29
|
+
select,
|
|
29
30
|
distinct: true
|
|
30
31
|
});
|
|
31
32
|
}
|
|
32
|
-
selectDistinctOn(columns,
|
|
33
|
+
selectDistinctOn(columns, select) {
|
|
33
34
|
return new Select({
|
|
34
35
|
...getData(this),
|
|
35
|
-
select
|
|
36
|
+
select,
|
|
36
37
|
distinctOn: columns
|
|
37
38
|
});
|
|
38
39
|
}
|
|
39
40
|
update(table) {
|
|
40
|
-
return new UpdateTable({ ...getData(this), table });
|
|
41
|
+
return new UpdateTable({ ...getData(this), update: table });
|
|
41
42
|
}
|
|
42
43
|
insert(into) {
|
|
43
|
-
return new InsertInto({ ...getData(this), into });
|
|
44
|
+
return new InsertInto({ ...getData(this), insert: into });
|
|
44
45
|
}
|
|
45
46
|
delete(from) {
|
|
46
|
-
return new DeleteFrom({ ...getData(this), from });
|
|
47
|
+
return new DeleteFrom({ ...getData(this), delete: from });
|
|
47
48
|
}
|
|
48
49
|
};
|
|
49
50
|
var Builder = class extends BuilderBase {
|
|
50
51
|
$with(cteName) {
|
|
51
52
|
return {
|
|
52
53
|
as(query) {
|
|
53
|
-
|
|
54
|
-
return Object.assign(fields, {
|
|
55
|
-
[internalQuery]: getQuery(query).nameSelf(cteName)
|
|
56
|
-
});
|
|
54
|
+
return createCTE(cteName, query);
|
|
57
55
|
}
|
|
58
56
|
};
|
|
59
57
|
}
|
|
60
58
|
with(...definitions) {
|
|
61
|
-
return new BuilderBase({
|
|
62
|
-
...getData(this),
|
|
63
|
-
cte: { recursive: false, definitions }
|
|
64
|
-
});
|
|
59
|
+
return new BuilderBase({ ...getData(this), with: definitions });
|
|
65
60
|
}
|
|
66
61
|
withRecursive(...definitions) {
|
|
67
|
-
return new BuilderBase({
|
|
68
|
-
...getData(this),
|
|
69
|
-
cte: { recursive: true, definitions }
|
|
70
|
-
});
|
|
62
|
+
return new BuilderBase({ ...getData(this), withRecursive: definitions });
|
|
71
63
|
}
|
|
72
64
|
};
|
|
73
65
|
export {
|
package/dist/core/Column.d.ts
CHANGED
|
@@ -11,13 +11,12 @@ export interface ColumnData {
|
|
|
11
11
|
notNull?: boolean;
|
|
12
12
|
isUnique?: boolean;
|
|
13
13
|
autoIncrement?: boolean;
|
|
14
|
-
$default?(): Sql;
|
|
15
14
|
defaultValue?: Sql;
|
|
16
15
|
references?(): FieldData;
|
|
17
|
-
onUpdate?: Sql;
|
|
18
|
-
onDelete?: Sql;
|
|
19
16
|
mapFromDriverValue?(value: unknown, specs: DriverSpecs): unknown;
|
|
20
17
|
mapToDriverValue?(value: unknown): unknown;
|
|
18
|
+
$default?(): Sql;
|
|
19
|
+
$onUpdate?(): Sql;
|
|
21
20
|
}
|
|
22
21
|
type WithoutNull<Value> = Exclude<Value, null>;
|
|
23
22
|
export declare class Column<Value = unknown> {
|
|
@@ -26,9 +25,13 @@ export declare class Column<Value = unknown> {
|
|
|
26
25
|
notNull(): RequiredColumn<Value>;
|
|
27
26
|
$defaultFn(value: () => Input<WithoutNull<Value>>): Column<WithoutNull<Value>>;
|
|
28
27
|
$default(value: Input<WithoutNull<Value>> | (() => Input<WithoutNull<Value>>)): Column<WithoutNull<Value>>;
|
|
28
|
+
$onUpdateFn(fn: () => Input<Value>): Column<Value>;
|
|
29
|
+
$onUpdate(fn: () => Input<Value>): Column<Value>;
|
|
29
30
|
default(value: Input<WithoutNull<Value>>): Column<WithoutNull<Value>>;
|
|
30
31
|
defaultNow(): Column<WithoutNull<Value>>;
|
|
31
|
-
primaryKey(
|
|
32
|
+
primaryKey(options?: {
|
|
33
|
+
autoIncrement: boolean;
|
|
34
|
+
}): Column<WithoutNull<Value>>;
|
|
32
35
|
unique(name?: string): Column<Value>;
|
|
33
36
|
references(foreignField: Field | (() => Field)): Column<Value>;
|
|
34
37
|
$type<T>(): Column<null extends Value ? T | null : T>;
|
|
@@ -46,4 +49,10 @@ export interface Columns {
|
|
|
46
49
|
[key: string]: (...args: Array<Input<any>>) => Sql<any>;
|
|
47
50
|
}
|
|
48
51
|
export declare const column: Columns;
|
|
52
|
+
export type ColumnArguments<Options> = [name?: string] | [options: Options] | [name: string, options: Options];
|
|
53
|
+
export declare function columnConfig<Options>(args: ColumnArguments<Options>): {
|
|
54
|
+
name: string | undefined;
|
|
55
|
+
options: Options | undefined;
|
|
56
|
+
};
|
|
57
|
+
export declare function formatColumn(column: ColumnData): Sql;
|
|
49
58
|
export {};
|
package/dist/core/Column.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// src/core/Column.ts
|
|
2
2
|
import { getData, getField, internalData } from "./Internal.js";
|
|
3
3
|
import { sql } from "./Sql.js";
|
|
4
|
-
import {
|
|
4
|
+
import { callFunction } from "./expr/Functions.js";
|
|
5
|
+
import { input, mapToColumn } from "./expr/Input.js";
|
|
5
6
|
var Column = class _Column {
|
|
6
7
|
[internalData];
|
|
7
8
|
constructor(data) {
|
|
@@ -19,15 +20,22 @@ var Column = class _Column {
|
|
|
19
20
|
$default(value) {
|
|
20
21
|
return new _Column({
|
|
21
22
|
...getData(this),
|
|
22
|
-
$default()
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
$default: () => mapToColumn(getData(this), value instanceof Function ? value() : value)
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
$onUpdateFn(fn) {
|
|
27
|
+
return this.$onUpdate(fn);
|
|
28
|
+
}
|
|
29
|
+
$onUpdate(fn) {
|
|
30
|
+
return new _Column({
|
|
31
|
+
...getData(this),
|
|
32
|
+
$onUpdate: () => mapToColumn(getData(this), fn())
|
|
25
33
|
});
|
|
26
34
|
}
|
|
27
35
|
default(value) {
|
|
28
36
|
return new _Column({
|
|
29
37
|
...getData(this),
|
|
30
|
-
defaultValue: input(value)
|
|
38
|
+
defaultValue: input(value)
|
|
31
39
|
});
|
|
32
40
|
}
|
|
33
41
|
defaultNow() {
|
|
@@ -36,8 +44,8 @@ var Column = class _Column {
|
|
|
36
44
|
defaultValue: sql.unsafe("now()")
|
|
37
45
|
});
|
|
38
46
|
}
|
|
39
|
-
primaryKey() {
|
|
40
|
-
return new _Column({ ...getData(this), primary: true });
|
|
47
|
+
primaryKey(options) {
|
|
48
|
+
return new _Column({ ...getData(this), ...options, primary: true });
|
|
41
49
|
}
|
|
42
50
|
unique(name) {
|
|
43
51
|
return new _Column({ ...getData(this), isUnique: true });
|
|
@@ -78,8 +86,35 @@ var column = new Proxy(createColumn, {
|
|
|
78
86
|
};
|
|
79
87
|
}
|
|
80
88
|
});
|
|
89
|
+
function columnConfig(args) {
|
|
90
|
+
if (typeof args[0] === "string")
|
|
91
|
+
return { name: args[0], options: args[1] };
|
|
92
|
+
return { name: void 0, options: args[0] };
|
|
93
|
+
}
|
|
94
|
+
function formatReferences(fields) {
|
|
95
|
+
return callFunction(
|
|
96
|
+
sql.identifier(fields[0].targetName),
|
|
97
|
+
fields.map((field) => sql.identifier(field.fieldName))
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
function formatColumn(column2) {
|
|
101
|
+
const references = column2.references && sql`references ${formatReferences([column2.references()])}`;
|
|
102
|
+
return sql.query(
|
|
103
|
+
column2.type,
|
|
104
|
+
{
|
|
105
|
+
primaryKey: column2.primary,
|
|
106
|
+
notNull: column2.notNull,
|
|
107
|
+
unique: column2.isUnique,
|
|
108
|
+
autoincrement: column2.autoIncrement
|
|
109
|
+
},
|
|
110
|
+
column2.defaultValue !== void 0 ? sql`default (${column2.defaultValue})`.inlineValues() : void 0,
|
|
111
|
+
references
|
|
112
|
+
);
|
|
113
|
+
}
|
|
81
114
|
export {
|
|
82
115
|
Column,
|
|
83
116
|
JsonColumn,
|
|
84
|
-
column
|
|
117
|
+
column,
|
|
118
|
+
columnConfig,
|
|
119
|
+
formatColumn
|
|
85
120
|
};
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type HasConstraint, type HasData, internalConstraint, internalData } from './Internal.js';
|
|
2
2
|
import { type Sql } from './Sql.js';
|
|
3
3
|
import type { Field, FieldData } from './expr/Field.js';
|
|
4
|
-
export interface
|
|
4
|
+
export interface ConstraintData {
|
|
5
|
+
name?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface UniqueConstraintData extends ConstraintData {
|
|
5
8
|
fields: Array<FieldData>;
|
|
6
9
|
nullsNotDistinct?: boolean;
|
|
7
10
|
}
|
|
@@ -13,8 +16,8 @@ export declare class UniqueConstraint<TableName extends string = string> impleme
|
|
|
13
16
|
nullsNotDistinct(): UniqueConstraint<TableName>;
|
|
14
17
|
get [internalConstraint](): Sql;
|
|
15
18
|
}
|
|
16
|
-
export declare function unique(): UniqueConstraint;
|
|
17
|
-
export interface PrimaryKeyConstraintData {
|
|
19
|
+
export declare function unique(name?: string): UniqueConstraint;
|
|
20
|
+
export interface PrimaryKeyConstraintData extends ConstraintData {
|
|
18
21
|
fields: Array<FieldData>;
|
|
19
22
|
}
|
|
20
23
|
export declare class PrimaryKeyConstraint<TableName extends string = string> implements HasData<PrimaryKeyConstraintData>, HasConstraint {
|
|
@@ -24,8 +27,12 @@ export declare class PrimaryKeyConstraint<TableName extends string = string> imp
|
|
|
24
27
|
constructor(data: PrimaryKeyConstraintData);
|
|
25
28
|
get [internalConstraint](): Sql;
|
|
26
29
|
}
|
|
30
|
+
export declare function primaryKey<TableName extends string = string>(options: {
|
|
31
|
+
name?: string;
|
|
32
|
+
columns: Array<Field<unknown, TableName>>;
|
|
33
|
+
}): PrimaryKeyConstraint<TableName>;
|
|
27
34
|
export declare function primaryKey<TableName extends string = string>(...fields: Array<Field<unknown, TableName>>): PrimaryKeyConstraint<TableName>;
|
|
28
|
-
export interface ForeignKeyConstraintData {
|
|
35
|
+
export interface ForeignKeyConstraintData extends ConstraintData {
|
|
29
36
|
fields: Array<FieldData>;
|
|
30
37
|
references: Array<FieldData>;
|
|
31
38
|
}
|
|
@@ -36,5 +43,9 @@ export declare class ForeignKeyConstraint<TableName extends string = string> imp
|
|
|
36
43
|
references<ForeignTable extends string>(...fields: Array<Field<unknown, ForeignTable>>): ForeignKeyConstraint<TableName>;
|
|
37
44
|
get [internalConstraint](): Sql;
|
|
38
45
|
}
|
|
46
|
+
export declare function foreignKey<TableName extends string = string>(options: {
|
|
47
|
+
name?: string;
|
|
48
|
+
columns: Array<Field<unknown, TableName>>;
|
|
49
|
+
}): ForeignKeyConstraint<TableName>;
|
|
39
50
|
export declare function foreignKey<TableName extends string = string>(...fields: Array<Field<unknown, TableName>>): ForeignKeyConstraint<TableName>;
|
|
40
51
|
export type Constraint = UniqueConstraint | PrimaryKeyConstraint | ForeignKeyConstraint;
|
package/dist/core/Constraint.js
CHANGED
|
@@ -30,8 +30,8 @@ var UniqueConstraint = class _UniqueConstraint {
|
|
|
30
30
|
]);
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
|
-
function unique() {
|
|
34
|
-
return new UniqueConstraint({ fields: [] });
|
|
33
|
+
function unique(name) {
|
|
34
|
+
return new UniqueConstraint({ name, fields: [] });
|
|
35
35
|
}
|
|
36
36
|
var PrimaryKeyConstraint = class {
|
|
37
37
|
constructor(data) {
|
|
@@ -47,8 +47,13 @@ var PrimaryKeyConstraint = class {
|
|
|
47
47
|
)})`;
|
|
48
48
|
}
|
|
49
49
|
};
|
|
50
|
-
function primaryKey(...
|
|
51
|
-
|
|
50
|
+
function primaryKey(...args) {
|
|
51
|
+
if (args.length === 1 && "columns" in args[0])
|
|
52
|
+
return new PrimaryKeyConstraint({
|
|
53
|
+
name: args[0].name,
|
|
54
|
+
fields: args[0].columns.map(getField)
|
|
55
|
+
});
|
|
56
|
+
return new PrimaryKeyConstraint({ fields: args.map(getField) });
|
|
52
57
|
}
|
|
53
58
|
var ForeignKeyConstraint = class _ForeignKeyConstraint {
|
|
54
59
|
[internalData];
|
|
@@ -72,9 +77,15 @@ var ForeignKeyConstraint = class _ForeignKeyConstraint {
|
|
|
72
77
|
)})`;
|
|
73
78
|
}
|
|
74
79
|
};
|
|
75
|
-
function foreignKey(...
|
|
80
|
+
function foreignKey(...args) {
|
|
81
|
+
if (args.length === 1 && "columns" in args[0])
|
|
82
|
+
return new ForeignKeyConstraint({
|
|
83
|
+
name: args[0].name,
|
|
84
|
+
fields: args[0].columns.map(getField),
|
|
85
|
+
references: []
|
|
86
|
+
});
|
|
76
87
|
return new ForeignKeyConstraint({
|
|
77
|
-
fields:
|
|
88
|
+
fields: args.map(getField),
|
|
78
89
|
references: []
|
|
79
90
|
});
|
|
80
91
|
}
|
package/dist/core/Database.d.ts
CHANGED
|
@@ -4,9 +4,11 @@ import type { Diff } from './Diff.js';
|
|
|
4
4
|
import type { Driver } from './Driver.js';
|
|
5
5
|
import { type HasQuery, type HasResolver, type HasSql, type HasTable, internalResolver } from './Internal.js';
|
|
6
6
|
import type { Async, Deliver, Either, QueryDialect, QueryMeta, Sync } from './MetaData.js';
|
|
7
|
-
import {
|
|
7
|
+
import { BatchQuery } from './Queries.js';
|
|
8
8
|
import { Resolver } from './Resolver.js';
|
|
9
|
+
import { type Sql } from './Sql.js';
|
|
9
10
|
import type { Table } from './Table.js';
|
|
11
|
+
import type { SelectFirst } from './query/Select.js';
|
|
10
12
|
export declare class Database<Meta extends QueryMeta = Either> extends Builder<Meta> implements HasResolver<Meta> {
|
|
11
13
|
driver: Driver;
|
|
12
14
|
dialect: Dialect;
|
|
@@ -17,14 +19,19 @@ export declare class Database<Meta extends QueryMeta = Either> extends Builder<M
|
|
|
17
19
|
close(this: Database<Sync>): void;
|
|
18
20
|
[Symbol.dispose](this: Database<Sync>): void;
|
|
19
21
|
[Symbol.asyncDispose](this: Database<Async>): Promise<void>;
|
|
20
|
-
create(...tables: Array<HasTable>):
|
|
21
|
-
drop(...tables: Array<HasTable>):
|
|
22
|
+
create(...tables: Array<HasTable>): BatchQuery<unknown, Meta>;
|
|
23
|
+
drop(...tables: Array<HasTable>): BatchQuery<unknown, Meta>;
|
|
24
|
+
run(input: HasSql): Deliver<Meta, void>;
|
|
25
|
+
get<Result extends Array<unknown>>(input: HasQuery<Result>): Deliver<Meta, Result[number]>;
|
|
26
|
+
get<Result>(input: HasSql<Result>): Deliver<Meta, Result>;
|
|
27
|
+
all<Result>(input: HasSql<Result>): Deliver<Meta, Array<Result>>;
|
|
22
28
|
migrate(...tables: Array<Table>): Deliver<Meta, void>;
|
|
23
|
-
batch<Queries extends Array<HasSql | HasQuery>>(queries: Queries):
|
|
29
|
+
batch<Queries extends Array<HasSql | HasQuery>>(queries: Queries): BatchQuery<unknown, Meta>;
|
|
24
30
|
execute(input: HasSql): Deliver<Meta, void>;
|
|
25
31
|
transaction<T>(this: Database<Sync>, run: (tx: Transaction<Meta>) => T, options?: TransactionOptions[Meta['dialect']]): T;
|
|
26
32
|
transaction<T>(this: Database<Async>, run: (tx: Transaction<Meta>) => Promise<T>, options?: TransactionOptions[Meta['dialect']]): Promise<T>;
|
|
27
33
|
transaction<T>(run: (tx: Transaction<Meta>) => T | Promise<T>, options?: TransactionOptions[Meta['dialect']]): Deliver<Meta, T>;
|
|
34
|
+
$count(source: Table | HasSql, condition?: HasSql<boolean>): SelectFirst<Sql<number>, Meta>;
|
|
28
35
|
}
|
|
29
36
|
export interface TransactionUniversalOptions {
|
|
30
37
|
async?: boolean;
|
package/dist/core/Database.js
CHANGED
|
@@ -6,9 +6,10 @@ import {
|
|
|
6
6
|
getTable,
|
|
7
7
|
internalResolver
|
|
8
8
|
} from "./Internal.js";
|
|
9
|
-
import {
|
|
9
|
+
import { BatchQuery } from "./Queries.js";
|
|
10
10
|
import { Resolver } from "./Resolver.js";
|
|
11
11
|
import { sql } from "./Sql.js";
|
|
12
|
+
import { count } from "./expr/Aggregate.js";
|
|
12
13
|
var Database = class extends Builder {
|
|
13
14
|
driver;
|
|
14
15
|
dialect;
|
|
@@ -32,17 +33,29 @@ var Database = class extends Builder {
|
|
|
32
33
|
return this.close();
|
|
33
34
|
}
|
|
34
35
|
create(...tables) {
|
|
35
|
-
return new
|
|
36
|
+
return new BatchQuery(
|
|
36
37
|
getResolver(this),
|
|
37
38
|
tables.flatMap((table) => getTable(table).create())
|
|
38
39
|
);
|
|
39
40
|
}
|
|
40
41
|
drop(...tables) {
|
|
41
|
-
return new
|
|
42
|
+
return new BatchQuery(
|
|
42
43
|
getResolver(this),
|
|
43
44
|
tables.map((table) => getTable(table).drop())
|
|
44
45
|
);
|
|
45
46
|
}
|
|
47
|
+
run(input) {
|
|
48
|
+
const sql2 = this.dialect.inline(input);
|
|
49
|
+
return this.driver.exec(sql2);
|
|
50
|
+
}
|
|
51
|
+
get(input) {
|
|
52
|
+
const emitter = this.dialect.emit(input);
|
|
53
|
+
return this.driver.prepare(emitter.sql).get(emitter.bind());
|
|
54
|
+
}
|
|
55
|
+
all(input) {
|
|
56
|
+
const emitter = this.dialect.emit(input);
|
|
57
|
+
return this.driver.prepare(emitter.sql).all(emitter.bind());
|
|
58
|
+
}
|
|
46
59
|
migrate(...tables) {
|
|
47
60
|
const computeDiff = this.diff;
|
|
48
61
|
return this.transaction(
|
|
@@ -55,7 +68,7 @@ var Database = class extends Builder {
|
|
|
55
68
|
);
|
|
56
69
|
}
|
|
57
70
|
batch(queries) {
|
|
58
|
-
return new
|
|
71
|
+
return new BatchQuery(getResolver(this), queries);
|
|
59
72
|
}
|
|
60
73
|
execute(input) {
|
|
61
74
|
const emitter = this.dialect.emit(input);
|
|
@@ -71,6 +84,9 @@ var Database = class extends Builder {
|
|
|
71
84
|
{ async: run.constructor.name === "AsyncFunction", ...options }
|
|
72
85
|
);
|
|
73
86
|
}
|
|
87
|
+
$count(source, condition) {
|
|
88
|
+
return this.select(count()).from(source).where(condition).$first();
|
|
89
|
+
}
|
|
74
90
|
};
|
|
75
91
|
var Rollback = class extends Error {
|
|
76
92
|
constructor(data) {
|
package/dist/core/Dialect.js
CHANGED
|
@@ -14,13 +14,13 @@ var Dialect = class {
|
|
|
14
14
|
emit = (input) => {
|
|
15
15
|
const sql = hasQuery(input) ? getQuery(input) : getSql(input);
|
|
16
16
|
const emitter = new this.#createEmitter(this.runtime);
|
|
17
|
-
sql.
|
|
17
|
+
sql.emit(emitter);
|
|
18
18
|
return emitter;
|
|
19
19
|
};
|
|
20
20
|
inline = (input) => {
|
|
21
21
|
const sql = hasQuery(input) ? getQuery(input) : getSql(input);
|
|
22
22
|
const emitter = new this.#createEmitter(this.runtime);
|
|
23
|
-
sql.inlineValues().
|
|
23
|
+
sql.inlineValues().emit(emitter);
|
|
24
24
|
return emitter.sql;
|
|
25
25
|
};
|
|
26
26
|
};
|
package/dist/core/Driver.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { TransactionUniversalOptions } from './Database.js';
|
|
2
2
|
export type Driver = SyncDriver | AsyncDriver;
|
|
3
3
|
export type Statement = SyncStatement | AsyncStatement;
|
|
4
|
-
export interface
|
|
4
|
+
export interface BatchedQuery {
|
|
5
5
|
sql: string;
|
|
6
6
|
params: Array<unknown>;
|
|
7
7
|
isSelection: boolean;
|
|
@@ -19,7 +19,7 @@ export interface SyncDriver extends DriverSpecs {
|
|
|
19
19
|
exec(query: string): void;
|
|
20
20
|
prepare(query: string, options?: PrepareOptions): SyncStatement;
|
|
21
21
|
transaction<T>(run: (inner: SyncDriver) => T, options: TransactionUniversalOptions): T;
|
|
22
|
-
batch(queries: Array<
|
|
22
|
+
batch(queries: Array<BatchedQuery>): Array<Array<unknown>>;
|
|
23
23
|
}
|
|
24
24
|
export interface SyncStatement {
|
|
25
25
|
all(params: Array<unknown>): Array<object>;
|
|
@@ -33,7 +33,7 @@ export interface AsyncDriver extends DriverSpecs {
|
|
|
33
33
|
exec(query: string): Promise<void>;
|
|
34
34
|
prepare(query: string, options?: PrepareOptions): AsyncStatement;
|
|
35
35
|
transaction<T>(run: (inner: AsyncDriver) => Promise<T>, options: TransactionUniversalOptions): Promise<T>;
|
|
36
|
-
batch(queries: Array<
|
|
36
|
+
batch(queries: Array<BatchedQuery>): Promise<Array<Array<unknown>>>;
|
|
37
37
|
}
|
|
38
38
|
export interface AsyncStatement {
|
|
39
39
|
all(params: Array<unknown>): Promise<Array<object>>;
|
package/dist/core/Emitter.d.ts
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { type HasQuery, type HasTarget } from './Internal.js';
|
|
1
|
+
import { type HasSql } from './Internal.js';
|
|
3
2
|
import type { Runtime } from './MetaData.js';
|
|
4
3
|
import { type Param } from './Param.js';
|
|
5
4
|
import { Sql } from './Sql.js';
|
|
6
|
-
import type { TableApi } from './Table.js';
|
|
7
5
|
import type { FieldData } from './expr/Field.js';
|
|
8
|
-
import type {
|
|
9
|
-
import { type JsonPath } from './expr/Json.js';
|
|
10
|
-
import type { Delete } from './query/Delete.js';
|
|
11
|
-
import type { Insert } from './query/Insert.js';
|
|
12
|
-
import type { SelectData } from './query/Select.js';
|
|
13
|
-
import type { UnionData } from './query/Union.js';
|
|
14
|
-
import type { Update } from './query/Update.js';
|
|
6
|
+
import type { JsonPath } from './expr/Json.js';
|
|
15
7
|
export declare abstract class Emitter {
|
|
16
8
|
#private;
|
|
17
9
|
sql: string;
|
|
@@ -26,25 +18,11 @@ export declare abstract class Emitter {
|
|
|
26
18
|
bind(inputs?: Record<string, unknown>): Array<unknown>;
|
|
27
19
|
processValue(value: unknown): unknown;
|
|
28
20
|
emitIdentifierOrSelf(value: string): void;
|
|
29
|
-
selfName?: string;
|
|
30
|
-
emitSelf({ name, inner }: {
|
|
31
|
-
name: string;
|
|
32
|
-
inner: Sql;
|
|
33
|
-
}): void;
|
|
34
21
|
emitUnsafe(value: string): void;
|
|
22
|
+
emitSelf(inner: Sql, name: string): void;
|
|
23
|
+
inlineFields(inner: Sql, withTableName: boolean): void;
|
|
24
|
+
inlineValues(inner: Sql): void;
|
|
35
25
|
emitField({ targetName, fieldName }: FieldData): void;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
emitReferences(fields: Array<FieldData>): void;
|
|
39
|
-
emitDelete(deleteOp: Delete<unknown>): void;
|
|
40
|
-
emitInsert(insert: Insert<unknown>): void;
|
|
41
|
-
emitSelect({ select, cte, from, distinct, distinctOn, where, groupBy, orderBy, having, limit, offset }: SelectData): void;
|
|
42
|
-
emitUnion({ left, operator, right }: UnionData): void;
|
|
43
|
-
emitUpdate(update: Update<unknown>): void;
|
|
44
|
-
emitWith(cte: {
|
|
45
|
-
recursive: boolean;
|
|
46
|
-
definitions: Array<HasQuery & HasTarget>;
|
|
47
|
-
}): void;
|
|
48
|
-
emitInclude(data: IncludeData): void;
|
|
49
|
-
emitUniversal(runtimes: Partial<Record<Runtime | 'default', Sql>>): void;
|
|
26
|
+
emitValueOrInline(value: unknown): void;
|
|
27
|
+
emitUniversal(runtimes: Partial<Record<Runtime | 'default', HasSql>>): void;
|
|
50
28
|
}
|