Merged in feature/textExtractionsPart3 (pull request #195)

enrich doc responses

* enrich doc responses
This commit is contained in:
Jay Brown
2025-12-11 14:25:51 +00:00
parent a94637ab13
commit 8c218f162b
22 changed files with 1797 additions and 363 deletions
+12
View File
@@ -76,3 +76,15 @@ SELECT
FROM parts p
RIGHT JOIN max_part mp ON p.part = mp.max_part_num
GROUP BY p.part;
-- name: GetDocumentEnriched :one
-- Retrieves a document with extended metadata including folder and filename
SELECT
d.id,
d.clientId,
d.hash,
d.folderId,
d.filename,
d.originalPath
FROM documents d
WHERE d.id = $1;
@@ -250,3 +250,18 @@ WHERE id = $1;
SELECT * FROM documentFieldExtractions
WHERE documentId = $1
ORDER BY createdAt DESC;
-- name: HasFieldExtraction :one
-- Checks if a field extraction exists for a document
SELECT EXISTS(
SELECT 1 FROM documentFieldExtractionVersions
WHERE documentId = $1
) as has_extraction;
-- name: HasFieldExtractionBatch :many
-- Checks if field extractions exist for multiple documents (for folder listing)
-- Returns one row per input document ID indicating whether extraction exists
SELECT
t.documentId,
EXISTS(SELECT 1 FROM documentFieldExtractionVersions dfev WHERE dfev.documentId = t.documentId) as has_extraction
FROM unnest(@documentIds::uuid[]) as t(documentId);
+12
View File
@@ -87,3 +87,15 @@ INSERT INTO folders (path, parentId, clientId, createdBy)
VALUES ($1, $2, $3, $4)
ON CONFLICT (clientId, path) DO UPDATE SET path = EXCLUDED.path
RETURNING *;
-- name: GetDocumentsByFolderEnriched :many
-- Gets documents in a folder with all enriched metadata fields
SELECT
d.id,
d.hash,
d.folderId,
d.filename,
d.originalPath
FROM documents d
WHERE d.folderId = $1
ORDER BY d.id;
@@ -94,6 +94,52 @@ func (q *Queries) CreateDocument(ctx context.Context, arg *CreateDocumentParams)
return id, err
}
const getDocumentEnriched = `-- name: GetDocumentEnriched :one
SELECT
d.id,
d.clientId,
d.hash,
d.folderId,
d.filename,
d.originalPath
FROM documents d
WHERE d.id = $1
`
type GetDocumentEnrichedRow struct {
ID uuid.UUID `db:"id"`
Clientid string `db:"clientid"`
Hash string `db:"hash"`
Folderid *uuid.UUID `db:"folderid"`
Filename *string `db:"filename"`
Originalpath *string `db:"originalpath"`
}
// Retrieves a document with extended metadata including folder and filename
//
// SELECT
// d.id,
// d.clientId,
// d.hash,
// d.folderId,
// d.filename,
// d.originalPath
// FROM documents d
// WHERE d.id = $1
func (q *Queries) GetDocumentEnriched(ctx context.Context, id uuid.UUID) (*GetDocumentEnrichedRow, error) {
row := q.db.QueryRow(ctx, getDocumentEnriched, id)
var i GetDocumentEnrichedRow
err := row.Scan(
&i.ID,
&i.Clientid,
&i.Hash,
&i.Folderid,
&i.Filename,
&i.Originalpath,
)
return &i, err
}
const getDocumentEntry = `-- name: GetDocumentEntry :one
SELECT documentId, bucket, key FROM documentEntries WHERE documentId = $1 ORDER BY id DESC LIMIT 1
`
@@ -1196,6 +1196,65 @@ func (q *Queries) GetMaxFieldExtractionVersion(ctx context.Context, documentid u
return max_version, err
}
const hasFieldExtraction = `-- name: HasFieldExtraction :one
SELECT EXISTS(
SELECT 1 FROM documentFieldExtractionVersions
WHERE documentId = $1
) as has_extraction
`
// Checks if a field extraction exists for a document
//
// SELECT EXISTS(
// SELECT 1 FROM documentFieldExtractionVersions
// WHERE documentId = $1
// ) as has_extraction
func (q *Queries) HasFieldExtraction(ctx context.Context, documentid uuid.UUID) (bool, error) {
row := q.db.QueryRow(ctx, hasFieldExtraction, documentid)
var has_extraction bool
err := row.Scan(&has_extraction)
return has_extraction, err
}
const hasFieldExtractionBatch = `-- name: HasFieldExtractionBatch :many
SELECT
t.documentId,
EXISTS(SELECT 1 FROM documentFieldExtractionVersions dfev WHERE dfev.documentId = t.documentId) as has_extraction
FROM unnest($1::uuid[]) as t(documentId)
`
type HasFieldExtractionBatchRow struct {
Documentid *uuid.UUID `db:"documentid"`
HasExtraction bool `db:"has_extraction"`
}
// Checks if field extractions exist for multiple documents (for folder listing)
// Returns one row per input document ID indicating whether extraction exists
//
// SELECT
// t.documentId,
// EXISTS(SELECT 1 FROM documentFieldExtractionVersions dfev WHERE dfev.documentId = t.documentId) as has_extraction
// FROM unnest($1::uuid[]) as t(documentId)
func (q *Queries) HasFieldExtractionBatch(ctx context.Context, documentids []uuid.UUID) ([]*HasFieldExtractionBatchRow, error) {
rows, err := q.db.Query(ctx, hasFieldExtractionBatch, documentids)
if err != nil {
return nil, err
}
defer rows.Close()
items := []*HasFieldExtractionBatchRow{}
for rows.Next() {
var i HasFieldExtractionBatchRow
if err := rows.Scan(&i.Documentid, &i.HasExtraction); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const lockFieldExtractionVersionsForDocument = `-- name: LockFieldExtractionVersionsForDocument :many
SELECT id, version FROM documentFieldExtractionVersions
WHERE documentId = $1
@@ -127,6 +127,63 @@ func (q *Queries) GetDocumentsByFolder(ctx context.Context, folderid *uuid.UUID)
return items, nil
}
const getDocumentsByFolderEnriched = `-- name: GetDocumentsByFolderEnriched :many
SELECT
d.id,
d.hash,
d.folderId,
d.filename,
d.originalPath
FROM documents d
WHERE d.folderId = $1
ORDER BY d.id
`
type GetDocumentsByFolderEnrichedRow struct {
ID uuid.UUID `db:"id"`
Hash string `db:"hash"`
Folderid *uuid.UUID `db:"folderid"`
Filename *string `db:"filename"`
Originalpath *string `db:"originalpath"`
}
// Gets documents in a folder with all enriched metadata fields
//
// SELECT
// d.id,
// d.hash,
// d.folderId,
// d.filename,
// d.originalPath
// FROM documents d
// WHERE d.folderId = $1
// ORDER BY d.id
func (q *Queries) GetDocumentsByFolderEnriched(ctx context.Context, folderid *uuid.UUID) ([]*GetDocumentsByFolderEnrichedRow, error) {
rows, err := q.db.Query(ctx, getDocumentsByFolderEnriched, folderid)
if err != nil {
return nil, err
}
defer rows.Close()
items := []*GetDocumentsByFolderEnrichedRow{}
for rows.Next() {
var i GetDocumentsByFolderEnrichedRow
if err := rows.Scan(
&i.ID,
&i.Hash,
&i.Folderid,
&i.Filename,
&i.Originalpath,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getFolderByID = `-- name: GetFolderByID :one
SELECT id, path, parentid, clientid, createdat, createdby FROM folders
WHERE id = $1