Connection in jsstore is instance of class Connection. All apis are called using connection.

Syntax


With Web Worker

var connection = new JsStore.Connection(new Worker('jsstore worker path'));

example - https://github.com/ujjwalguptaofficial/jsstore-examples/tree/master/withoutwebworker

Without web worker

var connection = new JsStore.Connection();

example - https://github.com/ujjwalguptaofficial/jsstore-examples/tree/master/simple_example


How to create connection in webpack


  1. Install jsstore using npm or yarn - npm i jsstore

  2. Install file-loader - npm i file-loader -D

  3. Create a file jsstore_con.js or jsstore_con.ts (for typescript). This file will be used to create the jsstore connection. You can choose any file name.

  4. Inside the file jsstore_con.js, write below code -

const getWorkerPath = () => {

    // return dev build when env is development
    if (process.env.NODE_ENV === 'development') {

        return require("file-loader?name=scripts/[name].[hash].js!jsstore/dist/jsstore.worker.js");

    }
    else { // return prod build when env is production

        return require("file-loader?name=scripts/[name].[hash].js!jsstore/dist/jsstore.worker.min.js");

    }
};

const workerPath = getWorkerPath();
export const connection = new JsStore.Connection(new Worker(workerPath));
  1. Step 4 creates a connection and export the connection variable. You can import this connection variable to anywhere in your code.


In the above code we are using file-loader to load jsstore worker file but you are free to use your own technique to load jsstore worker. The simplest approach is download jsstore.worker.js and then specify its path.

If you are finding difficult to understand, please take a look at examples or our medium page


Important points :-

  • The connection variable will be used to execute the further query.
  • A connection will handle one db at a time.
  • Do not create multiple connection for one db.