Skip to content

Commit

Permalink
Merge pull request #173 from minghinmatthewlam/main
Browse files Browse the repository at this point in the history
postgres updates
  • Loading branch information
lalalune authored Nov 3, 2024
2 parents b00d1ea + 3b2d129 commit bc4c482
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions core/src/adapters/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
}): Promise<Memory[]> {
const client = await this.pool.connect();
try {
if (params.roomIds.length === 0) return [];
const placeholders = params.roomIds
.map((_, i) => `$${i + 2}`)
.join(", ");
Expand Down Expand Up @@ -304,7 +305,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
`;

if (params.unique) {
sql += " AND unique = true";
sql += ` AND "unique" = true`;
}

sql += ` AND 1 - (embedding <-> $3) >= $4
Expand Down Expand Up @@ -349,18 +350,18 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {

if (params.start) {
paramCount++;
sql += ` AND "createdAt" >= to_timestamp($${paramCount / 1000})`;
values.push(params.start);
sql += ` AND "createdAt" >= to_timestamp($${paramCount})`;
values.push(params.start/1000);
}

if (params.end) {
paramCount++;
sql += ` AND "createdAt" <= to_timestamp($${paramCount / 1000})`;
values.push(params.end);
sql += ` AND "createdAt" <= to_timestamp($${paramCount})`;
values.push(params.end/1000);
}

if (params.unique) {
sql += " AND unique = true";
sql += ` AND "unique" = true`;
}

if (params.agentId) {
Expand All @@ -382,9 +383,9 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
return rows.map((row) => ({
...row,
content:
typeof rows.content === "string"
? JSON.parse(rows.content)
: rows.content,
typeof row.content === "string"
? JSON.parse(row.content)
: row.content,
}));
} finally {
client.release();
Expand Down Expand Up @@ -759,7 +760,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
try {
let sql = `SELECT COUNT(*) as count FROM memories WHERE type = $1 AND "roomId" = $2`;
if (unique) {
sql += " AND unique = true";
sql += ` AND "unique" = true`;
}

const { rows } = await client.query(sql, [tableName, roomId]);
Expand Down Expand Up @@ -796,7 +797,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
async getRoomsForParticipants(userIds: UUID[]): Promise<UUID[]> {
const client = await this.pool.connect();
try {
const placeholders = userIds.map((_, i) => `${i + 1}`).join(", ");
const placeholders = userIds.map((_, i) => `$${i + 1}`).join(", ");
const { rows } = await client.query(
`SELECT DISTINCT "roomId" FROM participants WHERE "userId" IN (${placeholders})`,
userIds
Expand Down

0 comments on commit bc4c482

Please sign in to comment.