Store()

Stores are containers of records.
Factory pattern: this class cannot be construced using the new keyword. This class is constructed when calling a specific method or attribute, e.g. constructing the module:qm.Base using schema or with the module:qm.Base#createStore.

Examples

Creating a store with createStore function

// import qm module
var qm = require('qminer');
// factory based construction using base.createStore
var base = new qm.Base({ mode: 'createClean' });
base.createStore([{
   name: "People",
   fields: [
       { name: "Name", type: "string", primary: true },
       { name: "Gender", type: "string", shortstring: true },
       { name: "Age", type: "int" }
   ],
   joins: [
       { name: "ActedIn", type: "index", store: "Movies", inverse: "Actor" },
       { name: "Directed", type: "index", store: "Movies", inverse: "Director" }
   ],
   keys: [
       { field: "Name", type: "text" },
       { field: "Gender", type: "value" }
   ]
},
{
   name: "Movies",
   fields: [
       { name: "Title", type: "string", primary: true },
       { name: "Plot", type: "string", store: "cache" },
       { name: "Year", type: "int" },
       { name: "Rating", type: "float" },
       { name: "Genres", type: "string_v", codebook: true }
   ],
   joins: [
       { name: "Actor", type: "index", store: "People", inverse: "ActedIn" },
       { name: "Director", type: "index", store: "People", inverse: "Directed" }
   ],
   keys: [
       { field: "Title", type: "value" },
       { field: "Plot", type: "text", vocabulary: "voc_01" },
       { field: "Genres", type: "value" }
   ]
}]);
base.close();

Creating store with schema in base constructor

// import qm module
var qm = require('qminer');
// using the base constructor
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Class",
       fields: [
           { name: "Name", type: "string" },
           { name: "StudyGroup", type: "string" }
       ]
   }]
});
base.close();

Properties

allRecords

Creates a record set containing all the records from the store. Type module:qm.RecordSet.

backwardIter

Returns an iterator for iterating over the store form end to start. Type module:qm.Iterator.

base

Returns the base, in which the store is contained. Type module:qm.Base.

empty

Checks if the store is empty. Type boolean.

fields

Gives an array of all field descriptor objects. Type Array of objects, where the objects contain the properties:
1. id - The ID of the field. Type B4numberB4.
2. name - The name of the field. Type string.
3. type - The type of the field. Type string.
4. nullable - If the field value can be null. Type boolean.
5. internal - If the field is internal. Type boolean.
6. primary - If the field is primary. Type boolean.

first

Returns the first record of the store. Type module:qm.Record.

forwardIter

Returns an iterator for iterating over the store from start to end. Type module:qm.Iterator.

joins

Gives an array of all join descriptor objects. Type Array of objects, where the objects contain the properties:
1. id - The ID of the join. Type B4numberB4.
2. name - The name of the join. Type string.
2. store - The store the join was created in. Type string.
2. inverse - The inverse join. Type string.
3. type - The type of the field. Type string.
4. key - The index key. Type module:qm~DetailKeyObject.

keys

Gives an array of all key descriptor objects. Type Array of module:qm~DetailKeyObject.

last

Returns the last record of the store. Type module:qm.Record.

length

Gives the number of records. Type number.

name

Gives the name of the store. Type string.

Methods

addStreamAggr(arg)

Adds a stream aggregate to the store. For use example see module:qm.StreamAggr constructor example.

Parameter

Name Type Optional Description

arg

(module:qm~StreamAggregator or function())

 

Constructor arguments. There are two argument types:
1. Using the module:qm~StreamAggregator object,
2. using a function/JavaScript class. The function has defined The object containing the schema of the stream aggregate or the function object defining the operations of the stream aggregate.

addTrigger(trigger)

Adds a stream aggregate to a store.

Parameter

Name Type Optional Description

trigger

function()

 

The trigger containing the method module:qm.StreamAggr#onAdd and optional module:qm.StreamAggr#onUpdate and module:qm.StreamAggr#onDelete.

cell(recId, fieldName) → (number, string, Array of number, or Array of string)

Gives the field value of a specific record.

Example

//import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Festivals",
       fields: [
           { name: "Name", type: "string" },
           { name: "Type", type: "string" },
           { name: "Location", type: "string" }
       ]
   }]
});
// add some records in the store
base.store("Festivals").push({ Name: "Metaldays", Type: "music", Location: "Tolmin, Slovenia" });
base.store("Festivals").push({ Name: "Festival de Cannes", Type: "movie", Location: "Cannes, France" });
base.store("Festivals").push({ Name: "The Festival of Chocolate", Type: "food", Location: "Hillsborough, USA" });
// get the field value of the second record for field "Type"
var fieldValue = base.store("Festivals").cell(1, "Type"); // returns "movie"
base.close();

