@remix-run/data-table-mysql 0.4.0 → 0.5.1

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/README.md CHANGED
@@ -1,13 +1,12 @@
1
1
  # data-table-mysql
2
2
 
3
- MySQL adapter for [`remix/data-table`](https://github.com/remix-run/remix/tree/main/packages/data-table).
4
- Use this package when you want `data-table` APIs backed by `mysql2`.
3
+ MySQL database driver for [`remix/data-table`](https://github.com/remix-run/remix/tree/main/packages/data-table), backed by `mysql2`.
5
4
 
6
5
  ## Features
7
6
 
8
- - **Native `mysql2` Integration**: Works with `mysql2/promise` `Pool` and `PoolConnection` instances
7
+ - **Native `mysql2` Integration**: Creates a pool from `mysql2` configuration or uses an existing pool or connection
9
8
  - **Full `data-table` API Support**: Queries, relations, writes, and transactions
10
- - **Adapter-Owned Compiler**: SQL compilation lives in this adapter, with optional shared pure helpers from `data-table`
9
+ - **MySQL Compiler**: SQL compilation is handled automatically for MySQL
11
10
  - **Multi-Statement Migrations**: `executeScript()` runs `up.sql` / `down.sql` files via `mysql2` (requires `multipleStatements: true`)
12
11
  - **MySQL Capabilities Enabled By Default**:
13
12
  - `returning: false`
@@ -25,18 +24,17 @@ npm i remix mysql2
25
24
  ## Usage
26
25
 
27
26
  ```ts
28
- import { createPool } from 'mysql2/promise'
29
- import { createDatabase } from 'remix/data-table'
30
- import { createMysqlDatabaseAdapter } from 'remix/data-table/mysql'
27
+ import { createMysqlDatabase } from 'remix/data-table/mysql'
31
28
 
32
- let pool = createPool(process.env.DATABASE_URL as string)
33
- let db = createDatabase(createMysqlDatabaseAdapter(pool))
29
+ let db = createMysqlDatabase({
30
+ uri: process.env.DATABASE_URL,
31
+ multipleStatements: true,
32
+ })
34
33
  ```
35
34
 
36
- Use `db.query(...)`, relation loading, and transactions from `remix/data-table`.
37
- Import any driver-specific types you need directly from `mysql2/promise`.
35
+ Use `db.query(...)`, relation loading, and transactions from `remix/data-table`. Import any driver-specific types you need directly from `mysql2/promise`.
38
36
 
39
- ## Adapter Capabilities
37
+ ## Database Capabilities
40
38
 
41
39
  `data-table-mysql` reports this capability set by default:
42
40
 
@@ -50,26 +48,26 @@ Import any driver-specific types you need directly from `mysql2/promise`.
50
48
 
51
49
  ### Multi-Statement Migrations
52
50
 
53
- `remix/data-table/migrations` sends each migration to the adapter as a single multi-statement SQL
54
- script. mysql2 only accepts multi-statement scripts when the connection is created with
55
- `multipleStatements: true`:
51
+ `remix/data-table/migrations` sends each migration as a single multi-statement SQL script. mysql2 only accepts multi-statement scripts when the connection is created with `multipleStatements: true`:
56
52
 
57
53
  ```ts
58
- import { createPool } from 'mysql2/promise'
54
+ import { createMysqlDatabase } from 'remix/data-table/mysql'
59
55
 
60
- let pool = createPool({
56
+ let db = createMysqlDatabase({
61
57
  uri: process.env.DATABASE_URL,
62
58
  multipleStatements: true,
63
59
  })
64
60
  ```
65
61
 
62
+ Config-backed databases support `db.wipe()` and `db.reset()`. Call `await db.close()` during application shutdown to close the internally created pool. You may pass an existing `mysql2` pool or connection when your application owns the driver lifecycle; `db.close()` leaves supplied clients alone, and destructive lifecycle methods are unavailable in that mode. `db.wipe()` requires a database name in the connection config (`database`, or the path of a connection URI) and throws when none is present.
63
+
64
+ Migration runs reserve one connection for the MySQL named lock, migration SQL, and journal updates. Lock acquisition waits up to 60 seconds and fails with an error instead of allowing the migration to proceed. After a successful run the connection is unlocked and returned to the pool; if the migration or unlock fails, the reserved connection is destroyed instead of being reused, so a dirty session can never leak back into the pool. Nested migration lock acquisition throws instead of deadlocking.
65
+
66
66
  ### `returning` On MySQL
67
67
 
68
- MySQL does not natively support SQL `RETURNING`. In this adapter, using `returning` on write
69
- operations throws `DataTableQueryError`.
68
+ MySQL does not natively support SQL `RETURNING`. Using `returning` on write operations therefore throws `DataTableQueryError`.
70
69
 
71
- Use write metadata (`affectedRows`, `insertId`) on MySQL, or switch adapters when returned rows
72
- are required.
70
+ Use write metadata (`affectedRows`, `insertId`) on MySQL, or switch databases when returned rows are required.
73
71
 
74
72
  ```ts
75
73
  import { DataTableQueryError } from 'remix/data-table'
@@ -80,17 +78,42 @@ try {
80
78
  .insert({ email: 'a@example.com', status: 'active' }, { returning: ['id'] })
81
79
  } catch (error) {
82
80
  if (error instanceof DataTableQueryError) {
83
- // insert() returning is not supported by this adapter
81
+ // insert() returning is not supported by MySQL
84
82
  }
85
83
  }
86
84
  ```
87
85
 
86
+ ## Running integration tests locally
87
+
88
+ To start a local MySQL container matching CI:
89
+
90
+ ```sh
91
+ podman run --name mysql \
92
+ -e MYSQL_ROOT_PASSWORD=root \
93
+ -e MYSQL_DATABASE=remix \
94
+ -p 3306:3306 \
95
+ -d mysql:8
96
+ ```
97
+
98
+ Then run:
99
+
100
+ ```sh
101
+ REMIX_DATA_TABLE_MYSQL_TEST_URL=mysql://root:root@127.0.0.1:3306/remix \
102
+ pnpm test src/lib/driver.integration.test.ts
103
+ ```
104
+
105
+ Remove the container when you are done:
106
+
107
+ ```sh
108
+ podman rm -f mysql
109
+ ```
110
+
88
111
  ## Related Packages
89
112
 
90
113
  - [`data-table`](https://github.com/remix-run/remix/tree/main/packages/data-table) - Core query/relations API
91
114
  - [`data-schema`](https://github.com/remix-run/remix/tree/main/packages/data-schema) - Schema parsing and validation
92
- - [`data-table-postgres`](https://github.com/remix-run/remix/tree/main/packages/data-table-postgres) - PostgreSQL adapter
93
- - [`data-table-sqlite`](https://github.com/remix-run/remix/tree/main/packages/data-table-sqlite) - SQLite adapter
115
+ - [`data-table-postgres`](https://github.com/remix-run/remix/tree/main/packages/data-table-postgres) - PostgreSQL database driver
116
+ - [`data-table-sqlite`](https://github.com/remix-run/remix/tree/main/packages/data-table-sqlite) - SQLite database driver
94
117
 
95
118
  ## License
96
119
 
package/dist/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
- export { createMysqlDatabaseAdapter, MysqlDatabaseAdapter } from './lib/adapter.ts';
1
+ export { createMysqlDatabase, MysqlDatabase } from './lib/database.ts';
2
+ export type { MysqlDatabaseOptions } from './lib/database.ts';
3
+ export type { MysqlDatabaseInput } from './lib/driver.ts';
2
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACtE,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AAC7D,YAAY,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA"}
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { createMysqlDatabaseAdapter, MysqlDatabaseAdapter } from "./lib/adapter.js";
1
+ export { createMysqlDatabase, MysqlDatabase } from './lib/database.js';
@@ -0,0 +1,36 @@
1
+ import { Database, type DatabaseOptions } from '@remix-run/data-table';
2
+ import { type MysqlDatabaseInput } from './driver.ts';
3
+ /** Options for creating a MySQL database. */
4
+ export interface MysqlDatabaseOptions extends DatabaseOptions {
5
+ /** Character set assigned to the recreated database. */
6
+ characterSet?: string;
7
+ /** Collation assigned to the recreated database. */
8
+ collation?: string;
9
+ }
10
+ /** A {@link Database} backed by MySQL. */
11
+ export declare class MysqlDatabase extends Database<'mysql'> {
12
+ /**
13
+ * Creates a MySQL-backed database.
14
+ * @param input MySQL pool configuration, pool, connection, or URI.
15
+ * @param options Database runtime and recreation options.
16
+ */
17
+ constructor(input: MysqlDatabaseInput, options?: MysqlDatabaseOptions);
18
+ }
19
+ /**
20
+ * Creates a MySQL-backed database.
21
+ *
22
+ * @param input MySQL pool configuration, pool, connection, or URI.
23
+ * @param options Database runtime and recreation options.
24
+ * @returns A MySQL database.
25
+ * @example
26
+ * ```ts
27
+ * import { createMysqlDatabase } from 'remix/data-table/mysql'
28
+ *
29
+ * let db = createMysqlDatabase({
30
+ * uri: process.env.DATABASE_URL,
31
+ * multipleStatements: true,
32
+ * })
33
+ * ```
34
+ */
35
+ export declare function createMysqlDatabase(input: MysqlDatabaseInput, options?: MysqlDatabaseOptions): MysqlDatabase;
36
+ //# sourceMappingURL=database.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../src/lib/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,KAAK,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAEtE,OAAO,EAAuB,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAE1E,6CAA6C;AAC7C,MAAM,WAAW,oBAAqB,SAAQ,eAAe;IAC3D,wDAAwD;IACxD,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,oDAAoD;IACpD,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,0CAA0C;AAC1C,qBAAa,aAAc,SAAQ,QAAQ,CAAC,OAAO,CAAC;IAClD;;;;OAIG;IACH,YAAY,KAAK,EAAE,kBAAkB,EAAE,OAAO,GAAE,oBAAyB,EAExE;CACF;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,kBAAkB,EACzB,OAAO,GAAE,oBAAyB,GACjC,aAAa,CAEf"}
@@ -0,0 +1,32 @@
1
+ import { Database } from '@remix-run/data-table';
2
+ import { MysqlDatabaseDriver } from './driver.js';
3
+ /** A {@link Database} backed by MySQL. */
4
+ export class MysqlDatabase extends Database {
5
+ /**
6
+ * Creates a MySQL-backed database.
7
+ * @param input MySQL pool configuration, pool, connection, or URI.
8
+ * @param options Database runtime and recreation options.
9
+ */
10
+ constructor(input, options = {}) {
11
+ super(new MysqlDatabaseDriver(input, options), options);
12
+ }
13
+ }
14
+ /**
15
+ * Creates a MySQL-backed database.
16
+ *
17
+ * @param input MySQL pool configuration, pool, connection, or URI.
18
+ * @param options Database runtime and recreation options.
19
+ * @returns A MySQL database.
20
+ * @example
21
+ * ```ts
22
+ * import { createMysqlDatabase } from 'remix/data-table/mysql'
23
+ *
24
+ * let db = createMysqlDatabase({
25
+ * uri: process.env.DATABASE_URL,
26
+ * multipleStatements: true,
27
+ * })
28
+ * ```
29
+ */
30
+ export function createMysqlDatabase(input, options = {}) {
31
+ return new MysqlDatabase(input, options);
32
+ }
@@ -1,27 +1,35 @@
1
- import type { DataManipulationRequest, DataManipulationResult, DataManipulationOperation, DatabaseAdapter, SqlStatement, TableRef, TransactionOptions, TransactionToken } from '@remix-run/data-table';
2
- import type { Connection as MysqlConnection, Pool as MysqlPool, PoolConnection as MysqlPoolConnection } from 'mysql2/promise';
1
+ import type { DataManipulationOperation, DataManipulationRequest, DataManipulationResult, DatabaseDriver, SqlStatement, TableRef, TransactionOptions, TransactionToken } from '@remix-run/data-table';
2
+ import type { Connection as MysqlConnection, Pool as MysqlPool, PoolConnection as MysqlPoolConnection, PoolOptions as MysqlPoolOptions } from 'mysql2/promise';
3
3
  type MysqlTransactionConnection = MysqlConnection | MysqlPoolConnection;
4
4
  type MysqlQueryable = MysqlPool | MysqlTransactionConnection;
5
+ export type MysqlDatabaseInput = string | MysqlPoolOptions | MysqlQueryable;
6
+ /** Database creation options used when wiping a config-backed MySQL driver. */
7
+ export interface MysqlDatabaseDriverOptions {
8
+ /** Character set assigned to the recreated database. */
9
+ characterSet?: string;
10
+ /** Collation assigned to the recreated database. */
11
+ collation?: string;
12
+ }
5
13
  /**
6
- * `DatabaseAdapter` implementation for mysql-compatible clients.
14
+ * MySQL database driver backed by a mysql-compatible client.
7
15
  */
8
- export declare class MysqlDatabaseAdapter implements DatabaseAdapter {
16
+ export declare class MysqlDatabaseDriver implements DatabaseDriver<'mysql'> {
9
17
  #private;
10
18
  /**
11
- * The SQL dialect identifier reported by this adapter.
19
+ * The SQL dialect identifier reported by this database.
12
20
  */
13
- dialect: string;
21
+ get dialect(): 'mysql';
14
22
  /**
15
- * Feature flags describing the mysql behaviors supported by this adapter.
23
+ * Feature flags describing the MySQL behaviors supported by this database.
16
24
  */
17
- capabilities: {
18
- returning: boolean;
19
- savepoints: boolean;
20
- upsert: boolean;
21
- transactionalDdl: boolean;
22
- migrationLock: boolean;
23
- };
24
- constructor(client: MysqlQueryable);
25
+ get capabilities(): Readonly<{
26
+ returning: false;
27
+ savepoints: true;
28
+ upsert: true;
29
+ transactionalDdl: false;
30
+ migrationLock: true;
31
+ }>;
32
+ constructor(config: MysqlDatabaseInput, options?: MysqlDatabaseDriverOptions);
25
33
  /**
26
34
  * Compiles a data-manipulation operation to mysql SQL statements.
27
35
  * @param operation Operation to compile.
@@ -99,32 +107,24 @@ export declare class MysqlDatabaseAdapter implements DatabaseAdapter {
99
107
  */
100
108
  releaseSavepoint(token: TransactionToken, name: string): Promise<void>;
101
109
  /**
102
- * Acquires the mysql migration lock.
103
- * @returns A promise that resolves when the lock is acquired.
110
+ * Destructively recreates the configured MySQL database.
111
+ * @returns A promise that resolves when the database is ready for use.
104
112
  */
105
- acquireMigrationLock(): Promise<void>;
113
+ wipe(): Promise<void>;
114
+ /** Closes a pool created from configuration. Supplied connections and pools remain caller-owned. */
115
+ close(): Promise<void>;
106
116
  /**
107
- * Releases the mysql migration lock.
108
- * @returns A promise that resolves when the lock is released.
117
+ * Runs migration work on the mysql connection that owns the named lock.
118
+ *
119
+ * Lock acquisition waits up to 60 seconds and throws when the lock cannot
120
+ * be acquired. Re-entering this method from inside `run` throws instead of
121
+ * deadlocking, and a failed run destroys the reserved connection instead of
122
+ * returning it to the pool.
123
+ * @param name Logical migration lock name.
124
+ * @param run Migration work to run with a connection-bound driver.
125
+ * @returns The callback result.
109
126
  */
110
- releaseMigrationLock(): Promise<void>;
127
+ withMigrationLock<result>(name: string, run: (driver: DatabaseDriver<'mysql'>) => Promise<result>): Promise<result>;
111
128
  }
112
- /**
113
- * Creates a mysql `DatabaseAdapter`.
114
- * @param client Mysql pool or connection.
115
- * @param options Optional adapter capability overrides.
116
- * @returns A configured mysql adapter.
117
- * @example
118
- * ```ts
119
- * import { createPool } from 'mysql2/promise'
120
- * import { createDatabase } from 'remix/data-table'
121
- * import { createMysqlDatabaseAdapter } from 'remix/data-table/mysql'
122
- *
123
- * let pool = createPool({ uri: process.env.DATABASE_URL })
124
- * let adapter = createMysqlDatabaseAdapter(pool)
125
- * let db = createDatabase(adapter)
126
- * ```
127
- */
128
- export declare function createMysqlDatabaseAdapter(client: MysqlQueryable): MysqlDatabaseAdapter;
129
129
  export {};
130
- //# sourceMappingURL=adapter.d.ts.map
130
+ //# sourceMappingURL=driver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"driver.d.ts","sourceRoot":"","sources":["../../src/lib/driver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,uBAAuB,EACvB,sBAAsB,EACtB,cAAc,EACd,YAAY,EACZ,QAAQ,EACR,kBAAkB,EAClB,gBAAgB,EACjB,MAAM,uBAAuB,CAAA;AAK9B,OAAO,KAAK,EACV,UAAU,IAAI,eAAe,EAC7B,IAAI,IAAI,SAAS,EACjB,cAAc,IAAI,mBAAmB,EACrC,WAAW,IAAI,gBAAgB,EAGhC,MAAM,gBAAgB,CAAA;AAcvB,KAAK,0BAA0B,GAAG,eAAe,GAAG,mBAAmB,CAAA;AACvE,KAAK,cAAc,GAAG,SAAS,GAAG,0BAA0B,CAAA;AAE5D,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,gBAAgB,GAAG,cAAc,CAAA;AAU3E,+EAA+E;AAC/E,MAAM,WAAW,0BAA0B;IACzC,wDAAwD;IACxD,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,oDAAoD;IACpD,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;GAEG;AACH,qBAAa,mBAAoB,YAAW,cAAc,CAAC,OAAO,CAAC;;IACjE;;OAEG;IACH,IAAI,OAAO,IAAI,OAAO,CAErB;IAED;;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