@danielsimonjr/memoryjs 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1048 -1152
- package/dist/cli/index.js +434 -1
- package/dist/cli/index.js.map +1 -1
- package/dist/index.cjs +429 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +429 -1
- package/dist/index.js.map +1 -1
- package/package.json +9 -1
package/dist/cli/index.js
CHANGED
|
@@ -9037,6 +9037,435 @@ var init_SQLiteStorage = __esm({
|
|
|
9037
9037
|
}
|
|
9038
9038
|
});
|
|
9039
9039
|
|
|
9040
|
+
// src/core/PostgreSQLStorage.ts
|
|
9041
|
+
var ENTITY_COLUMNS, FIRST_CLASS_ENTITY_KEYS, SCHEMA_DDL, PostgreSQLStorage;
|
|
9042
|
+
var init_PostgreSQLStorage = __esm({
|
|
9043
|
+
"src/core/PostgreSQLStorage.ts"() {
|
|
9044
|
+
"use strict";
|
|
9045
|
+
init_esm_shims();
|
|
9046
|
+
init_logger();
|
|
9047
|
+
ENTITY_COLUMNS = [
|
|
9048
|
+
"name",
|
|
9049
|
+
"entity_type",
|
|
9050
|
+
"observations",
|
|
9051
|
+
"parent_id",
|
|
9052
|
+
"tags",
|
|
9053
|
+
"importance",
|
|
9054
|
+
"created_at",
|
|
9055
|
+
"last_modified",
|
|
9056
|
+
"ttl",
|
|
9057
|
+
"confidence",
|
|
9058
|
+
"project_id",
|
|
9059
|
+
"version",
|
|
9060
|
+
"parent_entity_name",
|
|
9061
|
+
"root_entity_name",
|
|
9062
|
+
"is_latest",
|
|
9063
|
+
"superseded_by",
|
|
9064
|
+
"content_hash",
|
|
9065
|
+
"valid_from",
|
|
9066
|
+
"valid_until",
|
|
9067
|
+
"observation_meta",
|
|
9068
|
+
"lifecycle_status",
|
|
9069
|
+
"extra"
|
|
9070
|
+
];
|
|
9071
|
+
FIRST_CLASS_ENTITY_KEYS = /* @__PURE__ */ new Set([
|
|
9072
|
+
"name",
|
|
9073
|
+
"entityType",
|
|
9074
|
+
"observations",
|
|
9075
|
+
"parentId",
|
|
9076
|
+
"tags",
|
|
9077
|
+
"importance",
|
|
9078
|
+
"createdAt",
|
|
9079
|
+
"lastModified",
|
|
9080
|
+
"ttl",
|
|
9081
|
+
"confidence",
|
|
9082
|
+
"projectId",
|
|
9083
|
+
"version",
|
|
9084
|
+
"parentEntityName",
|
|
9085
|
+
"rootEntityName",
|
|
9086
|
+
"isLatest",
|
|
9087
|
+
"supersededBy",
|
|
9088
|
+
"contentHash",
|
|
9089
|
+
"validFrom",
|
|
9090
|
+
"validUntil",
|
|
9091
|
+
"observationMeta",
|
|
9092
|
+
"lifecycleStatus"
|
|
9093
|
+
]);
|
|
9094
|
+
SCHEMA_DDL = `
|
|
9095
|
+
CREATE TABLE IF NOT EXISTS entities (
|
|
9096
|
+
name TEXT PRIMARY KEY,
|
|
9097
|
+
entity_type TEXT NOT NULL,
|
|
9098
|
+
observations TEXT[] NOT NULL DEFAULT '{}',
|
|
9099
|
+
parent_id TEXT,
|
|
9100
|
+
tags TEXT[],
|
|
9101
|
+
importance NUMERIC(4, 2),
|
|
9102
|
+
created_at TIMESTAMPTZ DEFAULT NOW(),
|
|
9103
|
+
last_modified TIMESTAMPTZ DEFAULT NOW(),
|
|
9104
|
+
ttl INTEGER,
|
|
9105
|
+
confidence NUMERIC(4, 3),
|
|
9106
|
+
project_id TEXT,
|
|
9107
|
+
version INTEGER,
|
|
9108
|
+
parent_entity_name TEXT,
|
|
9109
|
+
root_entity_name TEXT,
|
|
9110
|
+
is_latest BOOLEAN,
|
|
9111
|
+
superseded_by TEXT,
|
|
9112
|
+
content_hash TEXT,
|
|
9113
|
+
valid_from TIMESTAMPTZ,
|
|
9114
|
+
valid_until TIMESTAMPTZ,
|
|
9115
|
+
observation_meta JSONB,
|
|
9116
|
+
lifecycle_status TEXT,
|
|
9117
|
+
extra JSONB
|
|
9118
|
+
);
|
|
9119
|
+
|
|
9120
|
+
CREATE INDEX IF NOT EXISTS idx_entities_entity_type ON entities(entity_type);
|
|
9121
|
+
CREATE INDEX IF NOT EXISTS idx_entities_project_id ON entities(project_id);
|
|
9122
|
+
CREATE INDEX IF NOT EXISTS idx_entities_content_hash ON entities(content_hash);
|
|
9123
|
+
CREATE INDEX IF NOT EXISTS idx_entities_tags_gin ON entities USING GIN(tags);
|
|
9124
|
+
|
|
9125
|
+
CREATE TABLE IF NOT EXISTS relations (
|
|
9126
|
+
from_name TEXT NOT NULL,
|
|
9127
|
+
to_name TEXT NOT NULL,
|
|
9128
|
+
relation_type TEXT NOT NULL,
|
|
9129
|
+
metadata JSONB,
|
|
9130
|
+
created_at TIMESTAMPTZ DEFAULT NOW(),
|
|
9131
|
+
valid_from TIMESTAMPTZ,
|
|
9132
|
+
valid_until TIMESTAMPTZ,
|
|
9133
|
+
PRIMARY KEY (from_name, to_name, relation_type)
|
|
9134
|
+
);
|
|
9135
|
+
|
|
9136
|
+
CREATE INDEX IF NOT EXISTS idx_relations_to ON relations(to_name);
|
|
9137
|
+
CREATE INDEX IF NOT EXISTS idx_relations_type ON relations(relation_type);
|
|
9138
|
+
`;
|
|
9139
|
+
PostgreSQLStorage = class {
|
|
9140
|
+
connectionString;
|
|
9141
|
+
pool = null;
|
|
9142
|
+
cache = null;
|
|
9143
|
+
nameIndex = /* @__PURE__ */ new Map();
|
|
9144
|
+
outgoingRelations = /* @__PURE__ */ new Map();
|
|
9145
|
+
incomingRelations = /* @__PURE__ */ new Map();
|
|
9146
|
+
pendingAppends = 0;
|
|
9147
|
+
schemaInitPromise = null;
|
|
9148
|
+
constructor(connectionString) {
|
|
9149
|
+
this.connectionString = connectionString;
|
|
9150
|
+
}
|
|
9151
|
+
// ==================== Connection management ====================
|
|
9152
|
+
/**
|
|
9153
|
+
* Get or lazily-construct the `pg.Pool`. Throws a friendly error if
|
|
9154
|
+
* the `pg` package isn't installed.
|
|
9155
|
+
*/
|
|
9156
|
+
async getPool() {
|
|
9157
|
+
if (this.pool) return this.pool;
|
|
9158
|
+
let pgModule;
|
|
9159
|
+
try {
|
|
9160
|
+
pgModule = await import("pg");
|
|
9161
|
+
} catch {
|
|
9162
|
+
throw new Error(
|
|
9163
|
+
"The 'pg' package is required for the PostgreSQL backend but is not installed. Run: npm install pg @types/pg"
|
|
9164
|
+
);
|
|
9165
|
+
}
|
|
9166
|
+
this.pool = new pgModule.Pool({ connectionString: this.connectionString });
|
|
9167
|
+
return this.pool;
|
|
9168
|
+
}
|
|
9169
|
+
/**
|
|
9170
|
+
* Run schema DDL idempotently. Called from `ensureLoaded`.
|
|
9171
|
+
*/
|
|
9172
|
+
async initSchema() {
|
|
9173
|
+
if (this.schemaInitPromise) return this.schemaInitPromise;
|
|
9174
|
+
this.schemaInitPromise = (async () => {
|
|
9175
|
+
const pool = await this.getPool();
|
|
9176
|
+
await pool.query(SCHEMA_DDL);
|
|
9177
|
+
})();
|
|
9178
|
+
return this.schemaInitPromise;
|
|
9179
|
+
}
|
|
9180
|
+
// ==================== Row ⇄ Entity / Relation mapping ====================
|
|
9181
|
+
entityToRow(entity) {
|
|
9182
|
+
const extra = {};
|
|
9183
|
+
for (const key of Object.keys(entity)) {
|
|
9184
|
+
if (!FIRST_CLASS_ENTITY_KEYS.has(key)) {
|
|
9185
|
+
extra[key] = entity[key];
|
|
9186
|
+
}
|
|
9187
|
+
}
|
|
9188
|
+
return {
|
|
9189
|
+
name: entity.name,
|
|
9190
|
+
entity_type: entity.entityType,
|
|
9191
|
+
observations: entity.observations,
|
|
9192
|
+
parent_id: entity.parentId ?? null,
|
|
9193
|
+
tags: entity.tags ?? null,
|
|
9194
|
+
importance: entity.importance ?? null,
|
|
9195
|
+
created_at: entity.createdAt ?? null,
|
|
9196
|
+
last_modified: entity.lastModified ?? null,
|
|
9197
|
+
ttl: entity.ttl ?? null,
|
|
9198
|
+
confidence: entity.confidence ?? null,
|
|
9199
|
+
project_id: entity.projectId ?? null,
|
|
9200
|
+
version: entity.version ?? null,
|
|
9201
|
+
parent_entity_name: entity.parentEntityName ?? null,
|
|
9202
|
+
root_entity_name: entity.rootEntityName ?? null,
|
|
9203
|
+
is_latest: entity.isLatest ?? null,
|
|
9204
|
+
superseded_by: entity.supersededBy ?? null,
|
|
9205
|
+
content_hash: entity.contentHash ?? null,
|
|
9206
|
+
valid_from: entity.validFrom ?? null,
|
|
9207
|
+
valid_until: entity.validUntil ?? null,
|
|
9208
|
+
observation_meta: entity.observationMeta ?? null,
|
|
9209
|
+
lifecycle_status: entity.lifecycleStatus ?? null,
|
|
9210
|
+
extra: Object.keys(extra).length > 0 ? extra : null
|
|
9211
|
+
};
|
|
9212
|
+
}
|
|
9213
|
+
rowToEntity(row) {
|
|
9214
|
+
const entity = {
|
|
9215
|
+
name: String(row.name),
|
|
9216
|
+
entityType: String(row.entity_type),
|
|
9217
|
+
observations: Array.isArray(row.observations) ? row.observations : []
|
|
9218
|
+
};
|
|
9219
|
+
if (row.parent_id != null) entity.parentId = String(row.parent_id);
|
|
9220
|
+
if (Array.isArray(row.tags)) entity.tags = row.tags;
|
|
9221
|
+
if (row.importance != null) entity.importance = Number(row.importance);
|
|
9222
|
+
if (row.created_at != null) entity.createdAt = new Date(String(row.created_at)).toISOString();
|
|
9223
|
+
if (row.last_modified != null) entity.lastModified = new Date(String(row.last_modified)).toISOString();
|
|
9224
|
+
if (row.ttl != null) entity.ttl = Number(row.ttl);
|
|
9225
|
+
if (row.confidence != null) entity.confidence = Number(row.confidence);
|
|
9226
|
+
if (row.project_id != null) entity.projectId = String(row.project_id);
|
|
9227
|
+
if (row.version != null) entity.version = Number(row.version);
|
|
9228
|
+
if (row.parent_entity_name != null) entity.parentEntityName = String(row.parent_entity_name);
|
|
9229
|
+
if (row.root_entity_name != null) entity.rootEntityName = String(row.root_entity_name);
|
|
9230
|
+
if (row.is_latest != null) entity.isLatest = Boolean(row.is_latest);
|
|
9231
|
+
if (row.superseded_by != null) entity.supersededBy = String(row.superseded_by);
|
|
9232
|
+
if (row.content_hash != null) entity.contentHash = String(row.content_hash);
|
|
9233
|
+
if (row.valid_from != null) entity.validFrom = new Date(String(row.valid_from)).toISOString();
|
|
9234
|
+
if (row.valid_until != null) entity.validUntil = new Date(String(row.valid_until)).toISOString();
|
|
9235
|
+
if (row.observation_meta != null) {
|
|
9236
|
+
entity.observationMeta = row.observation_meta;
|
|
9237
|
+
}
|
|
9238
|
+
if (row.lifecycle_status != null) {
|
|
9239
|
+
entity.lifecycleStatus = String(row.lifecycle_status);
|
|
9240
|
+
}
|
|
9241
|
+
if (row.extra != null && typeof row.extra === "object") {
|
|
9242
|
+
Object.assign(entity, row.extra);
|
|
9243
|
+
}
|
|
9244
|
+
return entity;
|
|
9245
|
+
}
|
|
9246
|
+
rowToRelation(row) {
|
|
9247
|
+
const relation = {
|
|
9248
|
+
from: String(row.from_name),
|
|
9249
|
+
to: String(row.to_name),
|
|
9250
|
+
relationType: String(row.relation_type)
|
|
9251
|
+
};
|
|
9252
|
+
return relation;
|
|
9253
|
+
}
|
|
9254
|
+
// ==================== Cache management ====================
|
|
9255
|
+
rebuildIndexes(graph) {
|
|
9256
|
+
this.nameIndex.clear();
|
|
9257
|
+
this.outgoingRelations.clear();
|
|
9258
|
+
this.incomingRelations.clear();
|
|
9259
|
+
for (const e of graph.entities) this.nameIndex.set(e.name, e);
|
|
9260
|
+
for (const r of graph.relations) {
|
|
9261
|
+
const outArr = this.outgoingRelations.get(r.from) ?? [];
|
|
9262
|
+
outArr.push(r);
|
|
9263
|
+
this.outgoingRelations.set(r.from, outArr);
|
|
9264
|
+
const inArr = this.incomingRelations.get(r.to) ?? [];
|
|
9265
|
+
inArr.push(r);
|
|
9266
|
+
this.incomingRelations.set(r.to, inArr);
|
|
9267
|
+
}
|
|
9268
|
+
}
|
|
9269
|
+
// ==================== IGraphStorage — read ====================
|
|
9270
|
+
async loadGraph() {
|
|
9271
|
+
await this.ensureLoaded();
|
|
9272
|
+
return this.cache;
|
|
9273
|
+
}
|
|
9274
|
+
async getGraphForMutation() {
|
|
9275
|
+
await this.ensureLoaded();
|
|
9276
|
+
const cache = this.cache;
|
|
9277
|
+
return {
|
|
9278
|
+
entities: cache.entities.map((e) => ({
|
|
9279
|
+
...e,
|
|
9280
|
+
observations: [...e.observations],
|
|
9281
|
+
tags: e.tags ? [...e.tags] : void 0
|
|
9282
|
+
})),
|
|
9283
|
+
relations: cache.relations.map((r) => ({ ...r }))
|
|
9284
|
+
};
|
|
9285
|
+
}
|
|
9286
|
+
async ensureLoaded() {
|
|
9287
|
+
if (this.cache) return;
|
|
9288
|
+
await this.initSchema();
|
|
9289
|
+
const pool = await this.getPool();
|
|
9290
|
+
const [eRes, rRes] = await Promise.all([
|
|
9291
|
+
pool.query("SELECT * FROM entities"),
|
|
9292
|
+
pool.query("SELECT * FROM relations")
|
|
9293
|
+
]);
|
|
9294
|
+
const graph = {
|
|
9295
|
+
entities: eRes.rows.map((r) => this.rowToEntity(r)),
|
|
9296
|
+
relations: rRes.rows.map((r) => this.rowToRelation(r))
|
|
9297
|
+
};
|
|
9298
|
+
this.cache = graph;
|
|
9299
|
+
this.rebuildIndexes(graph);
|
|
9300
|
+
}
|
|
9301
|
+
get cachedGraph() {
|
|
9302
|
+
return this.cache;
|
|
9303
|
+
}
|
|
9304
|
+
// ==================== IGraphStorage — write ====================
|
|
9305
|
+
async saveGraph(graph) {
|
|
9306
|
+
await this.initSchema();
|
|
9307
|
+
const pool = await this.getPool();
|
|
9308
|
+
await pool.query("TRUNCATE entities, relations");
|
|
9309
|
+
for (const entity of graph.entities) {
|
|
9310
|
+
await this.insertEntityRow(pool, entity);
|
|
9311
|
+
}
|
|
9312
|
+
for (const relation of graph.relations) {
|
|
9313
|
+
await this.insertRelationRow(pool, relation);
|
|
9314
|
+
}
|
|
9315
|
+
this.cache = {
|
|
9316
|
+
entities: graph.entities.map((e) => ({ ...e })),
|
|
9317
|
+
relations: graph.relations.map((r) => ({ ...r }))
|
|
9318
|
+
};
|
|
9319
|
+
this.rebuildIndexes(this.cache);
|
|
9320
|
+
this.pendingAppends = 0;
|
|
9321
|
+
}
|
|
9322
|
+
async insertEntityRow(pool, entity) {
|
|
9323
|
+
const row = this.entityToRow(entity);
|
|
9324
|
+
const cols = ENTITY_COLUMNS.join(", ");
|
|
9325
|
+
const placeholders = ENTITY_COLUMNS.map((_, i) => `$${i + 1}`).join(", ");
|
|
9326
|
+
const values = ENTITY_COLUMNS.map((c) => row[c]);
|
|
9327
|
+
await pool.query(
|
|
9328
|
+
`INSERT INTO entities (${cols}) VALUES (${placeholders})
|
|
9329
|
+
ON CONFLICT (name) DO UPDATE SET ${ENTITY_COLUMNS.filter((c) => c !== "name").map((c) => `${c} = EXCLUDED.${c}`).join(", ")}`,
|
|
9330
|
+
values
|
|
9331
|
+
);
|
|
9332
|
+
}
|
|
9333
|
+
async insertRelationRow(pool, relation) {
|
|
9334
|
+
await pool.query(
|
|
9335
|
+
`INSERT INTO relations (from_name, to_name, relation_type)
|
|
9336
|
+
VALUES ($1, $2, $3) ON CONFLICT DO NOTHING`,
|
|
9337
|
+
[relation.from, relation.to, relation.relationType]
|
|
9338
|
+
);
|
|
9339
|
+
}
|
|
9340
|
+
async appendEntity(entity) {
|
|
9341
|
+
await this.ensureLoaded();
|
|
9342
|
+
const pool = await this.getPool();
|
|
9343
|
+
await this.insertEntityRow(pool, entity);
|
|
9344
|
+
if (!this.cache) return;
|
|
9345
|
+
const existingIdx = this.cache.entities.findIndex((e) => e.name === entity.name);
|
|
9346
|
+
if (existingIdx >= 0) this.cache.entities[existingIdx] = entity;
|
|
9347
|
+
else this.cache.entities.push(entity);
|
|
9348
|
+
this.nameIndex.set(entity.name, entity);
|
|
9349
|
+
this.pendingAppends += 1;
|
|
9350
|
+
}
|
|
9351
|
+
async appendRelation(relation) {
|
|
9352
|
+
await this.ensureLoaded();
|
|
9353
|
+
const pool = await this.getPool();
|
|
9354
|
+
await this.insertRelationRow(pool, relation);
|
|
9355
|
+
if (!this.cache) return;
|
|
9356
|
+
const duplicate = this.cache.relations.some(
|
|
9357
|
+
(r) => r.from === relation.from && r.to === relation.to && r.relationType === relation.relationType
|
|
9358
|
+
);
|
|
9359
|
+
if (!duplicate) {
|
|
9360
|
+
this.cache.relations.push(relation);
|
|
9361
|
+
const outArr = this.outgoingRelations.get(relation.from) ?? [];
|
|
9362
|
+
outArr.push(relation);
|
|
9363
|
+
this.outgoingRelations.set(relation.from, outArr);
|
|
9364
|
+
const inArr = this.incomingRelations.get(relation.to) ?? [];
|
|
9365
|
+
inArr.push(relation);
|
|
9366
|
+
this.incomingRelations.set(relation.to, inArr);
|
|
9367
|
+
}
|
|
9368
|
+
this.pendingAppends += 1;
|
|
9369
|
+
}
|
|
9370
|
+
async updateEntity(entityName, updates) {
|
|
9371
|
+
await this.ensureLoaded();
|
|
9372
|
+
const existing = this.nameIndex.get(entityName);
|
|
9373
|
+
if (!existing) return false;
|
|
9374
|
+
const merged = {
|
|
9375
|
+
...existing,
|
|
9376
|
+
...updates,
|
|
9377
|
+
name: entityName,
|
|
9378
|
+
lastModified: updates.lastModified ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
9379
|
+
};
|
|
9380
|
+
const pool = await this.getPool();
|
|
9381
|
+
await this.insertEntityRow(pool, merged);
|
|
9382
|
+
if (this.cache) {
|
|
9383
|
+
const idx = this.cache.entities.findIndex((e) => e.name === entityName);
|
|
9384
|
+
if (idx >= 0) this.cache.entities[idx] = merged;
|
|
9385
|
+
}
|
|
9386
|
+
this.nameIndex.set(entityName, merged);
|
|
9387
|
+
this.pendingAppends += 1;
|
|
9388
|
+
return true;
|
|
9389
|
+
}
|
|
9390
|
+
async compact() {
|
|
9391
|
+
if (this.cache) await this.saveGraph(this.cache);
|
|
9392
|
+
}
|
|
9393
|
+
clearCache() {
|
|
9394
|
+
this.cache = null;
|
|
9395
|
+
this.nameIndex.clear();
|
|
9396
|
+
this.outgoingRelations.clear();
|
|
9397
|
+
this.incomingRelations.clear();
|
|
9398
|
+
this.pendingAppends = 0;
|
|
9399
|
+
}
|
|
9400
|
+
// ==================== IGraphStorage — sync getters ====================
|
|
9401
|
+
getEntityByName(name) {
|
|
9402
|
+
return this.nameIndex.get(name);
|
|
9403
|
+
}
|
|
9404
|
+
hasEntity(name) {
|
|
9405
|
+
return this.nameIndex.has(name);
|
|
9406
|
+
}
|
|
9407
|
+
getEntitiesByType(entityType) {
|
|
9408
|
+
const result = [];
|
|
9409
|
+
for (const entity of this.nameIndex.values()) {
|
|
9410
|
+
if (entity.entityType === entityType) result.push(entity);
|
|
9411
|
+
}
|
|
9412
|
+
return result;
|
|
9413
|
+
}
|
|
9414
|
+
getEntityTypes() {
|
|
9415
|
+
const types = /* @__PURE__ */ new Set();
|
|
9416
|
+
for (const entity of this.nameIndex.values()) types.add(entity.entityType);
|
|
9417
|
+
return Array.from(types);
|
|
9418
|
+
}
|
|
9419
|
+
getLowercased(entityName) {
|
|
9420
|
+
const e = this.nameIndex.get(entityName);
|
|
9421
|
+
if (!e) return void 0;
|
|
9422
|
+
return {
|
|
9423
|
+
name: e.name.toLowerCase(),
|
|
9424
|
+
entityType: e.entityType.toLowerCase(),
|
|
9425
|
+
observations: e.observations.map((o) => o.toLowerCase()),
|
|
9426
|
+
tags: e.tags?.map((t) => t.toLowerCase()) ?? []
|
|
9427
|
+
};
|
|
9428
|
+
}
|
|
9429
|
+
getRelationsFrom(entityName) {
|
|
9430
|
+
return this.outgoingRelations.get(entityName) ?? [];
|
|
9431
|
+
}
|
|
9432
|
+
getRelationsTo(entityName) {
|
|
9433
|
+
return this.incomingRelations.get(entityName) ?? [];
|
|
9434
|
+
}
|
|
9435
|
+
getRelationsFor(entityName) {
|
|
9436
|
+
return [
|
|
9437
|
+
...this.outgoingRelations.get(entityName) ?? [],
|
|
9438
|
+
...this.incomingRelations.get(entityName) ?? []
|
|
9439
|
+
];
|
|
9440
|
+
}
|
|
9441
|
+
hasRelations(entityName) {
|
|
9442
|
+
return (this.outgoingRelations.get(entityName)?.length ?? 0) + (this.incomingRelations.get(entityName)?.length ?? 0) > 0;
|
|
9443
|
+
}
|
|
9444
|
+
// ==================== Utility ====================
|
|
9445
|
+
getFilePath() {
|
|
9446
|
+
return this.connectionString;
|
|
9447
|
+
}
|
|
9448
|
+
getPendingAppends() {
|
|
9449
|
+
return this.pendingAppends;
|
|
9450
|
+
}
|
|
9451
|
+
/**
|
|
9452
|
+
* Close the underlying `pg.Pool`. Call from process-shutdown handlers to
|
|
9453
|
+
* avoid keeping the event loop alive on idle connections.
|
|
9454
|
+
*/
|
|
9455
|
+
async close() {
|
|
9456
|
+
if (this.pool) {
|
|
9457
|
+
try {
|
|
9458
|
+
await this.pool.end();
|
|
9459
|
+
} catch (e) {
|
|
9460
|
+
logger.warn("PostgreSQLStorage.close: pool.end failed:", e);
|
|
9461
|
+
}
|
|
9462
|
+
this.pool = null;
|
|
9463
|
+
}
|
|
9464
|
+
}
|
|
9465
|
+
};
|
|
9466
|
+
}
|
|
9467
|
+
});
|
|
9468
|
+
|
|
9040
9469
|
// src/core/StorageFactory.ts
|
|
9041
9470
|
function createStorage(config) {
|
|
9042
9471
|
const storageType = process.env.MEMORY_STORAGE_TYPE || config.type || DEFAULT_STORAGE_TYPE;
|
|
@@ -9045,9 +9474,12 @@ function createStorage(config) {
|
|
|
9045
9474
|
return new GraphStorage(config.path);
|
|
9046
9475
|
case "sqlite":
|
|
9047
9476
|
return new SQLiteStorage(config.path);
|
|
9477
|
+
case "postgres":
|
|
9478
|
+
case "postgresql":
|
|
9479
|
+
return new PostgreSQLStorage(config.path);
|
|
9048
9480
|
default:
|
|
9049
9481
|
throw new Error(
|
|
9050
|
-
`Unknown storage type: ${storageType}. Supported types: jsonl, sqlite`
|
|
9482
|
+
`Unknown storage type: ${storageType}. Supported types: jsonl, sqlite, postgres`
|
|
9051
9483
|
);
|
|
9052
9484
|
}
|
|
9053
9485
|
}
|
|
@@ -9062,6 +9494,7 @@ var init_StorageFactory = __esm({
|
|
|
9062
9494
|
init_esm_shims();
|
|
9063
9495
|
init_GraphStorage();
|
|
9064
9496
|
init_SQLiteStorage();
|
|
9497
|
+
init_PostgreSQLStorage();
|
|
9065
9498
|
DEFAULT_STORAGE_TYPE = "jsonl";
|
|
9066
9499
|
}
|
|
9067
9500
|
});
|