Currently, I'm in the process of developing an AngularJS service provider (function) that achieves the following objectives:
- Gathers data from multiple tables within an SQLite database
- Delivers the resulting object to various controller functions
Since the service interacts with different tables having varying columns, the properties of the result
object vary based on the source table.
- Using
Object.defineProperties
for specific object properties isn't feasible due to the uncertainty of the properties prior to creating the query 'case' - One option is to define the object variable properties within the 'switch' statement, although this approach appears messy...
Upon returning the object to the controller functions, certain manipulations are necessary:
- Ability to override some properties in the returned object (i.e., requiring
writable: true
) - Precisely, employing JSON.parse() followed by overwriting various properties as arrays stored in the SQLite DB are converted using JSON.stringify() before insertion and stored as strings in the DB
- Encountering complications since the default for the object is
writable: false
Inquiry:
How can I establish an object with attributes
for all (future) properties of an object? In other words, how do you set the default attributes for an object without knowing the exact property names of the object beforehand?configurable: true, writable: true, enumerable: true
Sample code:
this.checkRowExists = function(table, id) {
try {
var deferred = $q.defer();
switch (table) {
case "table1" :
var selectQuery = 'SELECT * FROM ' + table + ' WHERE id=?';
break;
case "table2" :
var selectQuery = 'SELECT * FROM ' + table + ' WHERE id=?';
break;
case "table3" :
var selectQuery = 'SELECT * FROM ' + table + ' WHERE id=?';
break;
}
this.db.transaction(function(tx) {
tx.executeSql(selectQuery, [id], function(tx, results) {
if (results.rows.length > 0){
var myRow = {}; // <- How can all properties in the "myRow" object be defined as configurable and writable ??
var myRow = results.rows.item(0);
deferred.resolve(myRow); // <- The object "myRow" is then returned to various controller functions
} else {
deferred.resolve(results.rows.length);
}
}, function(tx, error) {
console.log('checkRowExists: Error occurred while searching for profile.', error);
deferred.reject(error);
});
});
return deferred.promise;
} catch(exception) {
console.log('checkRowExists: Error occurred while trying this function.', exception);
}
};
PS: Although the code functionality is intact, it only returns an object with
writable: false, enumerable: true, configurable: false
, whereas every attribute needs to be set to true
UPDATE:
Despite a partial resolution below, lingering questions persist:
- Why can't a property descriptor be manually altered utilizing the Object.defineProperty
method?
- How come enumerable: true
when it should default to false? Could this be linked to SQLite db transactions?
- Why aren't the defaults of
writable: false, enumerable: false, configurable: false
being applied to the new object cloned using the JSON methodology?The solution can be accessed here - contingent on how the object is formed - refer to the commentary below