Parameters

Name Type Optional Description

recId

number

 

The record id.

fieldName

string

 

The field name.

Returns

(number, string, Array of number, or Array of string)B The fieldName value of the record with ID recId.

clear([num]) → number

Deletes the first records in the store.

Example

// import qm module
var qm = require('qminer');
// create a base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "TVSeries",
       fields: [
           { name: "Title", type: "string", primary: true },
           { name: "NumberOfEpisodes", type: "int" }
       ]
   }]
});
// add some records in the store
base.store("TVSeries").push({ Title: "Archer", NumberOfEpisodes: 75 });
base.store("TVSeries").push({ Title: "The Simpsons", NumberOfEpisodes: 574 });
base.store("TVSeries").push({ Title: "New Girl", NumberOfEpisodes: 94 });
base.store("TVSeries").push({ Title: "Rick and Morty", NumberOfEpisodes: 11 });
base.store("TVSeries").push({ Title: "Game of Thrones", NumberOfEpisodes: 47 });
// deletes the first 2 records (Archer and The Simpsons) in TVSeries
base.store("TVSeries").clear(2); // returns 3
// delete all remaining records in TVStore
base.store("TVSeries").clear();  // returns 0
base.close();

Parameter

Name Type Optional Description

num

number

Yes

The number of deleted records. If the number is given, the first num records will be deleted.

Returns

numberB The number of remaining records in the store.

each(callback) → module:qm.Store

Executes a function on each record in store.

Example

// import qm module
var qm = require('qminer');
// create a base containing the store Class
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Class",
       fields: [
           { name: "Name", type: "string" },
           { name: "StudyGroup", type: "string" }
       ]
   }]
});
// add some records to the store
base.store("Class").push({ Name: "Abed", StudyGroup: "A" });
base.store("Class").push({ Name: "Annie", StudyGroup: "B" });
base.store("Class").push({ Name: "Britta", StudyGroup: "C" });
base.store("Class").push({ Name: "Jeff", StudyGroup: "A" });
// change the StudyGroup of all records of store Class to A
base.store("Class").each(function (rec) { rec.StudyGroup = "A"; });   // all records in Class are now in study group A
base.close();

Parameter

Name Type Optional Description

callback

function()

 

Function to be executed. It takes two parameters:
1. rec - The current record. Type module:qm.Record.
2. idx - The index of the current record (optional). Type number.

Returns

module:qm.StoreB Self.

field(fieldName) → object

Gets the details of the selected field.

Example

// import qm module
var qm = require("qminer");
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "People",
       fields: [
           { name: "Name", type: "string", primary: true },
           { name: "Gender", type: "string" },
           { name: "Age", type: "int" }
       ]
   }]
});
// get the details of the field "Name" of store "People"
// it returns a JSON object:
// { id: 0, name: "Name", type: "string", primary: true, nullable: false, internal: false }
var details = base.store("People").field("Name");
base.close();

Parameter

Name Type Optional Description

fieldName

string

 

The field name.

Returns

objectB The object containing the details of the field. The properties are:
1. id - The ID of the field. Type B4numberB4.
2. name - The name of the field. Type string.
3. type - The type of the field. Type string.
4. nullable - If the field value can be null. Type boolean.
5. internal - If the field is internal. Type boolean.
6. primary - If the field is primary. Type boolean.

getMatrix(fieldName) → (module:la.Matrix or module:la.SparseMatrix)

Gives a matrix containing the field values of each record.

Example

// import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "ArcheryChampionship",
       fields: [
           { name: "Name", type: "string" },
           { name: "ScorePerRound", type: "float_v" }
       ]
   }]
});
// set new records in the store
base.store("ArcheryChampionship").push({ Name: "Robin Hood", ScorePerRound: [50, 48, 48] });
base.store("ArcheryChampionship").push({ Name: "Oliver Queen", ScorePerRound: [44, 46, 44] });
base.store("ArcheryChampionship").push({ Name: "Legolas", ScorePerRound: [50, 50, 48] });
// get the matrix containing the "score per round" values
// The values of the i-th column are the values of the i-th record.
// The function will give the matrix:
// 50  44  50
// 48  46  50
// 48  44  48
var matrix = base.store("ArcheryChampionship").getMatrix("ScorePerRound");
base.close();

Parameter

Name Type Optional Description

fieldName

string

 

The field name. Field mustn't be of type string.

