[
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2015 MeteorHacks Pvt Ltd (Sri Lanka). <hello@meteorhacks.com>\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE."
  },
  {
    "path": "README.md",
    "content": "search-source\n=============\n\n#### Reactive Data Source for building search solutions with Meteor\n\nIf you are new to search source, it's a good idea to look at this introductory [article](https://meteorhacks.com/implementing-an-instant-search-solution-with-meteor.html) on MeteorHacks.\n\n## Installation\n\n```\nmeteor add meteorhacks:search-source\n```\n\n### Creating a source in client\n\n```js\nvar options = {\n  keepHistory: 1000 * 60 * 5,\n  localSearch: true\n};\nvar fields = ['packageName', 'description'];\n\nPackageSearch = new SearchSource('packages', fields, options);\n```\n\n* First parameter for the source is the name of the source itself. You need to use it for defining the data source on the server.\n* second arguments is the number of fields to search on the client (used for client side search and text transformation)\n* set of options. Here are they\n    * `keepHistory` - cache the search data locally. You need to give an expire time(in millis) to cache it on the client. Caching is done based on the search term. Then if you search again for that term, it search source won't ask the server to get data again.\n    * `localSearch` - allow to search locally with the data it has.\n\n### Define the data source on the server\n\nIn the server, get data from any backend and send those data to the client as shown below. You need to return an array of documents where each of those object consists of `_id` field.\n\n> Just like inside a method, you can use `Meteor.userId()` and `Meteor.user()` inside a source definition.\n\n```js\nSearchSource.defineSource('packages', function(searchText, options) {\n  var options = {sort: {isoScore: -1}, limit: 20};\n\n  if(searchText) {\n    var regExp = buildRegExp(searchText);\n    var selector = {packageName: regExp, description: regExp};\n    return Packages.find(selector, options).fetch();\n  } else {\n    return Packages.find({}, options).fetch();\n  }\n});\n\nfunction buildRegExp(searchText) {\n  var words = searchText.trim().split(/[ \\-\\:]+/);\n  var exps = _.map(words, function(word) {\n    return \"(?=.*\" + word + \")\";\n  });\n  var fullExp = exps.join('') + \".+\";\n  return new RegExp(fullExp, \"i\");\n}\n```\n\n### Get the reactive data source\n\nYou can get the reactive data source with the `PackageSearch.getData` api. This is an example usage of that:\n\n```js\nTemplate.searchResult.helpers({\n  getPackages: function() {\n    return PackageSearch.getData({\n      transform: function(matchText, regExp) {\n        return matchText.replace(regExp, \"<b>$&</b>\")\n      },\n      sort: {isoScore: -1}\n    });\n  }\n});\n```\n\n`.getData()` api accepts an object with options (and an optional argument to ask for a cursor instead of a fetched array; see example below). These are the options you can pass:\n\n* `transform` - a transform function to alter the selected search texts. See above for an example usage.\n* `sort` - an object with MongoDB sort specifiers\n* `limit` - no of objects to limit\n* `docTransform` - a transform function to transform the documents in the search result. Use this for computed values or model helpers. (see example below)\n\n\n```js\nTemplate.searchResult.helpers({\n  getPackages: function() {\n    return PackageSearch.getData({\n      docTransform: function(doc) {\n        return _.extend(doc, {\n          owner: function() {\n            return Meteor.users.find({_id: this.ownerId})\n          }\n        })\n      },\n      sort: {isoScore: -1}\n    }, true);\n  }\n});\n```\n\n### Searching\n\nFinally we can invoke search queries by invoking following API.\n\n```js\nPackageSearch.search(\"the text to search\");\n```\n\n### Status\n\nYou can get the status of the search source by invoking following API. It's reactive too.\n\n```\nvar status = PackageSearch.getStatus();\n```\n\nStatus has following fields depending on the status.\n\n* loading - indicator when loading\n* loaded - indicator after loaded\n* error - the error object, mostly if backend data source throws an error\n\n### Metadata\n\nWith metadata, you get some useful information about search along with the search results. These metadata can be time it takes to process the search or the number of results for this search term.\n\nYou can get the metadata with following API. It's reactive too.\n\n```js\nvar metadata = PackageSearch.getMetadata();\n```\n\nNow we need a way to send metadata to the client. This is how we can do it. You need to change the server side search source as follows\n\n```js\nSearchSource.defineSource('packages', function(searchText, options) {\n  var data = getSearchResult(searchText);\n  var metadata = getMetadata();\n\n  return {\n    data: data,\n    metadata: metadata\n  }\n});\n```\n\n### Passing Options with Search\n\nWe can also pass some options while searching. This is the way we can implement pagination and other extra functionality.\n\nLet's pass some options to the server:\n\n```js\n// In the client\nvar options = {page: 10};\nPackageSearch.search(\"the text to search\", options);\n```\n\nNow you can get the options object from the server. See:\n\n```js\n// In the server\nSearchSource.defineSource('packages', function(searchText, options) {\n  // do anything with options\n  console.log(options); // {\"page\": 10}\n});\n```\n\n### Get Current Search Query\n\nYou can get the current search query with following API. It's reactive too.\n\n```js\nvar searchText = PackageSearch.getCurrentQuery();\n```\n\n### Clean History\n\nYou can clear the stored history (if enabled the `keepHistory` option) via the following API.\n\n```js\nPackageSearch.cleanHistory();\n```\n\n### Defining Data Source in the Client\n\nSometime, we don't need to fetch data from the server. We need to get it from a data source aleady available on the client. So, this is how we do it:\n\n```js\nPackageSearch.fetchData = function(searchText, options, success) {\n  SomeOtherDDPConnection.call('getPackages', searchText, options, function(err, data) {\n    success(err, data);\n  });\n};\n```\n"
  },
  {
    "path": "lib/client.js",
    "content": "SearchSource = function SearchSource(source, fields, options) {\n  this.source = source;\n  this.searchFields = fields;\n  this.currentQuery = null;\n  this.options = options || {};\n\n  this.status =  new ReactiveVar({loaded: true});\n  this.metaData = new ReactiveVar({});\n  this.history = {};\n  this.store = new Mongo.Collection(null);\n\n  this._storeDep = new Tracker.Dependency();\n  this._currentQueryDep = new Tracker.Dependency();\n  this._currentVersion = 0;\n  this._loadedVersion = 0;\n}\n\nSearchSource.prototype._loadData = function(query, options) {\n  var self = this;\n  var version = 0;\n  var historyKey = query + EJSON.stringify(options);\n  if(this._canUseHistory(historyKey)) {\n    this._updateStore(this.history[historyKey].data);\n    this.metaData.set(this.history[historyKey].metadata);\n    self._storeDep.changed();\n  } else {\n    this.status.set({loading: true});\n    version = ++this._currentVersion;\n    this._fetch(this.source, query, options, handleData);\n  }\n\n  function handleData(err, payload) {\n    if(err) {\n      self.status.set({error: err});\n      throw err;\n    } else {\n      if(payload instanceof Array) {\n        var data = payload;\n        var metadata = {};\n      } else {\n        var data = payload.data;\n        var metadata = payload.metadata;\n        self.metaData.set(payload.metadata || {});\n      }\n\n      if(self.options.keepHistory) {\n        self.history[historyKey] = {data: data, loaded: new Date(), metadata: metadata};\n      }\n\n      if(version > self._loadedVersion) {\n        self._updateStore(data);\n        self._loadedVersion = version;\n      }\n\n      if(version == self._currentVersion) {\n        self.status.set({loaded: true});\n      }\n\n      self._storeDep.changed();\n    }\n  }\n};\n\nSearchSource.prototype._canUseHistory = function(historyKey) {\n  var historyItem = this.history[historyKey];\n  if(this.options.keepHistory && historyItem) {\n    var diff = Date.now() - historyItem.loaded.getTime();\n    return diff < this.options.keepHistory;\n  }\n\n  return false;\n};\n\nSearchSource.prototype._updateStore = function(data) {\n  var self = this;\n  var storeIds = _.pluck(this.store.find().fetch(), \"_id\");\n  var currentIds = [];\n  data.forEach(function(item) {\n    currentIds.push(item._id);\n    self.store.update(item._id, item, {upsert: true});\n  });\n\n  // Remove items in client DB that we no longer need\n  var currentIdMappings  = {};\n  _.each(currentIds, function(currentId) {\n    // to support Object Ids\n    var str = (currentId._str)? currentId._str : currentId;\n    currentIdMappings[str] = true;\n  });\n\n  _.each(storeIds, function(storeId) {\n    // to support Object Ids\n    var str = (storeId._str)? storeId._str : storeId;\n    if(!currentIdMappings[str]) {\n      self.store.remove(storeId);\n    }\n  });\n};\n\nSearchSource.prototype.search = function(query, options) {\n  this.currentQuery = query;\n  this._currentQueryDep.changed();\n\n  this._loadData(query, options);\n\n  if(this.options.localSearch) {\n    this._storeDep.changed();\n  }\n};\n\nSearchSource.prototype.getData = function(options, getCursor) {\n  options = options || {};\n  var self = this;\n  this._storeDep.depend();\n  var selector = {$or: []};\n\n  var regExp = this._buildRegExp(self.currentQuery);\n\n  // only do client side searching if we are on the loading state\n  // once loaded, we need to send all of them\n  if(this.getStatus().loading) {\n    self.searchFields.forEach(function(field) {\n      var singleQuery = {};\n      singleQuery[field] = regExp;\n      selector['$or'].push(singleQuery);\n    });\n  } else {\n    selector = {};\n  }\n\n  function transform(doc) {\n    if(options.transform) {\n      self.searchFields.forEach(function(field) {\n        if(self.currentQuery && doc[field]) {\n          doc[field] = options.transform(doc[field], regExp, field, self.currentQuery);\n        }\n      });\n    }\n    if(options.docTransform) {\n      return options.docTransform(doc);\n    }\n\n    return doc;\n  }\n\n  var cursor = this.store.find(selector, {\n    sort: options.sort,\n    limit: options.limit,\n    transform: transform\n  });\n\n  if(getCursor) {\n    return cursor;\n  }\n\n  return cursor.fetch();\n};\n\nSearchSource.prototype._fetch = function(source, query, options, callback) {\n  if(typeof this.fetchData == 'function') {\n    this.fetchData(query, options, callback);\n  } else if(Meteor.status().connected) {\n    this._fetchDDP.apply(this, arguments);\n  } else {\n    this._fetchHttp.apply(this, arguments);\n  }\n};\n\nSearchSource.prototype._fetchDDP = function(source, query, options, callback) {\n  Meteor.call(\"search.source\", this.source, query, options, callback);\n};\n\nSearchSource.prototype._fetchHttp = function(source, query, options, callback) {\n  var payload = {\n    source: source,\n    query: query,\n    options: options\n  };\n\n  var headers = {\n    \"Content-Type\": \"text/ejson\"\n  };\n\n  HTTP.post('/_search-source', {\n    content: EJSON.stringify(payload),\n    headers: headers\n  }, function(err, res) {\n    if(err) {\n      callback(err);\n    } else {\n      var response = EJSON.parse(res.content);\n      if(response.error) {\n        callback(response.error);\n      } else {\n        callback(null, response.data);\n      }\n    }\n  });\n};\n\nSearchSource.prototype.getMetadata = function() {\n  return this.metaData.get();\n};\n\nSearchSource.prototype.getCurrentQuery = function() {\n  this._currentQueryDep.depend();\n  return this.currentQuery;\n}\n\nSearchSource.prototype.getStatus = function() {\n  return this.status.get();\n};\n\nSearchSource.prototype.cleanHistory = function() {\n  this.history = {};\n};\n\nSearchSource.prototype._buildRegExp = function(query) {\n  query = query || \"\";\n\n  var afterFilteredRegExpChars = query.replace(this._getRegExpFilterRegExp(), \"\\\\$&\");\n  var parts = afterFilteredRegExpChars.trim().split(' ');\n\n  return new RegExp(\"(\" + parts.join('|') + \")\", \"ig\");\n};\n\nSearchSource.prototype._getRegExpFilterRegExp = _.once(function() {\n  var regExpChars = [\n    \"\\\\\", \"^\", \"$\", \"*\", \"+\", \"?\", \".\",\n     \"(\", \")\", \":\", \"|\", \"{\", \"}\", \"[\", \"]\",\n     \"=\", \"!\", \",\"\n  ];\n  var regExpCharsReplace = _.map(regExpChars, function(c) {\n    return \"\\\\\" + c;\n  }).join(\"|\");\n  return new RegExp(\"(\" + regExpCharsReplace + \")\", \"g\");\n});"
  },
  {
    "path": "lib/server.js",
    "content": "SearchSource = {};\nSearchSource._sources = {};\nvar bodyParser = Npm.require('body-parser');\n\nSearchSource.defineSource = function(name, callback) {\n  SearchSource._sources[name] = callback;\n};\n\nMeteor.methods({\n  \"search.source\": function(name, query, options) {\n    check(name, String);\n    check(query, Match.OneOf(String, null, undefined));\n    check(options, Match.OneOf(Object, null, undefined));\n    this.unblock();\n\n    // we need to send the context of the method\n    // that's why we use .call instead just invoking the function\n    return getSourceData.call(this, name, query, options);\n  }\n});\n\nvar postRoutes = Picker.filter(function(req, res) {\n  return req.method == \"POST\";\n});\n\npostRoutes.middleware(bodyParser.text({\n  type: \"text/ejson\"\n}));\n\npostRoutes.route('/_search-source', function(params, req, res, next) {\n  if(req.body) {\n    var payload = EJSON.parse(req.body);\n    try {\n      // supporting the use of Meteor.userId()\n      var data = DDP._CurrentInvocation.withValue({userId: null}, function() {\n        return getSourceData(payload.source, payload.query, payload.options);\n      });\n      sendData(res, null, data);\n    } catch(ex) {\n      if(ex instanceof Meteor.Error) {\n        var error = { code: ex.error, message: ex.reason };\n      } else {\n        var error = { message: ex.message };\n      }\n      sendData(res, error);\n    }\n  } else {\n    next();\n  }\n});\n\n\nfunction sendData(res, err, data) {\n  var payload = {\n    error: err,\n    data: data\n  };\n\n  res.end(EJSON.stringify(payload));\n}\n\nfunction getSourceData(name, query, options) {\n  var source = SearchSource._sources[name];\n  if(source) {\n    return source.call(this, query, options);\n  } else {\n    throw new Meteor.Error(404, \"No such search source: \" + name);\n  }\n}"
  },
  {
    "path": "package.js",
    "content": "Package.describe({\n  \"summary\": \"Reactive Data Source for Search\",\n  \"version\": \"1.4.3\n  \"git\": \"https://github.com/meteorhacks/search-source.git\",\n  \"name\": \"meteorhacks:search-source\"\n});\n\nNpm.depends({\n  \"body-parser\": \"1.10.1\"\n});\n\nPackage.onUse(function(api) {\n  configurePackage(api);\n  api.export(['SearchSource']);\n});\n\nPackage.onTest(function(api) {\n  configurePackage(api);\n\n  api.use(['tinytest', 'mongo-livedata'], ['client', 'server']);\n});\n\nfunction configurePackage(api) {\n  api.versionsFrom('METEOR@0.9.2');\n  api.use([\n    'tracker', 'underscore', 'mongo', 'reactive-var',\n    'http', 'ejson', 'check', 'ddp'\n  ], ['client']);\n\n  api.use(['ejson', 'check', 'ddp'], ['server']);\n  \n  api.use('meteorhacks:picker@1.0.1', 'server');\n\n  api.add_files([\n    'lib/server.js',\n  ], ['server']);\n\n  api.add_files([\n    'lib/client.js',\n  ], ['client']);\n}\n"
  }
]