Use PostgreSQL for advanced, feature-rich conversation storage
The PostgreSQL adapter provides the most advanced database solution for BuilderBot, featuring automatic contact management, stored procedures, and support for complex data relationships.
Stores conversation history with foreign key to contacts:
CREATE TABLE IF NOT EXISTS history ( id SERIAL PRIMARY KEY, ref VARCHAR(255) NOT NULL, keyword VARCHAR(255), answer TEXT NOT NULL, refSerialize TEXT NOT NULL, phone VARCHAR(255) DEFAULT NULL, options JSONB, created_at TIMESTAMP DEFAULT current_timestamp, updated_in TIMESTAMP, contact_id INTEGER REFERENCES contact(id));
CREATE OR REPLACE FUNCTION save_or_update_contact( in_phone VARCHAR(255), in_values JSONB)RETURNS VOID AS $$DECLARE contact_id INT;BEGIN SELECT id INTO contact_id FROM contact WHERE phone = in_phone; IF contact_id IS NULL THEN INSERT INTO contact (phone, "values") VALUES (in_phone, in_values); ELSE UPDATE contact SET "values" = in_values, updated_in = current_timestamp WHERE id = contact_id; END IF;END;$$ LANGUAGE plpgsql;
Saves history entry and updates contact automatically:
CREATE OR REPLACE FUNCTION save_or_update_history_and_contact( in_ref VARCHAR(255), in_keyword VARCHAR(255), in_answer TEXT, in_refserialize TEXT, in_phone VARCHAR(255), in_options JSONB)RETURNS VOID AS $$DECLARE _contact_id INT;BEGIN SELECT id INTO _contact_id FROM contact WHERE phone = in_phone; IF _contact_id IS NULL THEN INSERT INTO contact (phone) VALUES (in_phone) RETURNING id INTO _contact_id; ELSE UPDATE contact SET last_interaction = current_timestamp WHERE id = _contact_id; END IF; INSERT INTO history (ref, keyword, answer, refserialize, phone, options, contact_id, created_at) VALUES (in_ref, in_keyword, in_answer, in_refserialize, in_phone, in_options, _contact_id, current_timestamp);END;$$ LANGUAGE plpgsql;
-- Create databaseCREATE DATABASE builderbot;-- Create userCREATE USER builderbot_user WITH PASSWORD 'your_password';-- Grant privilegesGRANT ALL PRIVILEGES ON DATABASE builderbot TO builderbot_user;-- Connect to database\c builderbot-- Grant schema privilegesGRANT ALL ON SCHEMA public TO builderbot_user;-- Exit\q
-- Indexes on history tableCREATE INDEX idx_history_phone ON history(phone);CREATE INDEX idx_history_created ON history(created_at DESC);CREATE INDEX idx_history_contact ON history(contact_id);-- Indexes on contact tableCREATE INDEX idx_contact_phone ON contact(phone);CREATE INDEX idx_contact_last_interaction ON contact(last_interaction DESC);-- Index on JSONB values (for searching contact data)CREATE INDEX idx_contact_values ON contact USING GIN (values);
-- Find contacts with specific valueSELECT * FROM contact WHERE values->>'name' = 'John Doe';-- Find contacts with emailSELECT * FROM contact WHERE values ? 'email';-- Find contacts with notifications enabledSELECT * FROM contact WHERE values->'preferences'->>'notifications' = 'true';
import { Pool } from 'pg'async init(): Promise<boolean> { const pool = new Pool(this.credentials) const db = await pool.connect() this.db = db return true}
For custom pool configuration:
import { Pool } from 'pg'const pool = new Pool({ host: process.env.POSTGRES_DB_HOST, user: process.env.POSTGRES_DB_USER, database: process.env.POSTGRES_DB_NAME, password: process.env.POSTGRES_DB_PASSWORD, port: 5432, max: 20, // Maximum pool size idleTimeoutMillis: 30000, // Close idle clients after 30s connectionTimeoutMillis: 2000 // Return error after 2s if no connection available})
GRANT ALL ON SCHEMA public TO builderbot_user;GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO builderbot_user;GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO builderbot_user;