Issue: Automattic/mongoose#11656
to run your app, do npm i
and then npm run dev
, website will be avaiable on http://localhost:3000
.
./src/lib/db/sondy.js
contains schema and model.
./src/lib/db/init.js
contains init function, we call in .then()
after mongoose.connect
is done, in this file we don't create collection, but instead try to read it (even bofore collection was created)
./src/hooks.js
contains pluralize and connection code. Hooks (like handle function) are executed at every request, and are imported at first request to website
./src/routes/todos/index.js
contains code that use model sondy
and put some data do database. This is page endpoint file, that is executed when you call resolve(event)
inside ./src/hooks.js
if event properties point to URL /todos
. So whole flow is:
- from browser request come to
/todos
page - handle function in
./src/hooks.js
is executed (and as module works, if this is first time, import cause top scope code to be executed - like mongoose.connect()) - in handle hook, we call
resolve(event)
and it forward request to/todos
page and endpoint get()
function is executed in./src/routes/todos/index.js
that contains import of model for mongoose, and use it.- In that moment
sondy
collection is created - Now turn off app and drop database
repro_mongodb_pluralize_kit_issue
- uncomment code in
.src/lib/db/init.js
- again open app and visit
/todos
page - In that moment
sondies
collection is created - Now as
sondies
exist, even code in endpoint (that saves data do collection) usesondies
.
Pluralize doesn't work if we touch collection (reading) before it's created, in that case, mongoose somehow automatically generate collection with pluralized name even if pluralize option is set to null.