Giter Club home page Giter Club logo

contexture-mongo's Introduction

⚠️ Development has moved to the contexture monorepo: This package lives in https://github.com/smartprocure/contexture/tree/master/packages/provider-mongo

contexture-mongo

Mongo Provider for Contexture

Overview

This library assumes you'll pass a native Mongo client. For example, if you're using the package mongo, you would be passing the database object you get right after calling connect. Most of other MongoDB clients and similar tools provide a way to access the native client.

Usage

This provider takes a config object as a parameter, and expects a getClient method to be provided, which should return an instantiated MongoDB client.

Option Type Description Required
getClient function Returns an instantiated MongoDB client x
types object Contexture node types, like all other providers

Schemas

Schemas using this mongo provider must specify a collection property, which is the name of the collection it runs against.

Option Type Description Required
collection string The MongoDB collection that will be used to run the queries x

Example Schema for SomeMongoCollection

export default {
  mongo: {
    collection: 'SomeMongoCollection'
  }
}

Seting up contexture

let Contexture = require('contexture')
let provider = require('contexture-mongo')
let types = require('contexture-mongo/types')
let schemas = require('./path/to/schemas')

let process = Contexture({
  schemas,
  providers: {
    mongo: provider({
      getClient: () => client,
      types: types()
    })
  }
})

Default Types

Requiring contexture-mongo/types and calling it as a function will allow you to use a curated set of types we offer by default. contexture-mongo/types allows you to pass a customization object that will allow you to pass custom parameters to the provided types.

mongoId

mongoId is filter only and compares against a mongo id, which in mongoose needs to be cast.

text

text is filter only and supports an array of values, a join, and an operator which it uses to construct a $regex filter.

The following operators are supported:

Operator Description
containsWord /WORD/ (matches mid-word)
containsExact /\bWORD\b/ (matches word in field)
startsWith /^WORD/
endsWith /WORD$/
is /^WORD$/ (exact match)
wordStartsWith /\bWORD/
wordEndsWith /WORD\b/

date

date is filter only and converts {from, to} to a {$gte, $lte} mongo date filter. It also supports dateMath via @elastic/datemath (the same as supported by elasticsearch) to provide time ranges as well as three custom strings lastQuarter, thisQuarter, and nextQuarter (which are calculated on the fly). If you have dates that aren't store as dates, you can use dateType to change how the dates are output. The default is date, but you can also set it to unix for seconds from epoch or timestamp for a timestamp with milliseconds.

number

exists

facet

results

statistical

statistical will produce a list of statistical values in the context of the node. It does this by running count: { $sum: 1 }, $max, $min, $avg and $sum after running other available filters.

termsStats

termsStats is like statistical, grouped by the keyField

dateHistogram

Aggregates values for a field and groups into time periods specified by interval. Implements the statistical values listed above plus a cardinality count of unique values within each interval (the use case for this is when value_field isn't numeric).

Integration Tests

This repository offers integration tests to practice and understand the example types we offer. You can run the integration tests with the command: npm run test-integration. If you have a mongo database available at localhost (default port), the tests will connect to it and do changes on a database named contexture-test.

contexture-mongo's People

Contributors

daedalus28 avatar chris110408 avatar doug-patterson avatar sadasant avatar ccalbaugh avatar dubiousdavid avatar rudolph9 avatar stellarhoof avatar michsa avatar dshishkov avatar sean-clayton avatar decrapifier avatar chrislatorres avatar

Stargazers

 avatar Kristoffer Hebert avatar

Watchers

 avatar James Cloos avatar  avatar Scott Thomson avatar Baseer Siddiqui avatar Joe Ortiz avatar Emre Celenli avatar Conrad Davis Jr avatar  avatar Dylan Parisi avatar Geovanny Fajardo avatar Dominique Northecide avatar  avatar

Forkers

warrenv

contexture-mongo's Issues

Improve results node populate performance when includes are specified

Specifying includes for a results node populate ought to increase overall performance by leaving everything else untouched and reducing response sizes.

However, if you're in this unlucky situation, the facts are quite otherwise: collection A has property K, which for most records is undefined, but when it is present you want a $lookup to collection B. In this case (most A records have no value at K), if collection B is at all large (and even if collection A is tiny) your query performance will drop through the basement -- while working I saw adding includes to a populate take a query from a few millisecond to 5-10 seconds where collection A had 16 records and collection B had about 35k.

For some reason at https://github.com/smartprocure/contexture-mongo/blob/master/src/example-types/results.js#L20-L39 the aggregation stages that you get if you do specify includes perform far worse than those you get if you don't.

Need to figure out why and prevent it -- there's no reason that projecting properties of the looked-up records should affect performance so badly. By the mongo manual and the code it looks like the queries should perform equivalently, but with the second form somehow mongo knows not to do the lookup when the key isn't even set or it uses an index while the first doesn't

update isMongo facet to sort by checked:desc, count:desc

Right now checked values in a facet don't get any special treatment, and so don't show up at the top of the facet list unless the list is long enough to reach the position where they'd be if they weren't checked. This is a regression from something.

Need to add to https://github.com/smartprocure/contexture-mongo/blob/master/src/example-types/facet.js#L17 roughly like this:

  { $addFields: { isSelected: <is in node.values> } },
  { $sort: { isSelected: -1, count: 1 } } 

to produce a sort by checked-ness first

fix dateHistogram test

looks like the component itself is doing $addToSet when it ought to be $sum, and the test is unfinished/inaccurate given those difficulties.

Fix date histogram test

Somehow the new-ish date histogram code got checked in with failing tests. Need to uncomment the file and fix the tests. The code will be commented out by #64

correct dateHistogram cardinality prop

My judgment as to what the cardinality prop on a dateHistogram was intended to count was mistaken on #68 -- the prop should hold a count of unique values. Need to update the code and test to implement that properly.

Detect and throw error when a search tries to populate a missing populated property

this pattern on a results node

{ 
  type: 'results'
  ...
  include: [
    ...
    'prop1'
  ],
  populate: {
    prop1: {
      localField: ...
      foreignField: ...
      include: propsWithoutProp2
    },
    prop3: {
      localField: 'prop1.prop2', 
      ...
    }
  }
}

will result in a hard-to-debug long-running query when the foreign collection $lookup for prop1.prop2 tries to find a match for undefined.

Better to detect this situation in the tree populates and throw and error before running

Add `bool` example type

We have it in contexture-elasticsearch. The main feature is that false would also match against null, undefined, and missing.

To exclude soft deleted records, right now users often do this:

{
  key: 'isDeleted',
  field: 'isDeleted',
  type: 'facet',
  mode: 'exclude',
  values: [true],
}

which could become more semantic like this:

{
  key: 'isDeleted',
  field: 'isDeleted',
  type: 'bool',
  value: false,
}

or even

{
  key: 'isDeleted',
  field: 'isDeleted',
  type: 'bool',
}

support labeling facet values from a foreign collection

like "populate" for mongo facet values -- support props for collection, foreignField and some fields to _.pick off the foreign record (to keep the response small), and implement a $lookup, $unwind, $project to pass the picked foreign props along on the facet value objects for use in (for example) contexture-react's Facet type display function futil-js/contexture-react#390

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.