src/DataBase/VwRegister/VwRegister.js
/**
     * Creates a register
     * @param {string} tableIdRef table idRef
     * @param {JSON} data Json with the register data. Keys must be fields IDs.
     * @returns {VwRegister} VWRegister
     */
Creates a register
@param {string} tableIdRef table idRef
@param {JSON} data Json with the register data. Keys must be fields IDs.
Creates a register @param { string } tableIdRef table idRef @param { JSON } data Json with the register data . Keys must be fields IDs .
static createRegister(tableIdRef, data, skipErrors=false) {
        const vregister = new VRegister(theRoot);
        vregister.setTable(tableIdRef);
        const tableInfo = vregister.tableInfo();
        const tableName = tableInfo.name();
        let saved = false;
        let currentKey;
        for (let key in data) {
            if (tableInfo.fieldName(key)) {
                currentKey = key
                const value = data[key];
                if(typeof value == "object") {
                    try {
                        const info = new VwTableInfo(tableInfo);
                        const bountdedTableInfo = info.getBoundedTableInfo(key);
                        if(info.getFieldBoundedType(key) == VTableInfo.BindTypeMasterExt) {
                            VWTransactions.transaction('Create register', () => {
                                vregister.addRegister();
                            });
                            saved = true;
                            value["ID"] = vregister.fieldToString("ID");
                        }
                        const master = VwRegister.createRegister(bountdedTableInfo.idRef(), value);
                        if(info.getFieldBoundedType(key) != VTableInfo.BindTypeMasterExt) {
                            vregister.setField(key, master.ID());
                        }
                    } catch(e) {
                        if(!skipErrors) {
                            throw new Error(e.message);
                        } else {
                            alert(e.message);
                        }
                    }
                } else {
                    try {
                        vregister.setField(key, value);
                    } catch(e) {
                        if(!skipErrors) {
                            if(typeof data[currentKey] == "object") {
                                    throw new Error(`setField error. key: ${currentKey}, value: ${JSON.stringify(data[currentKey])}`);
                            } else {
                                    throw new Error(`setField error. key: ${currentKey}, value: ${data[currentKey]}`);
                            }
                        } else {
                            alert(`setField error. key: ${currentKey}, value: ${JSON.stringify(data[currentKey])}`);
                        }
                    }
                }
            } else {
                if(!skipErrors) {
                    throw new Error(`The field ${key} does not exist in the table ${tableName}`);
                } else {
                    alert(`The field ${key} does not exist in the table ${tableName}`);
                }
            }
        }
        if(!saved) {
            VWTransactions.transaction('Create register', () => {
                vregister.addRegister();
            });
        } else {
            VWTransactions.transaction('Save register', () => {
                vregister.modifyRegister();
            });
        }
        const mapper = new VwMapper(tableInfo);
        return new VwRegister(vregister, mapper);
    }
static createRegister ( tableIdRef , data , skipErrors = false ) { const vregister = new VRegister ( theRoot ) ; vregister . setTable ( tableIdRef ) ; const tableInfo = vregister . tableInfo ( ) ; const tableName = tableInfo . name ( ) ; let saved = false ; let currentKey ; for ( let key in data ) { if ( tableInfo . fieldName ( key ) ) { currentKey = key const value = data [ key ] ; if ( typeof value == "object" ) { try { const info = new VwTableInfo ( tableInfo ) ; const bountdedTableInfo = info . getBoundedTableInfo ( key ) ; if ( info . getFieldBoundedType ( key ) == VTableInfo . BindTypeMasterExt ) { VWTransactions . transaction ( 'Create register' , ( ) => { vregister . addRegister ( ) ; } ) ; saved = true ; value [ "ID" ] = vregister . fieldToString ( "ID" ) ; } const master = VwRegister . createRegister ( bountdedTableInfo . idRef ( ) , value ) ; if ( info . getFieldBoundedType ( key ) != VTableInfo . BindTypeMasterExt ) { vregister . setField ( key , master . ID ( ) ) ; } } catch ( e ) { if ( ! skipErrors ) { throw new Error ( e . message ) ; } else { alert ( e . message ) ; } } } else { try { vregister . setField ( key , value ) ; } catch ( e ) { if ( ! skipErrors ) { if ( typeof data [ currentKey ] == "object" ) { throw new Error ( ` ${ currentKey } ${ JSON . stringify ( data [ currentKey ] ) } ` ) ; } else { throw new Error ( ` ${ currentKey } ${ data [ currentKey ] } ` ) ; } } else { alert ( ` ${ currentKey } ${ JSON . stringify ( data [ currentKey ] ) } ` ) ; } } } } else { if ( ! skipErrors ) { throw new Error ( ` ${ key } ${ tableName } ` ) ; } else { alert ( ` ${ key } ${ tableName } ` ) ; } } } if ( ! saved ) { VWTransactions . transaction ( 'Create register' , ( ) => { vregister . addRegister ( ) ; } ) ; } else { VWTransactions . transaction ( 'Save register' , ( ) => { vregister . modifyRegister ( ) ; } ) ; } const mapper = new VwMapper ( tableInfo ) ; return new VwRegister ( vregister , mapper ) ; }
Creates a register
 @param {string} tableIdRef table idRef
 @param {JSON} data Json with the register data.
Creates a register @param { string } tableIdRef table idRef @param { JSON } data Json with the register data .