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
findResultArray¶
Extends: Array<object|number>
The result of find(): an array of documents (output mode
objects), of document IDs (output modeids) or an array with the number of found documents as only element (output modecount).
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 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,endswithorcontains. For the keyidonly=,<>,>,<,>=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]]];
clear()¶
Clears all data from the specified collection.
Signature:
clear( collection: string ): void
Parameters:
- collection:
stringThe 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:
stringThe 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:
stringThe name of the collection to create.
-
capped:
boolean, optionalWhether the number of documents in the collection should be limited. If the collection contains
maxCountdocuments, insert() removes the oldest document (the one with the lowest ID) before adding the new one. Default isfalse. -
maxCount:
number, optionalThe 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:
stringThe 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:
stringThe 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:
stringThe name of the collection to be queried.
-
query:
infinity.noSql.queryArrayThe query (see queryArray). An empty array returns all documents.
-
orderBy:
string, optionalThe 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
orderDirectionis notnone. -
orderDirection:
infinity.noSql.orderDirection, optionalSpecifies the order direction. Default is
infinity.noSql.orderDirection.none. -
offset:
number, optionalThe number of documents to skip. Default is
0. An offset beyond the end of the result returns an empty array. -
limit:
number, optionalThe maximum number of documents to return.
0or omitted means no limit. -
outputMode:
infinity.noSql.outputMode, optionalSpecifies 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:
stringThe name of the collection to be queried.
-
id:
numberThe 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:
stringThe name of the collection to be queried.
-
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:
stringThe name of the collection to be checked.
-
id:
numberThe 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:
stringThe name of the collection to be altered.
-
key:
stringThe name of the property to index.
-
unique:
boolean, optionalSpecifies 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:
stringThe name of the collection to be altered.
-
document:
objectAn 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:
stringThe name of the collection to be locked.
-
readOnly:
boolean, optionalSpecifies 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:
stringThe name of the collection to be altered.
-
query:
infinity.noSql.queryArrayThe 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:
stringThe name of the collection to be altered.
- id:
numberThe 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:
stringThe 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:
stringThe name of the collection to be updated.
-
query:
infinity.noSql.queryArrayThe query selecting the documents to update. An empty array updates all documents.
-
update:
objectAn 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:
stringThe name of the collection to be updated.
-
id:
numberThe ID of the document to be updated.
-
update:
objectAn 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:
stringThe name of the collection to be updated.
-
document:
objectThe 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) });
infinity.noSql.orderDirection¶
Specifies the order direction of the returned result set.
Values:
-
none:
0Not sorted (ascending order of the document IDs).
-
ascending:
1Ascending order.
-
descending:
2Descending 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:
0As objects.
-
ids:
1As an array of document IDs.
-
count:
2As 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;