firebase-safe-mongoose-objectid

0.2.0 • Public • Published

Firebase-Safe Mongoose ObjectId

Turn all ObjectId objects into strings and back again.

Usage

castToString(graph, [clone=false])

Turn all the ObjectIds in the graph into strings for sending safely to firebase

  • graph object|array - Any object graph
  • clone boolean (optional) - Whether to create deep clone of the graph (defaults to false)
var safeObjectId = require('firebase-safe-mongoose-objectid');

Model.find({}).exec().then(function (docs) {

  // create a deep clone, leaving original un-modified
  var safeForFirebase = safeObjectId.castToString(docs);
  console.log(typeof docs[0]._id); // object
  console.log(docs[0]._id instanceof mongoose.Types.ObjectId); // true
  console.log(typeof safeForFirebase[0]._id); // string
  console.log(safeForFirebase[0]._id instanceof mongoose.Types.ObjectId); // false

  // modify in place
  firebaseSafeMongooseObjectId.asString(docs);
  console.log(typeof docs[0]._id); // string
  console.log(docs[0]._id instanceof mongoose.Types.ObjectId); // false
});

castFromString

Turn all the strings that are valid ObjectIds back into ObjectId proper

  • graph object|array - Any object graph
  • clone boolean (optional) - Whether to create deep clone of the graph (defaults to false)
var mongoose = require('mongoose');
var safeObjectId = require('firebase-safe-mongoose-objectid');

Model.find({}).exec().then(function (docs) {

  // create a deep clone, leaving original un-modified
  var safeForFirebase = safeObjectId.castFromString(docs);
  console.log(typeof safeForFirebase[0]._id); // object
  console.log(safeForFirebase[0]._id instanceof mongoose.Types.ObjectId); // true

  // modify in place
  firebaseSafeMongooseObjectId.castFromString(docs);  
  console.log(docs[0]._id instanceof mongoose.Types.ObjectId); // true
});

Readme

Keywords

none

Package Sidebar

Install

npm i firebase-safe-mongoose-objectid

Weekly Downloads

0

Version

0.2.0

License

MIT

Last publish

Collaborators

  • matmar10