Add support for nested json metadata.
This commit is contained in:
@@ -229,6 +229,18 @@ MBTiles.prototype.getInfo = function(callback) {
|
||||
if (err && err.errno !== 1) return callback(err);
|
||||
if (rows) rows.forEach(function(row) {
|
||||
switch (row.name) {
|
||||
// The special "json" key/value pair allows JSON to be serialized
|
||||
// and merged into the metadata of an MBTiles based source. This
|
||||
// enables nested properties and non-string datatypes to be
|
||||
// captured by the MBTiles metadata table.
|
||||
case 'json':
|
||||
try { var jsondata = JSON.parse(row.value); }
|
||||
catch (err) { return callback(err); }
|
||||
Object.keys(jsondata).reduce(function(memo, key) {
|
||||
memo[key] = memo[key] || jsondata[key];
|
||||
return memo;
|
||||
}, info);
|
||||
break;
|
||||
case 'minzoom':
|
||||
case 'maxzoom':
|
||||
info[row.name] = parseInt(row.value, 10);
|
||||
@@ -537,11 +549,21 @@ MBTiles.prototype.putInfo = function(data, callback) {
|
||||
if (!this.open) return callback(new Error('MBTiles not yet loaded'));
|
||||
if (!this._isWritable) return callback(new Error('MBTiles not in write mode'));
|
||||
|
||||
var jsondata;
|
||||
var stmt = this._db.prepare('REPLACE INTO metadata (name, value) VALUES (?, ?)');
|
||||
stmt.on('error', callback);
|
||||
for (var key in data) {
|
||||
stmt.run(key, String(data[key]));
|
||||
// If a data property is a javascript hash/object, slip it into
|
||||
// the 'json' field which contains stringified JSON to be merged
|
||||
// in at read time. Allows nested/deep metadata to be recorded.
|
||||
if (typeof data[key] === 'object' && !Array.isArray(data[key])) {
|
||||
jsondata = jsondata || {};
|
||||
jsondata[key] = data[key];
|
||||
} else {
|
||||
stmt.run(key, String(data[key]));
|
||||
}
|
||||
}
|
||||
if (jsondata) stmt.run('json', JSON.stringify(jsondata));
|
||||
|
||||
var mbtiles = this;
|
||||
stmt.finalize(function(err) {
|
||||
|
||||
Reference in New Issue
Block a user