Skip to content

Commit d44944e

Browse files
chore: wip
1 parent 4360d93 commit d44944e

File tree

14 files changed

+357
-4571
lines changed

14 files changed

+357
-4571
lines changed

storage/framework/core/db/src/migrations/migrate.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { generateMigration, config } from 'bun-query-builder'
1+
import { generateMigration, config, GenerateMigrationResult } from 'bun-query-builder'
2+
import { userModelsPath } from '@stacksjs/path'
23

34
export { generateMigration as generateMigration2 } from 'bun-query-builder'
45

5-
export async function generateMigrations2(): Promise<any> {
6-
return await generateMigration('./models', {
6+
export async function generateMigrations2(): Promise<GenerateMigrationResult> {
7+
return await generateMigration(userModelsPath(), {
78
dialect: config.dialect,
89
apply: true,
910
full: true
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import { db2 } from '../../db'
2+
3+
class UserModel {
4+
private readonly hidden: string[] = ['password']
5+
private readonly fillable: string[] = ['name', 'email', 'password', 'uuid', 'two_factor_secret', 'public_key']
6+
private readonly guarded: string[] = []
7+
protected attributes: Record<string, any> = {}
8+
private query: any
9+
10+
constructor(data?: Record<string, any>) {
11+
if (data) {
12+
this.attributes = { ...data }
13+
}
14+
this.query = db2.selectFrom('users')
15+
}
16+
17+
// Static method to create a new query instance
18+
static query() {
19+
return new UserModel()
20+
}
21+
22+
// Find by ID
23+
static async find(id: number) {
24+
const result = await db2.selectFrom('users').where('id', '=', id).executeTakeFirst()
25+
return result ? new UserModel(result) : undefined
26+
}
27+
28+
// Get all records
29+
static async all() {
30+
const results = await db2.selectFrom('users').execute()
31+
return results.map((result: any) => new UserModel(result))
32+
}
33+
34+
// Get the first record
35+
async first() {
36+
const result = await this.query.executeTakeFirst()
37+
return result ? new UserModel(result) : undefined
38+
}
39+
40+
// Get all records from the query
41+
async get() {
42+
const results = await this.query.execute()
43+
return results.map((result: any) => new UserModel(result))
44+
}
45+
46+
// Chainable where clause
47+
where(column: string, operator: any, value?: any) {
48+
if (value === undefined) {
49+
this.query = this.query.where(column, '=', operator)
50+
} else {
51+
this.query = this.query.where(column, operator, value)
52+
}
53+
return this
54+
}
55+
56+
// Chainable select clause
57+
select(...columns: string[]) {
58+
this.query = this.query.select(columns as any)
59+
return this
60+
}
61+
62+
// Chainable orderBy clause
63+
orderBy(column: string, direction: 'asc' | 'desc' = 'asc') {
64+
this.query = this.query.orderBy(column, direction)
65+
return this
66+
}
67+
68+
// Chainable limit clause
69+
limit(count: number) {
70+
this.query = this.query.limit(count)
71+
return this
72+
}
73+
74+
// Create a new record
75+
static async create(data: Record<string, any>) {
76+
const instance = new UserModel()
77+
78+
// Filter based on fillable and guarded
79+
const filteredData = Object.fromEntries(
80+
Object.entries(data).filter(([key]) =>
81+
!instance.guarded.includes(key) && instance.fillable.includes(key)
82+
)
83+
)
84+
85+
const result = await db2.insertInto('users')
86+
.values(filteredData)
87+
.execute()
88+
89+
// Fetch the created record
90+
const created = await db2.selectFrom('users')
91+
.where('id', '=', Number((result as any).insertId))
92+
.executeTakeFirst()
93+
94+
return created ? new UserModel(created) : undefined
95+
}
96+
97+
// Update the current record
98+
async update(data: Record<string, any>) {
99+
if (!this.attributes.id) {
100+
throw new Error('Cannot update a model without an ID')
101+
}
102+
103+
// Filter based on fillable and guarded
104+
const filteredData = Object.fromEntries(
105+
Object.entries(data).filter(([key]) =>
106+
!this.guarded.includes(key) && this.fillable.includes(key)
107+
)
108+
)
109+
110+
await (db2 as any).updateTable('users')
111+
.set(filteredData)
112+
.where('id', '=', this.attributes.id)
113+
.execute()
114+
115+
// Fetch the updated record
116+
const updated = await db2.selectFrom('users')
117+
.where('id', '=', this.attributes.id)
118+
.executeTakeFirst()
119+
120+
if (updated) {
121+
this.attributes = { ...updated }
122+
}
123+
124+
return this
125+
}
126+
127+
// Delete the current record
128+
async delete() {
129+
if (!this.attributes.id) {
130+
throw new Error('Cannot delete a model without an ID')
131+
}
132+
133+
await (db2 as any).deleteFrom('users')
134+
.where('id', '=', this.attributes.id)
135+
.execute()
136+
137+
return true
138+
}
139+
140+
// Convert to JSON (excluding hidden fields)
141+
toJSON() {
142+
const json = { ...this.attributes }
143+
144+
for (const field of this.hidden) {
145+
delete json[field]
146+
}
147+
148+
return json
149+
}
150+
}
151+
152+
export default UserModel

0 commit comments

Comments
 (0)