rado 1.3.0-preview.5 → 1.3.0-preview.6
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/core/Database.js +6 -0
- package/dist/core/Table.d.ts +3 -1
- package/dist/core/Table.js +14 -0
- package/dist/mysql/diff.js +13 -3
- package/dist/postgres/diff.js +13 -4
- package/package.json +1 -1
package/dist/core/Database.js
CHANGED
|
@@ -53,6 +53,12 @@ var Database = class extends Builder {
|
|
|
53
53
|
return this.driver.prepare(emitter.sql).all(emitter.bind());
|
|
54
54
|
}
|
|
55
55
|
migrate(...tables) {
|
|
56
|
+
if (this.dialect.runtime === "mysql" && tables.length > 1) {
|
|
57
|
+
const run = async () => {
|
|
58
|
+
for (const table of tables) await this.migrate(table);
|
|
59
|
+
};
|
|
60
|
+
return run();
|
|
61
|
+
}
|
|
56
62
|
const computeDiff = this.diff;
|
|
57
63
|
return this.transaction(
|
|
58
64
|
txGenerator(function* (tx) {
|
package/dist/core/Table.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type Column, type JsonColumn } from './Column.js';
|
|
2
|
-
import type { ForeignKeyConstraint,
|
|
2
|
+
import type { ForeignKeyConstraint, UniqueConstraint } from './Constraint.js';
|
|
3
|
+
import { PrimaryKeyConstraint } from './Constraint.js';
|
|
3
4
|
import { Field } from './expr/Field.js';
|
|
4
5
|
import type { Input } from './expr/Input.js';
|
|
5
6
|
import { type JsonExpr } from './expr/Json.js';
|
|
@@ -32,6 +33,7 @@ export declare class TableApi<Definition extends TableDefinition = TableDefiniti
|
|
|
32
33
|
indexes(): Record<string, Index>;
|
|
33
34
|
}
|
|
34
35
|
export declare function tableFields(targetName: string, columns: TableDefinition): Record<string, HasSql>;
|
|
36
|
+
export declare function primaryKeyColumns(tableApi: TableApi): Set<string>;
|
|
35
37
|
export type Table<Definition extends TableDefinition = Record<never, Column>, Name extends string = string> = TableFields<Definition, Name> & HasTable<Definition, Name> & HasSelection & HasCreate & HasDrop;
|
|
36
38
|
export type TableFields<Definition extends TableDefinition, TableName extends string = string> = {
|
|
37
39
|
[K in keyof Definition]: Definition[K] extends JsonColumn<infer T> ? JsonExpr<T> : Definition[K] extends Column<infer T, [
|
package/dist/core/Table.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/core/Table.ts
|
|
2
2
|
import { collectEnumQuery } from "../postgres/enum.js";
|
|
3
3
|
import { formatColumn } from "./Column.js";
|
|
4
|
+
import { PrimaryKeyConstraint } from "./Constraint.js";
|
|
4
5
|
import { Field } from "./expr/Field.js";
|
|
5
6
|
import { jsonExpr } from "./expr/Json.js";
|
|
6
7
|
import { Index } from "./Index.js";
|
|
@@ -114,6 +115,18 @@ function tableFields(targetName, columns) {
|
|
|
114
115
|
})
|
|
115
116
|
);
|
|
116
117
|
}
|
|
118
|
+
function primaryKeyColumns(tableApi) {
|
|
119
|
+
const columns = /* @__PURE__ */ new Set();
|
|
120
|
+
for (const [name, column] of entries(tableApi.columns)) {
|
|
121
|
+
const columnApi = getData(column);
|
|
122
|
+
if (columnApi.primary) columns.add(columnApi.name ?? name);
|
|
123
|
+
}
|
|
124
|
+
for (const constraint of Object.values(tableApi.config ?? {})) {
|
|
125
|
+
if (!(constraint instanceof PrimaryKeyConstraint)) continue;
|
|
126
|
+
for (const field of getData(constraint).fields) columns.add(field.fieldName);
|
|
127
|
+
}
|
|
128
|
+
return columns;
|
|
129
|
+
}
|
|
117
130
|
function table(name, columns, config, schemaName) {
|
|
118
131
|
const api = assign(new TableApi(), {
|
|
119
132
|
name,
|
|
@@ -164,6 +177,7 @@ function tableCreator(nameTable) {
|
|
|
164
177
|
export {
|
|
165
178
|
TableApi,
|
|
166
179
|
alias,
|
|
180
|
+
primaryKeyColumns,
|
|
167
181
|
table,
|
|
168
182
|
tableCreator,
|
|
169
183
|
tableFields
|
package/dist/mysql/diff.js
CHANGED
|
@@ -4,6 +4,7 @@ import { eq } from "../core/expr/Conditions.js";
|
|
|
4
4
|
import { getData, getTable } from "../core/Internal.js";
|
|
5
5
|
import { schema } from "../core/Schema.js";
|
|
6
6
|
import { sql } from "../core/Sql.js";
|
|
7
|
+
import { primaryKeyColumns } from "../core/Table.js";
|
|
7
8
|
import { txGenerator } from "../universal.js";
|
|
8
9
|
import * as column from "./columns.js";
|
|
9
10
|
import { mysqlDialect } from "./dialect.js";
|
|
@@ -77,15 +78,24 @@ var mysqlDiff = (hasTable) => {
|
|
|
77
78
|
{
|
|
78
79
|
type: sql.unsafe(type),
|
|
79
80
|
notNull: column2.notNull && !isAutoIncrement,
|
|
81
|
+
nullable: !column2.notNull,
|
|
80
82
|
defaultValue: column2.defaultValue && !isAutoIncrement ? sql.unsafe(column2.defaultValue) : void 0
|
|
81
83
|
}
|
|
82
84
|
];
|
|
83
85
|
})
|
|
84
86
|
);
|
|
87
|
+
const primaryKeys = primaryKeyColumns(tableApi);
|
|
85
88
|
const schemaColumns = new Map(
|
|
86
89
|
Object.entries(tableApi.columns).map(([name, column2]) => {
|
|
87
90
|
const columnApi = getData(column2);
|
|
88
|
-
|
|
91
|
+
const columnName = columnApi.name ?? name;
|
|
92
|
+
return [
|
|
93
|
+
columnName,
|
|
94
|
+
{
|
|
95
|
+
...columnApi,
|
|
96
|
+
nullable: !columnApi.notNull && !primaryKeys.has(columnName)
|
|
97
|
+
}
|
|
98
|
+
];
|
|
89
99
|
})
|
|
90
100
|
);
|
|
91
101
|
const columnNames = /* @__PURE__ */ new Set([
|
|
@@ -120,13 +130,13 @@ var mysqlDiff = (hasTable) => {
|
|
|
120
130
|
})
|
|
121
131
|
);
|
|
122
132
|
}
|
|
123
|
-
if (Boolean(localInstruction.
|
|
133
|
+
if (Boolean(localInstruction.nullable) !== Boolean(schemaInstruction.nullable)) {
|
|
124
134
|
stmts.push(
|
|
125
135
|
sql.query({
|
|
126
136
|
alterTable,
|
|
127
137
|
modifyColumn: [
|
|
128
138
|
column2,
|
|
129
|
-
schemaInstruction.
|
|
139
|
+
schemaInstruction.nullable ? sql`${schemaInstruction.type} null` : sql`${schemaInstruction.type} not null`
|
|
130
140
|
]
|
|
131
141
|
})
|
|
132
142
|
);
|
package/dist/postgres/diff.js
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
} from "../core/expr/Conditions.js";
|
|
15
15
|
import { getData, getTable } from "../core/Internal.js";
|
|
16
16
|
import { sql } from "../core/Sql.js";
|
|
17
|
-
import { table } from "../core/Table.js";
|
|
17
|
+
import { primaryKeyColumns, table } from "../core/Table.js";
|
|
18
18
|
import { concat, txGenerator } from "../universal.js";
|
|
19
19
|
import * as column from "./columns.js";
|
|
20
20
|
import { postgresDialect } from "./dialect.js";
|
|
@@ -115,15 +115,24 @@ var postgresDiff = (hasTable) => {
|
|
|
115
115
|
{
|
|
116
116
|
type: sql.unsafe(column2.type.toLowerCase()),
|
|
117
117
|
notNull: column2.notNull,
|
|
118
|
+
nullable: !column2.notNull,
|
|
118
119
|
defaultValue: column2.defaultValue ? sql.unsafe(column2.defaultValue) : void 0
|
|
119
120
|
}
|
|
120
121
|
];
|
|
121
122
|
})
|
|
122
123
|
);
|
|
124
|
+
const primaryKeys = primaryKeyColumns(tableApi);
|
|
123
125
|
const schemaColumns = new Map(
|
|
124
126
|
Object.entries(tableApi.columns).map(([name, column2]) => {
|
|
125
127
|
const columnApi = getData(column2);
|
|
126
|
-
|
|
128
|
+
const columnName = columnApi.name ?? name;
|
|
129
|
+
return [
|
|
130
|
+
columnName,
|
|
131
|
+
{
|
|
132
|
+
...columnApi,
|
|
133
|
+
nullable: !columnApi.notNull && !primaryKeys.has(columnName)
|
|
134
|
+
}
|
|
135
|
+
];
|
|
127
136
|
})
|
|
128
137
|
);
|
|
129
138
|
const stmts = [];
|
|
@@ -159,13 +168,13 @@ var postgresDiff = (hasTable) => {
|
|
|
159
168
|
})
|
|
160
169
|
);
|
|
161
170
|
}
|
|
162
|
-
if (Boolean(localInstruction.
|
|
171
|
+
if (Boolean(localInstruction.nullable) !== Boolean(schemaInstruction.nullable)) {
|
|
163
172
|
stmts.push(
|
|
164
173
|
sql.query({
|
|
165
174
|
alterTable,
|
|
166
175
|
alterColumn: [
|
|
167
176
|
column2,
|
|
168
|
-
schemaInstruction.
|
|
177
|
+
schemaInstruction.nullable ? sql`drop not null` : sql`set not null`
|
|
169
178
|
]
|
|
170
179
|
})
|
|
171
180
|
);
|