Skip to content

Commit cb9eb7f

Browse files
committed
chore: wip
1 parent 0af70a0 commit cb9eb7f

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

storage/framework/core/actions/src/orm/generate-model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { log } from '@stacksjs/logging'
2+
import { modelTableName } from '@stacksjs/orm'
23
import { path } from '@stacksjs/path'
34
import { fs, glob } from '@stacksjs/storage'
45
import { pascalCase } from '@stacksjs/strings'
56
import type { Model, RelationConfig } from '@stacksjs/types'
67
import { isString } from '@stacksjs/validation'
7-
import { modelTableName } from '@stacksjs/orm'
88

99
export interface FieldArrayElement {
1010
entity: string
@@ -160,7 +160,7 @@ async function initiateModelGeneration(): Promise<void> {
160160
log.debug(`Processing model file: ${modelFile}`)
161161

162162
const model = (await import(modelFile)).default as Model
163-
const tableName = model.table
163+
const tableName = await modelTableName(model)
164164
const modelName = path.basename(modelFile, '.ts')
165165

166166
await generateApiRoutes(model)
@@ -250,7 +250,7 @@ async function setKyselyTypes() {
250250

251251
for (const modelFile of modelFiles) {
252252
const model = (await import(modelFile)).default as Model
253-
const tableName = model.table
253+
const tableName = await modelTableName(model)
254254
const formattedTableName = tableName.charAt(0).toUpperCase() + tableName.slice(1)
255255
const modelName = model.name
256256

@@ -281,7 +281,7 @@ async function setKyselyTypes() {
281281

282282
for (const modelFile of modelFiles) {
283283
const model = (await import(modelFile)).default as Model
284-
const tableName = model.table
284+
const tableName = await modelTableName(model)
285285
const formattedTableName = tableName.charAt(0).toUpperCase() + tableName.slice(1)
286286
const pivotTables = await getPivotTables(model)
287287

storage/framework/core/database/src/drivers/mysql.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function generateMysqlMigration(modelPath: string) {
7070

7171
const model = (await import(modelPath)).default as Model
7272
const fileName = path.basename(modelPath)
73-
const tableName = model.table
73+
const tableName = await modelTableName(model)
7474

7575
const fieldsString = JSON.stringify(model.attributes, null, 2) // Pretty print the JSON
7676
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)
@@ -121,14 +121,14 @@ async function getPivotTables(
121121
const modelRelationPath = path.userModelsPath(`${belongsToManyRelation.model}.ts`)
122122
const modelRelation = (await import(modelRelationPath)).default
123123
const formattedModelName = model.name.toLowerCase()
124-
124+
125125
pivotTable.push({
126126
table: belongsToManyRelation?.pivotTable || `${formattedModelName}_${modelRelation.table}`,
127127
firstForeignKey: belongsToManyRelation.firstForeignKey,
128128
secondForeignKey: belongsToManyRelation.secondForeignKey,
129129
})
130130
}
131-
131+
132132
return pivotTable
133133
}
134134
}
@@ -140,7 +140,7 @@ async function createTableMigration(modelPath: string) {
140140
log.debug('createTableMigration modelPath:', modelPath)
141141

142142
const model = (await import(modelPath)).default as Model
143-
const tableName = model.table
143+
const tableName = await modelTableName(model)
144144

145145
await createPivotTableMigration(model)
146146

@@ -241,7 +241,7 @@ export async function createAlterTableMigration(modelPath: string) {
241241

242242
const model = (await import(modelPath)).default as Model
243243
const modelName = path.basename(modelPath)
244-
const tableName = model.table
244+
const tableName = await modelTableName(model)
245245

246246
// Assuming you have a function to get the fields from the last migration
247247
// For simplicity, this is not implemented here
@@ -288,7 +288,7 @@ export async function fetchMysqlTables(): Promise<string[]> {
288288

289289
for (const modelPath of modelFiles) {
290290
const model = (await import(modelPath)).default as Model
291-
const tableName = model.table as string
291+
const tableName = await modelTableName(model)
292292

293293
tables.push(tableName)
294294
}

storage/framework/core/database/src/drivers/postgres.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { italic, log } from '@stacksjs/cli'
22
import { db } from '@stacksjs/database'
33
import { ok } from '@stacksjs/error-handling'
4+
import { modelTableName } from '@stacksjs/orm'
45
import { path } from '@stacksjs/path'
56
import { fs, glob } from '@stacksjs/storage'
67
import type { Attribute, Attributes } from '@stacksjs/types'
@@ -71,7 +72,7 @@ export async function generatePostgresMigration(modelPath: string) {
7172

7273
const model = await import(modelPath)
7374
const fileName = path.basename(modelPath)
74-
const tableName = model.default.table
75+
const tableName = await modelTableName(model)
7576

7677
const fieldsString = JSON.stringify(model.default.attributes, null, 2) // Pretty print the JSON
7778
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)
@@ -115,7 +116,7 @@ async function createTableMigration(modelPath: string) {
115116
log.debug('createTableMigration modelPath:', modelPath)
116117

117118
const model = await import(modelPath)
118-
const tableName = model.default.table
119+
const tableName = await modelTableName(model)
119120

120121
await createPivotTableMigration(model)
121122

@@ -241,7 +242,7 @@ export async function createAlterTableMigration(modelPath: string) {
241242

242243
const model = await import(modelPath)
243244
const modelName = path.basename(modelPath)
244-
const tableName = model.default.table
245+
const tableName = await modelTableName(model)
245246

246247
// Assuming you have a function to get the fields from the last migration
247248
// For simplicity, this is not implemented here
@@ -288,7 +289,7 @@ export async function fetchMysqlTables(): Promise<string[]> {
288289
for (const modelPath of modelFiles) {
289290
const model = await import(modelPath)
290291

291-
const tableName = model.default.table
292+
const tableName = await modelTableName(model)
292293

293294
tables.push(tableName)
294295
}

storage/framework/core/database/src/drivers/sqlite.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ export async function generateSqliteMigration(modelPath: string) {
6767
}
6868
}
6969

70-
const model = await import(modelPath)
70+
const model = await import(modelPath) as Model
7171
const fileName = path.basename(modelPath)
72-
const tableName = model.default.table
72+
const tableName = await modelTableName(model)
7373

7474
const fieldsString = JSON.stringify(model.default.attributes, null, 2) // Pretty print the JSON
7575
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)
@@ -237,15 +237,15 @@ async function createPivotTableMigration(model: Model) {
237237
export async function createAlterTableMigration(modelPath: string) {
238238
console.log('createAlterTableMigration')
239239

240-
const model = await import(modelPath)
240+
const model = (await import(modelPath)).default as Model
241241
const modelName = path.basename(modelPath)
242-
const tableName = model.default.table
242+
const tableName = await modelTableName(model)
243243

244244
// Assuming you have a function to get the fields from the last migration
245245
// For simplicity, this is not implemented here
246246
const lastMigrationFields = await getLastMigrationFields(modelName)
247247
const lastFields = lastMigrationFields ?? {}
248-
const currentFields = model.default.attributes as Attributes
248+
const currentFields = model.attributes as Attributes
249249

250250
// Determine fields to add and remove
251251
const fieldsToAdd = Object.keys(currentFields)

storage/framework/core/database/src/migrations.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ export async function runDatabaseMigration() {
5555
return ok('No new migrations were executed')
5656
}
5757

58-
if (results) {
59-
log.success('Database migrated successfully.')
58+
if (results)
6059
return ok(results)
61-
}
6260

6361
log.success('Database migration completed with no new migrations.')
6462
return ok('Database migration completed with no new migrations.')

storage/framework/core/database/src/seeder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { italic, log } from '@stacksjs/cli'
22
import { db } from '@stacksjs/database'
3+
import { modelTableName } from '@stacksjs/orm'
34
import { path } from '@stacksjs/path'
45
import { fs, glob } from '@stacksjs/storage'
56
import { snakeCase } from '@stacksjs/strings'
@@ -15,7 +16,7 @@ async function seedModel(name: string, model?: Model) {
1516

1617
if (!model) model = (await import(path.userModelsPath(name))) as Model
1718

18-
const tableName = model.table ?? snakeCase(model.name ?? name.replace('.ts', ''))
19+
const tableName = await modelTableName(model)
1920
const seedCount =
2021
typeof model.traits?.useSeeder === 'object' && model.traits?.useSeeder?.count ? model.traits.useSeeder.count : 10
2122

0 commit comments

Comments
 (0)