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

Error when importing abstraction that uses dynamodb-toolbox #732

Open
jhunter-motorway opened this issue May 1, 2024 · 0 comments
Open

Comments

@jhunter-motorway
Copy link

jhunter-motorway commented May 1, 2024

I have a package that provides a ddb abstraction and uses dynamodb-toolbox v0.9.3.

The package exports the abstraction, but when I import it into my client repo (a next application), I get the error Error: Please provide a valid 'indexes' object, which is odd as, when I console log the indexes object, I get

{
  GSI1: { partitionKey: 'gsi1_pk', sortKey: 'gsi1_sk' },
  GSI2: { partitionKey: 'gsi2_pk', sortKey: 'gsi2_sk' },
  LSI1: { sortKey: 'lsi1_sk' },
  LSI2: { sortKey: 'lsi2_sk' }
}

which seems fine to me.

The abstraction looks like this:

import snakeCase from "lodash/snakeCase";

import type { Identity } from "../../";

import { DealerContact } from "./entities/DealerContact";
import { Dealership } from "./entities/Dealership";
import { VehicleProfile } from "./entities/VehicleProfile";

import type { EntityNames } from "./enums";
import type { IDbClient } from "./types";

const entities = {
  DealerContact,
  Dealership,
  VehicleProfile,
};

const constantCase = (str: string) => snakeCase(str).toUpperCase();

export const dbClientFactory = (): IDbClient => {
  const getKeys = async (identity: Identity) => {
    if (!identity) {
      throw new Error("No identity found in token");
    }
    const { dealerId, id: dealerContactId } = identity;
    return { dealerContactId, dealerId };
  };

  const getById = async <T>(entity: EntityNames, identity: Identity, id: string): Promise<T | null> => {
    const { dealerContactId, dealerId } = await getKeys(identity);
    const pk = `DEALER_ID#${dealerId}#${constantCase(entity)}`;
    const sk = `DEALER_CONTACT_ID#${dealerContactId}#ID#${id}`;
    return entities[entity].get({ pk, sk }) as Promise<T>;
  };

  const listAll = async <T>(entity: EntityNames, identity: Identity): Promise<T[]> => {
    const { dealerContactId, dealerId } = await getKeys(identity);
    const pk = `DEALER_ID#${dealerId}#${constantCase(entity)}`;

    const result = await entities[entity].query(pk, {
      beginsWith: `DEALER_CONTACT_ID#${dealerContactId}#ID#`,
    });
    if (result.Items) {
      return result.Items as T[];
    }
    return [];
  };

  return { getById, listAll };
};

And an entity looks like this:

/* eslint-disable sort-keys-fix/sort-keys-fix */
import { Entity } from 'dynamodb-toolbox';

import { TradeTable as table } from '../table';

/**
 * Once we have the table, we define the AccountEntity entity.
 * If needed, additional entities can be defined using the same approach.
 */
export const VehicleProfile = new Entity({
	attributes: {
		pk: { default: (data: { dealerId: string }) => `DEALER_ID#${data.dealerId}#VEHICLE_PROFILES`, partitionKey: true },
		sk: {
			default: (data: { dealerContactId: string; id: string }) =>
				`DEALER_CONTACT_ID#${data.dealerContactId}#ID${data.id}`,
			sortKey: true,
		},
		lsi1_sk: { default: (data: { enquiryId: string }) => `ENQUIRY_ID#${data.enquiryId}` },
		lsi2_sk: { default: (data: { vrm: string }) => `VRM#${data.vrm}` },
		id: { type: 'string' },
		vrm: { type: 'string' },
		enquiryId: { type: 'number' },
		makeId: { type: 'number' },
		makeName: { type: 'string' },
		makeSlug: { type: 'string' },
		modelId: { type: 'number' },
		modelName: { type: 'string' },
		stateId: { type: 'number' },
		stateName: { type: 'string' },
		stateSlug: { type: 'string' },
		stateCategory: { type: 'string' },
		priceReserve: { type: 'number' },
		createdAt: { type: 'string' },
		updatedAt: { type: 'string' },
		dealerId: { type: 'string' },
		dealerContactId: { type: 'string' },
		sequenceId: { type: 'number' },
	},
	name: 'VehicleProfile',
	table,
	timestamps: false,
} as const);

And the table definition looks like this:

import { Table } from "dynamodb-toolbox";

import { DocumentClient } from "./ddbClient";

console.log("process.env.TABLE_NAME: ", process.env.TABLE_NAME);

export const TradeTable = new Table({
  DocumentClient,
  entityField: "TYPE",
  indexes: {
    GSI1: { partitionKey: "gsi1_pk", sortKey: "gsi1_sk" },
    GSI2: { partitionKey: "gsi2_pk", sortKey: "gsi2_sk" },
    LSI1: { sortKey: "lsi1_sk" },
    LSI2: { sortKey: "lsi2_sk" },
  },
  name: String(process.env.TABLE_NAME),
  partitionKey: "pk",
  sortKey: "sk",
});

Any ideas why this might be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant