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

fix(util-dynamodb): accept interface as in input for marshall #1952

Closed
wants to merge 4 commits into from
Closed
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
55 changes: 47 additions & 8 deletions packages/util-dynamodb/src/marshall.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,73 @@
import { convertToAttr } from "./convertToAttr";
import { marshall } from "./marshall";
import { INativeAttributeValue, NativeAttributeValue } from "./models";

jest.mock("./convertToAttr");

describe("marshall", () => {
const input = { a: "A", b: "B" };

beforeEach(() => {
(convertToAttr as jest.Mock).mockReturnValue({ M: input });
});
const mockOutput = { S: "mockOutput" };
(convertToAttr as jest.Mock).mockReturnValue({ M: mockOutput });

afterEach(() => {
jest.clearAllMocks();
});

it("calls convertToAttr", () => {
expect(marshall(input)).toEqual(input);
it("with object as an input", () => {
const input = { a: "A", b: "B" };

expect(marshall(input)).toEqual(mockOutput);
expect(convertToAttr).toHaveBeenCalledTimes(1);
expect(convertToAttr).toHaveBeenCalledWith(input, undefined);
});

["convertEmptyValues", "removeUndefinedValues"].forEach((option) => {
describe(`options.${option}`, () => {
const input = { a: "A", b: "B" };

[false, true].forEach((value) => {
it(`passes ${value} to convertToAttr`, () => {
expect(marshall(input, { [option]: value })).toEqual(input);
expect(marshall(input, { [option]: value })).toEqual(mockOutput);
expect(convertToAttr).toHaveBeenCalledTimes(1);
expect(convertToAttr).toHaveBeenCalledWith(input, { [option]: value });
});
});
});
});

it("with type as an input", () => {
type TestInputType = { a: string; b: string };
const input: TestInputType = { a: "A", b: "B" };

expect(marshall(input)).toEqual(mockOutput);
expect(convertToAttr).toHaveBeenCalledTimes(1);
expect(convertToAttr).toHaveBeenCalledWith(input, undefined);
});

it("with Interface as an input", () => {
interface TestInputInterface extends INativeAttributeValue<NativeAttributeValue> {
a: string;
b: string;
}
const input: TestInputInterface = { a: "A", b: "B" };

expect(marshall(input)).toEqual(mockOutput);
expect(convertToAttr).toHaveBeenCalledTimes(1);
expect(convertToAttr).toHaveBeenCalledWith(input, undefined);
});

it("with nested interfaces as an input", () => {
interface IPersonName extends INativeAttributeValue<NativeAttributeValue> {
firstname: string;
lastname: string;
}
interface IPerson extends INativeAttributeValue<NativeAttributeValue> {
id: string;
name: IPersonName;
}
const input: IPerson = { id: "id", name: { firstname: "John", lastname: "Doe" } };

expect(marshall(input)).toEqual(mockOutput);
expect(convertToAttr).toHaveBeenCalledTimes(1);
expect(convertToAttr).toHaveBeenCalledWith(input, undefined);
});
});
4 changes: 2 additions & 2 deletions packages/util-dynamodb/src/marshall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface marshallOptions {
* @param {any} data - The data to convert to a DynamoDB record
* @param {marshallOptions} options - An optional configuration object for `marshall`
*/
export const marshall = (
data: { [key: string]: NativeAttributeValue },
export const marshall = <T extends { [K in keyof T]: NativeAttributeValue }>(
data: T,
options?: marshallOptions
): { [key: string]: AttributeValue } => convertToAttr(data, options).M as { [key: string]: AttributeValue };
4 changes: 4 additions & 0 deletions packages/util-dynamodb/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* the `wrapNumbers` flag is set. This allows for numeric values that lose
* precision when converted to JavaScript's `number` type.
*/
export interface INativeAttributeValue<T> {
[key: string]: T;
}

export interface NumberValue {
readonly value: string;
}
Expand Down