@sentzunhat/zacatl
Version:
A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.
185 lines • 6.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeSqliteAdapter = void 0;
const error_1 = require("../../../../../error");
const third_party_1 = require("../../../../../third-party");
class NodeSqliteAdapter {
config;
constructor(config) {
this.config = config;
this.ensureTableExists();
}
get model() {
return this.config.database;
}
ensureTableExists() {
const { database, tableName } = this.config;
try {
const checkTable = database.prepare(`SELECT name FROM sqlite_master WHERE type='table' AND name=?`);
const exists = checkTable.get(tableName);
if (exists == null) {
database.exec(`
CREATE TABLE IF NOT EXISTS ${tableName} (
id TEXT PRIMARY KEY,
data TEXT NOT NULL,
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
}
}
catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
throw new error_1.InternalServerError({
message: `Failed to ensure node:sqlite table exists: ${error.message}`,
reason: 'Table initialization failed',
component: 'NodeSqliteAdapter',
operation: 'ensureTableExists',
});
}
}
toLean(input) {
if (input == null)
return null;
try {
if (typeof input === 'string') {
const parsed = JSON.parse(input);
return parsed;
}
if (typeof input === 'object') {
return input;
}
return null;
}
catch {
return null;
}
}
async findById(id) {
try {
const { database, tableName } = this.config;
const stmt = database.prepare(`SELECT data FROM ${tableName} WHERE id = ?`);
const row = stmt.get(id);
if (!row)
return null;
return this.toLean(row.data);
}
catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
throw new error_1.InternalServerError({
message: `Failed to find record by id: ${error.message}`,
reason: 'node:sqlite query failed',
component: 'NodeSqliteAdapter',
operation: 'findById',
metadata: { id },
});
}
}
async findMany(_filter) {
try {
const { database, tableName } = this.config;
const stmt = database.prepare(`SELECT data FROM ${tableName}`);
const rows = stmt.all();
return rows.map((row) => this.toLean(row.data));
}
catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
throw new error_1.InternalServerError({
message: `Failed to find records: ${error.message}`,
reason: 'node:sqlite query failed',
component: 'NodeSqliteAdapter',
operation: 'findMany',
});
}
}
async create(entity) {
try {
const { database, tableName } = this.config;
const id = (0, third_party_1.uuidv4)();
const now = new Date();
const stmt = database.prepare(`INSERT INTO ${tableName} (id, data, createdAt, updatedAt) VALUES (?, ?, ?, ?)`);
stmt.run(id, JSON.stringify(entity), now.toISOString(), now.toISOString());
return {
...entity,
id,
createdAt: now,
updatedAt: now,
};
}
catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
throw new error_1.InternalServerError({
message: `Failed to create record: ${error.message}`,
reason: 'node:sqlite insert failed',
component: 'NodeSqliteAdapter',
operation: 'create',
});
}
}
async update(id, update) {
try {
const { database, tableName } = this.config;
const existing = await this.findById(id);
if (!existing)
return null;
const now = new Date();
const updated = { ...existing, ...update };
const stmt = database.prepare(`UPDATE ${tableName} SET data = ?, updatedAt = ? WHERE id = ?`);
stmt.run(JSON.stringify(updated), now.toISOString(), id);
return {
...updated,
updatedAt: now,
};
}
catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
throw new error_1.InternalServerError({
message: `Failed to update record: ${error.message}`,
reason: 'node:sqlite update failed',
component: 'NodeSqliteAdapter',
operation: 'update',
metadata: { id },
});
}
}
async delete(id) {
try {
const { database, tableName } = this.config;
const existing = await this.findById(id);
if (!existing)
return null;
const stmt = database.prepare(`DELETE FROM ${tableName} WHERE id = ?`);
stmt.run(id);
return existing;
}
catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
throw new error_1.InternalServerError({
message: `Failed to delete record: ${error.message}`,
reason: 'node:sqlite delete failed',
component: 'NodeSqliteAdapter',
operation: 'delete',
metadata: { id },
});
}
}
async exists(id) {
try {
const { database, tableName } = this.config;
const stmt = database.prepare(`SELECT 1 FROM ${tableName} WHERE id = ? LIMIT 1`);
return stmt.get(id) != null;
}
catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
throw new error_1.InternalServerError({
message: `Failed to check if record exists: ${error.message}`,
reason: 'node:sqlite query failed',
component: 'NodeSqliteAdapter',
operation: 'exists',
metadata: { id },
});
}
}
}
exports.NodeSqliteAdapter = NodeSqliteAdapter;
//# sourceMappingURL=adapter.js.map