tywalch/electrodb

BatchGet returns zero results

g-farrow opened this issue · 2 comments

Describe the bug
Performing a .get({table: "TABLE_NAME"}) on a Model, when passing the table name in as an option does not return any results:

const response = await Segment.get([
  { tenantId, siteId, segmentId: "1" },
  { tenantId, siteId, segmentId: "2" },
]).go({ table });

I have debugged the code and I can see that the DynamoDB Doc Client does return results - however Electro does not surface those results back to the application code.

When I run in debug mode I can see that the code in entity.js:571 is not being run because the table variable appears to have not been set:

		if (response.Responses[table] && Array.isArray(response.Responses[table])) {
			const responses = response.Responses[table];
			for (let i = 0; i < responses.length; i++) {
				const item = responses[i];
				const slot = orderMaintainer.getOrder(item);
				const formatted = this.formatResponse({Item: item}, index, config);
				if (slot !== -1) {
					resultsAll[slot] = formatted.data;
				} else {
					resultsAll.push(formatted.data);
				}
			}
		}

In this code above, the response.Responses contains a correctly formatted array of results. But, because table is undefined it is not able to process those results. Therefore the return is an empty array.

I have looked into the code but I cannot see where the table should have been set. I suspect it is because I am passing table as an option to the go method.

ElectroDB Version
2.2.0

ElectroDB Playground Link
N/A - this issue is with the handling of the DynamoDB Doc Client response - not the formatting of the initial request.

Entity/Service Definitions
Include your entity model (or a model that sufficiently recreates your issue) to help troubleshoot.

const Segment = new Entity({
  model: {
    entity: "Segment",
    version: "1",
    service: "TourContent",
  },
  attributes: {
    tenantId: {
      type: "string",
      required: true,
    },
    siteId: {
      type: "string",
      required: true,
    },
    segmentId: {
      type: "string",
      required: true,
      default: uuidv4,
      readOnly: true,
      validate: /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i,
    },
    name: {
      type: "string",
      required: true,
    },
    createdAt,
    updatedAt,
    state: {
      type: ["active", "inactive"] as const,
      required: true,
    },
    image: {
      type: "map",
      required: false,
      properties: {
        bucket: {
          type: "string",
          required: false,
        },
        key: {
          type: "string",
          required: false,
        },
      },
    },
  },
  indexes: {
    _: {
      pk: {
        field: PARTITION_KEY,
        composite: ["tenantId", "siteId", "segmentId"],
      },
      sk: {
        field: SORT_KEY,
        composite: [],
      },
    },
    site: {
      collection: "site",
      index: GSI1_INDEX_NAME,
      pk: {
        field: GSI1_PARTITION_KEY,
        composite: ["tenantId", "siteId"],
      },
      sk: {
        field: GSI1_SORT_KEY,
        composite: ["segmentId"],
      },
    },
  },
});

Expected behavior
The results which DynamoDB Doc Client responds with is processed successfully and the items are returned to the application code

Errors
N/A

Thank you for opening this Greg, I will take a look soon!

@tywalch thanks for fixing this so quickly ❤️

The electrodb is awesome btw