Skip to content

Commit

Permalink
Add readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
kfirba committed Jan 12, 2018
1 parent 58ca9fd commit 52ecddd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Import Query Generator

An efficient query generator for mass resource import, **distinguishing between new records and records to update**. This library uses MySQL's `ON DUPLICATE KEY UPDATE` feature.

## Preface

I highly recommend you at least skim through [my blog about this library](https://kfirba.me/blog/performant-mass-update-or-create-strategy-for-data-imports) to get a better understanding of this library.

## Installation

You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):

```bash
composer require kfirba/import-query-generator
```

## Usage

```php
use Kfirba\QueryGenerator;

$table = 'users';
$data = [
['name' => 'John', 'email' => '[email protected]', 'password' => 'hashed_password', 'created_at' => date('Y-m-d'), 'updated_at' => date('Y-m-d')],
['name' => 'Jane', 'email' => '[email protected]', 'password' => 'hashed_password', 'created_at' => date('Y-m-d'), 'updated_at' => date('Y-m-d')],
['name' => 'Susy', 'email' => '[email protected]', 'password' => 'hashed_password', 'created_at' => date('Y-m-d'), 'updated_at' => date('Y-m-d')],
];
$excludedColumnsFromUpdate = ['password', 'created_at'];

$queryObject = (new QueryGenerator)->generate($table, $data, $excludedColumnsFromUpdate);

$queryObject->getQuery();
// -> "insert into `users` (`name`,`email`,`password`,`created_at`,`updated_at`) values (?,?,?,?,?),(?,?,?,?,?),(?,?,?,?,?) on duplicate key update `name`=VALUES(`name`),`email`=VALUES(`email`),`updated_at`=VALUES(`updated_at`)"

$queryObject->getBindings();
// -> ['John', '[email protected]', 'hashed_password', '2018-01-12', '2018-01-12', 'Jane', '[email protected]', 'hashed_password', '2018-01-12', '2018-01-12', 'Susy', '[email protected]', 'hashed_password', '2018-01-12', '2018-01-12']
```

As you may have noticed, the generator defaults to `column=VALUES(column)` since this is usually what we use when we attempt to bulk import some data.
Need another behavior? You can submit a PR or just [open an issue](https://github.com/kfirba/import-query-generator/issues/new) and we can talk about it 🤓.

## License
This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kfirba/import-query-generator",
"description": "An efficient query generator for mass resource imports, distinguishing between new records and records to update.",
"description": "An efficient query generator for mass resource import, distinguishing between new records and records to update.",
"type": "library",
"license": "MIT",
"authors": [
Expand Down

0 comments on commit 52ecddd

Please sign in to comment.