Skip to content

infinity.noSql

Provides non-relational in-memory database functionality. Collections store JSON documents and are shared by all threads of one infinity instance. The data is not persisted and is lost when the program ends.

Every document has a numeric primary key in the property id. All functions lock the collection implicitly while they access it; use lock() and unlock() to combine several operations into one atomic operation. Functions throw an error if the specified collection does not exist (except exists() and count()).

Module: infinity.noSql

Example:

infinity.loadModule('infinity.noSql');

if (!infinity.noSql.exists('testCollection')) {
  infinity.noSql.create('testCollection');
  infinity.noSql.index('testCollection', 'username', true);
  infinity.noSql.index('testCollection', 'modified');
}

infinity.noSql.insert('testCollection', { "username": "TheUserName1", "email": "theusername@email.com", "rights": ["read", "write", "create", "delete"] });
infinity.noSql.insert('testCollection', { "username": "AnotherUserName2", "email": "someother@email.net", "rights": ["read"] });
infinity.noSql.insert('testCollection', { username: "aUserName3", email: "yetanother@email.com" });

let ids = infinity.noSql.find('testCollection', [['username', 'beginswith', 'a']], 'username', infinity.noSql.orderDirection.ascending, 0, 0, infinity.noSql.outputMode.ids);
console.debug(ids);
//[2,3]

infinity.noSql.update('testCollection', { id: 2, username: "aUserName", email: "yetanother@email.com", modified: Math.floor(Date.now() * 0.001) });

let secondRow = infinity.noSql.get('testCollection', 2);
console.debug(secondRow);
let firstTwoRows = infinity.noSql.get('testCollection', [1, 2]);
console.debug(firstTwoRows);

console.debug(infinity.noSql.has('testCollection', 2));
//true


Interfaces

findResultArray

Extends: Array<object|number>

The result of find(): an array of documents (output mode objects), of document IDs (output mode ids) or an array with the number of found documents as only element (output mode count).


getResultArray

Extends: Array<object>

An array of documents.


idArray

Extends: Array<number>

An array of document IDs.


queryArray

Extends: Array<string|Array<string|number|boolean>>

A query for find(), update() and remove().

A query is an array of conditions, separated by combine operators: [condition, operator, condition, ...]. An empty array matches all documents.

A condition is an array [key, compareOperator, value], or a nested query array (to group conditions like parentheses).

  • key: The name of the document property. Documents without this property, or whose value is not a string, number or boolean (e.g. an array), never match.
  • compareOperator: = (or ==), <> (or !=), >, <, >=, <=, beginswith, endswith or contains. For the key id only =, <>, >, <, >= and <= are allowed.
  • value: A non-empty string, a number or a boolean. Strings are compared case-insensitively.

Combine operators: and (or &, &&), or (or |, ||) and not (or !, !!; the following condition must not match). Operators are evaluated from left to right.

Example:

let query = [['username', 'beginswith', 'a'], 'and', [['email', 'endswith', '.com'], 'or', ['id', '<', 10]]];

Functions

clear()

Clears all data from the specified collection.

Signature:

clear( collection: string ): void

Parameters:

  • collection: string

    The collection to clear data from.

Example:

infinity.loadModule('infinity.noSql');
infinity.noSql.clear('testCollection');

count()

Returns the number of documents in the specified collection, or 0 if the collection does not exist.

Signature:

count( collection: string ): number

Parameters:

  • collection: string

    The collection to examine.

Return type: number

Example:

infinity.loadModule('infinity.noSql');
let rowsInCollection = infinity.noSql.count('testCollection');

create()

Creates a collection with the specified name in memory. Throws an error if the collection already exists.

Signature:

create( collection: string, capped?: boolean, maxCount?: number ): void

Parameters:

  • collection: string

    The name of the collection to create.

  • capped: boolean, optional

    Whether the number of documents in the collection should be limited. If the collection contains maxCount documents, insert() removes the oldest document (the one with the lowest ID) before adding the new one. Default is false.

  • maxCount: number, optional

    The maximum number of documents of a capped collection. 0 (default) means no limit.

Example:

infinity.loadModule('infinity.noSql');
infinity.noSql.create('testCollection');

drop()

Deletes the specified collection including all documents and indexes from memory.

Signature:

drop( collection: string ): void

Parameters:

  • collection: string

    The name of the collection to delete.

Example:

infinity.loadModule('infinity.noSql');
infinity.noSql.drop('testCollection');

exists()

Checks whether a collection with the specified name exists in memory.

Signature:

exists( collection: string ): boolean

Parameters:

  • collection: string

    The collection name to check for.

Return type: boolean

Example:

infinity.loadModule('infinity.noSql');
if ( infinity.noSql.exists('testCollection') ) {
  //...
}

find()

