Skip to content

Commit a72bd24

Browse files
chore: wip
1 parent d09d26b commit a72bd24

File tree

6 files changed

+2018
-1864
lines changed

6 files changed

+2018
-1864
lines changed

storage/framework/core/commerce/src/orders/store.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Import dependencies
22
import type { ProductUnitRequestType } from '@stacksjs/orm'
3-
import { db } from '@stacksjs/database'
43
import type { NewProductUnit, ProductUnitJsonResponse } from '../../../../orm/src/models/ProductUnit'
4+
import { db } from '@stacksjs/database'
55

66
/**
77
* Create a new product unit
@@ -96,7 +96,7 @@ export async function bulkStore(requests: ProductUnitRequestType[]): Promise<num
9696
.executeTakeFirst()
9797

9898
const unitId = Number(result.insertId)
99-
99+
100100
// If this unit is set as default, update all other units of the same type
101101
if (unitData.is_default) {
102102
await trx
@@ -154,7 +154,7 @@ export function formatUnitOptions(
154154

155155
/**
156156
* Get the default unit for a specific type
157-
*
157+
*
158158
* @param type The unit type to get the default for
159159
* @returns The default unit or undefined if none found
160160
*/
@@ -176,4 +176,4 @@ export async function getDefaultUnit(type: string): Promise<ProductUnitJsonRespo
176176

177177
throw error
178178
}
179-
}
179+
}

storage/framework/core/commerce/src/unit/store.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Import dependencies
22
import type { ProductUnitRequestType } from '@stacksjs/orm'
3-
import { db } from '@stacksjs/database'
43
import type { NewProductUnit, ProductUnitJsonResponse } from '../../../../orm/src/models/ProductUnit'
4+
import { db } from '@stacksjs/database'
55

66
/**
77
* Create a new product unit
@@ -96,7 +96,7 @@ export async function bulkStore(requests: ProductUnitRequestType[]): Promise<num
9696
.executeTakeFirst()
9797

9898
const unitId = Number(result.insertId)
99-
99+
100100
// If this unit is set as default, update all other units of the same type
101101
if (unitData.is_default) {
102102
await trx
@@ -142,13 +142,13 @@ export function formatUnitOptions(
142142
query = query.where('type', '=', type)
143143

144144
// Convert db results to ensure id is string and handle potentially undefined is_default
145-
return query.execute().then(results =>
145+
return query.execute().then(results =>
146146
results.map(result => ({
147147
id: String(result.id), // Convert id to string
148148
name: result.name,
149149
abbreviation: result.abbreviation,
150-
is_default: result.is_default
151-
}))
150+
is_default: result.is_default,
151+
})),
152152
)
153153
}
154154
catch (error) {
@@ -162,7 +162,7 @@ export function formatUnitOptions(
162162

163163
/**
164164
* Get the default unit for a specific type
165-
*
165+
*
166166
* @param type The unit type to get the default for
167167
* @returns The default unit or undefined if none found
168168
*/
@@ -184,4 +184,4 @@ export async function getDefaultUnit(type: string): Promise<ProductUnitJsonRespo
184184

185185
throw error
186186
}
187-
}
187+
}

