Skip to content

Commit d8aa407

Browse files
chore: wip
1 parent 3a2ee63 commit d8aa407

35 files changed

+1871
-43
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,22 @@ export class BaseOrm<T, C, J> {
552552
return this.applyJoin(table, firstCol, secondCol)
553553
}
554554

555+
async applyUpdate<U>(values: U): Promise<T | undefined> {
556+
if (!values || Object.keys(values).length === 0)
557+
return undefined
558+
559+
await this.updateFromQuery
560+
.set(values as any)
561+
.executeTakeFirst()
562+
563+
// If we have an ID in the current instance, try to find the updated model
564+
if ((this as any).id) {
565+
return await this.applyFind((this as any).id)
566+
}
567+
568+
return undefined
569+
}
570+
555571
// Methods to be implemented by child classes
556572
protected mapCustomGetters(_model: T): void {}
557573

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

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,50 @@ export async function generateModelString(
16731673
return await instance.applyCreate(new${modelName})
16741674
}
16751675
1676+
async update(new${modelName}: ${modelName}Update): Promise<${modelName}Model | undefined> {
1677+
const filteredValues = Object.fromEntries(
1678+
Object.entries(new${modelName}).filter(([key]) =>
1679+
!this.guarded.includes(key) && this.fillable.includes(key)
1680+
),
1681+
) as ${modelName}Update
1682+
1683+
await this.mapCustomSetters(filteredValues)
1684+
1685+
await DB.instance.updateTable('${tableName}')
1686+
.set(filteredValues)
1687+
.where('id', '=', this.id)
1688+
.executeTakeFirst()
1689+
1690+
if (this.id) {
1691+
const model = await this.find(this.id)
1692+
1693+
${mittUpdateStatement}
1694+
1695+
return model
1696+
}
1697+
1698+
this.hasSaved = true
1699+
1700+
return undefined
1701+
}
1702+
1703+
async forceUpdate(new${modelName}: ${modelName}Update): Promise<${modelName}Model | undefined> {
1704+
await DB.instance.updateTable('${tableName}')
1705+
.set(new${modelName})
1706+
.where('id', '=', this.id)
1707+
.executeTakeFirst()
1708+
1709+
if (this.id) {
1710+
const model = await this.find(this.id)
1711+
1712+
${mittUpdateStatement}
1713+
1714+
return model
1715+
}
1716+
1717+
return undefined
1718+
}
1719+
16761720
static async createMany(new${modelName}: New${modelName}[]): Promise<void> {
16771721
const instance = new ${modelName}Model(undefined)
16781722
@@ -1709,13 +1753,27 @@ export async function generateModelString(
17091753
async delete(): Promise<${formattedTableName}Table> {
17101754
if (this.id === undefined)
17111755
this.deleteFromQuery.execute()
1712-
${mittDeleteFindStatement}
1713-
${mittDeleteStatement}
1714-
${thisSoftDeleteStatementsUpdateFrom}
1756+
${mittDeleteFindStatement}
1757+
${instanceSoftDeleteStatements}
1758+
${mittDeleteStatement}
1759+
1760+
await DB.instance.deleteFrom('${tableName}')
1761+
.where('id', '=', this.id)
1762+
.execute()
1763+
}
1764+
1765+
static async remove(id: number): Promise<any> {
1766+
${removeInstanceStatment}
17151767
1716-
return await DB.instance.deleteFrom('${tableName}')
1717-
.where('id', '=', this.id)
1718-
.execute()
1768+
${mittDeleteStaticFindStatement}
1769+
1770+
${instanceSoftDeleteStatementsUpdateFrom}
1771+
1772+
${mittDeleteStatement}
1773+
1774+
return await DB.instance.deleteFrom('${tableName}')
1775+
.where('id', '=', id)
1776+
.execute()
17191777
}
17201778
17211779
${whereStatements}

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,46 @@ export class AccessTokenModel extends BaseOrm<AccessTokenModel, PersonalAccessTo
896896
return await instance.applyCreate(newAccessToken)
897897
}
898898

899+
async update(newAccessToken: AccessTokenUpdate): Promise<AccessTokenModel | undefined> {
900+
const filteredValues = Object.fromEntries(
901+
Object.entries(newAccessToken).filter(([key]) =>
902+
!this.guarded.includes(key) && this.fillable.includes(key),
903+
),
904+
) as AccessTokenUpdate
905+
906+
await this.mapCustomSetters(filteredValues)
907+
908+
await DB.instance.updateTable('personal_access_tokens')
909+
.set(filteredValues)
910+
.where('id', '=', this.id)
911+
.executeTakeFirst()
912+
913+
if (this.id) {
914+
const model = await this.find(this.id)
915+
916+
return model
917+
}
918+
919+
this.hasSaved = true
920+
921+
return undefined
922+
}
923+
924+
async forceUpdate(newAccessToken: AccessTokenUpdate): Promise<AccessTokenModel | undefined> {
925+
await DB.instance.updateTable('personal_access_tokens')
926+
.set(newAccessToken)
927+
.where('id', '=', this.id)
928+
.executeTakeFirst()
929+
930+
if (this.id) {
931+
const model = await this.find(this.id)
932+
933+
return model
934+
}
935+
936+
return undefined
937+
}
938+
899939
static async createMany(newAccessToken: NewAccessToken[]): Promise<void> {
900940
const instance = new AccessTokenModel(undefined)
901941

@@ -929,11 +969,17 @@ export class AccessTokenModel extends BaseOrm<AccessTokenModel, PersonalAccessTo
929969
if (this.id === undefined)
930970
this.deleteFromQuery.execute()
931971

932-
return await DB.instance.deleteFrom('personal_access_tokens')
972+
await DB.instance.deleteFrom('personal_access_tokens')
933973
.where('id', '=', this.id)
934974
.execute()
935975
}
936976

977+
static async remove(id: number): Promise<any> {
978+
return await DB.instance.deleteFrom('personal_access_tokens')
979+
.where('id', '=', id)
980+
.execute()
981+
}
982+
937983
static whereName(value: string): AccessTokenModel {
938984
const instance = new AccessTokenModel(undefined)
939985

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,52 @@ export class CouponModel extends BaseOrm<CouponModel, CouponsTable, CouponJsonRe
941941
return await instance.applyCreate(newCoupon)
942942
}
943943

944+
async update(newCoupon: CouponUpdate): Promise<CouponModel | undefined> {
945+
const filteredValues = Object.fromEntries(
946+
Object.entries(newCoupon).filter(([key]) =>
947+
!this.guarded.includes(key) && this.fillable.includes(key),
948+
),
949+
) as CouponUpdate
950+
951+
await this.mapCustomSetters(filteredValues)
952+
953+
await DB.instance.updateTable('coupons')
954+
.set(filteredValues)
955+
.where('id', '=', this.id)
956+
.executeTakeFirst()
957+
958+
if (this.id) {
959+
const model = await this.find(this.id)
960+
961+
if (model)
962+
dispatch('coupon:updated', model)
963+
964+
return model
965+
}
966+
967+
this.hasSaved = true
968+
969+
return undefined
970+
}
971+
972+
async forceUpdate(newCoupon: CouponUpdate): Promise<CouponModel | undefined> {
973+
await DB.instance.updateTable('coupons')
974+
.set(newCoupon)
975+
.where('id', '=', this.id)
976+
.executeTakeFirst()
977+
978+
if (this.id) {
979+
const model = await this.find(this.id)
980+
981+
if (model)
982+
dispatch('coupon:updated', model)
983+
984+
return model
985+
}
986+
987+
return undefined
988+
}
989+
944990
static async createMany(newCoupon: NewCoupon[]): Promise<void> {
945991
const instance = new CouponModel(undefined)
946992

@@ -979,14 +1025,28 @@ export class CouponModel extends BaseOrm<CouponModel, CouponsTable, CouponJsonRe
9791025
if (this.id === undefined)
9801026
this.deleteFromQuery.execute()
9811027
const model = await this.find(Number(this.id))
1028+
9821029
if (model)
9831030
dispatch('coupon:deleted', model)
9841031

985-
return await DB.instance.deleteFrom('coupons')
1032+
await DB.instance.deleteFrom('coupons')
9861033
.where('id', '=', this.id)
9871034
.execute()
9881035
}
9891036

1037+
static async remove(id: number): Promise<any> {
1038+
const instance = new CouponModel(undefined)
1039+
1040+
const model = await instance.find(Number(id))
1041+
1042+
if (model)
1043+
dispatch('coupon:deleted', model)
1044+
1045+
return await DB.instance.deleteFrom('coupons')
1046+
.where('id', '=', id)
1047+
.execute()
1048+
}
1049+
9901050
static whereCode(value: string): CouponModel {
9911051
const instance = new CouponModel(undefined)
9921052

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,52 @@ export class CustomerModel extends BaseOrm<CustomerModel, CustomersTable, Custom
886886
return await instance.applyCreate(newCustomer)
887887
}
888888

889+
async update(newCustomer: CustomerUpdate): Promise<CustomerModel | undefined> {
890+
const filteredValues = Object.fromEntries(
891+
Object.entries(newCustomer).filter(([key]) =>
892+
!this.guarded.includes(key) && this.fillable.includes(key),
893+
),
894+
) as CustomerUpdate
895+
896+
await this.mapCustomSetters(filteredValues)
897+
898+
await DB.instance.updateTable('customers')
899+
.set(filteredValues)
900+
.where('id', '=', this.id)
901+
.executeTakeFirst()
902+
903+
if (this.id) {
904+
const model = await this.find(this.id)
905+
906+
if (model)
907+
dispatch('customer:updated', model)
908+
909+
return model
910+
}
911+
912+
this.hasSaved = true
913+
914+
return undefined
915+
}
916+
917+
async forceUpdate(newCustomer: CustomerUpdate): Promise<CustomerModel | undefined> {
918+
await DB.instance.updateTable('customers')
919+
.set(newCustomer)
920+
.where('id', '=', this.id)
921+
.executeTakeFirst()
922+
923+
if (this.id) {
924+
const model = await this.find(this.id)
925+
926+
if (model)
927+
dispatch('customer:updated', model)
928+
929+
return model
930+
}
931+
932+
return undefined
933+
}
934+
889935
static async createMany(newCustomer: NewCustomer[]): Promise<void> {
890936
const instance = new CustomerModel(undefined)
891937

@@ -924,14 +970,28 @@ export class CustomerModel extends BaseOrm<CustomerModel, CustomersTable, Custom
924970
if (this.id === undefined)
925971
this.deleteFromQuery.execute()
926972
const model = await this.find(Number(this.id))
973+
927974
if (model)
928975
dispatch('customer:deleted', model)
929976

930-
return await DB.instance.deleteFrom('customers')
977+
await DB.instance.deleteFrom('customers')
931978
.where('id', '=', this.id)
932979
.execute()
933980
}
934981

982+
static async remove(id: number): Promise<any> {
983+
const instance = new CustomerModel(undefined)
984+
985+
const model = await instance.find(Number(id))
986+
987+
if (model)
988+
dispatch('customer:deleted', model)
989+
990+
return await DB.instance.deleteFrom('customers')
991+
.where('id', '=', id)
992+
.execute()
993+
}
994+
935995
static whereName(value: string): CustomerModel {
936996
const instance = new CustomerModel(undefined)
937997

0 commit comments

Comments
 (0)