Skip to content

Commit d110fa5

Browse files
chore: wip
1 parent 29787d0 commit d110fa5

File tree

6 files changed

+84
-5
lines changed

6 files changed

+84
-5
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ async function createTableMigration(modelPath: string) {
174174
}
175175

176176
if (otherModelRelations?.length) {
177-
console.log(otherModelRelations)
178177
for (const modelRelation of otherModelRelations) {
179178
migrationContent += ` .addColumn('${modelRelation.foreignKey}', 'integer', (col) =>
180179
col.references('${modelRelation.relationTable}.id').onDelete('cascade').notNull()

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

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { italic, log } from '@stacksjs/cli'
22
import { db } from '@stacksjs/database'
33
import { path } from '@stacksjs/path'
4-
import { fs } from '@stacksjs/storage'
4+
import { fs, glob } from '@stacksjs/storage'
5+
import { isString } from '@stacksjs/validation'
56
import { snakeCase } from '@stacksjs/strings'
6-
import type { Model } from '@stacksjs/types'
7+
import type { Model, RelationConfig } from '@stacksjs/types'
78
import { generateMigrations, resetDatabase, runDatabaseMigration } from './migrations'
89

910
async function seedModel(name: string, model?: Model) {
@@ -20,20 +21,99 @@ async function seedModel(name: string, model?: Model) {
2021
log.info(`Seeding ${seedCount} records into ${italic(tableName)}`)
2122
const records = []
2223

24+
const otherRelations = await fetchOtherModelRelations(model)
25+
26+
console.log(otherRelations)
27+
2328
for (let i = 0; i < seedCount; i++) {
2429
const record: any = {}
2530
for (const fieldName in model.attributes) {
2631
const field = model.attributes[fieldName]
2732
// Use the factory function if available, otherwise leave the field undefined
2833
record[fieldName] = field?.factory ? field.factory() : undefined
2934
}
35+
36+
if (otherRelations?.length) {
37+
for (let j = 0; j < otherRelations.length; j++) {
38+
const relationElement = otherRelations[j] as RelationConfig
39+
40+
record[relationElement?.foreignKey] = 1 // TODO: make this dynamic
41+
}
42+
}
43+
3044
records.push(record)
3145
}
3246

3347
// @ts-expect-error todo: we can improve this in the future
3448
await db.insertInto(tableName).values(records).execute()
3549
}
3650

51+
export async function getRelations(model: Model): Promise<RelationConfig[]> {
52+
const relationsArray = ['hasOne', 'hasMany', 'belongsToMany', 'hasOneThrough']
53+
const relationships = []
54+
55+
for (const relation of relationsArray) {
56+
if (hasRelations(model, relation)) {
57+
for (const relationInstance of model[relation]) {
58+
let relationModel = relationInstance.model
59+
60+
if (isString(relationInstance)) {
61+
relationModel = relationInstance
62+
}
63+
64+
const modelRelationPath = path.userModelsPath(`${relationModel}.ts`)
65+
const modelRelation = (await import(modelRelationPath)).default
66+
const formattedModelName = model.name.toLowerCase()
67+
68+
relationships.push({
69+
relationship: relation,
70+
model: relationModel,
71+
table: modelRelation.table,
72+
relationModel: model.name,
73+
relationTable: model.table,
74+
foreignKey: relationInstance.foreignKey || `${formattedModelName}_id`,
75+
relationName: relationInstance.relationName || '',
76+
throughModel: relationInstance.through || '',
77+
throughForeignKey: relationInstance.throughForeignKey || '',
78+
pivotTable: relationInstance?.pivotTable || `${formattedModelName}_${modelRelation.table}`,
79+
})
80+
}
81+
}
82+
}
83+
84+
return relationships
85+
}
86+
87+
export async function fetchOtherModelRelations(model: Model) {
88+
89+
const modelFiles = glob.sync(path.userModelsPath('*.ts'))
90+
91+
const modelRelations = []
92+
93+
for (let i = 0; i < modelFiles.length; i++) {
94+
const modelFileElement = modelFiles[i] as string
95+
96+
const modelFile = await import(modelFileElement)
97+
98+
if (model.name === modelFile.default.name) continue
99+
100+
const relations = await getRelations(modelFile.default)
101+
102+
if (! relations.length) continue
103+
104+
const relation = relations.find(relation => relation.model === model.name)
105+
106+
if (relation)
107+
modelRelations.push(relation)
108+
109+
return modelRelations
110+
}
111+
}
112+
113+
function hasRelations(obj: any, key: string): boolean {
114+
return key in obj
115+
}
116+
37117
export async function seed() {
38118
// TODO: need to check other databases too
39119
const dbPath = path.userDatabasePath('stacks.sqlite')

storage/framework/core/types/src/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ export interface RelationConfig {
122122
relationship: string
123123
model: string
124124
table: string
125-
relationModel: string
126-
relationTable: string,
125+
relationModel?: string
126+
relationTable?: string,
127127
foreignKey: string
128128
relationName: string
129129
throughModel: string

0 commit comments

Comments
 (0)