rado 1.3.0-preview.4 → 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.
@@ -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) {
@@ -1,5 +1,6 @@
1
1
  import { type Column, type JsonColumn } from './Column.js';
2
- import type { ForeignKeyConstraint, PrimaryKeyConstraint, UniqueConstraint } from './Constraint.js';
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, [
@@ -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/driver/pg.js CHANGED
@@ -3,6 +3,17 @@ import { AsyncDatabase } from "../core/Database.js";
3
3
  import { postgresDialect } from "../postgres/dialect.js";
4
4
  import { postgresDiff } from "../postgres/diff.js";
5
5
  import { setTransaction } from "../postgres/transactions.js";
6
+ function isPool(client) {
7
+ return "connect" in client && "totalCount" in client && "idleCount" in client && "waitingCount" in client;
8
+ }
9
+ async function acquireClient(client, depth) {
10
+ if (depth > 0 || !isPool(client)) return { client };
11
+ const acquired = await client.connect();
12
+ return {
13
+ client: acquired,
14
+ release: () => acquired.release()
15
+ };
16
+ }
6
17
  var PreparedStatement = class {
7
18
  constructor(client, sql, name) {
8
19
  this.client = client;
@@ -59,8 +70,9 @@ var PgDriver = class _PgDriver {
59
70
  return new PreparedStatement(this.client, sql, options?.name);
60
71
  }
61
72
  async close() {
62
- if ("end" in this.client) return this.client.end();
73
+ if (this.depth > 0) throw new Error("Cannot close a transaction");
63
74
  if ("release" in this.client) return this.client.release();
75
+ if ("end" in this.client) return this.client.end();
64
76
  }
65
77
  async batch(queries) {
66
78
  return this.transaction(async (tx) => {
@@ -71,8 +83,7 @@ var PgDriver = class _PgDriver {
71
83
  }, {});
72
84
  }
73
85
  async transaction(run, options) {
74
- const acquiredClient = this.depth === 0 && "totalCount" in this.client ? await this.client.connect() : void 0;
75
- const client = acquiredClient ?? this.client;
86
+ const { client, release } = await acquireClient(this.client, this.depth);
76
87
  try {
77
88
  await client.query(this.depth > 0 ? `savepoint d${this.depth}` : "begin");
78
89
  if (this.depth === 0) await client.query(setTransaction(options));
@@ -87,7 +98,7 @@ var PgDriver = class _PgDriver {
87
98
  );
88
99
  throw error;
89
100
  } finally {
90
- acquiredClient?.release();
101
+ release?.();
91
102
  }
92
103
  }
93
104
  };
@@ -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
- return [columnApi.name ?? name, columnApi];
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.notNull) !== Boolean(schemaInstruction.notNull)) {
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.notNull ? sql`${schemaInstruction.type} not null` : sql`${schemaInstruction.type} null`
139
+ schemaInstruction.nullable ? sql`${schemaInstruction.type} null` : sql`${schemaInstruction.type} not null`
130
140
  ]
131
141
  })
132
142
  );
@@ -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
- return [columnApi.name ?? name, columnApi];
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.notNull) !== Boolean(schemaInstruction.notNull)) {
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.notNull ? sql`set not null` : sql`drop not null`
177
+ schemaInstruction.nullable ? sql`drop not null` : sql`set not null`
169
178
  ]
170
179
  })
171
180
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "1.3.0-preview.4",
3
+ "version": "1.3.0-preview.6",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",