Tagged: mongoose

Sometimes you have to fail hard

This was a post I wrote in the middle of 2013 but never published. I wanted to share this since it’s a common story across all technologies and developers of all skill levels. Sometimes things really just don’t work. As a post-script, I did come back to this project and had a lot of success. When in doubt, let time figure it out :)


For the last couple of weeks, I’ve been trying my hand at the node.js ecosystem. I had an app idea but I wanted to make sure I chose tech stacks wisely. There’s no better way to get familiar with different stacks than to get your hands dirty and try them all, so that’s what I did.

Sometimes when you start with a new language or platform things come easy, you can blaze a burning trail writing great software. You’re like an extension of the computer, everything … Read more

Mongoose with TypeScript

Mongoose is a library for node.js that wraps the mongoDB driver. Since I’ve been playing with typescript, I wanted to show a short demo of strongly typing mongoose with unit tests run in nodeunit all using typescript.

Definitions

First, I have a collection of definition files that represent mongoose types, nodeunit types, and my own document types (in schemaDef.d.ts).

/def/all.d.ts

[ts]
///<reference path="./mongoose.d.ts"/>
///<reference path="./nodeUnit.d.ts"/>
///<reference path="./schemaDef.d.ts"/>
[/ts]

The nodeunit definitions /def/nodeUnit.d.ts

[ts]
interface ITest{
done(): void;
ok(isGood:Boolean, message?:string):void;
equal(expected:any, actual:any, message?:string);
}
[/ts]

Here are the basic mongoose definitions in /def/mongoose.d.ts. I can’t guarantee that these types are right, I’m updating them as I go along. I’m inferring the structure from the documentation and personal experimentation. As I encounter new mongoose definitions, I can just add them to the appropriate scope. You can see that I’m also chaining definitions: one definitions functions might return another interface that has … Read more