UNPKG

@iota-big3/sdk-security

Version:

Advanced security features including zero trust, quantum-safe crypto, and ML threat detection

85 lines 3.02 kB
"use strict"; /** * Database Adapter for SDK Security * Adapts the actual SDKDatabaseManager to match our SecurityDatabaseContract */ Object.defineProperty(exports, "__esModule", { value: true }); exports.DatabaseAdapter = void 0; class DatabaseAdapter { constructor(db) { this.db = db; } /** * Insert using raw SQL since SDKDatabaseManager doesn't have insert method */ async insert(table, data) { const columns = Object.keys(data); const values = Object.values(data); const placeholders = columns.map(() => '?').join(', '); const sql = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${placeholders})`; // Use transaction to get the inserted ID const result = await this.db.transaction(async (trx) => { await trx.raw(sql, values); const [{ lastID }] = await trx.raw('SELECT last_insert_rowid() as lastID'); return { id: String(lastID), affected: 1 }; }); return result; } /** * Query adapter - transforms array result to { rows: [] } format */ async query(sql, params) { const results = await this.db.query(sql, params); return { rows: results }; } /** * Update using raw SQL */ async update(table, data, where) { const setClauses = Object.keys(data).map(key => `${key} = ?`).join(', '); const whereClauses = Object.keys(where).map(key => `${key} = ?`).join(' AND '); const values = [...Object.values(data), ...Object.values(where)]; const sql = `UPDATE ${table} SET ${setClauses} WHERE ${whereClauses}`; const result = await this.db.transaction(async (trx) => { const { rowCount } = await trx.raw(sql, values); return { affected: rowCount || 0 }; }); return result; } /** * Delete using raw SQL */ async delete(table, where) { const whereClauses = Object.keys(where).map(key => `${key} = ?`).join(' AND '); const values = Object.values(where); const sql = `DELETE FROM ${table} WHERE ${whereClauses}`; const result = await this.db.transaction(async (trx) => { const { rowCount } = await trx.raw(sql, values); return { affected: rowCount || 0 }; }); return result; } /** * Pass through transaction method */ async transaction(callback) { return this.db.transaction(callback); } /** * Pass through initialize method */ async initialize() { return this.db.initialize(); } /** * Create table using raw SQL */ async createTable(name, schema) { // This would need to be implemented based on schema format // For now, it's a placeholder const sql = `CREATE TABLE IF NOT EXISTS ${name} (id INTEGER PRIMARY KEY)`; await this.db.query(sql); } } exports.DatabaseAdapter = DatabaseAdapter; //# sourceMappingURL=database.adapter.js.map