Skip to content

Commit 32ff551

Browse files
chore: wip
1 parent cdf1aaa commit 32ff551

36 files changed

+285
-3435
lines changed

storage/framework/actions/src/PaymentShowOrmAction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { PaymentRequestType } from '@stacksjs/orm'
22
import { Action } from '@stacksjs/actions'
33

4-
import Payment from '../../orm/src/models/Payment'
5-
64
import { response } from '@stacksjs/router'
75

6+
import Payment from '../../orm/src/models/Payment'
7+
88
export default new Action({
99
name: 'Payment Show',
1010
description: 'Payment Show ORM Action',

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,84 @@ export class BaseOrm<T, C> {
115115
return this.applyWhereRaw(sqlStatement)
116116
}
117117

118+
applyOrWhere(...conditions: [string, any][]): this {
119+
this.selectFromQuery = this.selectFromQuery.where((eb: any) => {
120+
return eb.or(
121+
conditions.map(([column, value]) => eb(column, '=', value)),
122+
)
123+
})
124+
125+
this.updateFromQuery = this.updateFromQuery.where((eb: any) => {
126+
return eb.or(
127+
conditions.map(([column, value]) => eb(column, '=', value)),
128+
)
129+
})
130+
131+
this.deleteFromQuery = this.deleteFromQuery.where((eb: any) => {
132+
return eb.or(
133+
conditions.map(([column, value]) => eb(column, '=', value)),
134+
)
135+
})
136+
137+
return this
138+
}
139+
140+
orWhere(...conditions: [string, any][]): this {
141+
return this.applyOrWhere(...conditions)
142+
}
143+
144+
applyWhen(condition: boolean, callback: (query: this) => T): this {
145+
if (condition)
146+
callback(this)
147+
148+
return this
149+
}
150+
151+
when(condition: boolean, callback: (query: this) => T,
152+
): this {
153+
return this.applyWhen(condition, callback)
154+
}
155+
156+
applyWhereNotNull(column: keyof C): this {
157+
this.selectFromQuery = this.selectFromQuery.where((eb: any) =>
158+
eb(column, '=', '').or(column, 'is not', null),
159+
)
160+
161+
this.updateFromQuery = this.updateFromQuery.where((eb: any) =>
162+
eb(column, '=', '').or(column, 'is not', null),
163+
)
164+
165+
this.deleteFromQuery = this.deleteFromQuery.where((eb: any) =>
166+
eb(column, '=', '').or(column, 'is not', null),
167+
)
168+
169+
return this
170+
}
171+
172+
whereNotNull(column: keyof C): this {
173+
return this.applyWhereNotNull(column)
174+
}
175+
176+
applyWhereNull(column: keyof C): this {
177+
this.selectFromQuery = this.selectFromQuery.where((eb: any) =>
178+
eb(column, '=', '').or(column, 'is', null),
179+
)
180+
181+
this.updateFromQuery = this.updateFromQuery.where((eb: any) =>
182+
eb(column, '=', '').or(column, 'is', null),
183+
)
184+
185+
this.deleteFromQuery = this.deleteFromQuery.where((eb: any) =>
186+
eb(column, '=', '').or(column, 'is', null),
187+
)
188+
189+
return this
190+
}
191+
192+
whereNull(column: keyof C): this {
193+
return this.applyWhereNull(column)
194+
}
195+
118196
// Methods to be implemented by child classes
119197
protected mapCustomGetters(_model: T): void {}
120198

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

Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,123 +1768,28 @@ export async function generateModelString(
17681768
return instance
17691769
}
17701770
1771-
applyOrWhere(...conditions: [string, any][]): ${modelName}Model {
1772-
this.selectFromQuery = this.selectFromQuery.where((eb: any) => {
1773-
return eb.or(
1774-
conditions.map(([column, value]) => eb(column, '=', value))
1775-
)
1776-
})
1777-
1778-
this.updateFromQuery = this.updateFromQuery.where((eb: any) => {
1779-
return eb.or(
1780-
conditions.map(([column, value]) => eb(column, '=', value))
1781-
)
1782-
})
1783-
1784-
this.deleteFromQuery = this.deleteFromQuery.where((eb: any) => {
1785-
return eb.or(
1786-
conditions.map(([column, value]) => eb(column, '=', value))
1787-
)
1788-
})
1789-
1790-
return this
1791-
}
1792-
1793-
orWhere(...conditions: [string, any][]): ${modelName}Model {
1794-
return this.applyOrWhere(...conditions)
1795-
}
1796-
17971771
static orWhere(...conditions: [string, any][]): ${modelName}Model {
17981772
const instance = new ${modelName}Model(undefined)
17991773
18001774
return instance.applyOrWhere(...conditions)
18011775
}
18021776
1803-
when(
1804-
condition: boolean,
1805-
callback: (query: ${modelName}Model) => ${modelName}Model,
1806-
): ${modelName}Model {
1807-
return ${modelName}Model.when(condition, callback)
1808-
}
1809-
1810-
static when(
1811-
condition: boolean,
1812-
callback: (query: ${modelName}Model) => ${modelName}Model,
1813-
): ${modelName}Model {
1777+
static when(condition: boolean, callback: (query: ${modelName}Model) => ${modelName}Model): ${modelName}Model {
18141778
let instance = new ${modelName}Model(undefined)
1815-
1816-
if (condition)
1817-
instance = callback(instance)
1818-
1819-
return instance
1820-
}
18211779
1822-
whereNotNull(column: keyof ${formattedTableName}Table): ${modelName}Model {
1823-
this.selectFromQuery = this.selectFromQuery.where((eb: any) =>
1824-
eb(column, '=', '').or(column, 'is not', null)
1825-
)
1826-
1827-
this.updateFromQuery = this.updateFromQuery.where((eb: any) =>
1828-
eb(column, '=', '').or(column, 'is not', null)
1829-
)
1830-
1831-
this.deleteFromQuery = this.deleteFromQuery.where((eb: any) =>
1832-
eb(column, '=', '').or(column, 'is not', null)
1833-
)
1834-
1835-
return this
1780+
return instance.applyWhen(condition, callback)
18361781
}
1837-
1782+
18381783
static whereNotNull(column: keyof ${formattedTableName}Table): ${modelName}Model {
1839-
const instance = new ${modelName}Model(undefined)
1840-
1841-
instance.selectFromQuery = instance.selectFromQuery.where((eb: any) =>
1842-
eb(column, '=', '').or(column, 'is not', null)
1843-
)
1844-
1845-
instance.updateFromQuery = instance.updateFromQuery.where((eb: any) =>
1846-
eb(column, '=', '').or(column, 'is not', null)
1847-
)
1848-
1849-
instance.deleteFromQuery = instance.deleteFromQuery.where((eb: any) =>
1850-
eb(column, '=', '').or(column, 'is not', null)
1851-
)
1784+
const instance = new UserModel(undefined)
18521785
1853-
return instance
1786+
return instance.applyWhereNotNull(column)
18541787
}
18551788
1856-
whereNull(column: keyof ${formattedTableName}Table): ${modelName}Model {
1857-
this.selectFromQuery = this.selectFromQuery.where((eb: any) =>
1858-
eb(column, '=', '').or(column, 'is', null)
1859-
)
1860-
1861-
this.updateFromQuery = this.updateFromQuery.where((eb: any) =>
1862-
eb(column, '=', '').or(column, 'is', null)
1863-
)
1864-
1865-
this.deleteFromQuery = this.deleteFromQuery.where((eb: any) =>
1866-
eb(column, '=', '').or(column, 'is', null)
1867-
)
1868-
1869-
return this
1870-
}
1871-
18721789
static whereNull(column: keyof ${formattedTableName}Table): ${modelName}Model {
18731790
const instance = new ${modelName}Model(undefined)
1874-
1875-
instance.selectFromQuery = instance.selectFromQuery.where((eb: any) =>
1876-
eb(column, '=', '').or(column, 'is', null)
1877-
)
1878-
1879-
instance.updateFromQuery = instance.updateFromQuery.where((eb: any) =>
1880-
eb(column, '=', '').or(column, 'is', null)
1881-
)
1882-
1883-
instance.deleteFromQuery = instance.deleteFromQuery.where((eb: any) =>
1884-
eb(column, '=', '').or(column, 'is', null)
1885-
)
18861791
1887-
return instance
1792+
return instance.applyWhereNull(column)
18881793
}
18891794
18901795
${whereStatements}

storage/framework/orm/src/models/AccessToken.ts

Lines changed: 6 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -976,123 +976,28 @@ export class AccessTokenModel extends BaseOrm<AccessTokenModel, PersonalAccessTo
976976
return instance
977977
}
978978

979-
applyOrWhere(...conditions: [string, any][]): AccessTokenModel {
980-
this.selectFromQuery = this.selectFromQuery.where((eb: any) => {
981-
return eb.or(
982-
conditions.map(([column, value]) => eb(column, '=', value)),
983-
)
984-
})
985-
986-
this.updateFromQuery = this.updateFromQuery.where((eb: any) => {
987-
return eb.or(
988-
conditions.map(([column, value]) => eb(column, '=', value)),
989-
)
990-
})
991-
992-
this.deleteFromQuery = this.deleteFromQuery.where((eb: any) => {
993-
return eb.or(
994-
conditions.map(([column, value]) => eb(column, '=', value)),
995-
)
996-
})
997-
998-
return this
999-
}
1000-
1001-
orWhere(...conditions: [string, any][]): AccessTokenModel {
1002-
return this.applyOrWhere(...conditions)
1003-
}
1004-
1005979
static orWhere(...conditions: [string, any][]): AccessTokenModel {
1006980
const instance = new AccessTokenModel(undefined)
1007981

1008982
return instance.applyOrWhere(...conditions)
1009983
}
1010984

1011-
when(
1012-
condition: boolean,
1013-
callback: (query: AccessTokenModel) => AccessTokenModel,
1014-
): AccessTokenModel {
1015-
return AccessTokenModel.when(condition, callback)
1016-
}
1017-
1018-
static when(
1019-
condition: boolean,
1020-
callback: (query: AccessTokenModel) => AccessTokenModel,
1021-
): AccessTokenModel {
1022-
let instance = new AccessTokenModel(undefined)
1023-
1024-
if (condition)
1025-
instance = callback(instance)
1026-
1027-
return instance
1028-
}
1029-
1030-
whereNotNull(column: keyof PersonalAccessTokensTable): AccessTokenModel {
1031-
this.selectFromQuery = this.selectFromQuery.where((eb: any) =>
1032-
eb(column, '=', '').or(column, 'is not', null),
1033-
)
1034-
1035-
this.updateFromQuery = this.updateFromQuery.where((eb: any) =>
1036-
eb(column, '=', '').or(column, 'is not', null),
1037-
)
1038-
1039-
this.deleteFromQuery = this.deleteFromQuery.where((eb: any) =>
1040-
eb(column, '=', '').or(column, 'is not', null),
1041-
)
1042-
1043-
return this
1044-
}
1045-
1046-
static whereNotNull(column: keyof PersonalAccessTokensTable): AccessTokenModel {
985+
static when(condition: boolean, callback: (query: AccessTokenModel) => AccessTokenModel): AccessTokenModel {
1047986
const instance = new AccessTokenModel(undefined)
1048987

1049-
instance.selectFromQuery = instance.selectFromQuery.where((eb: any) =>
1050-
eb(column, '=', '').or(column, 'is not', null),
1051-
)
1052-
1053-
instance.updateFromQuery = instance.updateFromQuery.where((eb: any) =>
1054-
eb(column, '=', '').or(column, 'is not', null),
1055-
)
1056-
1057-
instance.deleteFromQuery = instance.deleteFromQuery.where((eb: any) =>
1058-
eb(column, '=', '').or(column, 'is not', null),
1059-
)
1060-
1061-
return instance
988+
return instance.applyWhen(condition, callback)
1062989
}
1063990

1064-
whereNull(column: keyof PersonalAccessTokensTable): AccessTokenModel {
1065-
this.selectFromQuery = this.selectFromQuery.where((eb: any) =>
1066-
eb(column, '=', '').or(column, 'is', null),
1067-
)
1068-
1069-
this.updateFromQuery = this.updateFromQuery.where((eb: any) =>
1070-
eb(column, '=', '').or(column, 'is', null),
1071-
)
1072-
1073-
this.deleteFromQuery = this.deleteFromQuery.where((eb: any) =>
1074-
eb(column, '=', '').or(column, 'is', null),
1075-
)
991+
static whereNotNull(column: keyof PersonalAccessTokensTable): AccessTokenModel {
992+
const instance = new UserModel(undefined)
1076993

1077-
return this
994+
return instance.applyWhereNotNull(column)
1078995
}
1079996

1080997
static whereNull(column: keyof PersonalAccessTokensTable): AccessTokenModel {
1081998
const instance = new AccessTokenModel(undefined)
1082999

1083-
instance.selectFromQuery = instance.selectFromQuery.where((eb: any) =>
1084-
eb(column, '=', '').or(column, 'is', null),
1085-
)
1086-
1087-
instance.updateFromQuery = instance.updateFromQuery.where((eb: any) =>
1088-
eb(column, '=', '').or(column, 'is', null),
1089-
)
1090-
1091-
instance.deleteFromQuery = instance.deleteFromQuery.where((eb: any) =>
1092-
eb(column, '=', '').or(column, 'is', null),
1093-
)
1094-
1095-
return instance
1000+
return instance.applyWhereNull(column)
10961001
}
10971002

10981003
static whereName(value: string): AccessTokenModel {

0 commit comments

Comments
 (0)