refactoring and clean up
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const {getDB} = require('../db');
|
||||
const { parseRow } = require ('@nexusai/shared')
|
||||
|
||||
/******* Entities ********/
|
||||
|
||||
@@ -21,13 +22,13 @@ function upsertEntity(name, type, notes = null, metadata = null) {
|
||||
// Get an entity by its ID
|
||||
function getEntity(id) {
|
||||
const db = getDB();
|
||||
return parseEntity(db.prepare(`SELECT * FROM entities WHERE id = ?`).get(id));
|
||||
return parseRow(db.prepare(`SELECT * FROM entities WHERE id = ?`).get(id));
|
||||
}
|
||||
|
||||
// Get all entities of a given type
|
||||
function getEntitiesByType(type) {
|
||||
const db = getDB();
|
||||
return db.prepare(`SELECT * FROM entities WHERE type = ? ORDER BY name`).all(type).map(parseEntity);
|
||||
return db.prepare(`SELECT * FROM entities WHERE type = ? ORDER BY name`).all(type).map(parseRow);
|
||||
}
|
||||
|
||||
// Delete an entity by ID, cascades to delete relationships involving this entity
|
||||
@@ -55,7 +56,7 @@ function upsertRelationship(fromId, toId, label, metadata = null){
|
||||
function getRelationship(fromId, toId, label) {
|
||||
const db = getDB();
|
||||
|
||||
return parseRelationship(
|
||||
return parseRow(
|
||||
db.prepare(`SELECT * FROM relationships WHERE from_id = ? AND to_id = ? AND label = ?`)
|
||||
.get(fromId, toId, label)
|
||||
);
|
||||
@@ -64,13 +65,13 @@ function getRelationship(fromId, toId, label) {
|
||||
// Retrieves an entity by its unique (name, type) combination
|
||||
function getEntityByNameType(name, type) {
|
||||
const db = getDB();
|
||||
return parseEntity(db.prepare(`SELECT * FROM entities WHERE name = ? AND type = ?`).get(name, type));
|
||||
return parseRow(db.prepare(`SELECT * FROM entities WHERE name = ? AND type = ?`).get(name, type));
|
||||
}
|
||||
|
||||
// Retrive all relationships originating from a given entity
|
||||
function getRelationshipsByEntity(entityId) {
|
||||
const db = getDB();
|
||||
return db.prepare(`SELECT * FROM relationships WHERE from_id = ?`).all(entityId).map(parseRelationship);
|
||||
return db.prepare(`SELECT * FROM relationships WHERE from_id = ?`).all(entityId).map(parseRow);
|
||||
}
|
||||
|
||||
// Delete a specific relationship by (from_id, to_id, label)
|
||||
@@ -80,24 +81,6 @@ function deleteRelationship(fromId, toId, label) {
|
||||
db.prepare(`DELETE FROM relationships WHERE from_id = ? AND to_id = ? AND label = ?`).run(fromId, toId, label);
|
||||
}
|
||||
|
||||
/*********** Parse Functions ***********/
|
||||
|
||||
function parseEntity(row) {
|
||||
if (!row) return null;
|
||||
return {
|
||||
...row,
|
||||
metadata: row.metadata ? JSON.parse(row.metadata) : null
|
||||
};
|
||||
}
|
||||
|
||||
function parseRelationship(row) {
|
||||
if (!row) return null;
|
||||
return {
|
||||
...row,
|
||||
metadata: row.metadata ? JSON.parse(row.metadata) : null
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
upsertEntity,
|
||||
getEntity,
|
||||
|
||||
Reference in New Issue
Block a user