Reference Source
public class | source

DataCollection

Extends:

Loggable → DataCollection

DataCollection class.

Example:

	API:
		->constructor(arg_cache_manager,arg_data_adapter,arg_model_schema)
		
		->get_name():string - get tenant to use inside this datastore.
		->get_cache_manager():CacheManager - get cache manager instance.
		->get_adapter():DataAdapter - get data adapter instance.
		->get_model():DataModel - get collection model instance.

	->validate_record(arg_record):Promise(boolean) - test if given datas are valid for collection model.
	->new_record(arg_record_datas, arg_record_id) - create a new record instance.
		->create_record(arg_record):Promise(DataRecord) - create an existing unsaved data record.
		->delete_record(arg_record):Promise(boolean) - delete an existing data record.
		->update_record(arg_record):Promise(DataRecord) - update an existing data record.
		->reload_record(arg_record):Promise(DataRecord) - reload an existing data record.
		->has_record(arg_record_id):Promise(boolean) - test if a data record exists with an id.
		
		->find_one_record(arg_record_id):Promise(DataRecord) - find an existing data record with an id.
		->find_records(arg_query):Promise(DataRecordArray) - find existing data records with a query.
		->find_all_records():Promise(DataRecordArray) - find all xisting data records.

	PRIVATE:
		->_emit(arg_event, arg_datas=undefined):nothing
		->_trigger(arg_event, arg_datas):nothing
		->_has_cached_record_by_id(arg_id):Promise(boolean)
		->_get_cached_record_by_id(arg_id):Promise(DataRecord)
		->_set_cached_record_by_id(arg_record):Promise(boolean)
		->_remove_cached_record_by_id(arg_id):Promise(boolean)


	USAGE ON BROWSER:
		// ds is a DataStore instance
		var cars = ds.get_collection('cars')
		var car_12 = cars.find_one_record('12')

Constructor Summary

Public Constructor
public

constructor(arg_cache_manager: CacheManager, arg_data_adapter: DataAdapter, arg_model_schema: array): nothing

DataCollection class is responsible to manage one model records from one adapter: all records operations, cached records, model logic (field value validation, triggers).

Member Summary

Public Members
public

Class type flag.

Private Members
private

Datas adapter instance.

private

CacheManager instance.

private

Datas collection prefix.

private

Datas collection name.

private

_schema: array

Topology model schema instance.

Method Summary

Public Methods
public

Create a data collection record.

public

Delete a data collection record.

public

Find all existing data records from adapter.

public

find_one_record(arg_record_id: string): Promise

Find an existing data record with an id.

public

Find existing data records with a query.

public

Get data adapter.

public

Get cache manager.

public

Get data model.

public

Get collection name.

public

Get data model.

public

has_record(arg_record_id: string): Promise

Update a data collection record.

public

new_record(arg_record_datas: object, arg_record_id: string): Promise

Create a new data record instance, not saved.

public

Update a data collection record.

public

Validate data record values.

Private Methods
private

_emit(arg_event: string, arg_datas: any): nothing

Emit on event.

private

Get a cached record.

private

Get a cached record array.

private

Test if a record is cached.

private

Test if a record array is cached.

private

Remove a cached record.

private

Add a record to cache.

private

_set_cached_record_by_query(arg_query: DataQuery, arg_record_array: DataRecordArray): Promise

Add a record array to cache.

private

_trigger(arg_event: string, arg_datas: any): nothing

Call event triggers.

Inherited Summary

From class Loggable
public

Log context.

public

Class type flag.

public

Trace is enabled for this flag.

private

Logger manager instance (default undefined)

public

debug(args: string | array): nothing

Trace DEBUG formatted message.

public

disable_trace(): nothing

Disable traces.

public

enable_trace(): nothing

Enable traces.

public

enter_group(arg_group: string): nothing

Trace INFO message on "enter trace group".

public

error(args: string | array): nothing

Trace ERROR formatted message.

public

Define get class name method for non Instance classes.

public

Get instance context.

public

Get logger manager.

public

Define get instance name method for non Instance classes.

public

Get trace flag.

public

info(args: string | array): nothing

Trace INFO formatted message.

public

leave_group(arg_group: string): nothing

Trace INFO message on "leave trace group".

public

separate_level_1(): nothing

Trace INFO trace level 1 separator.

public

separate_level_2(): nothing

Trace INFO trace level 2 separator.

public

separate_level_3(): nothing

Trace INFO trace level 3 separator.

public

set_trace(arg_value: boolean): nothing

Set trace flag.

public

should_trace(arg_traces_cfg: object): boolean

Calculate should trace flag.

public

should_trace_class(arg_traces_cfg: object): boolean

Calculate should trace flag for classes.

public

should_trace_collection_item(arg_traces_cfg: object, arg_collection_name: string, arg_this_item_accessor: string): boolean

Calculate should trace flag for given collection of names or patterns.

public

should_trace_module(arg_traces_cfg: object): boolean

Calculate should trace flag for modules.

public

should_trace_name(arg_traces_cfg: object): boolean

Calculate should trace flag for instances names.

public

Toggle trace flag.

public

Update trace enabled flag.

public

warn(args: string | array): nothing

Trace WARN formatted message.

Public Constructors

public constructor(arg_cache_manager: CacheManager, arg_data_adapter: DataAdapter, arg_model_schema: array): nothing source

DataCollection class is responsible to manage one model records from one adapter: all records operations, cached records, model logic (field value validation, triggers). DataCollection instances are managed by a DataStore instance.

Override:

Loggable#constructor

Params:

NameTypeAttributeDescription
arg_cache_manager CacheManager

cache manager instance.

arg_data_adapter DataAdapter

collection data adapter.

arg_model_schema array

