@@ -153,7 +153,7 @@ export async function generateModelString(
153
153
if (this.id === undefined)
154
154
throw new HttpError(500, 'Relation Error!')
155
155
156
- const firstModel = await db .selectFrom('${ throughTableRelation } ')
156
+ const firstModel = await Db.instance .selectFrom('${ throughTableRelation } ')
157
157
.where('${ foreignKeyRelation } ', '=', this.id)
158
158
.selectAll()
159
159
.executeTakeFirst()
@@ -188,7 +188,7 @@ export async function generateModelString(
188
188
if (this.id === undefined)
189
189
throw new HttpError(500, 'Relation Error!')
190
190
191
- const results = await db .selectFrom('${ tableRelation } ')
191
+ const results = await Db.instance .selectFrom('${ tableRelation } ')
192
192
.where('${ foreignKeyRelation } ', '=', this.id)
193
193
.limit(5)
194
194
.selectAll()
@@ -260,7 +260,7 @@ export async function generateModelString(
260
260
if (this.id === undefined)
261
261
throw new HttpError(500, 'Relation Error!')
262
262
263
- const results = await db .selectFrom('${ pivotTable } ')
263
+ const results = await Db.instance .selectFrom('${ pivotTable } ')
264
264
.where('${ pivotKey } ', '=', this.id)
265
265
.selectAll()
266
266
.execute()
@@ -437,7 +437,7 @@ export async function generateModelString(
437
437
}
438
438
439
439
async activeSubscription() {
440
- const subscription = await db .selectFrom('subscriptions')
440
+ const subscription = await Db.instance .selectFrom('subscriptions')
441
441
.where('user_id', '=', this.id)
442
442
.where('provider_status', '=', 'active')
443
443
.selectAll()
@@ -606,7 +606,7 @@ export async function generateModelString(
606
606
} \n\n`
607
607
608
608
whereFunctionStatements += `export async function where${ pascalCase ( attribute . field ) } (value: ${ entity } ): Promise<${ modelName } Model[]> {
609
- const query = db .selectFrom('${ tableName } ').where('${ snakeCase ( attribute . field ) } ', '=', value)
609
+ const query = Db.instance .selectFrom('${ tableName } ').where('${ snakeCase ( attribute . field ) } ', '=', value)
610
610
const results = await query.execute()
611
611
612
612
return results.map(modelItem => new ${ modelName } Model(modelItem))
@@ -777,7 +777,7 @@ export async function generateModelString(
777
777
778
778
// Method to find a ${ modelName } by ID
779
779
static async find(id: number): Promise<${ modelName } Model | undefined> {
780
- const model = await db .selectFrom('${ tableName } ').where('id', '=', id).selectAll().executeTakeFirst()
780
+ const model = await Db.instance .selectFrom('${ tableName } ').where('id', '=', id).selectAll().executeTakeFirst()
781
781
782
782
if (!model)
783
783
return undefined
@@ -798,7 +798,7 @@ export async function generateModelString(
798
798
}
799
799
800
800
static async first(): Promise<${ modelName } Model | undefined> {
801
- const model = await db .selectFrom('${ tableName } ')
801
+ const model = await Db.instance .selectFrom('${ tableName } ')
802
802
.selectAll()
803
803
.executeTakeFirst()
804
804
@@ -841,7 +841,7 @@ export async function generateModelString(
841
841
}
842
842
843
843
static async all(): Promise<${ modelName } Model[]> {
844
- const models = await db .selectFrom('${ tableName } ').selectAll().execute()
844
+ const models = await Db.instance .selectFrom('${ tableName } ').selectAll().execute()
845
845
846
846
const data = await Promise.all(models.map(async (model: ${ modelName } Type) => {
847
847
const instance = new ${ modelName } Model(model)
@@ -860,7 +860,7 @@ export async function generateModelString(
860
860
}
861
861
862
862
static async findOrFail(id: number): Promise<${ modelName } Model> {
863
- const model = await db .selectFrom('${ tableName } ').where('id', '=', id).selectAll().executeTakeFirst()
863
+ const model = await Db.instance .selectFrom('${ tableName } ').where('id', '=', id).selectAll().executeTakeFirst()
864
864
865
865
const instance = new ${ modelName } Model(null)
866
866
@@ -879,7 +879,7 @@ export async function generateModelString(
879
879
}
880
880
881
881
static async findMany(ids: number[]): Promise<${ modelName } Model[]> {
882
- let query = db .selectFrom('${ tableName } ').where('id', 'in', ids)
882
+ let query = Db.instance .selectFrom('${ tableName } ').where('id', 'in', ids)
883
883
884
884
const instance = new ${ modelName } Model(null)
885
885
@@ -1176,14 +1176,14 @@ export async function generateModelString(
1176
1176
1177
1177
// Method to get all ${ tableName }
1178
1178
static async paginate(options: QueryOptions = { limit: 10, offset: 0, page: 1 }): Promise<${ modelName } Response> {
1179
- const totalRecordsResult = await db .selectFrom('${ tableName } ')
1179
+ const totalRecordsResult = await Db.instance .selectFrom('${ tableName } ')
1180
1180
.select(db.fn.count('id').as('total')) // Use 'id' or another actual column name
1181
1181
.executeTakeFirst()
1182
1182
1183
1183
const totalRecords = Number(totalRecordsResult?.total) || 0
1184
1184
const totalPages = Math.ceil(totalRecords / (options.limit ?? 10))
1185
1185
1186
- const ${ tableName } WithExtra = await db .selectFrom('${ tableName } ')
1186
+ const ${ tableName } WithExtra = await Db.instance .selectFrom('${ tableName } ')
1187
1187
.selectAll()
1188
1188
.orderBy('id', 'asc') // Assuming 'id' is used for cursor-based pagination
1189
1189
.limit((options.limit ?? 10) + 1) // Fetch one extra record
@@ -1269,7 +1269,7 @@ export async function generateModelString(
1269
1269
1270
1270
${ mittDeleteStatement }
1271
1271
1272
- return await db .deleteFrom('${ tableName } ')
1272
+ return await Db.instance .deleteFrom('${ tableName } ')
1273
1273
.where('id', '=', id)
1274
1274
.execute()
1275
1275
}
@@ -1436,7 +1436,7 @@ export async function generateModelString(
1436
1436
}
1437
1437
1438
1438
static async latest(): Promise<${ modelName } Type | undefined> {
1439
- const model = await db .selectFrom('${ tableName } ')
1439
+ const model = await Db.instance .selectFrom('${ tableName } ')
1440
1440
.selectAll()
1441
1441
.orderBy('created_at', 'desc')
1442
1442
.executeTakeFirst()
@@ -1452,7 +1452,7 @@ export async function generateModelString(
1452
1452
}
1453
1453
1454
1454
static async oldest(): Promise<${ modelName } Type | undefined> {
1455
- const model = await db .selectFrom('${ tableName } ')
1455
+ const model = await Db.instance .selectFrom('${ tableName } ')
1456
1456
.selectAll()
1457
1457
.orderBy('created_at', 'asc')
1458
1458
.executeTakeFirst()
@@ -1481,7 +1481,7 @@ export async function generateModelString(
1481
1481
const value = condition[key]
1482
1482
1483
1483
// Attempt to find the first record matching the condition
1484
- const existing${ modelName } = await db .selectFrom('${ tableName } ')
1484
+ const existing${ modelName } = await Db.instance .selectFrom('${ tableName } ')
1485
1485
.selectAll()
1486
1486
.where(key, '=', value)
1487
1487
.executeTakeFirst()
@@ -1509,7 +1509,7 @@ export async function generateModelString(
1509
1509
const value = condition[key]
1510
1510
1511
1511
// Attempt to find the first record matching the condition
1512
- const existing${ modelName } = await db .selectFrom('${ tableName } ')
1512
+ const existing${ modelName } = await Db.instance .selectFrom('${ tableName } ')
1513
1513
.selectAll()
1514
1514
.where(key, '=', value)
1515
1515
.executeTakeFirst()
@@ -1522,7 +1522,7 @@ export async function generateModelString(
1522
1522
.executeTakeFirstOrThrow()
1523
1523
1524
1524
// Fetch and return the updated record
1525
- const updated${ modelName } = await db .selectFrom('${ tableName } ')
1525
+ const updated${ modelName } = await Db.instance .selectFrom('${ tableName } ')
1526
1526
.selectAll()
1527
1527
.where(key, '=', value)
1528
1528
.executeTakeFirst()
@@ -1553,14 +1553,14 @@ export async function generateModelString(
1553
1553
}
1554
1554
1555
1555
async last(): Promise<${ modelName } Type | undefined> {
1556
- return await db .selectFrom('${ tableName } ')
1556
+ return await Db.instance .selectFrom('${ tableName } ')
1557
1557
.selectAll()
1558
1558
.orderBy('id', 'desc')
1559
1559
.executeTakeFirst()
1560
1560
}
1561
1561
1562
1562
static async last(): Promise<${ modelName } Type | undefined> {
1563
- const model = await db .selectFrom('${ tableName } ').selectAll().orderBy('id', 'desc').executeTakeFirst()
1563
+ const model = await Db.instance .selectFrom('${ tableName } ').selectAll().orderBy('id', 'desc').executeTakeFirst()
1564
1564
1565
1565
if (!model)
1566
1566
return undefined
@@ -1713,7 +1713,7 @@ export async function generateModelString(
1713
1713
${ mittDeleteStatement }
1714
1714
${ thisSoftDeleteStatementsUpdateFrom }
1715
1715
1716
- return await db .deleteFrom('${ tableName } ')
1716
+ return await Db.instance .deleteFrom('${ tableName } ')
1717
1717
.where('id', '=', this.id)
1718
1718
.execute()
1719
1719
}
@@ -1772,7 +1772,7 @@ export async function generateModelString(
1772
1772
}
1773
1773
1774
1774
async function find(id: number): Promise<${ modelName } Model | undefined> {
1775
- let query = db .selectFrom('${ tableName } ').where('id', '=', id).selectAll()
1775
+ let query = Db.instance .selectFrom('${ tableName } ').where('id', '=', id).selectAll()
1776
1776
1777
1777
const model = await query.executeTakeFirst()
1778
1778
@@ -1801,7 +1801,7 @@ export async function generateModelString(
1801
1801
}
1802
1802
1803
1803
export async function remove(id: number): Promise<void> {
1804
- await db .deleteFrom('${ tableName } ')
1804
+ await Db.instance .deleteFrom('${ tableName } ')
1805
1805
.where('id', '=', id)
1806
1806
.execute()
1807
1807
}
0 commit comments