Comments (7)
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.
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.
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.
It worked like a charm! 👍 As your recommendation, where should I register my schemas?
from mongoose-fixture.
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.
Marking issue as closed as the main point was addressed :)
from mongoose-fixture.
Thanks a lot for your help :)
from mongoose-fixture.
Related Issues (20)
- Cluster support for mongodb HOT 20
- fixture itemName/collection name must be same? HOT 4
- Improve CLI HOT 1
- Review the current interface for fixture loading within an app
- Auto generate data-fixtures using the defined mongo-database
- Improve CLI interface or Mongoose-Config to support node-conf for DEV, QA, PROD mongo connections
- Versioning for Mongoose Schemas
- Error for duplicate models from config
- Zealous Exception handling during (reset || remove)
- Update Data Fixtures
- Fixture Helper Service
- Unable to create fixture-listings with model dependencies not in loading scope
- How to add a data fixture for GridFS? HOT 4
- Difference in schema between mongoose and mongoose fixture HOT 2
- TypeError: Cannot call method 'create' of undefined
- Getting 'Arguments to path.resolve must be strings' for every command HOT 5
- Add an example that uses mongodb references HOT 16
- Improve comment added to generated fixture file / boilerplate HOT 2
- Implment simpler interface for collections and references HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose-fixture.