Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
djunehor committed Sep 4, 2020
0 parents commit d853711
Show file tree
Hide file tree
Showing 30 changed files with 2,276 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/.scrutinizer.yml export-ignore
/.styleci.yml export-ignore
/tests export-ignore
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: shagital
38 changes: 38 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Execute test
run: npm run test

- name: Send Slack notification
uses: 8398a7/action-slack@v2
if: failure()
with:
status: ${{ job.status }}
author_name: ${{ github.actor }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
docs
node_modules
.idea
.DS_Store
19 changes: 19 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
filter:
excluded_paths: [tests/*]

checks:
node:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true

1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: node
Empty file added CHANGELOG.md
Empty file.
32 changes: 32 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/shagital/db-dumper).


## Pull Requests

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **Create feature branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.


## Running Tests

``` bash
$ composer test
```


**Happy coding**!
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Shagital <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
232 changes: 232 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
# Dump the contents of a database

This repo contains an easy to use class to dump a database using Nodejs. Currently MySQL, PostgreSQL, SQLite and MongoDB are supported. Behind
the scenes, `mysqldump`, `pg_dump`, `sqlite3` and `mongodump` are used.

Here are simple examples of how to create a database dump with different drivers:

**MySQL**

```js
const { MySql } = require('@shagital/db-dumper');
MySql.create().setDbName('dbName').setUserName('userName').setPassword('password').dumpToFile('./dump.sql');
```

**PostgreSQL**

```js
const { PostgreSql } = require('@shagital/db-dumper');
PostgreSql.create().setDbName('dbName').setUserName('userName').setPassword('password').dumpToFile('./dump.sql');
```

**SQLite**

```js
const { Sqlite } = require('@shagital/db-dumper');
Sqlite.create().setDbName('path_to_sqlite.sqlite').dumpToFile('dump.sql');
```

**MongoDB**

```js
const { MongoDb } = require('@shagital/db-dumper');
MongoDb.create().setDbName('dbName').setCollection('collectionName').dumpToFile('./dump.gz');
```

## Requirements
For dumping MySQL-db's `mysqldump` should be installed.

For dumping PostgreSQL-db's `pg_dump` should be installed.

For dumping SQLite-db's `sqlite3` should be installed.

For dumping MongoDB-db's `mongodump` should be installed.

## Installation

You can install the package via composer:
``` bash
npm install @shagital/db-dumper
```
Or with yarn
``` bash
yarn add @shagital/db-dumper
```

### Dump specific tables

Using an array:

```js
const { MySql } = require('@shagital/db-dumper');
MySql.create()
.setDbName(databaseName)
.setUserName(userName)
.setPassword(password)
.setIncludedTables(['table1', 'table2', 'table3'])
.dumpToFile('dump.sql');
```
Using a string:

```js
const { MySql } = require('@shagital/db-dumper');
MySql.create()
.setDbName(databaseName)
.setUserName(userName)
.setPassword(password)
.setIncludedTables('table1,table2,table3')
.dumpToFile('dump.sql');
```

### Excluding tables from the dump

Using an array:

```js
const { MySql } = require('@shagital/db-dumper');
MySql.create()
.setDbName(databaseName)
.setUserName(userName)
.setPassword(password)
.setExcludedTables(['table1', 'table2', 'table3'])
.dumpToFile('dump.sql');
```
Using a string:

```js
const { MySql } = require('@shagital/db-dumper');
MySql.create()
.setDbName(databaseName)
.setUserName(userName)
.setPassword(password)
.setExcludedTables('table1','table2','table3')
.dumpToFile('dump.sql');
```

### Do not write CREATE TABLE statements that create each dumped table.
```js
const { MySql } = require('@shagital/db-dumper');
let dumpCommand = MySql.create()
.setDbName('dbname')
.setUserName('username')
.setPassword('password')
.doNotCreateTables()
.getDumpCommand('dump.sql', 'credentials.txt');
```

### Adding extra options
If you want to add an arbitrary option to the dump command you can use `addExtraOption`

```js
const { MySql } = require('@shagital/db-dumper');
let dumpCommand = MySql.create()
.setDbName('dbname')
.setUserName('username')
.setPassword('password')
.addExtraOption('--xml')
.getDumpCommand('dump.sql', 'credentials.txt');
```

If you're working with MySql you can set the database name using `--databases` as an extra option. This is particularly useful when used in conjunction with the `--add-drop-database` `mysqldump` option (see the [mysqldump docs](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_add-drop-database)).

```js
const { MySql } = require('@shagital/db-dumper');
let dumpCommand = MySql.create()
.setUserName('username')
.setPassword('password')
.addExtraOption('--databases dbname')
.addExtraOption('--add-drop-database')
.getDumpCommand('dump.sql', 'credentials.txt');
```

With MySql, you also have the option to use the `--all-databases` extra option. This is useful when you want to run a full backup of all the databases in the specified MySQL connection.

```js
const { MySql } = require('@shagital/db-dumper');
let dumpCommand = MySql.create()
.setUserName('username')
.setPassword('password')
.addExtraOption('--all-databases')
.getDumpCommand('dump.sql', 'credentials.txt');
```

Please note that using the `.addExtraOption('--databases dbname')` or `.addExtraOption('--all-databases')` will override the database name set on a previous `.setDbName()` call.

### Using compression
If you want to compress the outputted file, you can use one of the compressors and the resulted dump file will be compressed.

There is one compressor that comes out of the box: `GzipCompressor`. It will compress your db dump with `gzip`. Make sure `gzip` is installed on your system before using this.

```js
const { MySql, GzipCompressor } = require('@shagital/db-dumper');

let dumpCommand = MySql.create()
.setDbName('dbname')
.setUserName('username')
.setPassword('password')
.useCompressor(new GzipCompressor())
.dumpToFile('dump.sql.gz');
```

### Creating your own compressor

You can create you own compressor implementing the `Compressor` class. Here's how that class looks like:

```js

class Compressor
{
useCommand() {};

useExtension() {};
}
```

The `useCommand` should simply return the compression command the db dump will get pumped to. Here's the implementation of `GzipCompression`.

```js
const { Compressor } = require('@shagital/db-dumper');

class GzipCompressor extends Compressor
{
useCommand()
{
return 'gzip';
}

useExtension()
{
return 'gz';
}
}
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Testing

``` bash
npm run test

// or with yarn
yarn test
```

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Credits
This Nodejs was heavily influenced by [Spatie `db-dumper` PHP package](https://github.com/spatie/db-dumper)
- [Zacchaeus Bolaji](https://github.com/djunehor)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
Loading

0 comments on commit d853711

Please sign in to comment.