Registry / database / mongoose-function

mongoose-function

JSON →
library0.1.0jsnpmunverified

Plugin for Mongoose that adds support for storing and retrieving JavaScript functions as a schema type. Version 0.1.0 is stable but appears to be in maintenance mode with no recent updates. Unlike storing functions as strings, this plugin enables direct execution of stored functions after retrieval. It monkey-patches Mongoose's schema types, which may cause compatibility issues with newer Mongoose versions. The package is designed for Mongoose 3.x/4.x and requires a separate Mongoose instance.

npm install mongoose-function
INSTALL
IMPORT
SIG · MONGOOSE-FUNCTION
M
mongoose-function
databasejavascriptv0.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mongoose-function
require('mongoose-function')(mongoose);
const mongooseFunction = require('mongoose-function');
The package is a function that must be invoked with a Mongoose instance as argument.
Schema.Types.Function
var mySchema = new Schema({ func: Function });
var mySchema = new Schema({ func: { type: Function } });
Using the global `Function` constructor as the type is correct; { type: Function } also works but is redundant.

Shows how to register the plugin, define a schema with Function type, store a function, and retrieve/execute it.

var mongoose = require('mongoose'); require('mongoose-function')(mongoose); var mySchema = new mongoose.Schema({ func: Function }); var M = mongoose.model('Functions', mySchema); var m = new M; m.func = function(){ console.log('stored function'); }; m.save(function (err) { M.findById(m._id, function (err, doc) { doc.func(); // logs "stored function" }); });
Debug
Known issues
deprecatedPackage is not compatible with Mongoose 5+; requires Mongoose 3.x/4.x.
fix
Upgrade to Mongoose 5+ and avoid using this plugin, or pin Mongoose to version 4.x.
affects: >0.1.0
gotchaFunction scope is not preserved when storing; only the function body is stored.
fix
Store any referenced closure variables separately as document properties.
affects: >=0.1.0
gotchaRequires calling require('mongoose-function')(mongoose) before defining any schemas that use the Function type.
fix
Always invoke the plugin at the top of your application, before any model definitions.
affects: >=0.1.0
gotchaThe plugin monkey-patches Mongoose's schema type system, which can conflict with other plugins or future Mongoose versions.
fix
Use with caution; consider alternatives like storing functions as strings or using eval.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: Cannot read property 'prototype' of undefined
Mongoose instance not passed to the plugin function, or Mongoose not installed.
fix
Ensure mongoose is installed and pass the mongoose instance: require('mongoose-function')(require('mongoose'));
MissingSchemaError: Schema hasn't been registered for model "Functions". Use mongoose.model(name, schema)
Calling model() before registering the schema or the plugin not invoked.
fix
Invoke require('mongoose-function')(mongoose) after requiring mongoose but before any model definitions.
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies
mongooserequiredpeer dependency; plugin monkey-patches Mongoose's schema types
Agent activity
8 hits · last 30 days
node
8
Resources
mongoose-function — npm install mongoose-function · libregistry