GithubHelp home page GithubHelp logo

Comments (16)

mgan59 avatar mgan59 commented on July 3, 2024

Thanks for the example I like it and will clean it up and add it to the collection. Want to put a bunch of this on the project wiki. I also have an example of using async to pull documents already in mongo to do nesting, it is slightly more complicated for showing nested/embed documents. So yours is a good example that may be easier for some to follow.

Thanks.

from mongoose-fixture.

hal-gh avatar hal-gh commented on July 3, 2024

You're welcome, @mgan59 :)

from mongoose-fixture.

hal-gh avatar hal-gh commented on July 3, 2024

Of course, this example does not accommodate references to documents in other collections. Is there any chance you could give a quick example or how to go about that?

from mongoose-fixture.

mgan59 avatar mgan59 commented on July 3, 2024

Been meaning to do a writeup about how I'm using this project to stub data into my video game project. So here is a small snippet.

The thing to note is I use the async module and by passing in the exportCallback into the module I can then return in the final async callback return exportCallback(null, {dataFixtures:fixture}); keep in mind you need to mount your fixture array into an object literal key of dataFixtures so that the fixture-loader can do it's job.

/*
 *  File: PlanetStructures.js
 *  Generated by: Mongoose-Fixture (v0.1.3)
 *
 */

var async = require('async');


// callback comes in
module.exports = function(mongoose, exportCallback){
    var fixture = [];

    // Dig into mongoose to extract our already loaded Facility model
    // why we pass mongoose into fixture now
    var FacilityModel = mongoose.models.Facility;

    // setup mganic planet fixture
    var planetMganic = {
        internalName:'Mganic',
        planetType:'earthly',
        aggregateProduction:[],
        facilities:[]
    };

    // setup planet faren fixture
    var planetFaren = {
        internalName:'faren',
        name:'Faren',
        planetType:'desert',
        facilities:[]
    };

    async.series([
        function(callback){
            // async call that loads facility data for planet mganic
            var mganicFacilityQuery = FacilityModel.find(
                {'headQuarters.planet._id':'mganic'}, 
                'name headQuarters.planet internalName', 
                function(err,items){

                    // iterate over items and push into planets
                    // facility array
                    var item=null,docEmbed=null;
                    for(var ctr=0; ctr < items.length; ctr++){
                        item = items[ctr];
                        planetMganic.facilities.push(item);
                    }

                    fixture.push(planetMganic);
                    return callback(null,fixture);
                }
            ); 
        },
        function(callback){
            // TODO add planet farens facilities using a query
            // async call that loads facility data for planet mganic
            var facilityQuery = FacilityModel.find(
                {'headQuarters.planet._id':'faren'}, 
                'name headQuarters.planet internalName', 
                function(err,items){

                    // iterate over items and push into planets
                    // facility array
                    var item=null,docEmbed=null;
                    for(var ctr=0; ctr < items.length; ctr++){
                        item = items[ctr];
                        planetFaren.facilities.push(item);
                    }

                    fixture.push(planetFaren);
                    return callback(null,fixture);
                }
            );
        }
    ],
        function(err, results){
            // err and [{data},{data}]
            // TODO once results are the actual fixture items combine
            // dont use the global version
            return exportCallback(null, {dataFixtures:fixture});

        }
    );


};

from mongoose-fixture.

hal-gh avatar hal-gh commented on July 3, 2024

Thanks for this. I will try it out tomorrow.
Are you aware that only the commandline --add argument works - the reset and remove arguments have no effect?

from mongoose-fixture.

mgan59 avatar mgan59 commented on July 3, 2024

Hhmm the reset should work as that is what I use 99% of the time.

Do u have an error message or does it hang and u have to ctrl+brk?

Can u give me env details? Win/mac/linux also what version of mongoose? I need to update mongoose for the lib it is at 0.3.5.10 and 0.3.6.20 is the latest and I know ut hangs which is why I havent uodated the package on my todo list.

from mongoose-fixture.

hal-gh avatar hal-gh commented on July 3, 2024

Good point! To be fair, I neglected to check your package's requirements. :/
My project's package.json file specifies "mongoose": "2.7.x", but surely your package uses it's own dependency, which is set at "3.5.7"?
I'm using Ubuntu 12.04 and the project is using node v0.10.18.

from mongoose-fixture.

hal-gh avatar hal-gh commented on July 3, 2024

Btw, no error message is generated. The data simply loads as if --add was used.

from mongoose-fixture.

mgan59 avatar mgan59 commented on July 3, 2024

