skorphil / nextjs-form

Educational NextJs project to build a complex mobile-first form
https://www.chromatic.com/library?appId=65b2882ac1586a6b21cba0dd
MIT License
3 stars 0 forks source link

Implement server action with mongoose and mongoDB integration #66

Closed skorphil closed 7 months ago

skorphil commented 7 months ago
skorphil commented 7 months ago

example how to separately store mongoDb model

it is good practice to cache mongo connection. (to prevent creating connections on each operation). For this author suggests use of global object in nodejs

Seems like it is not necessary to have additional caching logic, because mongoose has connection pool Caching pattern is used in serverless functions: https://github.com/Automattic/mongoose/discussions/14425#discussion-6340493

Official docs on serverless function https://mongoosejs.com/docs/lambda.html

skorphil commented 7 months ago

Previously i came up to quotes structure as:

quotes: [
      {
        usd: [
          { amd: 401.94591125 },
          { brl: 4.94543198 },
          { usd: 1 },
        ],
      },
]

but this seems like not best structure due to dynamic key names (currencies). This is not convenient for schemas. I.e. for mongoose scheme.

Better to use static key names:

quotes: [
    {
        baseCurrency: "usd", 
        rates:[
             {currency: "brl", rate: 4.94543198},
             {currency: "amd", rate: 401.94591125},
        ] 
    }
]
skorphil commented 7 months ago

MongoDB supports subdocuments, so for example quotes can be stored separately as a subdocument of record. For the sake of simplicity will not be use em and store whole nested record inside single document

skorphil commented 7 months ago

Implemented base functionality.

utils/mongooseConnect - handle mongoose connection serverActions/appendRecord - handle reformatting of FormData and submitting to db models/record - record model for mongoose

For the future - maybe i should group all stuff in feature-based folder, like features/mongoAppendRecord

Remaining