Skip to content

Commit 1171920

Browse files
chore: wip
1 parent 0e304bb commit 1171920

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

storage/framework/core/db/src/models/User.ts renamed to storage/framework/core/db/src/orm/Models/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { SelectQueryBuilder } from 'bun-query-builder'
2-
import { db2, schema } from '../db'
2+
import { db2, schema } from '../../db'
33

44
class UserModel {
55
private query: SelectQueryBuilder<typeof schema, 'users', Record<string, unknown>, 'users'>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { path } from '@stacksjs/path'
2+
import { fs } from '@stacksjs/storage'
3+
import { plural, snakeCase } from '@stacksjs/strings'
4+
import type { Model } from '@stacksjs/types'
5+
import { globSync } from 'tinyglobby'
6+
7+
export function getModelName(model: Model, modelPath: string): string {
8+
if (model.name)
9+
return model.name
10+
11+
const baseName = path.basename(modelPath)
12+
13+
return baseName.replace(/\.ts$/, '')
14+
}
15+
16+
export function getTableName(model: Model, modelPath: string): string {
17+
if (model.table)
18+
return model.table
19+
20+
return snakeCase(plural(getModelName(model, modelPath)))
21+
}
22+
23+
export function generateOrmModelString(modelName: string, tableName: string): string {
24+
return `import { db2 } from '../../db'
25+
26+
class ${modelName}Model {
27+
private query: any
28+
29+
constructor() {
30+
this.query = db2.selectFrom('${tableName}')
31+
}
32+
33+
// Static method to create a new query instance
34+
static query() {
35+
return new ${modelName}Model()
36+
}
37+
38+
// Find by ID
39+
static async find(id: number) {
40+
return await db2.selectFrom('${tableName}').where('id', '=', id).executeTakeFirst()
41+
}
42+
43+
// Get all records
44+
static async all() {
45+
return await db2.selectFrom('${tableName}').execute()
46+
}
47+
48+
// Get the first record
49+
async first() {
50+
return await this.query.executeTakeFirst()
51+
}
52+
53+
// Get all records from the query
54+
async get() {
55+
return await this.query.execute()
56+
}
57+
58+
// Chainable where clause
59+
where(column: string, operator: any, value?: any) {
60+
if (value === undefined) {
61+
this.query = this.query.where(column, '=', operator)
62+
} else {
63+
this.query = this.query.where(column, operator, value)
64+
}
65+
return this
66+
}
67+
68+
// Chainable select clause
69+
select(...columns: string[]) {
70+
this.query = this.query.select(columns as any)
71+
return this
72+
}
73+
74+
// Chainable orderBy clause
75+
orderBy(column: string, direction: 'asc' | 'desc' = 'asc') {
76+
this.query = this.query.orderBy(column, direction)
77+
return this
78+
}
79+
80+
// Chainable limit clause
81+
limit(count: number) {
82+
this.query = this.query.limit(count)
83+
return this
84+
}
85+
}
86+
87+
export default ${modelName}Model
88+
`
89+
}
90+
91+
export async function generateOrmModels(): Promise<void> {
92+
const userModelFiles = globSync([
93+
path.userModelsPath('*.ts'),
94+
path.storagePath('framework/defaults/models/**/*.ts'),
95+
], { absolute: true })
96+
97+
for (const userModelFile of userModelFiles) {
98+
const model = (await import(userModelFile)).default as Model
99+
const modelName = getModelName(model, userModelFile)
100+
const tableName = getTableName(model, userModelFile)
101+
102+
const modelString = generateOrmModelString(modelName, tableName)
103+
104+
// Ensure the directory exists
105+
const ormModelsDir = path.storagePath('framework/core/db/src/orm/Models')
106+
await fs.promises.mkdir(ormModelsDir, { recursive: true })
107+
108+
// Write the generated model file
109+
const outputPath = path.join(ormModelsDir, `${modelName}.ts`)
110+
await fs.promises.writeFile(outputPath, modelString, 'utf8')
111+
112+
console.log(`Generated ORM model: ${modelName} -> ${outputPath}`)
113+
}
114+
}
115+
116+
export async function generateOrmModel(modelPath: string): Promise<void> {
117+
const model = (await import(modelPath)).default as Model
118+
const modelName = getModelName(model, modelPath)
119+
const tableName = getTableName(model, modelPath)
120+
121+
const modelString = generateOrmModelString(modelName, tableName)
122+
123+
// Ensure the directory exists
124+
const ormModelsDir = path.storagePath('framework/core/db/src/orm/Models')
125+
await fs.promises.mkdir(ormModelsDir, { recursive: true })
126+
127+
// Write the generated model file
128+
const outputPath = path.join(ormModelsDir, `${modelName}.ts`)
129+
await fs.promises.writeFile(outputPath, modelString, 'utf8')
130+
131+
console.log(`Generated ORM model: ${modelName} -> ${outputPath}`)
132+
}
133+

0 commit comments

Comments
 (0)