Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed May 18, 2024
1 parent 0af70a0 commit cb9eb7f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions storage/framework/core/actions/src/orm/generate-model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { log } from '@stacksjs/logging'
import { modelTableName } from '@stacksjs/orm'
import { path } from '@stacksjs/path'
import { fs, glob } from '@stacksjs/storage'
import { pascalCase } from '@stacksjs/strings'
import type { Model, RelationConfig } from '@stacksjs/types'
import { isString } from '@stacksjs/validation'
import { modelTableName } from '@stacksjs/orm'

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

const model = (await import(modelFile)).default as Model
const tableName = model.table
const tableName = await modelTableName(model)
const modelName = path.basename(modelFile, '.ts')

await generateApiRoutes(model)
Expand Down Expand Up @@ -250,7 +250,7 @@ async function setKyselyTypes() {

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

Expand Down Expand Up @@ -281,7 +281,7 @@ async function setKyselyTypes() {

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

Expand Down
12 changes: 6 additions & 6 deletions storage/framework/core/database/src/drivers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function generateMysqlMigration(modelPath: string) {

const model = (await import(modelPath)).default as Model
const fileName = path.basename(modelPath)
const tableName = model.table
const tableName = await modelTableName(model)

const fieldsString = JSON.stringify(model.attributes, null, 2) // Pretty print the JSON
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)
Expand Down Expand Up @@ -121,14 +121,14 @@ async function getPivotTables(
const modelRelationPath = path.userModelsPath(`${belongsToManyRelation.model}.ts`)
const modelRelation = (await import(modelRelationPath)).default
const formattedModelName = model.name.toLowerCase()

pivotTable.push({
table: belongsToManyRelation?.pivotTable || `${formattedModelName}_${modelRelation.table}`,
firstForeignKey: belongsToManyRelation.firstForeignKey,
secondForeignKey: belongsToManyRelation.secondForeignKey,
})
}

return pivotTable
}
}
Expand All @@ -140,7 +140,7 @@ async function createTableMigration(modelPath: string) {
log.debug('createTableMigration modelPath:', modelPath)

const model = (await import(modelPath)).default as Model
const tableName = model.table
const tableName = await modelTableName(model)

await createPivotTableMigration(model)

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

const model = (await import(modelPath)).default as Model
const modelName = path.basename(modelPath)
const tableName = model.table
const tableName = await modelTableName(model)

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

for (const modelPath of modelFiles) {
const model = (await import(modelPath)).default as Model
const tableName = model.table as string
const tableName = await modelTableName(model)

tables.push(tableName)
}
Expand Down
9 changes: 5 additions & 4 deletions storage/framework/core/database/src/drivers/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { italic, log } from '@stacksjs/cli'
import { db } from '@stacksjs/database'
import { ok } from '@stacksjs/error-handling'
import { modelTableName } from '@stacksjs/orm'
import { path } from '@stacksjs/path'
import { fs, glob } from '@stacksjs/storage'
import type { Attribute, Attributes } from '@stacksjs/types'
Expand Down Expand Up @@ -71,7 +72,7 @@ export async function generatePostgresMigration(modelPath: string) {

const model = await import(modelPath)
const fileName = path.basename(modelPath)
const tableName = model.default.table
const tableName = await modelTableName(model)

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

const model = await import(modelPath)
const tableName = model.default.table
const tableName = await modelTableName(model)

await createPivotTableMigration(model)

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

const model = await import(modelPath)
const modelName = path.basename(modelPath)
const tableName = model.default.table
const tableName = await modelTableName(model)

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

const tableName = model.default.table
const tableName = await modelTableName(model)

tables.push(tableName)
}
Expand Down
10 changes: 5 additions & 5 deletions storage/framework/core/database/src/drivers/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export async function generateSqliteMigration(modelPath: string) {
}
}

const model = await import(modelPath)
const model = await import(modelPath) as Model
const fileName = path.basename(modelPath)
const tableName = model.default.table
const tableName = await modelTableName(model)

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

const model = await import(modelPath)
const model = (await import(modelPath)).default as Model
const modelName = path.basename(modelPath)
const tableName = model.default.table
const tableName = await modelTableName(model)

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

// Determine fields to add and remove
const fieldsToAdd = Object.keys(currentFields)
Expand Down
4 changes: 1 addition & 3 deletions storage/framework/core/database/src/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ export async function runDatabaseMigration() {
return ok('No new migrations were executed')
}

if (results) {
log.success('Database migrated successfully.')
if (results)
return ok(results)
}

log.success('Database migration completed with no new migrations.')
return ok('Database migration completed with no new migrations.')
Expand Down
3 changes: 2 additions & 1 deletion storage/framework/core/database/src/seeder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { italic, log } from '@stacksjs/cli'
import { db } from '@stacksjs/database'
import { modelTableName } from '@stacksjs/orm'
import { path } from '@stacksjs/path'
import { fs, glob } from '@stacksjs/storage'
import { snakeCase } from '@stacksjs/strings'
Expand All @@ -15,7 +16,7 @@ async function seedModel(name: string, model?: Model) {

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

const tableName = model.table ?? snakeCase(model.name ?? name.replace('.ts', ''))
const tableName = await modelTableName(model)
const seedCount =
typeof model.traits?.useSeeder === 'object' && model.traits?.useSeeder?.count ? model.traits.useSeeder.count : 10

Expand Down

0 comments on commit cb9eb7f

Please sign in to comment.