Skip to content

Getting Started

Steven Barnett edited this page Nov 15, 2018 · 1 revision

Getting Started

In order to feel the power of JSIndex, it helps to work with large collections of data. Eventually I may host a sample data set, but for the time being I just did a Google search for "sample csv database". Feel free to use any large CSV, but I found the following: https://support.spatialkey.com/spatialkey-sample-csv-data/

Loading your data

JSIndex provides a helper function for reading CSV files (and eventually other data types), so we can start with this:

const fs = require("fs");
require("jsindex");

const csvData = fs.readFileSync("/path/to/your.csv", "utf8");
const collection = Array.load(csvData);

Wasn't that easy? This demonstrates the Array.load method which we will go into more detail on later.

Indexing your data

Most of the features of JSIndex can't be used without first indexing your data, so let's get started. The sample data set I found has a lot of fields that we likely won't be filtering on (fl_site_limit, tiv_2011, etc), so let's just index the fields we can reasonably use as keys:

collection.index(["policyID", "statecode", "county", "line", "construction", "point_granularity"]);

Now that we've done this, you can access the individual indexes like so:

console.log(collection.idx.policyID);
console.log(collection.idx.county);

Filtering the data

Let's say that we're only interested in Commercial claims from Suwannee County. We can trivially filter like so:

const dataOfInterest = collection.search({
    line: "Commercial",
    county: "SUWANNEE COUNTY"
});

Saving the filtered data

Now let's say that we've populated a data table with our collection, the user has done some filtering, sorting, and searching, and now they want to save the results. Couldn't be easier!

const savedData = Array.store(dataOfInterest);
fs.writeFileSync("/path/to/output.csv", savedData);

More documentation

There are additional features (currently just joining/merging, but more may come later) and there's additional documentation beyond added features (overridden array natives and the Proxy object), so keep reading the wiki for more details.