0

I have a University project requirement to develop simple database management in C++, using binary files for storing database entries. The program is mean to run in the terminal but I've decided that is a good opportunity for me to work in a more complex project that can sum to my portfolio. So, I wanted to add Electron for the frontend, and every "backend" action will be C++ functions compiled as node modules, creating, reading, and writing binary files.

I understand that using C++ as node modules is not exactly intended for this purposes, but I'm required to use C++ for operations with binary files, so looking for a bit of help as I'm a very beginner with this.

I'm using a controller.h for the binary files operations. In "plain" C++, here's the code:

#include <iostream>
//#include <stdio.h>
//#include <stdlib.h>
#include <fstream>
#include <cstring>
#include <string>

using namespace std;
template <typename T>
class Controller
{
private:
    T buf;
    char name[40]{};

public:
    explicit Controller(char *name) { strcpy(this->name, name); }
    char *GetName() { return name; }
    bool Insert(T model);
    //    int Search(T &bus);
    //    int Delete(T model);
};

template <typename T>
bool Controller<T>::Insert(T model)
{
    fstream file;
    string db_dir = "db//";
    string file_path = db_dir + name;
    file.open(file_path.c_str(), ios::out | ios::binary);
    if (file.is_open())
    {
        file.write((char *)&model, sizeof(model));
        file.close();
        cout << "Successfully wrote" << endl;
        return true;
    }
    else
    {
        cout << "Failed" << endl;
        return false;
    }
}

And here's my intent to use the napi.h for the NAPI::Object:

#include <iostream>
#include <napi.h>
//#include <stdio.h>
//#include <stdlib.h>
#include <fstream>
#include <cstring>
#include <string>

using namespace std;

namespace Controller
{
    template <typename T>
    bool Insert(T);
    template <typename T>
    Napi::String InsertWrapped(const Napi::CallbackInfo &info);
    Napi::Object Init(Napi::Env env, Napi::Object exports);
}

template <typename T>
bool Controller::Insert(T model)
{
    return true;
}

template <typename T>
Napi::String Controller::InsertWrapped(const Napi::CallbackInfo &info)
{
    Napi::Env env = info.Env();
    Napi::String returnValue = Napi::String::New(env, Controller::Insert(T model));

    return returnValue;
}

Napi::Object Controller::Init(Napi::Env env, Napi::Object exports)
{
    exports.Set(
        "hello", Napi::Function::New(env, Controller::InsertWrapped));

    return exports;
}

Here's the main.cpp where I'm exporting the node module:

#include <napi.h>
#include "controller/Controller.h"
#include "auth/Login.cpp"
#include "model/Structs.cpp"

// Napi::String Method(const Napi::CallbackInfo &info)
// {
//     Napi::Env env = info.Env();
//     return Napi::String::New(env, "world");
// }

// Napi::Object Init(Napi::Env env, Napi::Object exports)
// {
//     exports.Set(Napi::String::New(env, "hello"),
//                 Napi::Function::New(env, Method));
//     return exports;
// }
void main()
{
    Cliente client = *new Cliente();
}

NODE_API_MODULE(hello, Controller::InsertWrappedInit);

But I've got the error

name followed by '::' must be a class or namespace name

Additionally, here's my modules/index.js where I'm importing the c++ module:

var TDS = require('../build/Release/TDS.node');

module.exports = TDS;

And the main.js where I use the C++ module and the electron app:

const path = require('path');
const { app, BrowserWindow } = require('electron');

const TDS = require('./modules/index');
console.log(TDS.hello());

function createMainWindow(){
    const mainWindow = new BrowserWindow({
        title: 'Transactional System',
        width: 500,
        height: 600
    });
    console.log(TDS.hello);
    mainWindow.loadFile(path.join(__dirname, './app/renderer/index.html'));
}

app.whenReady().then(() => {
    createMainWindow(); 
});

Appreciate any guidance for the process of creating c++ node modules.

Jesus Jimenez
  • 351
  • 1
  • 3
  • 13

0 Answers0