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

nestjs-mindsdb

v1.4.12

Published

A NestJS module for integrating with MindsDB. This allows for easy model management and prediction within your NestJS application.

Downloads

10

Readme

MindsDB NestJS Module

A NestJS module for integrating with MindsDB. This allows for easy model management and prediction within your NestJS application.

Installation

npm install nestjs-mindsdb

Defining Models

Models in MindsDB are described through the IModel interface. Here's how you can define your models:

import { Granularity, IModel } from "./mindsdb.models";

export const Models = new Map<string, IModel>([
  [
    "balance_auto",
    {
      name: "balance_auto",
      granularity: Granularity.day,
      targetColumn: "sum",
      predictOptions: {
        join: "views.enriched_balance",
        where: ["t.customerId = $CUSTOMER_ID$", "t.date > $DATE$"],
      },
      trainingOptions: {
        select: "select * from enriched_balance",
        groupBy: "customerId",
        orderBy: "date",
        horizon: 60,
        window: 90,
        // using: {
        //   submodels: [{ module: 'GluonTSMixer', args: {} }],
        // },
      },
      finetuneOptions: {
        select: "select * from enriched_balance where customerId = $CUSTOMER_ID$ and date > $DATE$",
        integration: undefined,
      },
    },
  ],
  [
    "balance_gluon",
    {
      name: "balance_gluon",
      granularity: Granularity.day,
      targetColumn: "sum",
      view: {
        select: "select * from production.banking.enriched_balance",
        name: "balance_gluon_enriched_balance",
      },
      predictOptions: {
        join: "views.enriched_balance",
        where: ["t.customerId = $CUSTOMER_ID$", "t.date > $DATE$"],
      },
      trainingOptions: {
        select: "select * from balance_gluon_enriched_balance",
        groupBy: "customerId",
        orderBy: "date",
        horizon: 60,
        window: 90,
        // using: {
        //   submodels: [{ module: 'GluonTSMixer', args: {} }],
        // },
      },
      finetuneOptions: {
        select: "select * from balance_gluon_enriched_balance",
        integration: undefined,
      },
    },
  ],
]);

Placeholders such as $CUSTOMER_ID$ and $DATE$ can be used in the model definition. These will be replaced when you're making predictions.


Usage

Setting up the Controller

To expose your models through a RESTful API, extend the AbstractMindsdbController and use NestJS's @Controller decorator. This controller will automatically have all the endpoints corresponding to the methods in the AbstractMindsdbController.

import { Controller } from '@nestjs/common';
import { MindsdbService } from 'nestjs-mindsdb';
import { AbstractMindsdbController } from 'nestjs-mindsdb';

@Controller('mindsdb')
export class MindsdbController extends AbstractMindsdbController {
  constructor(mindsdbService: MindsdbService) {
    super(mindsdbService);
  }
}

Using the MindsdbService

The MindsdbService provides several methods that correspond to different operations you can perform on your models. If parameters are not provided will use the one's defined on the IModel object for each.

1. Create a Model

const createDto = new CreateMindsdbDto();
createDto.name = 'balance_auto';
mindsdbService.create(createDto);

2. Predict Using a Model

const predictDto = new PredictMindsdbDto();
predictDto.join = "views.enriched_balance";
predictDto.where = ["t.customerId = $CUSTOMER_ID$", "t.date > $DATE$"];
predictDto.limit = 100;
predictDto.params = {
  $CUSTOMER_ID$: 1234,
  $DATE$: new Date('2023-01-01')
};
mindsdbService.predict('balance_auto', predictDto);

3. Fine-tune a Model

const finetuneDto = new FinetuneMindsdbDto();
finetuneDto.select = "select * from enriched_balance where customerId = $CUSTOMER_ID$ and date > $DATE$";
finetuneDto.params = {
  $CUSTOMER_ID$: 1234,
  $DATE$: new Date('2023-01-01')
};
mindsdbService.finetune('balance_auto', finetuneDto);

4. Retrain a Model

const retrainDto = new RetrainMindsDbDto();
retrainDto.select = "select * from balance_gluon_enriched_balance";
retrainDto.groupBy = "customerId";
retrainDto.orderBy = "date";
retrainDto.window = 90;
retrainDto.horizon = 60;
mindsdbService.retrain('balance_auto', retrainDto);

5. Retrieve All Models

const allModels = mindsdbService.findAll();

6. Retrieve a Specific Model

mindsdbService.findOne('balance_auto');

7. Remove a Model

mindsdbService.remove('balance_auto');

Remember to adjust any paths, references, and examples according to your project structure and requirements.

Configuration

To utilize MindsDB within your NestJS application, you have to set up the module. Given the nature of this module being dynamic, configurations are essential for its proper functioning. Here's a basic setup:

import { MindsdbModule } from 'nestjs-mindsdb';
import { Models } from './path-to-your-models-definition';

@Module({
  imports: [MindsdbModule.forRoot(Models)],
})
export class AppModule {}