Update category files
CI/CD Pipeline / test (push) Failing after 1m46s
CI/CD Pipeline / deploy-internal (push) Has been skipped

This commit is contained in:
chris committed 2026-08-28 20:19:11 -04:00
1 parent 86b656ee5c
commit fea861f042
2 files changed
+83 -51

No files matched your search

+48 -20
View File
@@ -1,30 +1,58 @@
// structural interface to the test table
export interface CatetoryNode {
import { Pool } from 'pg';
export interface CategoryNode {
id: number;
name: string;
path: string; // ltree field
path: string;
}
export class CategoryRepository {
// simulate generating a SQL query targeting child paths
generateDescendantsQuery(parentPath: string): { sql: string; values: string[] } {
return {
sql: 'SELECT id, name, path FROM categories WHERE path <@ $1::ltree',
values: [parentPath]
};
// Inject the database connection pool
constructor(private db: Pool) { }
/**
* Finds all descendants of a given path (including the path itself).
* Uses the ltree operator <@ (is-descendant-of).
*/
async findAllDescendants(parentPath: string): Promise<CategoryNode[]> {
const query = `
SELECT id, name, path
FROM categories
WHERE path <@ $1::ltree
ORDER BY path ASC;
`;
const result = await this.db.query(query, [parentPath]);
return result.rows;
}
generateWildcardQuery(pattern: string): { sql: string, values: string[] } {
// Simulate genrating a SQL query using ltree wildcards
return {
sql: 'SELECT id, name, path FROM categories WHERE path ~ $1::lquery',
values: [pattern]
};
/**
* Finds only the direct children of a given path.
* Uses lquery pattern matching (parent.*) and matches the exact next depth level.
*/
async findDirectChildren(parentPath: string): Promise<CategoryNode[]> {
const query = `
SELECT id, name, path
FROM categories
WHERE path ~ $1::lquery
AND nlevel(path) = nlevel($2::ltree) + 1;
`;
const values = [`${parentPath}.*`, parentPath];
const result = await this.db.query(query, values);
return result.rows;
}
// helper utility to validate a string as an ltree value
isValidLtreePath(path: string): boolean {
const ltreeRegex = /^[A-Za-z0-9_]+(\.[A-Za-z0-9_]+)*$/;
return ltreeRegex.test(path);
/**
* Finds all ancestors of a path (useful for breadcrumbs).
* Uses the ltree operator @> (is-ancestor-of).
*/
async findAncestors(childPath: string): Promise<CategoryNode[]> {
const query = `
SELECT id, name, path
FROM categories
WHERE path @> $1::ltree
ORDER BY nlevel(path) ASC;
`;
const result = await this.db.query(query, [childPath]);
return result.rows;
}
}
}