schema-seed 0.1.2 → 0.1.4
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.
- package/README.md +85 -120
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -18,67 +18,53 @@ A production-ready tool to seed your database with realistic, deterministic data
|
|
|
18
18
|
npm install -g schema-seed
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
##
|
|
21
|
+
## Supported Databases
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
| Database | Adapter Package | Connection String Example |
|
|
24
|
+
| :--- | :--- | :--- |
|
|
25
|
+
| **PostgreSQL** | `schema-seed-adapter-postgres` | `postgres://user:pass@localhost:5432/db` |
|
|
26
|
+
| **MySQL** | `schema-seed-adapter-mysql` | `mysql://user:pass@localhost:3306/db` |
|
|
27
|
+
| **SQLite** | `schema-seed-adapter-sqlite` | `sqlite://path/to/database.db` |
|
|
28
|
+
| **SQL Server** | `schema-seed-adapter-mssql` | `sqlserver://user:pass@localhost:1433?database=db` |
|
|
29
|
+
| **Oracle** | `schema-seed-adapter-oracle` | `oracle://user:pass@localhost:1521/service_name` |
|
|
30
|
+
| **MongoDB** | `schema-seed-adapter-mongodb` | `mongodb://localhost:27017/db` |
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Database-Specific Usage
|
|
35
|
+
|
|
36
|
+
### 🐘 PostgreSQL
|
|
37
|
+
Seed all tables in a Postgres database:
|
|
24
38
|
```bash
|
|
25
|
-
schema-seed seed --db "postgres://
|
|
39
|
+
schema-seed seed --db "postgres://postgres:password@localhost:5432/my_db" --all
|
|
26
40
|
```
|
|
27
41
|
|
|
28
|
-
###
|
|
42
|
+
### 🐬 MySQL
|
|
43
|
+
Seed 50 rows into the `users` table:
|
|
29
44
|
```bash
|
|
30
|
-
schema-seed seed --db "mysql://
|
|
45
|
+
schema-seed seed --db "mysql://root:password@127.0.0.1:3306/my_db" --table users --rows 50
|
|
31
46
|
```
|
|
32
47
|
|
|
33
|
-
###
|
|
34
|
-
|
|
48
|
+
### 📁 SQLite
|
|
49
|
+
Seed a local SQLite file:
|
|
35
50
|
```bash
|
|
36
|
-
schema-seed seed --db "sqlite
|
|
51
|
+
schema-seed seed --db "sqlite://./dev.db" --all
|
|
37
52
|
```
|
|
38
53
|
|
|
39
|
-
###
|
|
40
|
-
|
|
54
|
+
### 🏢 Microsoft SQL Server (MSSQL)
|
|
55
|
+
Seed with identity insert support:
|
|
41
56
|
```bash
|
|
42
|
-
schema-seed seed --db "
|
|
57
|
+
schema-seed seed --db "sqlserver://sa:Password123!@localhost:1433?database=master" --all
|
|
43
58
|
```
|
|
44
59
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
import { defineConfig } from 'schema-seed';
|
|
51
|
-
|
|
52
|
-
export default defineConfig({
|
|
53
|
-
db: "postgres://localhost/mydb",
|
|
54
|
-
rows: 10,
|
|
55
|
-
overrides: {
|
|
56
|
-
users: {
|
|
57
|
-
// Custom logic
|
|
58
|
-
email: ({ i }) => `user${i}@example.com`,
|
|
59
|
-
// Weighted enum
|
|
60
|
-
status: { enum: ["active", "blocked"], weights: [95, 5] },
|
|
61
|
-
// Date range
|
|
62
|
-
created_at: { dateBetween: ["2024-01-01", "2025-12-31"] },
|
|
63
|
-
// Literal value
|
|
64
|
-
role: 'user'
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
hooks: {
|
|
68
|
-
beforeInsert: async (table, rows) => {
|
|
69
|
-
console.log(`Preparing ${rows.length} rows for ${table}`);
|
|
70
|
-
return rows;
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
plugins: ["@schema-seed/plugin-ecommerce"]
|
|
74
|
-
});
|
|
60
|
+
### 🔴 Oracle
|
|
61
|
+
Seed an Oracle database:
|
|
62
|
+
```bash
|
|
63
|
+
schema-seed seed --db "oracle://system:password@localhost:1521/xe" --all
|
|
75
64
|
```
|
|
76
65
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
`schema-seed` supports MongoDB seeding exclusively through a `seed.config.ts` file. Since MongoDB is schema-less, you must define the structure of the documents you want to generate.
|
|
80
|
-
|
|
81
|
-
### Example Configuration
|
|
66
|
+
### 🍃 MongoDB (Config-Based)
|
|
67
|
+
Since MongoDB is schema-less, you must define your collection structures in a `seed.config.ts` file.
|
|
82
68
|
|
|
83
69
|
```typescript
|
|
84
70
|
// seed.config.ts
|
|
@@ -86,101 +72,80 @@ import { defineConfig } from 'schema-seed';
|
|
|
86
72
|
|
|
87
73
|
export default defineConfig({
|
|
88
74
|
dbType: "mongodb",
|
|
89
|
-
seed: 123, // Deterministic random seed
|
|
90
|
-
// The seed option controls how random data is generated. When a seed value is provided (for example seed: 123), Schema-Seed will generate the same data every time you run the seeder with the same configuration. This makes database seeding deterministic and reproducible, which is especially useful for team environments, CI pipelines, debugging, and demos. Changing the seed value will produce a different, but still consistent, dataset. If no seed is provided, the generated data will be random on each run.
|
|
91
75
|
mongodb: {
|
|
92
76
|
uri: "mongodb://localhost:27017/appdb",
|
|
93
|
-
|
|
94
77
|
collections: {
|
|
95
78
|
users: {
|
|
96
|
-
rows:
|
|
79
|
+
rows: 100,
|
|
97
80
|
fields: {
|
|
98
81
|
_id: "objectId",
|
|
99
82
|
email: { type: "email", unique: true },
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
age: { type: "int", min: 18, max: 65 },
|
|
103
|
-
status: {
|
|
104
|
-
type: "enum",
|
|
105
|
-
values: ["active", "blocked"],
|
|
106
|
-
weights: [95, 5]
|
|
107
|
-
},
|
|
108
|
-
profile: {
|
|
109
|
-
type: "object",
|
|
110
|
-
fields: {
|
|
111
|
-
city: "city",
|
|
112
|
-
country: "country"
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
createdAt: {
|
|
116
|
-
type: "dateBetween",
|
|
117
|
-
from: "2024-01-01",
|
|
118
|
-
to: "2025-12-31"
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
|
|
123
|
-
orders: {
|
|
124
|
-
rows: 500,
|
|
125
|
-
fields: {
|
|
126
|
-
_id: "objectId",
|
|
127
|
-
userId: { ref: "users._id" }, // Reference to another collection
|
|
128
|
-
total: { type: "decimal", min: 5, max: 500 },
|
|
129
|
-
createdAt: "dateRecent"
|
|
83
|
+
name: "fullName",
|
|
84
|
+
age: { type: "int", min: 18, max: 99 }
|
|
130
85
|
}
|
|
131
86
|
}
|
|
132
87
|
}
|
|
133
88
|
}
|
|
134
89
|
});
|
|
135
90
|
```
|
|
91
|
+
Then run:
|
|
92
|
+
```bash
|
|
93
|
+
schema-seed seed
|
|
94
|
+
```
|
|
136
95
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
- **References**: Use `{ ref: "collection.field" }` to link documents across collections. `schema-seed` automatically calculates the correct insertion order.
|
|
140
|
-
- **Nested Objects**: Define complex structures using the `object` type and a nested `fields` map.
|
|
141
|
-
- **Arrays**: Use the `array` type with `of` to generate lists of values.
|
|
142
|
-
- **Deterministic**: Provide a `seed` at the top level to ensure the same data is generated every time.
|
|
143
|
-
- **Safety**: Production detection prevents accidental seeding of live databases unless `--allow-production` is used.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
## Safety Features
|
|
147
|
-
|
|
148
|
-
- **Production Check**: Refuses to run if `NODE_ENV=production` or if the DB host looks like production. Use `--allow-production` to override.
|
|
149
|
-
- **Confirmation**: Require a typed string to proceed.
|
|
150
|
-
```bash
|
|
151
|
-
schema-seed seed --db "postgres://prod-db/db" --allow-production --confirm "SEED_PROD"
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
## Supported Adapters
|
|
155
|
-
|
|
156
|
-
- `schema-seed-adapter-postgres`
|
|
157
|
-
- `schema-seed-adapter-mysql`
|
|
158
|
-
- `schema-seed-adapter-sqlite`
|
|
159
|
-
- `schema-seed-adapter-mssql`
|
|
160
|
-
- `schema-seed-adapter-oracle`
|
|
161
|
-
- `schema-seed-adapter-mongodb`
|
|
162
|
-
|
|
163
|
-
## Contributing
|
|
96
|
+
---
|
|
164
97
|
|
|
165
|
-
|
|
98
|
+
## Advanced Configuration (`seed.config.ts`)
|
|
166
99
|
|
|
167
|
-
|
|
100
|
+
For complex seeding logic, create a `seed.config.ts` in your project root.
|
|
168
101
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
2. In your GitHub repo, go to **Settings > Secrets and variables > Actions**.
|
|
172
|
-
3. Add a new repository secret named `NPM_TOKEN` with your token value.
|
|
102
|
+
```typescript
|
|
103
|
+
import { defineConfig } from 'schema-seed';
|
|
173
104
|
|
|
174
|
-
|
|
105
|
+
export default defineConfig({
|
|
106
|
+
db: "postgres://localhost/mydb",
|
|
107
|
+
rows: 10,
|
|
108
|
+
overrides: {
|
|
109
|
+
users: {
|
|
110
|
+
// Custom function
|
|
111
|
+
email: ({ i }) => `user${i}@example.com`,
|
|
112
|
+
// Weighted enum (95% active, 5% blocked)
|
|
113
|
+
status: { enum: ["active", "blocked"], weights: [95, 5] },
|
|
114
|
+
// Date range
|
|
115
|
+
created_at: { dateBetween: ["2024-01-01", "2025-12-31"] },
|
|
116
|
+
// Constant value
|
|
117
|
+
role: 'user'
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
hooks: {
|
|
121
|
+
beforeInsert: async (table, rows) => {
|
|
122
|
+
console.log(`Seeding ${table}...`);
|
|
123
|
+
return rows;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
```
|
|
175
128
|
|
|
176
|
-
|
|
129
|
+
## CLI Reference
|
|
130
|
+
|
|
131
|
+
| Flag | Description | Default |
|
|
132
|
+
| :--- | :--- | :--- |
|
|
133
|
+
| `--db <url>` | Database connection string | - |
|
|
134
|
+
| `--dbType <type>` | Force database type (postgres, mysql, etc.) | Auto-inferred |
|
|
135
|
+
| `--all` | Seed all discovered tables | `true` |
|
|
136
|
+
| `--table <names...>` | Seed specific tables only | - |
|
|
137
|
+
| `--rows <n>` | Number of rows per table | `10` |
|
|
138
|
+
| `--seed <string>` | Fixed seed for deterministic data | Random |
|
|
139
|
+
| `--dry-run` | Preview changes without writing to DB | `false` |
|
|
140
|
+
| `--truncate` | Delete all data from tables before seeding | `false` |
|
|
141
|
+
| `--with-parents` | Automatically seed required parent tables | `false` |
|
|
142
|
+
| `--confirm <str>` | Require a confirmation string (for safety) | - |
|
|
143
|
+
| `--allow-production`| Allow running against production hosts | `false` |
|
|
177
144
|
|
|
178
|
-
|
|
179
|
-
2. **Version packages**: Run `pnpm version-packages` to bump versions and update changelogs.
|
|
180
|
-
3. **Verify**: Run `pnpm preflight:release` to build, test, and validate package contents.
|
|
181
|
-
4. **Publish**: Run `pnpm release` to push to NPM.
|
|
145
|
+
## Safety Features
|
|
182
146
|
|
|
183
|
-
|
|
147
|
+
- **Production Detection**: The tool will refuse to run if the database host looks like a production environment (e.g., `aws.com`, `rds.com`) or if `NODE_ENV=production`, unless the `--allow-production` flag is provided.
|
|
148
|
+
- **Confirmation**: Use `--confirm "YES"` to force a manual confirmation step before seeding.
|
|
184
149
|
|
|
185
150
|
## License
|
|
186
151
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "schema-seed",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "CLI for schema-seed",
|
|
6
6
|
"author": "Ali Nazar",
|
|
@@ -40,20 +40,20 @@
|
|
|
40
40
|
"commander": "^12.1.0",
|
|
41
41
|
"dotenv": "^17.2.3",
|
|
42
42
|
"jiti": "^2.6.1",
|
|
43
|
-
"schema-seed-core": "0.1.
|
|
44
|
-
"schema-seed-generators": "0.1.
|
|
43
|
+
"schema-seed-core": "0.1.4",
|
|
44
|
+
"schema-seed-generators": "0.1.4"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/node": "^20.19.27",
|
|
48
48
|
"tsup": "^8.3.5",
|
|
49
49
|
"typescript": "^5.7.2",
|
|
50
50
|
"vitest": "^2.1.8",
|
|
51
|
-
"schema-seed-adapter-mongodb": "0.1.
|
|
52
|
-
"schema-seed-adapter-mysql": "0.1.
|
|
53
|
-
"schema-seed-adapter-postgres": "0.1.
|
|
54
|
-
"schema-seed-adapter-sqlite": "0.1.
|
|
55
|
-
"schema-seed-adapter-mssql": "0.1.
|
|
56
|
-
"schema-seed-adapter-oracle": "0.1.
|
|
51
|
+
"schema-seed-adapter-mongodb": "0.1.4",
|
|
52
|
+
"schema-seed-adapter-mysql": "0.1.4",
|
|
53
|
+
"schema-seed-adapter-postgres": "0.1.4",
|
|
54
|
+
"schema-seed-adapter-sqlite": "0.1.4",
|
|
55
|
+
"schema-seed-adapter-mssql": "0.1.4",
|
|
56
|
+
"schema-seed-adapter-oracle": "0.1.4"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"build": "tsup",
|