mysql-memory-server 1.4.0 → 1.4.2

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
@@ -13,15 +13,20 @@ Download with your package manager of choice. The package name is `mysql-memory-
13
13
  - Node.js 16.6.0 and newer
14
14
  - macOS 13+, Windows, or Linux (This package is only tested on Ubuntu 20.04, 22.04, 24.04, and Fedora 40. Other Linux distributions may or may not work at this time.)
15
15
 
16
- Windows only requirements:
16
+ #### Requirements for downloaded MySQL versions
17
+
18
+ The following requirements only apply if you intend to have `mysql-memory-server` download a version of MySQL to use instead of using an already installed version of MySQL on the system:
19
+
20
+ Requirements for Windows:
17
21
  - `Microsoft Visual C++ 2019 Redistributable Package` needs to be installed
18
22
 
19
- Linux only requirements:
23
+ Requirements for Linux:
20
24
  - The `libaio1` or `libaio1t64` package needs to be installed
21
25
  - If `libaio1` is not available but `libaio1t64` is, the `ldconfig` command needs to be available to run
22
- - The `tar` package needs to be installed if you want to use MySQL versions that aren't system installed
26
+ - The `tar` package needs to be installed
27
+
28
+ #### Currently supported MySQL versions
23
29
 
24
- Currently supported MySQL versions:
25
30
  - If using the system installed MySQL server: 8.0.20 and newer
26
31
  - If not using the system installed MySQL server: 8.0.39, 8.1.0, 8.2.0, 8.3.0, 8.4.2, 9.0.1
27
32
 
@@ -31,15 +36,26 @@ Currently supported MySQL versions:
31
36
  import { createDB } from 'mysql-memory-server';
32
37
  import sql from 'mysql2/promise'
33
38
 
34
- // Create a new database
39
+ // Create a new database with default options
35
40
  const db = await createDB()
36
41
 
42
+ //OR
43
+
44
+ //Create a new database with custom options set
45
+ const db = await createDB({
46
+ // see Options for the options you can use in this object and their default values
47
+ // for example:
48
+ version: '8.4.x'
49
+ })
50
+
37
51
  // Connect to the new database with the port provided
52
+ // The database is initialized with an empty password so use an empty string for the password
38
53
  const connection = await sql.createConnection({
39
54
  host: '127.0.0.1',
40
55
  user: db.username,
41
56
  port: db.port,
42
57
  database: db.dbName
58
+ password: ''
43
59
  })
44
60
 
45
61
  // Run your queries here
@@ -52,6 +68,9 @@ await connection.end()
52
68
  await db.stop()
53
69
  ```
54
70
 
71
+ MySQL database initialization can take some time. If you run into a "Timeout exceeded" error with your tests, the timeout should be extended.
72
+ If using Jest, information about how to do this can be found here: https://jestjs.io/docs/jest-object#jestsettimeouttimeout
73
+
55
74
  ## Documentation
56
75
 
57
76
  ##### `createDB(options: ServerOptions): Promise<MySQLDB>`
package/dist/src/index.js CHANGED
@@ -31,16 +31,12 @@ const Logger_1 = __importDefault(require("./libraries/Logger"));
31
31
  const os = __importStar(require("node:os"));
32
32
  const Executor_1 = __importDefault(require("./libraries/Executor"));
33
33
  const semver_1 = require("semver");
34
- const AbortSignal_1 = __importDefault(require("./libraries/AbortSignal"));
35
34
  const Version_1 = __importDefault(require("./libraries/Version"));
36
35
  const versions_json_1 = __importDefault(require("./versions.json"));
37
36
  const Downloader_1 = require("./libraries/Downloader");
38
37
  const crypto_1 = require("crypto");
39
38
  const path_1 = require("path");
40
39
  const constants_1 = __importDefault(require("./constants"));
41
- process.on('exit', () => {
42
- AbortSignal_1.default.abort('Process is exiting');
43
- });
44
40
  async function createDB(opts) {
45
41
  const defaultOptions = {
46
42
  dbName: 'dbdata',
@@ -1,4 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const DBDestroySignal = new AbortController();
4
+ function abortSignal() {
5
+ if (!DBDestroySignal.signal.aborted) {
6
+ DBDestroySignal.abort('Process is exiting');
7
+ }
8
+ }
9
+ process.on('beforeExit', abortSignal);
10
+ process.on('exit', abortSignal);
4
11
  exports.default = DBDestroySignal;
@@ -107,7 +107,7 @@ function downloadFromCDN(url, downloadLocation, logger) {
107
107
  });
108
108
  });
109
109
  }
