openraas
Version:
Open Robot-as-a-Service Protocol - A comprehensive TypeScript library for building and consuming RaaS applications with X402 payment support on Solana
43 lines (42 loc) • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostgresRegistry = void 0;
const pg_1 = require("pg");
class PostgresRegistry {
constructor(connectionString) {
this.pool = new pg_1.Pool({ connectionString });
this.init();
}
async init() {
await this.pool.query(`
CREATE TABLE IF NOT EXISTS robots (
id TEXT PRIMARY KEY,
data JSONB
)
`);
}
async register(robot) {
await this.pool.query('INSERT INTO robots (id, data) VALUES ($1, $2) ON CONFLICT (id) DO UPDATE SET data = $2', [robot.id, robot]);
}
async unregister(robotId) {
await this.pool.query('DELETE FROM robots WHERE id = $1', [robotId]);
}
async find(criteria) {
// Basic JSONB query - in production this would be more complex
const result = await this.pool.query('SELECT data FROM robots');
const robots = result.rows.map((row) => row.data);
return robots.filter((robot) => {
for (const key in criteria) {
if (robot[key] !== criteria[key]) {
return false;
}
}
return true;
});
}
async get(robotId) {
const result = await this.pool.query('SELECT data FROM robots WHERE id = $1', [robotId]);
return result.rows.length > 0 ? result.rows[0].data : null;
}
}
exports.PostgresRegistry = PostgresRegistry;