Mongoose and the lost subdocument reference

In mongoose subdocuments are embedded in documents. If the document is saved, all its subdocuments are saved too. However, if a save command is issued on a subdocument, a copy of this subdoc is created in its own collection and there is no relationship with the embedded document. The separately saved subdoc is ‘lost’. Updating ...

using mocha with requirejs v2.1.0 in node

Starting with requirejs version 2.1.0, the top level requirejs call requirejs([], function() {}) became asynchronous instead of synchronous. When writting tests using test programs like mocha, you can us this synchronous nature to load the dependencies before executing the tests. A same tests may look like this: var requirejs = require('requirejs'); requirejs.config({ baseUrl: __dirname, nodeRequire: ...

MongoDb and valid field names

Today I had a hard time figuring out why a value wasn’t stored in MongoDB. If you ever encounter MongoError: not okForStorage you’re probably using a dot or $ in the field name. Don’t, it won’t work

Get the classname of a mongoose model instance

Sometimes it is handy to know the name of the Mongoose model when handling an model instance. Mongoose has a model methode ‘modelName’ that holds the name of the model (how obvious). If obj is the instance of a model, you can use obj.constructor.modelName to get the name. Below is a simple example: var CatSchema ...

PHP closure scope trap

Closures in PHP are a valuable addition since version 5.3. I use them quite frequently, an habit from my developments in Javascript I think. However, I was surprised by a result I got today where I applied a closure to implement lazy loading of an object. To explain what happened I’ve created a simple class ...

How to test PHP and go for that extra mile

Testing the PHP core engine is worth the effort as so many people are using it every day. It can become even an addictive job if you can monitor your progress. The coverage of the code is updated when you commit your new tests and can be seen here. It would be nice to be ...

Aura.Di: difference between defs and services

The Aura Project has a nice component Di, Dependency Injector. It enables you the collect classes into a container, together with the parameters needed to instantiate them. Aura.Di has the concept op lazy loading, or better phrased, lazy instantiation. To differentiate between the ‘would-be’ object (= class) and the object itself, it uses the terms ‘defs’ and ‘services’. ...