Skip to content

Commit ec8c6fc

Browse files
chore: wip
1 parent 7177fc2 commit ec8c6fc

File tree

11 files changed

+863
-233
lines changed

11 files changed

+863
-233
lines changed

storage/framework/core/actions/src/orm/generate-model.ts

Lines changed: 92 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ async function generateModelString(
818818
if (observer) {
819819
mittCreateStatement += `dispatch('${formattedModelName}.created', model)`
820820
mittUpdateStatement += `dispatch('${formattedModelName}.updated', model)`
821-
mittDeleteStatement += `dispatch('${formattedModelName}.deleted', model)`
821+
mittDeleteStatement += `dispatch('${formattedModelName}.deleted', this)`
822822
}
823823
}
824824

@@ -1169,24 +1169,30 @@ async function generateModelString(
11691169
}
11701170
11711171
static async all(): Promise<${modelName}Model[]> {
1172-
let query = db.selectFrom('${tableName}').selectAll()
1172+
let query = db.selectFrom('${tableName}').selectAll();
11731173
1174-
const instance = new this(null)
1174+
// Check if soft deletes are enabled
1175+
if (this.softDeletes) {
1176+
query = query.where('deleted_at', 'is', null);
1177+
}
11751178
1176-
const results = await query.execute()
1179+
const instance = new this(null);
1180+
const results = await query.execute();
11771181
1178-
return results.map(modelItem => instance.parseResult(new ${modelName}Model(modelItem)))
1182+
return results.map(modelItem => instance.parseResult(new ${modelName}Model(modelItem)));
11791183
}
11801184
1181-
static async findOrFail(id: number, fields?: (keyof ${modelName}Type)[]): Promise<${modelName}Model> {
1185+
1186+
static async findOrFail(id: number): Promise<${modelName}Model> {
11821187
let query = db.selectFrom('${tableName}').where('id', '=', id)
11831188
11841189
const instance = new this(null)
11851190
1186-
if (fields)
1187-
query = query.select(fields)
1188-
else
1189-
query = query.selectAll()
1191+
if (instance.softDeletes) {
1192+
query = query.where('deleted_at', 'is', null);
1193+
}
1194+
1195+
query = query.selectAll()
11901196
11911197
const model = await query.executeTakeFirst()
11921198
@@ -1197,15 +1203,16 @@ async function generateModelString(
11971203
return instance.parseResult(new this(model))
11981204
}
11991205
1200-
static async findMany(ids: number[], fields?: (keyof ${modelName}Type)[]): Promise<${modelName}Model[]> {
1206+
static async findMany(ids: number[]): Promise<${modelName}Model[]> {
12011207
let query = db.selectFrom('${tableName}').where('id', 'in', ids)
12021208
12031209
const instance = new this(null)
12041210
1205-
if (fields)
1206-
query = query.select(fields)
1207-
else
1208-
query = query.selectAll()
1211+
if (instance.softDeletes) {
1212+
query = query.where('deleted_at', 'is', null);
1213+
}
1214+
1215+
query = query.selectAll()
12091216
12101217
const model = await query.execute()
12111218
@@ -1216,6 +1223,8 @@ async function generateModelString(
12161223
static async fetch(criteria: Partial<${modelName}Type>, options: QueryOptions = {}): Promise<${modelName}Model[]> {
12171224
let query = db.selectFrom('${tableName}')
12181225
1226+
const instance = new this(null)
1227+
12191228
// Apply sorting from options
12201229
if (options.sort)
12211230
query = query.orderBy(options.sort.column, options.sort.order)
@@ -1227,26 +1236,46 @@ async function generateModelString(
12271236
if (options.offset !== undefined)
12281237
query = query.offset(options.offset)
12291238
1239+
if (instance.softDeletes) {
1240+
query = query.where('deleted_at', 'is', null);
1241+
}
1242+
12301243
const model = await query.selectAll().execute()
12311244
return model.map(modelItem => new ${modelName}Model(modelItem))
12321245
}
12331246
12341247
// Method to get a ${modelName} by criteria
12351248
static async get(): Promise<${modelName}Model[]> {
1236-
const query = db.selectFrom('${tableName}')
1249+
let query = db.selectFrom('${tableName}');
12371250
1238-
const model = await query.selectAll().execute()
1251+
const instance = new this(null)
12391252
1240-
return model.map(modelItem => new ${modelName}Model(modelItem))
1253+
// Check if soft deletes are enabled
1254+
if (instance.softDeletes) {
1255+
query = query.where('deleted_at', 'is', null);
1256+
}
1257+
1258+
const model = await query.selectAll().execute();
1259+
1260+
return model.map(modelItem => new ${modelName}Model(modelItem));
12411261
}
12421262
12431263
// Method to get a ${modelName} by criteria
12441264
async get(): Promise<${modelName}Model[]> {
12451265
if (this.hasSelect) {
1266+
1267+
if (this.softDeletes) {
1268+
this.query = this.query.where('deleted_at', 'is', null);
1269+
}
1270+
12461271
const model = await this.query.execute()
12471272
12481273
return model.map((modelItem: ${modelName}Model) => new ${modelName}Model(modelItem))
12491274
}
1275+
1276+
if (this.softDeletes) {
1277+
this.query = this.query.where('deleted_at', 'is', null);
1278+
}
12501279
12511280
const model = await this.query.selectAll().execute()
12521281
@@ -1256,13 +1285,22 @@ async function generateModelString(
12561285
static async count(): Promise<number> {
12571286
const instance = new this(null)
12581287
1288+
if (instance.softDeletes) {
1289+
instance.query = instance.query.where('deleted_at', 'is', null);
1290+
}
1291+
12591292
const results = await instance.query.selectAll().execute()
12601293
12611294
return results.length
12621295
}
12631296
12641297
async count(): Promise<number> {
12651298
if (this.hasSelect) {
1299+
1300+
if (this.softDeletes) {
1301+
this.query = this.query.where('deleted_at', 'is', null);
1302+
}
1303+
12661304
const results = await this.query.execute()
12671305
12681306
return results.length
@@ -1331,14 +1369,22 @@ async function generateModelString(
13311369
13321370
// Method to remove a ${modelName}
13331371
static async remove(id: number): Promise<void> {
1334-
13351372
const instance = new this(null)
13361373
13371374
const model = await instance.find(id)
13381375
1339-
await db.deleteFrom('${tableName}')
1376+
if (instance.softDeletes) {
1377+
await db.updateTable('${tableName}')
1378+
.set({
1379+
deleted_at: sql.raw('CURRENT_TIMESTAMP')
1380+
})
13401381
.where('id', '=', id)
1341-
.execute()
1382+
.execute();
1383+
} else {
1384+
await db.deleteFrom('${tableName}')
1385+
.where('id', '=', id)
1386+
.execute();
1387+
}
13421388
13431389
${mittDeleteStatement}
13441390
}
@@ -1465,14 +1511,14 @@ async function generateModelString(
14651511
}
14661512
14671513
// Method to update the ${tableName} instance
1468-
async update(${formattedModelName}: ${modelName}Update): Promise<${modelName}Model | null> {
1514+
async update(${formattedModelName}: ${modelName}Update): Promise<${modelName}Model | undefined> {
14691515
if (this.id === undefined)
14701516
throw new Error('${modelName} ID is undefined')
14711517
1472-
const filteredValues = Object.keys(new${modelName})
1518+
const filteredValues = Object.keys(${formattedModelName})
14731519
.filter(key => this.fillable.includes(key))
14741520
.reduce((obj, key) => {
1475-
obj[key] = new${modelName}[key];
1521+
obj[key] = ${formattedModelName}[key]
14761522
return obj;
14771523
}, {});
14781524
@@ -1505,18 +1551,28 @@ async function generateModelString(
15051551
}
15061552
}
15071553
1508-
// Method to delete the ${formattedModelName} instance
1554+
// Method to delete (soft delete) the ${formattedModelName} instance
15091555
async delete(): Promise<void> {
1510-
if (this.id === undefined)
1511-
throw new Error('${modelName} ID is undefined')
1512-
1513-
await db.deleteFrom('${tableName}')
1514-
.where('id', '=', this.id)
1515-
.execute()
1516-
1517-
const model = this
1556+
if (this.id === undefined)
1557+
throw new Error('${modelName} ID is undefined');
1558+
1559+
// Check if soft deletes are enabled
1560+
if (this.softDeletes) {
1561+
// Update the deleted_at column with the current timestamp
1562+
await db.updateTable('${tableName}')
1563+
.set({
1564+
deleted_at: sql.raw('CURRENT_TIMESTAMP')
1565+
})
1566+
.where('id', '=', this.id)
1567+
.execute();
1568+
} else {
1569+
// Perform a hard delete
1570+
await db.deleteFrom('${tableName}')
1571+
.where('id', '=', this.id)
1572+
.execute();
1573+
}
15181574
1519-
${mittDeleteStatement}
1575+
${mittDeleteStatement}
15201576
}
15211577
15221578
${relationMethods}
@@ -1571,6 +1627,7 @@ async function generateModelString(
15711627
delete model['fillable']
15721628
delete model['two_factor_secret']
15731629
delete model['hasSelect']
1630+
delete model['softDeletes']
15741631
15751632
for (const hiddenAttribute of this.hidden) {
15761633
delete model[hiddenAttribute]
@@ -1582,7 +1639,7 @@ async function generateModelString(
15821639
${twoFactorStatements}
15831640
}
15841641
1585-
async function find(id: number, fields?: (keyof ${modelName}Type)[]): Promise<${modelName}Model | null> {
1642+
async function find(id: number): Promise<${modelName}Model | null> {
15861643
let query = db.selectFrom('${tableName}').where('id', '=', id)
15871644
15881645
if (fields) query = query.select(fields)

storage/framework/core/components/notification/auto-imports.d.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/* eslint-disable */
2-
/* prettier-ignore */
3-
// @ts-nocheck
4-
// noinspection JSUnusedGlobalSymbols
5-
// Generated by unplugin-auto-import
6-
export {}
71
declare global {
82
const Action: typeof import('../../actions/src/action')['Action']
93
const BunCronJob: typeof import('../../scheduler/src/index')['BunCronJob']
@@ -961,11 +955,23 @@ declare global {
961955
// for type re-export
962956
declare global {
963957
// @ts-ignore
964-
export type { Component, ComponentPublicInstance, ComputedRef, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue'
958+
export type {
959+
Component,
960+
ComponentPublicInstance,
961+
ComputedRef,
962+
ExtractDefaultPropTypes,
963+
ExtractPropTypes,
964+
ExtractPublicPropTypes,
965+
InjectionKey,
966+
PropType,
967+
Ref,
968+
VNode,
969+
WritableComputedRef,
970+
} from 'vue'
965971
import('vue')
966972
}
967973
// for vue template auto import
968-
import { UnwrapRef } from 'vue'
974+
import type { UnwrapRef } from 'vue'
969975
declare module 'vue' {
970976
interface GlobalComponents {}
971977
interface ComponentCustomProperties {

0 commit comments

Comments
 (0)