How can I share code (e.g. Mongo schema definitions) between files in an Azure function app?
I need to do this, as my functions require access to a shared mongo sche
I fixed this issue by doing the following steps:
hosts.json
to watch
a shared folder. "watchDirectories": [ "Shared" ]
blogPostModel.js
file containing the following schema/model definition and exportshared\blogPostModel.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var blogPostSchema = new Schema({
id: 'number',
title: 'string',
date: 'date',
content: 'string'
});
module.exports = mongoose.model('BlogPost', blogPostSchema);
require
the shared file with the following path:
var blogPostModel = require('../Shared/blogPostModel.js');
I can then make a connection and interact with the model doing find
s etc in each individual function.
This solution was composed from the following SO posts:
Azure Function in Node.js and shared files
Cannot overwrite model once compiled Mongoose