@@ -243,8 +243,8 @@ export class PGLiteDatabaseAdapter
243
243
try {
244
244
const accountId = account . id ?? v4 ( ) ;
245
245
await this . query (
246
- `INSERT INTO accounts (id, name, username, email, "avatarUrl", details, status)
247
- VALUES ($1, $2, $3, $4, $5, $6, $7)` ,
246
+ `INSERT INTO accounts (id, name, username, email, "avatarUrl", details, status, pid, source )
247
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9 )` ,
248
248
[
249
249
accountId ,
250
250
account . name ,
@@ -253,6 +253,8 @@ export class PGLiteDatabaseAdapter
253
253
account . avatarUrl || "" ,
254
254
JSON . stringify ( account . details ) ,
255
255
account . status || "paused" ,
256
+ account . pid || "" ,
257
+ account . source || "" ,
256
258
]
257
259
) ;
258
260
elizaLogger . debug ( "Account created successfully:" , {
@@ -1599,42 +1601,11 @@ export class PGLiteDatabaseAdapter
1599
1601
} = params ;
1600
1602
1601
1603
const offset = ( page - 1 ) * pageSize ;
1602
- const whereClause : string [ ] = [ ] ;
1603
- const values : any [ ] = [ ] ;
1604
- let paramCount = 0 ;
1605
-
1606
- // Build where clause with proper parameter indexing
1607
- // biome-ignore lint/complexity/noForEach: <explanation>
1608
- Object . entries ( where ) . forEach ( ( [ key , value ] ) => {
1609
- if ( value === null || value === undefined ) return ;
1610
-
1611
- if ( typeof value === 'object' ) {
1612
- if ( key === 'createdAt' ) {
1613
- if ( value . gte ) {
1614
- paramCount ++ ;
1615
- whereClause . push ( `"${ key } " >= $${ paramCount } ` ) ;
1616
- values . push ( value . gte ) ;
1617
- }
1618
- if ( value . lte ) {
1619
- paramCount ++ ;
1620
- whereClause . push ( `"${ key } " <= $${ paramCount } ` ) ;
1621
- values . push ( value . lte ) ;
1622
- }
1623
- }
1624
- } else {
1625
- paramCount ++ ;
1626
- whereClause . push ( `"${ key } " = $${ paramCount } ` ) ;
1627
- values . push ( value ) ;
1628
- }
1629
- } ) ;
1630
-
1631
- const whereStr = whereClause . length > 0
1632
- ? `WHERE ${ whereClause . join ( ' AND ' ) } `
1604
+ const { whereClause, whereParams } = this . buildWhereClause ( where ) ;
1605
+ const whereStr = whereClause . length > 0
1606
+ ? `WHERE ${ whereClause . join ( ' AND ' ) } `
1633
1607
: '' ;
1634
-
1635
- const orderClause = Object . entries ( order )
1636
- . map ( ( [ key , direction ] ) => `"${ key } " ${ direction } ` )
1637
- . join ( ', ' ) ;
1608
+ const orderClause = this . buildOrderClause ( order ) ;
1638
1609
1639
1610
// Count total records
1640
1611
const countQuery = `
@@ -1643,40 +1614,103 @@ export class PGLiteDatabaseAdapter
1643
1614
${ whereStr }
1644
1615
` ;
1645
1616
1646
- const { rows : countRows } = await this . query < { total : number } > (
1647
- countQuery ,
1648
- values
1649
- ) ;
1650
- const total = Number ( countRows [ 0 ] ?. total || 0 ) ;
1651
-
1652
1617
// Get paginated data
1653
- paramCount ++ ;
1654
- const limitParam = `$${ paramCount } ` ;
1655
- paramCount ++ ;
1656
- const offsetParam = `$${ paramCount } ` ;
1657
-
1658
1618
const dataQuery = `
1659
1619
SELECT *
1660
1620
FROM "${ table } "
1661
1621
${ whereStr }
1662
- ORDER BY ${ orderClause }
1663
- LIMIT ${ limitParam } OFFSET ${ offsetParam }
1622
+ ${ orderClause }
1623
+ LIMIT $${ whereParams . length + 1 }
1624
+ OFFSET $${ whereParams . length + 2 }
1664
1625
` ;
1665
1626
1627
+ elizaLogger . debug ( "Pagination query:" , {
1628
+ countQuery,
1629
+ dataQuery,
1630
+ params : [ ...whereParams , pageSize , offset ]
1631
+ } ) ;
1632
+
1633
+ const { rows : countRows } = await this . query < { total : number } > (
1634
+ countQuery ,
1635
+ whereParams
1636
+ ) ;
1637
+ const total = Number ( countRows [ 0 ] ?. total || 0 ) ;
1638
+
1666
1639
const { rows : list } = await this . query (
1667
1640
dataQuery ,
1668
- [ ...values , pageSize , offset ]
1641
+ [ ...whereParams , pageSize , offset ]
1669
1642
) ;
1670
1643
1671
1644
return {
1672
- list,
1673
1645
total,
1674
1646
page,
1675
1647
pageSize,
1676
1648
totalPages : Math . ceil ( total / pageSize ) ,
1649
+ list,
1677
1650
} ;
1678
1651
} , "paginate" ) ;
1679
1652
}
1653
+
1654
+ private buildWhereClause ( where : Record < string , any > ) : { whereClause : string [ ] , whereParams : any [ ] } {
1655
+ const whereClause : string [ ] = [ ] ;
1656
+ const whereParams : any [ ] = [ ] ;
1657
+ let paramCount = 0 ;
1658
+
1659
+ // Handle where conditions
1660
+ // biome-ignore lint/complexity/noForEach: <explanation>
1661
+ Object . entries ( where ) . forEach ( ( [ key , value ] ) => {
1662
+ if ( value === undefined ) return ;
1663
+
1664
+ if ( typeof value === 'object' ) {
1665
+ if ( key === 'createdAt' ) {
1666
+ if ( value . gte ) {
1667
+ paramCount ++ ;
1668
+ whereClause . push ( `"${ key } " >= $${ paramCount } ` ) ;
1669
+ whereParams . push ( value . gte ) ;
1670
+ }
1671
+ if ( value . lte ) {
1672
+ paramCount ++ ;
1673
+ whereClause . push ( `"${ key } " <= $${ paramCount } ` ) ;
1674
+ whereParams . push ( value . lte ) ;
1675
+ }
1676
+ }
1677
+ // Add other comparison operators as needed
1678
+ if ( 'ne' in value ) {
1679
+ paramCount ++ ;
1680
+ whereClause . push ( `"${ key } " != $${ paramCount } ` ) ;
1681
+ whereParams . push ( value . ne ) ;
1682
+ }
1683
+ if ( 'eq' in value ) {
1684
+ paramCount ++ ;
1685
+ whereClause . push ( `"${ key } " = $${ paramCount } ` ) ;
1686
+ whereParams . push ( value . eq ) ;
1687
+ }
1688
+ if ( 'gt' in value ) {
1689
+ paramCount ++ ;
1690
+ whereClause . push ( `"${ key } " > $${ paramCount } ` ) ;
1691
+ whereParams . push ( value . gt ) ;
1692
+ }
1693
+ if ( 'lt' in value ) {
1694
+ paramCount ++ ;
1695
+ whereClause . push ( `"${ key } " < $${ paramCount } ` ) ;
1696
+ whereParams . push ( value . lt ) ;
1697
+ }
1698
+ } else {
1699
+ paramCount ++ ;
1700
+ whereClause . push ( `"${ key } " = $${ paramCount } ` ) ;
1701
+ whereParams . push ( value ) ;
1702
+ }
1703
+ } ) ;
1704
+
1705
+ return { whereClause, whereParams } ;
1706
+ }
1707
+
1708
+ private buildOrderClause ( order : Record < string , string > ) : string {
1709
+ const orderClause = Object . entries ( order )
1710
+ . map ( ( [ key , direction ] ) => `"${ key } " ${ direction } ` )
1711
+ . join ( ', ' ) ;
1712
+ return orderClause ? ` ORDER BY ${ orderClause } ` : '' ;
1713
+ }
1680
1714
}
1681
1715
1682
1716
export default PGLiteDatabaseAdapter ;
0 commit comments