Skip to content

Commit 9321a2b

Browse files
committed
chore: wip
1 parent ec5a56c commit 9321a2b

File tree

6 files changed

+64
-22
lines changed

6 files changed

+64
-22
lines changed

.stacks/buddy/src/commands/make.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { CLI, MakeOptions } from '@stacksjs/types'
33
import { intro, italic, log, outro, prompts } from '@stacksjs/cli'
44

55
import {
6+
createModel,
67
createNotification,
78
invoke,
89
component as makeComponent,
@@ -18,6 +19,7 @@ import {
1819

1920
async function make(buddy: CLI) {
2021
const descriptions = {
22+
model: 'Create a new model',
2123
component: 'Create a new component',
2224
page: 'Create a new page',
2325
function: 'Create a new function',
@@ -236,6 +238,21 @@ async function make(buddy: CLI) {
236238

237239
await makeStack(options)
238240
})
241+
242+
buddy
243+
.command('make:model', descriptions.model)
244+
.option('-n, --name', 'The name of the model')
245+
.action(async (options: MakeOptions) => {
246+
const name = buddy.args[0] || options.name
247+
options.name = name
248+
249+
if (!name) {
250+
log.error('You need to specify the model name')
251+
process.exit()
252+
}
253+
254+
await createModel(options)
255+
})
239256
}
240257

241258
function hasNoOptions(options: MakeOptions) {

.stacks/core/actions/src/generate.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { log } from '@stacksjs/logging'
2-
import { readModelsFromFolder } from '@stacksjs/database'
2+
import { readModelsFromFolder, generatePrismaSchema } from '@stacksjs/database'
33
import { Action, NpmScript } from '@stacksjs/types'
44
import type { GeneratorOptions } from '@stacksjs/types'
55
import { runNpmScript } from '@stacksjs/utils'
@@ -120,6 +120,11 @@ export async function types(options?: GeneratorOptions) {
120120
}
121121

122122
export async function models(options?: any) {
123-
const test = await readModelsFromFolder(`${projectPath()}/config/models`)
124-
console.log(test)
123+
const models = await readModelsFromFolder(`${projectPath()}/config/models`)
124+
125+
await generatePrismaSchema(
126+
models,
127+
`${projectPath()}/.stacks/database/schema.prisma`,
128+
{ database: "postgresql"}
129+
)
125130
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { italic, log, spawn } from '@stacksjs/cli'
22
import { createFolder, doesFolderExist, writeTextFile } from '@stacksjs/storage'
33
import { resolve } from '@stacksjs/path'
44
import type { MakeOptions } from '@stacksjs/types'
5+
import { projectPath } from '@stacksjs/path'
56

67
export async function invoke(options: MakeOptions) {
78
if (options.component)
@@ -278,3 +279,28 @@ function send(): ${importOption} {
278279
return false
279280
}
280281
}
282+
283+
export async function createModel(options: MakeOptions) {
284+
const name = options.name
285+
const path = `${projectPath()}/config/models/${name}.json`
286+
try {
287+
await writeTextFile({
288+
path: `${path}`,
289+
data: `{
290+
"name": "${name[0].toUpperCase() + name.slice(1)}",
291+
"fields": [
292+
{
293+
"name": "...",
294+
"type": "..."
295+
}
296+
]
297+
}
298+
`,
299+
})
300+
301+
log.success(`Successfully created your model at /config/models/${name}.json`)
302+
} catch (error) {
303+
log.error('There was an error creating your model. Please try again')
304+
}
305+
306+
}

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { filesystem } from '@stacksjs/storage'
2+
import type { SchemaOptions } from '@stacksjs/types'
23

34
const { fs } = filesystem
45

@@ -19,9 +20,9 @@ interface ModelData {
1920
[key: string]: any
2021
}
2122

22-
function generatePrismaSchema(models: Model[], path: string): void {
23+
function generatePrismaSchema(models: Model[], path: string, options: SchemaOptions): void {
2324
let schema = `datasource db {
24-
provider = "postgresql"
25+
provider = "${options.database}"
2526
url = env("DATABASE_URL")
2627
}
2728
@@ -72,18 +73,15 @@ function readModelsFromFolder(folderPath: string): ModelData[] {
7273
const models: ModelData[] = []
7374

7475
fs.readdirSync(folderPath).forEach((file) => {
75-
if (file.endsWith('.ts')) {
76+
if (file.endsWith('.json')) {
7677
const filePath = `${folderPath}/${file}`
7778
const fileContents = fs.readFileSync(filePath, 'utf-8')
79+
const data = JSON.parse(fileContents)
7880

79-
const regex = /return\s*{([^}]*)}/m
80-
const match = fileContents.match(regex)
81-
82-
if (match) {
83-
console.log(match[0]);
84-
const modelData = eval(`({${match[1]}})`)
85-
models.push(modelData)
86-
}
81+
models.push({
82+
name: data.name,
83+
columns: data.fields
84+
})
8785
}
8886
})
8987

.stacks/core/types/src/database.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ export interface DatabaseOptions {
99
export interface SeedData {
1010
[key: string]: any
1111
}
12+
13+
export interface SchemaOptions {
14+
database: string
15+
}

config/models/test.ts

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

0 commit comments

Comments
 (0)