npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pgmharikrishnan/mongodb-atlas-search

v1.0.3

Published

Automatic mapping generation and index management for atlas search based on mongoose schema

Downloads

58

Readme

mongodb-atlas-search

mongodb-atlas-search is a library for creating mongodb atlas mapping, automating the creation and updation of atlas indexes as needed.

Installation

npm i @pgmharikrishnan/mongodb-atlas-search

Usage

import { MongoDbAtlas } from '@pgmharikrishnan/mongodb-atlas-search';
const options = {
  privateKey: string;
  publicKey: string;  // Get it from 'https://docs.atlas.mongodb.com/configure-api-access/'
  baseUrl: string;  // 'https://cloud.mongodb.com/api/atlas/v1.0/'
  groupId: string; 
  clusterName: string;
}
const databaseName="DbName"

## Initialising with credentials
const atlasSearch = new MongoDbAtlas(options,databaseName);

## Fetching indexes created on a collection
 atlasSearch.getAtlasSearchIndexes(collectionName) // 'returns array of AtlasSearchIndex'

## Building atlas mapping schema from mongoose schema
  const atlasMapping = atlasSearch.buildMappingFromSchema(schema.schema); //'returns atlas mapping'

## Creating new atlas search index on a collection
const createResponse = await atlasSearch.createAtlasSearchIndex({
  database: databaseName,
  collectionName: collectionName,
  name: 'default', // 'Index name'
  mappings: atlasMapping, // 'Generated atlas mapping from buildMappingFromSchema'
});

## Updating existing atlas search index
const patchResponse = await atlasSearch.patchAtlasSearchIndex(
  defaultIndex.indexID, // 'Can be fetched using getAtlasSearchIndexes"
  {
    database: defaultIndex.database,
    collectionName: defaultIndex.collectionName,
    name: defaultIndex.name,
    mappings: atlasMapping, // 'Generated atlas mapping from buildMappingFromSchema'
  },
);

Example

import * as pluralize from 'mongoose-legacy-pluralize';
import { get, isEqual } from 'lodash';
 const collectionOptions = get(schema.schema, 'options');
    const collectionName =
      collectionOptions?.collection ?? pluralize(schema.name);
    const atlasSearchIndex = <AtlasSearchIndex[]>(
      await atlasSearch.getAtlasSearchIndexes(collectionName)
    );
    const defaultIndex = atlasSearchIndex.find(
      (a) => a.name === 'default',
    );

    // Building mappings from mongoose schema
    const atlasMapping = atlasSearch.buildMappingFromSchema(
      schema.schema,
    );
    if (defaultIndex) {
      // Default index exists so patch flow
      if (isEqual(atlasMapping, defaultIndex.mappings)) {
        // No changes in mapping skipping patch
      } else {
        // New changes in mapping sending patch request
        const patchResponse = await atlasSearch.patchAtlasSearchIndex(
          defaultIndex.indexID,
          {
            database: defaultIndex.database,
            collectionName: defaultIndex.collectionName,
            name: defaultIndex.name,
            mappings: atlasMapping,
          },
        );
        console.log(JSON.stringify(atlasMapping, null, 4));
        console.log(JSON.stringify(patchResponse, null, 4));
        console.log(`Mapping updated for ${collectionName}`);
      }
    } else {
      // Default index does not exists so creating default index
      console.log('Initiating creation of default index');
      const createResponse = await atlasSearch.createAtlasSearchIndex({
        database: databaseName,
        collectionName: collectionName,
        name: 'default',
        mappings: atlasMapping,
      });
      console.log(JSON.stringify(atlasMapping, null, 4));
      console.log(JSON.stringify(createResponse, null, 4));
      console.log(`Mapping created for ${collectionName}`);
    }
  }
},

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.