Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added importStream support for documents #215

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
861 changes: 715 additions & 146 deletions dist/typesense.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/typesense.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/typesense.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/typesense.min.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/Typesense/Documents.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ export default class Documents<T extends DocumentSchema = object> extends Search
*/
import(documents: string, options?: DocumentImportParameters): Promise<string>;
import(documents: T[], options?: DocumentImportParameters): Promise<ImportResponse[]>;
/**
* Imports documents from a NodeJS readable stream of JSONL.
*/
importStream(readableStream: ReadStream, options?: DocumentImportParameters): Promise<ImportResponse[]>;
/**
* Returns a JSONL string for all the documents in this collection
*/
Expand Down
33 changes: 33 additions & 0 deletions lib/Typesense/Documents.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Typesense/Documents.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/Typesense/Documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,42 @@ export default class Documents<T extends DocumentSchema = object>
return resultsInJSONLFormat as string;
}
}
/**
* Imports documents from a NodeJS readable stream of JSONL.
*/
async importStream(
readableStream: ReadStream,
options: DocumentImportParameters = {},
): Promise<ImportResponse[]> {
const resultsInJSONLFormat = await this.apiCall.performRequest<string>(
"post",
this.endpointPath("import"),
{
queryParameters: options,
bodyParameters: readableStream,
additionalHeaders: { "Content-Type": "text/plain" },
skipConnectionTimeout: true, // We never want to client-side-timeout on an import and retry, since imports are syncronous and we want to let them take as long as it takes to complete fully
enableKeepAlive: true, // This is to prevent ECONNRESET socket hang up errors. Reference: https://github.com/axios/axios/issues/2936#issuecomment-779439991
},
);

const resultsInJSONFormat = resultsInJSONLFormat
.split("\n")
.map((r) => JSON.parse(r)) as ImportResponse[];
const failedItems = resultsInJSONFormat.filter(
(r) => r.success === false,
);
if (failedItems.length > 0) {
throw new ImportError(
`${resultsInJSONFormat.length - failedItems.length
} documents imported successfully, ${failedItems.length
} documents failed during import. Use \`error.importResults\` from the raised exception to get a detailed error reason for each document.`,
resultsInJSONFormat,
);
} else {
return resultsInJSONFormat;
}
}

/**
* Returns a JSONL string for all the documents in this collection
Expand Down
Loading
Loading