Class: Database

Database()

new Database()

Represents a Database instance
Do not call constructor directly. To create an database instance
Source:
See:

Methods

bulk(data) → {Object}

Put multiple objects at once
Parameters:
Name Type Description
data Array the array objects
Source:
Returns:
- an object containing all total counts
Type
Object
Example
var result = db.bulk ({
	  	Employees: [{
	  		name: 'John', age: 34	
	  	}, {
	  		name: 'Smith', age: 26	
	  	}],
	  	Pets: [{
	  		name: 'Rocky'
	  	}, {
	  		name: 'Rado'
	  	}]
	  });
	  
	  api.logger.info (result.totalCount);
	  api.logger.info (result.Employees.count);
	  
	  

commit()

Commit transaction
Source:

count(entity) → {integer}

Count the number of records in this entity
Parameters:
Name Type Description
entity string the entity/table name
Source:
Returns:
- number of records found
Type
integer

createIndex()

Create an index on an entity. Supported index types
  • Unique
  • NotUnique
  • Text
  • Spatial
Parameters:
Type Description
string the entity name
type the index type
name the index name. Should be unique by entity
varray properties names
Source:

delete(entity, id) → {integer}

Delete a database object
Parameters:
Name Type Description
entity string the entity/table name
id Object the database object id
Source:
Returns:
- positive value means that the object is found and it was deleted
Type
integer

deleteByQuery(entity, query, bindingsopt) → {integer}

Delete records based on a query
Parameters:
Name Type Attributes Description
entity string the entity/table name
query JsonObject the query spec
bindings JsonObject <optional>
the query parameters
Source:
Returns:
- number of records deleted
Type
integer
Example
db.deleteByQuery ({
	  	entity: 'YourEntityName',
	  	where: {
	  		prop1: '123',
	  		prop2: { op: 'gt', value: 35 }
	  	}
	  });
	  
	  

drop(entity)

Drop or Remove an entity and its associated data
Use with precaution!
Parameters:
Name Type Description
entity string the entity name
Source:

dropIndex(entity, name)

Drop or delete an index by name
Parameters:
Name Type Description
entity string the entity name
name name the index name. Should be unique by entity
Source:

find(entity, query, visitor, bindingsopt)

Find records based on a query
Parameters:
Name Type Attributes Description
entity string the entity/table name
query JsonObject the query spec
visitor JsonObject the callback function to execute for each record found.
bindings JsonObject <optional>
the query parameters
Source:
Example
db.find (entity: 'YourEntityName', {
	  	where: {
	  		prop1: '123',
	  		prop2: { op: 'gt', value: 35 }
	  	}
	  }, function (dbo) {
	  	// do something useful with the DatabaseObject
	  });
	  
	  

findOne(entity, query, bindingsopt) → {DatabaseObject}

Find only 1 object matching your query
Parameters:
Name Type Attributes Description
entity string the entity/table name
query JsonObject the query spec
bindings JsonObject <optional>
the query parameters
Source:
Returns:
- the database object
Type
DatabaseObject
Example
var dbo = db.findOne ('YourEntityName', {
	  	where: {
	  		prop1: '123',
	  		prop2: { op: 'gt', value: 35 }
	  	}
	  });
	  
	  

get(entity, id) → {DatabaseObject}

Get a database object by id
Parameters:
Name Type Description
entity string the entity/table name
id Object the database object id
Source:
Returns:
- the database object
Type
DatabaseObject

pop(entity, query, visitor, bindingsopt)

Pop records based on a query. It's a queue system, so records you pop are deleted
Parameters:
Name Type Attributes Description
entity string the entity/table name
query JsonObject the query spec
visitor JsonObject the callback function to execute for each record found.
bindings JsonObject <optional>
the query parameters
Source:
Example
db.pop ('YourEntityName', {
	  	where: {
	  		prop1: '123',
	  		prop2: { op: 'gt', value: 35 }
	  	}
	  }, function (dbo) {
	  	// do something useful with the DatabaseObject
	  });
	  
	  

popOne(entity, query, bindingsopt) → {DatabaseObject}

Pop only 1 object matching your query
Parameters:
Name Type Attributes Description
entity string the entity/table name
query JsonObject the query spec
bindings JsonObject <optional>
the query parameters
Source:
Returns:
- the database object
Type
DatabaseObject
Example
var dbo = db.popOne ('YourEntityName', {
	  	where: {
	  		prop1: '123',
	  		prop2: { op: 'gt', value: 35 }
	  	}
	  });
	  
	  

put(dbo) → {DatabaseObject}

Create or update a database object
If the database object id is present and it's found in the database, an update will be performed
Parameters:
Name Type Description
dbo DatabaseObject the database object
Source:
Returns:
- the database object
Type
DatabaseObject

rollback()

Rollback transaction
Source:

schedule(event, cronExpression, entity, query, bindingsopt)

Schedule a event/task to run based on an expression
Parameters:
Name Type Attributes Description
event string the event name
cronExpression string the cron expression (read more at https://en.wikipedia.org/wiki/Cron#CRON_expression)
entity string the entity/table name
query JsonObject the query to run
bindings JsonObject <optional>
the query parameters
Source:
Example
db.schedule ('DeleteInvalidOrders', 'Orders', {
	  	construct: 'delete',
	  	where: {
	  		status: 'pending',
	  		when: { op: 'lte', value: 'sysdate() - 3153600' }
	  	}
	  });
	  
	  

trx()

Start a database transaction
Source:

unschedule(event, bindingsopt)

Unschedule an event
Parameters:
Name Type Attributes Description
event string the event name
bindings JsonObject <optional>
the query parameters
Source:
Example
db.unschedule ('DeleteInvalidOrders');