Skip to content

Commit

Permalink
Merge pull request #19 from MasterOdin/chore-lru-cache
Browse files Browse the repository at this point in the history
Bump lru-cache dependency to ^7.14.1
  • Loading branch information
sidorares authored Jan 9, 2023
2 parents 6bfdfec + 9534aeb commit c6ae00d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [6.x, 8.x, 10.x, 12.x, 14.x, 16.x, 18.x]
node-version: [12.x, 14.x, 16.x, 18.x]

name: Node.js ${{ matrix.node-version }}

Expand Down
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ function parse(query) {
return [query];
};

const EMPTY_LRU_FN = (key, value) => {};

function createCompiler(config) {
if (!config)
config = {};
Expand All @@ -82,7 +80,7 @@ function createCompiler(config) {
cache = config.cache;
}
if (config.cache !== false && !cache) {
cache = require('lru-cache')({ max: ncache, dispose: EMPTY_LRU_FN });
cache = new (require('lru-cache'))({ max: ncache });
}

function toArrayParams(tree, params) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"placeholders"
],
"engines": {
"node": ">=6.0.0"
"node": ">=12.0.0"
},
"author": "Andrey Sidorov <[email protected]>",
"files": [],
Expand All @@ -27,6 +27,6 @@
"should": "^13.2.3"
},
"dependencies": {
"lru-cache": "^4.1.3"
"lru-cache": "^7.14.1"
}
}
61 changes: 33 additions & 28 deletions test/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const assert = require('assert');
require('should');

describe('given input query with named parameters', () => {

it('should build corresponding query with `?` placeholders and fill array of parameters from input object', () => {
let query = 'Select users.json,EXISTS(Select 1 from moderators where moderators.id = :id) as is_moderator from users where users.id = :id and users.status = :status and users.complete_status = :complete_status';

Expand Down Expand Up @@ -33,38 +32,44 @@ describe('given input query with named parameters', () => {
compile(query);
},
/Named query contains placeholders, but parameters object is undefined/
);
});
);
});

it('should replace ::name style placeholders with `??` placeholders', () => {
it('should replace ::name style placeholders with `??` placeholders', () => {
let query = 'normal placeholder :p1 and double semicolon ::p2';
compile(query, {p1: 'test1', p2: 'test2'})
.should.eql([ 'normal placeholder ? and double semicolon ??', [ 'test1', 'test2' ] ]);

let query = 'normal placeholder :p1 and double semicolon ::p2';
compile(query, {p1: 'test1', p2: 'test2'})
.should.eql([ 'normal placeholder ? and double semicolon ??', [ 'test1', 'test2' ] ]);
query = 'normal placeholder ::p1 and double semicolon :p2';
compile(query, {p1: 'test1', p2: 'test2'})
.should.eql([ 'normal placeholder ?? and double semicolon ?', [ 'test1', 'test2' ] ]);

query = 'normal placeholder ::p1 and double semicolon :p2';
compile(query, {p1: 'test1', p2: 'test2'})
.should.eql([ 'normal placeholder ?? and double semicolon ?', [ 'test1', 'test2' ] ]);
query = 'normal placeholder ::p2 and double semicolon :p1';
compile(query, {p1: 'test1', p2: 'test2'})
.should.eql([ 'normal placeholder ?? and double semicolon ?', [ 'test2', 'test1' ] ]);

query = 'normal placeholder ::p2 and double semicolon :p1';
compile(query, {p1: 'test1', p2: 'test2'})
.should.eql([ 'normal placeholder ?? and double semicolon ?', [ 'test2', 'test1' ] ]);
query = 'normal placeholder :p1 and double semicolon ::p2 test';
compile(query, {p1: 'test1', p2: 'test2'})
.should.eql([ 'normal placeholder ? and double semicolon ?? test', [ 'test1', 'test2' ] ]);
});

query = 'normal placeholder :p1 and double semicolon ::p2 test';
compile(query, {p1: 'test1', p2: 'test2'})
.should.eql([ 'normal placeholder ? and double semicolon ?? test', [ 'test1', 'test2' ] ]);
});
it('compiles the query the same twice', () => {
const query = 'SELECT * FROM foo WHERE id = :id';
const expected = [ 'SELECT * FROM foo WHERE id = ?', [ 123 ] ];
compile(query, { id: 123 }).should.eql(expected);
compile(query, { id: 123 }).should.eql(expected);
});
});

describe('postgres-style toNumbered conversion', () => {
it('basic test', () => {
const toNumbered = require('..').toNumbered;
const query = 'SELECT usr.p_pause_stop_track(:doc_dtl_id, :plan_id, :wc_id, 20, :time_from)';
toNumbered(query, {
doc_dtl_id: 123,
time_from: 345,
plan_id: 456,
wc_id: 678
}).should.eql([ 'SELECT usr.p_pause_stop_track($1, $2, $3, 20, $4)', [ 123, 456, 678, 345 ]]);
});
describe('postgres-style toNumbered conversion', () => {
it('basic test', () => {
const toNumbered = require('..').toNumbered;
const query = 'SELECT usr.p_pause_stop_track(:doc_dtl_id, :plan_id, :wc_id, 20, :time_from)';
toNumbered(query, {
doc_dtl_id: 123,
time_from: 345,
plan_id: 456,
wc_id: 678
}).should.eql([ 'SELECT usr.p_pause_stop_track($1, $2, $3, 20, $4)', [ 123, 456, 678, 345 ]]);
});
});

0 comments on commit c6ae00d

Please sign in to comment.