dynamoose/dynamoose

[BUG] "Date" type fails in combination with using NextJS v15

Open

#1 722 ouverte le 11 févr. 2025

Voir sur GitHub
 (2 commentaires) (3 réactions) (0 assignés)JavaScript (2 025 stars) (349 forks)batch import
help wantedstatus:triagetype:bug

Description

Summary:

Next.JS v15 overrides the default Date object in the node environment.

This causes the following exception to be thrown during schema creation when a Date type field is used:

 InvalidParameter: Attribute createdAt does not have a valid type.

Code sample:

After investigating the error is due to this code now returning Date1 instead of Date as expected. This fails to match the dynamo type DynamoDBType({ "name": "Date" ... (Code)

const regexFuncName = /^Function ([^(]+)\(/iu;
[, type] = typeVal.toString().match(regexFuncName);

Schema

const videoSchema = new dynamoose.Schema ({
    "video_name": String,
}, {
    timestamps: true
});

Model

export const VideoModel = dynamoose.model<Video & Item>("Video", videoSchema);

General

I confirmed this issue by adding this condition and the exception is no longer thrown:

+ else if (typeVal === Date) {
+     type = 'Date';
+ }
   else {
      const regexFuncName = /^Function ([^(]+)\(/iu;
      [, type] = typeVal.toString().match(regexFuncName);
  }

I'm unsure what the ideal fix here is besides hardcoding the relevant types.

Current output and behavior (including stack trace):

⨯ unhandledRejection:  InvalidParameter: Attribute createdAt does not have a valid type.
    at Array.forEach (<anonymous>)
    at eval (src/lib/database.ts:53:20)
    at ./src/lib/database.ts (.next/server/pages/index.js:77:1)
    at __webpack_require__ (.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///./src/lib/auth.ts:21:67)
    at ./src/lib/auth.ts (.next/server/pages/index.js:44:1)
    at __webpack_require__ (.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///./src/lib/_components.tsx:39:63)
    at __webpack_require__.a (.next/server/webpack-runtime.js:97:13)
    at eval (webpack-internal:///./src/lib/_components.tsx:1:21)
    at ./src/lib/_components.tsx (.next/server/pages/index.js:33:1)
    at __webpack_require__ (.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///./src/pages/index.tsx:9:73)
  51 | }
  52 |
> 53 | const videoSchema = new dynamoose.Schema ({
     |                    ^
  54 |     "video_name": String,
  55 |     "exercises": {
  56 |         "type": Array,

Expected output and behavior:

The Date type would not cause an exception to be thrown.

Environment:

Operating System: macOS Operating System Version: 15.3 Node.js version (node -v): v23.6.0 NPM version: (npm -v): 10.9.2 Dynamoose version: 4.0.3

Other information (if applicable):

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

Guide contributeur