viewmodel
Advanced tools
Comparing version 1.2.7 to 1.2.8
@@ -17,76 +17,95 @@ 'use strict'; | ||
function generateEntity(obj) { | ||
var entity = _.clone(obj); | ||
for (var property in entity) { | ||
if (property !== '_metadata') { | ||
if (_.isArray(entity[property])) { | ||
entity[property] = JSON.stringify(entity[property]); | ||
continue; | ||
} | ||
if (_.isBoolean(entity[property])) { | ||
entity[property] = eg.Boolean(entity[property]); | ||
continue; | ||
} | ||
if (_.isDate(entity[property])) { | ||
entity[property] = eg.DateTime(entity[property]); | ||
continue; | ||
} | ||
if (_.isString(entity[property])) { | ||
entity[property] = eg.String(entity[property]); | ||
continue; | ||
} | ||
if (_.isObject(entity[property])) { | ||
entity[property] = JSON.stringify(entity[property]); | ||
continue; | ||
} | ||
var entity = _.clone(obj); | ||
for (var property in entity) { | ||
if (property !== '_metadata') { | ||
if (_.isArray(entity[property])) { | ||
entity[property] = JSON.stringify(entity[property]); | ||
continue; | ||
} | ||
if (_.isBoolean(entity[property])) { | ||
entity[property] = eg.Boolean(entity[property]); | ||
continue; | ||
} | ||
if (_.isDate(entity[property])) { | ||
entity[property] = eg.DateTime(entity[property]); | ||
continue; | ||
} | ||
if (_.isString(entity[property])) { | ||
entity[property] = eg.String(entity[property]); | ||
continue; | ||
} | ||
if (_.isObject(entity[property])) { | ||
entity[property] = eg.String(JSON.stringify(entity[property])); | ||
continue; | ||
} | ||
entity[property] = eg.Entity(entity[property]); | ||
entity[property] = eg.Entity(entity[property]); | ||
} | ||
} | ||
} | ||
return entity; | ||
return entity; | ||
} | ||
var generateObject = function (entity) { | ||
var obj = _.clone(entity); | ||
var obj = _.clone(entity); | ||
obj = _.omit(obj, 'Timestamp', 'PartitionKey', 'RowKey', '.metadata'); | ||
obj = _.omit(obj, 'Timestamp', 'PartitionKey', 'RowKey', '.metadata'); | ||
function isJsonString(str) { | ||
try { | ||
jsondate.parse(str); | ||
} catch (e) { | ||
return false; | ||
function isJsonString(str) { | ||
try { | ||
jsondate.parse(str); | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
return true; | ||
} | ||
for (var property in obj) { | ||
if (property !== '.metadata') { | ||
if (isJsonString(obj[property]['_'])) { | ||
obj[property]['_'] = jsondate.parse(obj[property]['_']); | ||
if (!_.isArray(obj[property]['_']) | ||
&& !_.isObject(obj[property]['_']) | ||
&& obj[property]['$'] | ||
&& obj[property]['$'] == 'Edm.String') { | ||
obj[property]['_'] = obj[property]['_'].toString(); | ||
for (var property in obj) { | ||
if (property !== '.metadata') { | ||
if (isJsonString(obj[property]['_'])) { | ||
obj[property]['_'] = jsondate.parse(obj[property]['_']); | ||
if (!_.isArray(obj[property]['_']) | ||
&& !_.isObject(obj[property]['_']) | ||
&& obj[property]['$'] | ||
&& obj[property]['$'] == 'Edm.String') { | ||
obj[property]['_'] = obj[property]['_'].toString(); | ||
} | ||
} | ||
obj[property] = obj[property]['_']; | ||
} | ||
} | ||
obj[property] = obj[property]['_']; | ||
} | ||
} | ||
return obj; | ||
return obj; | ||
}; | ||
var defaultQueryValues = function (queryValue) { | ||
var IsJsonString = function IsJsonString(str) { | ||
var parsed = true; | ||
try { | ||
parsed = jsondate.parse(str); | ||
} catch (e) { | ||
return false; | ||
} | ||
return _.isObject(parsed); | ||
}; | ||
if (IsJsonString(queryValue)) { | ||
queryValue = JSON.parse(queryValue); | ||
} else { | ||
queryValue = {value: queryValue, operator: 'eq'}; | ||
} | ||
return _.defaults(queryValue, {operator: 'eq'}); | ||
} | ||
// Class | ||
function AzureTable(options) { | ||
Repository.call(this, options); | ||
Repository.call(this, options); | ||
options = options || {}; | ||
options = options || {}; | ||
var azureConf = { | ||
storageAccount: 'nodeeventstore', | ||
storageAccessKey: 'aXJaod96t980AbNwG9Vh6T3ewPQnvMWAn289Wft9RTv+heXQBxLsY3Z4w66CI7NN12+1HUnHM8S3sUbcI5zctg==', | ||
storageTableHost: 'https://nodeeventstore.table.core.windows.net/' | ||
}; | ||
var azureConf = { | ||
storageAccount: 'nodeeventstore', | ||
storageAccessKey: 'aXJaod96t980AbNwG9Vh6T3ewPQnvMWAn289Wft9RTv+heXQBxLsY3Z4w66CI7NN12+1HUnHM8S3sUbcI5zctg==', | ||
storageTableHost: 'https://nodeeventstore.table.core.windows.net/' | ||
}; | ||
this.options = _.defaults(options, azureConf); | ||
this.options = _.defaults(options, azureConf); | ||
} | ||
@@ -98,283 +117,292 @@ | ||
connect: function (callback) { | ||
var retryOperations = new azure.ExponentialRetryPolicyFilter(); | ||
connect: function (callback) { | ||
var retryOperations = new azure.ExponentialRetryPolicyFilter(); | ||
var self = this; | ||
var self = this; | ||
this.client = azure.createTableService(this.options.storageAccount, this.options.storageAccessKey, this.options.storageTableHost).withFilter(retryOperations); | ||
this.client = azure.createTableService(this.options.storageAccount, this.options.storageAccessKey, this.options.storageTableHost).withFilter(retryOperations); | ||
self.emit('connect'); | ||
self.emit('connect'); | ||
if (callback) callback(null, self); | ||
}, | ||
if (callback) callback(null, self); | ||
}, | ||
disconnect: function (callback) { | ||
this.emit('disconnect'); | ||
if (callback) callback(null); | ||
}, | ||
disconnect: function (callback) { | ||
this.emit('disconnect'); | ||
if (callback) callback(null); | ||
}, | ||
get: function (id, callback) { | ||
get: function (id, callback) { | ||
var self = this; | ||
var options = {}; | ||
var self = this; | ||
var options = {}; | ||
if (_.isFunction(id)) { | ||
callback = id; | ||
id = null; | ||
} | ||
if (_.isFunction(id)) { | ||
callback = id; | ||
id = null; | ||
} | ||
if (!id) { | ||
id = uuid().toString(); | ||
} | ||
if (!id) { | ||
id = uuid().toString(); | ||
} | ||
options.autoResolveProperties = true; | ||
//options.entityResolver = generateObject; | ||
options.autoResolveProperties = true; | ||
//options.entityResolver = generateObject; | ||
self.checkConnection(function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
self.client.retrieveEntity(self.collectionName, | ||
id, | ||
id, | ||
options, | ||
function (err, entity) { | ||
if (err && err.code != 'ResourceNotFound') { | ||
return callback(err); | ||
self.checkConnection(function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (!entity) { | ||
return callback(null, new ViewModel({id: id}, self)); | ||
} | ||
self.client.retrieveEntity(self.collectionName, | ||
id, | ||
id, | ||
options, | ||
function (err, entity) { | ||
if (err && err.code != 'ResourceNotFound') { | ||
return callback(err); | ||
} | ||
entity = generateObject(entity); | ||
var vm = new ViewModel(entity, self); | ||
vm.actionOnCommit = 'update'; | ||
callback(null, vm); | ||
}); | ||
}); | ||
}, | ||
if (!entity) { | ||
return callback(null, new ViewModel({id: id}, self)); | ||
} | ||
find: function (query, queryOptions, callback) { | ||
entity = generateObject(entity); | ||
var vm = new ViewModel(entity, self); | ||
vm.actionOnCommit = 'update'; | ||
callback(null, vm); | ||
}); | ||
}); | ||
}, | ||
var self = this; | ||
find: function (query, queryOptions, callback) { | ||
var options = { | ||
autoResolveProperties: true, | ||
entityResolver: generateObject | ||
}; | ||
var self = this; | ||
queryOptions = _.defaults(queryOptions, {skip: 0, limit: -1}); | ||
var options = { | ||
autoResolveProperties: true, | ||
entityResolver: generateObject | ||
}; | ||
var tableQuery = new azure.TableQuery(); | ||
var pageSize = queryOptions.skip + queryOptions.limit; | ||
queryOptions = _.defaults(queryOptions, {skip: 0, limit: -1}); | ||
tableQuery = _(query) | ||
.reduce(function (result, val, key) { | ||
if (key.indexOf('.') == -1) { | ||
if (result._where.length === 0) return tableQuery.where(key + ' eq ?', val); | ||
return result.and(key + ' eq ?', val); | ||
} else { | ||
return result; | ||
} | ||
}, tableQuery); | ||
var tableQuery = new azure.TableQuery(); | ||
var pageSize = queryOptions.skip + queryOptions.limit; | ||
if (queryOptions.limit !== -1) { | ||
tableQuery = tableQuery.top(pageSize); | ||
} | ||
self.checkConnection(function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
tableQuery = _(query) | ||
.map(function (queryValue, queryKey) { | ||
if (_.isArray(queryValue)) { | ||
return _.map(queryValue, function (qV) { | ||
qV = defaultQueryValues(qV); | ||
return {key: queryKey + ' ' + qV.operator + ' ?', value: qV.value} | ||
}); | ||
} else { | ||
queryValue = defaultQueryValues(queryValue); | ||
return {key: queryKey + ' ' + queryValue.operator + ' ?', value: queryValue.value} | ||
} | ||
}) | ||
.flatten() | ||
.reduce(function (result, val, key) { | ||
if (result._where.length === 0) return tableQuery.where(val.key, val.value); | ||
return result.and(val.key, val.value); | ||
}, tableQuery); | ||
var entities = []; | ||
var continuationToken = queryOptions.continuationToken; | ||
if (queryOptions.limit !== -1) { | ||
tableQuery = tableQuery.top(pageSize); | ||
} | ||
async.doWhilst(function (end) { | ||
// retrieve entities | ||
self.client.queryEntities(self.collectionName, tableQuery, continuationToken, options, function (err, results) { | ||
if (err) { | ||
return end(err); | ||
} | ||
self.checkConnection(function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
continuationToken = results.continuationToken; | ||
var entities = []; | ||
var continuationToken = queryOptions.continuationToken; | ||
entities = entities.concat(results.entries); | ||
async.doWhilst(function (end) { | ||
// retrieve entities | ||
self.client.queryEntities(self.collectionName, tableQuery, continuationToken, options, function (err, results) { | ||
if (err) { | ||
return end(err); | ||
} | ||
end(null); | ||
}); | ||
}, function () { | ||
// test if we need to load more | ||
return (entities.length < pageSize || pageSize == -1) ? continuationToken !== null : false; | ||
}, function (err) { | ||
continuationToken = results.continuationToken; | ||
// return results | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (queryOptions.skip !== undefined && queryOptions.limit !== undefined) { | ||
if (queryOptions.limit === -1) { | ||
entities = entities.slice(queryOptions.skip); | ||
} else { | ||
entities = entities.slice(queryOptions.skip, pageSize); | ||
} | ||
} | ||
entities = entities.concat(results.entries); | ||
var vms = _.map(entities, function (data) { | ||
var vm = new ViewModel(data, self); | ||
vm.actionOnCommit = 'update'; | ||
return vm; | ||
end(null); | ||
}); | ||
}, function () { | ||
// test if we need to load more | ||
return (entities.length < pageSize || pageSize == -1) ? continuationToken !== null : false; | ||
}, function (err) { | ||
// return results | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (queryOptions.skip !== undefined && queryOptions.limit !== undefined) { | ||
if (queryOptions.limit === -1) { | ||
entities = entities.slice(queryOptions.skip); | ||
} else { | ||
entities = entities.slice(queryOptions.skip, pageSize); | ||
} | ||
} | ||
var vms = _.map(entities, function (data) { | ||
var vm = new ViewModel(data, self); | ||
vm.actionOnCommit = 'update'; | ||
return vm; | ||
}); | ||
if (continuationToken) { | ||
vms.continuationToken = continuationToken; | ||
} | ||
callback(null, vms); | ||
}); | ||
}); | ||
}, | ||
if (continuationToken) { | ||
vms.continuationToken = continuationToken; | ||
} | ||
commit: function (vm, callback) { | ||
callback(null, vms); | ||
}); | ||
}); | ||
}, | ||
var self = this; | ||
commit: function (vm, callback) { | ||
self.checkConnection(function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var self = this; | ||
if (!vm.actionOnCommit) return callback(new Error()); | ||
self.checkConnection(function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var objDescriptor = { | ||
PartitionKey: eg.String(vm.id), | ||
RowKey: eg.String(vm.id) | ||
}; | ||
if (!vm.actionOnCommit) return callback(new Error()); | ||
var obj; | ||
var objDescriptor = { | ||
PartitionKey: eg.String(vm.id), | ||
RowKey: eg.String(vm.id) | ||
}; | ||
switch (vm.actionOnCommit) { | ||
case 'delete': | ||
var obj; | ||
self.client.deleteEntity(self.collectionName, objDescriptor, function (err) { | ||
//if (err) { | ||
// if (callback) callback(err); | ||
// return; | ||
//} | ||
if (callback) callback(null, vm); | ||
}); | ||
switch (vm.actionOnCommit) { | ||
case 'delete': | ||
break; | ||
self.client.deleteEntity(self.collectionName, objDescriptor, function (err) { | ||
//if (err) { | ||
// if (callback) callback(err); | ||
// return; | ||
//} | ||
if (callback) callback(null, vm); | ||
}); | ||
case 'create': | ||
break; | ||
obj = vm.toJSON(); | ||
obj = generateEntity(obj); | ||
obj = _.assign(obj, objDescriptor); | ||
case 'create': | ||
self.client.insertEntity(self.collectionName, obj, function (err) { | ||
if (err) { | ||
if (err.code == 'EntityAlreadyExists') err = new ConcurrencyError(); | ||
if (callback) callback(err); | ||
return; | ||
} | ||
vm.actionOnCommit = 'update'; | ||
if (callback) callback(null, vm); | ||
}); | ||
break; | ||
obj = vm.toJSON(); | ||
obj = generateEntity(obj); | ||
obj = _.assign(obj, objDescriptor); | ||
case 'update': | ||
self.client.insertEntity(self.collectionName, obj, function (err) { | ||
if (err) { | ||
if (err.code == 'EntityAlreadyExists') err = new ConcurrencyError(); | ||
if (callback) callback(err); | ||
return; | ||
} | ||
vm.actionOnCommit = 'update'; | ||
if (callback) callback(null, vm); | ||
}); | ||
break; | ||
obj = vm.toJSON(); | ||
obj = generateEntity(obj); | ||
obj = _.assign(obj, objDescriptor); | ||
case 'update': | ||
self.client.insertOrReplaceEntity(self.collectionName, obj, function (err) { | ||
if (err) { | ||
if (err.code == 'ConditionNotMet' && err.statusCode == 412) err = new ConcurrencyError(); | ||
if (callback) callback(err); | ||
return; | ||
} | ||
vm.actionOnCommit = 'update'; | ||
if (callback) callback(null, vm); | ||
}); | ||
break; | ||
obj = vm.toJSON(); | ||
obj = generateEntity(obj); | ||
obj = _.assign(obj, objDescriptor); | ||
default: | ||
return callback(new Error()); | ||
} | ||
}); | ||
}, | ||
self.client.insertOrReplaceEntity(self.collectionName, obj, function (err) { | ||
checkConnection: function (callback) { | ||
var self = this; | ||
this.client.createTableIfNotExists(this.collectionName, function (err) { | ||
if (err) { | ||
if (err.code == 'ConditionNotMet' && err.statusCode == 412) err = new ConcurrencyError(); | ||
if (callback) callback(err); | ||
return; | ||
return callback(err); | ||
} | ||
vm.actionOnCommit = 'update'; | ||
if (callback) callback(null, vm); | ||
}); | ||
break; | ||
default: | ||
return callback(new Error()); | ||
} | ||
}); | ||
}, | ||
if (collections.indexOf(self.collectionName) < 0) { | ||
collections.push(self.collectionName); | ||
} | ||
checkConnection: function (callback) { | ||
var self = this; | ||
this.client.createTableIfNotExists(this.collectionName, function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
callback(null); | ||
}); | ||
}, | ||
if (collections.indexOf(self.collectionName) < 0) { | ||
collections.push(self.collectionName); | ||
} | ||
clear: function (callback) { | ||
var self = this; | ||
callback(null); | ||
}); | ||
}, | ||
this.checkConnection(function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
clear: function (callback) { | ||
var self = this; | ||
if (!self.collectionName) { | ||
if (callback) callback(null); | ||
return; | ||
} | ||
this.checkConnection(function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var query = new azure.TableQuery(); | ||
if (!self.collectionName) { | ||
if (callback) callback(null); | ||
return; | ||
} | ||
var query = new azure.TableQuery(); | ||
self.client.queryEntities(self.collectionName, query, null, function (err, entities) { | ||
if (err) { | ||
if (callback) callback(err); | ||
return; | ||
} | ||
async.each(entities.entries, function (entity, callback) { | ||
self.client.deleteEntity(col, entity, function (error, response) { | ||
if (callback) callback(error); | ||
self.client.queryEntities(self.collectionName, query, null, function (err, entities) { | ||
if (err) { | ||
if (callback) callback(err); | ||
return; | ||
} | ||
async.each(entities.entries, function (entity, callback) { | ||
self.client.deleteEntity(col, entity, function (error, response) { | ||
if (callback) callback(error); | ||
}); | ||
}, | ||
function (error) { | ||
if (callback) callback(error); | ||
} | ||
); | ||
}); | ||
}, | ||
function (error) { | ||
if (callback) callback(error); | ||
} | ||
); | ||
}); | ||
}); | ||
}, | ||
}); | ||
}, | ||
clearAll: function (callback) { | ||
var self = this; | ||
var query = new azure.TableQuery(); | ||
clearAll: function (callback) { | ||
var self = this; | ||
var query = new azure.TableQuery(); | ||
async.each(collections, function (col, callback) { | ||
async.each(collections, function (col, callback) { | ||
self.client.queryEntities(col, query, null, function (err, entities) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
async.each(entities.entries, function (entity, callback) { | ||
self.client.deleteEntity(col, entity, function (error, response) { | ||
callback(error); | ||
}); | ||
}, | ||
function (error) { | ||
callback(error); | ||
self.client.queryEntities(col, query, null, function (err, entities) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
async.each(entities.entries, function (entity, callback) { | ||
self.client.deleteEntity(col, entity, function (error, response) { | ||
callback(error); | ||
}); | ||
}, | ||
function (error) { | ||
callback(error); | ||
}); | ||
}); | ||
}); | ||
}, callback); | ||
} | ||
}, callback); | ||
} | ||
@@ -381,0 +409,0 @@ }); |
{ | ||
"author": "adrai", | ||
"name": "viewmodel", | ||
"version": "1.2.7", | ||
"version": "1.2.8", | ||
"private": false, | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
@@ -0,1 +1,4 @@ | ||
## [v1.2.8](https://github.com/adrai/node-viewmodel/compare/v1.2.7...v1.2.8) | ||
- some fix for azure-table [#9](https://github.com/adrai/node-viewmodel/pull/9) thanks to [sbiaudet](https://github.com/sbiaudet) | ||
## [v1.2.7](https://github.com/adrai/node-viewmodel/compare/v1.2.6...v1.2.7) | ||
@@ -2,0 +5,0 @@ - fix usage with own db implementation |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71815
1900