topology model schema.

Return:

nothing

Public Members

public is_data_collection: boolean source

Class type flag.

Private Members

private _adapter: DataAdapter source

Datas adapter instance.

private _cache_manager: CacheManager source

CacheManager instance.

private _cache_prefix: string source

Datas collection prefix.

private _name: string source

Datas collection name.

private _schema: array source

Topology model schema instance.

Public Methods

public create_record(arg_record: DataRecord): Promise source

Create a data collection record. 1-Call 'before_create' triggers. 2-Check existing record id within cached records. 3-Validate record datas: reject on failure 4-Create a DataRecord instance with adapter.new_record. 5-Add record to cache. 6-Call record.save() 7-Call 'after_create' triggers.

Params:

NameTypeAttributeDescription
arg_record DataRecord

data record instance.

Return:

Promise

Promise of a DataRecord object.

public delete_record(arg_record: DataRecord): Promise source

Delete a data collection record. 1-Call 'before_delete' triggers. 2-Remove cached record. 3-Call record.delete() 4-Call 'after_delete' triggers.

Params:

NameTypeAttributeDescription
arg_record DataRecord

data record instance.

Return:

Promise

Promise of boolean success.

public find_all_records(): Promise source

Find all existing data records from adapter.

Return:

Promise

Promise of DataRecordArray.

public find_one_record(arg_record_id: string): Promise source

Find an existing data record with an id. 1-Search record into cache 2-Search record into adapter 3-Save record into cache

Params:

NameTypeAttributeDescription
arg_record_id string

data record id.

Return:

Promise

Promise of DataRecord.

public find_records(arg_query: DataQuery): Promise source

Find existing data records with a query. 1-Search records into cache 2-Search records into adapter 3-Save records into cache

Params:

NameTypeAttributeDescription
arg_query DataQuery

data query.

Return:

Promise

Promise of DataRecordArray.

public get_adapter(): DataAdapter source

Get data adapter.

Return:

DataAdapter

public get_cache_manager(): DataAdapter source

Get cache manager.

Return:

DataAdapter

public get_model(): DataModel source

Get data model.

Return:

DataModel

public get_name(): string source

Get collection name.

Override:

Loggable#get_name

Return:

string

public get_schema(): DataModel source

Get data model.

Return:

DataModel

public has_record(arg_record_id: string): Promise source

Update a data collection record. 1-Search record into cache 2-Search record into adapter

Params:

NameTypeAttributeDescription
arg_record_id string

data record id.

Return:

Promise

Promise of boolean found:true, not found:false.

public new_record(arg_record_datas: object, arg_record_id: string): Promise source

Create a new data record instance, not saved.

Params:

NameTypeAttributeDescription
arg_record_datas object

new record attributes.

arg_record_id string

new record unique id (optional).

Return:

Promise

Promise(DataRecord)

public update_record(arg_record: DataRecord): Promise source

Update a data collection record. 1-Call 'before_update' triggers. 2-Update cached record. 3-Call record.update() 4-Call 'after_update' triggers.

Params:

NameTypeAttributeDescription
arg_record DataRecord

data record instance.

Return:

Promise

Promise of boolean success.

public validate_record(arg_record: DataRecord): Promise source

Validate data record values. 1-Call 'before_validate' triggers. 2-Check all fields values. 3-Call 'after_validate_ok' triggers on success. Call 'after_validate_ko' triggers on failure. 4-Return success (true) or failure (false)

Params:

NameTypeAttributeDescription
arg_record DataRecord

data record instance.

Return:

Promise

Promise of a boolean.

Private Methods

private _emit(arg_event: string, arg_datas: any): nothing source

Emit on event.

Params:

NameTypeAttributeDescription
arg_event string

event name.

arg_datas any

event datas (optional, default:undefined).

Return:

nothing

private _get_cached_record_by_id(arg_id: string): Promise source

Get a cached record.

Params:

NameTypeAttributeDescription
arg_id string

record id.

Return:

Promise

Promise of a DataRecord instance.

private _get_cached_record_by_query(arg_query: DataQuery): Promise source

Get a cached record array.

Params:

NameTypeAttributeDescription
arg_query DataQuery

data query.

Return:

Promise

Promise of a DataRecordArray instance.

private _has_cached_record_by_id(arg_id: string): Promise source

Test if a record is cached.

Params:

NameTypeAttributeDescription
arg_id string

record id.

Return:

Promise

Promise of boolean value: found (true) or not found (false).

private _has_cached_record_by_query(arg_query: DataQuery): Promise source

Test if a record array is cached.

Params:

NameTypeAttributeDescription
arg_query DataQuery

data query.

Return:

Promise

Promise of boolean value: found (true) or not found (false).

private _remove_cached_record_by_id(arg_id: string): Promise source

Remove a cached record.

Params:

NameTypeAttributeDescription
arg_id string

record id.

Return:

Promise

Promise of boolean value: success (true) or failure (false).

private _set_cached_record_by_id(arg_record: DataRecord): Promise source

Add a record to cache.

Params:

NameTypeAttributeDescription
arg_record DataRecord

record

Return:

Promise

Promise of boolean value: success (true) or failure (false).

private _set_cached_record_by_query(arg_query: DataQuery, arg_record_array: DataRecordArray): Promise source

Add a record array to cache.

Params:

NameTypeAttributeDescription
arg_query DataQuery

data query.

arg_record_array DataRecordArray

record array.

Return:

Promise

Promise of boolean value: success (true) or failure (false).

private _trigger(arg_event: string, arg_datas: any): nothing source

Call event triggers.

Params:

NameTypeAttributeDescription
arg_event string

event name.

arg_datas any

event datas (optional, default:undefined).

Return:

nothing