Iridium
Iridium copied to clipboard
Index decorator not working
Working with [email protected]
on v8.11.3
and 5.6.0
I want to make an unique index of a collection with @Index
decorator. But it seems not working, when I inserted the same object into the collection, it didn't throw any error.
import { Collection, Core, Index, Instance, Model, ObjectID, Property } from 'iridium';
interface Colour {
r: number;
g: number;
b: number;
}
interface Car {
make: string;
model: string;
colour: Colour;
}
interface HouseDocument {
_id?: string;
name: string;
cars?: Car[];
}
@Index({ name: 1 }, {unique: true}) // It seems not work
@Collection('houses')
class House extends Instance<HouseDocument, House> implements HouseDocument {
@ObjectID _id: string;
@Property(/^.+$/)
name: string;
@Property([{
make: String,
model: String,
colour: {
r: Number,
g: Number,
b: Number
}
}])
cars: Car[];
static onCreating(doc: HouseDocument) {
doc.cars = doc.cars || [];
}
addCar(make: string, model: string, colour: Colour) {
this.cars.push({
make: make,
model: model,
colour: colour
});
}
get numberOfCars() {
return this.cars.length;
}
}
class MyDatabase extends Core {
Houses = new Model<HouseDocument, House>(this, House);
}
var myDb = new MyDatabase({ database: 'houses_test' });
myDb.connect()
// I have to use this:
// .then(() => myDb.Houses.ensureIndex({name: 1}, { unique: true }))
.then(() => myDb.Houses.insert({
name: 'My House',
cars: [{
make: 'Audi',
model: 'A4',
colour: { r: 0, g: 0, b: 0 }
}]
}))
.then(() => myDb.Houses.insert({
name: 'My House',
cars: [{
make: 'Audi',
model: 'A4',
colour: { r: 0, g: 0, b: 0 }
}]
}))
.then(() => myDb.close())
Hey @datdev98, you need to call Model.ensureIndexes, checkout previous issue for details https://github.com/SierraSoftworks/Iridium/issues/80
there is also an example of it in the readme https://github.com/SierraSoftworks/Iridium/tree/release/8.0.0#example-core-implementation