Skip to content

Commit f08bac9

Browse files
chore: wip
1 parent 3be1fda commit f08bac9

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

.stacks/core/actions/src/generate/model-classes.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { db } from '@stacksjs/database'
2+
23
import type { Collection } from '@stacksjs/collections'
34
import { collect } from '@stacksjs/collections'
45

@@ -8,50 +9,54 @@ export interface User {
89
email: string
910
password: string
1011
created_at: Date
12+
deleted_at: Date
1113
}
1214

15+
export type UserColumn = keyof User
16+
export type UserColumns = Array<keyof User>
17+
1318
export class UserModel {
1419
private _data: User | undefined = undefined
1520

1621
private _isSelectInvoked = false
1722
public _id: number | undefined = undefined
18-
private cols: string[] = []
23+
private cols: UserColumns = []
1924
private useSoftDeletes = true
25+
private keyName: keyof User = 'id'
2026

2127
queryBuilder = db.selectFrom('users')// Initialize queryBuilder
2228
queryBuilderStore = db.insertInto('users')// Initialize queryBuilder
2329
queryBuilderUpdate = db.updateTable('users')// Initialize queryBuilder
2430
queryBuilderDelete = db.deleteFrom('users')// Initialize queryBuilder
2531

26-
private getKeyName(): string {
27-
return 'id'
28-
}
29-
3032
public async find(id: number | number[]): Promise<User | Collection<User>> {
3133
if (Array.isArray(id))
3234
return await this.findMany(id)
3335

3436
let query = this.queryBuilder.selectAll()
35-
.where(this.getKeyName(), '=', id)
37+
.where(this.keyName, '=', id)
3638

3739
if (this.useSoftDeletes)
3840
query = query.where('deleted_at', 'is', null)
3941

4042
this._data = await query.executeTakeFirst()
4143

44+
if (!this._data)
45+
throw new Error('User not found!')
46+
4247
return this._createProxy()
4348
}
4449

4550
public async findMany(id: number[] | string[]): Promise<User | Collection<User>> {
46-
return await this.whereIn(this.getKeyName(), id).get()
51+
return await this.whereIn(this.keyName, id).get()
4752
}
4853

49-
private _createProxy(): User & { [key: string]: Function } {
54+
private _createProxy() {
5055
return new Proxy(this._data || {}, {
51-
get: (target, prop: keyof User | string): any => {
56+
get: (target: User, prop: UserColumn) => {
5257
// Property lookup in the User data
5358
if (prop in target)
54-
return target[prop as keyof User]
59+
return target[prop]
5560

5661
// If it's a method on the UserModel, bind it
5762
const method = this[prop as keyof this]
@@ -63,12 +68,16 @@ export class UserModel {
6368
})
6469
}
6570

66-
public update(obj: Partial<User>): any {
71+
public async update(obj: Partial<User>): Promise<User | undefined | Collection<User>> {
6772
if (this._data && this._data?.id) {
68-
return this.queryBuilderUpdate.set(obj)
69-
.where(this.getKeyName(), '=', this._data?.id)
73+
await this.queryBuilderUpdate.set(obj)
74+
.where(this.keyName, '=', this._data?.id)
7075
.executeTakeFirst()
76+
77+
return await this.find(this._data?.id)
7178
}
79+
80+
return undefined
7281
}
7382

7483
public async all() {
@@ -103,7 +112,7 @@ export class UserModel {
103112
public where(...args: (string | number | boolean)[]): this {
104113
if (args.length === 2) {
105114
const [column, value] = args
106-
this.queryBuilder = this.queryBuilder.where(column, '=', value)
115+
this.queryBuilder = this.queryBuilder.where(column as UserColumn, '=', value)
107116
}
108117
else { this.queryBuilder = this.queryBuilder.where(...args) }
109118

@@ -128,7 +137,7 @@ export class UserModel {
128137
return this
129138
}
130139

131-
public whereNull(col: string): this {
140+
public whereNull(col: UserColumn): this {
132141
this.queryBuilder = this.queryBuilder.where(col, 'is', null)
133142

134143
return this
@@ -141,7 +150,7 @@ export class UserModel {
141150
return this
142151
}
143152

144-
public whereNotNull(col: string): this {
153+
public whereNotNull(col: UserColumn): this {
145154
this.queryBuilder = this.queryBuilder.where(col, 'is not', null)
146155

147156
return this
@@ -153,7 +162,7 @@ export class UserModel {
153162
return this
154163
}
155164

156-
public distinctOn(col: string): this {
165+
public distinctOn(col: UserColumn): this {
157166
this.queryBuilder = this.queryBuilder.distinctOn(col)
158167

159168
return this
@@ -165,13 +174,13 @@ export class UserModel {
165174
return this
166175
}
167176

168-
public orderBy(col: string) {
177+
public orderBy(col: UserColumn) {
169178
this.queryBuilder = this.queryBuilder.orderBy(col)
170179

171180
return this
172181
}
173182

174-
public groupBy(col: string) {
183+
public groupBy(col: UserColumn) {
175184
this.queryBuilder = this.queryBuilder.groupBy(col)
176185

177186
return this
@@ -231,13 +240,13 @@ export class UserModel {
231240
return this
232241
}
233242

234-
public orderByDesc(col: string): this {
243+
public orderByDesc(col: UserColumn): this {
235244
this.queryBuilder = this.queryBuilder.orderBy(col, 'desc')
236245

237246
return this
238247
}
239248

240-
public select(...args: string[]): this {
249+
public select(...args: UserColumns): this {
241250
this.cols = args
242251
this._isSelectInvoked = true
243252

@@ -281,19 +290,17 @@ export class UserModel {
281290

282291
public forceDelete(): any {
283292
if (this._data && this._data?.id) {
284-
return this.queryBuilderDelete.where(this.getKeyName(), '=', this._data?.id)
293+
return this.queryBuilderDelete.where(this.keyName, '=', this._data?.id)
285294
.executeTakeFirst()
286295
}
287296
}
288297
}
289298

290299
const UserInstance = new UserModel()
291300

292-
const user = await UserInstance.when(true, (query) => {
293-
// Modify the query as needed
294-
return query.where('id', 1)
295-
}).get()
301+
const user = await UserInstance.find(1)
296302

297-
console.log(user)
303+
const res = await user.update({ name: 'glenn 2' })
298304

305+
console.log(res)
299306
process.exit(0)

.stacks/core/database/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface UsersTable {
3030
// a `Date`, can optionally be provided as a `string` in inserts and
3131
// can never be updated:
3232
created_at: ColumnType<Date, string | undefined, never>
33+
deleted_at: ColumnType<Date, string | undefined, never>
3334
}
3435

3536
export interface Database {
@@ -46,7 +47,7 @@ if (driver === 'sqlite') {
4647
else {
4748
dialect = new MysqlDialect({
4849
pool: createPool({
49-
database: 'carefree',
50+
database: 'stacks',
5051
host: '127.0.0.1',
5152
user: 'root',
5253
password: '',

0 commit comments

Comments
 (0)