Skip to content

Commit 86b1774

Browse files
chore: wip
1 parent 4be7d33 commit 86b1774

File tree

6 files changed

+67
-7
lines changed

6 files changed

+67
-7
lines changed

storage/framework/core/buddy/src/commands/search.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CLI, SearchOptions } from '@stacksjs/types'
1+
import type { CLI, SearchCommandOptions } from '@stacksjs/types'
22
import process from 'node:process'
33
import { runAction } from '@stacksjs/actions'
44
import { intro, log, outro } from '@stacksjs/cli'
@@ -23,7 +23,7 @@ export function search(buddy: CLI): void {
2323
.option('-s, --settings [settings]', descriptions.settings, { default: false })
2424
.option('--verbose', descriptions.verbose, { default: false })
2525
.option('-p, --project [project]', descriptions.project, { default: false })
26-
.action(async (options: SearchOptions) => {
26+
.action(async (options: SearchCommandOptions) => {
2727
log.debug('Running `search-engine:update` ...', options)
2828

2929
let actionString = Action.SearchEngineImport
@@ -68,7 +68,7 @@ export function search(buddy: CLI): void {
6868
.command('search-engine:settings', descriptions.list)
6969
.option('-m, --model [model]', descriptions.model, { default: false })
7070
.option('--verbose', descriptions.verbose, { default: false })
71-
.action(async (options: SearchOptions) => {
71+
.action(async (options: SearchCommandOptions) => {
7272
if (!options.model)
7373
log.error('Missing required option --model')
7474

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,55 @@ export async function writeModelNames(): Promise<void> {
279279
await fs.promises.writeFile(typeFilePath, fileString, 'utf8')
280280
}
281281

282+
export async function writeTableNames(): Promise<void> {
283+
const models = globSync([path.userModelsPath('*.ts')], { absolute: true })
284+
const coreModelFiles = globSync([path.storagePath('framework/database/models/generated/*.ts')], { absolute: true })
285+
286+
let fileString = `export type TableNames = `
287+
288+
for (let i = 0; i < models.length; i++) {
289+
const modelPath = models[i] as string
290+
const model = (await import(modelPath)).default as Model
291+
const tableName = getTableName(model, modelPath)
292+
293+
const pivotTables = await getPivotTables(model, modelPath)
294+
295+
for (const pivot of pivotTables) {
296+
fileString += `'${pivot.table}'`
297+
fileString += ' | '
298+
}
299+
300+
fileString += `'${tableName}'`
301+
302+
if (i < models.length - 1) {
303+
fileString += ' | '
304+
}
305+
}
306+
307+
fileString += ' | '
308+
309+
for (let j = 0; j < coreModelFiles.length; j++) {
310+
const modelPath = coreModelFiles[j] as string
311+
312+
const model = (await import(modelPath)).default as Model
313+
const tableName = getTableName(model, modelPath)
314+
315+
fileString += `'${tableName}'`
316+
317+
if (j < coreModelFiles.length - 1) {
318+
fileString += ' | '
319+
}
320+
}
321+
322+
// Ensure the directory exists
323+
const typesDir = path.dirname(path.typesPath(`src/table-names.ts`))
324+
await fs.promises.mkdir(typesDir, { recursive: true })
325+
326+
// Write to the file
327+
const typeFilePath = path.typesPath(`src/table-names.ts`)
328+
await fs.promises.writeFile(typeFilePath, fileString, 'utf8')
329+
}
330+
282331
export async function writeModelRequest(): Promise<void> {
283332
const modelFiles = globSync([path.userModelsPath('*.ts')], { absolute: true })
284333
const requestD = Bun.file(path.frameworkPath('types/requests.d.ts'))
@@ -2116,6 +2165,15 @@ export async function generateModelFiles(modelStringFile?: string): Promise<void
21162165
handleError('Error while writing Model Names', error)
21172166
}
21182167

2168+
try {
2169+
log.info('Writing Table Names...')
2170+
await writeTableNames()
2171+
log.success('Wrote Table Names')
2172+
}
2173+
catch (error) {
2174+
handleError('Error while writing Table Names', error)
2175+
}
2176+
21192177
try {
21202178
log.info('Writing Model Requests...')
21212179
await writeModelRequest()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export interface CleanOptions extends CliOptions {}
365365

366366
export interface SaasOptions extends CliOptions {}
367367

368-
export interface SearchOptions extends CliOptions {
368+
export interface SearchCommandOptions extends CliOptions {
369369
model: string
370370
settings: boolean
371371
flush: boolean

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export * from './sms'
5757
export * from './ssg'
5858
export * from './stacks'
5959
export * from './storage'
60+
export * from './table-names'
6061
export * from './tables'
6162
export * from './team'
6263
export * from './ui'

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ModelNames } from '@stacksjs/types'
1+
import type { ModelNames, TableNames } from '@stacksjs/types'
22
import type { VineBoolean, VineNumber, VineString } from '@vinejs/vine'
33
import type { DeepPartial, Nullable } from '.'
44
import type { SearchOptions } from './search-engine'
@@ -183,13 +183,13 @@ export interface Attributes {
183183
export interface RelationConfig {
184184
relationship: string
185185
model: string
186-
table: string
186+
table: TableNames
187187
relationModel?: string
188188
relationTable?: string
189189
foreignKey: string
190190
modelKey: string
191191
relationName?: string
192192
throughModel?: string
193193
throughForeignKey?: string
194-
pivotTable: string
194+
pivotTable: TableNames
195195
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type TableNames = 'projects' | 'subscriber_emails' | 'personal_access_tokens' | 'personal_access_token_teams' | 'team_users' | 'teams' | 'subscribers' | 'deployments' | 'releases' | 'team_users' | 'users' | 'posts' | 'subscriptions' | 'errors'

0 commit comments

Comments
 (0)