Comparing version 3.0.0 to 3.1.0
@@ -8,2 +8,3 @@ 'use strict'; | ||
var sqlstring = require('sqlstring'); | ||
var zlib = require('zlib'); | ||
var mysql$1 = require('mysql2/promise'); | ||
@@ -70,3 +71,6 @@ | ||
// only include the tables from the options that actually exist in the db | ||
tables = tables.filter(t => restrictedTables.indexOf(t.name) !== -1); | ||
// keeping the order of the passed-in whitelist and filtering out non-existing tables | ||
tables = restrictedTables | ||
.map(tableName => actualTables.find(t => t.name === tableName)) | ||
.filter((t) => t !== undefined); | ||
} | ||
@@ -681,2 +685,41 @@ } | ||
function compressFile(filename) { | ||
const tempFilename = `${filename}.temp`; | ||
fs.renameSync(filename, tempFilename); | ||
const deleteFile = (file) => { | ||
try { | ||
fs.unlinkSync(file); | ||
} | ||
catch (_err) { | ||
/* istanbul ignore next */ | ||
} | ||
}; | ||
try { | ||
const read = fs.createReadStream(tempFilename); | ||
const zip = zlib.createGzip(); | ||
const write = fs.createWriteStream(filename); | ||
read.pipe(zip).pipe(write); | ||
return new Promise((resolve, reject) => { | ||
write.on('error', | ||
/* istanbul ignore next */ err => { | ||
// close the write stream and propagate the error | ||
write.end(); | ||
reject(err); | ||
}); | ||
write.on('finish', () => { | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
catch (err) /* istanbul ignore next */ { | ||
// in case of an error: remove the output file and propagate the error | ||
deleteFile(filename); | ||
throw err; | ||
} | ||
finally { | ||
// in any case: remove the temp file | ||
deleteFile(tempFilename); | ||
} | ||
} | ||
const pool = []; | ||
@@ -904,2 +947,6 @@ class DB { | ||
} | ||
// compress output file | ||
if (options.dumpToFile && options.compressFile) { | ||
yield compressFile(options.dumpToFile); | ||
} | ||
return res; | ||
@@ -906,0 +953,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { appendFileSync, createWriteStream, writeFileSync } from 'fs'; | ||
import { appendFileSync, createReadStream, createWriteStream, renameSync, unlinkSync, writeFileSync } from 'fs'; | ||
import { all } from 'deepmerge'; | ||
@@ -6,2 +6,3 @@ import { format } from 'sql-formatter'; | ||
import { escape } from 'sqlstring'; | ||
import { createGzip } from 'zlib'; | ||
import { createConnection as createConnection$1 } from 'mysql2/promise'; | ||
@@ -68,3 +69,6 @@ | ||
// only include the tables from the options that actually exist in the db | ||
tables = tables.filter(t => restrictedTables.indexOf(t.name) !== -1); | ||
// keeping the order of the passed-in whitelist and filtering out non-existing tables | ||
tables = restrictedTables | ||
.map(tableName => actualTables.find(t => t.name === tableName)) | ||
.filter((t) => t !== undefined); | ||
} | ||
@@ -679,2 +683,41 @@ } | ||
function compressFile(filename) { | ||
const tempFilename = `${filename}.temp`; | ||
renameSync(filename, tempFilename); | ||
const deleteFile = (file) => { | ||
try { | ||
unlinkSync(file); | ||
} | ||
catch (_err) { | ||
/* istanbul ignore next */ | ||
} | ||
}; | ||
try { | ||
const read = createReadStream(tempFilename); | ||
const zip = createGzip(); | ||
const write = createWriteStream(filename); | ||
read.pipe(zip).pipe(write); | ||
return new Promise((resolve, reject) => { | ||
write.on('error', | ||
/* istanbul ignore next */ err => { | ||
// close the write stream and propagate the error | ||
write.end(); | ||
reject(err); | ||
}); | ||
write.on('finish', () => { | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
catch (err) /* istanbul ignore next */ { | ||
// in case of an error: remove the output file and propagate the error | ||
deleteFile(filename); | ||
throw err; | ||
} | ||
finally { | ||
// in any case: remove the temp file | ||
deleteFile(tempFilename); | ||
} | ||
} | ||
const pool = []; | ||
@@ -902,2 +945,6 @@ class DB { | ||
} | ||
// compress output file | ||
if (options.dumpToFile && options.compressFile) { | ||
yield compressFile(options.dumpToFile); | ||
} | ||
return res; | ||
@@ -904,0 +951,0 @@ } |
@@ -245,2 +245,7 @@ /// <reference types="node" /> | ||
dumpToFile?: string | null; | ||
/** | ||
* Should the output file be compressed (gzip)? | ||
* Defaults to false. | ||
*/ | ||
compressFile?: boolean; | ||
} | ||
@@ -247,0 +252,0 @@ export interface ColumnList { |
{ | ||
"name": "mysqldump", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Create a DUMP from MySQL", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs.js", |
565
README.md
@@ -36,2 +36,14 @@ # Mysql Dump | ||
// dump the result straight to a compressed file | ||
mysqldump({ | ||
connection: { | ||
host: 'localhost', | ||
user: 'root', | ||
password: '123456', | ||
database: 'my_database', | ||
}, | ||
dumpToFile: './dump.sql.gz', | ||
compressFile: true, | ||
}); | ||
// return the dump from the function and not to a file | ||
@@ -85,267 +97,318 @@ const result = await mysqldump({ | ||
export interface ConnectionOptions { | ||
/** | ||
* The database host to connect to. | ||
* Defaults to 'localhost'. | ||
*/ | ||
host?: string; | ||
/** | ||
* The port on the host to connect to. | ||
* Defaults to 3306. | ||
*/ | ||
port?: number; | ||
/** | ||
* The database to dump. | ||
*/ | ||
database: string; | ||
/** | ||
* The DB username to use to connect. | ||
*/ | ||
user: string; | ||
/** | ||
* The password to use to connect. | ||
*/ | ||
password: string; | ||
/** | ||
* The charset to use for the connection. | ||
* Defaults to 'UTF8_GENERAL_CI'. | ||
*/ | ||
charset?: string; | ||
/** | ||
* The database host to connect to. | ||
* Defaults to 'localhost'. | ||
*/ | ||
host?: string; | ||
/** | ||
* The port on the host to connect to. | ||
* Defaults to 3306. | ||
*/ | ||
port?: number; | ||
/** | ||
* The database to dump. | ||
*/ | ||
database: string; | ||
/** | ||
* The DB username to use to connect. | ||
*/ | ||
user: string; | ||
/** | ||
* The password to use to connect. | ||
*/ | ||
password: string; | ||
/** | ||
* The charset to use for the connection. | ||
* Defaults to 'UTF8_GENERAL_CI'. | ||
*/ | ||
charset?: string; | ||
/** | ||
* SSL configuration options. | ||
* Passing 'Amazon RDS' will use Amazon's RDS CA certificate. | ||
* | ||
* Otherwise you can pass the options which get passed to tls.createSecureContext. | ||
* See: https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options | ||
*/ | ||
ssl?: 'Amazon RDS' | null | { | ||
/** | ||
* Optionally override the trusted CA certificates. Default is to trust the well-known CAs curated by Mozilla. | ||
*/ | ||
ca?: string | Buffer; | ||
/** | ||
* Optional cert chains in PEM format. | ||
*/ | ||
cert?: string | Buffer; | ||
/** | ||
* Optional cipher suite specification, replacing the default. | ||
*/ | ||
ciphers?: string; | ||
/** | ||
* Optional PEM formatted CRLs (Certificate Revocation Lists). | ||
*/ | ||
crl?: string | Array<string>; | ||
/** | ||
* Attempt to use the server's cipher suite preferences instead of the client's. | ||
*/ | ||
honorCipherOrder?: boolean; | ||
/** | ||
* Optional private keys in PEM format. | ||
*/ | ||
key?: string | Buffer; | ||
/** | ||
* Optional shared passphrase used for a single private key and/or a PFX. | ||
*/ | ||
passphrase?: string; | ||
/** | ||
* Optional PFX or PKCS12 encoded private key and certificate chain. | ||
*/ | ||
pfx?: string | Buffer; | ||
/** | ||
* DO NOT USE THIS OPTION UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!!! | ||
* Set to false to allow connection to a MySQL server without properly providing the appropraite CA to trust. | ||
*/ | ||
rejectUnauthorized?: boolean; | ||
}; | ||
} | ||
export interface SchemaDumpOptions { | ||
/** | ||
* True to include autoincrement values in schema, false otherwise. | ||
* Defaults to true. | ||
*/ | ||
autoIncrement?: boolean; | ||
/** | ||
* True to include engine values in schema, false otherwise. | ||
* Defaults to true. | ||
*/ | ||
engine?: boolean; | ||
/** | ||
* True to run a sql formatter over the output, false otherwise. | ||
* Defaults to true. | ||
*/ | ||
format?: boolean; | ||
/** | ||
* Options for table dumps | ||
*/ | ||
table?: { | ||
/** | ||
* Guard create table calls with an "IF NOT EXIST" | ||
* Defaults to true. | ||
*/ | ||
ifNotExist?: boolean; | ||
/** | ||
* Drop tables before creation (overrides `ifNotExist`). | ||
* Defaults to false. | ||
*/ | ||
dropIfExist?: boolean; | ||
/** | ||
* Include the `DEFAULT CHARSET = x` at the end of the table definition | ||
* Set to true to include the value form the DB. | ||
* Set to false to exclude it altogether. | ||
* Set to a string to explicitly set the charset. | ||
* Defaults to true. | ||
*/ | ||
charset?: boolean | string; | ||
}; | ||
view?: { | ||
/** | ||
* Uses `CREATE OR REPLACE` to define views. | ||
* Defaults to true. | ||
*/ | ||
createOrReplace?: boolean; | ||
/** | ||
* Include the `DEFINER = {\`user\`@\`host\` | CURRENT_USER}` in the view definition or not | ||
* Defaults to false. | ||
*/ | ||
definer?: boolean; | ||
/** | ||
* Include the `ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}` in the view definition or not | ||
* Defaults to false. | ||
*/ | ||
algorithm?: boolean; | ||
/** | ||
* Incldue the `SQL SECURITY {DEFINER | INVOKER}` in the view definition or not | ||
* Defaults to false. | ||
*/ | ||
sqlSecurity?: boolean; | ||
}; | ||
/** | ||
* True to include autoincrement values in schema, false otherwise. | ||
* Defaults to true. | ||
*/ | ||
autoIncrement?: boolean; | ||
/** | ||
* True to include engine values in schema, false otherwise. | ||
* Defaults to true. | ||
*/ | ||
engine?: boolean; | ||
/** | ||
* True to run a sql formatter over the output, false otherwise. | ||
* Defaults to true. | ||
*/ | ||
format?: boolean; | ||
/** | ||
* Options for table dumps | ||
*/ | ||
table?: { | ||
/** | ||
* Guard create table calls with an "IF NOT EXIST" | ||
* Defaults to true. | ||
*/ | ||
ifNotExist?: boolean; | ||
/** | ||
* Drop tables before creation (overrides `ifNotExist`). | ||
* Defaults to false. | ||
*/ | ||
dropIfExist?: boolean; | ||
/** | ||
* Include the `DEFAULT CHARSET = x` at the end of the table definition | ||
* Set to true to include the value form the DB. | ||
* Set to false to exclude it altogether. | ||
* Set to a string to explicitly set the charset. | ||
* Defaults to true. | ||
*/ | ||
charset?: boolean | string; | ||
}; | ||
view?: { | ||
/** | ||
* Uses `CREATE OR REPLACE` to define views. | ||
* Defaults to true. | ||
*/ | ||
createOrReplace?: boolean; | ||
/** | ||
* Include the `DEFINER = {\`user\`@\`host\` | CURRENT_USER}` in the view definition or not | ||
* Defaults to false. | ||
*/ | ||
definer?: boolean; | ||
/** | ||
* Include the `ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}` in the view definition or not | ||
* Defaults to false. | ||
*/ | ||
algorithm?: boolean; | ||
/** | ||
* Incldue the `SQL SECURITY {DEFINER | INVOKER}` in the view definition or not | ||
* Defaults to false. | ||
*/ | ||
sqlSecurity?: boolean; | ||
}; | ||
} | ||
export interface TriggerDumpOptions { | ||
/** | ||
* The temporary delimiter to use between statements. | ||
* Set to false to not use delmiters | ||
* Defaults to ';;'. | ||
*/ | ||
delimiter?: string | false; | ||
/** | ||
* Drop triggers before creation. | ||
* Defaults to false. | ||
*/ | ||
dropIfExist?: boolean; | ||
/** | ||
* Include the `DEFINER = {\`user\`@\`host\` | CURRENT_USER}` in the view definition or not | ||
* Defaults to false. | ||
*/ | ||
definer?: boolean; | ||
/** | ||
* The temporary delimiter to use between statements. | ||
* Set to false to not use delmiters | ||
* Defaults to ';;'. | ||
*/ | ||
delimiter?: string | false; | ||
/** | ||
* Drop triggers before creation. | ||
* Defaults to false. | ||
*/ | ||
dropIfExist?: boolean; | ||
/** | ||
* Include the `DEFINER = {\`user\`@\`host\` | CURRENT_USER}` in the view definition or not | ||
* Defaults to false. | ||
*/ | ||
definer?: boolean; | ||
} | ||
export interface DataDumpOptions { | ||
/** | ||
* True to run a sql formatter over the output, false otherwise. | ||
* Defaults to true. | ||
*/ | ||
format?: boolean; | ||
/** | ||
* Include file headers in output | ||
* Defaults to true. | ||
*/ | ||
verbose ?: boolean | ||
/** | ||
* Use a read lock during the data dump (see: https://dev.mysql.com/doc/refman/5.7/en/replication-solutions-backups-read-only.html) | ||
* Defaults to false. | ||
*/ | ||
lockTables ?: boolean | ||
/** | ||
* Dump data from views. | ||
* Defaults to false. | ||
*/ | ||
includeViewData?: boolean; | ||
/** | ||
* Maximum number of rows to include in each multi-line insert statement | ||
* Defaults to 1 (i.e. new statement per row). | ||
*/ | ||
maxRowsPerInsertStatement?: number; | ||
/** | ||
* True to return the data in a function, false to not. | ||
* This is useful in databases with a lot of data. | ||
* | ||
* We stream data from the DB to reduce the memory footprint. | ||
* However note that if you want the result returned from the function, | ||
* this will result in a larger memory footprint as the string has to be stored in memory. | ||
* | ||
* Defaults to false if dumpToFile is truthy, or true if not dumpToFile is falsey. | ||
*/ | ||
returnFromFunction?: boolean; | ||
/** | ||
* A map of tables to additional where strings to add. | ||
* Use this to limit the number of data that is dumped. | ||
* Defaults to no limits | ||
*/ | ||
where?: { | ||
[k: string]: string; | ||
}; | ||
/** | ||
* True to run a sql formatter over the output, false otherwise. | ||
* Defaults to true. | ||
*/ | ||
format?: boolean; | ||
/** | ||
* Include file headers in output | ||
* Defaults to true. | ||
*/ | ||
verbose?: boolean; | ||
/** | ||
* Use a read lock during the data dump (see: https://dev.mysql.com/doc/refman/5.7/en/replication-solutions-backups-read-only.html) | ||
* Defaults to false. | ||
*/ | ||
lockTables?: boolean; | ||
/** | ||
* Dump data from views. | ||
* Defaults to false. | ||
*/ | ||
includeViewData?: boolean; | ||
/** | ||
* Maximum number of rows to include in each multi-line insert statement | ||
* Defaults to 1 (i.e. new statement per row). | ||
*/ | ||
maxRowsPerInsertStatement?: number; | ||
/** | ||
* True to return the data in a function, false to not. | ||
* This is useful in databases with a lot of data. | ||
* | ||
* We stream data from the DB to reduce the memory footprint. | ||
* However note that if you want the result returned from the function, | ||
* this will result in a larger memory footprint as the string has to be stored in memory. | ||
* | ||
* Defaults to false if dumpToFile is truthy, or true if not dumpToFile is falsey. | ||
*/ | ||
returnFromFunction?: boolean; | ||
/** | ||
* A map of tables to additional where strings to add. | ||
* Use this to limit the number of data that is dumped. | ||
* Defaults to no limits | ||
*/ | ||
where?: { | ||
[k: string]: string; | ||
}; | ||
} | ||
export interface DumpOptions { | ||
/** | ||
* The list of tables that you want to dump. | ||
* Defaults to all tables (signalled by passing an empty array). | ||
*/ | ||
tables?: string[]; | ||
/** | ||
* True to use the `tables` options as a blacklist, false to use it as a whitelist. | ||
* Defaults to false. | ||
*/ | ||
excludeTables?: boolean; | ||
/** | ||
* Explicitly set to false to not include the schema in the dump. | ||
* Defaults to including the schema. | ||
*/ | ||
schema?: false | SchemaDumpOptions; | ||
/** | ||
* Explicitly set to false to not include data in the dump. | ||
* Defaults to including the data. | ||
*/ | ||
data?: false | DataDumpOptions; | ||
/** | ||
* Explicitly set to false to not include triggers in the dump. | ||
* Defaults to including the triggers. | ||
*/ | ||
trigger?: false | TriggerDumpOptions; | ||
/** | ||
* The list of tables that you want to dump. | ||
* Defaults to all tables (signalled by passing an empty array). | ||
*/ | ||
tables?: Array<string>; | ||
/** | ||
* True to use the `tables` options as a blacklist, false to use it as a whitelist. | ||
* Defaults to false. | ||
*/ | ||
excludeTables?: boolean; | ||
/** | ||
* Explicitly set to false to not include the schema in the dump. | ||
* Defaults to including the schema. | ||
*/ | ||
schema?: false | SchemaDumpOptions; | ||
/** | ||
* Explicitly set to false to not include data in the dump. | ||
* Defaults to including the data. | ||
*/ | ||
data?: false | DataDumpOptions; | ||
/** | ||
* Explicitly set to false to not include triggers in the dump. | ||
* Defaults to including the triggers. | ||
*/ | ||
trigger?: false | TriggerDumpOptions; | ||
} | ||
export interface Options { | ||
/** | ||
* Database connection options | ||
*/ | ||
connection: ConnectionOptions; | ||
/** | ||
* Dump configuration options | ||
*/ | ||
dump?: DumpOptions; | ||
/** | ||
* Set to a path to dump to a file. | ||
* Exclude to just return the string. | ||
*/ | ||
dumpToFile?: string; | ||
/** | ||
* Database connection options | ||
*/ | ||
connection: ConnectionOptions; | ||
/** | ||
* Dump configuration options | ||
*/ | ||
dump?: DumpOptions; | ||
/** | ||
* Set to a path to dump to a file. | ||
* Exclude to just return the string. | ||
*/ | ||
dumpToFile?: string | null; | ||
/** | ||
* Should the output file be compressed (gzip)? | ||
* Defaults to false. | ||
*/ | ||
compressFile?: boolean; | ||
} | ||
export interface ColumnList { | ||
/** | ||
* Key is the name of the column | ||
*/ | ||
[k: string]: { | ||
/** | ||
* The type of the column as reported by the underlying DB. | ||
*/ | ||
type: string; | ||
/** | ||
* True if the column is nullable, false otherwise. | ||
*/ | ||
nullable: boolean; | ||
}; | ||
/** | ||
* Key is the name of the column | ||
*/ | ||
[k: string]: { | ||
/** | ||
* The type of the column as reported by the underlying DB. | ||
*/ | ||
type: string; | ||
/** | ||
* True if the column is nullable, false otherwise. | ||
*/ | ||
nullable: boolean; | ||
}; | ||
} | ||
export interface Table { | ||
/** | ||
* The name of the table. | ||
*/ | ||
name: string; | ||
/** | ||
* The raw SQL schema dump for the table. | ||
* Null if configured to not dump. | ||
*/ | ||
schema: string | null; | ||
/** | ||
* The raw SQL data dump for the table. | ||
* Null if configured to not dump. | ||
*/ | ||
data: string | null; | ||
/** | ||
* The list of column definitions for the table. | ||
*/ | ||
columns: ColumnList; | ||
/** | ||
* An ordered list of columns (for consistently outputing as per the DB definition) | ||
*/ | ||
columnsOrdered: string[]; | ||
/** | ||
* True if the table is actually a view, false otherwise. | ||
*/ | ||
isView: boolean; | ||
/** | ||
* A list of triggers attached to the table | ||
*/ | ||
triggers: string[]; | ||
/** | ||
* The name of the table. | ||
*/ | ||
name: string; | ||
/** | ||
* The raw SQL schema dump for the table. | ||
* Null if configured to not dump. | ||
*/ | ||
schema: string | null; | ||
/** | ||
* The raw SQL data dump for the table. | ||
* Null if configured to not dump. | ||
*/ | ||
data: string | null; | ||
/** | ||
* The list of column definitions for the table. | ||
*/ | ||
columns: ColumnList; | ||
/** | ||
* An ordered list of columns (for consistently outputing as per the DB definition) | ||
*/ | ||
columnsOrdered: Array<string>; | ||
/** | ||
* True if the table is actually a view, false otherwise. | ||
*/ | ||
isView: boolean; | ||
/** | ||
* A list of triggers attached to the table | ||
*/ | ||
triggers: Array<string>; | ||
} | ||
export interface DumpReturn { | ||
/** | ||
* The result of the dump | ||
*/ | ||
dump: { | ||
/** | ||
* The concatenated SQL schema dump for the entire database. | ||
* Null if configured not to dump. | ||
*/ | ||
schema: string | null; | ||
/** | ||
* The concatenated SQL data dump for the entire database. | ||
* Null if configured not to dump. | ||
*/ | ||
data: string | null; | ||
/** | ||
* The concatenated SQL trigger dump for the entire database. | ||
* Null if configured not to dump. | ||
*/ | ||
trigger: string | null; | ||
}; | ||
tables: Table[]; | ||
/** | ||
* The result of the dump | ||
*/ | ||
dump: { | ||
/** | ||
* The concatenated SQL schema dump for the entire database. | ||
* Null if configured not to dump. | ||
*/ | ||
schema: string | null; | ||
/** | ||
* The concatenated SQL data dump for the entire database. | ||
* Null if configured not to dump. | ||
*/ | ||
data: string | null; | ||
/** | ||
* The concatenated SQL trigger dump for the entire database. | ||
* Null if configured not to dump. | ||
*/ | ||
trigger: string | null; | ||
}; | ||
tables: Array<Table>; | ||
} | ||
@@ -363,7 +426,7 @@ export default function main(inputOptions: Options): Promise<DumpReturn>; | ||
### Installation | ||
### Local Installation | ||
Make sure to first install all the required development dependencies: | ||
``` | ||
```shell | ||
yarn | ||
@@ -370,0 +433,0 @@ // or |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100100
2181
447