schema-seed 0.1.3 ā 0.1.5
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 -102
- package/dist/bin.js +18 -4
- package/dist/bin.js.map +1 -1
- package/dist/index.js +18 -4
- package/dist/index.js.map +1 -1
- 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,83 +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.
|
|
96
|
+
---
|
|
144
97
|
|
|
98
|
+
## Advanced Configuration (`seed.config.ts`)
|
|
145
99
|
|
|
146
|
-
|
|
100
|
+
For complex seeding logic, create a `seed.config.ts` in your project root.
|
|
147
101
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
```bash
|
|
151
|
-
schema-seed seed --db "postgres://prod-db/db" --allow-production --confirm "SEED_PROD"
|
|
152
|
-
```
|
|
102
|
+
```typescript
|
|
103
|
+
import { defineConfig } from 'schema-seed';
|
|
153
104
|
|
|
154
|
-
|
|
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
|
+
```
|
|
155
128
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
-
|
|
161
|
-
-
|
|
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` |
|
|
162
144
|
|
|
163
|
-
##
|
|
145
|
+
## Safety Features
|
|
164
146
|
|
|
165
|
-
|
|
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.
|
|
166
149
|
|
|
167
150
|
## License
|
|
168
151
|
|
package/dist/bin.js
CHANGED
|
@@ -19,15 +19,29 @@ import { existsSync } from "fs";
|
|
|
19
19
|
import { resolve } from "path";
|
|
20
20
|
async function loadConfig(configPath) {
|
|
21
21
|
const jiti = createJiti(import.meta.url);
|
|
22
|
-
|
|
22
|
+
if (configPath) {
|
|
23
|
+
const fullPath = resolve(process.cwd(), configPath);
|
|
24
|
+
if (!existsSync(fullPath)) {
|
|
25
|
+
throw new Error(`Config file not found at ${fullPath}`);
|
|
26
|
+
}
|
|
27
|
+
const module = await jiti.import(fullPath);
|
|
28
|
+
console.log(`\u2705 Loaded config from ${configPath}`);
|
|
29
|
+
return module.default || module;
|
|
30
|
+
}
|
|
31
|
+
const defaultPaths = [
|
|
23
32
|
resolve(process.cwd(), "seed.config.ts"),
|
|
24
33
|
resolve(process.cwd(), "seed.config.js"),
|
|
25
34
|
resolve(process.cwd(), "seed.config.mjs")
|
|
26
35
|
];
|
|
27
|
-
for (const path of
|
|
36
|
+
for (const path of defaultPaths) {
|
|
28
37
|
if (existsSync(path)) {
|
|
29
|
-
|
|
30
|
-
|
|
38
|
+
try {
|
|
39
|
+
const module = await jiti.import(path);
|
|
40
|
+
console.log(`\u2705 Loaded config from ${path}`);
|
|
41
|
+
return module.default || module;
|
|
42
|
+
} catch (err) {
|
|
43
|
+
throw new Error(`Failed to load config file ${path}: ${err.message}`);
|
|
44
|
+
}
|
|
31
45
|
}
|
|
32
46
|
}
|
|
33
47
|
return {};
|
package/dist/bin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/bin.ts","../src/config.mts"],"sourcesContent":["import 'dotenv/config'\nimport { Command } from 'commander'\nimport {\n version as coreVersion,\n runSeedSql,\n runSeedMongo,\n createSeedPlan,\n reportToConsole,\n reportToJson\n} from 'schema-seed-core'\nimport { generators, inferGenerator } from 'schema-seed-generators'\nimport { loadConfig } from './config.mjs'\nimport { writeFileSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { createInterface } from 'node:readline/promises'\n\nconst program = new Command()\n\nprogram\n .name('schema-seed')\n .description('CLI to seed your database with realistic data')\n .version(coreVersion)\n\nasync function getAdapter(dbType: string, dbUrl: string) {\n const packageName = `schema-seed-adapter-${dbType}`\n try {\n const module = await import(packageName)\n const adapterName = dbType.charAt(0).toUpperCase() + dbType.slice(1) + 'Adapter'\n const AdapterClass = module[adapterName] || module.Adapter || module.default\n\n if (!AdapterClass) {\n // Try factory function\n const factoryMethod = `create${dbType.charAt(0).toUpperCase() + dbType.slice(1)}Adapter`\n if (typeof module[factoryMethod] === 'function') {\n return module[factoryMethod](dbUrl)\n }\n throw new Error(`Could not find adapter class or factory in ${packageName}`)\n }\n\n return new AdapterClass(dbUrl)\n } catch (err: any) {\n if (err.code === 'ERR_MODULE_NOT_FOUND' || err.message.includes('Cannot find module')) {\n throw new Error(`Adapter not installed. Run: pnpm add ${packageName}`)\n }\n throw new Error(`Failed to load adapter ${packageName}: ${err.message}`)\n }\n}\n\nfunction inferDbType(dbUrl: string): string {\n if (dbUrl.startsWith('postgres://') || dbUrl.startsWith('postgresql://')) return 'postgres'\n if (dbUrl.startsWith('mysql://')) return 'mysql'\n if (dbUrl.startsWith('sqlite://') || dbUrl.endsWith('.db')) return 'sqlite'\n if (dbUrl.startsWith('mongodb://')) return 'mongodb'\n if (dbUrl.startsWith('sqlserver://')) return 'mssql'\n return 'postgres' // Default\n}\n\nasync function confirmAction(expected: string): Promise<boolean> {\n const rl = createInterface({\n input: process.stdin,\n output: process.stdout\n })\n const answer = await rl.question(`ā ļø Type \"${expected}\" to confirm: `)\n rl.close()\n return answer === expected\n}\n\nconst commonOptions = (cmd: Command) => {\n return cmd\n .option('--db <url>', 'Database connection string')\n .option('--dbType <type>', 'Database type (postgres, mysql, sqlite, mssql, oracle, mongodb)')\n .option('--config <path>', 'Path to config file')\n .option('--table <names...>', 'Specific tables to seed')\n .option('--all', 'Seed all tables', true)\n .option('--rows <number>', 'Number of rows per table', (v) => parseInt(v), 10)\n .option('--rows-per-table <json>', 'JSON mapping of table names to row counts', (v) => JSON.parse(v))\n .option('--seed <string>', 'Random seed')\n .option('--dry-run', 'Do not execute any writes')\n .option('--truncate', 'Truncate tables before seeding')\n .option('--allow-production', 'Allow running against production databases')\n .option('--confirm <string>', 'Require typed confirmation string to proceed')\n .option('--with-parents', 'Include parent tables for foreign keys')\n .option('--out <path>', 'Output report to JSON file')\n}\n\ncommonOptions(program.command('seed'))\n .description('Seed the database')\n .action(async (options) => {\n const config = await loadConfig(options.config)\n const mergedOptions = { ...config, ...options }\n\n if (options.rowsPerTable) {\n mergedOptions.rows = options.rowsPerTable\n }\n\n if (mergedOptions.confirm && !mergedOptions.dryRun) {\n const confirmed = await confirmAction(mergedOptions.confirm)\n if (!confirmed) {\n console.error('ā Confirmation failed. Aborting.')\n process.exit(1)\n }\n }\n\n let dbUrl = mergedOptions.db\n if (mergedOptions.dbType === 'mongodb' && mergedOptions.mongodb?.uri) {\n dbUrl = mergedOptions.mongodb.uri\n }\n\n if (!dbUrl) {\n console.error('Error: Database connection string (--db) is required')\n process.exit(1)\n }\n\n const dbType = mergedOptions.dbType || inferDbType(dbUrl)\n\n try {\n const adapter = await getAdapter(dbType, dbUrl)\n\n let report;\n if (dbType === 'mongodb') {\n // For MongoDB, we use the new config-based seeding\n report = await runSeedMongo(adapter as any, mergedOptions as any, {\n dryRun: mergedOptions.dryRun,\n allowProduction: mergedOptions.allowProduction\n }, { generators, inferGenerator })\n } else {\n await adapter.connect()\n const schema = await (adapter as any).introspectSchema()\n const plan = createSeedPlan(schema, mergedOptions, (adapter as any).capabilities?.deferrableConstraints)\n\n report = await runSeedSql(adapter as any, schema, plan, mergedOptions, {\n generators,\n inferGenerator,\n overrides: mergedOptions.overrides\n })\n }\n\n reportToConsole(report)\n\n if (options.out) {\n writeFileSync(resolve(process.cwd(), options.out), reportToJson(report))\n }\n } catch (err: any) {\n console.error(`\\nā Error: ${err.message}`)\n process.exit(1)\n }\n })\n\nprogram.command('preview')\n .description('Preview the seeding plan without executing')\n .action(async (options) => {\n // Same as seed --dry-run\n const seedCmd = program.commands.find(c => c.name() === 'seed')\n await seedCmd?.parseAsync(['--dry-run', ...process.argv.slice(3)], { from: 'user' })\n })\n\nprogram.command('reset')\n .description('Truncate all tables')\n .option('--db <url>', 'Database connection string')\n .option('--allow-production', 'Allow running against production databases')\n .action(async (options) => {\n console.log('Resetting database...')\n // Implementation would call adapter.truncateTables()\n })\n\nprogram.command('introspect')\n .description('Introspect the database schema and print as JSON')\n .option('--db <url>', 'Database connection string')\n .action(async (options) => {\n console.log('Introspecting database...')\n // Implementation would call adapter.introspectSchema()\n })\n\nprogram.parse()\n","import { createJiti } from 'jiti'\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { type SeedOptions } from 'schema-seed-core'\nimport { type MongoSeedConfig } from 'schema-seed-core/mongo'\n\nexport type Config = (SeedOptions & {\n db?: string\n dbType?: string\n overrides?: Record<string, any>\n}) | MongoSeedConfig\n\nexport async function loadConfig(configPath?: string): Promise<Config> {\n const jiti = createJiti(import.meta.url)\n const paths = configPath\n ? [resolve(process.cwd(), configPath)]\n : [\n resolve(process.cwd(), 'seed.config.ts'),\n resolve(process.cwd(), 'seed.config.js'),\n resolve(process.cwd(), 'seed.config.mjs'),\n ]\n\n for (const path of paths) {\n if (existsSync(path)) {\n const module = await jiti.import(path) as any\n return module.default || module\n }\n }\n\n return {}\n}\n"],"mappings":";;;AAAA,OAAO;AACP,SAAS,eAAe;AACxB;AAAA,EACI,WAAW;AAAA,EACX;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AACP,SAAS,YAAY,sBAAsB;;;ACV3C,SAAS,kBAAkB;AAC3B,S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,oBAAcA,SAAQ,QAAQ,IAAI,GAAG,QAAQ,GAAG,GAAG,aAAa,MAAM,CAAC;AAAA,IAC3E;AAAA,EACJ,SAAS,KAAU;AACf,YAAQ,MAAM;AAAA,gBAAc,IAAI,OAAO,EAAE;AACzC,YAAQ,KAAK,CAAC;AAAA,EAClB;AACJ,CAAC;AAEL,QAAQ,QAAQ,SAAS,EACpB,YAAY,4CAA4C,EACxD,OAAO,OAAO,YAAY;AAEvB,QAAM,UAAU,QAAQ,SAAS,KAAK,OAAK,EAAE,KAAK,MAAM,MAAM;AAC9D,QAAM,SAAS,WAAW,CAAC,aAAa,GAAG,QAAQ,KAAK,MAAM,CAAC,CAAC,GAAG,EAAE,MAAM,OAAO,CAAC;AACvF,CAAC;AAEL,QAAQ,QAAQ,OAAO,EAClB,YAAY,qBAAqB,EACjC,OAAO,cAAc,4BAA4B,EACjD,OAAO,sBAAsB,4CAA4C,EACzE,OAAO,OAAO,YAAY;AACvB,UAAQ,IAAI,uBAAuB;AAEvC,CAAC;AAEL,QAAQ,QAAQ,YAAY,EACvB,YAAY,kDAAkD,EAC9D,OAAO,cAAc,4BAA4B,EACjD,OAAO,OAAO,YAAY;AACvB,UAAQ,IAAI,2BAA2B;AAE3C,CAAC;AAEL,QAAQ,MAAM;","names":["resolve"]}
|
|
1
|
+
{"version":3,"sources":["../src/bin.ts","../src/config.mts"],"sourcesContent":["import 'dotenv/config'\nimport { Command } from 'commander'\nimport {\n version as coreVersion,\n runSeedSql,\n runSeedMongo,\n createSeedPlan,\n reportToConsole,\n reportToJson\n} from 'schema-seed-core'\nimport { generators, inferGenerator } from 'schema-seed-generators'\nimport { loadConfig } from './config.mjs'\nimport { writeFileSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { createInterface } from 'node:readline/promises'\n\nconst program = new Command()\n\nprogram\n .name('schema-seed')\n .description('CLI to seed your database with realistic data')\n .version(coreVersion)\n\nasync function getAdapter(dbType: string, dbUrl: string) {\n const packageName = `schema-seed-adapter-${dbType}`\n try {\n const module = await import(packageName)\n const adapterName = dbType.charAt(0).toUpperCase() + dbType.slice(1) + 'Adapter'\n const AdapterClass = module[adapterName] || module.Adapter || module.default\n\n if (!AdapterClass) {\n // Try factory function\n const factoryMethod = `create${dbType.charAt(0).toUpperCase() + dbType.slice(1)}Adapter`\n if (typeof module[factoryMethod] === 'function') {\n return module[factoryMethod](dbUrl)\n }\n throw new Error(`Could not find adapter class or factory in ${packageName}`)\n }\n\n return new AdapterClass(dbUrl)\n } catch (err: any) {\n if (err.code === 'ERR_MODULE_NOT_FOUND' || err.message.includes('Cannot find module')) {\n throw new Error(`Adapter not installed. Run: pnpm add ${packageName}`)\n }\n throw new Error(`Failed to load adapter ${packageName}: ${err.message}`)\n }\n}\n\nfunction inferDbType(dbUrl: string): string {\n if (dbUrl.startsWith('postgres://') || dbUrl.startsWith('postgresql://')) return 'postgres'\n if (dbUrl.startsWith('mysql://')) return 'mysql'\n if (dbUrl.startsWith('sqlite://') || dbUrl.endsWith('.db')) return 'sqlite'\n if (dbUrl.startsWith('mongodb://')) return 'mongodb'\n if (dbUrl.startsWith('sqlserver://')) return 'mssql'\n return 'postgres' // Default\n}\n\nasync function confirmAction(expected: string): Promise<boolean> {\n const rl = createInterface({\n input: process.stdin,\n output: process.stdout\n })\n const answer = await rl.question(`ā ļø Type \"${expected}\" to confirm: `)\n rl.close()\n return answer === expected\n}\n\nconst commonOptions = (cmd: Command) => {\n return cmd\n .option('--db <url>', 'Database connection string')\n .option('--dbType <type>', 'Database type (postgres, mysql, sqlite, mssql, oracle, mongodb)')\n .option('--config <path>', 'Path to config file')\n .option('--table <names...>', 'Specific tables to seed')\n .option('--all', 'Seed all tables', true)\n .option('--rows <number>', 'Number of rows per table', (v) => parseInt(v), 10)\n .option('--rows-per-table <json>', 'JSON mapping of table names to row counts', (v) => JSON.parse(v))\n .option('--seed <string>', 'Random seed')\n .option('--dry-run', 'Do not execute any writes')\n .option('--truncate', 'Truncate tables before seeding')\n .option('--allow-production', 'Allow running against production databases')\n .option('--confirm <string>', 'Require typed confirmation string to proceed')\n .option('--with-parents', 'Include parent tables for foreign keys')\n .option('--out <path>', 'Output report to JSON file')\n}\n\ncommonOptions(program.command('seed'))\n .description('Seed the database')\n .action(async (options) => {\n const config = await loadConfig(options.config)\n const mergedOptions = { ...config, ...options }\n\n if (options.rowsPerTable) {\n mergedOptions.rows = options.rowsPerTable\n }\n\n if (mergedOptions.confirm && !mergedOptions.dryRun) {\n const confirmed = await confirmAction(mergedOptions.confirm)\n if (!confirmed) {\n console.error('ā Confirmation failed. Aborting.')\n process.exit(1)\n }\n }\n\n let dbUrl = mergedOptions.db\n if (mergedOptions.dbType === 'mongodb' && mergedOptions.mongodb?.uri) {\n dbUrl = mergedOptions.mongodb.uri\n }\n\n if (!dbUrl) {\n console.error('Error: Database connection string (--db) is required')\n process.exit(1)\n }\n\n const dbType = mergedOptions.dbType || inferDbType(dbUrl)\n\n try {\n const adapter = await getAdapter(dbType, dbUrl)\n\n let report;\n if (dbType === 'mongodb') {\n // For MongoDB, we use the new config-based seeding\n report = await runSeedMongo(adapter as any, mergedOptions as any, {\n dryRun: mergedOptions.dryRun,\n allowProduction: mergedOptions.allowProduction\n }, { generators, inferGenerator })\n } else {\n await adapter.connect()\n const schema = await (adapter as any).introspectSchema()\n const plan = createSeedPlan(schema, mergedOptions, (adapter as any).capabilities?.deferrableConstraints)\n\n report = await runSeedSql(adapter as any, schema, plan, mergedOptions, {\n generators,\n inferGenerator,\n overrides: mergedOptions.overrides\n })\n }\n\n reportToConsole(report)\n\n if (options.out) {\n writeFileSync(resolve(process.cwd(), options.out), reportToJson(report))\n }\n } catch (err: any) {\n console.error(`\\nā Error: ${err.message}`)\n process.exit(1)\n }\n })\n\nprogram.command('preview')\n .description('Preview the seeding plan without executing')\n .action(async (options) => {\n // Same as seed --dry-run\n const seedCmd = program.commands.find(c => c.name() === 'seed')\n await seedCmd?.parseAsync(['--dry-run', ...process.argv.slice(3)], { from: 'user' })\n })\n\nprogram.command('reset')\n .description('Truncate all tables')\n .option('--db <url>', 'Database connection string')\n .option('--allow-production', 'Allow running against production databases')\n .action(async (options) => {\n console.log('Resetting database...')\n // Implementation would call adapter.truncateTables()\n })\n\nprogram.command('introspect')\n .description('Introspect the database schema and print as JSON')\n .option('--db <url>', 'Database connection string')\n .action(async (options) => {\n console.log('Introspecting database...')\n // Implementation would call adapter.introspectSchema()\n })\n\nprogram.parse()\n","import { createJiti } from 'jiti'\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { type SeedOptions } from 'schema-seed-core'\nimport { type MongoSeedConfig } from 'schema-seed-core/mongo'\n\nexport type Config = (SeedOptions & {\n db?: string\n dbType?: string\n overrides?: Record<string, any>\n}) | MongoSeedConfig\n\nexport async function loadConfig(configPath?: string): Promise<Config> {\n const jiti = createJiti(import.meta.url)\n\n if (configPath) {\n const fullPath = resolve(process.cwd(), configPath)\n if (!existsSync(fullPath)) {\n throw new Error(`Config file not found at ${fullPath}`)\n }\n const module = await jiti.import(fullPath) as any\n console.log(`ā
Loaded config from ${configPath}`)\n return module.default || module\n }\n\n const defaultPaths = [\n resolve(process.cwd(), 'seed.config.ts'),\n resolve(process.cwd(), 'seed.config.js'),\n resolve(process.cwd(), 'seed.config.mjs'),\n ]\n\n for (const path of defaultPaths) {\n if (existsSync(path)) {\n try {\n const module = await jiti.import(path) as any\n console.log(`ā
Loaded config from ${path}`)\n return module.default || module\n } catch (err: any) {\n throw new Error(`Failed to load config file ${path}: ${err.message}`)\n }\n }\n }\n\n return {}\n}\n"],"mappings":";;;AAAA,OAAO;AACP,SAAS,eAAe;AACxB;AAAA,EACI,WAAW;AAAA,EACX;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AACP,SAAS,YAAY,sBAAsB;;;ACV3C,SAAS,kBAAkB;AAC3B,SAAS,kBAAkB;AAC3B,SAAS,eAAe;AAUxB,eAAsB,WAAW,YAAsC;AACnE,QAAM,OAAO,WAA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iti = createJiti(import.meta.url);
|
|
9
|
-
|
|
9
|
+
if (configPath) {
|
|
10
|
+
const fullPath = resolve(process.cwd(), configPath);
|
|
11
|
+
if (!existsSync(fullPath)) {
|
|
12
|
+
throw new Error(`Config file not found at ${fullPath}`);
|
|
13
|
+
}
|
|
14
|
+
const module = await jiti.import(fullPath);
|
|
15
|
+
console.log(`\u2705 Loaded config from ${configPath}`);
|
|
16
|
+
return module.default || module;
|
|
17
|
+
}
|
|
18
|
+
const defaultPaths = [
|
|
10
19
|
resolve(process.cwd(), "seed.config.ts"),
|
|
11
20
|
resolve(process.cwd(), "seed.config.js"),
|
|
12
21
|
resolve(process.cwd(), "seed.config.mjs")
|
|
13
22
|
];
|
|
14
|
-
for (const path of
|
|
23
|
+
for (const path of defaultPaths) {
|
|
15
24
|
if (existsSync(path)) {
|
|
16
|
-
|
|
17
|
-
|
|
25
|
+
try {
|
|
26
|
+
const module = await jiti.import(path);
|
|
27
|
+
console.log(`\u2705 Loaded config from ${path}`);
|
|
28
|
+
return module.default || module;
|
|
29
|
+
} catch (err) {
|
|
30
|
+
throw new Error(`Failed to load config file ${path}: ${err.message}`);
|
|
31
|
+
}
|
|
18
32
|
}
|
|
19
33
|
}
|
|
20
34
|
return {};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/config.mts","../src/index.ts"],"sourcesContent":["import { createJiti } from 'jiti'\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { type SeedOptions } from 'schema-seed-core'\nimport { type MongoSeedConfig } from 'schema-seed-core/mongo'\n\nexport type Config = (SeedOptions & {\n db?: string\n dbType?: string\n overrides?: Record<string, any>\n}) | MongoSeedConfig\n\nexport async function loadConfig(configPath?: string): Promise<Config> {\n const jiti = createJiti(import.meta.url)\n
|
|
1
|
+
{"version":3,"sources":["../src/config.mts","../src/index.ts"],"sourcesContent":["import { createJiti } from 'jiti'\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { type SeedOptions } from 'schema-seed-core'\nimport { type MongoSeedConfig } from 'schema-seed-core/mongo'\n\nexport type Config = (SeedOptions & {\n db?: string\n dbType?: string\n overrides?: Record<string, any>\n}) | MongoSeedConfig\n\nexport async function loadConfig(configPath?: string): Promise<Config> {\n const jiti = createJiti(import.meta.url)\n\n if (configPath) {\n const fullPath = resolve(process.cwd(), configPath)\n if (!existsSync(fullPath)) {\n throw new Error(`Config file not found at ${fullPath}`)\n }\n const module = await jiti.import(fullPath) as any\n console.log(`ā
Loaded config from ${configPath}`)\n return module.default || module\n }\n\n const defaultPaths = [\n resolve(process.cwd(), 'seed.config.ts'),\n resolve(process.cwd(), 'seed.config.js'),\n resolve(process.cwd(), 'seed.config.mjs'),\n ]\n\n for (const path of defaultPaths) {\n if (existsSync(path)) {\n try {\n const module = await jiti.import(path) as any\n console.log(`ā
Loaded config from ${path}`)\n return module.default || module\n } catch (err: any) {\n throw new Error(`Failed to load config file ${path}: ${err.message}`)\n }\n }\n }\n\n return {}\n}\n","import { Config } from './config.mjs'\nexport { Config } from './config.mjs'\n\nexport function defineConfig(config: Config): Config {\n return config\n}\n\nexport { loadConfig } from './config.mjs'\n"],"mappings":";;;AAAA,SAAS,kBAAkB;AAC3B,SAAS,kBAAkB;AAC3B,SAAS,eAAe;AAUxB,eAAsB,WAAW,YAAsC;AACnE,QAAM,OAAO,WAAW,YAAY,GAAG;AAEvC,MAAI,YAAY;AACZ,UAAM,WAAW,QAAQ,QAAQ,IAAI,GAAG,UAAU;AAClD,QAAI,CAAC,WAAW,QAAQ,GAAG;AACvB,YAAM,IAAI,MAAM,4BAA4B,QAAQ,EAAE;AAAA,IAC1D;AACA,UAAM,SAAS,MAAM,KAAK,OAAO,QAAQ;AACzC,YAAQ,IAAI,6BAAwB,UAAU,EAAE;AAChD,WAAO,OAAO,WAAW;AAAA,EAC7B;AAEA,QAAM,eAAe;AAAA,IACjB,QAAQ,QAAQ,IAAI,GAAG,gBAAgB;AAAA,IACvC,QAAQ,QAAQ,IAAI,GAAG,gBAAgB;AAAA,IACvC,QAAQ,QAAQ,IAAI,GAAG,iBAAiB;AAAA,EAC5C;AAEA,aAAW,QAAQ,cAAc;AAC7B,QAAI,WAAW,IAAI,GAAG;AAClB,UAAI;AACA,cAAM,SAAS,MAAM,KAAK,OAAO,IAAI;AACrC,gBAAQ,IAAI,6BAAwB,IAAI,EAAE;AAC1C,eAAO,OAAO,WAAW;AAAA,MAC7B,SAAS,KAAU;AACf,cAAM,IAAI,MAAM,8BAA8B,IAAI,KAAK,IAAI,OAAO,EAAE;AAAA,MACxE;AAAA,IACJ;AAAA,EACJ;AAEA,SAAO,CAAC;AACZ;;;ACzCO,SAAS,aAAa,QAAwB;AACjD,SAAO;AACX;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "schema-seed",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
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.5",
|
|
44
|
+
"schema-seed-generators": "0.1.5"
|
|
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.5",
|
|
52
|
+
"schema-seed-adapter-mysql": "0.1.5",
|
|
53
|
+
"schema-seed-adapter-postgres": "0.1.5",
|
|
54
|
+
"schema-seed-adapter-sqlite": "0.1.5",
|
|
55
|
+
"schema-seed-adapter-mssql": "0.1.5",
|
|
56
|
+
"schema-seed-adapter-oracle": "0.1.5"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"build": "tsup",
|