|
1 |
| -import { MysqlDialect, QueryBuilder, createPool } from '@stacksjs/query-builder' |
2 |
| -import { filesystem } from '@stacksjs/storage' |
3 |
| -import type { Model } from '@stacksjs/types' |
4 |
| -import { projectPath } from '@stacksjs/path' |
5 |
| -import { database as config } from '@stacksjs/config' |
6 |
| - |
7 |
| -const { fs } = filesystem |
8 |
| - |
9 |
| -function readModels(folderPath: string): Promise<Model[]> { |
10 |
| - return new Promise((resolve, reject) => { |
11 |
| - const models: Model[] = [] |
12 |
| - |
13 |
| - fs.readdir(folderPath, (err, files) => { |
14 |
| - if (err) |
15 |
| - reject(err) |
16 |
| - |
17 |
| - const promises = files |
18 |
| - .filter(file => file.endsWith('.ts')) |
19 |
| - .map((file) => { |
20 |
| - const filePath = `${folderPath}/${file}` |
21 |
| - |
22 |
| - return import(filePath).then((data) => { |
23 |
| - models.push({ |
24 |
| - name: data.default.name, |
25 |
| - fields: data.default.fields, |
26 |
| - useSeed: data.default.useSeed, |
27 |
| - }) |
28 |
| - }) |
29 |
| - }) |
30 |
| - |
31 |
| - Promise.all(promises) |
32 |
| - .then(() => resolve(models)) |
33 |
| - .catch(err => reject(err)) |
34 |
| - }) |
35 |
| - }) |
36 |
| -} |
| 1 | +// import { MysqlDialect, QueryBuilder, createPool } from '@stacksjs/query-builder' |
| 2 | +// import { filesystem } from '@stacksjs/storage' |
| 3 | +// import type { Model } from '@stacksjs/types' |
| 4 | +// import { projectPath } from '@stacksjs/path' |
| 5 | +// import { database as config } from '@stacksjs/config' |
| 6 | + |
| 7 | +// const { fs } = filesystem |
| 8 | + |
| 9 | +// function readModels(folderPath: string): Promise<Model[]> { |
| 10 | +// return new Promise((resolve, reject) => { |
| 11 | +// const models: Model[] = [] |
| 12 | + |
| 13 | +// fs.readdir(folderPath, (err, files) => { |
| 14 | +// if (err) |
| 15 | +// reject(err) |
| 16 | + |
| 17 | +// const promises = files |
| 18 | +// .filter(file => file.endsWith('.ts')) |
| 19 | +// .map((file) => { |
| 20 | +// const filePath = `${folderPath}/${file}` |
| 21 | + |
| 22 | +// return import(filePath).then((data) => { |
| 23 | +// models.push({ |
| 24 | +// name: data.default.name, |
| 25 | +// fields: data.default.fields, |
| 26 | +// useSeed: data.default.useSeed, |
| 27 | +// }) |
| 28 | +// }) |
| 29 | +// }) |
| 30 | + |
| 31 | +// Promise.all(promises) |
| 32 | +// .then(() => resolve(models)) |
| 33 | +// .catch(err => reject(err)) |
| 34 | +// }) |
| 35 | +// }) |
| 36 | +// } |
37 | 37 |
|
38 | 38 | async function seed() {
|
39 |
| - const db = new QueryBuilder({ |
40 |
| - dialect: new MysqlDialect({ |
41 |
| - pool: createPool({ |
42 |
| - database: config.database, |
43 |
| - host: config.host, |
44 |
| - password: config.password, |
45 |
| - user: config.username, |
46 |
| - }), |
47 |
| - }), |
48 |
| - }) |
49 |
| - |
50 |
| - const models = await readModels(projectPath('app/models')) |
51 |
| - |
52 |
| - const queries = models.flatMap((model) => { |
53 |
| - const { seedable, fields } = model |
54 |
| - |
55 |
| - if (!seedable) |
56 |
| - return [] |
57 |
| - |
58 |
| - const count = typeof seedable === 'boolean' ? 10 : seedable.count |
59 |
| - |
60 |
| - const records: Record<string, any>[] = [] |
61 |
| - for (let i = 0; i < count; i++) { |
62 |
| - const record: Record<string, any> = {} |
63 |
| - Object.entries(fields).forEach(([name, field]) => { |
64 |
| - if (field.factory) |
65 |
| - record[name] = field.factory() |
66 |
| - }) |
67 |
| - records.push(record) |
68 |
| - } |
69 |
| - |
70 |
| - return model |
71 |
| - // return db.insertInto('User').values(records).build(sql`RETURNING *`) |
72 |
| - }) |
| 39 | + // const db = new QueryBuilder({ |
| 40 | + // dialect: new MysqlDialect({ |
| 41 | + // pool: createPool({ |
| 42 | + // database: config.database, |
| 43 | + // host: config.host, |
| 44 | + // password: config.password, |
| 45 | + // user: config.username, |
| 46 | + // }), |
| 47 | + // }), |
| 48 | + // }) |
| 49 | + |
| 50 | + // const models = await readModels(projectPath('app/models')) |
| 51 | + |
| 52 | + // const queries = models.flatMap((model) => { |
| 53 | + // const { seedable, fields } = model |
| 54 | + |
| 55 | + // if (!seedable) |
| 56 | + // return [] |
| 57 | + |
| 58 | + // const count = typeof seedable === 'boolean' ? 10 : seedable.count |
| 59 | + |
| 60 | + // const records: Record<string, any>[] = [] |
| 61 | + // for (let i = 0; i < count; i++) { |
| 62 | + // const record: Record<string, any> = {} |
| 63 | + // Object.entries(fields).forEach(([name, field]) => { |
| 64 | + // if (field.factory) |
| 65 | + // record[name] = field.factory() |
| 66 | + // }) |
| 67 | + // records.push(record) |
| 68 | + // } |
| 69 | + |
| 70 | + // return model |
| 71 | + // // return db.insertInto('User').values(records).build(sql`RETURNING *`) |
| 72 | + // }) |
73 | 73 |
|
74 | 74 | // const { rows } = await db.transaction().execute()
|
75 | 75 |
|
76 | 76 | // return rows
|
77 | 77 | }
|
78 | 78 |
|
79 | 79 | export { seed }
|
80 |
| - |
81 |
| -export {} |
0 commit comments