jaydata
Version:
Cross-platform HTML5 data-management, JavaScript Language Query (JSLQ) support for OData, SQLite, WebSQL, IndexedDB, YQL and Facebook (packaged for Node.JS)
900 lines (795 loc) • 43.7 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _index = require('../TypeSystem/index.js');
var _index2 = _interopRequireDefault(_index);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_index2.default.Class.define('$data.Queryable', null, null, {
constructor: function constructor(source, rootExpression) {
/// <signature>
/// <summary>Provides a base class for classes supporting JavaScript Language Query.</summary>
/// <description>Provides a base class for classes supporting JavaScript Language Query.</description>
/// <param name="source" type="$data.EntitySet" />
/// <param name="rootExpression" type="$data.Expressions.ExpressionNode"></param>
/// </signature>
/// <signature>
/// <summary>Provides a base class for classes supporting JavaScript Language Query.</summary>
/// <description>Provides a base class for classes supporting JavaScript Language Query.</description>
/// <param name="source" type="$data.EntityContext" />
/// <param name="rootExpression" type="$data.Expressions.ExpressionNode"></param>
/// </signature>
var context = source instanceof _index2.default.EntityContext ? source : source.entityContext;
this.defaultType = source instanceof _index2.default.EntityContext ? null : source.defaultType;
this.entityContext = context;
this.expression = rootExpression;
},
filter: function filter(predicate, thisArg) {
///<summary>Filters a set of entities using a boolean expression.</summary>
///<param name="predicate" type="Function">A boolean query expression</param>
///<param name="thisArg" type="Object">The query parameters</param>
///<returns type="$data.Queryable" />
///<signature>
///<summary>Filters a set of entities using a boolean expression formulated as string.</summary>
///<param name="predicate" type="string">
///The expression body of the predicate function in string.
///To reference the lambda parameter use the 'it' context variable.
///Example: filter("it.Title == 'Hello'")
///</param>
///<param name="thisArg" type="Object" />
///<returns type="$data.Queryable" />
///</signature>
///<signature>
///<summary>Filters a set of entities using a bool expression formulated as a JavaScript function.</summary>
///<param name="predicate" type="Function">
///</param>
///<param name="thisArg" type="Object" optional="true">
///Contains the predicate parameters
///</param>
///<returns type="$data.Queryable" />
///<example>
///Filtering a set of entities with a predicate function
///var males = Persons.filter( function( person ) { return person.Gender == 'Male' } );
///</example>
///<example>
///Filtering a set of entities with a predicate function and parameters
///var draftables = Persons.filter( function( person ) {
/// return person.Gender == this.gender && person.Age > this.age
/// }, { gender: 'Male', age: 21 });
///</example>
///<example>
///Filtering a set of entities with a predicate as a string and parameters
///var draftables = Persons.filter("it.Gender == this.gender && it.Age > this.age",
/// { gender: 'Male', age: 21 });
///</example>
///</signature>
if (arguments.length === 3) {
predicate = "it." + arguments[0] + (arguments[1][0] === "." ? arguments[1] + "(param)" : " " + arguments[1] + " param");
thisArg = { param: arguments[2] };
}
this._checkOperation('filter');
var expression = _index.Container.createCodeExpression(predicate, thisArg);
var expressionSource = this.expression;
if (this.expression instanceof _index2.default.Expressions.FilterExpression) {
expressionSource = this.expression.source;
var operatorResolution = this.entityContext.storageProvider.resolveBinaryOperator("and");
expression = _index.Container.createSimpleBinaryExpression(this.expression.selector, expression, "and", "filter", "boolean", operatorResolution);
}
var exp = _index.Container.createFilterExpression(expressionSource, expression);
var q = _index.Container.createQueryable(this, exp);
return q;
},
where: function where(predicate, params) {
///<summary>Where is a convenience alias for C# developers. Use filter instead.</summary>
///<returns type="$data.Queryable" />
return this.filter(predicate, params);
},
map: function map(projection, thisArg, mappedTo) {
/// <summary>Map specifies the shape or type of each returned element. You can specify whether your results will consist of complete Person objects, just one member, a subset of members, or some completely different result type based on a computation or new object creation. When map produces something other than a copy of the source element, the operation is called a projection. The use of projections to transform data is a powerful capability of JavaScript Language Query expressions.</summary>
/// <param name="projection" type="Function">A projection expression</param>
/// <param name="thisArg" type="Object">The query parameters</param>
/// <returns type="$data.Queryable" />
/// <signature>
/// <summary>Map specifies the shape or type of each returned element. You can specify whether your results will consist of complete Person objects, just one member, a subset of members, or some completely different result type based on a computation or new object creation. When map produces something other than a copy of the source element, the operation is called a projection. The use of projections to transform data is a powerful capability of JavaScript Language Query expressions.</summary>
/// <param name="projection" type="string">
/// The expression body of the projection function in string.
/// To reference the lambda parameter use the 'it' context variable.
/// Example: map("{ i: it.Id, t: it.Title }")
/// </param>
/// <param name="thisArg" type="Object" />
/// <returns type="$data.Queryable" />
/// </signature>
/// <signature>
/// <summary>Map specifies the shape or type of each returned element. You can specify whether your results will consist of complete Person objects, just one member, a subset of members, or some completely different result type based on a computation or new object creation. When map produces something other than a copy of the source element, the operation is called a projection. The use of projections to transform data is a powerful capability of JavaScript Language Query expressions.</summary>
/// <param name="projection" type="Function">
/// Projection function to specify the shape or type of each returned element.
/// </param>
/// <param name="thisArg" type="Object" optional="true">
/// Contains the projection parameters.
/// </param>
/// <returns type="$data.Queryable" />
/// <example>
/// Projection to get an array of the full name property of a set of Person entities
/// var personFullNames = Persons.map( function( person ) { return person.FullName; } );
/// </example>
/// <example>
/// Projection to get an array of the required fields of Person entities in an anonymous type.
/// var custom = Persons.map( function( person ) {
/// return { FullName: person.FullName, Info: { Address: person.Location.Address, Phone: person.Phone } };
/// });
/// </example>
/// </signature>
this._checkOperation('map');
var codeExpression = _index.Container.createCodeExpression(projection, thisArg);
var exp = _index.Container.createProjectionExpression(this.expression, codeExpression);
if (mappedTo === 'default') exp.projectionAs = this.defaultType;else if (mappedTo) exp.projectionAs = _index.Container.resolveType(mappedTo);else exp.projectionAs = _index2.default.Object;
var q = _index.Container.createQueryable(this, exp);
return q;
},
select: function select(projection, thisArg, mappedTo) {
///<summary>Select is a convenience alias for C# developers. Use map instead.</summary>
///<returns type="$data.Queryable" />
return this.map(projection, thisArg, mappedTo);
},
length: function length(onResult, transaction) {
/// <summary>Returns the number of entities (or projected object) in a query as the callback parameter.</summary>
/// <param name="onResult" type="Function">A callback function</param>
/// <returns type="$data.Promise" />
/// <signature>
/// <summary>Returns the number of entities (or projected object) in a query as the callback parameter.</summary>
/// <param name="onResult" type="Function">
/// The callback function to handle the result.
/// </param>
/// <returns type="$data.Promise" />
/// </signature>
/// <signature>
/// <summary>Returns the number of entities (or projected object) in a query as the callback parameter.</summary>
/// <param name="onResult" type="Object">
/// Object of callback functions to handle success and error.
/// Example: { success: function(cnt) { ... }, error: function() { alert("Something went wrong..."); } }
/// </param>
/// <returns type="$data.Promise" />
/// <example>
/// Get the count of Person entities.
/// Persons.length( function( cnt ) { alert("There are " + cnt + " person(s) in the database."); } );
/// </example>
/// </signature>
this._checkOperation('length');
var pHandler = new _index2.default.PromiseHandler();
var cbWrapper = pHandler.createCallback(onResult);
var countExpression = _index.Container.createCountExpression(this.expression);
var preparator = _index.Container.createQueryExpressionCreator(this.entityContext);
try {
var expression = preparator.Visit(countExpression);
this.entityContext.log({ event: "EntityExpression", data: expression });
this.entityContext.executeQuery(_index.Container.createQueryable(this, expression), cbWrapper, transaction);
} catch (e) {
cbWrapper.error(e);
}
return pHandler.getPromise();
},
count: function count(onResult, transaction) {
///<summary>Count is a convenience alias for C# developers. Use length instead.</summary>
///<returns type="$data.Integer" />
return this.length(onResult, transaction);
},
forEach: function forEach(iterator, transaction) {
/// <summary>Calls the iterator function for all entity (or projected object) in the query.</summary>
/// <param name="iterator" type="Function">Iterator function</param>
/// <returns type="$data.Promise" />
/// <signature>
/// <summary>Calls the iterator function for all entity (or projected object) in the query.</summary>
/// <param name="iterator" type="Function">
/// Iterator function to handle the result elements.
/// </param>
/// <returns type="$data.Promise" />
/// <example>
/// Log the full name of each Person.
/// Persons.forEach( function( person ) { console.log(person.FullName; } );
/// </example>
/// </signature>
this._checkOperation('forEach');
var pHandler = new _index2.default.PromiseHandler();
function iteratorFunc(items) {
items.forEach(iterator);
}
var cbWrapper = pHandler.createCallback(iteratorFunc);
var forEachExpression = _index.Container.createForEachExpression(this.expression);
var preparator = _index.Container.createQueryExpressionCreator(this.entityContext);
try {
var expression = preparator.Visit(forEachExpression);
this.entityContext.log({ event: "EntityExpression", data: expression });
this.entityContext.executeQuery(_index.Container.createQueryable(this, expression), cbWrapper, transaction);
} catch (e) {
cbWrapper.error(e);
}
return pHandler.getPromise();
},
toArray: function toArray(onResult_items, transaction) {
/// <summary>Returns the query result as the callback parameter.</summary>
/// <param name="onResult_items" type="Function">A callback function</param>
/// <returns type="$data.Promise" />
/// <signature>
/// <summary>Returns the query result as the callback parameter.</summary>
/// <param name="onResult_items" type="Function">
/// The callback function to handle the result.
/// </param>
/// <returns type="$data.Promise" />
/// </signature>
/// <signature>
/// <summary>Returns the query result as the callback parameter.</summary>
/// <param name="onResult_items" type="Object">
/// Object of callback functions to handle success and error.
/// Example: { success: function(result) { ... }, error: function() { alert("Something went wrong..."); } }
/// </param>
/// <returns type="$data.Promise" />
/// <example>
/// Get all Person entities.
/// Persons.toArray( function( result ) { console.dir(result); } );
/// </example>
/// </signature>
if (onResult_items instanceof _index2.default.Array) {
return this.toArray(function (results) {
onResult_items.length = 0;
results.forEach(function (item, idx) {
onResult_items.push(item);
});
});
}
this._checkOperation('toArray');
var pHandler = new _index2.default.PromiseHandler();
var cbWrapper = pHandler.createCallback(onResult_items);
var toArrayExpression = _index.Container.createToArrayExpression(this.expression);
var preparator = _index.Container.createQueryExpressionCreator(this.entityContext);
try {
var expression = preparator.Visit(toArrayExpression);
this.entityContext.log({ event: "EntityExpression", data: expression });
this.entityContext.executeQuery(_index.Container.createQueryable(this, expression), cbWrapper, transaction);
} catch (e) {
cbWrapper.error(e);
}
return pHandler.getPromise();
},
toLiveArray: function toLiveArray(onResult, transaction) {
var self = this;
var result = [];
var doAction = function doAction(action) {
return function (onResult) {
var pHandler = new _index2.default.PromiseHandler();
var callback = pHandler.createCallback(onResult);
var successFunc = function successFunc(res) {
result.length = 0;
var data = res;
_index2.default.typeSystem.extend(result, data);
result.prev = doAction(function (cb) {
data.prev(cb);
});
result.next = doAction(function (cb) {
data.next(cb);
});
callback.success.apply(this, [result].concat(Array.prototype.slice.call(arguments, 1)));
};
action({
success: successFunc,
error: callback.error
}, transaction);
var promise = pHandler.getPromise();
_index2.default.typeSystem.extend(result, promise);
return result;
};
};
result.refresh = doAction(function (cb) {
self.toArray(cb);
});
return result.refresh.apply(result, arguments);
},
single: function single(filterPredicate, thisArg, onResult, transaction) {
/// <summary>Filters a set of entities using a boolean expression and returns a single element or throws an error if more than one element is filtered.</summary>
/// <param name="onResult_items" type="Function">A callback function</param>
/// <returns type="$data.Promise" />
/// <signature>
/// <summary>Filters a set of entities using a boolean expression and returns a single element or throws an error if more than one element is filtered.</summary>
/// <param name="filterPredicate" type="string">
/// Same as in filter.
/// </param>
/// <param name="onResult" type="Function">
/// The callback function to handle the result, same as in toArray.
/// </param>
/// <returns type="$data.Promise" />
/// </signature>
/// <signature>
/// <summary>Filters a set of entities using a boolean expression and returns a single element or throws an error if more than one element is filtered.</summary>
/// <param name="filterPredicate" type="Function">
/// Same as in filter.
/// </param>
/// <param name="onResult" type="Function">
/// The callback function to handle the result, same as in toArray.
/// </param>
/// <returns type="$data.Promise" />
/// <example>
/// Get "George" from the Person entity set.
/// Persons.single( function( person ) { return person.FirstName == this.name; }, { name: "George" }, {
/// success: function ( result ){ ... },
/// error: function () { ... }
/// });
/// </example>
/// </signature>
this._checkOperation('single');
var q = this;
if (filterPredicate) {
q = this.filter(filterPredicate, thisArg);
}
q = q.take(2);
var pHandler = new _index2.default.PromiseHandler();
var cbWrapper = pHandler.createCallback(onResult);
var singleExpression = _index.Container.createSingleExpression(q.expression);
var preparator = _index.Container.createQueryExpressionCreator(q.entityContext);
try {
var expression = preparator.Visit(singleExpression);
this.entityContext.log({ event: "EntityExpression", data: expression });
q.entityContext.executeQuery(_index.Container.createQueryable(q, expression), cbWrapper, transaction);
} catch (e) {
cbWrapper.error(e);
}
return pHandler.getPromise();
},
some: function some(filterPredicate, thisArg, onResult, transaction) {
/// <summary>Filters a set of entities using a boolean expression and returns true if the query has any result element.</summary>
/// <param name="filterPredicate" type="Function">Filter function</param>
/// <param name="thisArg" type="Function">The query parameters for filter function</param>
/// <param name="onResult_items" type="Function">A callback function</param>
/// <returns type="$data.Promise" />
/// <signature>
/// <summary>Filters a set of entities using a boolean expression and returns true if the query has any result element.</summary>
/// <param name="filterPredicate" type="string">
/// Same as in filter.
/// </param>
/// <param name="onResult" type="Function">
/// The callback function to handle the result, same as in toArray.
/// </param>
/// <returns type="$data.Promise" />
/// </signature>
/// <signature>
/// <summary>Filters a set of entities using a boolean expression and returns true if the query has any result element.</summary>
/// <param name="filterPredicate" type="Function">
/// Same as in filter.
/// </param>
/// <param name="onResult" type="Function">
/// The callback function to handle the result, same as in toArray.
/// </param>
/// <returns type="$data.Promise" />
/// <example>
/// Is there any person who's first name is "George"?
/// Persons.some( function( person ) { return person.FirstName == this.name; }, { name: "George" }, {
/// success: function ( result ){ ... },
/// error: function () { ... }
/// });
/// </example>
/// </signature>
this._checkOperation('some');
var q = this;
if (filterPredicate) {
q = this.filter(filterPredicate, thisArg);
}
q = q.take(1);
var pHandler = new _index2.default.PromiseHandler();
var cbWrapper = pHandler.createCallback(onResult);
var someExpression = _index.Container.createSomeExpression(q.expression);
var preparator = _index.Container.createQueryExpressionCreator(q.entityContext);
try {
var expression = preparator.Visit(someExpression);
this.entityContext.log({ event: "EntityExpression", data: expression });
q.entityContext.executeQuery(_index.Container.createQueryable(q, expression), cbWrapper, transaction);
} catch (e) {
cbWrapper.error(e);
}
return pHandler.getPromise();
},
every: function every(filterPredicate, thisArg, onResult, transaction) {
/// <summary>Filters a set of entities using a boolean expression and returns true if all elements of the EntitySet is in the result set.</summary>
/// <param name="filterPredicate" type="Function">Filter function</param>
/// <param name="thisArg" type="Function">The query parameters for filter function</param>
/// <param name="onResult_items" type="Function">A callback function</param>
/// <returns type="$data.Promise" />
/// <signature>
/// <summary>Filters a set of entities using a boolean expression and returns a </summary>
/// <param name="filterPredicate" type="string">
/// Same as in filter.
/// </param>
/// <param name="onResult" type="Function">
/// The callback function to handle the result, same as in toArray.
/// </param>
/// <returns type="$data.Promise" />
/// </signature>
/// <signature>
/// <summary>Filters a set of entities using a boolean expression and returns a single element or throws an error if more than one element is filtered.</summary>
/// <param name="filterPredicate" type="Function">
/// Same as in filter.
/// </param>
/// <param name="onResult" type="Function">
/// The callback function to handle the result, same as in toArray.
/// </param>
/// <returns type="$data.Promise" />
/// <example>
/// Result is true when all person are married.
/// Persons.every( function( person ) { return person.Married == true; }, null, {
/// success: function ( result ){ ... },
/// error: function () { ... }
/// });
/// </example>
/// </signature>
this._checkOperation('every');
var q = this;
if (filterPredicate) {
q = this.filter(filterPredicate, thisArg);
}
q = q.take(1);
var pHandler = new _index2.default.PromiseHandler();
var cbWrapper = pHandler.createCallback(onResult);
var everyExpression = _index.Container.createEveryExpression(q.expression);
var preparator = _index.Container.createQueryExpressionCreator(q.entityContext);
try {
var expression = preparator.Visit(everyExpression);
this.entityContext.log({ event: "EntityExpression", data: expression });
q.entityContext.executeQuery(_index.Container.createQueryable(q, expression), cbWrapper, transaction);
} catch (e) {
cbWrapper.error(e);
}
return pHandler.getPromise();
},
take: function take(amount) {
/// <summary>Returns only a specified number of elements from the start of the result set.</summary>
/// <param name="amount" type="$data.Integer">The number of elements to return.</param>
/// <returns type="$data.Queryable" />
/// <signature>
/// <summary>Returns only a specified number of elements from the start of the result set.</summary>
/// <param name="amount" type="$data.Integer">
/// The number of elements to skip.
/// </param>
/// <returns type="$data.Queryable" />
/// <example>
/// Log the full name of each Person.
/// Persons.take(10).forEach( function( person ) { console.log(person.FullName; } );
/// </example>
/// </signature>
this._checkOperation('take');
var constExp = _index.Container.createConstantExpression(amount, "integer");
var takeExp = _index.Container.createPagingExpression(this.expression, constExp, _index2.default.Expressions.ExpressionType.Take);
return _index.Container.createQueryable(this, takeExp);
},
skip: function skip(amount) {
/// <summary>Skip a specified number of elements from the start of the result set.</summary>
/// <param name="amount" type="$data.Integer">The number of elements to skip.</param>
/// <returns type="$data.Queryable" />
/// <signature>
/// <summary>Skip a specified number of elements from the start of the result set.</summary>
/// <param name="amount" type="$data.Integer">
/// The number of elements to skip.
/// </param>
/// <returns type="$data.Queryable" />
/// <example>
/// Log the full name of each Person.
/// Persons.skip(1).take(5).forEach( function( person ) { console.log(person.FullName; } );
/// </example>
/// </signature>
this._checkOperation('skip');
var constExp = _index.Container.createConstantExpression(amount, "integer");
var takeExp = _index.Container.createPagingExpression(this.expression, constExp, _index2.default.Expressions.ExpressionType.Skip);
return _index.Container.createQueryable(this, takeExp);
},
order: function order(selector) {
if (selector === '' || selector === undefined || selector === null) {
return this;
}
if (selector[0] === "-") {
var orderString = "it." + selector.replace("-", "");
return this.orderByDescending(orderString);
} else {
return this.orderBy("it." + selector);
}
},
orderBy: function orderBy(selector, thisArg) {
///<summary>Order a set of entities using an expression.</summary>
///<param name="selector" type="Function">An order expression</param>
///<param name="thisArg" type="Object">The query parameters</param>
///<returns type="$data.Queryable" />
///<signature>
///<summary>Order a set of entities using an expression.</summary>
///<param name="selector" type="string">
///The expression body of the order function in string.
///To reference the lambda parameter use the 'it' context variable.
///Example: orderBy("it.Id")
///</param>
///<param name="thisArg" type="Object" />
///<returns type="$data.Queryable" />
///</signature>
///<signature>
///<summary>Order a set of entities using an expression.</summary>
///<param name="selector" type="Function">
///</param>
///<param name="thisArg" type="Object" optional="true">
///Contains the predicate parameters
///</param>
///<returns type="$data.Queryable" />
///<example>
///Ordering a set of entities with a predicate function
///var males = Persons.orderBy( function( person ) { return person.Id; } );
///</example>
///</signature>
this._checkOperation('orderBy');
var codeExpression = _index.Container.createCodeExpression(selector, thisArg);
var exp = _index.Container.createOrderExpression(this.expression, codeExpression, _index2.default.Expressions.ExpressionType.OrderBy);
var q = _index.Container.createQueryable(this, exp);
return q;
},
orderByDescending: function orderByDescending(selector, thisArg) {
///<summary>Order a set of entities descending using an expression.</summary>
///<param name="selector" type="Function">An order expression</param>
///<param name="thisArg" type="Object">The query parameters</param>
///<returns type="$data.Queryable" />
///<signature>
///<summary>Order a set of entities descending using an expression.</summary>
///<param name="selector" type="string">
///The expression body of the order function in string.
///To reference the lambda parameter use the 'it' context variable.
///Example: orderBy("it.Id")
///</param>
///<param name="thisArg" type="Object" />
///<returns type="$data.Queryable" />
///</signature>
///<signature>
///<summary>Order a set of entities descending using an expression.</summary>
///<param name="selector" type="Function">
///</param>
///<param name="thisArg" type="Object" optional="true">
///Contains the predicate parameters
///</param>
///<returns type="$data.Queryable" />
///<example>
///Ordering a set of entities with a predicate function
///var males = Persons.orderByDescending( function( person ) { return person.Id; } );
///</example>
///</signature>
this._checkOperation('orderByDescending');
var codeExpression = _index.Container.createCodeExpression(selector, thisArg);
var exp = _index.Container.createOrderExpression(this.expression, codeExpression, _index2.default.Expressions.ExpressionType.OrderByDescending);
var q = _index.Container.createQueryable(this, exp);
return q;
},
first: function first(filterPredicate, thisArg, onResult, transaction) {
/// <summary>Filters a set of entities using a boolean expression and returns the first element.</summary>
/// <param name="onResult_items" type="Function">A callback function</param>
/// <returns type="$data.Promise" />
/// <signature>
/// <summary>Filters a set of entities using a boolean expression and returns the first element.</summary>
/// <param name="filterPredicate" type="string">
/// Same as in filter.
/// </param>
/// <param name="onResult" type="Function">
/// The callback function to handle the result, same as in toArray.
/// </param>
/// <returns type="$data.Promise" />
/// </signature>
/// <signature>
/// <summary>Filters a set of entities using a boolean expression and returns the first element.</summary>
/// <param name="filterPredicate" type="Function">
/// Same as in filter.
/// </param>
/// <param name="onResult" type="Function">
/// The callback function to handle the result, same as in toArray.
/// </param>
/// <returns type="$data.Promise" />
/// <example>
/// Get "George" from the Person entity set.
/// Persons.first( function( person ) { return person.FirstName == this.name; }, { name: "George" }, function ( result ){ ... });
/// </example>
/// </signature>
this._checkOperation('first');
var q = this;
if (filterPredicate) {
q = this.filter(filterPredicate, thisArg);
}
q = q.take(1);
var pHandler = new _index2.default.PromiseHandler();
var cbWrapper = pHandler.createCallback(onResult);
var firstExpression = _index.Container.createFirstExpression(q.expression);
var preparator = _index.Container.createQueryExpressionCreator(q.entityContext);
try {
var expression = preparator.Visit(firstExpression);
q.entityContext.log({ event: "EntityExpression", data: expression });
q.entityContext.executeQuery(_index.Container.createQueryable(q, expression), cbWrapper, transaction);
} catch (e) {
cbWrapper.error(e);
}
return pHandler.getPromise();
},
find: function find(keyValue, onResult, transaction) {
var pHandler = new _index2.default.PromiseHandler();
var cbWrapper = pHandler.createCallback(onResult);
var keys = this.defaultType.memberDefinitions.getKeyProperties();
try {
if (keys.length === 1 && (typeof keyValue === 'undefined' ? 'undefined' : _typeof(keyValue)) !== 'object') {
var keyV = {};
keyV[keys[0].name] = keyValue;
keyValue = keyV;
}
if ((typeof keyValue === 'undefined' ? 'undefined' : _typeof(keyValue)) !== 'object') {
throw new _index.Exception('Key parameter is invalid');
} else {
var parameters = [];
for (var i = 0; i < keys.length; i++) {
var keyProp = keys[i];
if (!(keyProp.name in keyValue)) {
throw new _index.Exception('Key value missing');
}
parameters.push(_index.Container.createConstantExpression(keyValue[keyProp.name], keyProp.type, keyProp.name));
}
var operation = this.entityContext.storageProvider.supportedSetOperations['find'];
if (operation) {
var findExpression = _index.Container.createFindExpression(this.expression, parameters);
var preparator = _index.Container.createQueryExpressionCreator(this.entityContext);
try {
var expression = preparator.Visit(findExpression);
this.entityContext.log({ event: "EntityExpression", data: expression });
this.entityContext.executeQuery(_index.Container.createQueryable(this, expression), cbWrapper, transaction);
} catch (e) {
cbWrapper.error(e);
}
} else {
var predicate = '';
var params = {};
for (var i = 0; i < parameters.length; i++) {
var param = parameters[i];
params[param.name] = param.value;
if (i > 0) predicate += ' && ';
predicate += "it." + param.name + " == this." + param.name;
}
this.single(predicate, params, cbWrapper, transaction);
}
}
} catch (e) {
cbWrapper.error(e);
}
return pHandler.getPromise();
},
include: function include(selector) {
/// <summary>Includes the given entity set in the query if it's an inverse property.</summary>
/// <param name="selector" type="$data.String">Entity set name</param>
/// <returns type="$data.Queryable" />
/// <signature>
/// <summary>Includes the given entity set in the query if it's an inverse property.</summary>
/// <param name="selector" type="$data.String">
/// The name of the entity set you want to include in the query.
/// </param>
/// <returns type="$data.Queryable" />
/// <example>
/// Include the Category on every Article.
/// Articles.include("Category");
/// </example>
/// </signature>
if (this.entityContext && this.entityContext.storageProvider && this.entityContext.storageProvider.name === "oData") {
return this.include2.apply(this, arguments);
}
this._checkOperation('include');
var constExp = _index.Container.createConstantExpression(selector, "string");
var takeExp = _index.Container.createIncludeExpression(this.expression, constExp);
return _index.Container.createQueryable(this, takeExp);
},
include2: function include2(selector, thisArg) {
/// <summary>Includes the given entity set in the query if it's an inverse property.</summary>
/// <param name="selector" type="$data.String">Entity set name</param>
/// <returns type="$data.Queryable" />
/// <signature>
/// <summary>Includes the given entity set in the query if it's an inverse property.</summary>
/// <param name="selector" type="$data.String">
/// The name of the entity set you want to include in the query.
/// </param>
/// <returns type="$data.Queryable" />
/// <example>
/// Include the Category on every Article.
/// Articles.include("Category");
/// </example>
/// </signature>
this._checkOperation('include');
if (typeof selector === 'string' && (selector.length < 3 || selector.substr(0, 3) !== 'it.') && !/^[^\.]*(=>)/.test(selector)) {
selector = 'it.' + selector;
}
var expression = _index.Container.createCodeExpression(selector, thisArg);
var includeExp = _index.Container.createIncludeExpression(this.expression, expression);
return _index.Container.createQueryable(this, includeExp);
},
withInlineCount: function withInlineCount(selector) {
this._checkOperation('withInlineCount');
var constExp = _index.Container.createConstantExpression(selector || 'allpages', "string");
var inlineCountExp = _index.Container.createInlineCountExpression(this.expression, constExp);
return _index.Container.createQueryable(this, inlineCountExp);
},
withCount: function withCount(selector) {
return this.withInlineCount(selector);
},
removeAll: function removeAll(onResult, transaction) {
/// <summary>Delete the query result and returns the number of deleted entities in a query as the callback parameter.</summary>
/// <param name="onResult" type="Function">A callback function</param>
/// <returns type="$data.Promise" />
/// <signature>
/// <summary>Delete the query result and returns the number of deleted entities in a query as the callback parameter.</summary>
/// <param name="onResult" type="Function">
/// The callback function to handle the result.
/// </param>
/// <returns type="$data.Promise" />
/// </signature>
/// <signature>
/// <summary>Delete the query result and returns the number of deleted entities in a query as the callback parameter.</summary>
/// <param name="onResult" type="Object">
/// Object of callback functions to handle success and error.
/// Example: { success: function(result) { ... }, error: function() { alert("Something went wrong..."); } }
/// </param>
/// <returns type="$data.Promise" />
/// <example>
/// Delete all People who are younger than 18 years old.
/// Persons.filter( function( p ){ return p.Age < 18; } ).removeAll( function( result ) { console.dir(result); } );
/// </example>
/// </signature>
this._checkOperation('batchDelete');
var pHandler = new _index2.default.PromiseHandler();
var cbWrapper = pHandler.createCallback(onResult);
var batchDeleteExpression = _index.Container.createBatchDeleteExpression(this.expression);
var preparator = _index.Container.createQueryExpressionCreator(this.entityContext);
try {
var expression = preparator.Visit(batchDeleteExpression);
this.entityContext.log({ event: "EntityExpression", data: expression });
this.entityContext.executeQuery(_index.Container.createQueryable(this, expression), cbWrapper, transaction);
} catch (e) {
cbWrapper.error(e);
}
return pHandler.getPromise();
},
_runQuery: function _runQuery(onResult_items, transaction) {
var pHandler = new _index2.default.PromiseHandler();
var cbWrapper = pHandler.createCallback(onResult_items);
var preparator = _index.Container.createQueryExpressionCreator(this.entityContext);
try {
var expression = preparator.Visit(this.expression);
this.entityContext.log({ event: "EntityExpression", data: expression });
this.entityContext.executeQuery(_index.Container.createQueryable(this, expression), cbWrapper, transaction);
} catch (e) {
cbWrapper.error(e);
}
return pHandler.getPromise();
},
toTraceString: function toTraceString(name) {
/// <summary>Returns the trace string of the query.</summary>
/// <param name="name" type="$data.String">Name of the execution method (toArray, length, etc.).</param>
/// <returns type="$data.String" />
/// <signature>
/// <summary>Returns the trace string of the query.</summary>
/// <param name="name" type="$data.String">
/// Name of the execution method (toArray, length, etc.). Optional. Default value is "toArray".
/// </param>
/// <returns type="$data.String" />
/// <example>
/// Get the trace string for Articles.toArray()
/// Articles.toTraceString();
/// </example>
/// </signature>
var expression = this.expression;
if (name) {
expression = _index.Container['create' + name + 'Expression'](expression);
} else {
expression = _index.Container.createToArrayExpression(expression);
}
var preparator = _index.Container.createQueryExpressionCreator(this.entityContext);
expression = preparator.Visit(expression);
//this.expression = expression;
var q = _index.Container.createQueryable(this, expression);
return q.entityContext.getTraceString(q);
},
_checkOperation: function _checkOperation(name) {
var operation = this.entityContext.resolveSetOperations(name);
if (operation.invokable != undefined && !operation.invokable) _index.Guard.raise(new _index.Exception("Operation '" + name + "' is not invokable with the provider"));
},
defaultType: {}
}, null);
exports.default = _index2.default;
module.exports = exports['default'];