-
-
Save clemos/6764489 to your computer and use it in GitHub Desktop.
| 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(); | |
| }); | |
| } | |
| } |
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 by require('mongoose')
Calling require('mongoose') in JS actually returns an instance of Mongoose (ie js.npm.mongoose.Mongoose in 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 use Mongoose._.model, etc
I decided to 'translate' the pattern this way in Haxe...
I could have made these static methods of js.npm.Mongoose to simplify usability, but it would have required to duplicate signatures in js.npm.Mongoose (static) and js.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) :
var db = new js.npm.mongoose.Mongoose();
db.connect('...');
var article = db.model( new Schema( { ... } ) );
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 cast here 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 model variable is actually typed js.npm.mongoose.Model.Models
This should do the trick.
I'll change the example accordingly
About save it indeed requires a callback,
At least, the extern does,
Not sure if mongoose actually requires it...
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 ...
Nice! But I have some questions:
mongoose.connect("mongodb://localhost/test");?Null<js.npm.mongoose.Model<js.npm.mongoose.Schema>> has no field datefor the 23th line. Need a cast ?And looks like you can avoid to put a function as a parameter for
save()function.Thanks again!