110
- function extractBinary(url, archiveLocation, extractedLocation) {
110
+ function extractBinary(url, archiveLocation, extractedLocation, logger) {
111
111
  return new Promise(async (resolve, reject) => {
112
112
  const lastDashIndex = url.lastIndexOf('-');
113
113
  const fileExtension = url.slice(lastDashIndex).split('.').splice(1).join('.');
@@ -140,8 +140,11 @@ function extractBinary(url, archiveLocation, extractedLocation) {
140
140
  try {
141
141
  await fsPromises.rm(archiveLocation);
142
142
  }
143
+ catch (e) {
144
+ logger.error('A non-fatal error occurred while removing no longer needed archive file:', e);
145
+ }
143
146
  finally {
144
- fsPromises.rename(`${extractedLocation}/${folderName}`, `${extractedLocation}/mysql`);
147
+ await fsPromises.rename(`${extractedLocation}/${folderName}`, `${extractedLocation}/mysql`);
145
148
  return resolve((0, path_1.normalize)(`${extractedLocation}/mysql/bin/mysqld.exe`));
146
149
  }
147
150
  }
@@ -149,8 +152,11 @@ function extractBinary(url, archiveLocation, extractedLocation) {
149
152
  try {
150
153
  await fsPromises.rm(archiveLocation);
151
154
  }
155
+ catch (e) {
156
+ logger.error('A non-fatal error occurred while removing no longer needed archive file:', e);
157
+ }
152
158
  finally {
153
- fsPromises.rename(`${extractedLocation}/${folderName}`, `${extractedLocation}/mysql`);
159
+ await fsPromises.rename(`${extractedLocation}/${folderName}`, `${extractedLocation}/mysql`);
154
160
  resolve(`${extractedLocation}/mysql/bin/mysqld`);
155
161
  }
156
162
  }).catch(error => {
@@ -180,7 +186,7 @@ function downloadBinary(binaryInfo, options, logger) {
180
186
  (0, proper_lockfile_1.lockSync)(extractedPath);
181
187
  lockedByUs = true;
182
188
  await downloadFromCDN(url, archivePath, logger);
183
- await extractBinary(url, archivePath, extractedPath);
189
+ await extractBinary(url, archivePath, extractedPath, logger);
184
190
  try {
185
191
  (0, proper_lockfile_1.unlockSync)(extractedPath);
186
192
  }
@@ -226,7 +232,7 @@ function downloadBinary(binaryInfo, options, logger) {
226
232
  reject(e);
227
233
  }
228
234
  try {
229
- const binaryPath = await extractBinary(url, zipFilepath, extractedPath);
235
+ const binaryPath = await extractBinary(url, zipFilepath, extractedPath, logger);
230
236
  resolve(binaryPath);
231
237
  }
232
238
  catch (e) {
@@ -226,6 +226,9 @@ _Executor_instances = new WeakSet(), _Executor_execute = function _Executor_exec
226
226
  await this.deleteDatabaseDirectory(dbPath);
227
227
  }
228
228
  }
229
+ catch (e) {
230
+ this.logger.error('An erorr occurred while deleting database directory at path:', dbPath, '| The error was:', e);
231
+ }
229
232
  finally {
230
233
  try {
231
234
  if (binaryFilepath.includes(os.tmpdir()) && !options.downloadBinaryOnce) {
@@ -237,6 +240,9 @@ _Executor_instances = new WeakSet(), _Executor_execute = function _Executor_exec
237
240
  await fsPromises.rm(splitPath.join('/'), { force: true, recursive: true });
238
241
  }
239
242
  }
243
+ catch (e) {
244
+ this.logger.error('An error occurred while deleting no longer needed MySQL binary:', e);
245
+ }
240
246
  finally {
241
247
  if (resolveFunction) {
242
248
  resolveFunction();
@@ -387,7 +393,7 @@ _Executor_instances = new WeakSet(), _Executor_execute = function _Executor_exec
387
393
  }
388
394
  }
389
395
  catch (error) {
390
- if (String(error) === 'Error: Lock file is already being held') {
396
+ if (String(error).includes('Lock file is already being held')) {
391
397
  this.logger.log('Waiting for lock for libaio copy');
392
398
  await (0, FileLock_1.waitForLock)(copyPath, options);
393
399
  this.logger.log('Lock is gone for libaio copy');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mysql-memory-server",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Spin up an ephemeral MySQL database from your JavaScript code",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",
@@ -24,10 +24,10 @@
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
26
  "@babel/core": "^7.24.9",
27
- "@babel/preset-env": "^7.24.8",
27
+ "@babel/preset-env": "^7.25.4",
28
28
  "@babel/preset-typescript": "^7.24.7",
29
29
  "@types/adm-zip": "^0.5.5",
30
- "@types/node": "^22.2.0",
30
+ "@types/node": "^22.5.4",
31
31
  "@types/proper-lockfile": "^4.1.4",
32
32
  "@types/semver": "^7.5.8",
33
33
  "babel-jest": "^29.7.0",
@@ -36,7 +36,7 @@
36
36
  "typescript": "^5.5.4"
37
37
  },
38
38
  "dependencies": {
39
- "adm-zip": "^0.5.15",
39
+ "adm-zip": "^0.5.16",
40
40
  "proper-lockfile": "^4.1.2",
41
41
  "semver": "^7.6.3"
42
42
  },