Source

fantomongo / fan / Collection.fan

Diff from to

File fan/Collection.fan

   }
   
   **
+  **  See `DB.indexExists` for details.
+  **
+  Bool indexExists(Str indexName)
+  {
+     return db.indexExists(this.name, indexName)
+  }
+  
+  **
+  **  Ensures that an index exists in this collection, if it does not it creates it.
+  **
+  Str ensureIndex([Str:Int][] fields, Bool unique := false, Bool dropDups := false)
+  {
+    namestr := indexName(fields)
+    if(indexExists(namestr))
+    {
+       return namestr
+    }
+       return createIndex(fields,unique, dropDups)
+  }
+
+  **
   **  Create a new index in this collection.
   **
-  Str createIndex([Str:Int][] fields, Bool unique := false)
+  Str createIndex([Str:Int][] fields, Bool unique := false, Bool dropDups := false)
   {
     key := [:] { ordered = true }
     fields.each |map| { key.addAll(map) }
     selector := ["name"    : namestr,
                   "ns"     : fullName,
                   "key"    : key,
-                  "unique" : unique]
-                  // todo - dropDups
+                  "unique" : unique,
+                  "dropDups" : dropDups]
+
     Collection(db, "system.indexes").insertDocs([selector], true) // add this in safe mode?
     return namestr
   }
-  
+
   **
   ** Creates the index name used for the given fields.
   ** This is the same name that is returned from `Collection.createIndex`