Skip to content

Commit

Permalink
fix(db): update index() for array use
Browse files Browse the repository at this point in the history
To be able to set several keys
  • Loading branch information
sogehige committed Mar 26, 2019
1 parent c5f65a5 commit cdb0ba6
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/bot/_interface.ts
Expand Up @@ -298,7 +298,7 @@ class Module {
this.timeouts[`${this.constructor.name}._indexDbs`] = setTimeout(() => this._indexDbs(), 1000);
} else {
// add indexing to settings
global.db.engine.index({ table: this._name + '.settings', index: 'key' });
global.db.engine.index(this._name + '.settings', [{ index: 'key' }]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bot/customvariables.js
Expand Up @@ -21,7 +21,7 @@ class CustomVariables {
if (isMainThread) {
this.addMenuAndListenersToPanel()
this.checkIfCacheOrRefresh()
global.db.engine.index({ table: 'custom.variables.history', index: 'cvarId' })
global.db.engine.index('custom.variables.history', [{ index: 'cvarId' }])
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/bot/databases/interface.js
Expand Up @@ -87,12 +87,12 @@ class Interface {
/**
* Sets up index of table
* note: Indexes should be set only at start on master
* @param {string} table table for indexing
* @param {object} opts options for indexing
* @param {string} opts.table table for indexing
* @param {string} opts.index index column
* @param {boolean} opts.unique index should be unique
*/
async index (opts) {
async index (table, opts) {
throw Error('function index() is not implemented in ' + this.constructor.name)
}

Expand Down
4 changes: 2 additions & 2 deletions src/bot/databases/master.js
Expand Up @@ -132,9 +132,9 @@ class IMasterController extends Interface {
})
}

async index (opts) {
async index (table, opts) {
const id = crypto.randomBytes(64).toString('hex')
const data = { type: 'db', fnc: 'index', opts, id }
const data = { type: 'db', fnc: 'index', opts: [table, opts], id }

return new Promise((resolve, reject) => {
this.sendRequest(resolve, reject, id, data)
Expand Down
19 changes: 12 additions & 7 deletions src/bot/databases/mongodb.js
Expand Up @@ -31,17 +31,22 @@ class IMongoDB extends Interface {
this.connect()
}

async index (opts) {
opts.unique = opts.unique || false
if (!opts.index) throw new Error('Missing index option')
if (!opts.table) throw new Error('Missing table option')
async index (table, opts) {
if (!table) throw new Error('Missing table option')

try {
if (!this.connected) throw new Error('Not connected yet')
let db = await this.client.db(this.dbName)
await db.createCollection(opts.table)
await db.collection(opts.table).dropIndexes()
await db.collection(opts.table).createIndex(opts.index, { unique: opts.unique })
await db.createCollection(table)
await db.collection(table).dropIndexes()

if (!Array.isArray(opts)) opts = [opts]
for (const o of opts) {
o.unique = o.unique || false
if (!o.index) throw new Error('Missing index option')
await db.collection(table).createIndex(o.index, { unique: o.unique })
}

return
} catch (e) {
// indexes will be created when collection is available
Expand Down
17 changes: 10 additions & 7 deletions src/bot/databases/nedb.js
Expand Up @@ -22,13 +22,16 @@ class INeDB extends Interface {
this.table = {}
}

async index (opts) {
opts.unique = opts.unique || false
if (!opts.index) throw new Error('Missing index option')
if (!opts.table) throw new Error('Missing table option')

await this.on(opts.table).removeIndex(opts.index)
await this.on(opts.table).ensureIndex({ fieldName: opts.index, unique: opts.unique })
async index (table, opts) {
if (!table) throw new Error('Missing table option')

await this.on(table).removeIndex()
if (!Array.isArray(opts)) opts = [opts]
for (const o of opts) {
o.unique = o.unique || false
if (!o.index) throw new Error('Missing index option')
await this.on(table).ensureIndex({ fieldName: o.index, unique: o.unique })
}
}

on (table) {
Expand Down
2 changes: 1 addition & 1 deletion src/bot/overlays/emotes.js
Expand Up @@ -138,7 +138,7 @@ class Emotes extends Overlay {
}
super({ settings, ui })
if (isMainThread) {
global.db.engine.index({ table: this.collection.cache, index: 'code' })
global.db.engine.index(this.collection.cache, { index: 'code' })
setTimeout(() => {
if (!this.fetch.global) this.fetchEmotesGlobal()
if (!this.fetch.subscribers) this.fetchEmotesSubsribers()
Expand Down
4 changes: 2 additions & 2 deletions src/bot/overlays/goals.ts
Expand Up @@ -25,8 +25,8 @@ class Goals extends Overlay {
this.addMenu({ category: 'registry', name: 'goals', id: '/registry/goals/list' });

if (isMainThread) {
global.db.engine.index({ table: this.collection.groups, index: 'uid', unique: true });
global.db.engine.index({ table: this.collection.goals, index: 'uid', unique: true });
global.db.engine.index(this.collection.groups, { index: 'uid', unique: true });
global.db.engine.index(this.collection.goals, { index: 'uid', unique: true });
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/bot/systems/polls.ts
Expand Up @@ -54,8 +54,8 @@ class Polls extends System {
this.lastTimeRemind = 0;

if (isMainThread) {
global.db.engine.index({ table: this.collection.votes, index: 'vid' });
global.db.engine.index({ table: this.collection.data, index: 'openedAt' });
global.db.engine.index(this.collection.votes, { index: 'vid' });
global.db.engine.index(this.collection.data, { index: 'openedAt' });

setInterval(() => this.reminder(), 1000);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bot/workers.ts
Expand Up @@ -215,7 +215,7 @@ class Workers {
data.items = await global.db.engine.update(data.table, data.where, data.object);
break;
case 'index':
data.items = await global.db.engine.index(data.opts);
data.items = await global.db.engine.index(...data.opts);
break;
case 'count':
data.items = await global.db.engine.count(data.table, data.where, data.object);
Expand Down

0 comments on commit cdb0ba6

Please sign in to comment.