GithubHelp home page GithubHelp logo

Comments (7)

mgan59 avatar mgan59 commented on July 3, 2024

The schema defined is what is used for deletion/reset and that is the purpose of that attribute. The fixture config loads all the schemas so that they are accessible in the fixtures themselves without having to bind them to mongoose as that would be overly verbose and could lead to duplication which would throw an error.

Based on the code you posted I'm not entirely sure if you are structuring your fixtures/schemas correctly. If you were to create a public repo with a more concrete example I could help identify if there is a valid issue or not.

Cheers.

from mongoose-fixture.

lvarayut avatar lvarayut commented on July 3, 2024

Thanks for you help! I didn't have a lot of things in my fixtures/schemas. The History schema has been structured as show in the previous post. Here is my Product Fixture and my Product schema.

Product Fixture:

/*
 *  File: product.js
 *  Type: DataFixture
 *  Generated by: Mongoose-Fixture (v0.4.1)
 *
 */

module.exports = function(mongoose, conn, callback) {

    // standard callback error
    var error = null;

    // create your data documents using object-literals
    var fixture = [];

    /*
     * Example of adding a data document/fixture item
     */
    fixture.push({
        // by not defining an _id mongoose-fixture
        // will by default set a mongo ObjectID
        // defining one manually will override mongoDB
        name: "Play for Java: Covers Play 2",
        type: "Book",
        description: "Play for Java shows you how to build Java-based web applications using the Play 2 framework...",
        company: "Manning Publications",
        price: 30,
        imagePath: "/public/assets/images/play-for-java.png",
        rating: 4.5,
        category: "Computer",
        author: "Nicolas Leroux",
        publicationDate: "2014-03-31",
        numPage: 320
    });
    return callback(error, fixture);
};

Product Schema:

module.exports = function(mongoose, conn) {
    var ProductSchema = mongoose.Schema({
        name: {
            type: String
        },
        type: {
            type: String
        },
        description: {
            type: String
        },
        company: {
            type: String
        },
        price: {
            type: Number
        },
        imagePath: {
            type: String
        },
        imageName: {
            type: String
        },
        rating: {
            type: Number
        },
        comments: {
            type: Array
        },
        userId: {
            type: String
        },
        category: {
            type: String
        },
        author: {
            type: String
        },
        publicationDate: {
            type: Date
        },
        numPage: {
            type: Number
        }
    });

    mongoose.model('Product', ProductSchema);

    return {
        name: 'Product',
        schema: ProductSchema
    };
};

from mongoose-fixture.

mgan59 avatar mgan59 commented on July 3, 2024

Sorry for the delay. I had a chance to look at this code a little bit. I think you need to adjust your mongoose schemas to use populate so that you can properly nest documents.

Try:

// history.js
module.exports = function(mongoose, conn) {
    var Schema = mongoose.Schema;
    var HistorySchema = new mongoose.Schema({
        ...
        products: {
           type: Schema.Types.ObjectId, 
           ref: 'product', 
           required: true
        }
    });

    // I would recommend not registering your schemas here as it can create
    // a coupling issue when trying to share schemas/models with other services like express
    mongoose.model('History', HistorySchema);

    return {
        name: 'History',
        schema: HistorySchema
    };
};

If you make the schema adjustments everything should work :) Let me know so I can close issue if it works.

from mongoose-fixture.

lvarayut avatar lvarayut commented on July 3, 2024

It worked like a charm! 👍 As your recommendation, where should I register my schemas?

from mongoose-fixture.

mgan59 avatar mgan59 commented on July 3, 2024

I looked at some of my other projects/code and there are some that do it this way and they work perfectly fine. So you are free to do this, but it could lead to a potential issue where an attempt to register a model twice can occur. My suggestion would be during your apps initialization phase have it iterate over all the schemas/models in a directory and require each one and then using the returned {name:'History', schema:HistorySchema} have it register each one. This is how I've done it in the past for express apps, that way all the models are registered already and I pass the conn object around.

By default if you are just running the example above from CLI then mongoose-fixture will automatically register your models during init. So for your above example it should work without the mongoose.model('History', HistorySchema);

Recommend reading, mongoose-fixture uses mongoose.createConnection which is why I recommend using the conn object. There is/was talk to deprecate the older mongoose connection method which is best summed up on StackOverflow. As a note this is how mongoose-fixture can support multiple mongodb connections per #10

from mongoose-fixture.

mgan59 avatar mgan59 commented on July 3, 2024

Marking issue as closed as the main point was addressed :)

from mongoose-fixture.

lvarayut avatar lvarayut commented on July 3, 2024

Thanks a lot for your help :)

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.