Skip to content

Commit 14f526e

Browse files
chore: wip
1 parent 9b04cee commit 14f526e

35 files changed

+164
-307
lines changed

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

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,45 @@ export class BaseOrm<T, C, J> {
3636
return this.applySelect(params)
3737
}
3838

39+
async first(): Promise<T | undefined> {
40+
const model = await this.applyFirst()
41+
42+
if (!model)
43+
return undefined
44+
45+
return model as T
46+
}
47+
48+
async firstOrFail(): Promise<T> {
49+
const model = await this.applyFirstOrFail()
50+
51+
if (!model)
52+
throw new HttpError(404, `No ${this.tableName} results found for query`)
53+
54+
return model as T
55+
}
56+
57+
protected async applyFirstOrFail(): Promise<T | undefined> {
58+
let model
59+
60+
if (this.hasSelect) {
61+
model = await this.selectFromQuery.executeTakeFirst()
62+
}
63+
else {
64+
model = await this.selectFromQuery.selectAll().executeTakeFirst()
65+
}
66+
67+
if (!model)
68+
throw new HttpError(404, `No ${this.tableName} results found for query`)
69+
70+
if (model) {
71+
this.mapCustomGetters(model)
72+
await this.loadRelations(model)
73+
}
74+
75+
return model
76+
}
77+
3978
// The protected helper method that does the actual work
4079
protected async applyFind(id: number): Promise<T | undefined> {
4180
const model = await DB.instance.selectFrom(this.tableName)
@@ -77,6 +116,17 @@ export class BaseOrm<T, C, J> {
77116
return await this.applyFindMany(ids)
78117
}
79118

119+
async all(): Promise<T[]> {
120+
const models = await DB.instance.selectFrom(this.tableName)
121+
.selectAll()
122+
.execute()
123+
124+
this.mapCustomGetters(models)
125+
await this.loadRelations(models)
126+
127+
return models as T[]
128+
}
129+
80130
async applyFirst(): Promise<T | undefined> {
81131
let model
82132

@@ -120,6 +170,32 @@ export class BaseOrm<T, C, J> {
120170
return await this.applyFind(id)
121171
}
122172

173+
async findOrFail(id: number): Promise<T> {
174+
const model = await this.applyFindOrFail(id)
175+
176+
if (!model)
177+
throw new HttpError(404, `No ${this.tableName} results found for id ${id}`)
178+
179+
return model as T
180+
}
181+
182+
protected async applyFindOrFail(id: number): Promise<T | undefined> {
183+
const model = await DB.instance.selectFrom(this.tableName)
184+
.where('id', '=', id)
185+
.selectAll()
186+
.executeTakeFirst()
187+
188+
if (!model)
189+
throw new HttpError(404, `No ${this.tableName} results found for id ${id}`)
190+
191+
this.mapCustomGetters(model)
192+
await this.loadRelations(model)
193+
194+
cache.getOrSet(`${this.tableName}:${id}`, JSON.stringify(model))
195+
196+
return model
197+
}
198+
123199
applyWhereColumn(first: keyof C, operator: Operator, second: keyof C): this {
124200
this.selectFromQuery = this.selectFromQuery.whereRef(first, operator, second)
125201

@@ -329,14 +405,67 @@ export class BaseOrm<T, C, J> {
329405
return model
330406
}
331407

408+
async last(): Promise<T | undefined> {
409+
const model = await this.applyLast()
410+
411+
if (!model)
412+
return undefined
413+
414+
return model as T
415+
}
416+
417+
async applyGet(): Promise<T[]> {
418+
let models
419+
420+
if (this.hasSelect) {
421+
models = await this.selectFromQuery.execute()
422+
}
423+
else {
424+
models = await this.selectFromQuery.selectAll().execute()
425+
}
426+
427+
this.mapCustomGetters(models)
428+
await this.loadRelations(models)
429+
430+
return models as T[]
431+
}
432+
433+
async get(): Promise<T[]> {
434+
return await this.applyGet()
435+
}
436+
437+
skip(count: number): this {
438+
this.selectFromQuery = this.selectFromQuery.offset(count)
439+
440+
return this
441+
}
442+
443+
applyTake(count: number): this {
444+
this.selectFromQuery = this.selectFromQuery.limit(count)
445+
446+
return this
447+
}
448+
449+
take(count: number): this {
450+
return this.applyTake(count)
451+
}
452+
453+
async count(): Promise<number> {
454+
const result = await this.selectFromQuery
455+
.select(sql`COUNT(*) as count`)
456+
.executeTakeFirst()
457+
458+
return result.count || 0
459+
}
460+
332461
applyOrderBy(column: keyof C, order: 'asc' | 'desc'): this {
333462
this.selectFromQuery = this.selectFromQuery.orderBy(column, order)
334463

335464
return this
336465
}
337466

338467
orderBy(column: keyof C, order: 'asc' | 'desc'): this {
339-
return this.orderBy(column, order)
468+
return this.applyOrderBy(column, order)
340469
}
341470

342471
applyGroupBy(column: keyof C): this {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,18 +1231,10 @@ export async function generateModelString(
12311231
await instance.applyChunk(size, callback)
12321232
}
12331233
1234-
take(count: number): ${modelName}Model {
1235-
this.selectFromQuery = this.selectFromQuery.limit(count)
1236-
1237-
return this
1238-
}
1239-
12401234
static take(count: number): ${modelName}Model {
12411235
const instance = new ${modelName}Model(undefined)
12421236
1243-
instance.selectFromQuery = instance.selectFromQuery.limit(count)
1244-
1245-
return instance
1237+
return instance.applyTake(count)
12461238
}
12471239
12481240
static async pluck<K extends keyof ${modelName}Model>(field: K): Promise<${modelName}Model[K][]> {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -454,18 +454,10 @@ export class AccessTokenModel extends BaseOrm<AccessTokenModel, PersonalAccessTo
454454
await instance.applyChunk(size, callback)
455455
}
456456

457-
take(count: number): AccessTokenModel {
458-
this.selectFromQuery = this.selectFromQuery.limit(count)
459-
460-
return this
461-
}
462-
463457
static take(count: number): AccessTokenModel {
464458
const instance = new AccessTokenModel(undefined)
465459

466-
instance.selectFromQuery = instance.selectFromQuery.limit(count)
467-
468-
return instance
460+
return instance.applyTake(count)
469461
}
470462

471463
static async pluck<K extends keyof AccessTokenModel>(field: K): Promise<AccessTokenModel[K][]> {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,18 +494,10 @@ export class CouponModel extends BaseOrm<CouponModel, CouponsTable, CouponJsonRe
494494
await instance.applyChunk(size, callback)
495495
}
496496

497-
take(count: number): CouponModel {
498-
this.selectFromQuery = this.selectFromQuery.limit(count)
499-
500-
return this
501-
}
502-
503497
static take(count: number): CouponModel {
504498
const instance = new CouponModel(undefined)
505499

506-
instance.selectFromQuery = instance.selectFromQuery.limit(count)
507-
508-
return instance
500+
return instance.applyTake(count)
509501
}
510502

511503
static async pluck<K extends keyof CouponModel>(field: K): Promise<CouponModel[K][]> {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,18 +439,10 @@ export class CustomerModel extends BaseOrm<CustomerModel, CustomersTable, Custom
439439
await instance.applyChunk(size, callback)
440440
}
441441

442-
take(count: number): CustomerModel {
443-
this.selectFromQuery = this.selectFromQuery.limit(count)
444-
445-
return this
446-
}
447-
448442
static take(count: number): CustomerModel {
449443
const instance = new CustomerModel(undefined)
450444

451-
instance.selectFromQuery = instance.selectFromQuery.limit(count)
452-
453-
return instance
445+
return instance.applyTake(count)
454446
}
455447

456448
static async pluck<K extends keyof CustomerModel>(field: K): Promise<CustomerModel[K][]> {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,18 +424,10 @@ export class DeploymentModel extends BaseOrm<DeploymentModel, DeploymentsTable,
424424
await instance.applyChunk(size, callback)
425425
}
426426

427-
take(count: number): DeploymentModel {
428-
this.selectFromQuery = this.selectFromQuery.limit(count)
429-
430-
return this
431-
}
432-
433427
static take(count: number): DeploymentModel {
434428
const instance = new DeploymentModel(undefined)
435429

436-
instance.selectFromQuery = instance.selectFromQuery.limit(count)
437-
438-
return instance
430+
return instance.applyTake(count)
439431
}
440432

441433
static async pluck<K extends keyof DeploymentModel>(field: K): Promise<DeploymentModel[K][]> {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,10 @@ export class ErrorModel extends BaseOrm<ErrorModel, ErrorsTable, ErrorJsonRespon
384384
await instance.applyChunk(size, callback)
385385
}
386386

387-
take(count: number): ErrorModel {
388-
this.selectFromQuery = this.selectFromQuery.limit(count)
389-
390-
return this
391-
}
392-
393387
static take(count: number): ErrorModel {
394388
const instance = new ErrorModel(undefined)
395389

396-
instance.selectFromQuery = instance.selectFromQuery.limit(count)
397-
398-
return instance
390+
return instance.applyTake(count)
399391
}
400392

401393
static async pluck<K extends keyof ErrorModel>(field: K): Promise<ErrorModel[K][]> {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,10 @@ export class FailedJobModel extends BaseOrm<FailedJobModel, FailedJobsTable, Fai
384384
await instance.applyChunk(size, callback)
385385
}
386386

387-
take(count: number): FailedJobModel {
388-
this.selectFromQuery = this.selectFromQuery.limit(count)
389-
390-
return this
391-
}
392-
393387
static take(count: number): FailedJobModel {
394388
const instance = new FailedJobModel(undefined)
395389

396-
instance.selectFromQuery = instance.selectFromQuery.limit(count)
397-
398-
return instance
390+
return instance.applyTake(count)
399391
}
400392

401393
static async pluck<K extends keyof FailedJobModel>(field: K): Promise<FailedJobModel[K][]> {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,18 +503,10 @@ export class GiftCardModel extends BaseOrm<GiftCardModel, GiftCardsTable, GiftCa
503503
await instance.applyChunk(size, callback)
504504
}
505505

506-
take(count: number): GiftCardModel {
507-
this.selectFromQuery = this.selectFromQuery.limit(count)
508-
509-
return this
510-
}
511-
512506
static take(count: number): GiftCardModel {
513507
const instance = new GiftCardModel(undefined)
514508

515-
instance.selectFromQuery = instance.selectFromQuery.limit(count)
516-
517-
return instance
509+
return instance.applyTake(count)
518510
}
519511

520512
static async pluck<K extends keyof GiftCardModel>(field: K): Promise<GiftCardModel[K][]> {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,10 @@ export class JobModel extends BaseOrm<JobModel, JobsTable, JobJsonResponse> {
384384
await instance.applyChunk(size, callback)
385385
}
386386

387-
take(count: number): JobModel {
388-
this.selectFromQuery = this.selectFromQuery.limit(count)
389-
390-
return this
391-
}
392-
393387
static take(count: number): JobModel {
394388
const instance = new JobModel(undefined)
395389

396-
instance.selectFromQuery = instance.selectFromQuery.limit(count)
397-
398-
return instance
390+
return instance.applyTake(count)
399391
}
400392

401393
static async pluck<K extends keyof JobModel>(field: K): Promise<JobModel[K][]> {

0 commit comments

Comments
 (0)