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

[BUG] Storing Timestamp As Iso Not Working - Expected createdAt to be of type date, instead found type string #1638

Open
6 tasks done
liam-ot opened this issue Oct 12, 2023 · 2 comments

Comments

@liam-ot
Copy link

liam-ot commented Oct 12, 2023

Summary:

Setting a custom timestamp with storage set to iso and value to string causes an issue when creating a new Dynamo document.

Error seems to occur on the PutItem request when creating a new item in table 2. I am also interacting with table 1 here, but that has a simple timestamps: true in it's schema so it doesnt have to do with that as far as i can tell

Hopefully I have given enough info here, please let me know if not.

Code sample:

Schema

new dynamoose.Schema(
    {
      key1: {
        type: String,
        hashKey: true
      },
      key2: {
        type: String,
        rangeKey: true
      },
      anotherField: {
        type: String,
        enum: ['enum1', 'enum2']
      }
    },
    {
      timestamps: {
        createdAt: {
          createdAt: {
            type: {
              value: Date,
              settings: {
                storage: 'iso'
              }
            }
          }
        },
        updatedAt: {
          updatedAt: {
            type: {
              value: Date,
              settings: {
                storage: 'iso'
              }
            }
          }
        }
      },
      saveUnknown: true
    }
  );

General

  const tableOne = new TableOneRepository();
  const tableOneItem = await tableOne.get(id);
  if (!tableOneItem) return callback('invalid-id');
  const tableTwo = new TableTwoRepository();
  // error here
  const tableTwoItem = await tableTwo.create(
    key1,
    key2,
    anotherField
  );

class TableTwoRepository {
  schema = new dynamoose.Schema(
    {
      key1: {
        type: String,
        hashKey: true
      },
      key2: {
        type: String,
        rangeKey: true
      },
      anotherField: {
        type: String,
        enum: ['enum1', 'enum2']
      }
    },
    {
      timestamps: {
        createdAt: {
          createdAt: {
            type: {
              value: Date,
              settings: {
                storage: 'iso'
              }
            }
          }
        },
        updatedAt: {
          updatedAt: {
            type: {
              value: Date,
              settings: {
                storage: 'iso'
              }
            }
          }
        }
      },
      saveUnknown: true
    }
  );

  constructor() {
    const tableName = process.env.TABLE_TWO;
    this.dbInstance = dynamoose.model(tableName, this.schema);
  }

  async create(username, id, type, connectorId) {
    const document = { username, id, type, connectorId, status: 'unknown' };
    await this.dbInstance.create(document);
    return document;
  }
}

class TableTwoRepository {
  schema = new dynamoose.Schema(
    {
      id: {
        type: String,
        hashKey: true,
      },
    },
    {
      timestamps: true,
      saveUnknown: true,
    }
  );

  constructor() {
    const tableName = process.env.TABLE_ONE;
    this.dbInstance = dynamoose.model(tableName, this.schema);
  }

  async get(id) {
    const document = this.dbInstance.get({ id });
    if (!document) return null;
    return document;
  }
};

Current output and behavior (including stack trace):

calling create through the dynamoose model