Returns the documents of the specified collection matching the given query. Without sorting, the documents are returned in ascending order of their IDs.

Signature:

find( collection: string, query: infinity.noSql.queryArray, orderBy?: string, orderDirection?: infinity.noSql.orderDirection, offset?: number, limit?: number, outputMode?: infinity.noSql.outputMode ): infinity.noSql.findResultArray

Parameters:

  • collection: string

    The name of the collection to be queried.

  • query: infinity.noSql.queryArray

    The query (see queryArray). An empty array returns all documents.

  • orderBy: string, optional

    The name of the property to sort the result by. Strings are sorted case-insensitively; documents without this property are sorted as empty values. Only used if orderDirection is not none.

  • orderDirection: infinity.noSql.orderDirection, optional

    Specifies the order direction. Default is infinity.noSql.orderDirection.none.

  • offset: number, optional

    The number of documents to skip. Default is 0. An offset beyond the end of the result returns an empty array.

  • limit: number, optional

    The maximum number of documents to return. 0 or omitted means no limit.

  • outputMode: infinity.noSql.outputMode, optional

    Specifies how the data is returned. Default is infinity.noSql.outputMode.objects.

Return type: infinity.noSql.findResultArray

Example:

infinity.loadModule('infinity.noSql');
let ids = infinity.noSql.find('testCollection', [['username', 'beginswith', 'a']], '', infinity.noSql.orderDirection.ascending, 0, 0, infinity.noSql.outputMode.ids);

get()

Returns a copy of the document with the given ID out of the specified collection, or undefined if there is no such document.

Signature:

get( collection: string, id: number ): object

Parameters:

  • collection: string

    The name of the collection to be queried.

  • id: number

    The ID of the row to be returned.

Return type: object

Example:

infinity.loadModule('infinity.noSql');
let secondRow = infinity.noSql.get('testCollection', 2);

get()

Returns copies of the documents with the given IDs out of the specified collection as an array of objects. IDs without a matching document are skipped.

Signature:

get( collection: string, ids: infinity.noSql.idArray ): infinity.noSql.getResultArray

Parameters:

  • collection: string

    The name of the collection to be queried.

  • ids: infinity.noSql.idArray

    An array containing the IDs of the documents to be returned.

Return type: infinity.noSql.getResultArray

Example:

infinity.loadModule('infinity.noSql');
let someRows = infinity.noSql.get('testCollection', [2, 3]);

has()

Indicates whether the specified collection has a document with the given ID.

Signature:

has( collection: string, id: number ): boolean

Parameters:

  • collection: string

    The name of the collection to be checked.

  • id: number

    The row to be checked.

Return type: boolean

Example:

infinity.loadModule('infinity.noSql');
if ( infinity.noSql.has('testCollection', 2) ) {
  //...
}

index()

Creates an index for the given property in the specified collection, including all existing documents. Queries with the compare operators =, <>, >, <, >=, <= and beginswith on an indexed property use the index instead of scanning all documents. A unique index prevents documents with the same value (compared case-insensitively for strings) for this property: insert() and update() throw an error in this case.

Throws an error if an index for the property already exists, if key is id (the primary key is always indexed) or if a unique index is created and existing documents contain duplicate values (the index is not created then).

Signature:

index( collection: string, key: string, unique?: boolean ): void

Parameters:

  • collection: string

    The name of the collection to be altered.

  • key: string

    The name of the property to index.

  • unique: boolean, optional

    Specifies whether the index has to be unique. Default is false.

Example:

infinity.loadModule('infinity.noSql');
infinity.noSql.index('testCollection', 'username', true);

insert()

Inserts the given document into the specified collection and returns its ID. If the document has no numeric property id (or id is 0), the next value of the collection's auto increment counter is assigned. Otherwise the given ID is used; an error is thrown if a document with this ID already exists. The auto increment counter does not consider explicitly given IDs, so mixing both can lead to duplicate key errors. Property values can be of any JSON type including null. If an error is thrown (duplicate ID, unique index violation), the collection stays unchanged.

Signature:

insert( collection: string, document: object ): number

Parameters:

  • collection: string

    The name of the collection to be altered.

  • document: object

    An object containing the data to be inserted.

Return type: number

Example:

infinity.loadModule('infinity.noSql');
infinity.noSql.insert('testCollection', { "username": "TheUserName1", "email": "theusername@email.com", "rights": ["read", "write", "create", "delete"] });

lock()

Locks the specified collection for the current thread, waiting until the lock can be acquired. A write lock (default) is exclusive, read locks (readOnly = true) can be held by several threads at the same time. Locks are per thread: all functions of this module called by the thread holding the lock use it instead of locking again. Every successful lock must be released with unlock().

Returns true if the lock was acquired and false if the current thread already holds a lock on the collection. Throws an error if a write lock is requested while the thread holds a read lock.

