typeorm-extension
Advanced tools
Comparing version 0.0.6 to 0.0.7
import { SelectQueryBuilder } from "typeorm"; | ||
export declare function applyRequestFields(query: SelectQueryBuilder<any>, alias: string, fields: unknown, allowedFields: Record<string, string[]> | string[]): SelectQueryBuilder<any>; | ||
import { StringCaseOption } from "./utils"; | ||
export declare type RequestFieldOptions = { | ||
changeRequestFieldCase?: StringCaseOption | undefined; | ||
requestDefaultKey: string; | ||
aliasMapping: Record<string, string>; | ||
}; | ||
/** | ||
* Apply fields for specific entity. | ||
* aliasMapping is a mapping from request domain/entity key to query domain/entity key. | ||
* | ||
* @param query | ||
* @param requestFields | ||
* @param allowedFields | ||
* @param partialOptions | ||
*/ | ||
export declare function applyRequestFields(query: SelectQueryBuilder<any>, requestFields: unknown, allowedFields: Record<string, string[]> | string[], partialOptions?: Partial<RequestFieldOptions>): SelectQueryBuilder<any>; | ||
/** | ||
* Transform allowed fields in array or object representation to object representation. | ||
* | ||
* {field1: 'field1', ...} => {field1: 'field1', ...} | ||
* ['field1', 'field2'] => {field1: 'field1', field2: 'field2'} | ||
* | ||
* @param rawFields | ||
*/ | ||
export declare function transformAllowedFields(rawFields: string[] | Record<string, string>): Record<string, string>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.applyRequestFields = void 0; | ||
var change_case_1 = require("change-case"); | ||
function transformRequestFields(raw, allowedFilters) { | ||
exports.transformAllowedFields = exports.applyRequestFields = void 0; | ||
var utils_1 = require("../utils"); | ||
var utils_2 = require("./utils"); | ||
function transformRequestFields(raw, allowedFields, options) { | ||
var prototype = Object.prototype.toString.call(raw); | ||
@@ -22,9 +23,11 @@ if (prototype !== '[object Object]') { | ||
var fields = domainPrototype === '[object String]' ? domains[key].split(',') : domains[key]; | ||
if (!allowedFilters.hasOwnProperty(key)) { | ||
return "continue"; | ||
var allowedFieldsKey = utils_1.hasOwnProperty(options.aliasMapping, key) ? | ||
options.aliasMapping[key] : | ||
options.requestDefaultKey; | ||
fields = fields | ||
.map(function (field) { return utils_2.changeStringCase(field, options.changeRequestFieldCase); }) | ||
.filter(function (field) { return allowedFields[allowedFieldsKey].includes(field); }); | ||
if (fields.length > 0) { | ||
result[allowedFieldsKey] = fields; | ||
} | ||
fields = fields | ||
.map(function (field) { return change_case_1.snakeCase(field); }) | ||
.filter(function (x) { return allowedFilters[key].includes(x); }); | ||
result[key] = fields; | ||
}; | ||
@@ -36,15 +39,31 @@ for (var key in domains) { | ||
} | ||
function applyRequestFields(query, alias, fields, allowedFields) { | ||
var allowed = {}; | ||
if (Array.isArray(allowedFields)) { | ||
allowed[alias] = allowedFields.map(function (allowedField) { return change_case_1.snakeCase(allowedField); }); | ||
function transformAllowedDomainFields(data, options) { | ||
var allowedFields = {}; | ||
if (Array.isArray(data)) { | ||
allowedFields[options.requestDefaultKey] = data; | ||
} | ||
else { | ||
for (var key in allowedFields) { | ||
if (!allowedFields.hasOwnProperty(key)) | ||
continue; | ||
allowed[key] = allowedFields[key].map(function (allowedField) { return change_case_1.snakeCase(allowedField); }); | ||
} | ||
allowedFields = data; | ||
} | ||
var domains = transformRequestFields(fields, allowed); | ||
return allowedFields; | ||
} | ||
/** | ||
* Apply fields for specific entity. | ||
* aliasMapping is a mapping from request domain/entity key to query domain/entity key. | ||
* | ||
* @param query | ||
* @param requestFields | ||
* @param allowedFields | ||
* @param partialOptions | ||
*/ | ||
function applyRequestFields(query, requestFields, allowedFields, partialOptions) { | ||
var _a, _b; | ||
partialOptions = partialOptions !== null && partialOptions !== void 0 ? partialOptions : {}; | ||
var options = { | ||
changeRequestFieldCase: partialOptions.changeRequestFieldCase, | ||
requestDefaultKey: (_a = partialOptions.requestDefaultKey) !== null && _a !== void 0 ? _a : '__DEFAULT__', | ||
aliasMapping: (_b = partialOptions.aliasMapping) !== null && _b !== void 0 ? _b : {} | ||
}; | ||
allowedFields = transformAllowedDomainFields(allowedFields, options); | ||
var domains = transformRequestFields(requestFields, allowedFields, options); | ||
for (var key in domains) { | ||
@@ -54,3 +73,3 @@ if (!domains.hasOwnProperty(key)) | ||
for (var i = 0; i < domains[key].length; i++) { | ||
query.addSelect(key + '.' + domains[key][i]); | ||
query.addSelect((key === options.requestDefaultKey ? '' : key + '.') + domains[key][i]); | ||
} | ||
@@ -61,2 +80,27 @@ } | ||
exports.applyRequestFields = applyRequestFields; | ||
/** | ||
* Transform allowed fields in array or object representation to object representation. | ||
* | ||
* {field1: 'field1', ...} => {field1: 'field1', ...} | ||
* ['field1', 'field2'] => {field1: 'field1', field2: 'field2'} | ||
* | ||
* @param rawFields | ||
*/ | ||
function transformAllowedFields(rawFields) { | ||
var fields = {}; | ||
var allowedFiltersPrototype = Object.prototype.toString.call(rawFields); | ||
switch (allowedFiltersPrototype) { | ||
case '[object Array]': | ||
var tempStrArr = rawFields; | ||
for (var i = 0; i < tempStrArr.length; i++) { | ||
fields[tempStrArr[i]] = tempStrArr[i]; | ||
} | ||
break; | ||
case '[object Object]': | ||
fields = rawFields; | ||
break; | ||
} | ||
return fields; | ||
} | ||
exports.transformAllowedFields = transformAllowedFields; | ||
//# sourceMappingURL=fields.js.map |
import { SelectQueryBuilder } from "typeorm"; | ||
export declare function transformAllowedFilters(rawFields: string[] | Record<string, any>): Record<string, string>; | ||
export declare function applyRequestFilter(query: SelectQueryBuilder<any>, rawRequestFilters: unknown, rawAllowedFilters: string[] | Record<string, any>): SelectQueryBuilder<any>; | ||
import { StringCaseOption } from "./utils"; | ||
export declare type RequestFilterOptions = { | ||
changeRequestFieldCase?: StringCaseOption | undefined; | ||
}; | ||
export declare function applyRequestFilter(query: SelectQueryBuilder<any>, requestFilters: unknown, rawAllowedFilters: string[] | Record<string, any>, partialOptions?: Partial<RequestFilterOptions>): SelectQueryBuilder<any>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.applyRequestFilter = exports.transformAllowedFilters = void 0; | ||
exports.applyRequestFilter = void 0; | ||
var typeorm_1 = require("typeorm"); | ||
var change_case_1 = require("change-case"); | ||
function transformRequestFilters(rawFilters, allowedFilters) { | ||
var utils_1 = require("./utils"); | ||
var fields_1 = require("./fields"); | ||
function transformRequestFilters(rawFilters, allowedFilters, options) { | ||
var filters = {}; | ||
@@ -25,38 +26,18 @@ var prototype = Object.prototype.toString.call(rawFilters); | ||
} | ||
var newKey = change_case_1.snakeCase(key); | ||
if (!allowedFilters.hasOwnProperty(newKey)) { | ||
var allowedKey = utils_1.changeStringCase(key, options.changeRequestFieldCase); | ||
if (!allowedFilters.hasOwnProperty(allowedKey)) { | ||
continue; | ||
} | ||
result[newKey] = filters[key]; | ||
result[allowedFilters[allowedKey]] = filters[key]; | ||
} | ||
return result; | ||
} | ||
function transformAllowedFilters(rawFields) { | ||
var fields = {}; | ||
var allowedFiltersPrototype = Object.prototype.toString.call(rawFields); | ||
switch (allowedFiltersPrototype) { | ||
case '[object Array]': | ||
var tempStrArr = rawFields; | ||
for (var i = 0; i < tempStrArr.length; i++) { | ||
var newKey = change_case_1.snakeCase(tempStrArr[i]); | ||
fields[newKey] = newKey; | ||
} | ||
break; | ||
case '[object Object]': | ||
rawFields = rawFields; | ||
for (var key in rawFields) { | ||
if (!rawFields.hasOwnProperty(key)) | ||
continue; | ||
var newKey = change_case_1.snakeCase(key); | ||
fields[newKey] = rawFields[key]; | ||
} | ||
break; | ||
} | ||
return fields; | ||
} | ||
exports.transformAllowedFilters = transformAllowedFilters; | ||
function applyRequestFilter(query, rawRequestFilters, rawAllowedFilters) { | ||
var allowedFilters = transformAllowedFilters(rawAllowedFilters); | ||
var requestFilters = transformRequestFilters(rawRequestFilters, allowedFilters); | ||
var requestedFilterLength = Object.keys(requestFilters).length; | ||
function applyRequestFilter(query, requestFilters, rawAllowedFilters, partialOptions) { | ||
partialOptions = partialOptions !== null && partialOptions !== void 0 ? partialOptions : {}; | ||
var options = { | ||
changeRequestFieldCase: partialOptions.changeRequestFieldCase | ||
}; | ||
var allowedFilters = fields_1.transformAllowedFields(rawAllowedFilters); | ||
var filters = transformRequestFilters(requestFilters, allowedFilters, options); | ||
var requestedFilterLength = Object.keys(filters).length; | ||
if (requestedFilterLength === 0) { | ||
@@ -68,8 +49,8 @@ return query; | ||
var run = 0; | ||
for (var key in requestFilters) { | ||
if (!requestFilters.hasOwnProperty(key) || !allowedFilters.hasOwnProperty(key)) { | ||
for (var key in filters) { | ||
if (!filters.hasOwnProperty(key) || !allowedFilters.hasOwnProperty(key)) { | ||
continue; | ||
} | ||
run++; | ||
var value = requestFilters[key]; | ||
var value = filters[key]; | ||
var paramKey = 'filter-' + allowedFilters[key] + '-' + run; | ||
@@ -76,0 +57,0 @@ var whereKind = run === 1 ? 'where' : 'andWhere'; |
import { SelectQueryBuilder } from "typeorm"; | ||
export declare function applyRequestIncludes(query: SelectQueryBuilder<any>, alias: string, include: unknown, allowedIncludes: string[] | Record<string, any>): SelectQueryBuilder<any>; | ||
import { StringCaseOption } from "./utils"; | ||
export declare type RequestIncludeOptions = { | ||
changeRequestFieldCase?: StringCaseOption | undefined; | ||
}; | ||
export declare function applyRequestIncludes(query: SelectQueryBuilder<any>, queryAlias: string, include: unknown, allowedIncludes: string[] | Record<string, any>, partialOptions?: Partial<RequestIncludeOptions>): SelectQueryBuilder<any>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.applyRequestIncludes = void 0; | ||
var filter_1 = require("./filter"); | ||
function transformRequestFields(raw, allowed) { | ||
var fields_1 = require("./fields"); | ||
var utils_1 = require("./utils"); | ||
function transformRequestIncludes(raw, allowed, options) { | ||
var fields = []; | ||
var prototype = Object.prototype.toString.call(raw); | ||
if (prototype !== '[object Array]' && prototype !== '[object String]') { | ||
return {}; | ||
return []; | ||
} | ||
@@ -15,5 +16,5 @@ if (prototype === '[object String]') { | ||
if (prototype === '[object Array]') { | ||
fields = raw; | ||
fields = raw.filter(function (el) { return typeof el === 'string'; }); | ||
} | ||
var result = {}; | ||
var result = []; | ||
for (var i = 0; i < fields.length; i++) { | ||
@@ -23,8 +24,8 @@ if (typeof fields[i] !== 'string') { | ||
} | ||
var stripped = fields[i].trim(); | ||
if (stripped.length === 0 || !allowed.hasOwnProperty(stripped)) { | ||
var allowedKey = utils_1.changeStringCase(fields[i].trim(), options.changeRequestFieldCase); | ||
if (allowedKey.length === 0 || !allowed.hasOwnProperty(allowedKey)) { | ||
delete fields[i]; | ||
} | ||
else { | ||
result[stripped] = allowed[stripped]; | ||
result.push(allowed[allowedKey]); | ||
} | ||
@@ -34,9 +35,11 @@ } | ||
} | ||
function applyRequestIncludes(query, alias, include, allowedIncludes) { | ||
var allowedFields = filter_1.transformAllowedFilters(allowedIncludes); | ||
var requestIncludes = transformRequestFields(include, allowedFields); | ||
for (var key in requestIncludes) { | ||
if (!requestIncludes.hasOwnProperty(key)) | ||
continue; | ||
query.leftJoinAndSelect(alias + '.' + requestIncludes[key], requestIncludes[key]); | ||
function applyRequestIncludes(query, queryAlias, include, allowedIncludes, partialOptions) { | ||
partialOptions = partialOptions !== null && partialOptions !== void 0 ? partialOptions : {}; | ||
var options = { | ||
changeRequestFieldCase: partialOptions.changeRequestFieldCase | ||
}; | ||
var allowedFields = fields_1.transformAllowedFields(allowedIncludes); | ||
var requestIncludes = transformRequestIncludes(include, allowedFields, options); | ||
for (var i = 0; i < requestIncludes.length; i++) { | ||
query.leftJoinAndSelect(queryAlias + '.' + requestIncludes[i], requestIncludes[i]); | ||
} | ||
@@ -43,0 +46,0 @@ return query; |
{ | ||
"name": "typeorm-extension", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "author": { |
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
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
171414
100
2872