2

I'm trying to connect Apache Ignite with Nest JS. I'm getting error regarding the library I used: 'apache-ignite-client'.

Error: TypeError: apache_ignite_client_1.default is not a constructor.

Code

import { Injectable } from '@nestjs/common';
import IgniteClient from 'apache-ignite-client';
import SqlFieldsQuery from 'apache-ignite-client';

@Injectable()
export class AlarmsService {

    private igniteClient: IgniteClient;
    constructor(
    ) {
        this.igniteClient = new IgniteClient();
    }
    
    async connect() {
        await this.igniteClient.connect('localhost:10800');
    }

    async getData() {
        await this.connect();
        const query = new SqlFieldsQuery.query("SELECT * FROM alarms");
        const result = await this.igniteClient.query(query);
        return result.getAll();
    }
}

1 Answer 1

0

Since apache-ignite-client library doesn't support ES modules import you should use CommonJS import instead, as shown below:

const IgniteClient = require('apache-ignite-client');

Also, I would like to notice that there are several additional issues in your code:

  1. IgniteClientConfiguration object should be used instead of String for igniteClient.connect(cfg) method execution.
  2. SqlFieldsQuery doesn't have a query method, the SQL query string should be passed in SqlFieldsQuery's constructor instead.
  3. The cache.query() method should be used instead of igniteClient.query() to execute the query.

More details regarding that can be found in the documentation here.

Also, here is an example that is based on your code:

import { Injectable } from '@nestjs/common';

const IgniteClient = require('apache-ignite-client');
const SqlFieldsQuery = IgniteClient.SqlFieldsQuery;
const IgniteClientConfiguration = IgniteClient.IgniteClientConfiguration;

@Injectable() 
export class AlarmsService {
    private igniteClient: any;

    constructor() {
        this.igniteClient = new IgniteClient();
    }

    async connect() {
        const igniteClientConfiguration = new IgniteClientConfiguration('127.0.0.1:10800');
        await this.igniteClient.connect(igniteClientConfiguration);
    }

    async getData() {
        await this.connect();
        const cache = await this.igniteClient.getOrCreateCache('alarms');
        const query = new SqlFieldsQuery('SELECT * FROM alarms');
        const result = await cache.query(query);
        return result.getAll();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.