@danielsimonjr/memoryjs 2.7.0 → 2.8.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/dist/cli/index.js +53 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/index.cjs +53 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +53 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -9134,6 +9134,27 @@ CREATE INDEX IF NOT EXISTS idx_entities_project_id ON entities(project_id);
|
|
|
9134
9134
|
CREATE INDEX IF NOT EXISTS idx_entities_content_hash ON entities(content_hash);
|
|
9135
9135
|
CREATE INDEX IF NOT EXISTS idx_entities_tags_gin ON entities USING GIN(tags);
|
|
9136
9136
|
|
|
9137
|
+
-- v2.8.0 \u2014 generated tsvector column for full-text search.
|
|
9138
|
+
-- name is weighted A (highest), observations B, tags C. PostgreSQL 12+
|
|
9139
|
+
-- supports GENERATED ALWAYS AS ... STORED; the DO block makes the column
|
|
9140
|
+
-- add idempotent on older + newer servers without requiring CREATE OR
|
|
9141
|
+
-- REPLACE semantics on ALTER.
|
|
9142
|
+
DO $$ BEGIN
|
|
9143
|
+
IF NOT EXISTS (
|
|
9144
|
+
SELECT 1 FROM information_schema.columns
|
|
9145
|
+
WHERE table_name = 'entities' AND column_name = 'fts_vector'
|
|
9146
|
+
) THEN
|
|
9147
|
+
ALTER TABLE entities ADD COLUMN fts_vector tsvector
|
|
9148
|
+
GENERATED ALWAYS AS (
|
|
9149
|
+
setweight(to_tsvector('english', name), 'A') ||
|
|
9150
|
+
setweight(to_tsvector('english', coalesce(array_to_string(observations, ' '), '')), 'B') ||
|
|
9151
|
+
setweight(to_tsvector('english', coalesce(array_to_string(tags, ' '), '')), 'C')
|
|
9152
|
+
) STORED;
|
|
9153
|
+
END IF;
|
|
9154
|
+
END $$;
|
|
9155
|
+
|
|
9156
|
+
CREATE INDEX IF NOT EXISTS idx_entities_fts_gin ON entities USING GIN(fts_vector);
|
|
9157
|
+
|
|
9137
9158
|
CREATE TABLE IF NOT EXISTS relations (
|
|
9138
9159
|
from_name TEXT NOT NULL,
|
|
9139
9160
|
to_name TEXT NOT NULL,
|
|
@@ -9453,6 +9474,38 @@ CREATE INDEX IF NOT EXISTS idx_relations_type ON relations(relation_type);
|
|
|
9453
9474
|
hasRelations(entityName) {
|
|
9454
9475
|
return (this.outgoingRelations.get(entityName)?.length ?? 0) + (this.incomingRelations.get(entityName)?.length ?? 0) > 0;
|
|
9455
9476
|
}
|
|
9477
|
+
// ==================== Full-text search ====================
|
|
9478
|
+
/**
|
|
9479
|
+
* tsvector-backed full-text search. Uses `plainto_tsquery` so the input is
|
|
9480
|
+
* free-form (no boolean operator syntax required); ranks via `ts_rank` on
|
|
9481
|
+
* the weighted `fts_vector` column (name × A, observations × B, tags × C).
|
|
9482
|
+
*
|
|
9483
|
+
* Results are ordered by descending score and capped at `options.limit`
|
|
9484
|
+
* (default 50). Empty / whitespace-only queries return `[]` without
|
|
9485
|
+
* issuing a SQL call.
|
|
9486
|
+
*
|
|
9487
|
+
* @example
|
|
9488
|
+
* ```typescript
|
|
9489
|
+
* const matches = await storage.fullTextSearch('authentication flow', { limit: 10 });
|
|
9490
|
+
* // → [{ name: 'AuthService', score: 0.62 }, ...]
|
|
9491
|
+
* ```
|
|
9492
|
+
*/
|
|
9493
|
+
async fullTextSearch(query, options = {}) {
|
|
9494
|
+
const trimmed = query.trim();
|
|
9495
|
+
if (trimmed.length === 0) return [];
|
|
9496
|
+
await this.initSchema();
|
|
9497
|
+
const pool = await this.getPool();
|
|
9498
|
+
const limit = options.limit ?? 50;
|
|
9499
|
+
const res = await pool.query(
|
|
9500
|
+
`SELECT name, ts_rank(fts_vector, plainto_tsquery('english', $1))::float AS score
|
|
9501
|
+
FROM entities
|
|
9502
|
+
WHERE fts_vector @@ plainto_tsquery('english', $1)
|
|
9503
|
+
ORDER BY score DESC
|
|
9504
|
+
LIMIT $2`,
|
|
9505
|
+
[trimmed, limit]
|
|
9506
|
+
);
|
|
9507
|
+
return res.rows.map((r) => ({ name: r.name, score: Number(r.score) }));
|
|
9508
|
+
}
|
|
9456
9509
|
// ==================== Utility ====================
|
|
9457
9510
|
getFilePath() {
|
|
9458
9511
|
return this.connectionString;
|