Skip to content

Commit 8efe601

Browse files
chore: wip
1 parent 7f3031c commit 8efe601

File tree

17 files changed

+1514
-900
lines changed

17 files changed

+1514
-900
lines changed

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

Lines changed: 90 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,15 @@ export async function generateModelString(
758758
this.hasSelect = false
759759
}
760760
761-
static select(params: (keyof ${modelName}Type)[] | RawBuilder<string>): ${modelName}Model {
761+
select(params: (keyof ${modelName}Type)[] | RawBuilder<string> | string): ${modelName}Model {
762+
this.selectFromQuery = this.selectFromQuery.select(params)
763+
764+
this.hasSelect = true
765+
766+
return this
767+
}
768+
769+
static select(params: (keyof ${modelName}Type)[] | RawBuilder<string> | string): ${modelName}Model {
762770
const instance = new ${modelName}Model(null)
763771
764772
// Initialize a query with the table name and selected fields
@@ -1191,6 +1199,45 @@ export async function generateModelString(
11911199
11921200
return this
11931201
}
1202+
1203+
static where(...args: (string | number | boolean | undefined | null)[]): ${modelName}Model {
1204+
let column: any
1205+
let operator: any
1206+
let value: any
1207+
1208+
const instance = new ${modelName}Model(null)
1209+
1210+
if (args.length === 2) {
1211+
[column, value] = args
1212+
operator = '='
1213+
} else if (args.length === 3) {
1214+
[column, operator, value] = args
1215+
} else {
1216+
throw new HttpError(500, "Invalid number of arguments")
1217+
}
1218+
1219+
instance.selectFromQuery = instance.selectFromQuery.where(column, operator, value)
1220+
1221+
instance.updateFromQuery = instance.updateFromQuery.where(column, operator, value)
1222+
1223+
instance.deleteFromQuery = instance.deleteFromQuery.where(column, operator, value)
1224+
1225+
return instance
1226+
}
1227+
1228+
whereRef(column: string, operator: string, value: string): ${modelName}Model {
1229+
this.selectFromQuery = this.selectFromQuery.whereRef(column, operator, value)
1230+
1231+
return this
1232+
}
1233+
1234+
static whereRef(column: string, operator: string, value: string): ${modelName}Model {
1235+
const instance = new ${modelName}Model(null)
1236+
1237+
instance.selectFromQuery = instance.selectFromQuery.whereRef(column, operator, value)
1238+
1239+
return instance
1240+
}
11941241
11951242
orWhere(...args: Array<[string, string, any]>): ${modelName}Model {
11961243
if (args.length === 0) {
@@ -1247,30 +1294,15 @@ export async function generateModelString(
12471294
12481295
return instance
12491296
}
1297+
1298+
when(
1299+
condition: boolean,
1300+
callback: (query: ${modelName}Model) => ${modelName}Model,
1301+
): ${modelName}Model {
1302+
if (condition)
1303+
callback(this.selectFromQuery)
12501304
1251-
static where(...args: (string | number | boolean | undefined | null)[]): ${modelName}Model {
1252-
let column: any
1253-
let operator: any
1254-
let value: any
1255-
1256-
const instance = new ${modelName}Model(null)
1257-
1258-
if (args.length === 2) {
1259-
[column, value] = args
1260-
operator = '='
1261-
} else if (args.length === 3) {
1262-
[column, operator, value] = args
1263-
} else {
1264-
throw new HttpError(500, "Invalid number of arguments")
1265-
}
1266-
1267-
instance.selectFromQuery = instance.selectFromQuery.where(column, operator, value)
1268-
1269-
instance.updateFromQuery = instance.updateFromQuery.where(column, operator, value)
1270-
1271-
instance.deleteFromQuery = instance.deleteFromQuery.where(column, operator, value)
1272-
1273-
return instance
1305+
return this
12741306
}
12751307
12761308
static when(
@@ -1284,13 +1316,15 @@ export async function generateModelString(
12841316
12851317
return instance
12861318
}
1319+
1320+
whereNull(column: string): ${modelName}Model {
1321+
this.selectFromQuery = this.selectFromQuery.where((eb: any) =>
1322+
eb(column, '=', '').or(column, 'is', null)
1323+
)
12871324
1288-
when(
1289-
condition: boolean,
1290-
callback: (query: ${modelName}Model) => ${modelName}Model,
1291-
): ${modelName}Model {
1292-
if (condition)
1293-
callback(this.selectFromQuery)
1325+
this.updateFromQuery = this.updateFromQuery.where((eb: any) =>
1326+
eb(column, '=', '').or(column, 'is', null)
1327+
)
12941328
12951329
return this
12961330
}
@@ -1309,19 +1343,8 @@ export async function generateModelString(
13091343
return instance
13101344
}
13111345
1312-
whereNull(column: string): ${modelName}Model {
1313-
this.selectFromQuery = this.selectFromQuery.where((eb: any) =>
1314-
eb(column, '=', '').or(column, 'is', null)
1315-
)
1316-
1317-
this.updateFromQuery = this.updateFromQuery.where((eb: any) =>
1318-
eb(column, '=', '').or(column, 'is', null)
1319-
)
1320-
1321-
return this
1322-
}
13231346
1324-
${whereStatements}
1347+
${whereStatements}
13251348
13261349
whereIn(column: keyof ${modelName}Type, values: any[]): ${modelName}Model {
13271350
this.selectFromQuery = this.selectFromQuery.where(column, 'in', values)
@@ -1344,6 +1367,20 @@ export async function generateModelString(
13441367
13451368
return instance
13461369
}
1370+
1371+
whereBetween(column: keyof ${modelName}Type, range: [any, any]): ${modelName}Model {
1372+
if (range.length !== 2) {
1373+
throw new Error('Range must have exactly two values: [min, max]')
1374+
}
1375+
1376+
const query = sql\` \${sql.raw(column as string)} between \${range[0]} and \${range[1]} \`
1377+
1378+
this.selectFromQuery = this.selectFromQuery.where(query)
1379+
this.updateFromQuery = this.updateFromQuery.where(query)
1380+
this.deleteFromQuery = this.deleteFromQuery.where(query)
1381+
1382+
return this
1383+
}
13471384
13481385
static whereBetween(column: keyof ${modelName}Type, range: [any, any]): ${modelName}Model {
13491386
if (range.length !== 2) {
@@ -1360,6 +1397,17 @@ export async function generateModelString(
13601397
13611398
return instance
13621399
}
1400+
1401+
1402+
whereNotIn(column: keyof ${modelName}Type, values: any[]): ${modelName}Model {
1403+
this.selectFromQuery = this.selectFromQuery.where(column, 'not in', values)
1404+
1405+
this.updateFromQuery = this.updateFromQuery.where(column, 'not in', values)
1406+
1407+
this.deleteFromQuery = this.deleteFromQuery.where(column, 'not in', values)
1408+
1409+
return this
1410+
}
13631411
13641412
static whereNotIn(column: keyof ${modelName}Type, values: any[]): ${modelName}Model {
13651413
const instance = new ${modelName}Model(null)
@@ -1373,16 +1421,6 @@ export async function generateModelString(
13731421
return instance
13741422
}
13751423
1376-
whereNotIn(column: keyof ${modelName}Type, values: any[]): ${modelName}Model {
1377-
this.selectFromQuery = this.selectFromQuery.where(column, 'not in', values)
1378-
1379-
this.updateFromQuery = this.updateFromQuery.where(column, 'not in', values)
1380-
1381-
this.deleteFromQuery = this.deleteFromQuery.where(column, 'not in', values)
1382-
1383-
return this
1384-
}
1385-
13861424
async first(): Promise<${modelName}Model | undefined> {
13871425
const model = await this.selectFromQuery.selectAll().executeTakeFirst()
13881426

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

Lines changed: 89 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,15 @@ export class AccessTokenModel {
9898
this.hasSelect = false
9999
}
100100

101-
static select(params: (keyof AccessTokenType)[] | RawBuilder<string>): AccessTokenModel {
101+
select(params: (keyof AccessTokenType)[] | RawBuilder<string> | string): AccessTokenModel {
102+
this.selectFromQuery = this.selectFromQuery.select(params)
103+
104+
this.hasSelect = true
105+
106+
return this
107+
}
108+
109+
static select(params: (keyof AccessTokenType)[] | RawBuilder<string> | string): AccessTokenModel {
102110
const instance = new AccessTokenModel(null)
103111

104112
// Initialize a query with the table name and selected fields
@@ -502,6 +510,47 @@ export class AccessTokenModel {
502510
return this
503511
}
504512

513+
static where(...args: (string | number | boolean | undefined | null)[]): AccessTokenModel {
514+
let column: any
515+
let operator: any
516+
let value: any
517+
518+
const instance = new AccessTokenModel(null)
519+
520+
if (args.length === 2) {
521+
[column, value] = args
522+
operator = '='
523+
}
524+
else if (args.length === 3) {
525+
[column, operator, value] = args
526+
}
527+
else {
528+
throw new HttpError(500, 'Invalid number of arguments')
529+
}
530+
531+
instance.selectFromQuery = instance.selectFromQuery.where(column, operator, value)
532+
533+
instance.updateFromQuery = instance.updateFromQuery.where(column, operator, value)
534+
535+
instance.deleteFromQuery = instance.deleteFromQuery.where(column, operator, value)
536+
537+
return instance
538+
}
539+
540+
whereRef(column: string, operator: string, value: string): AccessTokenModel {
541+
this.selectFromQuery = this.selectFromQuery.whereRef(column, operator, value)
542+
543+
return this
544+
}
545+
546+
static whereRef(column: string, operator: string, value: string): AccessTokenModel {
547+
const instance = new AccessTokenModel(null)
548+
549+
instance.selectFromQuery = instance.selectFromQuery.whereRef(column, operator, value)
550+
551+
return instance
552+
}
553+
505554
orWhere(...args: Array<[string, string, any]>): AccessTokenModel {
506555
if (args.length === 0) {
507556
throw new HttpError(500, 'At least one condition must be provided')
@@ -558,31 +607,14 @@ export class AccessTokenModel {
558607
return instance
559608
}
560609

561-
static where(...args: (string | number | boolean | undefined | null)[]): AccessTokenModel {
562-
let column: any
563-
let operator: any
564-
let value: any
565-
566-
const instance = new AccessTokenModel(null)
567-
568-
if (args.length === 2) {
569-
[column, value] = args
570-
operator = '='
571-
}
572-
else if (args.length === 3) {
573-
[column, operator, value] = args
574-
}
575-
else {
576-
throw new HttpError(500, 'Invalid number of arguments')
577-
}
578-
579-
instance.selectFromQuery = instance.selectFromQuery.where(column, operator, value)
580-
581-
instance.updateFromQuery = instance.updateFromQuery.where(column, operator, value)
582-
583-
instance.deleteFromQuery = instance.deleteFromQuery.where(column, operator, value)
610+
when(
611+
condition: boolean,
612+
callback: (query: AccessTokenModel) => AccessTokenModel,
613+
): AccessTokenModel {
614+
if (condition)
615+
callback(this.selectFromQuery)
584616

585-
return instance
617+
return this
586618
}
587619

588620
static when(
@@ -597,12 +629,14 @@ export class AccessTokenModel {
597629
return instance
598630
}
599631

600-
when(
601-
condition: boolean,
602-
callback: (query: AccessTokenModel) => AccessTokenModel,
603-
): AccessTokenModel {
604-
if (condition)
605-
callback(this.selectFromQuery)
632+
whereNull(column: string): AccessTokenModel {
633+
this.selectFromQuery = this.selectFromQuery.where((eb: any) =>
634+
eb(column, '=', '').or(column, 'is', null),
635+
)
636+
637+
this.updateFromQuery = this.updateFromQuery.where((eb: any) =>
638+
eb(column, '=', '').or(column, 'is', null),
639+
)
606640

607641
return this
608642
}
@@ -621,18 +655,6 @@ export class AccessTokenModel {
621655
return instance
622656
}
623657

624-
whereNull(column: string): AccessTokenModel {
625-
this.selectFromQuery = this.selectFromQuery.where((eb: any) =>
626-
eb(column, '=', '').or(column, 'is', null),
627-
)
628-
629-
this.updateFromQuery = this.updateFromQuery.where((eb: any) =>
630-
eb(column, '=', '').or(column, 'is', null),
631-
)
632-
633-
return this
634-
}
635-
636658
static whereName(value: string): AccessTokenModel {
637659
const instance = new AccessTokenModel(null)
638660

@@ -687,6 +709,20 @@ export class AccessTokenModel {
687709
return instance
688710
}
689711

712+
whereBetween(column: keyof AccessTokenType, range: [any, any]): AccessTokenModel {
713+
if (range.length !== 2) {
714+
throw new Error('Range must have exactly two values: [min, max]')
715+
}
716+
717+
const query = sql` ${sql.raw(column as string)} between ${range[0]} and ${range[1]} `
718+
719+
this.selectFromQuery = this.selectFromQuery.where(query)
720+
this.updateFromQuery = this.updateFromQuery.where(query)
721+
this.deleteFromQuery = this.deleteFromQuery.where(query)
722+
723+
return this
724+
}
725+
690726
static whereBetween(column: keyof AccessTokenType, range: [any, any]): AccessTokenModel {
691727
if (range.length !== 2) {
692728
throw new Error('Range must have exactly two values: [min, max]')
@@ -703,6 +739,16 @@ export class AccessTokenModel {
703739
return instance
704740
}
705741

742+
whereNotIn(column: keyof AccessTokenType, values: any[]): AccessTokenModel {
743+
this.selectFromQuery = this.selectFromQuery.where(column, 'not in', values)
744+
745+
this.updateFromQuery = this.updateFromQuery.where(column, 'not in', values)
746+
747+
this.deleteFromQuery = this.deleteFromQuery.where(column, 'not in', values)
748+
749+
return this
750+
}
751+
706752
static whereNotIn(column: keyof AccessTokenType, values: any[]): AccessTokenModel {
707753
const instance = new AccessTokenModel(null)
708754

@@ -715,16 +761,6 @@ export class AccessTokenModel {
715761
return instance
716762
}
717763

718-
whereNotIn(column: keyof AccessTokenType, values: any[]): AccessTokenModel {
719-
this.selectFromQuery = this.selectFromQuery.where(column, 'not in', values)
720-
721-
this.updateFromQuery = this.updateFromQuery.where(column, 'not in', values)
722-
723-
this.deleteFromQuery = this.deleteFromQuery.where(column, 'not in', values)
724-
725-
return this
726-
}
727-
728764
async first(): Promise<AccessTokenModel | undefined> {
729765
const model = await this.selectFromQuery.selectAll().executeTakeFirst()
730766

0 commit comments

Comments
 (0)