Skip to content

Commit

Permalink
Import the latest main branch.
Browse files Browse the repository at this point in the history
Signed-off-by: huuyafwww <huuya1234fwww@gmail.com>
  • Loading branch information
huuyafwww committed Apr 15, 2023
1 parent 1951913 commit 2e47c79
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
17 changes: 16 additions & 1 deletion lib/Helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ interface UpdateActionOperation {
};
}

interface UpdateActionDocOperation {
doc: {
[key: string]: any;
};
}

interface UpdateActionScriptOperation {
script: {
[key: string]: any;
};
}

interface DeleteAction {
delete: {
_index: string;
Expand All @@ -111,7 +123,10 @@ interface DeleteAction {

type CreateAction = CreateActionOperation | [CreateActionOperation, unknown];
type IndexAction = IndexActionOperation | [IndexActionOperation, unknown];
type UpdateAction = [UpdateActionOperation, Record<string, any>];
type UpdateAction = [
UpdateActionOperation,
UpdateActionDocOperation | UpdateActionScriptOperation
];
type Action = IndexAction | CreateAction | UpdateAction | DeleteAction;
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

Expand Down
5 changes: 1 addition & 4 deletions lib/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,7 @@ class Helpers {
bulkBody.push(actionBody, payloadBody);
} else if (operation === 'update') {
actionBody = serializer.serialize(action);
payloadBody =
typeof chunk === 'string'
? `{"doc":${chunk}}`
: serializer.serialize({ doc: chunk, ...payload });
payloadBody = serializer.serialize(payload || chunk);
chunkBytes += Buffer.byteLength(actionBody) + Buffer.byteLength(payloadBody);
bulkBody.push(actionBody, payloadBody);
} else if (operation === 'delete') {
Expand Down
3 changes: 2 additions & 1 deletion test/types/helpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
ScrollSearchResponse,
OnDropDocument,
MsearchHelper,
UpdateActionDocOperation,
} from '../../lib/Helpers';
import { ApiResponse, ApiError, Context } from '../../lib/Transport';

Expand Down Expand Up @@ -107,7 +108,7 @@ expectError(
const options: BulkHelperOptions<Record<string, any>> = {
datasource: [],
onDocument(doc: Record<string, any>) {
return [{ update: { _index: 'test' } }, doc];
return [{ update: { _index: 'test' } }, doc as UpdateActionDocOperation];
},
};
expectAssignable<BulkHelperOptions<Record<string, any>>>(options);
Expand Down
12 changes: 9 additions & 3 deletions test/unit/helpers/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -971,14 +971,16 @@ test('bulk update', (t) => {
flushBytes: 1,
concurrency: 1,
onDocument() {
const currentId = id++;
return [
{
update: {
_index: 'test',
_id: id++,
_id: currentId,
},
},
{
doc: dataset[currentId],
doc_as_upsert: true,
},
];
Expand Down Expand Up @@ -1022,13 +1024,15 @@ test('bulk update', (t) => {
flushBytes: 1,
concurrency: 1,
onDocument() {
const currentId = id++;
return [
{
update: {
_index: 'test',
_id: id++,
_id: currentId,
},
},
{ doc: dataset[currentId] },
];
},
onDrop() {
Expand Down Expand Up @@ -1070,14 +1074,16 @@ test('bulk update', (t) => {
flushBytes: 1,
concurrency: 1,
onDocument() {
const currentId = id++;
return [
{
update: {
_index: 'test',
_id: id++,
_id: currentId,
},
},
{
doc: dataset[currentId],
doc_as_upsert: true,
},
];
Expand Down

0 comments on commit 2e47c79

Please sign in to comment.