Skip to content

Commit

Permalink
Merge pull request #1 from MikeIbberson/fix/discriminators
Browse files Browse the repository at this point in the history
Support for discriminated searchable values
  • Loading branch information
MikeIbberson committed Jan 23, 2020
2 parents 4b9d7bf + c6da7cd commit acda08c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 26 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [8.x, 10.x, 12.x]
node-version: [12.x]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: yarn install, lint, and test
- name: yarn install, build, and test
run: |
yarn install
yarn lint
yarn test
- name: Coveralls
uses: coverallsapp/github-action@v1.0.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<h1>🔎 Mongoose partial search plugin</h1>

<p>
<img src="https://github.com/MikeIbberson/mongoose-partial-search/workflows/Node%20CI/badge.svg" alt="Status" />
</p>
<a href='https://coveralls.io/github/MikeIbberson/mongoose-partial-search?branch=master'><img src='https://coveralls.io/repos/github/MikeIbberson/mongoose-partial-search/badge.svg?branch=master' alt='Coverage Status' /></a>
<img src='https://bettercodehub.com/edge/badge/MikeIbberson/mongoose-partial-search?branch=master'>
</p>

<p>This packages adds a <code>searchBuilder</code> static method to the <a href="https://mongoosejs.com/docs/schematypes.html">Mongoose</a> model that returns a case-insensitive, regex-powered query to drop into your find functions.</p>


Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ module.exports = (schema = {}) =>

iterateSchema(this.schema);

if (this.schema.discriminators)
Object.values(this.schema.discriminators).forEach(
(v) => iterateSchema(v),
);

if (!arr.length) return {};
if (statement.length === 1)
constructOr(arr)(statement);
Expand Down
89 changes: 66 additions & 23 deletions lib/search.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,96 @@ const base = new mongoose.Schema({
},
});

const variant = new mongoose.Schema({
colour: {
type: String,
searchable: true,
},
});

mongoose.plugin(plugin);

const SearchableModel = mongoose.model(
'SearchIntegration',
base,
);

const performSearchQuery = async (
const SearchableModelDiscriminator = SearchableModel.discriminator(
'SearchIntegrationD',
variant,
);

const performSearchQuery = (
term,
expectedResultsLength,
) => {
const resp = await SearchableModel.find(
SearchableModel.searchBuilder(term),
).exec();

return expect(resp).toHaveLength(expectedResultsLength);
};
) => (done) =>
SearchableModelDiscriminator.find(
SearchableModelDiscriminator.searchBuilder(term),
)
.then((resp) => {
return expect(resp).toHaveLength(
expectedResultsLength,
);
})
.then(() => {
done();
});

beforeAll(async () => {
await mongoose.connect(process.env.CONNECTION);
await SearchableModel.create([
await SearchableModelDiscriminator.create([
{ name: 'John Katie' },
{ name: 'Mary Anne', pet: { name: 'Sniffles' } },
{ name: 'John Kodie', pet: { name: 'Boots' } },
{
name: 'Mary Anne',
pet: { name: 'Sniffles' },
},
{
name: 'John Kodie',
pet: { name: 'Boots' },
colour: 'Green',
},
{
name: 'Henry Boyd',
friends: [{ name: 'Arnold' }, { name: 'Katie' }],
colour: 'Green',
},
]);
});

afterAll(async () => {
await mongoose.disconnect();
});

describe('Search', () => {
it('should not filter the results', async () =>
performSearchQuery(null, 4));
it(
'should not filter the results',
performSearchQuery(null, 4),
);

it(
'should yield all johns',
performSearchQuery('John', 2),
);

it('should yield all johns', async () =>
performSearchQuery('John', 2));
it('should match on pet', performSearchQuery('Boots', 1));

it('should match on pet', async () =>
performSearchQuery('Boots', 1));
it(
'should match on friend and name',
performSearchQuery('Katie', 2),
);

it('should match on friend and name', async () =>
performSearchQuery('Katie', 2));
it(
'should match on combined string in single field',
performSearchQuery('Henry B', 1),
);

it('should match on combined string in single field', async () =>
performSearchQuery('Henry B', 1));
it(
'should yield match on combined string on multiple fields',
performSearchQuery('John Katie', 1),
);

it('should yield match on combined string on multiple fields', async () =>
performSearchQuery('John Katie', 1));
it(
'should yield match on discriminated value',
performSearchQuery('green', 2),
);
});

0 comments on commit acda08c

Please sign in to comment.