<commit_before>/**
 * Deals with browser storage
 */

'use strict';

var db = require( 'db.js' );
var utils = require( './utils' );
var t = require( './translator' );

var server;
var blobEncoding;
var propertyStore;
var recordStore;
var surveyStore;
var dump;
var available = false;
var databaseName = 'enketo';

function init() {
    return db.open( {
            server: databaseName,
            version: 1,
            schema: {
                // the surveys
                surveys: {
                    key: {
                        keyPath: 'enketoId',
                        autoIncrement: false
                    },
                    indexes: {
                        enketoId: {
                            unique: true
                        }
                    }
                },
                // the resources that belong to a survey
                resources: {
                    key: {
                        autoIncrement: false
                    },
                    indexes: {
                        key: {
                            unique: true
                        }
                    }
                },
                // Records in separate table because it makes more sense for getting, updating and removing records
                // if they are not stored in one (giant) array or object value.
                // Need to watch out for bad iOS bug: http://www.raymondcamden.com/2014/9/25/IndexedDB-on-iOS-8--Broken-Bad
                // but with the current keys there is no risk of using the same key in multiple tables.
                // InstanceId is the key because instanceName may change when editing a draft.
                records: {
                    key: {
                        keyPath: 'instanceId',
                    },
                    indexes: {
                        // useful to check if name exists
                        name: {
                            unique: true
                        },
                        // the actual key
                        instanceId: {
                            unique: true
                        },
                        // to get all records belonging to a form
                        enketoId: {
                            unique: false
                        }
                    }
                },
                // the files that belong to a record
                files: {
                    key: {
                        autoIncrement: false
                    },
                    indexes: {
                        key: {
                            unique: true
                        }
                    }
                },
                // settings or other global app properties
                properties: {
                    key: {
                        keyPath: 'name',
                        autoIncrement: false
                    },
                    indexes: {
                        key: {
                            unique: true
                        }
                    }
                }
            }
        } )
        .then( function( s ) {
            server = s;
            console.debug( 'WHoohoeeee, we\'ve got ourselves a database! Now let\'s check if it works properly.' );
        } )
        .then( _isWriteable )
        .then( _setBlobStorageEncoding )
        .then( function() {
            available = true;
        } )
        .catch( function( e ) {<commit_msg>changed: log indexedDb initialization errors to console<commit_after>/**
 * Deals with browser storage
 */

'use strict';

var db = require( 'db.js' );
var utils = require( './utils' );
var t = require( './translator' );

var server;
var blobEncoding;
var propertyStore;
var recordStore;
var surveyStore;
var dump;
var available = false;
var databaseName = 'enketo';

function init() {
    return db.open( {
            server: databaseName,
            version: 1,
            schema: {
                // the surveys
                surveys: {
                    key: {
                        keyPath: 'enketoId',
                        autoIncrement: false
                    },
                    indexes: {
                        enketoId: {
                            unique: true
                        }
                    }
                },
                // the resources that belong to a survey
                resources: {
                    key: {
                        autoIncrement: false
                    },
                    indexes: {
                        key: {
                            unique: true
                        }
                    }
                },
                // Records in separate table because it makes more sense for getting, updating and removing records
                // if they are not stored in one (giant) array or object value.
                // Need to watch out for bad iOS bug: http://www.raymondcamden.com/2014/9/25/IndexedDB-on-iOS-8--Broken-Bad
                // but with the current keys there is no risk of using the same key in multiple tables.
                // InstanceId is the key because instanceName may change when editing a draft.
                records: {
                    key: {
                        keyPath: 'instanceId',
                    },
                    indexes: {
                        // useful to check if name exists
                        name: {
                            unique: true
                        },
                        // the actual key
                        instanceId: {
                            unique: true
                        },
                        // to get all records belonging to a form
                        enketoId: {
                            unique: false
                        }
                    }
                },
                // the files that belong to a record
                files: {
                    key: {
                        autoIncrement: false
                    },
                    indexes: {
                        key: {
                            unique: true
                        }
                    }
                },
                // settings or other global app properties
                properties: {
                    key: {
                        keyPath: 'name',
                        autoIncrement: false
                    },
                    indexes: {
                        key: {
                            unique: true
                        }
                    }
                }
            }
        } )
        .then( function( s ) {
            server = s;
            console.debug( 'WHoohoeeee, we\'ve got ourselves a database! Now let\'s check if it works properly.' );
        } )
        .then( _isWriteable )
        .then( _setBlobStorageEncoding )
        .then( function() {
            available = true;
        } )
        .catch( function( e ) {
            console.error( 'store initialization error', error );
{'fraction_non_alphanumeric': 0.07189357486555335, 'fraction_numerical': 0.002547410133031418, 'mean_word_length': 0.7492574257425743, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '97943', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1445, 'n_words': 471}