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

@oak-digital/nestjs-mindsdb

v0.0.1

Published

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

Downloads

2

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
npm install @oak-digital/nestjs-mindsdb
# pnpm
pnpm install @oak-digital/nestjs-mindsdb

Setup

This module works by defining your models in your code. It is currently not possible to create models dynamically.

In your module, you should import this module with the forRoot method.

// app.module.ts
@Module({
  imports: [
    MindsdbModule.forRoot({
      // The predefined models to use
      models: myModels,
      // The default project to use, 'mindsdb' by default
      project: 'mindsdb',
      // If the connection is omitted,
      // it will use the following default connection options
      connection: {
        host: 'http://127.0.0.1:47334',
        user: 'mindsdb',
        password: '',
        managed: false,
      },
    }),
  ],
})

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

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

The models are not created by default, so you need to create them with the create method

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');

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 '@oak-digital/nestjs-mindsdb';
import { AbstractMindsdbController } from '@oak-digital/nestjs-mindsdb';

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

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