Files
PyRIGS/RIGS/static/js/ravenjs.js

1 line
4.6 KiB
JavaScript

var querystring=require("querystring"),RavenClient=require("./lib/RavenClient"),HiLoIdGenerator=require("./lib/HiLoIdGenerator"),filter=require("./lib/filter");errorCodes=require("./lib/errorCodes"),_=require("lodash"),inflect=require("i")();var settings={host:"http://localhost:80",idFinder:defaultIdFinder,idGenerator:defaultIdGenerator,useOptimisticConcurrency:!1};function defaultIdFinder(e){if(e)return e["@metadata"]&&e["@metadata"]["@id"]?e["@metadata"]["@id"]:e.hasOwnProperty("id")?e.id:e.hasOwnProperty("Id")?e.Id:void 0}function defaultIdGenerator(e,t,r){if(!e)throw Error("Expected a valid doc object.");if(!t)throw Error("Expected a valid setings object.");if(!t.host)throw Error("Invalid settings. Expected host property.");if(!r||!_(r).isFunction)throw Error("Exepected a valid callback function.");new HiLoIdGenerator(t).nextId((function(t,i){return t?r(t):(collectionName="",e&&e["@metadata"]&&_.isString(e["@metadata"]["Raven-Entity-Name"])&&(collectionName=inflect.camelize(inflect.underscore(e["@metadata"]["Raven-Entity-Name"]),!1)+"/"),i=collectionName+i,r(void 0,i.toString()))}))}exports.connectionString=function(e){var t=this;if(!arguments.length)return settings.host;if(!_.isString(e))throw new Error("Expected a valid raven connection string");var r=querystring.parse(e,";","=");if(!r.Url)throw new Error('Required connection string property "Url" was not specified!');t.host(r.Url),t.database(r.Database),t.username(r.UserName),t.password(r.Password),t.apiKey(r.ApiKey)},exports.host=function(e){if(!arguments.length)return settings.host;if(!_.isString(e))throw new Error("Expected a valid raven host name");if(!~e.indexOf("http://")&&!~e.indexOf("https://"))throw new Error("Expected a host address with http:// or https://");settings.host=e},exports.database=function(e){if(!arguments.length)return settings.database;if(e){if(!_.isString(e))throw new Error("Expected a string for database name");settings.database=e}else settings.database&&delete settings.database},exports.username=function(e){if(!arguments.length)return settings.username;if(e){if(!_.isString(e))throw new Error("Expected a string for username");settings.username=e}else settings.username&&delete settings.username},exports.password=function(e){if(!arguments.length)return settings.password;if(e){if(!_.isString(e))throw new Error("Expected a string for password");settings.password=e}else settings.password&&delete settings.password},exports.apiKey=function(e){if(!arguments.length)return settings.apiKey;if(e){if(!_.isString(e))throw new Error("Expected a string for apiKey");settings.apiKey=e}else settings.apiKey&&delete settings.apiKey},exports.idFinder=function(e){if(!e)return settings.idFinder=defaultidFinder;if(!_(e).isFunction())throw new Error("Expected a valid function to use as the default key finder.");settings.idFinder=e},exports.idGenerator=function(e){if(!e)return settings.idGenerator=defaultIdGenerator;if(!_(e).isFunction())throw new Error("Expected a valid function to use as the default key generator.");settings.idGenerator=e},exports.useOptimisticConcurrency=function(e){if(!e)return settings.useOptimisticConcurrency;if(!_(e).isBoolean())throw new Error("Expected a boolean value when setting useOptimisticConcurrency");settings.useOptimisticConcurrency=e},exports.proxy=function(e){if(!e)return settings.proxy;if(!_.isString(e))throw new Error("Expected a valid proxy host address.");if(!~e.indexOf("http://")&&!~e.indexOf("https://"))throw new Error("Invaid proxy address scheme. Expected http or https scheme.");settings.proxy=e},exports.configure=function(e,t){_(e).isFunction()&&(t=e,e="all");var r=process.env.NODE_ENV||"development";("all"===e||~e.indexOf(r))&&t.call(this)},exports.defaultIdFinder=defaultIdFinder,exports.defaultIdGenerator=defaultIdGenerator,exports.connect=function(e){var t=_(settings).clone();return _(e).isObject()&&(t.host=e.host||t.host,t.database=e.database||t.database,t.username=e.username||t.username,t.password=e.password||t.password,t.apiKey=e.apiKey||t.apiKey,t.idFinder=e.idFinder||t.idFinder,t.idGenerator=e.idGenerator||t.idGenerator,t.proxy=e.proxy||t.proxy,t.useOptimisticConcurrency=e.useOptimisticConcurrency||t.useOptimisticConcurrency),new RavenClient(t)},exports.create=function(e,t){var r={},i={},n=!1;if(e){if(!_(e).isString())throw new Error("Expected a valid string for typeName");i["Raven-Clr-Type"]=e}if(_(t).isBoolean()&&(n=!1===t,t=void 0),t){if(!_(t).isString())throw new Error("Expected a valid string or bool for collectionName");i["Raven-Entity-Name"]=t}else e&&(t=n?e:inflect.pluralize(e),i["Raven-Entity-Name"]=t);return _.isEmpty(i)||(r["@metadata"]=i),r},exports.errorCodes=errorCodes;