Signature:

lock( collection: string, readOnly?: boolean ): boolean

Parameters:

  • collection: string

    The name of the collection to be locked.

  • readOnly: boolean, optional

    Specifies whether only reading access is required.

Return type: boolean

Example:

infinity.loadModule('infinity.noSql');
if ( infinity.noSql.lock('testCollection') ) {
  try {
    //...
  } finally {
    infinity.noSql.unlock('testCollection');
  }
}

remove()

Removes all documents matching the given query from the specified collection. Returns the number of removed documents.

Signature:

remove( collection: string, query: infinity.noSql.queryArray ): number

Parameters:

  • collection: string

    The name of the collection to be altered.

  • query: infinity.noSql.queryArray

    The query selecting the documents to remove. An empty array removes all documents.

Return type: number

Example:

infinity.loadModule('infinity.noSql');
let rowsAffected = infinity.noSql.remove('testCollection', [['id', '=',  2]]);

remove()

Removes the document with the given ID from the specified collection. Returns the number of removed documents (0 or 1).

Signature:

remove( collection: string, id: number ): number

Parameters:

  • collection: string

    The name of the collection to be altered.

  • id: number

    The unique identifier of the row to be removed.

Return type: number

Example:

infinity.loadModule('infinity.noSql');
let rowsAffected = infinity.noSql.remove('testCollection', 2);
console.log(rowsAffected + ' row(s) removed.');

unlock()

Releases the lock the current thread acquired with lock(). Has no effect if the current thread holds no lock on the collection.

Signature:

unlock( collection: string ): void

Parameters:

  • collection: string

    The name of the collection to be unlocked.

Example:

infinity.loadModule('infinity.noSql');
infinity.noSql.unlock('testCollection');

update()

Updates all documents matching the given query in the specified collection with the property values of the given object. Only properties that already exist in a document are changed, new properties are not added; the property id is ignored. Returns the number of updated documents. The update is applied to all matching documents or to none: if a unique index would be violated, an error is thrown and no document is changed.

Signature:

update( collection: string, query: infinity.noSql.queryArray, update: object ): number

Parameters:

  • collection: string

    The name of the collection to be updated.

  • query: infinity.noSql.queryArray

    The query selecting the documents to update. An empty array updates all documents.

  • update: object

    An object containing the property values to be written.

Return type: number

Example:

infinity.loadModule('infinity.noSql');
let rowsAffected = infinity.noSql.update('testCollection', [['username', '=', 'TheUserName1']], {"email": "somerandom@email.com"});

update()

Updates the document with the given ID in the specified collection with the property values of the given object. Only properties that already exist in the document are changed, new properties are not added; the property id is ignored. Returns the number of updated documents (0 or 1). If a unique index would be violated, an error is thrown and the document stays unchanged.

Signature:

update( collection: string, id: number, update: object ): number

Parameters:

  • collection: string

    The name of the collection to be updated.

  • id: number

    The ID of the document to be updated.

  • update: object

    An object containing the property values to be written.

Return type: number

Example:

infinity.loadModule('infinity.noSql');
let updateData = { username: "newUserName", email: "new@email.com", modified: Math.floor(Date.now() * 0.001) };
let rowsAffected = infinity.noSql.update('testCollection', 2, updateData);
console.log(rowsAffected + ' row(s) updated.');

update()

Replaces a document in the specified collection by the given document. The document to replace is identified by the numeric property id of the given document. Returns the number of replaced documents (0 if the document has no numeric id or no document with this ID exists, otherwise 1). If a unique index would be violated, an error is thrown and the stored document stays unchanged.

Signature:

update( collection: string, document: object ): number

Parameters:

  • collection: string

    The name of the collection to be updated.

  • document: object

    The new document, including the property id.

Return type: number

Example:

infinity.loadModule('infinity.noSql');
let rowsAffected = infinity.noSql.update('testCollection', { id: 2, username: "aUserName", email: "yetanother@email.com", modified: Math.floor(Date.now() * 0.001) });

Enums

infinity.noSql.orderDirection

Specifies the order direction of the returned result set.

Values:

  • none: 0

    Not sorted (ascending order of the document IDs).

  • ascending: 1

    Ascending order.

  • descending: 2

    Descending order.

Example:

infinity.loadModule('infinity.noSql');
let orderDirection = infinity.noSql.orderDirection.ascending;

infinity.noSql.outputMode

Specifies how the result data has to be returned.

Values:

  • objects: 0

    As objects.

  • ids: 1

    As an array of document IDs.

  • count: 2

    As an array with the number of found documents as only element ([0] if nothing is found, also for an empty collection).

Example:

infinity.loadModule('infinity.noSql');
let outputMode = infinity.noSql.outputMode.objects;