Skip to content

Commit c5e9b94

Browse files
committed
chore: wip
1 parent cb9eb7f commit c5e9b94

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import { log } from '@stacksjs/cli'
22
import { db } from '@stacksjs/database'
33
import { path } from '@stacksjs/path'
44
import { fs } from '@stacksjs/storage'
5-
import { isString } from '@stacksjs/validation'
65
import type { Attributes, Model, RelationConfig, VineType } from '@stacksjs/types'
6+
import { isString } from '@stacksjs/validation'
77

88
export * from './mysql'
99
export * from './postgres'
1010
export * from './sqlite'
1111

1212
export async function getLastMigrationFields(modelName: string): Promise<Attributes> {
1313
const oldModelPath = path.frameworkPath(`database/models/${modelName}`)
14-
const model = await import(oldModelPath)
14+
const model = (await import(oldModelPath)).default as Model
1515
let fields = {} as Attributes
1616

17-
if (typeof model.default.attributes === 'object') fields = model.default.attributes
18-
else fields = JSON.parse(model.default.attributes) as Attributes
17+
if (typeof model.attributes === 'object') fields = model.attributes
18+
else fields = JSON.parse(model.attributes) as Attributes
1919

2020
return fields
2121
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ export async function generatePostgresMigration(modelPath: string) {
7070
}
7171
}
7272

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

77-
const fieldsString = JSON.stringify(model.default.attributes, null, 2) // Pretty print the JSON
77+
const fieldsString = JSON.stringify(model.attributes, null, 2) // Pretty print the JSON
7878
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)
7979

8080
let haveFieldsChanged = false
@@ -115,18 +115,16 @@ export async function generatePostgresMigration(modelPath: string) {
115115
async function createTableMigration(modelPath: string) {
116116
log.debug('createTableMigration modelPath:', modelPath)
117117

118-
const model = await import(modelPath)
118+
const model = (await import(modelPath)).default as Model
119119
const tableName = await modelTableName(model)
120120

121121
await createPivotTableMigration(model)
122122

123123
const modelFiles = glob.sync(path.userModelsPath('*.ts'))
124-
125124
const otherModelRelations = await fetchOtherModelRelations(model, modelFiles)
126-
127-
const fields = model.default.attributes
128-
const useTimestamps = model.default?.traits?.useTimestamps ?? model.default?.traits?.timestampable
129-
const useSoftDeletes = model.default?.traits?.useSoftDeletes ?? model.default?.traits?.softDeletable
125+
const fields = model.attributes
126+
const useTimestamps = model.traits?.useTimestamps ?? model.traits?.timestampable
127+
const useSoftDeletes = model.traits?.useSoftDeletes ?? model.traits?.softDeletable
130128

131129
let migrationContent = `import type { Database } from '@stacksjs/database'\n`
132130
migrationContent += `import { sql } from '@stacksjs/database'\n\n`
@@ -240,15 +238,15 @@ async function getPivotTables(
240238
export async function createAlterTableMigration(modelPath: string) {
241239
console.log('createAlterTableMigration')
242240

243-
const model = await import(modelPath)
241+
const model = (await import(modelPath)).default as Model
244242
const modelName = path.basename(modelPath)
245243
const tableName = await modelTableName(model)
246244

247245
// Assuming you have a function to get the fields from the last migration
248246
// For simplicity, this is not implemented here
249247
const lastMigrationFields = await getLastMigrationFields(modelName)
250248
const lastFields = lastMigrationFields ?? {}
251-
const currentFields = model.default.attributes as Attributes
249+
const currentFields = model.attributes
252250

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

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,16 @@ export async function generateSqliteMigration(modelPath: string) {
6161
if (modelFiles.length) {
6262
log.debug('No existing model files in framework path...')
6363

64-
for (const file of modelFiles) {
64+
for (const file of modelFiles)
6565
if (file.endsWith('.ts')) await fs.unlink(path.frameworkPath(`database/models/${file}`))
66-
}
6766
}
6867
}
6968

70-
const model = await import(modelPath) as Model
69+
const model = (await import(modelPath)).default as Model
7170
const fileName = path.basename(modelPath)
7271
const tableName = await modelTableName(model)
7372

74-
const fieldsString = JSON.stringify(model.default.attributes, null, 2) // Pretty print the JSON
73+
const fieldsString = JSON.stringify(model.attributes, null, 2) // Pretty print the JSON
7574
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)
7675

7776
let haveFieldsChanged = false

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ export async function lastMigrationDate(): Promise<string | undefined> {
159159
// read the last migration file and extract the fields that were modified.
160160
export async function getLastMigrationFields(modelName: string): Promise<Attribute> {
161161
const oldModelPath = path.frameworkPath(`database/models/${modelName}`)
162-
const model = await import(oldModelPath)
162+
const model = (await import(oldModelPath)).default as Model
163163
let fields = {} as Attributes
164164

165-
if (typeof model.default.attributes === 'object') fields = model.default.attributes
166-
else fields = JSON.parse(model.default.attributes) as Attributes
165+
if (typeof model.attributes === 'object') fields = model.attributes
166+
else fields = JSON.parse(model.attributes) as Attributes
167167

168168
return fields
169169
}

storage/framework/core/orm/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function modelTableName(model: Model | ModelPath): Promise<string>
1212
model = (await import(model)).default as Model
1313
}
1414

15-
return model.table ?? snakeCase(plural(model.name ?? modelPath.replace(/.*\/(.*)\.ts$/, '$1')))
15+
return model.table ?? snakeCase(plural(model.name))
1616
}
1717

1818
export async function extractFieldsFromModel(filePath: string) {

0 commit comments

Comments
 (0)