Skip to content

Commit 31e5206

Browse files
committed
fix incorrect id index in tingo db. ensure index on start even if collection already created
1 parent 3a7022c commit 31e5206

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

src/bouncer/db/tingo-db.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,36 +92,45 @@ module.exports = class TingoDb extends IDb {
9292
return dbDoc
9393
}
9494

95-
async getCollectionNames(name){
95+
async getCollectionNames(){
9696
let names = await promisfy(this.tingo.collectionNames.bind(this.tingo))({})
9797

98-
return names.map(name=>{
99-
return name.replace(this.prefix, '')
98+
return names.map(col=>{
99+
return col.name.replace(this.prefix, '')
100100
})
101101
}
102102

103-
async createCollection(name, indexSettings){
104-
debug('createCollection', name, indexSettings)
105-
106-
if(this.hasCollection(name) !== null){ return }
103+
async ensureIndex(nameOrCollection, indexSettings){
104+
let collection = typeof nameOrCollection == 'string' ? await this.getCollection(nameOrCollection) : nameOrCollection
107105

108-
let collection = await promisfy(this.tingo.createCollection.bind(this.tingo))(this.prefix+name)
109106

110107
indexSettings.indices.map(index=>{
111108
let obj={}
112109
obj[index]=1
113110
collection.createIndex(obj, {unique: false})
114111
})
115112

116-
indexSettings.uniques.map(index=>{
113+
indexSettings.unique.map(index=>{
117114
let obj={}
118115
obj[index]=1
119116
collection.createIndex(obj, {unique: true})
120117
})
121118

122119

123-
collection.createIndex({'$meta.id': 1}, {unique: true})
120+
collection.createIndex({'_id': 1}, {unique: true})
121+
}
122+
123+
async createCollection(name, indexSettings){
124+
debug('createCollection', name, indexSettings)
125+
126+
if(this.hasCollection(name) !== null){
127+
await this.ensureIndex(name, indexSettings)
128+
return
129+
}
130+
131+
let collection = await promisfy(this.tingo.createCollection.bind(this.tingo))(this.prefix+name)
124132

133+
await this.ensureIndex(collection, indexSettings)
125134
}
126135

127136

0 commit comments

Comments
 (0)