Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mysql connector #86

Merged
merged 8 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
POSTGRESQL_URL=postgresql://<user>:@localhost:5432/db0
MYSQL_URL=

# PlanetScale
PLANETSCALE_HOST=aws.connect.psdb.cloud
PLANETSCALE_USERNAME=username
PLANETSCALE_PASSWORD=password

1 change: 1 addition & 0 deletions docs/2.connectors/1.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Currently supported connectors:
- [LibSQL](/connectors/libsql)
- [PlanetScale](/connectors/planetscale)
- [PostgreSQL](/connectors/postgresql)
- [MySQL](/connectors/mysql)
- [SQLite](/connectors/sqlite)

::read-more{to="https://github.com/unjs/db0/issues/32"}
Expand Down
27 changes: 22 additions & 5 deletions docs/2.connectors/mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@ icon: simple-icons:mysql

# MySQL

> Connect DB0 to Mysql Database
> Connect DB0 to Mysql Database using mysql2

:read-more{to="https://github.com/sidorares/node-mysql2"}
## Usage

::read-more{to="https://github.com/unjs/db0/issues/32"}
This connector is planned to be supported. Follow up via [unjs/db0#32](https://github.com/unjs/db0/issues/32).
::
For this connector, you need to install [`mysql2`](https://www.npmjs.com/package/mysql2) dependency:

:pm-install{name="mysql2"}

Use `mysql2` connector:

```js
import { createDatabase, sql } from "db0";
import mysql from "db0/connectors/mysql2";

const db = createDatabase(
mysql({
/* options */
}),
);
```

## Options

:read-more{to="https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Connection.d.ts#L82-L329"}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions src/connectors/mysql2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import mysql from "mysql2/promise";

import type { Connector, Statement } from "../types";

export default function mysqlConnector(opts: mysql.ConnectionOptions) {
let _connection: mysql.Connection | undefined;
const getConnection = async () => {
if (_connection) {
return _connection;
}

_connection = await mysql.createConnection({
pi0 marked this conversation as resolved.
Show resolved Hide resolved
...opts,
})

return _connection;
};

return <Connector>{
name: "mysql",
dialect: "mysql",
exec(sql: string) {
return getConnection().then((c) => c.query(sql).then((res) => res[0]));
},
prepare(sql: string) {
const stmt = <Statement>{
_sql: sql,
_params: [],
bind(...params) {
if (params.length > 0) {
this._params = params;
}
return stmt;
},
all(...params) {
return getConnection().then((c) => c.query(this._sql, params || this._params).then((res) => res[0]));
},
run(...params) {
return getConnection().then((c) => c.query(this._sql, params || this._params).then((res) => res[0]));
},
get(...params) {
return getConnection().then((c) => c.query(this._sql, params || this._params).then((res) => res[0][0]));
},
};
return stmt;
},
};
}
18 changes: 18 additions & 0 deletions test/connectors/mysql2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe } from "vitest";
import connector from "../../src/connectors/mysql2";
import { testConnector } from "./_tests";

describe.runIf(process.env.MYSQL_URL)(
"connectors: mysql2.test",
() => {
testConnector({
dialect: "mysql",
connector: connector({
host: "localhost",
user: "root",
password: "root",
database: "db0",
}),
});
},
);
Loading