aws:dynamodb:describeTable:request - {
    "TableName": "table1"
}
aws:dynamodb:describeTable:request - {
    "TableName": "table2"
}
aws:dynamodb:describeTable:response - {
    "$metadata": {
        "httpStatusCode": 200,
        "requestId": "17fb6997-84a5-4d55-acf6-c7136a93db6f",
        "attempts": 1,
        "totalRetryDelay": 0
    },
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "id",
                "AttributeType": "S"
            }
        ],
        "CreationDateTime": "2023-10-12T22:01:04.415Z",
        "ItemCount": 1,
        "KeySchema": [
            {
                "AttributeName": "id",
                "KeyType": "HASH"
            }
        ],
        "ProvisionedThroughput": {
            "LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
            "LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        },
        "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/table1",
        "TableName": "table1",
        "TableSizeBytes": 105,
        "TableStatus": "ACTIVE"
    }
}
aws:dynamodb:getItem:request - {
    "Key": {
        "id": {
            "S": "0ccada62-54d7-4692-a00d-acf4c121fe0e"
        }
    },
    "TableName": "table1"
}
aws:dynamodb:describeTable:response - {
    "$metadata": {
        "httpStatusCode": 200,
        "requestId": "ffe834a5-d2ba-4a7e-84ca-b005dcf1fa51",
        "attempts": 1,
        "totalRetryDelay": 0
    },
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "key1",
                "AttributeType": "S"
            },
            {
                "AttributeName": "key2",
                "AttributeType": "S"
            }
        ],
        "CreationDateTime": "2023-10-12T22:01:04.423Z",
        "ItemCount": 0,
        "KeySchema": [
            {
                "AttributeName": "key1",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "key2",
                "KeyType": "RANGE"
            }
        ],
        "ProvisionedThroughput": {
            "LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
            "LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5
        },
        "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/table2",
        "TableName": "table2",
        "TableSizeBytes": 0,
        "TableStatus": "ACTIVE"
    }
}
aws:dynamodb:putItem:request - {
    "Item": {
        "key1": {
            "S": "foobar"
        },
        "key2": {
            "S": "0ccada62-54d7-4692-a00d-acf4c121fe0e"
        },
        "anotherField": {
            "S": "unknown"
        },
        "createdAt": {
            "S": "2023-10-12T22:01:50.730Z"
        },
        "updatedAt": {
            "S": "2023-10-12T22:01:50.730Z"
        }
    },
    "TableName": "table2",
    "ConditionExpression": "attribute_not_exists(#__hash_key)",
    "ExpressionAttributeNames": {
        "#__hash_key": "username"
    }
}
aws:dynamodb:getItem:response - {
    "$metadata": {
        "httpStatusCode": 200,
        "requestId": "85ccdbd1-a3d2-4d04-acb3-ad61996d4e5f",
        "attempts": 1,
        "totalRetryDelay": 0
    },
    "Item": {
        "createdAt": {
            "S": "2023-10-12T22:01:27.619Z"
        },
        "foo": {
            "S": "garbar"
        },
        "key1": {
            "S": "0ccada62-54d7-4692-a00d-acf4c121fe0e"
        },
        "bar": {
            "S": "barfoo"
        },
        "foobar": {
            "N": "0"
        }
    }
}
✖ Uncaught exception
Environment: darwin, node 16.9.1, framework 3.33.0 (local), plugin 6.2.3, SDK 4.3.2
Credentials: Local, "default" profile
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
TypeMismatch: Expected createdAt to be of type date, instead found type string.
    at checkTypeFunction (/foo/node_modules/dynamoose/dist/Item.js:358:27)
    at Array.map (<anonymous>)
    at Function.Item.objectFromSchema (/foo/node_modules/dynamoose/dist/Item.js:383:128)
    at Item.conformToSchema (/foo/node_modules/dynamoose/dist/Item.js:569:39)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async /foo/node_modules/dynamoose/dist/Model/index.js:772:40

1 deprecation found: run 'serverless doctor' for more details
aws:dynamodb:putItem:response - {
    "$metadata": {
        "httpStatusCode": 200,
        "requestId": "5e38c664-186c-423a-9af9-96a1bf511d6a",
        "attempts": 1,
        "totalRetryDelay": 0
    }
}
(λ: endpoint) RequestId: clnnq5xzj00000ipt9k6cfk2h  Duration: 1690.00 ms  Billed Duration: 1690 ms

Expected output and behavior:

It should just return me the Document and not expect it to be a Date since i have set the value to String. I have set the value to Date and no change.

Environment:

Operating System: MacOS
Operating System Version: 12.6.9 (Monterey)
Node.js version (node -v): 16.9.1
NPM version: (npm -v): 7.21.1
Dynamoose version: 3.2.1

Other:

  • I have read through the Dynamoose documentation before posting this issue
  • I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
  • I have searched the internet and Stack Overflow to ensure this issue hasn't been raised or answered before
  • I have tested the code provided and am confident it doesn't work as intended
  • I have filled out all fields above
  • I am running the latest version of Dynamoose
@Nessvah
Copy link

Nessvah commented Nov 17, 2023

same problem here

@andrew-sol
Copy link

Duplicate of #1211

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

No branches or pull requests

3 participants