Skip to content

Commit 4b0afc5

Browse files
committed
chore: wip
1 parent a6dbdf9 commit 4b0afc5

File tree

5 files changed

+72
-38
lines changed

5 files changed

+72
-38
lines changed

.stacks/core/actions/src/make.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,24 +280,31 @@ function send(): ${importOption} {
280280
}
281281

282282
export async function createModel(options: MakeOptions) {
283-
const name = options.name
284-
const path = `${projectPath()}/config/models/${name}.json`
283+
const optionName = options.name
284+
const name = optionName[0].toUpperCase() + optionName.slice(1)
285+
const path = `${projectPath()}/config/models/${name}.ts`
285286
try {
286287
await writeTextFile({
287288
path: `${path}`,
288-
data: `{
289-
"name": "${name[0].toUpperCase() + name.slice(1)}",
290-
"fields": [
291-
{
292-
"name": "...",
293-
"type": "..."
294-
}
295-
]
296-
}
289+
data: `interface ModelData {
290+
[key: string]: any
291+
}
292+
293+
const ${name}: ModelData = {
294+
name: '${name}',
295+
fields: [
296+
{
297+
name: '...',
298+
type: '....',
299+
},
300+
],
301+
}
302+
303+
export default ${name}
297304
`,
298305
})
299306

300-
log.success(`Successfully created your model at /config/models/${name}.json`)
307+
log.success(`Successfully created your model at /config/models/${name}.ts`)
301308
}
302309
catch (error) {
303310
log.error('There was an error creating your model. Please try again')

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,33 @@ generator client {
7171
})
7272
}
7373

74-
function readModelsFromFolder(folderPath: string): ModelData[] {
75-
const models: ModelData[] = []
76-
77-
fs.readdirSync(folderPath).forEach((file) => {
78-
if (file.endsWith('.json')) {
79-
const filePath = `${folderPath}/${file}`
80-
const fileContents = fs.readFileSync(filePath, 'utf-8')
81-
const data = JSON.parse(fileContents)
82-
83-
models.push({
84-
name: data.name,
85-
columns: data.fields,
86-
})
87-
}
74+
function readModelsFromFolder(folderPath: string): Promise<ModelData[]> {
75+
return new Promise((resolve, reject) => {
76+
const models: ModelData[] = []
77+
78+
fs.readdir(folderPath, (err, files) => {
79+
if (err) {
80+
reject(err)
81+
}
82+
83+
const promises = files
84+
.filter((file) => file.endsWith('.ts'))
85+
.map((file) => {
86+
const filePath = `${folderPath}/${file}`
87+
88+
return import(filePath).then((data) => {
89+
models.push({
90+
name: data.default.name,
91+
columns: data.default.fields,
92+
})
93+
})
94+
})
95+
96+
Promise.all(promises)
97+
.then(() => resolve(models))
98+
.catch((err) => reject(err))
99+
})
88100
})
89-
90-
return models
91101
}
92102

93103
export {

.stacks/database/schema.prisma

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ model User {
1212
createdAt DateTime @default(now())
1313
updatedAt DateTime @updatedAt()
1414
name String @unique
15+
email String @unique
16+
password String
1517
}
1618

config/models/User.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
interface ModelData {
2+
[key: string]: any
3+
}
4+
5+
const User: ModelData = {
6+
name: 'User',
7+
fields: [
8+
{
9+
name: 'name',
10+
type: 'String',
11+
unique: true,
12+
},
13+
{
14+
name: 'email',
15+
type: 'String',
16+
unique: true,
17+
},
18+
{
19+
name: 'password',
20+
type: 'String',
21+
},
22+
],
23+
}
24+
25+
export default User

config/models/user.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)