diff --git a/server/augmented/database.js b/server/augmented/database.js index b3e3384..aa52778 100644 --- a/server/augmented/database.js +++ b/server/augmented/database.js @@ -51,12 +51,14 @@ } // only allows wildcards i.e. "user:*" - db.keys = db.keys || (function () { + db.keys = db.keys || db.scan || (function () { switch (dbType) { case 'mongo': return mongoKeys; case 'redis': return db.client.keys; + case 'postgres': + return postgresKeys; } }()); @@ -77,6 +79,24 @@ }); } + function postgresKeys(key, callback) { + if (key.startsWith('*')) { + key = '%' + key.substring(1); + } + if (key.endsWith('*')) { + key = key.substring(0, key.length - 1) + '%'; + } + + db.client.query({ + text: ` + SELECT o."_key" + FROM "legacy_object_live" o + WHERE o."_key" LIKE '${key}'`, + }) + .then(res => callback(null, res && res.rows ? res.rows.map(r => r._key) : [])) + .catch(err => callback(err)); + } + function setupConfigs() { // nconf defaults, if not set in config if (!nconf.get('sessionKey')) {