Updated semantic, and added entities/relationship implementation

This commit is contained in:
Storme-bit
2026-04-04 20:24:08 -07:00
parent 7d3f083485
commit c5dc60ec8e
4 changed files with 243 additions and 5 deletions

View File

@@ -0,0 +1,111 @@
const {getDB} = require('../db');
/******* Entities ********/
// Upsert an entity - insert or update if (name, type) already exists
function upsertEntity(name, type, notes = null, metadata = null) {
const db = getDB();
const stmt = db.prepare(`
INSERT INTO entities (name, type, notes, metadata)
VALUES (?, ?, ?, ?)
ON CONFLICT(name, type) DO UPDATE SET
notes = excluded.notes,
metadata = excluded.metadata,
updated_at = unixepoch()
`);
const result = stmt.run(name, type, notes, metadata ? JSON.stringify(metadata) : null);
return getEntityByNameType(name, type);
}
// Get an entity by its ID
function getEntity(id) {
const db = getDB();
return parseEntity(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);
}
// Delete an entity by ID, cascades to delete relationships involving this entity
function deleteEntity(id) {
const db = getDB();
db.prepare(`DELETE FROM entities WHERE id = ?`).run(id);
}
/********* Relationships *********/
// Upsert a relationship, insert or ignore if (from_id, to_id, label) already exists
function upsertRelationship(fromId, toId, label, metadata = null){
const db = getDB();
const stmt = db.prepare(`
INSERT INTO relationships (from_id, to_id, label, metadata)
VALUES (?, ?, ?, ?)
ON CONFLICT(from_id, to_id, label) DO NOTHING
`);
const result = stmt.run(fromId, toId, label, metadata ?JSON.stringify(metadata) : null);
return getRelationship(fromId, toId, label);
}
// Retrieve a relationship by (from_id, to_id, label)
function getRelationship(fromId, toId, label) {
const db = getDB();
return parseRelationship(
db.prepare(`SELECT * FROM relationships WHERE from_id = ? AND to_id = ? AND label = ?`)
.get(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));
}
// 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);
}
// Delete a specific relationship by (from_id, to_id, label)
function deleteRelationship(fromid, toId, label) {
const db = getDB();
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,
getEntitiesByType,
getEntityByNameType,
deleteEntity,
upsertRelationship,
getRelationship,
getRelationshipsByEntity,
deleteRelationship
}