-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete-row.ts
More file actions
87 lines (74 loc) · 2.82 KB
/
delete-row.ts
File metadata and controls
87 lines (74 loc) · 2.82 KB
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
import type { Client } from "../client";
import type { Condition, ConsumedCapacity, ReturnContent } from "../pb/type";
import type { PlainBufferCell, PlainBufferRow } from "../plainbuffer";
import { Buffer } from "node:buffer";
import { buildFilter } from "../builder/filter";
import { OTS_API_NAME } from "../const";
import { builder } from "../pb/builder";
import { RowExistenceExpectation } from "../pb/type";
import { decodePlainBuffer, encodePlainBuffer } from "../plainbuffer";
import { fixPlainBufferCellType } from "../utils";
export const ProtoDeleteRowRequest = builder.lookupType("ots.DeleteRowRequest");
export const ProtoDeleteRowResponse = builder.lookupType("ots.DeleteRowResponse");
export interface DeleteRowData {
tableName: string;
primaryKey: PlainBufferCell[];
condition?: Condition;
returnContent?: ReturnContent;
transactionID?: string;
}
export interface DeleteRowResponse {
consumed: ConsumedCapacity;
row: Array<PlainBufferRow> | null;
}
export class DeleteRow {
public constructor(private readonly client: Client) {
}
public static async builder(options: DeleteRowData) {
const payload: Record<string, any> = {
tableName: options.tableName,
primaryKey: Buffer.from(encodePlainBuffer([{
primaryKey: options.primaryKey.map(fixPlainBufferCellType),
attributes: [],
deleteMarker: true,
}])),
};
if (options.condition) {
payload.condition = options.condition;
if (options.condition.columnCondition) {
payload.condition.columnCondition = buildFilter(options.condition.columnCondition);
}
}
else {
payload.condition = {
rowExistence: RowExistenceExpectation.IGNORE,
};
}
if (options.returnContent) {
payload.returnContent = options.returnContent;
}
if (options.transactionID) {
payload.transactionID = options.transactionID;
}
return ProtoDeleteRowRequest.encode(ProtoDeleteRowRequest.create(payload)).finish();
}
public async do(data: DeleteRowData) {
const body = await DeleteRow.builder(data);
return await this.client.request.do({
apiName: OTS_API_NAME.DeleteRow,
body,
});
}
public static response(data: Uint8Array): DeleteRowResponse {
const ret: Record<string, any> = {
row: null,
};
const raw = ProtoDeleteRowResponse.toObject(ProtoDeleteRowResponse.decode(data));
ret.consumed = raw.consumed;
const row = raw.row as Uint8Array | undefined;
if (row && row.byteLength > 0) {
ret.row = decodePlainBuffer(Buffer.from(row).buffer);
}
return ret as DeleteRowResponse;
}
}