Comparing version 0.0.8-alpha.3 to 0.0.9-alpha.1
@@ -6,2 +6,4 @@ import { ApiEdgeDefinition } from "./edge/ApiEdgeDefinition"; | ||
export declare class Api { | ||
static defaultIdPostfix: string; | ||
static defaultIdField: string; | ||
version: string; | ||
@@ -8,0 +10,0 @@ edges: ApiEdgeDefinition[]; |
@@ -33,2 +33,4 @@ "use strict"; | ||
}; | ||
Api.defaultIdPostfix = "Id"; | ||
Api.defaultIdField = "id"; | ||
return Api; | ||
@@ -35,0 +37,0 @@ }()); |
@@ -7,2 +7,3 @@ import { ApiEdgeRelation } from "../relations/ApiEdgeRelation"; | ||
pluralName: string; | ||
idField: string; | ||
methods: Object; | ||
@@ -14,2 +15,3 @@ relations: ApiEdgeRelation[]; | ||
updateEntry: (context: ApiEdgeQueryContext, entryFields: any) => Promise<ApiEdgeQueryResponse>; | ||
patchEntry: (context: ApiEdgeQueryContext, entryFields: any) => Promise<ApiEdgeQueryResponse>; | ||
updateEntries: (context: ApiEdgeQueryContext, entryFields: any) => Promise<ApiEdgeQueryResponse>; | ||
@@ -16,0 +18,0 @@ removeEntry: (context: ApiEdgeQueryContext, entryFields: any) => Promise<ApiEdgeQueryResponse>; |
@@ -22,2 +22,4 @@ "use strict"; | ||
return _this.edge.updateEntry(_this.context, _this.body); | ||
case ApiEdgeQueryType_1.ApiEdgeQueryType.Patch: | ||
return _this.edge.patchEntry(_this.context, _this.body); | ||
case ApiEdgeQueryType_1.ApiEdgeQueryType.List: | ||
@@ -24,0 +26,0 @@ return _this.edge.listEntries(_this.context); |
@@ -10,2 +10,3 @@ import { ApiEdgeQueryFilter, ApiEdgeQueryFilterType } from "./ApiEdgeQueryFilter"; | ||
}; | ||
sortBy: any[]; | ||
filters: ApiEdgeQueryFilter[]; | ||
@@ -15,2 +16,3 @@ clone: () => ApiEdgeQueryContext; | ||
paginate: (skip: number, limit: number) => this; | ||
sort: (fieldName: string, ascending?: boolean) => this; | ||
populate(field: string): this; | ||
@@ -17,0 +19,0 @@ field(field: string): this; |
@@ -10,2 +10,3 @@ "use strict"; | ||
this.populatedFields = []; | ||
this.sortBy = []; | ||
this.filters = []; | ||
@@ -18,2 +19,3 @@ this.clone = function () { | ||
_this.filters.forEach(function (f) { return temp.filters.push(f.clone()); }); | ||
_this.sortBy.forEach(function (f) { return temp.sortBy.push([f[0], f[1]]); }); | ||
if (_this.pagination) { | ||
@@ -33,2 +35,7 @@ temp.pagination = { | ||
}; | ||
this.sort = function (fieldName, ascending) { | ||
if (ascending === void 0) { ascending = true; } | ||
_this.sortBy.push([fieldName, (ascending ? 1 : -1)]); | ||
return _this; | ||
}; | ||
this.id = id; | ||
@@ -35,0 +42,0 @@ this.fields = fields; |
@@ -6,5 +6,6 @@ export declare enum ApiEdgeQueryType { | ||
Update = 3, | ||
Delete = 4, | ||
Exists = 5, | ||
Call = 6, | ||
Patch = 4, | ||
Delete = 5, | ||
Exists = 6, | ||
Call = 7, | ||
} |
@@ -7,7 +7,8 @@ "use strict"; | ||
ApiEdgeQueryType[ApiEdgeQueryType["Update"] = 3] = "Update"; | ||
ApiEdgeQueryType[ApiEdgeQueryType["Delete"] = 4] = "Delete"; | ||
ApiEdgeQueryType[ApiEdgeQueryType["Exists"] = 5] = "Exists"; | ||
ApiEdgeQueryType[ApiEdgeQueryType["Call"] = 6] = "Call"; | ||
ApiEdgeQueryType[ApiEdgeQueryType["Patch"] = 4] = "Patch"; | ||
ApiEdgeQueryType[ApiEdgeQueryType["Delete"] = 5] = "Delete"; | ||
ApiEdgeQueryType[ApiEdgeQueryType["Exists"] = 6] = "Exists"; | ||
ApiEdgeQueryType[ApiEdgeQueryType["Call"] = 7] = "Call"; | ||
})(exports.ApiEdgeQueryType || (exports.ApiEdgeQueryType = {})); | ||
var ApiEdgeQueryType = exports.ApiEdgeQueryType; | ||
//# sourceMappingURL=ApiEdgeQueryType.js.map |
@@ -11,2 +11,3 @@ "use strict"; | ||
var OneToOneRelation_1 = require("../relations/OneToOneRelation"); | ||
var Api_1 = require("../Api"); | ||
var QueryEdgeQueryStep = (function () { | ||
@@ -38,3 +39,3 @@ function QueryEdgeQueryStep(query) { | ||
return reject(new ApiEdgeError_1.ApiEdgeError(404, "Missing Related Entry")); | ||
scope.context.filter(_this.relation.relationId, ApiEdgeQueryFilter_1.ApiEdgeQueryFilterType.Equals, scope.response.data.id); | ||
scope.context.filter(_this.relation.relationId, ApiEdgeQueryFilter_1.ApiEdgeQueryFilterType.Equals, scope.response.data[_this.relation.from.idField || Api_1.Api.defaultIdField]); | ||
resolve(scope); | ||
@@ -106,3 +107,3 @@ }); | ||
var _this = this; | ||
if (fieldName === void 0) { fieldName = "id"; } | ||
if (fieldName === void 0) { fieldName = Api_1.Api.defaultIdField; } | ||
this.execute = function (scope) { | ||
@@ -133,2 +134,3 @@ return new Promise(function (resolve, reject) { | ||
_this.context.filters.forEach(function (f) { return scope.context.filters.push(f); }); | ||
_this.context.sortBy.forEach(function (f) { return scope.context.sortBy.push(f); }); | ||
resolve(scope); | ||
@@ -209,3 +211,12 @@ }); | ||
if (lastSegment instanceof ApiRequest_1.RelatedFieldPathSegment) { | ||
throw new ApiEdgeError_1.ApiEdgeError(500, "Not Implemented"); | ||
if (request.type === ApiRequest_1.ApiRequestType.Update) { | ||
baseQuery = new ApiEdgeQuery_1.ApiEdgeQuery(lastSegment.edge, ApiEdgeQueryType_1.ApiEdgeQueryType.Patch); | ||
request.body = (_a = {}, _a[lastSegment.relation.relationId] = request.body.id || request.body._id, _a); | ||
} | ||
else if (request.type === ApiRequest_1.ApiRequestType.Patch) { | ||
baseQuery = new ApiEdgeQuery_1.ApiEdgeQuery(lastSegment.relation.to, ApiEdgeQueryType_1.ApiEdgeQueryType.Patch); | ||
} | ||
else { | ||
throw new ApiEdgeError_1.ApiEdgeError(400, "Invalid Delete Query"); | ||
} | ||
} | ||
@@ -216,2 +227,5 @@ else { | ||
} | ||
else if (request.type === ApiRequest_1.ApiRequestType.Patch) { | ||
baseQuery = new ApiEdgeQuery_1.ApiEdgeQuery(lastSegment.edge, ApiEdgeQueryType_1.ApiEdgeQueryType.Patch); | ||
} | ||
else { | ||
@@ -225,2 +239,3 @@ baseQuery = new ApiEdgeQuery_1.ApiEdgeQuery(lastSegment.edge, ApiEdgeQueryType_1.ApiEdgeQueryType.Delete); | ||
query.unshift(new ExtendContextQueryStep(request.context)); | ||
var readMode = true; | ||
if (lastSegment instanceof ApiRequest_1.EntryPathSegment) { | ||
@@ -230,7 +245,13 @@ query.unshift(new ExtendContextQueryStep(new ApiEdgeQueryContext_1.ApiEdgeQueryContext(lastSegment.id))); | ||
else if (lastSegment instanceof ApiRequest_1.RelatedFieldPathSegment) { | ||
query.unshift(new ProvideIdQueryStep(lastSegment.relation.relationId)); | ||
if (request.type === ApiRequest_1.ApiRequestType.Update) { | ||
var previousSegment = segments[segments.length - 2]; | ||
query.unshift(new ProvideIdQueryStep(previousSegment.edge.idField || Api_1.Api.defaultIdField)); | ||
readMode = false; | ||
} | ||
else { | ||
query.unshift(new ProvideIdQueryStep(lastSegment.relation.relationId)); | ||
} | ||
} | ||
else { | ||
} | ||
var readMode = true; | ||
for (var i = segments.length - 2; i >= 0; i--) { | ||
@@ -250,2 +271,3 @@ var currentSegment = segments[i]; | ||
return query; | ||
var _a; | ||
}; | ||
@@ -267,2 +289,3 @@ this.buildCreateQuery = function (request) { | ||
case ApiRequest_1.ApiRequestType.Update: | ||
case ApiRequest_1.ApiRequestType.Patch: | ||
case ApiRequest_1.ApiRequestType.Delete: | ||
@@ -293,3 +316,3 @@ return _this.buildChangeQuery(request); | ||
if (currentSegment instanceof ApiRequest_1.EntryPathSegment) { | ||
query.unshift(new SetResponseQueryStep(new ApiEdgeQueryResponse_1.ApiEdgeQueryResponse({ id: currentSegment.id }))); | ||
query.unshift(new SetResponseQueryStep(new ApiEdgeQueryResponse_1.ApiEdgeQueryResponse((_a = {}, _a[currentSegment.edge.idField || Api_1.Api.defaultIdField] = currentSegment.id, _a)))); | ||
return false; | ||
@@ -304,2 +327,3 @@ } | ||
return this.buildProvideIdStep(query, currentSegment); | ||
var _a; | ||
}; | ||
@@ -306,0 +330,0 @@ ApiQueryBuilder.prototype.buildReadStep = function (query, currentSegment) { |
"use strict"; | ||
var Api_1 = require("../Api"); | ||
var OneToManyRelation = (function () { | ||
@@ -8,4 +9,4 @@ function OneToManyRelation(from, to, options) { | ||
this.name = options.name || to.pluralName; | ||
this.relatedId = options.relatedId || to.name + "Id"; | ||
this.relationId = options.relationId || from.name + "Id"; | ||
this.relatedId = options.relatedId || to.name + Api_1.Api.defaultIdPostfix; | ||
this.relationId = options.relationId || from.name + Api_1.Api.defaultIdPostfix; | ||
} | ||
@@ -12,0 +13,0 @@ return OneToManyRelation; |
"use strict"; | ||
var Api_1 = require("../Api"); | ||
var OneToOneRelation = (function () { | ||
@@ -8,4 +9,4 @@ function OneToOneRelation(from, to, options) { | ||
this.name = options.name || to.name; | ||
this.relationId = options.relationId || to.name + "Id"; | ||
this.relatedId = options.relatedId || from.name + "Id"; | ||
this.relationId = options.relationId || to.name + Api_1.Api.defaultIdPostfix; | ||
this.relatedId = options.relatedId || from.name + Api_1.Api.defaultIdPostfix; | ||
} | ||
@@ -12,0 +13,0 @@ return OneToOneRelation; |
@@ -38,4 +38,5 @@ import { ApiEdgeDefinition } from "../edge/ApiEdgeDefinition"; | ||
Update = 2, | ||
Delete = 3, | ||
Exists = 4, | ||
Patch = 3, | ||
Delete = 4, | ||
Exists = 5, | ||
} | ||
@@ -42,0 +43,0 @@ export declare class ApiRequest { |
@@ -78,4 +78,5 @@ "use strict"; | ||
ApiRequestType[ApiRequestType["Update"] = 2] = "Update"; | ||
ApiRequestType[ApiRequestType["Delete"] = 3] = "Delete"; | ||
ApiRequestType[ApiRequestType["Exists"] = 4] = "Exists"; | ||
ApiRequestType[ApiRequestType["Patch"] = 3] = "Patch"; | ||
ApiRequestType[ApiRequestType["Delete"] = 4] = "Delete"; | ||
ApiRequestType[ApiRequestType["Exists"] = 5] = "Exists"; | ||
})(exports.ApiRequestType || (exports.ApiRequestType = {})); | ||
@@ -82,0 +83,0 @@ var ApiRequestType = exports.ApiRequestType; |
{ | ||
"name": "api-core", | ||
"version": "0.0.8-alpha.3", | ||
"version": "0.0.9-alpha.1", | ||
"description": "Core classes for building dynamic model based multi-level APIs for any provider.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -9,2 +9,4 @@ import {ApiEdgeDefinition} from "./edge/ApiEdgeDefinition"; | ||
export class Api { | ||
static defaultIdPostfix: string = "Id"; | ||
static defaultIdField: string = "id"; | ||
@@ -11,0 +13,0 @@ version: string; |
@@ -9,2 +9,3 @@ import {ApiEdgeRelation} from "../relations/ApiEdgeRelation"; | ||
pluralName: string; | ||
idField: string; | ||
@@ -18,2 +19,3 @@ methods: Object; | ||
updateEntry: (context: ApiEdgeQueryContext, entryFields: any) => Promise<ApiEdgeQueryResponse>; | ||
patchEntry: (context: ApiEdgeQueryContext, entryFields: any) => Promise<ApiEdgeQueryResponse>; | ||
updateEntries: (context: ApiEdgeQueryContext, entryFields: any) => Promise<ApiEdgeQueryResponse>; | ||
@@ -20,0 +22,0 @@ removeEntry: (context: ApiEdgeQueryContext, entryFields: any) => Promise<ApiEdgeQueryResponse>; |
@@ -59,2 +59,4 @@ import {ApiEdgeDefinition} from './ApiEdgeDefinition'; | ||
return this.edge.updateEntry(this.context, this.body); | ||
case ApiEdgeQueryType.Patch: | ||
return this.edge.patchEntry(this.context, this.body); | ||
case ApiEdgeQueryType.List: | ||
@@ -61,0 +63,0 @@ return this.edge.listEntries(this.context); |
@@ -11,2 +11,3 @@ import {ApiEdgeQueryFilter, ApiEdgeQueryFilterType} from "./ApiEdgeQueryFilter"; | ||
}; | ||
sortBy: any[] = []; | ||
filters: ApiEdgeQueryFilter[] = []; | ||
@@ -21,2 +22,3 @@ | ||
this.filters.forEach(f => temp.filters.push(f.clone())); | ||
this.sortBy.forEach(f => temp.sortBy.push([f[0], f[1]])); | ||
@@ -45,2 +47,7 @@ if(this.pagination) { | ||
sort = (fieldName: string, ascending: boolean = true) => { | ||
this.sortBy.push([fieldName, (ascending ? 1 : -1)]); | ||
return this | ||
}; | ||
populate(field: string) { | ||
@@ -47,0 +54,0 @@ this.populatedFields.push(field); |
@@ -27,2 +27,7 @@ /** | ||
/** | ||
* Edit an entity in the source model. | ||
*/ | ||
Patch, | ||
/** | ||
* Delete an entity from the source model. | ||
@@ -29,0 +34,0 @@ */ |
@@ -49,3 +49,3 @@ import {QueryStep, QueryScope, ApiQuery} from "./ApiQuery"; | ||
if(!scope.response) return reject(new ApiEdgeError(404, "Missing Related Entry")); | ||
scope.context.filter(this.relation.relationId, ApiEdgeQueryFilterType.Equals, scope.response.data.id); | ||
scope.context.filter(this.relation.relationId, ApiEdgeQueryFilterType.Equals, scope.response.data[this.relation.from.idField||Api.defaultIdField]); | ||
resolve(scope); | ||
@@ -123,3 +123,3 @@ }) | ||
constructor(fieldName: string = "id") { | ||
constructor(fieldName: string = Api.defaultIdField) { | ||
this.fieldName = fieldName; | ||
@@ -155,2 +155,3 @@ } | ||
this.context.filters.forEach(f => scope.context.filters.push(f)); | ||
this.context.sortBy.forEach(f => scope.context.sortBy.push(f)); | ||
resolve(scope) | ||
@@ -215,3 +216,3 @@ }) | ||
if(currentSegment instanceof EntryPathSegment) { | ||
query.unshift(new SetResponseQueryStep(new ApiEdgeQueryResponse({ id: currentSegment.id }))); | ||
query.unshift(new SetResponseQueryStep(new ApiEdgeQueryResponse({ [currentSegment.edge.idField||Api.defaultIdField]: currentSegment.id }))); | ||
return false | ||
@@ -311,4 +312,12 @@ } | ||
if(lastSegment instanceof RelatedFieldPathSegment) { | ||
throw new ApiEdgeError(500, "Not Implemented"); //TODO | ||
//baseQuery = new ApiEdgeQuery(lastSegment.relation.to, ApiEdgeQueryType.Get); | ||
if(request.type === ApiRequestType.Update) { | ||
baseQuery = new ApiEdgeQuery(lastSegment.edge, ApiEdgeQueryType.Patch); | ||
request.body = { [lastSegment.relation.relationId]: request.body.id||request.body._id }; | ||
} | ||
else if(request.type === ApiRequestType.Patch) { | ||
baseQuery = new ApiEdgeQuery(lastSegment.relation.to, ApiEdgeQueryType.Patch); | ||
} | ||
else { | ||
throw new ApiEdgeError(400, "Invalid Delete Query"); | ||
} | ||
} | ||
@@ -320,2 +329,5 @@ //TODO: Add support for method calls | ||
} | ||
else if(request.type === ApiRequestType.Patch) { | ||
baseQuery = new ApiEdgeQuery(lastSegment.edge, ApiEdgeQueryType.Patch); | ||
} | ||
else { | ||
@@ -332,2 +344,3 @@ baseQuery = new ApiEdgeQuery(lastSegment.edge, ApiEdgeQueryType.Delete); | ||
//STEP 3: Provide ID for the base query. | ||
let readMode = true; | ||
if(lastSegment instanceof EntryPathSegment) { | ||
@@ -337,3 +350,10 @@ query.unshift(new ExtendContextQueryStep(new ApiEdgeQueryContext(lastSegment.id))) | ||
else if(lastSegment instanceof RelatedFieldPathSegment) { | ||
query.unshift(new ProvideIdQueryStep(lastSegment.relation.relationId)) | ||
if(request.type === ApiRequestType.Update) { | ||
let previousSegment = segments[segments.length-2]; | ||
query.unshift(new ProvideIdQueryStep(previousSegment.edge.idField||Api.defaultIdField)); | ||
readMode = false; //Provide ID from the previous segment without querying the database. | ||
} | ||
else { | ||
query.unshift(new ProvideIdQueryStep(lastSegment.relation.relationId)) | ||
} | ||
} | ||
@@ -345,3 +365,2 @@ else { | ||
//STEP 4: Provide filters and validation for the base query. | ||
let readMode = true; | ||
for(let i = segments.length-2; i >= 0; i--) { | ||
@@ -396,2 +415,3 @@ let currentSegment = segments[i]; | ||
case ApiRequestType.Update: | ||
case ApiRequestType.Patch: | ||
case ApiRequestType.Delete: | ||
@@ -398,0 +418,0 @@ return this.buildChangeQuery(request); |
import {ApiEdgeDefinition} from "../edge/ApiEdgeDefinition"; | ||
import {ApiEdgeRelation} from "./ApiEdgeRelation"; | ||
import {Api} from "../Api"; | ||
@@ -18,5 +19,5 @@ export class OneToManyRelation implements ApiEdgeRelation { | ||
this.name = options.name || to.pluralName; | ||
this.relatedId = options.relatedId || to.name + "Id"; | ||
this.relationId = options.relationId || from.name + "Id"; | ||
this.relatedId = options.relatedId || to.name + Api.defaultIdPostfix; | ||
this.relationId = options.relationId || from.name + Api.defaultIdPostfix; | ||
} | ||
} |
import {ApiEdgeDefinition} from "../edge/ApiEdgeDefinition"; | ||
import {ApiEdgeRelation} from "./ApiEdgeRelation"; | ||
import {Api} from "../Api"; | ||
@@ -19,5 +20,5 @@ export class OneToOneRelation implements ApiEdgeRelation { | ||
this.name = options.name || to.name; | ||
this.relationId = options.relationId || to.name + "Id"; | ||
this.relatedId = options.relatedId || from.name + "Id"; | ||
this.relationId = options.relationId || to.name + Api.defaultIdPostfix; | ||
this.relatedId = options.relatedId || from.name + Api.defaultIdPostfix; | ||
} | ||
} |
@@ -78,2 +78,3 @@ import {ApiEdgeDefinition} from "../edge/ApiEdgeDefinition"; | ||
Update, | ||
Patch, | ||
Delete, | ||
@@ -80,0 +81,0 @@ Exists |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
318950
4552