-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathput-row.ts
More file actions
86 lines (74 loc) · 2.81 KB
/
put-row.ts
File metadata and controls
86 lines (74 loc) · 2.81 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
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 ProtoPutRowRequest = builder.lookupType("ots.PutRowRequest");
export const ProtoPutRowResponse = builder.lookupType("ots.PutRowResponse");
export interface PutRowData {
tableName: string;
primaryKey: PlainBufferCell[];
attributes: PlainBufferCell[];
condition?: Condition;
returnContent?: ReturnContent;
transactionID?: string;
}
export interface PutRowResponse {
consumed: ConsumedCapacity;
row: Array<PlainBufferRow> | null;
}
export class PutRow {
public constructor(private readonly client: Client) {
}
public static async builder(options: PutRowData) {
const payload: Record<string, any> = {
tableName: options.tableName,
row: Buffer.from(encodePlainBuffer([{
primaryKey: options.primaryKey.map(fixPlainBufferCellType),
attributes: options.attributes.map(fixPlainBufferCellType),
}])),
};
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 ProtoPutRowRequest.encode(ProtoPutRowRequest.create(payload)).finish();
}
public async do(data: PutRowData) {
const body = await PutRow.builder(data);
return await this.client.request.do({
apiName: OTS_API_NAME.PutRow,
body,
});
}
public static response(data: Uint8Array): PutRowResponse {
const ret: Record<string, any> = {
row: null,
};
const raw = ProtoPutRowResponse.toObject(ProtoPutRowResponse.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 PutRowResponse;
}
}