-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
couchbase.ts
94 lines (81 loc) · 2.68 KB
/
couchbase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Cluster, QueryResult } from "couchbase";
import { Document } from "@langchain/core/documents";
import { BaseDocumentLoader, DocumentLoader } from "../base.js";
import { logVersion020MigrationWarning } from "../../util/entrypoint_deprecation.js";
/* #__PURE__ */ logVersion020MigrationWarning({
oldEntrypointName: "document_loaders/web/couchbase",
newPackageName: "@langchain/community",
});
/**
* loader for couchbase document
*/
export class CouchbaseDocumentLoader
extends BaseDocumentLoader
implements DocumentLoader
{
private cluster: Cluster;
private query: string;
private pageContentFields?: string[];
private metadataFields?: string[];
/**
* construct Couchbase document loader with a requirement for couchbase cluster client
* @param client { Cluster } [ couchbase connected client to connect to database ]
* @param query { string } [ query to get results from while loading the data ]
* @param pageContentFields { Array<string> } [ filters fields of the document and shows these only ]
* @param metadataFields { Array<string> } [ metadata fields required ]
*/
constructor(
client: Cluster,
query: string,
pageContentFields?: string[],
metadataFields?: string[]
) {
super();
if (!client) {
throw new Error("Couchbase client cluster must be provided.");
}
this.cluster = client;
this.query = query;
this.pageContentFields = pageContentFields;
this.metadataFields = metadataFields;
}
/**
* Function to load document based on query from couchbase
* @returns {Promise<Document[]>} [ Returns a promise of all the documents as array ]
*/
async load(): Promise<Document[]> {
const documents: Document[] = [];
for await (const doc of this.lazyLoad()) {
documents.push(doc);
}
return documents;
}
/**
* Function to load documents based on iterator rather than full load
* @returns {AsyncIterable<Document>} [ Returns an iterator to fetch documents ]
*/
async *lazyLoad(): AsyncIterable<Document> {
// Run SQL++ Query
const result: QueryResult = await this.cluster.query(this.query);
for await (const row of result.rows) {
let { metadataFields, pageContentFields } = this;
if (!pageContentFields) {
pageContentFields = Object.keys(row);
}
if (!metadataFields) {
metadataFields = [];
}
const metadata = metadataFields.reduce(
(obj, field) => ({ ...obj, [field]: row[field] }),
{}
);
const document = pageContentFields
.map((k) => `${k}: ${JSON.stringify(row[k])}`)
.join("\n");
yield new Document({
pageContent: document,
metadata,
});
}
}
}