A module for node.js.
A module for node.js.
This is a library that makes saving objects to mongodb easy and transparent. It also can load default values for objects from jsons files.
This code was originally part of a game I am working on, and so I find it usefull for loading units, teams, etc into a database from json files. It should be considered pre-alpha quality, and will be subject to massive and sweeping changes. I haven't tested it extensively, but as I use it more, bugs will get ironed out and functionality added.
Basically persister will allow you to do things like this:
var rob =id: "rob" //required fieldname: "Robotic Operating Buddy"wikipediaUrl: ".";rob;// rob is now saved in the database. note that we don't yet have the mongodb// _id for him, but we can search for him using using mongodb queriesRobot;;
All you need to do is add a robot.js file in the right place using the one found below as a template, and this will be auto registered with mongo as a collection.
The id field is required, and every persist operation is an update-or-insert. If a record with the same id exists in the table robot, it will be updated. Otherwise, a new record is inserted.
First create a new node project. Your package.json should be something like this:
Create a directory in your project root called configs, and inside that, create a directory called countries. Then put the following json file in it: https://raw.github.com/gordlea/persister/master/tests/configs/countries/countries.json
Create another directory in your project root called objects. This is where we will put our persistable objects. Lets add two persistable objects:
var Persistable = Persistable;var _ = ;var Country = Persistable;Persistable;moduleexports = Country;
var Persistable = Persistable;var _ = ;var Robot = Persistable;Persistable;moduleexports = Robot;
Now create an example.js with the following in it:
var path = ;var assert = ;var Robot = ;var persister = ;// persister fires the ready event when it has registered all of your// collections and connected to the databasepersister;persisterstartpersistableObjectDir: pathpersistableObjectConfigDir: pathdbConfig:db: "persister-example"port:27017host:'localhost';