{"id":17831211,"url":"https://github.com/crsten/mongoose-algolia","last_synced_at":"2025-03-19T07:31:49.323Z","repository":{"id":44843522,"uuid":"73807666","full_name":"crsten/mongoose-algolia","owner":"crsten","description":null,"archived":false,"fork":false,"pushed_at":"2022-12-08T22:21:03.000Z","size":1108,"stargazers_count":36,"open_issues_count":18,"forks_count":12,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-05-27T20:48:55.051Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crsten.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-15T11:36:06.000Z","updated_at":"2024-03-05T11:38:44.000Z","dependencies_parsed_at":"2023-01-25T07:45:58.424Z","dependency_job_id":null,"html_url":"https://github.com/crsten/mongoose-algolia","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-algolia","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-algolia/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-algolia/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-algolia/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crsten","download_url":"https://codeload.github.com/crsten/mongoose-algolia/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243976644,"owners_count":20377693,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-27T19:42:26.373Z","updated_at":"2025-03-19T07:31:49.066Z","avatar_url":"https://github.com/crsten.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Community libraries"],"sub_categories":[],"readme":"# mongoose-algolia\n\n[![Build Status](https://travis-ci.org/crsten/mongoose-algolia.svg?branch=master\u0026style=flat-square)](https://travis-ci.org/crsten/mongoose-algolia)\n[![npm](https://img.shields.io/npm/dt/mongoose-algolia.svg?style=flat-square)](https://www.npmjs.com/package/mongoose-algolia)\n[![npm](https://img.shields.io/npm/v/mongoose-algolia.svg?style=flat-square)](https://www.npmjs.com/package/mongoose-algolia)\n\n[Mongoose](http://mongoosejs.com/) plugin to automatically sync documents to [Algolia](https://www.algolia.com/)\n\nThis module syncs your documents from mongoose to Algolia for you. By plugging into Mongoose, Algolia will automatically synchronize your new/changed/removed documents.\n\n### Installation\n\n`npm install --save mongoose-algolia`\n\n### Usage\n\n```js\nconst mongoose = require('mongoose')\nconst Schema = mongoose.Schema\nconst mongooseAlgolia = require('mongoose-algolia')\n\nlet YourSchema = new Schema({\n  title: String,\n  description: String,\n  author: String,\n})\n\nYourSchema.plugin(mongooseAlgolia, {\n  appId: YOUR_ALGOLIA_APP_ID,\n  apiKey: YOUR_ALGOLIA_API_KEY,\n  indexName: 'yourSchema', //The name of the index in Algolia, you can also pass in a function\n  selector: '-author', //You can decide which field that are getting synced to Algolia (same as selector in mongoose)\n  populate: {\n    path: 'comments',\n    select: 'author',\n  },\n  defaults: {\n    author: 'unknown',\n  },\n  mappings: {\n    title: function(value) {\n      return `Book: ${value}`\n    },\n  },\n  virtuals: {\n    whatever: function(doc) {\n      return `Custom data ${doc.title}`\n    },\n  },\n  filter: function(doc) {\n    return !doc.softdelete\n  },\n  debug: true, // Default: false -\u003e If true operations are logged out in your console\n})\n\nlet Model = mongoose.model('YourSchema', YourSchema)\n\nModel.SyncToAlgolia() //Clears the Algolia index for this schema and synchronizes all documents to Algolia (based on the settings defined in your plugin settings)\nModel.SetAlgoliaSettings({\n  searchableAttributes: ['name', 'properties', 'shows'], //Sets the settings for this schema, see [Algolia's Index settings parameters](https://www.algolia.com/doc/api-client/javascript/settings#set-settings) for more info.\n})\n```\n\n### Options\n\n#### appId / apiKey\n\nYou can find this in your `Algolia` instance\n\n#### indexName\n\nThis will be the name of the index in `Algolia`.\n\nThere are 2 ways of setting the `indexName` property\n\n1. as a string\n\n```js\nYourSchema.plugin(mongooseAlgolia, {\n  //other props...\n  indexName: 'yourSchema',\n  //other props...\n})\n```\n\n2. as a function (dynamically)\n\n```js\nYourSchema.plugin(mongooseAlgolia, {\n  //other props...\n  indexName: function(doc) {\n    return `yourSchema_${somethingelse}`\n  },\n  //other props...\n})\n```\n\nThis allows you to have multiple indexes splittet by some properties.\nVery handy in situations where you want to have a seperate index for each company or similar...\n\n**Powerful hint:** Your indexname function can return an array aswell! Let me give you an example: One user belongs to multiple companies, and each company has its own user index -\u003e return an array of all indices the document belongs to, to sync it to every index (company users).\n\n#### selector\n\nYou can decide which field should be excluded or included by setting the `selector` property (same as in mongoose) _Must be a string_\n\n#### populate\n\nYou can populate fields before sending them to `Algolia` by setting the populate property. (same as in mongoose, see [docs about population](http://mongoosejs.com/docs/api.html#document_Document-populate))\n\n#### defaults\n\nYou can set default values for fields that are blank in mongoose.\nThis is very useful in cases where you have documents with optional fields. Since it isn't possible to query `null` values in algolia, setting those fields to 'unknown' or 'notset' makes them searchable/filterable.\n\n_You can nest properties_\n\n#### mappings\n\nIf you want to modify your fields before sending it to algolia you can create mapping functions.\n\nLet me show you an example:\n\nDataset:\n\n```js\n  {\n    name: {\n      firstname: 'Peter',\n      lastname: 'Griffin'\n    }\n  }\n```\n\nNow we dont want to store each field individually but as one string instead. We do it the following way:\n\n```js\nmappings: {\n  name: function(value) {\n    //Value is the 'name' object\n    return `${value.firstname} ${value.lastname}`; //ES6 is awesome :)\n  }\n}\n```\n\n_You can nest properties_\n\n#### virtuals\n\nIf you need additional fields that are not part of your model, you can use virtuals to create any field you need.\n\nLet me show you an example:\n\nDataset:\n\n```js\n  {\n    users: ['uid1','uid2'],\n    groups: ['gid1','gid2']\n  }\n```\n\nNow we dont want to store each field individually but as one array named `acl` instead. We do it the following way:\n\n```js\nvirtuals: {\n  acl: function(doc) {\n    return [...doc.users, ...doc.groups]; //ES6 is awesome :)\n  }\n}\n```\n\n_No nesting here_\n\n#### filter\n\nIf you want to prevent some documents from being synced to algolia, you can do it by letting it go through the filter function.\nThe first property is the document.\n\nSimply return true or false (same principle as Array.filter) in order to tell mongooose-algolia if you want to sync it or not.\n\n_Hint_ You can enable softdeletion support (like [mongoose-delete](https://github.com/dsanel/mongoose-delete)) by setting filter function to following:\n\n```js\nfilter: function(doc) {\n  return !doc.softdelete;\n}\n```\n\n#### debug\n\nYou can enable logging of all operations by setting `debug` to true\n\n### Methods\n\n#### SyncToAlgolia\n\nCall this method if you want to sync all your documents with algolia (for single doc sync see **doc.SyncToAlgolia**)\n\nThis method clears the Algolia index for this schema and synchronizes all documents to Algolia (based on the settings defined in your plugin settings)\n\n```js\nModel.SyncToAlgolia()\n```\n\n#### SetAlgoliaSettings\n\nSets the settings for this schema, see [Algolia's Index settings parameters](https://www.algolia.com/doc/api-client/javascript/settings#set-settings) for more info about available parameters.\n\n```js\nModel.SetAlgoliaSettings({\n  searchableAttributes: ['name', 'properties', 'shows'],\n})\n```\n\n#### doc.SyncToAlgolia\n\ndoc = document from mongoose\n\nCall this method if you want to sync your document to Algolia\n\n```js\ndoc.SyncToAlgolia()\n```\n\n#### doc.RemoveFromAlgolia\n\ndoc = document from mongoose\n\nCall this method if you want to remove your document from the Algolia index\n\n```js\ndoc.RemoveFromAlgolia()\n```\n\n## License\n\n[The MIT License](http://opensource.org/licenses/MIT)\nCopyright (c) Carsten Jacobsen\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrsten%2Fmongoose-algolia","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrsten%2Fmongoose-algolia","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrsten%2Fmongoose-algolia/lists"}