@ -10,7 +10,6 @@ import type {
MessageEntity ,
MessageEntity ,
} from "./common.ts" ;
} from "./common.ts" ;
import type { CommittedMessage } from "../types" ;
import type { CommittedMessage } from "../types" ;
import { env } from "../server/env.js" ;
// export const pool = new Pool({
// export const pool = new Pool({
// connectionString: env.POSTGRES_CONNECTION_STRING as string,
// connectionString: env.POSTGRES_CONNECTION_STRING as string,
@ -21,244 +20,254 @@ import { env } from "../server/env.js";
// pool,
// pool,
// });
// });
const dialect = new NeonDialect ( {
export function getDbClient ( POSTGRES_CONNECTION_STRING : string ) {
neon : neon ( env . POSTGRES_CONNECTION_STRING as string ) ,
const dialect = new NeonDialect ( {
} ) ;
neon : neon ( POSTGRES_CONNECTION_STRING as string ) ,
} ) ;
// Database interface is passed to Kysely's constructor, and from now on, Kysely
// Database interface is passed to Kysely's constructor, and from now on, Kysely
// knows your database structure.
// knows your database structure.
// Dialect is passed to Kysely's constructor, and from now on, Kysely knows how
// Dialect is passed to Kysely's constructor, and from now on, Kysely knows how
// to communicate with your database.
// to communicate with your database.
export const dbClient = new Kysely < Database > ( {
const dbClient = new Kysely < Database > ( {
dialect ,
dialect ,
} ) ;
} ) ;
const conversations : ConversationEntity = {
return dbClient ;
construct : ( conversation ) = > conversation ,
}
create : async ( conversation ) = > {
const insertedRows = await dbClient
. insertInto ( "conversations" )
. values ( conversation )
. returningAll ( )
. execute ( ) ;
return insertedRows [ 0 ] ;
} ,
createMany : async ( conversations ) = > {
const insertedRows = await dbClient
. insertInto ( "conversations" )
. values ( conversations )
. returningAll ( )
. execute ( ) ;
return insertedRows ;
} ,
findAll : async ( ) = > {
const rows = await dbClient
. selectFrom ( "conversations" )
. selectAll ( )
. execute ( ) ;
return rows ;
} ,
findById : async ( id ) = > {
const row = await dbClient
. selectFrom ( "conversations" )
. selectAll ( )
. where ( "id" , "=" , id )
. execute ( ) ;
return row [ 0 ] ;
} ,
update : async ( id , data ) = > {
await dbClient
. updateTable ( "conversations" )
. set ( data )
. where ( "id" , "=" , id )
. execute ( ) ;
} ,
delete : async ( id ) = > {
await dbClient . deleteFrom ( "conversations" ) . where ( "id" , "=" , id ) . execute ( ) ;
} ,
fetchMessages : async ( conversationId ) = > {
const rows = await dbClient
. selectFrom ( "messages" )
. selectAll ( )
. where ( "conversationId" , "=" , conversationId )
. execute ( ) ;
return rows as Array < CommittedMessage > ;
} ,
} ;
const facts : FactEntity = {
export function getDb ( POSTGRES_CONNECTION_STRING : string ) {
construct : ( fact ) = > fact ,
const dbClient = getDbClient ( POSTGRES_CONNECTION_STRING ) ;
create : async ( fact ) = > {
const insertedRows = await dbClient
. insertInto ( "facts" )
. values ( fact )
. returningAll ( )
. execute ( ) ;
return insertedRows [ 0 ] ;
} ,
createMany : async ( facts ) = > {
const insertedRows = await dbClient
. insertInto ( "facts" )
. values ( facts )
. returningAll ( )
. execute ( ) ;
return insertedRows ;
} ,
findAll : async ( ) = > {
const rows = await dbClient . selectFrom ( "facts" ) . selectAll ( ) . execute ( ) ;
return rows ;
} ,
findById : async ( id ) = > {
const row = await dbClient
. selectFrom ( "facts" )
. selectAll ( )
. where ( "id" , "=" , id )
. execute ( ) ;
return row [ 0 ] ;
} ,
update : async ( id , data ) = > {
await dbClient
. updateTable ( "facts" )
. set ( data )
. where ( "id" , "=" , id )
. execute ( ) ;
} ,
delete : async ( id ) = > {
await dbClient . deleteFrom ( "facts" ) . where ( "id" , "=" , id ) . execute ( ) ;
} ,
findByConversationId : async ( conversationId ) = > {
const rows = await dbClient
. selectFrom ( "facts" )
. innerJoin ( "messages" , "messages.id" , "facts.sourceMessageId" )
. selectAll ( "facts" )
. where ( "conversationId" , "=" , conversationId )
. execute ( ) ;
return rows ;
} ,
} ;
const factTriggers : FactTriggerEntity = {
const conversations : ConversationEntity = {
construct : ( factTrigger ) = > factTrigger ,
construct : ( conversation ) = > conversation ,
create : async ( factTrigger ) = > {
create : async ( conversation ) = > {
const insertedRows = await dbClient
const insertedRows = await dbClient
. insertInto ( "fact_triggers" )
. insertInto ( "conversations" )
. values ( factTrigger )
. values ( conversation )
. returningAll ( )
. returningAll ( )
. execute ( ) ;
. execute ( ) ;
return insertedRows [ 0 ] ;
return insertedRows [ 0 ] ;
} ,
} ,
createMany : async ( factTriggers ) = > {
createMany : async ( conversations ) = > {
const insertedRows = await dbClient
const insertedRows = await dbClient
. insertInto ( "fact_triggers" )
. insertInto ( "conversations" )
. values ( factTriggers )
. values ( conversations )
. returningAll ( )
. returningAll ( )
. execute ( ) ;
. execute ( ) ;
return insertedRows ;
return insertedRows ;
} ,
} ,
findAll : async ( ) = > {
findAll : async ( ) = > {
const rows = await dbClient
const rows = await dbClient
. selectFrom ( "fact_triggers" )
. selectFrom ( "conversations" )
. selectAll ( )
. selectAll ( )
. execute ( ) ;
. execute ( ) ;
return rows ;
return rows ;
} ,
} ,
findById : async ( id ) = > {
findById : async ( id ) = > {
const row = await dbClient
const row = await dbClient
. selectFrom ( "fact_triggers" )
. selectFrom ( "conversations" )
. selectAll ( )
. selectAll ( )
. where ( "id" , "=" , id )
. where ( "id" , "=" , id )
. execute ( ) ;
. execute ( ) ;
return row [ 0 ] ;
return row [ 0 ] ;
} ,
} ,
update : async ( id , data ) = > {
update : async ( id , data ) = > {
await dbClient
await dbClient
. updateTable ( "fact_triggers" )
. updateTable ( "conversations" )
. set ( data )
. set ( data )
. where ( "id" , "=" , id )
. where ( "id" , "=" , id )
. execute ( ) ;
. execute ( ) ;
} ,
} ,
delete : async ( id ) = > {
delete : async ( id ) = > {
await dbClient . deleteFrom ( "fact_triggers" ) . where ( "id" , "=" , id ) . execute ( ) ;
await dbClient . deleteFrom ( "conversations" ) . where ( "id" , "=" , id ) . execute ( ) ;
} ,
} ,
findByFactId : async ( factId ) = > {
fetchMessages : async ( conversationId ) = > {
const rows = await dbClient
const rows = await dbClient
. selectFrom ( "fact_triggers" )
. selectFrom ( "messages" )
. innerJoin ( "facts" , "facts.id" , "fact_triggers.sourceFactId" )
. selectAll ( )
. selectAll ( "fact_triggers" )
. where ( "conversationId" , "=" , conversationId )
. where ( "sourceFactId" , "=" , factId )
. execute ( ) ;
. execute ( ) ;
return rows as Array < CommittedMessage > ;
return rows ;
} ,
} ,
} ;
findByConversationId : async ( conversationId ) = > {
const rows = await dbClient
. selectFrom ( "fact_triggers" )
. innerJoin ( "facts" , "facts.id" , "fact_triggers.sourceFactId" )
. innerJoin ( "messages" , "messages.id" , "facts.sourceMessageId" )
. selectAll ( "fact_triggers" )
. where ( "messages.conversationId" , "=" , conversationId )
. execute ( ) ;
return rows ;
} ,
} ;
const messages : MessageEntity = {
const facts : FactEntity = {
construct : ( message ) = > message ,
construct : ( fact ) = > fact ,
create : async ( message ) = > {
create : async ( fact ) = > {
const insertedRows = await dbClient
const insertedRows = await dbClient
. insertInto ( "messages" )
. insertInto ( "facts" )
. values ( { . . . message , parts : JSON.stringify ( message . parts ) } )
. values ( fact )
. returningAll ( )
. returningAll ( )
. execute ( ) ;
. execute ( ) ;
return insertedRows [ 0 ] as CommittedMessage ;
return insertedRows [ 0 ] ;
} ,
} ,
createMany : async ( messages ) = > {
createMany : async ( facts ) = > {
const insertedRows = await dbClient
const insertedRows = await dbClient
. insertInto ( "messages" )
. insertInto ( "facts" )
. values (
. values ( facts )
messages . map ( ( message ) = > ( {
. returningAll ( )
. . . message ,
. execute ( ) ;
parts : JSON.stringify ( message . parts ) ,
return insertedRows ;
} ) )
} ,
)
findAll : async ( ) = > {
. returningAll ( )
const rows = await dbClient . selectFrom ( "facts" ) . selectAll ( ) . execute ( ) ;
. execute ( ) ;
return rows ;
return insertedRows as Array < CommittedMessage > ;
} ,
} ,
findById : async ( id ) = > {
findAll : async ( ) = > {
const row = await dbClient
const rows = await dbClient . selectFrom ( "messages" ) . selectAll ( ) . execute ( ) ;
. selectFrom ( "facts" )
return rows as Array < CommittedMessage > ;
. selectAll ( )
} ,
. where ( "id" , "=" , id )
findById : async ( id ) = > {
. execute ( ) ;
const row = await dbClient
return row [ 0 ] ;
. selectFrom ( "messages" )
} ,
. selectAll ( )
update : async ( id , data ) = > {
. where ( "id" , "=" , id )
await dbClient
. execute ( ) ;
. updateTable ( "facts" )
return row [ 0 ] as CommittedMessage ;
. set ( data )
} ,
. where ( "id" , "=" , id )
update : async ( id , data ) = > {
. execute ( ) ;
await dbClient
} ,
. updateTable ( "messages" )
delete : async ( id ) = > {
. set ( data )
await dbClient . deleteFrom ( "facts" ) . where ( "id" , "=" , id ) . execute ( ) ;
. where ( "id" , "=" , id )
} ,
. execute ( ) ;
findByConversationId : async ( conversationId ) = > {
} ,
const rows = await dbClient
delete : async ( id ) = > {
. selectFrom ( "facts" )
await dbClient . deleteFrom ( "messages" ) . where ( "id" , "=" , id ) . execute ( ) ;
. innerJoin ( "messages" , "messages.id" , "facts.sourceMessageId" )
} ,
. selectAll ( "facts" )
findByConversationId : async ( conversationId ) = > {
. where ( "conversationId" , "=" , conversationId )
const rows = ( await dbClient
. execute ( ) ;
. selectFrom ( "messages" )
return rows ;
. selectAll ( )
} ,
. where ( "conversationId" , "=" , conversationId )
} ;
. execute ( ) ) as Array < CommittedMessage > ;
return rows ;
} ,
} ;
export const db = {
const factTriggers : FactTriggerEntity = {
conversations ,
construct : ( factTrigger ) = > factTrigger ,
facts ,
create : async ( factTrigger ) = > {
factTriggers ,
const insertedRows = await dbClient
messages ,
. insertInto ( "fact_triggers" )
} ;
. values ( factTrigger )
. returningAll ( )
. execute ( ) ;
return insertedRows [ 0 ] ;
} ,
createMany : async ( factTriggers ) = > {
const insertedRows = await dbClient
. insertInto ( "fact_triggers" )
. values ( factTriggers )
. returningAll ( )
. execute ( ) ;
return insertedRows ;
} ,
findAll : async ( ) = > {
const rows = await dbClient
. selectFrom ( "fact_triggers" )
. selectAll ( )
. execute ( ) ;
return rows ;
} ,
findById : async ( id ) = > {
const row = await dbClient
. selectFrom ( "fact_triggers" )
. selectAll ( )
. where ( "id" , "=" , id )
. execute ( ) ;
return row [ 0 ] ;
} ,
update : async ( id , data ) = > {
await dbClient
. updateTable ( "fact_triggers" )
. set ( data )
. where ( "id" , "=" , id )
. execute ( ) ;
} ,
delete : async ( id ) = > {
await dbClient . deleteFrom ( "fact_triggers" ) . where ( "id" , "=" , id ) . execute ( ) ;
} ,
findByFactId : async ( factId ) = > {
const rows = await dbClient
. selectFrom ( "fact_triggers" )
. innerJoin ( "facts" , "facts.id" , "fact_triggers.sourceFactId" )
. selectAll ( "fact_triggers" )
. where ( "sourceFactId" , "=" , factId )
. execute ( ) ;
return rows ;
} ,
findByConversationId : async ( conversationId ) = > {
const rows = await dbClient
. selectFrom ( "fact_triggers" )
. innerJoin ( "facts" , "facts.id" , "fact_triggers.sourceFactId" )
. innerJoin ( "messages" , "messages.id" , "facts.sourceMessageId" )
. selectAll ( "fact_triggers" )
. where ( "messages.conversationId" , "=" , conversationId )
. execute ( ) ;
return rows ;
} ,
} ;
const messages : MessageEntity = {
construct : ( message ) = > message ,
create : async ( message ) = > {
const insertedRows = await dbClient
. insertInto ( "messages" )
. values ( { . . . message , parts : JSON.stringify ( message . parts ) } )
. returningAll ( )
. execute ( ) ;
return insertedRows [ 0 ] as CommittedMessage ;
} ,
createMany : async ( messages ) = > {
const insertedRows = await dbClient
. insertInto ( "messages" )
. values (
messages . map ( ( message ) = > ( {
. . . message ,
parts : JSON.stringify ( message . parts ) ,
} ) )
)
. returningAll ( )
. execute ( ) ;
return insertedRows as Array < CommittedMessage > ;
} ,
findAll : async ( ) = > {
const rows = await dbClient . selectFrom ( "messages" ) . selectAll ( ) . execute ( ) ;
return rows as Array < CommittedMessage > ;
} ,
findById : async ( id ) = > {
const row = await dbClient
. selectFrom ( "messages" )
. selectAll ( )
. where ( "id" , "=" , id )
. execute ( ) ;
return row [ 0 ] as CommittedMessage ;
} ,
update : async ( id , data ) = > {
await dbClient
. updateTable ( "messages" )
. set ( data )
. where ( "id" , "=" , id )
. execute ( ) ;
} ,
delete : async ( id ) = > {
await dbClient . deleteFrom ( "messages" ) . where ( "id" , "=" , id ) . execute ( ) ;
} ,
findByConversationId : async ( conversationId ) = > {
const rows = ( await dbClient
. selectFrom ( "messages" )
. selectAll ( )
. where ( "conversationId" , "=" , conversationId )
. execute ( ) ) as Array < CommittedMessage > ;
return rows ;
} ,
} ;
const db = {
conversations ,
facts ,
factTriggers ,
messages ,
} ;
return db ;
}