Closed ReneHollander closed 8 years ago
As of the current version of Camo (v0.8.0
), there is not a way to set a unique index within the schema declaration. Although, it is still possible to set an index using the underlying node-mongodb-native database object.
One way to do this would be:
var getClient = require('camo').getClient;
// Retrieve the underlying MongoDB object with getClient().driver()
getClient().driver().collection('users').createIndex('email', {unique: true}, function(err, indexName) {
// Index created with name indexName
});
I'm hoping to add this feature to the schema declaration within the next few weeks or so, so hopefully you won't have to deal with this mess for too long.
Let me know if you have any questions.
Scott
Hello!
Thanks for your answer. I forgot to add that I use NeDB, but I should be able to figure it out by myself. The mess is not a problem for me for now. I am just doing a small throw away project.
Rene
I tried to create the index with help of the NeDB docs (https://github.com/louischatriot/nedb#indexing). But I discovered that camo.getClient().driver() returns an empty object. I made sure that the actual call is done after the db is connected.
Hi Rene,
Ok, so I looked in to this a bit more and remembered that Camo does lazy creation of collections. So the NeDB collection object won't be created until after you try to load
, save
, or delete
something, which means the object returned by camo.getClient().driver()
will be empty until after you load/save/delete.
I'll put this on the todo list to figure out a better way to do it and let you know when it's fixed. Thanks!
Scott
Any plan to update?) It's node 4.0 today. Generators, promisses, arrow functions out the box.
Yup, I'll be testing and upgrading the engine
field in package.json today
or tomorrow.
Scott
Any plan to update?) It's node 4.0 today. Generators, promisses, arrow functions out the box.
— Reply to this email directly or view it on GitHub https://github.com/scottwrobinson/camo/issues/12#issuecomment-150832422.
FYI, this was added back in v0.10.0. You can specify the 'unique' constraint like this:
class Company extends Document {
constructor() {
super();
this.name = {
type: String,
unique: true
};
}
}
Hello!
I want to set an unique index to an specific schema entry. Is this possible with camo?
Thanks in advance, Rene Hollander