Skip to content

Commit 8397cba

Browse files
committed
Update README.md
1 parent 2aa56ae commit 8397cba

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

README.md

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Default installation
2323
pip install sqlalchemyseed
2424
```
2525

26-
When using yaml to loading entities from yaml files. Execute this command to install necessary dependencies
26+
When using yaml to load entities from yaml files, execute this command to install necessary dependencies
2727

2828
```shell
2929
pip install sqlalchemyseed[yaml]
@@ -37,23 +37,24 @@ Required dependencies
3737

3838
Optional dependencies
3939

40-
- PyYAML>=5.4.0
40+
- yaml
41+
- PyYAML>=5.4.0
4142

4243
## Getting Started
4344

4445
```python
4546
# main.py
46-
from sqlalchemyseed import load_entities_from_json, Seeder
47+
from sqlalchemyseed import load_entities_from_json
48+
from sqlalchemyseed import Seeder
4749
from db import session
4850

4951
# load entities
50-
entities = load_entities_from_json('tests/test_data.json')
52+
entities = load_entities_from_json('data.json')
5153

5254
# Initializing Seeder
53-
seeder = Seeder() # or Seeder(session),
55+
seeder = Seeder(session)
5456

5557
# Seeding
56-
seeder.session = session # assign session if no session assigned before seeding
5758
seeder.seed(entities)
5859

5960
# Committing
@@ -62,16 +63,16 @@ session.commit() # or seeder.session.commit()
6263

6364
## Seeder vs. HybridSeeder
6465

65-
| Features & Options | Seeder | HybridSeeder |
66-
| :------------------------------------------------------------ | :----------------- | :----------------- |
67-
| Support `model` and `data` keys | :heavy_check_mark: | :heavy_check_mark: |
68-
| Support `model` and `filter` keys | :x: | :heavy_check_mark: |
69-
| Optional argument `add_to_session=False` in the `seed` method | :heavy_check_mark: | :x: |
66+
| Features & Options | Seeder | HybridSeeder |
67+
| :------------------------------------------------------------ | :----- | :----------- |
68+
| Support `model` and `data` keys | ✔️ | ✔️ |
69+
| Support `model` and `filter` keys | | ✔️ |
70+
| Optional argument `add_to_session=False` in the `seed` method | ✔️ | |
7071

7172
## When to use HybridSeeder and 'filter' key field?
7273

73-
Assuming that `Child(age=5)` exists in the database or session, then we should use *filter* instead of *data*, the
74-
values of *filter* will query from the database or session, and assign it to the `Parent.child`
74+
Assuming that `Child(age=5)` exists in the database or session, then we should use `filter` instead of `data`, the
75+
values of `filter` will query from the database or session, and assign it to the `Parent.child`
7576

7677
```python
7778
from sqlalchemyseed import HybridSeeder
@@ -89,8 +90,10 @@ data = {
8990
}
9091
}
9192

92-
# When seeding instances that has 'filter' key, then use HybridSeeder, otherwise use Seeder.
93-
# ref_prefix can be changed according to your needs, defaults to '!'
93+
# When seeding instances that has 'filter' key,
94+
# then use HybridSeeder, otherwise use Seeder.
95+
# ref_prefix can be changed according to your needs,
96+
# defaults to '!'
9497
seeder = HybridSeeder(session, ref_prefix='!')
9598
seeder.seed(data)
9699

@@ -99,14 +102,21 @@ session.commit() # or seeder.sesssion.commit()
99102

100103
## Relationships
101104

102-
In adding a reference attribute, add prefix **!** or to the key in order to identify it.
103-
If you want '@' as prefix, you can just specify it to what seeder you use by adding ref_prefix='@' in the argument when instantiating the seeder in order for the seeder to identify the referencing attributes
105+
In adding a reference attribute, add prefix to the key in order to identify it. Default prefix is `!`.
106+
107+
Reference attribute can either be foreign key or relationship attribute. See examples below.
108+
109+
### Customizing prefix
110+
111+
If you want '@' as prefix, you can just specify it to what seeder you use by adding ref_prefix='@' in the argument like this `seeder = Seeder(session, ref_prefix='@')`, in order for the seeder to identify the referencing attributes
104112

105113
### Referencing relationship object or a foreign key
106114

107115
If your class don't have a relationship attribute but instead a foreign key attribute you can use it the same as how you
108116
did it on a relationship attribute
109117

118+
**Note**: `model` can be removed if it is a reference attribute.
119+
110120
```python
111121
from sqlalchemyseed import HybridSeeder
112122
from db import session
@@ -150,7 +160,7 @@ seeder.session.commit() # or session.commit()
150160
### No Relationship
151161

152162
```json5
153-
// test_data.json
163+
// data.json
154164
[
155165
{
156166
"model": "models.Person",
@@ -196,7 +206,7 @@ seeder.session.commit() # or session.commit()
196206
}
197207
},
198208
// or this, if you want to add relationship that exists
199-
// in your database use 'filter' instead of 'obj'
209+
// in your database use 'filter' instead of 'data'
200210
{
201211
"model": "models.Person",
202212
"data": {
@@ -306,7 +316,9 @@ data:
306316

307317
### CSV
308318

309-
data.csv
319+
In line one, name and age, are attributes of a model that will be specified when loading the file.
320+
321+
`people.csv`
310322

311323
```text
312324
name, age
@@ -316,10 +328,13 @@ Juan Dela Cruz, 21
316328

317329
To load a csv file
318330

319-
`load_entities_from_csv("data.csv", models.Person)`
320-
321-
or
331+
`main.py`
322332

323-
`load_entities_from_csv("data.csv", "models.Person")`
333+
```python
334+
# second argument, model, accepts class
335+
load_entities_from_csv("people.csv", models.Person)
336+
# or string
337+
load_entities_from_csv("people.csv", "models.Person")
338+
```
324339

325340
**Note**: Does not support relationships

0 commit comments

Comments
 (0)