Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeljesus committed Feb 23, 2018
1 parent 4aec9c7 commit aeaacd1
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 57 deletions.
100 changes: 43 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,18 @@ All you need to have is a simple configuration file where you're going to define

Here is an example of how the config file should look like:

Dump all users based on the `match` field and use it for dumping their related orders:
```toml
[[Tables]]
# Dump only matching users
Name = "users"
[Tables.Filter]
# Behind the scenes it will generate the following sql query:
# SELECT users.* FROM users
# WHERE users.id IN ('39240e9f-ae09-4e95-9fd0-a712035c8ad7', '66a45c1b-19af-4ab5-8747-1b0e2d79339d')
Match = "users.id IN ('39240e9f-ae09-4e95-9fd0-a712035c8ad7', '66a45c1b-19af-4ab5-8747-1b0e2d79339d')"

[[Tables]]
# Dump only orders which are related to the matching users
Name = "orders"
[Tables.Filter]
# Behind the scenes it will generate the following sql query:
# SELECT orders.* FROM orders
# JOIN users ON orders.user_id = users.id
# WHERE users.id IN ('39240e9f-ae09-4e95-9fd0-a712035c8ad7', '66a45c1b-19af-4ab5-8747-1b0e2d79339d')
# GROUP BY orders.id
Match = "users.id IN ('39240e9f-ae09-4e95-9fd0-a712035c8ad7', '66a45c1b-19af-4ab5-8747-1b0e2d79339d')"
[[Tables.Relationships]]
ReferencedTable = "users"
ReferencedKey = "id"
ForeignKey = "user_id"
```

Additionally you can dump all orders based on the `match` field and use it for dumping all related users:
```toml
[[Tables]]
# Dump only matching orders
Name = "orders"
[Tables.Filter]
# Behind the scenes it will generate the following query:
# SELECT orders.* FROM orders
# WHERE orders.created_at BETWEEN '2018-01-01' AND now()
Match = "orders.created_at BETWEEN '2018-01-01' AND now()"

[[Tables]]
# Dump only users which are related to the orders
Name = "users"
[Tables.Anonymise]
email = "EmailAddress"
username = "FirstName"
password = "SimplePassword"
[Tables.Filter]
# Behind the scenes it will generate the following query:
# SELECT users.* FROM users
# JOIN orders ON users.id = orders.user_id
# WHERE orders.created_at BETWEEN '2018-01-01' AND now()
# GROUP BY users.id
Match = "orders.created_at BETWEEN '2018-01-01' AND now()"
[[Tables.Relationships]]
ReferencedTable = "users"
ReferencedKey = "id"
ForeignKey = "user_id"
Match = "users.status = 'active'"
Limit = 10
[Tables.Filter.Sorts]
created_at = "desc"
```

After you have created the file just run:
Expand All @@ -88,6 +48,41 @@ klepto steal \
--read-max-conns=8
```

## Ignore data

Additionally you can dump the database structure without importing data
```toml
[[Tables]]
Name = "logs"
IgnoreData = true
```

## Relationships

Dump the latest 100 users with it's orders
```toml
[[Tables]]
Name = "users"
[Tables.Filter]
Limit = 100
[Tables.Filter.Sorts]
created_at = "desc"

[[Tables]]
Name = "orders"
PrimaryKey = "id"
[[Tables.Relationships]]
ForeignKey = "user_id"
ReferencedTable = "users"
ReferencedKey = "id"
[Tables.Filter]
Limit = 100
[Tables.Filter.Sorts]
created_at = "desc"
```

See [examples](./examples) for more.

## Prerequisites

Klepto tries to keep external dependencies to a minimum, but some functionalities requires some dependencies. Here is a list:
Expand Down Expand Up @@ -154,15 +149,6 @@ $ go get github.com/ungerik/pkgreflect
$ fake master pkgreflect -notypes -novars -norecurs vendor/github.com/icrowley/fake/
```

## Ignore data

Additionally you can dump the database structure without importing data
```toml
[[Tables]]
Name = "logs"
IgnoreData = true
```

## Contributing

Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
Expand Down
58 changes: 58 additions & 0 deletions examples/user-orders.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[[Tables]]
Name = "users"
[Tables.Anonymise]
email = "EmailAddress"
firstName = "FirstName"
password = "SimplePassword"
[Tables.Filter]
# import the last 100 active created users
Match = "users.active = true"
Limit = 100
[Tables.Filter.Sorts]
created_at = "desc"

[[Tables]]
# Dump only orders which are related to the matching users
Name = "orders"
PrimaryKey = "id"
# Behind the scenes it will generate the following sql query:
# SELECT orders.* FROM orders
# JOIN users ON users.id = orders.user_id
# WHERE users.status = 'active'
# GROUP BY order_items.id
[[Tables.Relationships]]
Table = "orders"
ForeignKey = "user_id"
ReferencedTable = "users"
ReferencedKey = "id"
[Tables.Filter]
Match = "users.active = true"
Limit = 100
[Tables.Filter.Sorts]
created_at = "desc"

[[Tables]]
# Dump only order items which are related to the matching users orders
Name = "order_items"
PrimaryKey = "id"
# Behind the scenes it will generate the following sql query:
# SELECT order_items.* FROM order_items
# JOIN orders ON orders.id = order_items.order_id
# JOIN users ON users.id = orders.user_id
# WHERE users.status = 'active'
# GROUP BY order_items.id
[[Tables.Relationships]]
Table = "order_items"
ForeignKey = "order_id"
ReferencedTable = "orders"
ReferencedKey = "id"
[[Tables.Relationships]]
Table = "orders"
ForeignKey = "user_id"
ReferencedTable = "users"
ReferencedKey = "id"
[Tables.Filter]
Match = "users.active = true"
Limit = 100
[Tables.Filter.Sorts]
created_at = "desc"
16 changes: 16 additions & 0 deletions examples/user.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[[Tables]]
# Dump the database structure without importing data
Name = "user_history"
IgnoreData = true

[[Tables]]
Name = "users"
[Tables.Anonymise]
email = "EmailAddress"
username = "FirstName"
password = "SimplePassword"
[Tables.Filter]
# import the last 100 created users
Limit = 100
[Tables.Filter.Sorts]
created_at = "desc"

0 comments on commit aeaacd1

Please sign in to comment.