storage/framework/core/commerce/src/unit/update.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ export async function updateDefaultStatus(id: number, isDefault: boolean): Promi
145145
.select('type')
146146
.where('id', '=', id)
147147
.executeTakeFirst()
148-
148+
149149
if (!unit) {
150150
return false
151151
}
152-
152+
153153
const result = await db
154154
.updateTable('product_units')
155155
.set({
@@ -178,4 +178,4 @@ export async function updateDefaultStatus(id: number, isDefault: boolean): Promi
178178

179179
throw error
180180
}
181-
}
181+
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -857,14 +857,20 @@ export async function generateModelString(
857857

858858
const otherModelRelations = await fetchOtherModelRelations(modelName)
859859

860-
if (useTwoFactor && tableName === 'users')
860+
if (useTwoFactor && tableName === 'users') {
861+
jsonFields += 'two_factor_secret: this.two_factor_secret\n'
861862
fieldString += 'two_factor_secret?: string \n'
863+
}
862864

863-
if (usePasskey && tableName === 'users')
865+
if (usePasskey && tableName === 'users') {
866+
jsonFields += 'public_passkey: this.public_passkey\n'
864867
fieldString += 'public_passkey?: string \n'
868+
}
865869

866-
if (useBillable && tableName === 'users')
870+
if (useBillable && tableName === 'users') {
871+
jsonFields += 'stripe_id: this.stripe_id\n'
867872
fieldString += 'stripe_id?: string \n'
873+
}
868874

869875
if (useUuid)
870876
fieldString += 'uuid?: string \n'
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { cache } from '@stacksjs/cache'
2+
import { ModelNotFoundException } from '@stacksjs/error-handling'
3+
import { DB } from '@stacksjs/orm'
4+
5+
export class BaseOrm {
6+
// Method to find a record by ID
7+
static async find<T>(tableName: string, id: number): Promise<T | undefined> {
8+
const model = await DB.instance.selectFrom(tableName)
9+
.where('id', '=', id)
10+
.selectAll()
11+
.executeTakeFirst()
12+
13+
if (!model)
14+
return undefined
15+
16+
cache.getOrSet(`${tableName}:${id}`, JSON.stringify(model))
17+
18+
return model as T
19+
}
20+
21+
// Method to find a record by ID or fail
22+
static async findOrFail<T>(tableName: string, modelName: string, id: number): Promise<T> {
23+
const model = await DB.instance.selectFrom(tableName)
24+
.where('id', '=', id)
25+
.selectAll()
26+
.executeTakeFirst()
27+
28+
if (model === undefined)
29+
throw new ModelNotFoundException(404, `No ${modelName} results for ${id}`)
30+
31+
cache.getOrSet(`${tableName}:${id}`, JSON.stringify(model))
32+
33+
return model as T
34+
}
35+
36+
// // Method to get the first record
37+
// static async first<T>(tableName: string): Promise<T | undefined> {
38+
// const model = await DB.instance.selectFrom(tableName)
39+
// .selectAll()
40+
// .executeTakeFirst()
41+
42+
// return model as T | undefined
43+
// }
44+
45+
// // Method to get the first record or fail
46+
// static async firstOrFail<T>(tableName: string, modelName: string): Promise<T> {
47+
// const model = await DB.instance.selectFrom(tableName)
48+
// .selectAll()
49+
// .executeTakeFirst()
50+
51+
// if (model === undefined)
52+
// throw new ModelNotFoundException(404, `No ${modelName} results found for query`)
53+
54+
// return model as T
55+
// }
56+
57+
// // Method to get all records
58+
// static async all<T>(tableName: string): Promise<T[]> {
59+
// const models = await DB.instance.selectFrom(tableName)
60+
// .selectAll()
61+
// .execute()
62+
63+
// return models as T[]
64+
// }
65+
66+
// // Method to create a record
67+
// static async create<T>(tableName: string, data: any, eventName?: string): Promise<T> {
68+
// const result = await DB.instance.insertInto(tableName)
69+
// .values(data)
70+
// .executeTakeFirst()
71+
72+
// const model = await this.find<T>(tableName, Number(result.numInsertedOrUpdatedRows))
73+
74+
// if (model && eventName)
75+
// dispatch(eventName, model)
76+
77+
// return model as T
78+
// }
79+
80+
// // Method to update a record
81+
// static async update<T>(tableName: string, id: number, data: any, eventName?: string): Promise<T | undefined> {
82+
// await DB.instance.updateTable(tableName)
83+
// .set(data)
84+
// .where('id', '=', id)
85+
// .executeTakeFirst()
86+
87+
// const model = await this.find<T>(tableName, id)
88+
89+
// if (model && eventName)
90+
// dispatch(eventName, model)
91+
92+
// return model
93+
// }
94+
95+
// // Method to delete a record
96+
// static async delete(tableName: string, id: number, eventName?: string): Promise<any> {
97+
// const model = await this.find(tableName, id)
98+
99+
// if (model && eventName)
100+
// dispatch(eventName, model)
101+
102+
// return await DB.instance.deleteFrom(tableName)
103+
// .where('id', '=', id)
104+
// .execute()
105+
// }
106+
}

0 commit comments

Comments
 (0)