-
Notifications
You must be signed in to change notification settings - Fork 15
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(connectors): mysql2 support #86
Open
arashsheyda
wants to merge
4
commits into
unjs:main
Choose a base branch
from
arashsheyda:feat/mysql2-connector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a8ecd42
feat(connector): mysql2
arashsheyda f836b37
chore: add mysql to currently supported connectors list
arashsheyda 70127cb
Merge branch 'main' into feat/mysql2-connector
arashsheyda 2aeaccb
Merge branch 'main' into feat/mysql2-connector
arashsheyda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createDatabase } from "../../src"; | ||
import mysqlConnector from "../../src/connectors/mysql2" | ||
|
||
|
||
async function main() { | ||
const db = createDatabase(mysqlConnector({ | ||
host: "localhost", | ||
user: "root", | ||
password: "root", | ||
database: "db0", | ||
})); | ||
|
||
await db.sql`CREATE TABLE IF NOT EXISTS users ( | ||
id INT PRIMARY KEY AUTO_INCREMENT, | ||
name VARCHAR(255) NOT NULL | ||
)`; | ||
|
||
await db.sql`INSERT INTO users (name) VALUES (${randomValue()})`; | ||
|
||
const users = await db.sql`SELECT * FROM users`; | ||
|
||
console.log({ rows: users.rows }); | ||
} | ||
|
||
// eslint-disable-next-line unicorn/prefer-top-level-await | ||
main().catch((error) => { | ||
console.error(error); | ||
// eslint-disable-next-line unicorn/no-process-exit | ||
process.exit(1); | ||
}); | ||
|
||
function randomValue() { | ||
return Math.random().toString(36).slice(7); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "db0-with-mysql", | ||
"private": true, | ||
"scripts": { | ||
"start": "jiti ./index.ts" | ||
}, | ||
"devDependencies": { | ||
"db0": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
"build": "unbuild", | ||
"db0": "pnpm jiti src/cli", | ||
"dev": "vitest", | ||
"start": "f () { pnpm --filter db0-with-${1:-'*'} start ;}; f", | ||
"lint": "eslint --ext .ts . && prettier -c src test", | ||
"lint:fix": "eslint --ext .ts . --fix && prettier -w src test", | ||
"prepack": "pnpm build", | ||
|
@@ -64,6 +65,7 @@ | |
"eslint": "^8.57.0", | ||
"eslint-config-unjs": "^0.2.1", | ||
"jiti": "^1.21.0", | ||
"mysql2": "^3.9.6", | ||
"pg": "^8.11.5", | ||
"prettier": "^3.2.5", | ||
"typescript": "^5.4.5", | ||
|
@@ -87,4 +89,4 @@ | |
} | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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({ | ||
...opts, | ||
}) | ||
|
||
return _connection; | ||
}; | ||
|
||
return <Connector>{ | ||
name: "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; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe } from "vitest"; | ||
import connector from "../../src/connectors/mysql2"; | ||
import { testConnector } from "./_tests"; | ||
|
||
describe.runIf(process.env.POSTGRESQL_URL)( | ||
"connectors: mysql2.test", | ||
() => { | ||
testConnector({ | ||
connector: connector({ | ||
host: "localhost", | ||
user: "root", | ||
password: "root", | ||
database: "db0", | ||
}), | ||
}); | ||
}, | ||
); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wan't sure if we want to have some
options
as default, for example we can set thehost
tolocalhost
orpassword
to""
...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to default to root@localhost:3306 but unsure how useful in practice :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, or we can set
root@localhost:3306
to default only in dev