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

@cmmv/elastic

v0.0.5

Published

Elastic Module for CMMV

Downloads

244

Readme

Description

The @cmmv/elastic module provides a seamless interface for integrating with Elasticsearch, enabling developers to manage indices, perform searches, and handle documents efficiently. Designed for scalability, the module integrates smoothly with the CMMV framework, supporting advanced Elasticsearch features while maintaining simplicity.

Features

  • Index Management: Create, delete, and check indices with ease.
  • Document Operations: Insert, update, delete, and search documents.
  • Integration with CMMV Framework: Built-in support for modular CMMV applications.
  • Dynamic Query Support: Execute complex search queries with decorators and utility methods.
  • Decorator-Based API: Simplified Elasticsearch interactions with intuitive decorators.

Installation

Install the @cmmv/elastic package via npm:

$ pnpm add @cmmv/elastic

Configuration

The @cmmv/elastic module requires Elasticsearch connection details, configurable through the .cmmv.config.js file:

Elastic Documentation

import * as fs from "node:fs";

module.exports = {
    env: process.env.NODE_ENV,

    elastic: {
        node: 'http://localhost:9200',
        //cloud: { id: '<cloud-id>' },
        /*tls: {
            ca: fs.readFileSync('./http_ca.crt'),
            rejectUnauthorized: false
        },
        auth: {
            bearer: process.env.ELASTIC_BEARER || "",
            apiKey: process.env.ELASTIC_APIKEY || "",
            username: process.env.ELASTIC_USERNAME || "",
            password: process.env.ELASTIC_PASSWORD || ""
        }*/
    }
};

Setting Up the Application

In your index.ts, include the ElasticModule and services required for your application:

import { Application } from "@cmmv/core";
import { DefaultAdapter, DefaultHTTPModule } from "@cmmv/http";
import { ElasticModule, ElasticService } from "@cmmv/elastic";

Application.create({
    httpAdapter: DefaultAdapter,
    modules: [
        DefaultHTTPModule,
        ElasticModule,
    ],
    services: [ElasticService],
});

Usage

Creating an Index

Allows you to create a new Elasticsearch index with configurable settings like the number of shards and replicas. Use this to structure your data storage.

await ElasticService.createIndex('my-index', {
    number_of_shards: 1,
    number_of_replicas: 0,
});

Checking Index Existence

Verifies whether a specific index exists in Elasticsearch. Useful for conditional operations to avoid duplicate index creation.

const exists = await ElasticService.checkIndexExists('my-index');
console.log(`Index exists: ${exists}`);

Deleting an Index

Deletes an existing index from Elasticsearch. Use this to remove outdated or unnecessary indices.

await ElasticService.deleteIndex('my-index');

Inserting Documents

Adds a new document to a specified index. Ideal for storing and indexing structured data for retrieval and analysis.

await ElasticService.insertDocument('my-index', { id: 1, name: 'Test Document' });

Updating Documents

Updates an existing document in a specified index. Use this to modify stored data without creating duplicates.

await ElasticService.updateDocument('my-index', 'document-id', { name: 'Updated Name' });

Searching Documents

Performs a search query on a specified index. Use this to retrieve documents matching specific criteria.

const results = await ElasticService.searchDocuments('my-index', {
    query: { match: { name: 'Test Document' } },
});
console.log(results);

Alias Management

Creates or checks the existence of an alias for an index. Aliases are useful for abstracting access to indices, enabling flexible data management.

await ElasticService.createAlias('my-index');
const aliasExists = await ElasticService.checkAliasExists('my-alias');

Rollover Index

Performs a rollover operation on an alias, creating a new index when specified conditions are met (e.g., max documents). Useful for managing large-scale data storage.

await ElasticService.performRollover('my-alias', { max_docs: 1000 });