@@ -818,7 +818,7 @@ async function generateModelString(
818
818
if ( observer ) {
819
819
mittCreateStatement += `dispatch('${ formattedModelName } .created', model)`
820
820
mittUpdateStatement += `dispatch('${ formattedModelName } .updated', model)`
821
- mittDeleteStatement += `dispatch('${ formattedModelName } .deleted', model )`
821
+ mittDeleteStatement += `dispatch('${ formattedModelName } .deleted', this )`
822
822
}
823
823
}
824
824
@@ -1169,24 +1169,30 @@ async function generateModelString(
1169
1169
}
1170
1170
1171
1171
static async all(): Promise<${ modelName } Model[]> {
1172
- let query = db.selectFrom('${ tableName } ').selectAll()
1172
+ let query = db.selectFrom('${ tableName } ').selectAll();
1173
1173
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
+ }
1175
1178
1176
- const results = await query.execute()
1179
+ const instance = new this(null);
1180
+ const results = await query.execute();
1177
1181
1178
- return results.map(modelItem => instance.parseResult(new ${ modelName } Model(modelItem)))
1182
+ return results.map(modelItem => instance.parseResult(new ${ modelName } Model(modelItem)));
1179
1183
}
1180
1184
1181
- static async findOrFail(id: number, fields?: (keyof ${ modelName } Type)[]): Promise<${ modelName } Model> {
1185
+
1186
+ static async findOrFail(id: number): Promise<${ modelName } Model> {
1182
1187
let query = db.selectFrom('${ tableName } ').where('id', '=', id)
1183
1188
1184
1189
const instance = new this(null)
1185
1190
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()
1190
1196
1191
1197
const model = await query.executeTakeFirst()
1192
1198
@@ -1197,15 +1203,16 @@ async function generateModelString(
1197
1203
return instance.parseResult(new this(model))
1198
1204
}
1199
1205
1200
- static async findMany(ids: number[], fields?: (keyof ${ modelName } Type)[] ): Promise<${ modelName } Model[]> {
1206
+ static async findMany(ids: number[]): Promise<${ modelName } Model[]> {
1201
1207
let query = db.selectFrom('${ tableName } ').where('id', 'in', ids)
1202
1208
1203
1209
const instance = new this(null)
1204
1210
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()
1209
1216
1210
1217
const model = await query.execute()
1211
1218
@@ -1216,6 +1223,8 @@ async function generateModelString(
1216
1223
static async fetch(criteria: Partial<${ modelName } Type>, options: QueryOptions = {}): Promise<${ modelName } Model[]> {
1217
1224
let query = db.selectFrom('${ tableName } ')
1218
1225
1226
+ const instance = new this(null)
1227
+
1219
1228
// Apply sorting from options
1220
1229
if (options.sort)
1221
1230
query = query.orderBy(options.sort.column, options.sort.order)
@@ -1227,26 +1236,46 @@ async function generateModelString(
1227
1236
if (options.offset !== undefined)
1228
1237
query = query.offset(options.offset)
1229
1238
1239
+ if (instance.softDeletes) {
1240
+ query = query.where('deleted_at', 'is', null);
1241
+ }
1242
+
1230
1243
const model = await query.selectAll().execute()
1231
1244
return model.map(modelItem => new ${ modelName } Model(modelItem))
1232
1245
}
1233
1246
1234
1247
// Method to get a ${ modelName } by criteria
1235
1248
static async get(): Promise<${ modelName } Model[]> {
1236
- const query = db.selectFrom('${ tableName } ')
1249
+ let query = db.selectFrom('${ tableName } ');
1237
1250
1238
- const model = await query.selectAll().execute( )
1251
+ const instance = new this(null )
1239
1252
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));
1241
1261
}
1242
1262
1243
1263
// Method to get a ${ modelName } by criteria
1244
1264
async get(): Promise<${ modelName } Model[]> {
1245
1265
if (this.hasSelect) {
1266
+
1267
+ if (this.softDeletes) {
1268
+ this.query = this.query.where('deleted_at', 'is', null);
1269
+ }
1270
+
1246
1271
const model = await this.query.execute()
1247
1272
1248
1273
return model.map((modelItem: ${ modelName } Model) => new ${ modelName } Model(modelItem))
1249
1274
}
1275
+
1276
+ if (this.softDeletes) {
1277
+ this.query = this.query.where('deleted_at', 'is', null);
1278
+ }
1250
1279
1251
1280
const model = await this.query.selectAll().execute()
1252
1281
@@ -1256,13 +1285,22 @@ async function generateModelString(
1256
1285
static async count(): Promise<number> {
1257
1286
const instance = new this(null)
1258
1287
1288
+ if (instance.softDeletes) {
1289
+ instance.query = instance.query.where('deleted_at', 'is', null);
1290
+ }
1291
+
1259
1292
const results = await instance.query.selectAll().execute()
1260
1293
1261
1294
return results.length
1262
1295
}
1263
1296
1264
1297
async count(): Promise<number> {
1265
1298
if (this.hasSelect) {
1299
+
1300
+ if (this.softDeletes) {
1301
+ this.query = this.query.where('deleted_at', 'is', null);
1302
+ }
1303
+
1266
1304
const results = await this.query.execute()
1267
1305
1268
1306
return results.length
@@ -1331,14 +1369,22 @@ async function generateModelString(
1331
1369
1332
1370
// Method to remove a ${ modelName }
1333
1371
static async remove(id: number): Promise<void> {
1334
-
1335
1372
const instance = new this(null)
1336
1373
1337
1374
const model = await instance.find(id)
1338
1375
1339
- await db.deleteFrom('${ tableName } ')
1376
+ if (instance.softDeletes) {
1377
+ await db.updateTable('${ tableName } ')
1378
+ .set({
1379
+ deleted_at: sql.raw('CURRENT_TIMESTAMP')
1380
+ })
1340
1381
.where('id', '=', id)
1341
- .execute()
1382
+ .execute();
1383
+ } else {
1384
+ await db.deleteFrom('${ tableName } ')
1385
+ .where('id', '=', id)
1386
+ .execute();
1387
+ }
1342
1388
1343
1389
${ mittDeleteStatement }
1344
1390
}
@@ -1465,14 +1511,14 @@ async function generateModelString(
1465
1511
}
1466
1512
1467
1513
// 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 > {
1469
1515
if (this.id === undefined)
1470
1516
throw new Error('${ modelName } ID is undefined')
1471
1517
1472
- const filteredValues = Object.keys(new ${ modelName } )
1518
+ const filteredValues = Object.keys(${ formattedModelName } )
1473
1519
.filter(key => this.fillable.includes(key))
1474
1520
.reduce((obj, key) => {
1475
- obj[key] = new ${ modelName } [key];
1521
+ obj[key] = ${ formattedModelName } [key]
1476
1522
return obj;
1477
1523
}, {});
1478
1524
@@ -1505,18 +1551,28 @@ async function generateModelString(
1505
1551
}
1506
1552
}
1507
1553
1508
- // Method to delete the ${ formattedModelName } instance
1554
+ // Method to delete (soft delete) the ${ formattedModelName } instance
1509
1555
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
+ }
1518
1574
1519
- ${ mittDeleteStatement }
1575
+ ${ mittDeleteStatement }
1520
1576
}
1521
1577
1522
1578
${ relationMethods }
@@ -1571,6 +1627,7 @@ async function generateModelString(
1571
1627
delete model['fillable']
1572
1628
delete model['two_factor_secret']
1573
1629
delete model['hasSelect']
1630
+ delete model['softDeletes']
1574
1631
1575
1632
for (const hiddenAttribute of this.hidden) {
1576
1633
delete model[hiddenAttribute]
@@ -1582,7 +1639,7 @@ async function generateModelString(
1582
1639
${ twoFactorStatements }
1583
1640
}
1584
1641
1585
- async function find(id: number, fields?: (keyof ${ modelName } Type)[] ): Promise<${ modelName } Model | null> {
1642
+ async function find(id: number): Promise<${ modelName } Model | null> {
1586
1643
let query = db.selectFrom('${ tableName } ').where('id', '=', id)
1587
1644
1588
1645
if (fields) query = query.select(fields)
0 commit comments