better_seeder 0.2.1 → 0.2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +53 -72
- data/lib/better_seeder/{generators → builders}/structure.rb +1 -1
- data/lib/better_seeder/version.rb +1 -1
- data/lib/better_seeder.rb +2 -1
- data/lib/generators/better_seeder/structure_generator.rb +15 -0
- metadata +3 -3
- data/lib/generators/better_seeder/structure/structure_generator.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58577f255f87b959dca2b8d422ebf59f8a450ac857b05e5b9b46af1cfcd4fc9e
|
4
|
+
data.tar.gz: 6c4022a220f744d03209ec5ce45c6344f328e2dbe1c46b9a7e6ae9ca323133f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f322c8907aa1db395c3a231823080c50d4825f63f1d89d8dfa52784d0e686fa064b6c57cf8c6fafc830fc650741ca8853c7fb6c45fb248ef31b16c0f94de972
|
7
|
+
data.tar.gz: c9d8b6d14f21db93507ac0a5b856dd832f367fe6aab97043cf7dcb6310043b37fd79ca4a6e86a6a2a2b7368c250f0ccebb0e7e797153d06a1e5687efc2a72130
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# BetterSeeder
|
2
2
|
|
3
|
-
|
3
|
+
BetterSeeder is a Rails gem designed to simplify and centralize your application's seeding process. It offers a flexible system to generate dynamic data, validate it using Dry-schema, enforce uniqueness constraints, load data into the database, and export it in various formats (SQL, CSV, JSON). The configuration is managed through a Rails initializer, while model-specific logic is defined in dedicated structure files.
|
4
4
|
|
5
5
|
---
|
6
6
|
|
@@ -10,17 +10,19 @@
|
|
10
10
|
Define custom data generators for each model in dedicated structure files.
|
11
11
|
|
12
12
|
- **Validation and Uniqueness**
|
13
|
-
Validate generated records using Dry-schema and enforce uniqueness constraints
|
13
|
+
Validate generated records using Dry-schema and enforce uniqueness constraints across one or multiple columns.
|
14
14
|
|
15
15
|
- **Loading & Exporting**
|
16
|
-
|
17
|
-
- Export data as a single SQL INSERT statement, or in CSV or JSON formats.
|
16
|
+
Load generated data directly into your database—supporting parent/child relationships—and export the data as a single SQL INSERT statement, CSV, or JSON file.
|
18
17
|
|
19
18
|
- **Centralized Configuration**
|
20
|
-
|
19
|
+
Customize settings such as `log_language`, `structure_path`, and `preload_path` via a Rails initializer. If no initializer is provided, default settings apply.
|
21
20
|
|
22
|
-
- **
|
23
|
-
|
21
|
+
- **Initializer Installation**
|
22
|
+
An install method creates the necessary initializer file in your Rails application to streamline setup.
|
23
|
+
|
24
|
+
- **Rails Generator**
|
25
|
+
A custom Rails generator scaffolds a structure file template for your models, ensuring a consistent configuration format.
|
24
26
|
|
25
27
|
---
|
26
28
|
|
@@ -42,7 +44,7 @@ bundle install
|
|
42
44
|
|
43
45
|
## Configuration
|
44
46
|
|
45
|
-
BetterSeeder uses a centralized configuration defined in `BetterSeeder.configuration`. You can override the default settings
|
47
|
+
BetterSeeder uses a centralized configuration defined in `BetterSeeder.configuration`. You can override the default settings by creating an initializer. For example, create a file:
|
46
48
|
|
47
49
|
```ruby
|
48
50
|
# config/initializers/better_seeder.rb
|
@@ -55,53 +57,43 @@ BetterSeeder.configure do |config|
|
|
55
57
|
end
|
56
58
|
```
|
57
59
|
|
58
|
-
If these values are set in the initializer, they will be used; otherwise, the gem will
|
60
|
+
If these values are set in the initializer, they will be used; otherwise, the gem will use its default values.
|
59
61
|
|
60
62
|
---
|
61
63
|
|
62
|
-
##
|
64
|
+
## Initializer Installation
|
63
65
|
|
64
|
-
|
66
|
+
To streamline the setup, execute the following command in your Rails console:
|
65
67
|
|
66
68
|
```ruby
|
67
69
|
BetterSeeder.install
|
68
70
|
```
|
69
71
|
|
70
|
-
This command creates (if not already present) the file `config/initializers/better_seeder.rb` with
|
71
|
-
|
72
|
-
```ruby
|
73
|
-
# BetterSeeder initializer
|
74
|
-
BetterSeeder.configure do |config|
|
75
|
-
config.log_language = :en
|
76
|
-
config.structure_path = Rails.root.join('db', 'seed', 'structure')
|
77
|
-
config.preload_path = Rails.root.join('db', 'seed', 'preload')
|
78
|
-
end
|
79
|
-
```
|
72
|
+
This command creates (if not already present) the file `config/initializers/better_seeder.rb` with the necessary configuration.
|
80
73
|
|
81
74
|
---
|
82
75
|
|
83
76
|
## Structure Files
|
84
77
|
|
85
|
-
For each model, create a structure file that centralizes the logic for generating, validating, and configuring seed data. Each structure file should
|
78
|
+
For each model, create a structure file that centralizes the logic for generating, validating, and configuring seed data. Each structure file should include:
|
86
79
|
|
87
80
|
- **`structure`**
|
88
|
-
Returns a hash where each key
|
81
|
+
Returns a hash where each key is an attribute and its value is an array in the format `[type, lambda_generator]`.
|
89
82
|
|
90
83
|
- **`seed_schema` (Optional)**
|
91
|
-
Defines a Dry-schema for validating
|
84
|
+
Defines a Dry-schema for validating generated records.
|
92
85
|
|
93
86
|
- **`seed_config`**
|
94
87
|
Returns a hash with model-specific seeding settings:
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
88
|
+
- `file_name`: The output file name (without extension)
|
89
|
+
- `columns: { excluded: [...] }`: Columns to exclude from the generated data
|
90
|
+
- `generate_data`: Boolean flag indicating whether to generate data dynamically (if false, existing records are used)
|
91
|
+
- `count`: The number of records to generate (default: 10)
|
92
|
+
- `load_data`: Boolean flag indicating whether the generated records should be inserted into the database
|
93
|
+
- `parent`: For child models, specifies the parent model(s) used for injecting foreign keys
|
101
94
|
|
102
95
|
- **`unique_keys` (Optional)**
|
103
|
-
Returns an array of column groups (each group is an array of symbols) that must be unique.
|
104
|
-
For example:
|
96
|
+
Returns an array of column groups (each group is an array of symbols) that must be unique. For example:
|
105
97
|
|
106
98
|
```ruby
|
107
99
|
def self.unique_keys
|
@@ -111,13 +103,10 @@ For each model, create a structure file that centralizes the logic for generatin
|
|
111
103
|
|
112
104
|
### Example Structure File
|
113
105
|
|
114
|
-
For a generic model `MyModel` in the namespace `MyNamespace`, create a file at `db/seed/structure/my_namespace/my_model_structure.rb`:
|
115
|
-
|
116
106
|
```ruby
|
117
107
|
# db/seed/structure/my_namespace/my_model_structure.rb
|
118
108
|
module MyNamespace
|
119
109
|
class MyModelStructure < BetterSeeder::StructureBase
|
120
|
-
# Defines generators for each attribute.
|
121
110
|
def self.structure
|
122
111
|
{
|
123
112
|
name: [:string, -> { FFaker::Name.name }],
|
@@ -126,7 +115,6 @@ module MyNamespace
|
|
126
115
|
}
|
127
116
|
end
|
128
117
|
|
129
|
-
# Optional: Validate generated records using Dry-schema.
|
130
118
|
def self.seed_schema
|
131
119
|
Dry::Schema.Params do
|
132
120
|
required(:name).filled(:string)
|
@@ -135,7 +123,6 @@ module MyNamespace
|
|
135
123
|
end
|
136
124
|
end
|
137
125
|
|
138
|
-
# Specific seeding configuration for MyModel.
|
139
126
|
def self.seed_config
|
140
127
|
{
|
141
128
|
file_name: 'my_model_seed',
|
@@ -147,7 +134,6 @@ module MyNamespace
|
|
147
134
|
}
|
148
135
|
end
|
149
136
|
|
150
|
-
# Optional: Uniqueness constraints; for example, email must be unique.
|
151
137
|
def self.unique_keys
|
152
138
|
[[:email]]
|
153
139
|
end
|
@@ -162,19 +148,19 @@ end
|
|
162
148
|
When you call `BetterSeeder.magic` with a configuration that contains an array of model names (as strings), the gem will:
|
163
149
|
|
164
150
|
1. **Load Structure Files**
|
165
|
-
|
151
|
+
Retrieve the corresponding structure file from the directory defined by `BetterSeeder.configuration.structure_path` for each model.
|
166
152
|
|
167
153
|
2. **Retrieve Seeding Configurations**
|
168
|
-
|
154
|
+
Invoke the model's `seed_config` method to obtain its specific settings.
|
169
155
|
|
170
156
|
3. **Generate or Retrieve Records**
|
171
|
-
|
157
|
+
Use the `structure` method to generate data dynamically (or fetch existing records) and validate them using `seed_schema` if defined. Uniqueness is enforced via `unique_keys`.
|
172
158
|
|
173
159
|
4. **Handle Parent/Child Relationships**
|
174
|
-
|
160
|
+
Automatically inject foreign keys into child models using records from parent models.
|
175
161
|
|
176
|
-
5. **Load and Export**
|
177
|
-
If enabled (`load_data: true`), the generated records are inserted into the database and
|
162
|
+
5. **Load and Export Data**
|
163
|
+
If enabled (`load_data: true`), the generated records are inserted into the database and exported in the specified format (SQL, CSV, or JSON). Export files are saved in the directory specified by `BetterSeeder.configuration.preload_path`.
|
178
164
|
|
179
165
|
### Example Usage
|
180
166
|
|
@@ -190,24 +176,17 @@ BetterSeeder.magic(
|
|
190
176
|
)
|
191
177
|
```
|
192
178
|
|
193
|
-
This command processes each model by:
|
194
|
-
|
195
|
-
- Reading its structure file and retrieving its configuration via `seed_config`.
|
196
|
-
- Generating or fetching data according to the specified rules.
|
197
|
-
- Inserting the data into the database (if `load_data` is enabled).
|
198
|
-
- Exporting the data as an SQL file (or CSV/JSON, depending on `export_type`).
|
199
|
-
|
200
179
|
---
|
201
180
|
|
202
|
-
##
|
181
|
+
## Rails Generator
|
203
182
|
|
204
|
-
|
183
|
+
A custom Rails generator is available to scaffold a structure file template for your models. Execute the following command:
|
205
184
|
|
206
185
|
```bash
|
207
186
|
rails generate better_seeder:structure MyNamespace::MyModel
|
208
187
|
```
|
209
188
|
|
210
|
-
This command creates a structure file template in the appropriate subdirectory under `db/seed/structure`. The generated
|
189
|
+
This command creates a structure file template in the appropriate subdirectory under `db/seed/structure`. The generated template includes placeholders for:
|
211
190
|
|
212
191
|
- Attribute generators (via the `structure` method)
|
213
192
|
- Validation schema (via the `seed_schema` method)
|
@@ -219,21 +198,18 @@ This command creates a structure file template in the appropriate subdirectory u
|
|
219
198
|
```ruby
|
220
199
|
module MyNamespace
|
221
200
|
class MyModelStructure < BetterSeeder::StructureBase
|
222
|
-
# Defines generators for each attribute.
|
223
201
|
def self.structure
|
224
202
|
{
|
225
203
|
attribute_name: [:string, -> { "your value" }]
|
226
204
|
}
|
227
205
|
end
|
228
206
|
|
229
|
-
# Optional: Validate generated records using Dry-schema.
|
230
207
|
def self.seed_schema
|
231
208
|
Dry::Schema.Params do
|
232
209
|
required(:attribute_name).filled(:string)
|
233
210
|
end
|
234
211
|
end
|
235
212
|
|
236
|
-
# Specific seeding configuration for MyModel.
|
237
213
|
def self.seed_config
|
238
214
|
{
|
239
215
|
file_name: 'my_model_seed',
|
@@ -245,7 +221,6 @@ module MyNamespace
|
|
245
221
|
}
|
246
222
|
end
|
247
223
|
|
248
|
-
# Optional: Uniqueness constraints.
|
249
224
|
def self.unique_keys
|
250
225
|
[]
|
251
226
|
end
|
@@ -253,27 +228,33 @@ module MyNamespace
|
|
253
228
|
end
|
254
229
|
```
|
255
230
|
|
256
|
-
This generator
|
231
|
+
This generator ensures a consistent structure for your seeding configuration across models.
|
257
232
|
|
258
|
-
|
233
|
+
---
|
259
234
|
|
260
|
-
|
261
|
-
|
262
|
-
|
235
|
+
## Contact & Feature Requests
|
236
|
+
|
237
|
+
For suggestions, bug reports, or to report new feature requests, please reach out via email at: **alessio.bussolari@pandev.it**.
|
263
238
|
|
264
239
|
---
|
265
240
|
|
266
|
-
##
|
241
|
+
## Upcoming Features
|
267
242
|
|
268
|
-
|
243
|
+
The development team is continuously enhancing BetterSeeder. Current efforts include:
|
269
244
|
|
270
|
-
- **
|
271
|
-
|
245
|
+
- **Additional Export Formats:** Expanding beyond SQL, CSV, and JSON to offer more options.
|
246
|
+
- **Enhanced Rails Associations Integration:** Improving support for complex parent/child relationships.
|
247
|
+
- **Expanded Configuration Options:** Providing more granular control over the seeding process.
|
248
|
+
- **Performance Optimizations:** Refining data generation algorithms for faster processing.
|
249
|
+
|
250
|
+
---
|
251
|
+
|
252
|
+
## Conclusion
|
272
253
|
|
273
|
-
|
274
|
-
Define generation, validation, and configuration logic for each model in dedicated structure files.
|
254
|
+
BetterSeeder provides a modular, configurable, and extensible system for seeding your Rails application's data. Its key benefits include:
|
275
255
|
|
276
|
-
- **
|
277
|
-
|
256
|
+
- **Centralized Configuration:** Manage settings through a simple Rails initializer.
|
257
|
+
- **Modular Structure Files:** Define data generation, validation, and configuration logic on a per-model basis.
|
258
|
+
- **Seamless Data Handling:** Efficiently generate, validate, load, and export seed data while supporting complex relationships.
|
278
259
|
|
279
|
-
For further details or
|
260
|
+
For further details or to contribute, please refer to the official repository or documentation.
|
data/lib/better_seeder.rb
CHANGED
@@ -6,6 +6,7 @@ require_relative "better_seeder/exporters/base"
|
|
6
6
|
require_relative "better_seeder/exporters/json"
|
7
7
|
require_relative "better_seeder/exporters/csv"
|
8
8
|
require_relative "better_seeder/exporters/sql"
|
9
|
+
require_relative "better_seeder/builders/structure"
|
9
10
|
|
10
11
|
module BetterSeeder
|
11
12
|
class Configuration
|
@@ -110,7 +111,7 @@ module BetterSeeder
|
|
110
111
|
end
|
111
112
|
|
112
113
|
def self.generate_structure(model_name: )
|
113
|
-
BetterSeeder::
|
114
|
+
BetterSeeder::Builders::Structure.generate(model_name)
|
114
115
|
end
|
115
116
|
|
116
117
|
private
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# lib/generators/better_seeder/structure_generator.rb
|
2
|
+
require "rails/generators"
|
3
|
+
|
4
|
+
module BetterSeeder
|
5
|
+
class StructureGenerator < Rails::Generators::NamedBase
|
6
|
+
# Optionally, if you have a template directory, you can set it here:
|
7
|
+
# source_root File.expand_path("templates", __dir__)
|
8
|
+
|
9
|
+
def create_structure_file
|
10
|
+
say_status("info", "Generating structure file for #{name}", :green)
|
11
|
+
file_path = BetterSeeder.generate_structure(model_name: name)
|
12
|
+
say_status("info", "Structure file created at #{file_path}", :green)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_seeder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.1
|
4
|
+
version: 0.2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alessio_bussolari
|
@@ -133,17 +133,17 @@ files:
|
|
133
133
|
- LICENSE.txt
|
134
134
|
- README.md
|
135
135
|
- lib/better_seeder.rb
|
136
|
+
- lib/better_seeder/builders/structure.rb
|
136
137
|
- lib/better_seeder/configuration.rb
|
137
138
|
- lib/better_seeder/exporters/base.rb
|
138
139
|
- lib/better_seeder/exporters/csv.rb
|
139
140
|
- lib/better_seeder/exporters/json.rb
|
140
141
|
- lib/better_seeder/exporters/sql.rb
|
141
142
|
- lib/better_seeder/farms/farmer.rb
|
142
|
-
- lib/better_seeder/generators/structure.rb
|
143
143
|
- lib/better_seeder/structure/utils.rb
|
144
144
|
- lib/better_seeder/utils.rb
|
145
145
|
- lib/better_seeder/version.rb
|
146
|
-
- lib/generators/better_seeder/
|
146
|
+
- lib/generators/better_seeder/structure_generator.rb
|
147
147
|
homepage: https://github.com/alessiobussolari/better_seeder
|
148
148
|
licenses:
|
149
149
|
- MIT
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# lib/generators/better_seeder/structure/structure_generator.rb
|
2
|
-
require "rails/generators"
|
3
|
-
|
4
|
-
module BetterSeeder
|
5
|
-
module Structure
|
6
|
-
class StructureGenerator < Rails::Generators::NamedBase
|
7
|
-
# Optionally, if you have a template directory, you can set it here:
|
8
|
-
# source_root File.expand_path("templates", __dir__)
|
9
|
-
|
10
|
-
def create_structure_file
|
11
|
-
say_status("info", "Generating structure file for #{name}", :green)
|
12
|
-
file_path = BetterSeeder.generate_structure(model_name: name)
|
13
|
-
say_status("info", "Structure file created at #{file_path}", :green)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|