tywalch/electrodb

Incorrect type information when using data()

CaveSeal opened this issue · 4 comments

Hello! Love the library!

Describe the bug
The issue occurs when you leave the composite field empty on the sort key for the base table. The first argument to the data() function when performing an update is missing the attributes of the entity in the resulting type.

ElectroDB Version
2.5.1

ElectroDB Playground Link
Oddly, it works in the playground but not when I test the same code in my own environment.

I was able to reproduce the issue on Codesandbox.

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

import { Entity } from 'electrodb';
import moment from "moment";

export const users = new Entity({
  model: {
    entity: "user",
    service: "versioncontrol",
    version: "1"
  },
  attributes: {
    username: {
      type: "string"
    },
    fullName: {
      type: "string"
    },
    photo: {
      type: "string"
    },
    bio: {
      type: "string"
    },
    location: {
      type: "string"
    },
    pinned: {
      type: "any"
    },
    following: {
      type: "set",
      items: "string"
    },
    followers: {
      type: "set",
      items: "string"
    },
    createdAt: {
      type: "string",
      set: () => moment.utc().format(),
      readOnly: true,
    },
    updatedAt: {
        type: "string",
        watch: "*",
        set: () => moment.utc().format(),
        readOnly: true,
    },
  },
  indexes: {
    user: {
      collection: "overview",
      pk: {
        composite: ["username"],
        field: "pk"
      },
      sk: {
        composite: [],
        field: "sk"
      }
    },
    _: {
      collection: "owned",
      index: "gsi1pk-gsi1sk-index",
      pk: {
        composite: ["username"],
        field: "gsi1pk"
      },
      sk: {
        field: "gsi1sk",
        composite: []
      }
    },
    subscriptions: {
      collection: "watching",
      index: "gsi3pk-gsi3sk-index",
      pk: {
        composite: ["username"],
        field: "gsi3pk"
      },
      sk: {
        composite: [],
        field: "gsi3sk"
      }
    }
  }
});

Expected behavior
The type of data should have the attributes of the entity rather than an error message given by intellisense.

Errors

Screenshot 2023-04-23 at 12 41 49

Property 'username' does not exist on type 'W'. ts(2339)

Hi @CaveSeal 👋

This is actually an easy one! The "username" property is a composite attribute in your primary. This makes that attribute read-only, since the pk and sk associated with the primary index in dynamodb are immutable. The typing knows this so username is absent in set because it cannot be changed.

@tywalch Thanks for the quick response! In that case, my example was bad, as the same problem occurs for all attributes, like pinned or bio. Sorry if I'm still misunderstanding.

Screenshot 2023-04-24 at 00 05 29

I am not able to tell what version of TS that codesandbox uses, but I suspect that might be the issue here. Can you confirm which version you're currently using locally? There were some breaking changes in TS around the early/mid part of 4.* that might be the culprit here. Are you able to update to the latest version and verify what you're seeing against that? The playground uses the latest by default.

I tried to set up a couple of projects to see if I could reproduce the issue I'm having, but it seemed to work just fine for all of them. It just doesn't work for one specific package in our monorepo and that CodeSandbox, both of which are on version 2.5.1. The types in the node_modules are up to date, and I cross checked with a colleague too. It's all very strange.

Either way, it would seem this is a problem with something in my setup and not a problem on your end, so I will close the issue. Apologies for wasting your time.