Last active
December 24, 2015 07:29
-
-
Save clemos/6764489 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import js.npm.mongoose.Model; | |
| import js.npm.mongoose.Schema; | |
| import js.npm.Mongoose; | |
| class TestMongoose { | |
| public static function main(){ | |
| Mongoose._.connect( 'mongodb://localhost/test' , cast start ); | |
| } | |
| static function start(err){ | |
| if( err != null ){ | |
| trace(err); | |
| return; | |
| } | |
| var schema = new Schema({ | |
| title : String, | |
| content : String, | |
| date : Date | |
| }); | |
| var model : Models<Dynamic> = Mongoose._.model('Article',schema); | |
| // créer un article | |
| model.create({ title : 'My New Article', content : 'Bla bla'}, function(err, article){ | |
| if( err == null || article == null ){ | |
| throw "Error saving article..."; | |
| } | |
| // mise à jour de l'article | |
| article.date = Date.now(); | |
| article.save(); | |
| }); | |
| } | |
| } |
Author
Author
I've updated the example with code to connect to a local mongo instance.
Also, regarding Haxe typing (to address your error with Null<js.npm.mongoose.Model<js.npm.mongoose.Schema>> has no field date)
... I just commited a change so Document (and thus Model) implements Dynamic
This should remove the error, and makes sense until we find a proper way to type Documents / Schemas / Models ...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok sorry for the late reply
but I actually didn't get any notification or email whatsoever.
Anyway...
js.npm.Mongoose._is just what is returned byrequire('mongoose')Calling
require('mongoose')in JS actually returns an instance ofMongoose(iejs.npm.mongoose.Mongoosein Haxe/JS), I guess because most setups will only use one Mongoose instance.It's a kind of weird singleton pattern used by the library maintainters...
So you can indeed connect to your database using
Mongoose._.connect, and then useMongoose._.model, etcI decided to 'translate' the pattern this way in Haxe...
I could have made these static methods of
js.npm.Mongooseto simplify usability, but it would have required to duplicate signatures injs.npm.Mongoose(static) andjs.npm.mongoose.Mongoose(instance methods).I believe some day I'll just remove this possibility, to enforce users to instanciate Mongoose themselves like this (which is IMHO cleaner and feels less hacky from a Haxe POV) :
As for this error you get, I don't really manage to reproduce...
Now the extern is not 100% complete / tested for sure, so you may indeed need to
casthere and there.Don't hesitate to file issues in case you believe it needs to be fixed.
PS:
I re-read the source from which I pulled this example,
and realize the
modelvariable is actually typed js.npm.mongoose.Model.ModelsThis should do the trick.
I'll change the example accordingly
About
saveit indeed requires a callback,At least, the extern does,
Not sure if mongoose actually requires it...