@ruiapp/rapid-core
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -1,2 +0,5 @@ | ||
import { EntityFilterOptions } from "../types"; | ||
import { EntityFilterOptions, RpdDataModel, RpdDataModelIndexOptions } from "../types"; | ||
import { RowFilterOptions } from "../dataAccess/dataAccessTypes"; | ||
export declare function removeFiltersWithNullValue(filters?: EntityFilterOptions[]): EntityFilterOptions[]; | ||
export declare function convertModelIndexConditionsToRowFilterOptions(model: RpdDataModel, filters: RpdDataModelIndexOptions[]): RowFilterOptions[]; | ||
export declare function replaceModelIndexConditionEntityPropertyWithTableColumn(model: RpdDataModel, filter: RpdDataModelIndexOptions): RowFilterOptions; |
@@ -10,2 +10,4 @@ import { IRpdServer } from "../core/server"; | ||
export declare function getEntityProperty(server: IRpdServer, model: RpdDataModel, predicate: (item: RpdDataModelProperty) => boolean): RpdDataModelProperty | undefined; | ||
export declare function getEntityOwnPropertyByCode(model: RpdDataModel, propertyCode: string): RpdDataModelProperty | undefined; | ||
export declare function getEntityOwnProperty(model: RpdDataModel, predicate: (item: RpdDataModelProperty) => boolean): RpdDataModelProperty | undefined; | ||
export declare function getEntityPropertyByFieldName(server: IRpdServer, model: RpdDataModel, fieldName: string): RpdDataModelProperty | undefined; |
{ | ||
"name": "@ruiapp/rapid-core", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -1,3 +0,15 @@ | ||
import { EntityFilterOptions, FindEntityExistenceFilterOptions, FindEntityLogicalFilterOptions } from "~/types"; | ||
import { | ||
EntityFilterOptions, | ||
EntityNonRelationPropertyFilterOptions, | ||
FindEntityExistenceFilterOptions, | ||
FindEntityLogicalFilterOptions, | ||
RpdDataModel, | ||
RpdDataModelIndexOptions, | ||
RpdDataModelProperty, | ||
} from "~/types"; | ||
import { isNullOrUndefined } from "~/utilities/typeUtility"; | ||
import { getEntityOwnProperty, getEntityOwnPropertyByCode, isManyRelationProperty, isOneRelationProperty } from "./metaHelper"; | ||
import { IRpdServer } from "~/core/server"; | ||
import { isPlainObject } from "lodash"; | ||
import { RowFilterOptions } from "~/dataAccess/dataAccessTypes"; | ||
@@ -48,1 +60,77 @@ export function removeFiltersWithNullValue(filters?: EntityFilterOptions[]) { | ||
} | ||
export function convertModelIndexConditionsToRowFilterOptions(model: RpdDataModel, filters: RpdDataModelIndexOptions[]) { | ||
if (!filters || !filters.length) { | ||
return []; | ||
} | ||
const replacedFilters: RowFilterOptions[] = []; | ||
for (const filter of filters) { | ||
const { operator } = filter; | ||
if (operator === "and" || operator === "or") { | ||
replacedFilters.push({ | ||
operator: operator, | ||
filters: convertModelIndexConditionsToRowFilterOptions(model, filter.filters), | ||
}); | ||
} else { | ||
replacedFilters.push(replaceModelIndexConditionEntityPropertyWithTableColumn(model, filter)); | ||
} | ||
} | ||
return replacedFilters; | ||
} | ||
export function replaceModelIndexConditionEntityPropertyWithTableColumn(model: RpdDataModel, filter: RpdDataModelIndexOptions): RowFilterOptions { | ||
const { operator } = filter; | ||
const filterField = (filter as EntityNonRelationPropertyFilterOptions).field; | ||
let property: RpdDataModelProperty = getEntityOwnPropertyByCode(model, filterField); | ||
let filterValue = (filter as any).value; | ||
let columnName = ""; | ||
if (property) { | ||
if (isOneRelationProperty(property)) { | ||
columnName = property.targetIdColumnName; | ||
if (isPlainObject(filterValue)) { | ||
filterValue = filterValue.id; | ||
} | ||
} else if (isManyRelationProperty(property)) { | ||
throw new Error(`Condition on many-relation property "${property.code}" is not supported.`); | ||
} else { | ||
columnName = property.columnName || property.code; | ||
} | ||
} else if ((operator as any) === "exists" || (operator as any) === "notExists") { | ||
throw new Error(`"exists" and "notExists" operators are not supported in index conditions.`); | ||
} else { | ||
property = getEntityOwnProperty(model, (property) => { | ||
return property.columnName === filterField; | ||
}); | ||
if (property) { | ||
columnName = property.columnName; | ||
} else { | ||
// may be relation property. | ||
property = getEntityOwnProperty(model, (property) => { | ||
return property.targetIdColumnName === filterField; | ||
}); | ||
if (property) { | ||
if (isManyRelationProperty(property)) { | ||
throw new Error(`Condition on many-relation property "${property.code}" is not supported.`); | ||
} | ||
columnName = property.targetIdColumnName; | ||
if (isPlainObject(filterValue)) { | ||
filterValue = filterValue.id; | ||
} | ||
} else { | ||
throw new Error(`Unknown field "${filterField}" in index conditions.`); | ||
} | ||
} | ||
} | ||
// TODO: do not use `any` here | ||
return { | ||
operator: filter.operator, | ||
field: columnName, | ||
value: filterValue, | ||
itemType: (filter as any).itemType, | ||
} as RowFilterOptions; | ||
} |
@@ -69,2 +69,11 @@ import { cloneDeep } from "lodash"; | ||
export function getEntityOwnPropertyByCode(model: RpdDataModel, propertyCode: string): RpdDataModelProperty | undefined { | ||
return getEntityOwnProperty(model, (e) => e.code === propertyCode); | ||
} | ||
export function getEntityOwnProperty(model: RpdDataModel, predicate: (item: RpdDataModelProperty) => boolean): RpdDataModelProperty | undefined { | ||
let property = model.properties.find(predicate); | ||
return property; | ||
} | ||
export function getEntityPropertyByFieldName(server: IRpdServer, model: RpdDataModel, fieldName: string): RpdDataModelProperty | undefined { | ||
@@ -71,0 +80,0 @@ let property = getEntityPropertyByCode(server, model, fieldName); |
@@ -33,2 +33,3 @@ /** | ||
import { pgPropertyTypeColumnMap } from "~/dataAccess/columnTypeMapper"; | ||
import { convertModelIndexConditionsToRowFilterOptions } from "~/helpers/filterHelper"; | ||
@@ -392,2 +393,3 @@ class MetaManager implements RapidPlugin { | ||
logger.debug(`Creating indexes of table ${queryBuilder.quoteTable(model)}`); | ||
for (const index of model.indexes) { | ||
@@ -498,3 +500,4 @@ const sqlCreateIndex = generateTableIndexDDL(queryBuilder, server, model, index); | ||
if (index.conditions) { | ||
ddl += ` WHERE ${queryBuilder.buildFiltersExpression(model, index.conditions)}`; | ||
const rowFilterOptions = convertModelIndexConditionsToRowFilterOptions(model, index.conditions); | ||
ddl += ` WHERE ${queryBuilder.buildFiltersExpression(model, rowFilterOptions)}`; | ||
} | ||
@@ -501,0 +504,0 @@ |
Sorry, the diff of this file is too big to display
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
710756
19608