Returns

(module:la.Matrix or module:la.SparseMatrix)B The matrix containing the field values.

getStreamAggrNames() → Array of string

Returns an array of the stream aggregates names connected to the store.

Example

// import qm module
var qm = require('qminer');
// create a simple base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Laser",
       fields: [
           { name: "Time", type: "datetime" },
           { name: "WaveLength", type: "float" }
       ]
   }]
});

// create a new time series window buffer stream aggregator for 'Laser' store (with the JSON object)
var wavelength = {
    name: "WaveLengthLaser",
    type: "timeSeriesWinBuf",
    store: "Laser",
    timestamp: "Time",
    value: "WaveLength",
    winsize: 10000
}
var sa = base.store("Laser").addStreamAggr(wavelength);
// get the stream aggregates on store "Laser"
base.store("Laser").getStreamAggrNames();
base.close();
Returns

Array of stringB An array of stream aggregates names.

getVector(fieldName) → module:la.Vector

Gives a vector containing the field value of each record.

Example

// import qm module
var qm = require('qminer');
// create a base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Companies",
       fields: [
           { name: "Name", type: "string", primary: true },
           { name: "Location", type: "string" }
       ]
   }]
});
// add some records to the store
base.store("Companies").push({ Name: "DC Comics", Location: "Burbank, California" });
base.store("Companies").push({ Name: "DC Shoes", Location: "Huntington Beach, California" });
base.store("Companies").push({ Name: "21st Century Fox", Location: "New York City, New York" });
// get the vector of company names
var companyNames = base.store("Companies").getVector("Name"); // returns a vector ["DC Comics", "DC Shoes", "21st Century Fox"]
base.close();

Parameter

Name Type Optional Description

fieldName

string

 

The field name. Field must be of one-dimensional type, e.g. int, float, string...

Returns

module:la.VectorB The vector containing the field values of each record.

inspect(depth) → string

Inspects the stores.

Parameter

Name Type Optional Description

depth

number

 

The depth of inspection. How many times to recurse while formatting the store object.

Returns

stringB String representation of the store.

isDate(fieldName) → boolean

Checks if the field is of type Date.

Example

// import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "BasketballPlayers",
       fields: [
           { name: "Name", type: "string" },
           { name: "SeasonScore", type: "int_v" },
           { name: "DateOfBirth", type: "datetime" }
       ]
   }]
});
// check if the SeasonScore field is of type Date
var isSeasonScoreDate = base.store("BasketballPlayers").isDate("SeasonScore"); // returns false
// check if the FirstPlayed field is of type Date
var isFirstPlayedDate = base.store("BasketballPlayers").isDate("DateOfBirth"); // returns true
base.close();

Parameter

Name Type Optional Description

fieldName

string

 

The field name.

Returns

booleanB True, if the field is of type Date. Otherwise, false.

isNumeric(fieldName) → boolean

Checks if the field is of numeric type.

Example

// import qm module
var qm = require('qminer');
// create a base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "TVSeries",
       fields: [
           { name: "Title", type: "string", primary: true },
           { name: "NumberOfEpisodes", type: "int" }
       ]
   }]
});
// check if the field "Title" is of numeric type
var isTitleNumeric = base.store("TVSeries").isNumeric("Title"); // returns false
// check if the field "NumberOfEpisodes" is of numeric type
var isNumberOfEpisodesNumeric = base.store("TVSeries").isNumeric("NumberOfEpisodes"); // returns true
base.close();

Parameter

Name Type Optional Description

fieldName

string

 

The field name.

Returns

booleanB True, if the field is of numeric type. Otherwise, false.

isString(fieldName) → boolean

Checks if the field is of string type.

Example

// import qm module
var qm = require("qminer");
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "People",
       fields: [
           { name: "Name", type: "string", primary: true },
           { name: "Gender", type: "string" },
           { name: "Age", type: "int" }
       ]
   }]
});
// check if the field "Name" is of string type
var isNameString = base.store("People").isString("Name"); // returns true
// check if the field "Age" is of string type
var isAgeString = base.store("People").isString("Age"); // returns false
base.close();

Parameter

Name Type Optional Description

fieldName

string

 

The field name.

Returns

booleanB True, if the field is of string type. Otherwise, false.

key(keyName) → module:qm~DetailKeyObject

Returns the details of the selected key as a object.

Example

