Skip to content

Commit bb683f1

Browse files
Merge pull request #36 from Sebastian-Webster/add-username-option
Add username option
2 parents fe30630 + 53983d5 commit bb683f1

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ The port that the MySQL database is listening on
5858
The port that MySQLX is listening on
5959
- `dbName: string`
6060
The database that was created on database initialization
61+
- `username: string`
62+
The name of the user to use to login to the database
6163
- `stop: () => Promise<void>`
6264
The method to stop the database. The returned promise resolves when the database has successfully stopped.
6365

@@ -122,6 +124,14 @@ Default: 1,000
122124

123125
Description: If `downloadBinaryOnce` is set to `true`, `lockRetryWait` is the number of milliseconds to wait before checking if the lock has been released.
124126

127+
- `username: string`
128+
129+
Required: No
130+
131+
Default: root
132+
133+
Description: The username of the user that is used to login to the database.
134+
125135
## If using Ubuntu 24.04 and newer
126136

127137
Selecting what MySQL version to use is not currently supported on Ubuntu 24.04 and newer. To use this package on Ubuntu 24.04 and newer you must have the `mysql-server` package installed on your system and `ServerOptions.version` must either be the version that is installed on the system or undefined.

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const defaultOptions: InternalServerOptions = {
1414
portRetries: 10,
1515
downloadBinaryOnce: true,
1616
lockRetries: 1_000,
17-
lockRetryWait: 1_000
17+
lockRetryWait: 1_000,
18+
username: 'root'
1819
}
1920

2021
process.on('exit', () => {

src/libraries/Executor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class Executor {
9292
port,
9393
xPort: mySQLXPort,
9494
dbName: options.dbName,
95+
username: options.username,
9596
stop: () => {
9697
return new Promise(async (resolve, reject) => {
9798
resolveFunction = resolve;
@@ -203,7 +204,13 @@ class Executor {
203204
return reject(err || stderr)
204205
}
205206

206-
await fsPromises.writeFile(`${dbPath}/init.sql`, `CREATE DATABASE ${options.dbName};`, {encoding: 'utf8'})
207+
let initText = `CREATE DATABASE ${options.dbName};`;
208+
209+
if (options.username !== 'root') {
210+
initText += `RENAME USER 'root'@'localhost' TO '${options.username}'@'localhost';`
211+
}
212+
213+
await fsPromises.writeFile(`${dbPath}/init.sql`, initText, {encoding: 'utf8'})
207214

208215
let retries = 0;
209216

tests/sql.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ let db: MySQLDB;
99
let connection: sql.Connection;
1010

1111
beforeEach(async () => {
12-
db = await createDB()
12+
db = await createDB({username: ''})
1313
connection = await sql.createConnection({
1414
host: '127.0.0.1',
15-
user: 'root',
1615
port: db.port
1716
})
1817
})

tests/versions.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ jest.setTimeout(900_000);
99

1010
for (const version of versions) {
1111
test(`running on version ${version}`, async () => {
12-
const db = await createDB({version, dbName: 'testingdata'})
12+
const db = await createDB({version, dbName: 'testingdata', username: ''})
1313
const connection = await sql.createConnection({
1414
host: '127.0.0.1',
15-
user: 'root',
1615
port: db.port
1716
})
1817

types/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export type ServerOptions = {
99
portRetries?: number,
1010
downloadBinaryOnce?: boolean,
1111
lockRetries?: number,
12-
lockRetryWait?: number
12+
lockRetryWait?: number,
13+
username?: string
1314
}
1415

1516
export type InternalServerOptions = {
@@ -19,7 +20,8 @@ export type InternalServerOptions = {
1920
portRetries: number,
2021
downloadBinaryOnce: boolean,
2122
lockRetries: number,
22-
lockRetryWait: number
23+
lockRetryWait: number,
24+
username: string
2325
}
2426

2527
export type ExecutorOptions = {
@@ -36,6 +38,7 @@ export type MySQLDB = {
3638
port: number,
3739
xPort: number,
3840
dbName: string,
41+
username: string,
3942
stop: () => Promise<void>
4043
}
4144

0 commit comments

Comments
 (0)