Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(3163): Handle params with greater than or less than filter [1] #96

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const MODELS = schemas.models;
const MODEL_NAMES = Object.keys(MODELS);
const logger = require('screwdriver-logger');
const pg = require('pg');
// Regex patten for gt:123, lt:456
const INEQUALITY_SIGNS = /^(gt|lt):([\d]+)$/;

/**
* Converts data from the value stored in the datastore
Expand Down Expand Up @@ -474,7 +476,23 @@ class Squeakquel extends Datastore {
if (this._fieldInvalid({ validFields, field: paramName })) {
throw new Error(`Invalid param "${paramName}"`);
}
findParams.where[paramName] = paramValue;
// Check for gt: or lt: prefix to set greater than or less than operator
// Currently only matching for number values
if (INEQUALITY_SIGNS.test(paramValue)) {
const [, sign, paramVal] = paramValue.match(INEQUALITY_SIGNS);

if (sign === 'gt') {
findParams.where[paramName] = {
[Sequelize.Op.gt]: paramVal
};
} else if (sign === 'lt') {
findParams.where[paramName] = {
[Sequelize.Op.lt]: paramVal
};
}
} else {
findParams.where[paramName] = paramValue;
}
}

const indexIndex = model.indexes && model.rangeKeys ? model.indexes.indexOf(paramName) : -1;
Expand Down
43 changes: 43 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ describe('index test', function () {
or: 'OR',
gte: 'GTE',
lte: 'LTE',
gt: 'GT',
lt: 'LT',
eq: 'EQ'
};
sequelizeMock.col = sinon.stub().returns('col');
Expand Down Expand Up @@ -1259,6 +1261,47 @@ describe('index test', function () {
});
});

it('scans for some data with params with inequality sign (gt or lt)', () => {
const testData = [
{
id: 7,
name: 'foo'
},
{
id: 6,
name: 'foo'
}
];
const testInternal = [
{
toJSON: sinon.stub().returns(testData[0])
},
{
toJSON: sinon.stub().returns(testData[1])
}
];

testParams.params = {
name: 'foo',
id: 'gt:5'
};

sequelizeTableMock.findAll.resolves(testInternal);

return datastore.scan(testParams).then(data => {
assert.deepEqual(data, testData);
assert.calledWith(sequelizeTableMock.findAll, {
where: {
name: 'foo',
id: {
GT: '5'
}
},
order: [['id', 'DESC']]
});
});
});

it('throws an error when the param is not valid', () => {
testParams.params = {
foo: 'banana'
Expand Down