I'm following a Fastify tutorial, everything has been going well so far (connected to Database, routes working, etc.) however when attempting to add the notesDAL I'm receiving the following error:
Error: ERR_AVVIO_PLUGIN_TIMEOUT: plugin did not start in time: /routes/notes/notesDAL.js. You may have forgotten to call 'done' function or to resolve a Promise
The code is exactly the same as the tutorial, so I assume it's perhaps outdated but I'm lost. I've tried a few different ways and seem to constantly face errors. Any assistance would be much appreciated.
notes.js
"use strict";
const NotesDAL = require("./notesDAL");
module.exports = async function (fastify, opts) {
const notesDAL = NotesDAL(fastify.db);
fastify.route({
method: "POST",
url: "/",
handler: async (request, reply) => {
const { title, body } = request.body;
const newNote = await notesDAL.createNote(title, body);
return newNote;
},
});
};
notesDAL.js
const NotesDAL = (db) => {
const createNote = async (title, body) => {
const { id } = await db.one(
"INSERT INTO notes (title, body) VALUES ($1, $2) RETURNING id",
[title, body]
);
return { id, title, body };
};
return { createNote };
};
module.exports = NotesDAL;
If it matters, the folder structure is:
/app.js
/plugins/db.js
/routes/notes/notes.js
/routes/notes/notesDAL.js
Edit:
I've gotten it working however I am sure it's not the right way to do things, it looks very odd.
notesDAL.js
const NotesDAL = (db) => {}
updated to:
const NotesDAL = async (db) => {}
notes.js
const newNote = await notesDAL.createNote(title, body)
updated to:
const newNote = await (await.notesDAL).createNote(title, body)
As I mentioned, this looks bad but appears to be working. I'd still like to know a better method of doing this.
Thank you.
Turns out that by creating the project with fastify-cli I was using fastify-autoload which prevented this from working.
AutoLoad can ignore the DAL files by adding the following to app.js when it registers AutoLoad:
ignorePattern: /.*(DAL).js/,
It then works successfully. Alternatively, I can move notesDAL.js to a folder called services and it works. So the folder structure would be:
/app.js
/plugins/db.js
/routes/notes/notes.js
/services/notesDAL.js