// import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Countries",
       fields: [
           { name: "Name", type: "string", primary: true },
           { name: "Population", type: "int" },
           { name: "Continent", type: "string" }
       ],
       keys: [
           { field: "Name", type: "text" },
           { field: "Continent", type: "value" }
       ]
   }]
});
// get the details of the key of the field "Continent"
// returns a JSON object containing the details of the key:
// { fq: { length: 0 }, vocabulary: { length: 0 }, name: 'Continent', store: { name: 'Countries', ... }}
var details = base.store("Countries").key("Continent");
base.close();

Parameter

Name Type Optional Description

keyName

string

 

The key name.

Returns

module:qm~DetailKeyObjectB The object containing the details of the key.

loadJson(file[, limit]) → number

Load given file line by line, parse each line to JSON and push it to the store.

Parameters

Name Type Optional Description

file

String

 

Name of the JSON line file.

limit

Number

Yes

Maximal number of records to load from file.

Returns

numberB Number of records loaded from file.

map(callback) → Array of Object

Creates an array of function outputs created from the store records.

Example

// import qm module
var qm = require('qminer');
// create a base containing the store Class
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Class",
       fields: [
           { name: "Name", type: "string" },
           { name: "StudyGroup", type: "string" }
       ]
   }]
});
// add some records to the store
base.store("Class").push({ Name: "Shirley", StudyGroup: "A" });
base.store("Class").push({ Name: "Troy", StudyGroup: "B" });
base.store("Class").push({ Name: "Chang", StudyGroup: "C" });
base.store("Class").push({ Name: "Pierce", StudyGroup: "A" });
// make an array of record names
var arr = base.store("Class").map(function (rec) { return rec.Name; }); // returns an array ["Shirley", "Troy", "Chang", "Pierce"]
base.close();

Parameter

Name Type Optional Description

callback

function()

 

Function that generates the array. It takes two parameters:
1. rec - The current record. Type module:qm.Record.
2. idx - The index of the current record (optional). Type number.

Returns

Array of ObjectB The array created by the callback function.

newRecord(obj) → module:qm.Record

Creates a new record of given store. The record is not added to the store.

Example

// import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Planets",
       fields: [
           { name: "Name", type: "string" },
           { name: "Diameter", type: "int" },
           { name: "NearestStars", type: "string_v" }
       ]
   }]
});
// add a new planet in the store
base.store("Planets").push({ Name: "Earth", Diameter: 299196522, NearestStars: ["Sun"] });
// create a record of a planet (not added to the Planets store)
var planet = base.store("Planets").newRecord({ Name: "Tatooine", Diameter: 10465, NearestStars: ["Tatoo 1", "Tatoo 2"] });
base.close();

Parameter

Name Type Optional Description

obj

object

 

An object describing the record.

Returns

module:qm.RecordB The record created by obj and the store.

newRecordSet(idVec) → module:qm.RecordSet

Creates a new record set out of the records in store.

Example

// import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Superheroes",
       fields: [
           { name: "Name", type: "string" },
           { name: "Superpowers", type: "string_v" }
       ]
   }]
});
// add some new records to the store
base.store("Superheroes").push({ Name: "Superman", Superpowers: ["Superhuman strength, speed, hearing", "Flight", "Heat vision"] });
base.store("Superheroes").push({ Name: "Batman", Superpowers: ["Genius-level intellect", "Peak physical and mental conditioning", "Master detective"] });
base.store("Superheroes").push({ Name: "Thor", Superpowers: ["Superhuman strength, endurance and longevity", "Abilities via Mjolnir"] });
base.store("Superheroes").push({ Name: "Wonder Woman", Superpowers: ["Superhuman strength, agility and endurance", "Flight", "Highly skilled hand-to-hand combatant"] });
// create a new record set containing only the DC Comic superheroes (those with the record ids 0, 1 and 3)
var intVec = new qm.la.IntVector([0, 1, 3]);
var DCHeroes = base.store("Superheroes").newRecordSet(intVec);
base.close();

Parameter

Name Type Optional Description

idVec

module:la.IntVector

 

The integer vector containing the IDs of selected records.

Returns

module:qm.RecordSetB The record set that contains the records gained with idVec.

push(rec[, triggerEvents]) → number

Adds a record to the store.

Example

// import qm module
var qm = require('qminer');
// create a new base containing two stores
var base = new qm.Base({
   mode: "createClean",
   schema: [
   {
       name: "Superheroes",
       fields: [
           { name: "Name", type: "string" },
           { name: "Superpowers", type: "string_v" }
       ]
   },
   {
       name: "Supervillians",
       fields: [
           { name: "Name", type: "string" },
           { name: "Superpowers", type: "string_v" }
       ]
   }]
});
// add a new superhero to the Superheroes store
base.store("Superheroes").push({ Name: "Superman", Superpowers: ["flight", "heat vision", "bulletproof"] }); // returns 0
// add a new supervillian to the Supervillians store
base.store("Supervillians").push({ Name: "Lex Luthor", Superpowers: ["expert engineer", "genius-level intellect", "money"] }); // returns 0
base.close();

