Reacting
Reacting

Reputation: 6123

In DymanoDB/Dynamoose, how can I set a schema of a map of string keys and number values?

So I have this instructions:

Schema Defs: Result object: It's a map of string keys and number values

"result": { "M": { [STRING]: { "N": "401" } },

This is what I have so far

  result: {
    type: Object,
    schema: {
      // I am getting 2 errors:
      // Parsing error: Unexpected token, expected "]"
      // ',' expected.
      [id: String]: Number
    },
    required: true
  },

Any ideas?

Upvotes: 1

Views: 2204

Answers (2)

Stark
Stark

Reputation: 65

It's not totally related with the topic, but I'd like to share because it could be the same issue for any others. If you have an object like that below in your database, that the key it's not defined, it could be a string or number...

    "stockProportionByGroup": {
        "1": 0.2075,
        "2": 0.1265,
        "3": 0.18
    },

And your are trying to get this data from database and it's returning nothing, and your schema looks like that:

rules: {
      type: Array,
      schema: [
        {
          type: Object,
          schema: {
            batch: { type: Number },
            stockProportionByGroup: { type: Object },
          },
        },
      ],
    },

Just add the saveUnknown: true as mention above!

Upvotes: 0

Charlie Fish
Charlie Fish

Reputation: 20496

[id: String] is a TypeScript thing. Not allowed in standard JavaScript.

This is not technically possible in Dynamoose. The only option here is to use the saveUnknown schema setting.

This was brought up in a conversation before, and the user who wanted to do this I told to create an issue on the GitHub repo but it doesn't look like that happened. If you'd like support for this in Dynamoose in the future, please submit a feature request on the GitHub repo.


Edit

In order to do this your schema would looks something like:

new dynamoose.Schema({
  result: {
    type: Object,
    required: true
  },
}, {
  "saveUnknown": ["result.**"]
});

This will allow indefinitely nested objects within result.

Upvotes: 2

Related Questions