Browse Source

Feat: Allow empty namespaces in stores

redis has not been updated

Signed-off-by: Jytesh <44925963+Jytesh@users.noreply.github.com>
emp
Jytesh 3 years ago
parent
commit
1cddd4094a
  1. 4
      packages/keyv-mongo/src/index.js
  2. 4
      packages/keyv-sql/src/index.js
  3. 2
      packages/keyv/src/index.js

4
packages/keyv-mongo/src/index.js

@ -96,13 +96,13 @@ class KeyvMongo extends EventEmitter {
clear() {
return this.connect
.then(store => store.deleteMany({ key: new RegExp(`^${this.namespace}:`) })
.then(store => store.deleteMany({ key: new RegExp(`^${this.namespace}`) })
.then(() => undefined));
}
async * iterator() {
const iterator = await this.connect
.then(store => store.find({ key: new RegExp(`^${this.namespace}:`) }).map(x => {
.then(store => store.find({ key: new RegExp(`^${this.namespace}`) }).map(x => {
return [x.key, x.value];
}));
yield * iterator;

4
packages/keyv-sql/src/index.js

@ -65,14 +65,14 @@ class KeyvSql extends EventEmitter {
}
clear() {
const del = this.options.dialect === 'mysql' ? `DELETE FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` LIKE '${this.namespace}:%')` : `DELETE FROM "${this.options.table}" WHERE ("${this.options.table}"."key" LIKE '${this.namespace}:%')`;
const del = this.options.dialect === 'mysql' ? `DELETE FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` LIKE '${this.namespace}%')` : `DELETE FROM "${this.options.table}" WHERE ("${this.options.table}"."key" LIKE '${this.namespace}%')`;
return this.query(del)
.then(() => undefined);
}
async * iterator() {
const limit = Number.parseInt(this.options.iterationLimit, 10);
const selectChunk = this.options.dialect === 'mysql' ? `SELECT * FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` LIKE '${this.namespace}:%') LIMIT ${limit} OFFSET ` : `SELECT * FROM "${this.options.table}" WHERE ("${this.options.table}"."key" LIKE '${this.namespace}:%') LIMIT ${limit} OFFSET `;
const selectChunk = this.options.dialect === 'mysql' ? `SELECT * FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` LIKE '${this.namespace}%') LIMIT ${limit} OFFSET ` : `SELECT * FROM "${this.options.table}" WHERE ("${this.options.table}"."key" LIKE '${this.namespace}%') LIMIT ${limit} OFFSET `;
async function * iterate(offset, query) {
const entries = await query(selectChunk + offset);

2
packages/keyv/src/index.js

@ -25,7 +25,7 @@ class Keyv extends EventEmitter {
this.store.on('error', error => this.emit('error', error));
}
this.store.namespace = this.options.namespace;
this.store.namespace = this.options.namespace ? this.options.namespace + ':' : '';
const generateIterator = iterator => async function * () {
for await (const [key, raw] of (typeof iterator === 'function' ? iterator() : iterator)) {

Loading…
Cancel
Save