The versioning shouldn't be an issue unless you used npm link on a mongoose install which is why I asked, just in case.

Are you having this `--reset`` issue with the above snippet? If so I'll try and pulling it down this evening and taking a look at it.

from mongoose-fixture.

mgan59 avatar mgan59 commented on July 3, 2024

I created a test repo

And in the process found your problem. Inside your mongoose-fixture-config specifically in your fixture listing

// Create a Listing of fixtures
var allFixtures = [
    {
        // general name used in output log
        itemName:'Family',
        // name of the schema file (without the .js)
        schema:'FamilySchema',
        // name of the data-fixture file (without the .js)
        data:'Family',
        // collection name in for removal process
        // Note that mongoose will pluralize this name to "families" when it creates the collection
        collection:'family' 
    }
];

You need to change the line

collection:'family'

To the Following view repo

collection:'families'

Mongoose ORM by default makes your model's collection plural which causes the remove process of the fixture loader to not remove correctly since the collection names don't match.

I'm going to put in an exception handler to catch this issue going forward as some error should of propogated upwards to indicate that the fixture-removal process failed, instead it was saying it completed.

So thanks for finding this issue will update the code/tests and close this issue once I have a new release on npm. For now you should just be able to match the collection name accordingly.

from mongoose-fixture.

hal-gh avatar hal-gh commented on July 3, 2024

Oh, I thought I'd changed that. :/ Apologies, and thank you! That's great about the new exception handler - I think it will make it much more user-friendly.

In the automatically-generated example schema (shown below), is there any reason why you have chosen for the _id a String type rather than ObjectId, which requires less storage and is theoretically more performant? As the id will be automatically generated with ObjectId, maybe it would be better to replace that line with a comment that explains that there is no need to define it. Alternatively, make the mongoose ObjectId type available in the module, as I have done in my code above.

    var exportSchema = mongoose.Schema({
        /* 
           this is an example, fillin with
           your own schema data
        */
        _id:{type:String},
        name:{type:String},
        tags:{type:Array},
        score:{type:Number}
    });

Really good work - thanks again for all your effort! :)

from mongoose-fixture.

mgan59 avatar mgan59 commented on July 3, 2024

You're exactly right about the _id field it should be removed because by default mongoose-fixture will let the mongoose orm set the ObjectId all on its own. And I should change the comment to reflect that is what will happen by default. I think I made the change to the boilerplate for fixtures and must of forgot to do the change for the schemas.

I have a list of things to do for the 0.2.3 release and all this will be in it. Thanks again for helping find these issues, like you said it will ultimately make the library more user-friendly.

from mongoose-fixture.

hal-gh avatar hal-gh commented on July 3, 2024

The approach shown in #5 (comment) is probably very versatile, but it seems to rely on data that already exists in the database. Sorry to burden you, but it would be really useful to see a full "references" example where the data is purely described in text files or scripts.

Also, is there any possibility that an interface like the following could be supported (with all collections in a single file)? I feel that this would make the learning curve very shallow and, consequently, allow far greater adoption.

// fixture object - single letter for brevity
var o = {};

// collections
o.families = {};
o.people = {};

// documents
o.families.smith   = { name: "Smith" };
o.families.jones   = { name: "Jones" };
o.families.connors = { name: "Connors" };
o.people.peter     = { name: "Peter", family: o.families.smith };
o.people.catherine = { name: "Catherine", family: o.families.connors, parents: [ { name: o.people.peter.name } ] };

console.log("Peter's family: " + o.people.peter.family.name);
console.log("Catherine's family: " + o.people.catherine.family.name);
console.log("Catherine's parent(s): " + o.people.catherine.parents[0].name);

from mongoose-fixture.

mgan59 avatar mgan59 commented on July 3, 2024

completed this implementation and the error handling as part 0.2.3. closing.

from mongoose-fixture.

hal-gh avatar hal-gh commented on July 3, 2024

Hi there! It's good to see the project progressing so quickly. Also, thanks for the credit! :)

Could you look at the example code I gave in #5 (comment) . I don't believe this interface has been implemented (note how the collections are declared, which could be determined using lodash's _.keys(object) method).

This interface may well be quite difficult to implement or there may be some reasons you can think of why it wouldn't be practical or versatile which outweigh it's ease of use. If so, and if you do not object, I think it's worth discussing.

from mongoose-fixture.

hal-gh avatar hal-gh commented on July 3, 2024

I will open another issue devoted to the approach in #5 (comment)

from mongoose-fixture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.