Skip to content

Commit a7537fd

Browse files
committed
chore: wip
1 parent d19225b commit a7537fd

File tree

5 files changed

+30
-29
lines changed

5 files changed

+30
-29
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Action, NpmScript } from '@stacksjs/types'
44
import type { GeneratorOptions } from '@stacksjs/types'
55
import { runNpmScript } from '@stacksjs/utils'
66
import { runCommand } from '@stacksjs/cli'
7+
import { database } from '@stacksjs/config'
78
import { runAction } from './helpers'
89

910
export async function invoke(options?: GeneratorOptions) {
@@ -121,7 +122,7 @@ export async function types(options?: GeneratorOptions) {
121122
export async function migrations() {
122123
const path = frameworkPath('database/schema.prisma')
123124

124-
await migrate(path, { database: 'postgresql' })
125+
await migrate(path, { database: database.dbms })
125126

126127
await runCommand(`npx prisma migrate --schema=${path}`)
127128

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

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,6 @@ generator client {
2424
updatedAt DateTime @updatedAt()
2525
`
2626

27-
for (const column of model.columns) {
28-
let columnSchema = ` ${column.name} ${column.type}`
29-
30-
if (column.required)
31-
columnSchema += ' @required'
32-
33-
if (column.unique)
34-
columnSchema += ' @unique'
35-
36-
if (column.default)
37-
columnSchema += ` @default(${column.default})`
38-
39-
columnSchema += '\n'
40-
schema += columnSchema
41-
}
42-
4327
if (model.hasOne) {
4428
schema += ` ${model.hasOne} ${titleCase(model.hasOne)}?`
4529
schema += ` @relation(fields: [${model.hasOne}Id], references: [id])\n`
@@ -57,28 +41,37 @@ generator client {
5741
schema += ` @relation(fields: [id], references: [${model.name.toLowerCase()}Id])\n`
5842
}
5943

44+
if (model.hasThrough) {
45+
schema += ` ${model.hasThrough.model} ${titleCase(model.hasThrough.model)}[]`
46+
schema += ` @relation("${model.hasThrough.using}")`
47+
schema += ` ${model.hasThrough.using} ${titleCase(model.hasThrough.using)}`
48+
schema += ` @relation("${model.hasThrough.through}")`
49+
schema += ` ${model.hasThrough.through} ${titleCase(model.hasThrough.through)}`
50+
schema += ` @relation(fields: [${model.hasThrough.using}], references: [id])\n`
51+
}
52+
6053
schema += '}\n\n'
6154

6255
if (model.hasMany) {
6356
schema += `model ${titleCase(model.hasMany)} {
64-
id Int @id @default(autoincrement())
65-
createdAt DateTime @default(now())
66-
updatedAt DateTime @updatedAt()
67-
${model.name.toLowerCase()}Id Int
68-
${model.name.toLowerCase()} ${titleCase(model.name)}
69-
@relation(fields: [${model.name.toLowerCase()}Id], references: [id])
57+
id Int @id @default(autoincrement())
58+
createdAt DateTime @default(now())
59+
updatedAt DateTime @updatedAt()
60+
${model.name.toLowerCase()}Id Int
61+
${model.name.toLowerCase()} ${titleCase(model.name)}
62+
@relation(fields: [${model.name.toLowerCase()}Id], references: [id])
7063
}\n\n`
7164
}
7265

7366
if (model.hasOne || model.belongsTo) {
7467
const relatedModelName = model.hasOne || model.belongsTo
7568
schema += `model ${titleCase(relatedModelName)} {
76-
id Int @id @default(autoincrement())
77-
createdAt DateTime @default(now())
78-
updatedAt DateTime @updatedAt()
79-
${model.name.toLowerCase()} ${titleCase(model.name)}?
80-
${model.name.toLowerCase()}Id Int?
81-
@unique
69+
id Int @id @default(autoincrement())
70+
createdAt DateTime @default(now())
71+
updatedAt DateTime @updatedAt()
72+
${model.name.toLowerCase()} ${titleCase(model.name)}?
73+
${model.name.toLowerCase()}Id Int?
74+
@unique
8275
}\n\n`
8376
}
8477
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface DatabaseOptions {
88
driver?: string
99
url?: string
1010
client?: DatabaseClient
11+
dbms: string
1112
}
1213

1314
export interface FactoryOptions {

.stacks/core/types/src/model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export interface Model {
2020
hasOne?: string
2121
hasMany?: string
2222
belongsToMany?: string
23+
hasThrough?: {
24+
model: string
25+
through: string
26+
using: string
27+
}
2328
useSeed?: boolean | SeedOptions
2429
useSearch?: boolean | SearchIndexSettings
2530
useTimestamps?: boolean | TimestampOptions

config/database.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ import { defineDatabaseConfig } from '../.stacks/core/config/src/helpers'
1111
export default defineDatabaseConfig({
1212
driver: 'planetscale',
1313
url: env('DATABASE_URL', ''),
14+
dbms: env('DATABASE_NAME', 'mysql'),
1415
})

0 commit comments

Comments
 (0)