Skip to content

Commit 0fe928c

Browse files
committed
chore: wip
1 parent 2864bcd commit 0fe928c

File tree

1 file changed

+33
-28
lines changed
  • .stacks/core/database/src/migrations

1 file changed

+33
-28
lines changed

.stacks/core/database/src/migrations/index.ts

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ interface ModelData {
88
[key: string]: any
99
}
1010

11+
/**
12+
* Generates the Prisma schema file based on the given models and options.
13+
*/
1114
function generatePrismaSchema(models: ModelOptions[], path: string, options: SchemaOptions): void {
1215
let schema = `datasource db {
1316
provider = "${options.database}"
@@ -46,43 +49,45 @@ generator client {
4649
schema += '}\n\n'
4750
}
4851

49-
if (!fs.existsSync(`${projectPath()}/.stacks/database`))
50-
fs.mkdirSync(`${projectPath()}/.stacks/database`)
52+
if (!fs?.mkdirSync(`${projectPath()}/.stacks/database`, { recursive: true })) {
53+
console.error(`Error creating directory: ${projectPath()}/.stacks/database`)
54+
return
55+
}
5156

5257
fs.writeFile(path, schema, (err) => {
5358
if (err)
5459
console.error(`Error writing schema file: ${err.message}`)
55-
56-
// console.log(`Schema file generated successfully at path: ${path}`)
60+
else
61+
console.log(`Schema file generated successfully at path: ${path}`)
5762
})
5863
}
5964

60-
function readModelsFromFolder(folderPath: string): Promise<ModelData[]> {
61-
return new Promise((resolve, reject) => {
62-
const models: ModelData[] = []
63-
64-
fs.readdir(folderPath, (err, files) => {
65-
if (err)
66-
reject(err)
67-
68-
const promises = files
69-
.filter(file => file.endsWith('.ts'))
70-
.map((file) => {
71-
const filePath = `${folderPath}/${file}`
72-
73-
return import(filePath).then((data) => {
74-
models.push({
75-
name: data.default.name,
76-
columns: data.default.fields,
77-
})
78-
})
65+
/**
66+
* Reads the model data from the given folder path.
67+
*/
68+
async function readModelsFromFolder(folderPath: string): Promise<ModelData[]> {
69+
const models: ModelData[] = []
70+
71+
try {
72+
const files = await fs.promises.readdir(folderPath)
73+
const promises = files
74+
.filter(file => file.endsWith('.ts'))
75+
.map(async (file) => {
76+
const filePath = `${folderPath}/${file}`
77+
const data = await import(filePath)
78+
models.push({
79+
name: data.default.name,
80+
columns: data.default.fields,
7981
})
82+
})
8083

81-
Promise.all(promises)
82-
.then(() => resolve(models))
83-
.catch(err => reject(err))
84-
})
85-
})
84+
await Promise.all(promises)
85+
}
86+
catch (err) {
87+
console.error(`Error reading models from folder: ${folderPath}`, err)
88+
}
89+
90+
return models
8691
}
8792

8893
export {

0 commit comments

Comments
 (0)