UNPKG

donobu

Version:

Create browser automations with an LLM agent and replay them as Playwright scripts.

91 lines 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SuitesPersistenceSqlite = void 0; const SuiteNotFoundException_1 = require("../../exceptions/SuiteNotFoundException"); const SuiteMetadata_1 = require("../../models/SuiteMetadata"); class SuitesPersistenceSqlite { constructor(db) { this.db = db; } static async create(db) { return new SuitesPersistenceSqlite(db); } async createSuite(suiteMetadata) { const stmt = this.db.prepare(` INSERT INTO suite_metadata (id, name, metadata, created_at) VALUES (@id, @name, @metadata, @createdAt) `); const insert = this.db.transaction((suite) => { stmt.run({ id: suite.id, name: suite.name, metadata: JSON.stringify(suite), createdAt: Date.now(), }); }); insert.immediate(suiteMetadata); } async updateSuite(suiteMetadata) { const stmt = this.db.prepare(` UPDATE suite_metadata SET name = @name, metadata = @metadata WHERE id = @id `); const result = stmt.run({ id: suiteMetadata.id, name: suiteMetadata.name, metadata: JSON.stringify(suiteMetadata), }); if (result.changes === 0) { throw SuiteNotFoundException_1.SuiteNotFoundException.forId(suiteMetadata.id); } } async getSuiteById(suiteId) { const stmt = this.db.prepare('SELECT metadata FROM suite_metadata WHERE id = ?'); const row = stmt.get(suiteId); if (!row) { throw SuiteNotFoundException_1.SuiteNotFoundException.forId(suiteId); } return SuiteMetadata_1.SuiteMetadataSchema.parse(JSON.parse(row.metadata)); } async getSuites(query) { const validLimit = Math.max(1, Math.min(100, query.limit ?? 100)); const offset = query.pageToken ? parseInt(query.pageToken, 10) || 0 : 0; const whereConditions = []; const params = []; // partialName takes precedence over name if both are provided. if (query.partialName) { whereConditions.push('name LIKE ?'); params.push(`%${query.partialName}%`); } else if (query.name) { whereConditions.push('name = ?'); params.push(query.name); } let sql = 'SELECT metadata FROM suite_metadata'; if (whereConditions.length > 0) { sql += ' WHERE ' + whereConditions.join(' AND '); } const sortCol = query.sortBy ?? 'created_at'; const sortDir = query.sortOrder === 'asc' ? 'ASC' : 'DESC'; sql += ` ORDER BY ${sortCol} ${sortDir} LIMIT ? OFFSET ?`; params.push(validLimit + 1); params.push(offset); const stmt = this.db.prepare(sql); const rows = stmt.all(...params); const hasMore = rows.length > validLimit; const results = hasMore ? rows.slice(0, validLimit) : rows; const nextPageToken = hasMore ? `${offset + validLimit}` : undefined; return { items: results.map((row) => SuiteMetadata_1.SuiteMetadataSchema.parse(JSON.parse(row.metadata))), nextPageToken, }; } async deleteSuite(suiteId) { // Tests are orphaned (suite_id set to null) by the DB FK constraint. const stmt = this.db.prepare('DELETE FROM suite_metadata WHERE id = ?'); stmt.run(suiteId); } } exports.SuitesPersistenceSqlite = SuitesPersistenceSqlite; //# sourceMappingURL=SuitesPersistenceSqlite.js.map