Parameters

Name Type Optional Description

rec

object

 

The added record. The record must be a object corresponding to store schema created at store creation using module:qm~SchemaDef.

triggerEvents

boolean

Yes

If true, all stream aggregate callbacks onAdd will be called after the record is inserted. If false, no stream aggregate will be updated.

Defaults to true.

Returns

numberB The ID of the added record.

recordByName(recName) → (module:qm.Record or null)

Returns a record from the store.

Example

// import qm module
var qm = require('qminer');
// create a base containing the store Class. Let the Name field be the primary field.
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Class",
       fields: [
           { name: "Name", type: "string", primary: true },
           { name: "StudyGroup", type: "string" }
       ]
   }]
});
// add some records to the store
base.store("Class").push({ Name: "Dean", StudyGroup: "A" });
base.store("Class").push({ Name: "Chang", StudyGroup: "D" });
base.store("Class").push({ Name: "Magnitude", StudyGroup: "C" });
base.store("Class").push({ Name: "Leonard", StudyGroup: "B" });
// get the record with the name "Magnitude"
var record = base.store("Class").recordByName("Magnitude");
base.close();

Parameter

Name Type Optional Description

recName

string

 

Record name.

Returns

(module:qm.Record or null)B Returns the record. If the record doesn't exist, it returns null.

resetStreamAggregates()

Resets all stream aggregates.

Example

// import qm module
var qm = require('qminer');
// create a simple base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "Laser",
       fields: [
           { name: "Time", type: "datetime" },
           { name: "WaveLength", type: "float" }
       ]
   }]
});

// create a new time series window buffer stream aggregator for 'Laser' store (with the JSON object)
var wavelength = {
    name: "WaveLengthLaser",
    type: "timeSeriesWinBuf",
    store: "Laser",
    timestamp: "Time",
    value: "WaveLength",
    winsize: 10000
}
var sa = base.store("Laser").addStreamAggr(wavelength);
// reset the stream aggregates on store "Laser"
base.store("Laser").resetStreamAggregates();
base.close();

sample(sampleSize) → module:qm.RecordSet

Creates a record set containing random records from store.

Example

// import qm module
var qm = require('qminer');
// create a base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "TVSeries",
       fields: [
           { name: "Title", type: "string", primary: true },
           { name: "NumberOfEpisodes", type: "int" }
       ]
   }]
});
// add some records in the store
base.store("TVSeries").push({ Title: "Archer", NumberOfEpisodes: 75 });
base.store("TVSeries").push({ Title: "The Simpsons", NumberOfEpisodes: 574 });
base.store("TVSeries").push({ Title: "New Girl", NumberOfEpisodes: 94 });
base.store("TVSeries").push({ Title: "Rick and Morty", NumberOfEpisodes: 11 });
base.store("TVSeries").push({ Title: "Game of Thrones", NumberOfEpisodes: 47 });
// create a sample record set containing 3 records
var randomRecordSet = base.store("TVSeries").sample(3); // contains 3 random records from the TVSeries store
base.close();

Parameter

Name Type Optional Description

sampleSize

number

 

The size of sample.

Returns

module:qm.RecordSetB Returns a record set containing sampleSize random records.

toJSON() → Object

Returns the store as a JSON.

Example

// import qm module
var qm = require('qminer');
// create a new base containing one store
var base = new qm.Base({
   mode: "createClean",
   schema: [{
       name: "FootballPlayers",
       fields: [
           { name: "Name", type: "string" },
           { name: "FootballClubs", type: "string_v" },
           { name: "GoalsPerSeason", type: "int_v" },
       ]
   }]
});
// get the store as a JSON object
// the returned JSON object is:
// { storeId: 0, storeName: 'FootballPlayers', storeRecords: 0, fields: [...], keys: [], joins: [] }
var json = base.store("FootballPlayers").toJSON();
base.close();
Returns

ObjectB The store as a JSON.

triggerOnAddCallbacks([arg])

Calls onAdd callback on all stream aggregates.

Parameter

Name Type Optional Description

arg

(module:qm.Record or number)

Yes

The record or record ID which will be passed to onAdd callbacks. If the record or record ID is not provided, the last record will be used. Throws exception if the record cannot be provided.
Defaults to the last module:qm.Record in store.