feat: test psql without defineProperty (#7815)

* feat: test psql without defineProperty

* feat: refactor psql

remove .bind calls, use module.pool.query directly
move requires to top of file
move promisify to bottom so .init etc are promisified

* feat: mongodb

move requires to bottom

* feat: redis
This commit is contained in:
Barış Soner Uşaklı
2019-08-05 09:20:00 -04:00
committed by GitHub
parent 52a2e5d61d
commit af1f7249a7
33 changed files with 301 additions and 341 deletions

View File

@@ -1,6 +1,6 @@
'use strict';
module.exports = function (db, module) {
module.exports = function (module) {
var helpers = require('./helpers');
module.setObject = async function (key, data) {
@@ -13,11 +13,10 @@ module.exports = function (db, module) {
}
await module.transaction(async function (client) {
var query = client.query.bind(client);
const dataString = JSON.stringify(data);
async function setOne(key) {
await helpers.ensureLegacyObjectType(client, key, 'hash');
await query({
await client.query({
name: 'setObject',
text: `
INSERT INTO "legacy_hash" ("_key", "data")
@@ -41,11 +40,10 @@ module.exports = function (db, module) {
}
await module.transaction(async function (client) {
var query = client.query.bind(client);
const valueString = JSON.stringify(value);
async function setOne(key) {
await helpers.ensureLegacyObjectType(client, key, 'hash');
await query({
await client.query({
name: 'setObjectField',
text: `
INSERT INTO "legacy_hash" ("_key", "data")
@@ -69,7 +67,7 @@ module.exports = function (db, module) {
return null;
}
const res = await db.query({
const res = await module.pool.query({
name: 'getObject',
text: `
SELECT h."data"
@@ -90,7 +88,7 @@ SELECT h."data"
return [];
}
const res = await db.query({
const res = await module.pool.query({
name: 'getObjects',
text: `
SELECT h."data"
@@ -112,7 +110,7 @@ SELECT h."data"
return null;
}
const res = await db.query({
const res = await module.pool.query({
name: 'getObjectField',
text: `
SELECT h."data"->>$2::TEXT f
@@ -133,7 +131,7 @@ SELECT h."data"->>$2::TEXT f
return null;
}
const res = await db.query({
const res = await module.pool.query({
name: 'getObjectFields',
text: `
SELECT (SELECT jsonb_object_agg(f, d."value")
@@ -165,7 +163,7 @@ SELECT (SELECT jsonb_object_agg(f, d."value")
return [];
}
const res = await db.query({
const res = await module.pool.query({
name: 'getObjectsFields',
text: `
SELECT (SELECT jsonb_object_agg(f, d."value")
@@ -190,7 +188,7 @@ SELECT (SELECT jsonb_object_agg(f, d."value")
return;
}
const res = await db.query({
const res = await module.pool.query({
name: 'getObjectKeys',
text: `
SELECT ARRAY(SELECT jsonb_object_keys(h."data")) k
@@ -216,7 +214,7 @@ SELECT ARRAY(SELECT jsonb_object_keys(h."data")) k
return;
}
const res = await db.query({
const res = await module.pool.query({
name: 'isObjectField',
text: `
SELECT (h."data" ? $2::TEXT AND h."data"->>$2::TEXT IS NOT NULL) b
@@ -253,7 +251,7 @@ SELECT (h."data" ? $2::TEXT AND h."data"->>$2::TEXT IS NOT NULL) b
return;
}
await db.query({
await module.pool.query({
name: 'deleteObjectFields',
text: `
UPDATE "legacy_hash"
@@ -281,14 +279,13 @@ UPDATE "legacy_hash"
}
return await module.transaction(async function (client) {
var query = client.query.bind(client);
if (Array.isArray(key)) {
await helpers.ensureLegacyObjectsType(client, key, 'hash');
} else {
await helpers.ensureLegacyObjectType(client, key, 'hash');
}
const res = await query(Array.isArray(key) ? {
const res = await client.query(Array.isArray(key) ? {
name: 'incrObjectFieldByMulti',
text: `
INSERT INTO "legacy_hash" ("_key", "data")

View File

@@ -1,6 +1,6 @@
'use strict';
module.exports = function (db, module) {
module.exports = function (module) {
var helpers = require('./helpers');
module.listPrepend = async function (key, value) {
@@ -9,9 +9,8 @@ module.exports = function (db, module) {
}
await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectType(client, key, 'list');
await query({
await client.query({
name: 'listPrepend',
text: `
INSERT INTO "legacy_list" ("_key", "array")
@@ -29,9 +28,8 @@ DO UPDATE SET "array" = ARRAY[$2::TEXT] || "legacy_list"."array"`,
}
await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectType(client, key, 'list');
await query({
await client.query({
name: 'listAppend',
text: `
INSERT INTO "legacy_list" ("_key", "array")
@@ -48,7 +46,7 @@ DO UPDATE SET "array" = "legacy_list"."array" || ARRAY[$2::TEXT]`,
return;
}
const res = await db.query({
const res = await module.pool.query({
name: 'listRemoveLast',
text: `
WITH A AS (
@@ -75,7 +73,7 @@ RETURNING A."array"[array_length(A."array", 1)] v`,
return;
}
await db.query({
await module.pool.query({
name: 'listRemoveAll',
text: `
UPDATE "legacy_list" l
@@ -95,7 +93,7 @@ UPDATE "legacy_list" l
stop += 1;
await db.query(stop > 0 ? {
await module.pool.query(stop > 0 ? {
name: 'listTrim',
text: `
UPDATE "legacy_list" l
@@ -133,7 +131,7 @@ UPDATE "legacy_list" l
stop += 1;
const res = await db.query(stop > 0 ? {
const res = await module.pool.query(stop > 0 ? {
name: 'getListRange',
text: `
SELECT ARRAY(SELECT m.m
@@ -167,7 +165,7 @@ SELECT ARRAY(SELECT m.m
};
module.listLength = async function (key) {
const res = await db.query({
const res = await module.pool.query({
name: 'listLength',
text: `
SELECT array_length(l."array", 1) l

View File

@@ -1,17 +1,15 @@
'use strict';
module.exports = function (db, module) {
var helpers = require('./helpers');
var query = db.query.bind(db);
module.exports = function (module) {
const helpers = require('./helpers');
module.flushdb = async function () {
await query(`DROP SCHEMA "public" CASCADE`);
await query(`CREATE SCHEMA "public"`);
await module.pool.query(`DROP SCHEMA "public" CASCADE`);
await module.pool.query(`CREATE SCHEMA "public"`);
};
module.emptydb = async function () {
await query(`DELETE FROM "legacy_object"`);
await module.pool.query(`DELETE FROM "legacy_object"`);
};
module.exists = async function (key) {
@@ -20,7 +18,7 @@ module.exports = function (db, module) {
}
if (Array.isArray(key)) {
const res = await query({
const res = await module.pool.query({
name: 'existsArray',
text: `
SELECT o."_key" k
@@ -32,7 +30,7 @@ module.exports = function (db, module) {
return res.rows.some(r => r.k === k);
});
}
const res = await query({
const res = await module.pool.query({
name: 'exists',
text: `
SELECT EXISTS(SELECT *
@@ -49,7 +47,7 @@ module.exports = function (db, module) {
return;
}
await query({
await module.pool.query({
name: 'delete',
text: `
DELETE FROM "legacy_object"
@@ -63,7 +61,7 @@ DELETE FROM "legacy_object"
return;
}
await query({
await module.pool.query({
name: 'deleteAll',
text: `
DELETE FROM "legacy_object"
@@ -77,7 +75,7 @@ DELETE FROM "legacy_object"
return;
}
const res = await query({
const res = await module.pool.query({
name: 'get',
text: `
SELECT s."data" t
@@ -99,9 +97,8 @@ SELECT s."data" t
}
await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectType(client, key, 'string');
await query({
await client.query({
name: 'set',
text: `
INSERT INTO "legacy_string" ("_key", "data")
@@ -119,9 +116,8 @@ DO UPDATE SET "data" = $2::TEXT`,
}
return await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectType(client, key, 'string');
const res = await query({
const res = await client.query({
name: 'increment',
text: `
INSERT INTO "legacy_string" ("_key", "data")
@@ -137,15 +133,14 @@ RETURNING "data" d`,
module.rename = async function (oldKey, newKey) {
await module.transaction(async function (client) {
var query = client.query.bind(client);
await query({
await client.query({
name: 'deleteRename',
text: `
DELETE FROM "legacy_object"
WHERE "_key" = $1::TEXT`,
values: [newKey],
});
await query({
await client.query({
name: 'rename',
text: `
UPDATE "legacy_object"
@@ -157,7 +152,7 @@ WHERE "_key" = $1::TEXT`,
};
module.type = async function (key) {
const res = await query({
const res = await module.pool.query({
name: 'type',
text: `
SELECT "type"::TEXT t
@@ -171,7 +166,7 @@ SELECT "type"::TEXT t
};
async function doExpire(key, date) {
await query({
await module.pool.query({
name: 'expire',
text: `
UPDATE "legacy_object"

View File

@@ -2,7 +2,7 @@
var _ = require('lodash');
module.exports = function (db, module) {
module.exports = function (module) {
var helpers = require('./helpers');
module.setAdd = async function (key, value) {
@@ -11,9 +11,8 @@ module.exports = function (db, module) {
}
await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectType(client, key, 'set');
await query({
await client.query({
name: 'setAdd',
text: `
INSERT INTO "legacy_set" ("_key", "member")
@@ -38,9 +37,8 @@ DO NOTHING`,
keys = _.uniq(keys);
await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectsType(client, keys, 'set');
await query({
await client.query({
name: 'setsAdd',
text: `
INSERT INTO "legacy_set" ("_key", "member")
@@ -63,7 +61,7 @@ DO NOTHING`,
value = [value];
}
await db.query({
await module.pool.query({
name: 'setRemove',
text: `
DELETE FROM "legacy_set"
@@ -78,7 +76,7 @@ DELETE FROM "legacy_set"
return;
}
await db.query({
await module.pool.query({
name: 'setsRemove',
text: `
DELETE FROM "legacy_set"
@@ -93,7 +91,7 @@ DELETE FROM "legacy_set"
return false;
}
const res = await db.query({
const res = await module.pool.query({
name: 'isSetMember',
text: `
SELECT 1
@@ -116,7 +114,7 @@ SELECT 1
values = values.map(helpers.valueToString);
const res = await db.query({
const res = await module.pool.query({
name: 'isSetMembers',
text: `
SELECT s."member" m
@@ -141,7 +139,7 @@ SELECT s."member" m
value = helpers.valueToString(value);
const res = await db.query({
const res = await module.pool.query({
name: 'isMemberOfSets',
text: `
SELECT o."_key" k
@@ -164,7 +162,7 @@ SELECT o."_key" k
return [];
}
const res = await db.query({
const res = await module.pool.query({
name: 'getSetMembers',
text: `
SELECT s."member" m
@@ -184,7 +182,7 @@ SELECT s."member" m
return [];
}
const res = await db.query({
const res = await module.pool.query({
name: 'getSetsMembers',
text: `
SELECT o."_key" k,
@@ -208,7 +206,7 @@ SELECT o."_key" k,
return 0;
}
const res = await db.query({
const res = await module.pool.query({
name: 'setCount',
text: `
SELECT COUNT(*) c
@@ -224,7 +222,7 @@ SELECT COUNT(*) c
};
module.setsCount = async function (keys) {
const res = await db.query({
const res = await module.pool.query({
name: 'setsCount',
text: `
SELECT o."_key" k,
@@ -244,7 +242,7 @@ SELECT o."_key" k,
};
module.setRemoveRandom = async function (key) {
const res = await db.query({
const res = await module.pool.query({
name: 'setRemoveRandom',
text: `
WITH A AS (

View File

@@ -1,18 +1,16 @@
'use strict';
module.exports = function (db, module) {
module.exports = function (module) {
var helpers = require('./helpers');
const util = require('util');
var Cursor = require('pg-cursor');
Cursor.prototype.readAsync = util.promisify(Cursor.prototype.read);
const sleep = util.promisify(setTimeout);
var query = db.query.bind(db);
require('./sorted/add')(db, module);
require('./sorted/remove')(db, module);
require('./sorted/union')(db, module);
require('./sorted/intersect')(db, module);
require('./sorted/add')(module);
require('./sorted/remove')(module);
require('./sorted/union')(module);
require('./sorted/intersect')(module);
module.getSortedSetRange = async function (key, start, stop) {
return await getSortedSetRange(key, start, stop, 1, false);
@@ -60,7 +58,7 @@ module.exports = function (db, module) {
limit = null;
}
const res = await query({
const res = await module.pool.query({
name: 'getSortedSetRangeWithScores' + (sort > 0 ? 'Asc' : 'Desc'),
text: `
SELECT z."value",
@@ -125,7 +123,7 @@ OFFSET $2::INTEGER`,
max = null;
}
const res = await query({
const res = await module.pool.query({
name: 'getSortedSetRangeByScoreWithScores' + (sort > 0 ? 'Asc' : 'Desc'),
text: `
SELECT z."value",
@@ -164,7 +162,7 @@ OFFSET $2::INTEGER`,
max = null;
}
const res = await query({
const res = await module.pool.query({
name: 'sortedSetCount',
text: `
SELECT COUNT(*) c
@@ -186,7 +184,7 @@ SELECT COUNT(*) c
return 0;
}
const res = await query({
const res = await module.pool.query({
name: 'sortedSetCard',
text: `
SELECT COUNT(*) c
@@ -206,7 +204,7 @@ SELECT COUNT(*) c
return [];
}
const res = await query({
const res = await module.pool.query({
name: 'sortedSetsCard',
text: `
SELECT o."_key" k,
@@ -249,7 +247,7 @@ SELECT o."_key" k,
async function getSortedSetRank(sort, keys, values) {
values = values.map(helpers.valueToString);
const res = await query({
const res = await module.pool.query({
name: 'getSortedSetRank' + sort,
text: `
SELECT (SELECT r
@@ -310,7 +308,7 @@ SELECT (SELECT r
value = helpers.valueToString(value);
const res = await query({
const res = await module.pool.query({
name: 'sortedSetScore',
text: `
SELECT z."score" s
@@ -335,7 +333,7 @@ SELECT z."score" s
value = helpers.valueToString(value);
const res = await query({
const res = await module.pool.query({
name: 'sortedSetsScore',
text: `
SELECT o."_key" k,
@@ -364,7 +362,7 @@ SELECT o."_key" k,
}
values = values.map(helpers.valueToString);
const res = await query({
const res = await module.pool.query({
name: 'sortedSetScores',
text: `
SELECT z."value" v,
@@ -391,7 +389,7 @@ SELECT z."value" v,
value = helpers.valueToString(value);
const res = await query({
const res = await module.pool.query({
name: 'isSortedSetMember',
text: `
SELECT 1
@@ -414,7 +412,7 @@ SELECT 1
values = values.map(helpers.valueToString);
const res = await query({
const res = await module.pool.query({
name: 'isSortedSetMembers',
text: `
SELECT z."value" v
@@ -439,7 +437,7 @@ SELECT z."value" v
value = helpers.valueToString(value);
const res = await query({
const res = await module.pool.query({
name: 'isMemberOfSortedSets',
text: `
SELECT o."_key" k
@@ -462,7 +460,7 @@ SELECT o."_key" k
return [];
}
const res = await query({
const res = await module.pool.query({
name: 'getSortedSetsMembers',
text: `
SELECT o."_key" k,
@@ -490,9 +488,8 @@ SELECT o."_key" k,
increment = parseFloat(increment);
return await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectType(client, key, 'zset');
const res = await query({
const res = await client.query({
name: 'sortedSetIncrBy',
text: `
INSERT INTO "legacy_zset" ("_key", "value", "score")
@@ -517,7 +514,7 @@ RETURNING "score" s`,
module.sortedSetLexCount = async function (key, min, max) {
var q = buildLexQuery(key, min, max);
const res = await query({
const res = await module.pool.query({
name: 'sortedSetLexCount' + q.suffix,
text: `
SELECT COUNT(*) c
@@ -539,7 +536,7 @@ SELECT COUNT(*) c
var q = buildLexQuery(key, min, max);
q.values.push(start);
q.values.push(count <= 0 ? null : count);
const res = await query({
const res = await module.pool.query({
name: 'sortedSetLex' + (sort > 0 ? 'Asc' : 'Desc') + q.suffix,
text: `
SELECT z."value" v
@@ -559,7 +556,7 @@ OFFSET $` + (q.values.length - 1) + `::INTEGER`,
module.sortedSetRemoveRangeByLex = async function (key, min, max) {
var q = buildLexQuery(key, min, max);
await query({
await module.pool.query({
name: 'sortedSetRemoveRangeByLex' + q.suffix,
text: `
DELETE FROM "legacy_zset" z
@@ -614,7 +611,7 @@ DELETE FROM "legacy_zset" z
}
module.processSortedSet = async function (setKey, process, options) {
const client = await db.connect();
const client = await module.pool.connect();
var batchSize = (options || {}).batch || 100;
var cursor = client.query(new Cursor(`
SELECT z."value", z."score"
@@ -645,7 +642,7 @@ SELECT z."value", z."score"
try {
await process(rows);
} catch (err) {
await query.close();
await client.release();
throw err;
}
if (options.interval) {

View File

@@ -1,6 +1,6 @@
'use strict';
module.exports = function (db, module) {
module.exports = function (module) {
var helpers = require('../helpers');
var utils = require('../../../utils');
@@ -19,10 +19,8 @@ module.exports = function (db, module) {
score = parseFloat(score);
await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectType(client, key, 'zset');
await query({
await client.query({
name: 'sortedSetAdd',
text: `
INSERT INTO "legacy_zset" ("_key", "value", "score")
@@ -52,9 +50,8 @@ module.exports = function (db, module) {
helpers.removeDuplicateValues(values, scores);
await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectType(client, key, 'zset');
await query({
await client.query({
name: 'sortedSetAddBulk',
text: `
INSERT INTO "legacy_zset" ("_key", "value", "score")
@@ -84,9 +81,8 @@ DO UPDATE SET "score" = EXCLUDED."score"`,
scores = isArrayOfScores ? scores.map(score => parseFloat(score)) : parseFloat(scores);
await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectsType(client, keys, 'zset');
await query({
await client.query({
name: isArrayOfScores ? 'sortedSetsAddScores' : 'sortedSetsAdd',
text: isArrayOfScores ? `
INSERT INTO "legacy_zset" ("_key", "value", "score")
@@ -117,9 +113,8 @@ INSERT INTO "legacy_zset" ("_key", "value", "score")
values.push(item[2]);
});
await module.transaction(async function (client) {
var query = client.query.bind(client);
await helpers.ensureLegacyObjectsType(client, keys, 'zset');
await query({
await client.query({
name: 'sortedSetAddBulk2',
text: `
INSERT INTO "legacy_zset" ("_key", "value", "score")

View File

@@ -1,12 +1,12 @@
'use strict';
module.exports = function (db, module) {
module.exports = function (module) {
module.sortedSetIntersectCard = async function (keys) {
if (!Array.isArray(keys) || !keys.length) {
return 0;
}
const res = await db.query({
const res = await module.pool.query({
name: 'sortedSetIntersectCard',
text: `
WITH A AS (SELECT z."value" v,
@@ -55,7 +55,7 @@ SELECT COUNT(*) c
limit = null;
}
const res = await db.query({
const res = await module.pool.query({
name: 'getSortedSetIntersect' + aggregate + (params.sort > 0 ? 'Asc' : 'Desc') + 'WithScores',
text: `
WITH A AS (SELECT z."value",

View File

@@ -1,6 +1,6 @@
'use strict';
module.exports = function (db, module) {
module.exports = function (module) {
var helpers = require('../helpers');
module.sortedSetRemove = async function (key, value) {
@@ -20,7 +20,7 @@ module.exports = function (db, module) {
value = [value];
}
value = value.map(helpers.valueToString);
await db.query({
await module.pool.query({
name: 'sortedSetRemove',
text: `
DELETE FROM "legacy_zset"
@@ -37,7 +37,7 @@ DELETE FROM "legacy_zset"
value = helpers.valueToString(value);
await db.query({
await module.pool.query({
name: 'sortedSetsRemove',
text: `
DELETE FROM "legacy_zset"
@@ -59,7 +59,7 @@ DELETE FROM "legacy_zset"
max = null;
}
await db.query({
await module.pool.query({
name: 'sortedSetsRemoveRangeByScore',
text: `
DELETE FROM "legacy_zset"

View File

@@ -1,12 +1,12 @@
'use strict';
module.exports = function (db, module) {
module.exports = function (module) {
module.sortedSetUnionCard = async function (keys) {
if (!Array.isArray(keys) || !keys.length) {
return 0;
}
const res = await db.query({
const res = await module.pool.query({
name: 'sortedSetUnionCard',
text: `
SELECT COUNT(DISTINCT z."value") c
@@ -49,7 +49,7 @@ SELECT COUNT(DISTINCT z."value") c
limit = null;
}
const res = await db.query({
const res = await module.pool.query({
name: 'getSortedSetUnion' + aggregate + (params.sort > 0 ? 'Asc' : 'Desc') + 'WithScores',
text: `
WITH A AS (SELECT z."value",

View File

@@ -1,6 +1,6 @@
'use strict';
module.exports = function (db, module) {
module.exports = function (module) {
module.transaction = async function (perform, txClient) {
let res;
if (txClient) {
@@ -15,7 +15,7 @@ module.exports = function (db, module) {
return res;
}
// see https://node-postgres.com/features/transactions#a-pooled-client-with-async-await
const client = await db.connect();
const client = await module.pool.connect();
try {
await client.query('BEGIN');