Skip to content

Commit a7af365

Browse files
chore: wip
1 parent 1303609 commit a7af365

19 files changed

+188
-88
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ export async function generateModelString(
409409
410410
async paymentIntent(options: Stripe.PaymentIntentCreateParams): Promise<Stripe.Response<Stripe.PaymentIntent>> {
411411
if (!this.hasStripeId()) {
412-
throw new Error('Customer does not exist in Stripe')
412+
throw new HttpError(404, 'Customer does not exist in Stripe')
413413
}
414414
415415
const defaultOptions: Stripe.PaymentIntentCreateParams = {
@@ -1359,6 +1359,7 @@ export async function generateModelString(
13591359
condition: boolean,
13601360
callback: (query: ${modelName}Model) => ${modelName}Model,
13611361
): ${modelName}Model {
1362+
13621363
if (condition)
13631364
callback(this.selectFromQuery)
13641365
@@ -1430,7 +1431,7 @@ export async function generateModelString(
14301431
14311432
whereBetween(column: keyof ${modelName}Type, range: [any, any]): ${modelName}Model {
14321433
if (range.length !== 2) {
1433-
throw new Error('Range must have exactly two values: [min, max]')
1434+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
14341435
}
14351436
14361437
const query = sql\` \${sql.raw(column as string)} between \${range[0]} and \${range[1]} \`
@@ -1444,7 +1445,7 @@ export async function generateModelString(
14441445
14451446
static whereBetween(column: keyof ${modelName}Type, range: [any, any]): ${modelName}Model {
14461447
if (range.length !== 2) {
1447-
throw new Error('Range must have exactly two values: [min, max]')
1448+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
14481449
}
14491450
14501451
const instance = new ${modelName}Model(null)
@@ -1572,7 +1573,7 @@ export async function generateModelString(
15721573
const key = Object.keys(condition)[0] as keyof ${modelName}Type
15731574
15741575
if (!key) {
1575-
throw new Error('Condition must contain at least one key-value pair')
1576+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
15761577
}
15771578
15781579
const value = condition[key]
@@ -1601,7 +1602,7 @@ export async function generateModelString(
16011602
const key = Object.keys(condition)[0] as keyof ${modelName}Type
16021603
16031604
if (!key) {
1604-
throw new Error('Condition must contain at least one key-value pair')
1605+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
16051606
}
16061607
16071608
const value = condition[key]
@@ -1626,7 +1627,7 @@ export async function generateModelString(
16261627
.executeTakeFirst()
16271628
16281629
if (!updated${modelName}) {
1629-
throw new Error('Failed to fetch updated record')
1630+
throw new HttpError(500, 'Failed to fetch updated record')
16301631
}
16311632
16321633
const instance = new ${modelName}Model(null)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414

1515
export * from '../../../orm/src/types'
1616
export * from './requests'
17-
17+
export * from './subquery'
1818
export * from './utils'
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { HttpError } from '@stacksjs/error-handling'
2+
3+
interface WhereCondition {
4+
type: 'and' | 'or'
5+
method: 'where' | 'whereIn' | 'whereNull' | 'whereNotNull' | 'whereBetween' | 'whereExists'
6+
column: string
7+
operator?: string
8+
value?: any
9+
values?: any[]
10+
callback?: (query: SubqueryBuilder) => void
11+
}
12+
13+
class SubqueryBuilder {
14+
private conditions: WhereCondition[] = []
15+
16+
where(...args: (string | number | boolean | undefined | null)[]) {
17+
this.addCondition('and', 'where', ...args)
18+
}
19+
20+
orWhere(...args: (string | number | boolean | undefined | null)[]) {
21+
this.addCondition('or', 'where', ...args)
22+
}
23+
24+
whereIn(column: string, values: any[]) {
25+
this.conditions.push({
26+
type: 'and',
27+
method: 'whereIn',
28+
column,
29+
values,
30+
})
31+
}
32+
33+
whereNotIn(column: string, values: any[]) {
34+
this.conditions.push({
35+
type: 'and',
36+
method: 'whereIn',
37+
column,
38+
values,
39+
operator: 'not',
40+
})
41+
}
42+
43+
whereNull(column: string) {
44+
this.conditions.push({
45+
type: 'and',
46+
method: 'whereNull',
47+
column,
48+
})
49+
}
50+
51+
whereNotNull(column: string) {
52+
this.conditions.push({
53+
type: 'and',
54+
method: 'whereNotNull',
55+
column,
56+
})
57+
}
58+
59+
whereBetween(column: string, range: [any, any]) {
60+
this.conditions.push({
61+
type: 'and',
62+
method: 'whereBetween',
63+
column,
64+
values: range,
65+
})
66+
}
67+
68+
whereExists(callback: (query: SubqueryBuilder) => void) {
69+
this.conditions.push({
70+
type: 'and',
71+
method: 'whereExists',
72+
column: '',
73+
callback,
74+
})
75+
}
76+
77+
private addCondition(type: 'and' | 'or', method: 'where', ...args: (string | number | boolean | undefined | null)[]) {
78+
let column: any
79+
let operator: any
80+
let value: any
81+
82+
if (args.length === 2) {
83+
[column, value] = args
84+
operator = '='
85+
}
86+
else if (args.length === 3) {
87+
[column, operator, value] = args
88+
}
89+
else {
90+
throw new HttpError(500, 'Invalid number of arguments')
91+
}
92+
93+
this.conditions.push({ type, method, column, operator, value })
94+
}
95+
96+
getConditions() {
97+
return this.conditions
98+
}
99+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ export class AccessTokenModel {
771771

772772
whereBetween(column: keyof AccessTokenType, range: [any, any]): AccessTokenModel {
773773
if (range.length !== 2) {
774-
throw new Error('Range must have exactly two values: [min, max]')
774+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
775775
}
776776

777777
const query = sql` ${sql.raw(column as string)} between ${range[0]} and ${range[1]} `
@@ -785,7 +785,7 @@ export class AccessTokenModel {
785785

786786
static whereBetween(column: keyof AccessTokenType, range: [any, any]): AccessTokenModel {
787787
if (range.length !== 2) {
788-
throw new Error('Range must have exactly two values: [min, max]')
788+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
789789
}
790790

791791
const instance = new AccessTokenModel(null)
@@ -912,7 +912,7 @@ export class AccessTokenModel {
912912
const key = Object.keys(condition)[0] as keyof AccessTokenType
913913

914914
if (!key) {
915-
throw new Error('Condition must contain at least one key-value pair')
915+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
916916
}
917917

918918
const value = condition[key]
@@ -941,7 +941,7 @@ export class AccessTokenModel {
941941
const key = Object.keys(condition)[0] as keyof AccessTokenType
942942

943943
if (!key) {
944-
throw new Error('Condition must contain at least one key-value pair')
944+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
945945
}
946946

947947
const value = condition[key]
@@ -966,7 +966,7 @@ export class AccessTokenModel {
966966
.executeTakeFirst()
967967

968968
if (!updatedAccessToken) {
969-
throw new Error('Failed to fetch updated record')
969+
throw new HttpError(500, 'Failed to fetch updated record')
970970
}
971971

972972
const instance = new AccessTokenModel(null)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ export class DeploymentModel {
814814

815815
whereBetween(column: keyof DeploymentType, range: [any, any]): DeploymentModel {
816816
if (range.length !== 2) {
817-
throw new Error('Range must have exactly two values: [min, max]')
817+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
818818
}
819819

820820
const query = sql` ${sql.raw(column as string)} between ${range[0]} and ${range[1]} `
@@ -828,7 +828,7 @@ export class DeploymentModel {
828828

829829
static whereBetween(column: keyof DeploymentType, range: [any, any]): DeploymentModel {
830830
if (range.length !== 2) {
831-
throw new Error('Range must have exactly two values: [min, max]')
831+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
832832
}
833833

834834
const instance = new DeploymentModel(null)
@@ -955,7 +955,7 @@ export class DeploymentModel {
955955
const key = Object.keys(condition)[0] as keyof DeploymentType
956956

957957
if (!key) {
958-
throw new Error('Condition must contain at least one key-value pair')
958+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
959959
}
960960

961961
const value = condition[key]
@@ -984,7 +984,7 @@ export class DeploymentModel {
984984
const key = Object.keys(condition)[0] as keyof DeploymentType
985985

986986
if (!key) {
987-
throw new Error('Condition must contain at least one key-value pair')
987+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
988988
}
989989

990990
const value = condition[key]
@@ -1009,7 +1009,7 @@ export class DeploymentModel {
10091009
.executeTakeFirst()
10101010

10111011
if (!updatedDeployment) {
1012-
throw new Error('Failed to fetch updated record')
1012+
throw new HttpError(500, 'Failed to fetch updated record')
10131013
}
10141014

10151015
const instance = new DeploymentModel(null)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ export class ErrorModel {
769769

770770
whereBetween(column: keyof ErrorType, range: [any, any]): ErrorModel {
771771
if (range.length !== 2) {
772-
throw new Error('Range must have exactly two values: [min, max]')
772+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
773773
}
774774

775775
const query = sql` ${sql.raw(column as string)} between ${range[0]} and ${range[1]} `
@@ -783,7 +783,7 @@ export class ErrorModel {
783783

784784
static whereBetween(column: keyof ErrorType, range: [any, any]): ErrorModel {
785785
if (range.length !== 2) {
786-
throw new Error('Range must have exactly two values: [min, max]')
786+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
787787
}
788788

789789
const instance = new ErrorModel(null)
@@ -910,7 +910,7 @@ export class ErrorModel {
910910
const key = Object.keys(condition)[0] as keyof ErrorType
911911

912912
if (!key) {
913-
throw new Error('Condition must contain at least one key-value pair')
913+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
914914
}
915915

916916
const value = condition[key]
@@ -939,7 +939,7 @@ export class ErrorModel {
939939
const key = Object.keys(condition)[0] as keyof ErrorType
940940

941941
if (!key) {
942-
throw new Error('Condition must contain at least one key-value pair')
942+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
943943
}
944944

945945
const value = condition[key]
@@ -964,7 +964,7 @@ export class ErrorModel {
964964
.executeTakeFirst()
965965

966966
if (!updatedError) {
967-
throw new Error('Failed to fetch updated record')
967+
throw new HttpError(500, 'Failed to fetch updated record')
968968
}
969969

970970
const instance = new ErrorModel(null)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ export class FailedJobModel {
769769

770770
whereBetween(column: keyof FailedJobType, range: [any, any]): FailedJobModel {
771771
if (range.length !== 2) {
772-
throw new Error('Range must have exactly two values: [min, max]')
772+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
773773
}
774774

775775
const query = sql` ${sql.raw(column as string)} between ${range[0]} and ${range[1]} `
@@ -783,7 +783,7 @@ export class FailedJobModel {
783783

784784
static whereBetween(column: keyof FailedJobType, range: [any, any]): FailedJobModel {
785785
if (range.length !== 2) {
786-
throw new Error('Range must have exactly two values: [min, max]')
786+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
787787
}
788788

789789
const instance = new FailedJobModel(null)
@@ -910,7 +910,7 @@ export class FailedJobModel {
910910
const key = Object.keys(condition)[0] as keyof FailedJobType
911911

912912
if (!key) {
913-
throw new Error('Condition must contain at least one key-value pair')
913+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
914914
}
915915

916916
const value = condition[key]
@@ -939,7 +939,7 @@ export class FailedJobModel {
939939
const key = Object.keys(condition)[0] as keyof FailedJobType
940940

941941
if (!key) {
942-
throw new Error('Condition must contain at least one key-value pair')
942+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
943943
}
944944

945945
const value = condition[key]
@@ -964,7 +964,7 @@ export class FailedJobModel {
964964
.executeTakeFirst()
965965

966966
if (!updatedFailedJob) {
967-
throw new Error('Failed to fetch updated record')
967+
throw new HttpError(500, 'Failed to fetch updated record')
968968
}
969969

970970
const instance = new FailedJobModel(null)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ export class JobModel {
769769

770770
whereBetween(column: keyof JobType, range: [any, any]): JobModel {
771771
if (range.length !== 2) {
772-
throw new Error('Range must have exactly two values: [min, max]')
772+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
773773
}
774774

775775
const query = sql` ${sql.raw(column as string)} between ${range[0]} and ${range[1]} `
@@ -783,7 +783,7 @@ export class JobModel {
783783

784784
static whereBetween(column: keyof JobType, range: [any, any]): JobModel {
785785
if (range.length !== 2) {
786-
throw new Error('Range must have exactly two values: [min, max]')
786+
throw new HttpError(500, 'Range must have exactly two values: [min, max]')
787787
}
788788

789789
const instance = new JobModel(null)
@@ -910,7 +910,7 @@ export class JobModel {
910910
const key = Object.keys(condition)[0] as keyof JobType
911911

912912
if (!key) {
913-
throw new Error('Condition must contain at least one key-value pair')
913+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
914914
}
915915

916916
const value = condition[key]
@@ -939,7 +939,7 @@ export class JobModel {
939939
const key = Object.keys(condition)[0] as keyof JobType
940940

941941
if (!key) {
942-
throw new Error('Condition must contain at least one key-value pair')
942+
throw new HttpError(500, 'Condition must contain at least one key-value pair')
943943
}
944944

945945
const value = condition[key]
@@ -964,7 +964,7 @@ export class JobModel {
964964
.executeTakeFirst()
965965

966966
if (!updatedJob) {
967-
throw new Error('Failed to fetch updated record')
967+
throw new HttpError(500, 'Failed to fetch updated record')
968968
}
969969

970970
const instance = new JobModel(null)

0 commit comments

Comments
 (0)