@8base/utils
Advanced tools
Comparing version 0.28.5 to 0.29.0
@@ -1,2 +0,6 @@ | ||
import { TableSchema, FormatDataAfterQueryOptions } from '../types'; | ||
import { Schema, FormatDataAfterQueryOptions } from '../types'; | ||
interface IFormatDataAfterQueryMeta { | ||
tableName: string; | ||
schema: Schema; | ||
} | ||
/** | ||
@@ -9,6 +13,6 @@ * Remove unnecessary data after fetch entity data by query | ||
*/ | ||
declare const formatDataAfterQuery: (tableName: string, data: { | ||
declare const formatDataAfterQuery: (data: { | ||
[key: string]: any; | ||
}, schema: TableSchema[], options?: FormatDataAfterQueryOptions) => {}; | ||
}, { tableName, schema }: IFormatDataAfterQueryMeta, options?: FormatDataAfterQueryOptions) => {}; | ||
export { formatDataAfterQuery }; | ||
//# sourceMappingURL=formatDataAfterQuery.d.ts.map |
@@ -21,4 +21,4 @@ "use strict"; | ||
*/ | ||
const formatDataAfterQuery = (tableName, data, schema, options = {}) => { | ||
const tableSchema = selectors_1.getTableSchemaByName(schema, tableName); | ||
const formatDataAfterQuery = (data, { tableName, schema }, options = {}) => { | ||
const tableSchema = selectors_1.tablesListSelectors.getTableByName(schema, tableName); | ||
if (!tableSchema) { | ||
@@ -50,3 +50,3 @@ throw new errors_1.SDKError(errors_1.ERROR_CODES.TABLE_NOT_FOUND, errors_1.PACKAGES.UTILS, `Table schema with ${tableName} name not found in schema.`); | ||
else { | ||
const relationTableSchema = selectors_1.getTableSchemaById(schema, fieldSchema.relation.refTable.id); | ||
const relationTableSchema = selectors_1.tablesListSelectors.getTableById(schema, fieldSchema.relation.refTable.id); | ||
if (!relationTableSchema) { | ||
@@ -56,6 +56,12 @@ throw new errors_1.SDKError(errors_1.ERROR_CODES.TABLE_NOT_FOUND, errors_1.PACKAGES.UTILS, `Relation table schema with ${fieldSchema.relation.refTable.id} id not found in schema.`); | ||
if (verifiers_1.isListField(fieldSchema)) { | ||
result[fieldName] = result[fieldName].map((item) => formatDataAfterQuery(relationTableSchema.name, item, schema)); | ||
result[fieldName] = result[fieldName].map((item) => formatDataAfterQuery(item, { | ||
tableName: relationTableSchema.name, | ||
schema, | ||
})); | ||
} | ||
else { | ||
result[fieldName] = formatDataAfterQuery(relationTableSchema.name, result[fieldName], schema); | ||
result[fieldName] = formatDataAfterQuery(result[fieldName], { | ||
tableName: relationTableSchema.name, | ||
schema, | ||
}); | ||
} | ||
@@ -62,0 +68,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { TableSchema } from '../types'; | ||
import { Schema } from '../types'; | ||
interface IOptions { | ||
@@ -7,2 +7,7 @@ skip?: boolean | ((...args: any[]) => boolean); | ||
} | ||
interface IFormatDataForMutationMeta { | ||
tableName: string; | ||
appName?: string; | ||
schema: Schema; | ||
} | ||
/** | ||
@@ -15,4 +20,4 @@ * Formats entity data for create or update mutation based on passed schema. | ||
*/ | ||
declare const formatDataForMutation: (type: "CREATE" | "UPDATE", tableName: string, data: any, schema: TableSchema[], options?: IOptions) => any; | ||
declare const formatDataForMutation: (type: "CREATE" | "UPDATE", data: any, { tableName, schema }: IFormatDataForMutationMeta, options?: IOptions) => any; | ||
export { formatDataForMutation }; | ||
//# sourceMappingURL=formatDataForMutation.d.ts.map |
@@ -24,3 +24,3 @@ "use strict"; | ||
*/ | ||
const formatDataForMutation = (type, tableName, data, schema, options = {}) => { | ||
const formatDataForMutation = (type, data, { tableName, schema }, options = {}) => { | ||
if (R.not(type in constants_1.MUTATION_TYPE)) { | ||
@@ -32,3 +32,3 @@ throw new errors_1.SDKError(errors_1.ERROR_CODES.INVALID_ARGUMENT, errors_1.PACKAGES.UTILS, `Invalid mutation type: ${type}`); | ||
} | ||
const tableSchema = selectors_1.getTableSchemaByName(schema, tableName); | ||
const tableSchema = selectors_1.tablesListSelectors.getTableByName(schema, tableName); | ||
if (!tableSchema) { | ||
@@ -69,3 +69,3 @@ throw new errors_1.SDKError(errors_1.ERROR_CODES.TABLE_NOT_FOUND, errors_1.PACKAGES.UTILS, `Table schema with ${tableName} name not found in schema.`); | ||
} | ||
formatedFieldData = formatFieldDataForMutation_1.formatFieldDataForMutation(type, fieldSchema, formatedFieldData, schema); | ||
formatedFieldData = formatFieldDataForMutation_1.formatFieldDataForMutation(type, formatedFieldData, { fieldSchema, schema }); | ||
if (typeof mutate === 'function') { | ||
@@ -72,0 +72,0 @@ formatedFieldData = mutate(formatedFieldData, data[fieldName], fieldSchema); |
@@ -1,5 +0,10 @@ | ||
import { FieldSchema } from '../types'; | ||
export declare const formatFieldData: (type: "CREATE" | "UPDATE", fieldSchema: FieldSchema, data: any, schema: import("../types").TableSchema[]) => { | ||
import { FieldSchema, Schema } from '../types'; | ||
interface IFormatFieldDataMeta { | ||
fieldSchema: FieldSchema; | ||
schema: Schema; | ||
} | ||
export declare const formatFieldData: (type: "CREATE" | "UPDATE", data: any, { fieldSchema, schema }: IFormatFieldDataMeta) => { | ||
[x: string]: any; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=formatFieldData.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const formatFieldDataListItem_1 = require("./formatFieldDataListItem"); | ||
exports.formatFieldData = (type, fieldSchema, data, schema) => { | ||
const nextData = formatFieldDataListItem_1.formatFieldDataListItem(type, fieldSchema, data, schema); | ||
exports.formatFieldData = (type, data, { fieldSchema, schema }) => { | ||
const nextData = formatFieldDataListItem_1.formatFieldDataListItem(type, data, { fieldSchema, schema }); | ||
return { | ||
@@ -7,0 +7,0 @@ [nextData.type]: nextData.data, |
@@ -1,4 +0,8 @@ | ||
import { FieldSchema } from '../types'; | ||
declare const formatFieldDataForMutation: (type: "CREATE" | "UPDATE", fieldSchema: FieldSchema, data: any, schema: import("../types").TableSchema[]) => any; | ||
import { FieldSchema, Schema } from '../types'; | ||
interface IFormatFieldDataForMutationMeta { | ||
fieldSchema: FieldSchema; | ||
schema: Schema; | ||
} | ||
declare const formatFieldDataForMutation: (type: "CREATE" | "UPDATE", data: any, { fieldSchema, schema }: IFormatFieldDataForMutationMeta) => any; | ||
export { formatFieldDataForMutation }; | ||
//# sourceMappingURL=formatFieldDataForMutation.d.ts.map |
@@ -20,10 +20,10 @@ "use strict"; | ||
}; | ||
const formatFieldDataForMutation = (type, fieldSchema, data, schema) => { | ||
const formatFieldDataForMutation = (type, data, { fieldSchema, schema }) => { | ||
let nextData = data; | ||
if (verifiers.isFileField(fieldSchema) || verifiers.isRelationField(fieldSchema)) { | ||
if (verifiers.isListField(fieldSchema)) { | ||
nextData = formatFieldDataList_1.formatFieldDataList(type, fieldSchema, data, schema); | ||
nextData = formatFieldDataList_1.formatFieldDataList(type, data, { fieldSchema, schema }); | ||
} | ||
else { | ||
nextData = formatFieldData_1.formatFieldData(type, fieldSchema, data, schema); | ||
nextData = formatFieldData_1.formatFieldData(type, data, { fieldSchema, schema }); | ||
} | ||
@@ -30,0 +30,0 @@ } |
@@ -1,3 +0,8 @@ | ||
import { FieldSchema } from '../types'; | ||
export declare const formatFieldDataList: (type: "CREATE" | "UPDATE", fieldSchema: FieldSchema, data: any, schema: import("../types").TableSchema[]) => any; | ||
import { FieldSchema, Schema } from '../types'; | ||
interface IFormatFieldDataListMeta { | ||
fieldSchema: FieldSchema; | ||
schema: Schema; | ||
} | ||
export declare const formatFieldDataList: (type: "CREATE" | "UPDATE", data: any, { fieldSchema, schema }: IFormatFieldDataListMeta) => any; | ||
export {}; | ||
//# sourceMappingURL=formatFieldDataList.d.ts.map |
@@ -13,3 +13,3 @@ "use strict"; | ||
const formatFieldDataListItem_1 = require("./formatFieldDataListItem"); | ||
exports.formatFieldDataList = (type, fieldSchema, data, schema) => R.pipe(R.map(item => formatFieldDataListItem_1.formatFieldDataListItem(type, fieldSchema, item, schema)), R.groupBy(R.prop('type')), R.mapObjIndexed(R.map(R.prop('data'))), R.when(R.allPass([R.complement(R.has('reconnect')), R.always(R.equals(type, constants_1.MUTATION_TYPE.UPDATE))]), R.assoc('reconnect', [])))(data); | ||
exports.formatFieldDataList = (type, data, { fieldSchema, schema }) => R.pipe(R.map(item => formatFieldDataListItem_1.formatFieldDataListItem(type, item, { fieldSchema, schema })), R.groupBy(R.prop('type')), R.mapObjIndexed(R.map(R.prop('data'))), R.when(R.allPass([R.complement(R.has('reconnect')), R.always(R.equals(type, constants_1.MUTATION_TYPE.UPDATE))]), R.assoc('reconnect', [])))(data); | ||
//# sourceMappingURL=formatFieldDataList.js.map |
@@ -1,6 +0,11 @@ | ||
import { FieldSchema, TableSchema } from '../types'; | ||
export declare const formatFieldDataListItem: (type: "CREATE" | "UPDATE", fieldSchema: FieldSchema, data: any, schema: TableSchema[]) => { | ||
import { FieldSchema, Schema } from '../types'; | ||
interface IFormatFieldDataListItemMeta { | ||
fieldSchema: FieldSchema; | ||
schema: Schema; | ||
} | ||
export declare const formatFieldDataListItem: (type: "CREATE" | "UPDATE", data: any, { fieldSchema, schema }: IFormatFieldDataListItemMeta) => { | ||
data: any; | ||
type: string; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=formatFieldDataListItem.d.ts.map |
@@ -16,3 +16,3 @@ "use strict"; | ||
const errors_1 = require("../errors"); | ||
exports.formatFieldDataListItem = (type, fieldSchema, data, schema) => { | ||
exports.formatFieldDataListItem = (type, data, { fieldSchema, schema }) => { | ||
let nextData = data; | ||
@@ -38,10 +38,16 @@ if (R.isNil(nextData)) { | ||
if (verifiers_1.isRelationField(fieldSchema)) { | ||
const relationTableSchema = selectors_1.getTableSchemaById(schema, fieldSchema.relation.refTable.id); | ||
const relationTableSchema = selectors_1.tablesListSelectors.getTableById(schema, fieldSchema.relation.refTable.id); | ||
if (!relationTableSchema) { | ||
throw new errors_1.SDKError(errors_1.ERROR_CODES.TABLE_NOT_FOUND, errors_1.PACKAGES.UTILS, `Relation table schema with ${fieldSchema.relation.refTable.id} id not found in schema.`); | ||
} | ||
nextData = formatDataForMutation_1.formatDataForMutation(constants_1.MUTATION_TYPE.CREATE, relationTableSchema.name, nextData, schema); | ||
nextData = formatDataForMutation_1.formatDataForMutation(constants_1.MUTATION_TYPE.CREATE, nextData, { | ||
tableName: relationTableSchema.name, | ||
schema, | ||
}); | ||
} | ||
if (verifiers_1.isFileField(fieldSchema)) { | ||
nextData = formatDataForMutation_1.formatDataForMutation(constants_1.MUTATION_TYPE.CREATE, constants_1.SYSTEM_TABLES.FILES, nextData, schema); | ||
nextData = formatDataForMutation_1.formatDataForMutation(constants_1.MUTATION_TYPE.CREATE, nextData, { | ||
tableName: constants_1.SYSTEM_TABLES.FILES, | ||
schema, | ||
}); | ||
} | ||
@@ -48,0 +54,0 @@ return { |
@@ -5,10 +5,8 @@ import { TableSchema, QueryGeneratorConfig } from '../types'; | ||
} & QueryGeneratorConfig; | ||
export declare const createQueryColumnsList: (tablesList: TableSchema[], tableName: string, config?: CreateQueryColumnsListConfig, prevKey?: string) => { | ||
export declare const createQueryColumnsList: (tablesList: TableSchema[], tableId: string, config?: CreateQueryColumnsListConfig, prevKey?: string) => { | ||
name: string; | ||
title: string; | ||
meta: { | ||
[key: string]: any; | ||
}; | ||
meta: object; | ||
}[]; | ||
export {}; | ||
//# sourceMappingURL=createQueryColumnsList.d.ts.map |
@@ -11,14 +11,13 @@ "use strict"; | ||
const R = __importStar(require("ramda")); | ||
const tableFieldSelectors = __importStar(require("../selectors/tableFieldSelectors")); | ||
const getTableByName = (tablesList, tableName) => tablesList.find(({ name }) => tableName === name); | ||
const selectors_1 = require("../selectors"); | ||
const capitalizeFirstLetter = (str) => { | ||
return str.charAt(0).toUpperCase() + str.slice(1); | ||
}; | ||
exports.createQueryColumnsList = (tablesList, tableName, config = {}, prevKey = '') => { | ||
const { fields = [] } = getTableByName(tablesList, tableName) || {}; | ||
exports.createQueryColumnsList = (tablesList, tableId, config = {}, prevKey = '') => { | ||
const { fields = [] } = selectors_1.tablesListSelectors.getTableById(tablesList, tableId) || {}; | ||
const { deep = 1, withMeta = true, flatten = true, includeColumns } = config; | ||
const transformedList = fields | ||
.filter(field => { | ||
const isMeta = tableFieldSelectors.isMetaField(field); | ||
const isIdField = tableFieldSelectors.isIdField(field); | ||
const isMeta = selectors_1.tableFieldSelectors.isMetaField(field); | ||
const isIdField = selectors_1.tableFieldSelectors.isIdField(field); | ||
if (isIdField) { | ||
@@ -30,12 +29,12 @@ return false; | ||
.map(field => { | ||
const fieldName = tableFieldSelectors.getFieldName(field); | ||
const fieldType = tableFieldSelectors.getFieldType(field); | ||
const fieldTypeAttributes = tableFieldSelectors.getFieldTypesAttributes(field); | ||
const isRelation = tableFieldSelectors.isRelationField(field); | ||
const isFile = tableFieldSelectors.isFileField(field); | ||
const isList = tableFieldSelectors.isListField(field); | ||
const refTableName = tableFieldSelectors.getRelationTableName(field); | ||
const fieldName = selectors_1.tableFieldSelectors.getFieldName(field); | ||
const fieldType = selectors_1.tableFieldSelectors.getFieldType(field); | ||
const fieldTypeAttributes = selectors_1.tableFieldSelectors.getFieldTypesAttributes(field); | ||
const isRelation = selectors_1.tableFieldSelectors.isRelationField(field); | ||
const isFile = selectors_1.tableFieldSelectors.isFileField(field); | ||
const isList = selectors_1.tableFieldSelectors.isListField(field); | ||
const refTableId = selectors_1.tableFieldSelectors.getRelationTableId(field); | ||
const refTable = selectors_1.tablesListSelectors.getTableById(tablesList, refTableId); | ||
const currentKeyString = prevKey ? `${prevKey}.${fieldName}` : fieldName; | ||
const title = capitalizeFirstLetter(fieldName); | ||
const refTable = getTableByName(tablesList, refTableName); | ||
const meta = { | ||
@@ -58,10 +57,10 @@ fieldType, | ||
{ | ||
meta, | ||
name: currentKeyString, | ||
title, | ||
meta, | ||
}, | ||
]; | ||
} | ||
else if (isRelation && refTableName && refTable && deep > 1) { | ||
const innerKeys = exports.createQueryColumnsList(tablesList, refTableName, { deep: deep - 1, withMeta, includeColumns }, currentKeyString); | ||
else if (isRelation && refTableId && refTable && deep > 1) { | ||
const innerKeys = exports.createQueryColumnsList(tablesList, refTableId, { deep: deep - 1, withMeta, includeColumns }, currentKeyString); | ||
return flatten | ||
@@ -68,0 +67,0 @@ ? [...innerKeys, { name: currentKeyString, title, meta }] |
@@ -7,3 +7,3 @@ import { TableSchema, QueryGeneratorConfig } from '../types'; | ||
}; | ||
export declare const createQueryString: (tablesList: TableSchema[], tableName: string, queryObjectConfig?: QueryGeneratorConfig, prevKey?: string) => string; | ||
export declare const createQueryString: (tablesList: TableSchema[], tableId: string, queryObjectConfig?: QueryGeneratorConfig, prevKey?: string) => string; | ||
//# sourceMappingURL=createQueryString.d.ts.map |
@@ -11,8 +11,12 @@ "use strict"; | ||
const R = __importStar(require("ramda")); | ||
const tableFieldSelectors = __importStar(require("../selectors/tableFieldSelectors")); | ||
const constants_1 = require("../constants"); | ||
const selectors_1 = require("../selectors"); | ||
const errors_1 = require("../errors"); | ||
const DEFAULT_DEPTH = 1; | ||
const getTableByName = (tablesList, tableName) => tablesList.find(({ name }) => tableName === name); | ||
exports.createQueryString = (tablesList, tableName, queryObjectConfig = {}, prevKey = '') => { | ||
const { fields = [] } = getTableByName(tablesList, tableName) || {}; | ||
exports.createQueryString = (tablesList, tableId, queryObjectConfig = {}, prevKey = '') => { | ||
const table = selectors_1.tablesListSelectors.getTableById(tablesList, tableId); | ||
if (!table) { | ||
throw new errors_1.SDKError(errors_1.ERROR_CODES.TABLE_NOT_FOUND, errors_1.PACKAGES.UTILS, `Table with id ${tableId} not found`); | ||
} | ||
const { fields, name: tableName } = table; | ||
const { deep = DEFAULT_DEPTH, withMeta = true, relationItemsCount, includeColumns, permissions = {}, } = queryObjectConfig; | ||
@@ -22,7 +26,7 @@ let queryObject = ''; | ||
.filter(field => { | ||
const isMeta = tableFieldSelectors.isMetaField(field); | ||
const isRelation = tableFieldSelectors.isRelationField(field); | ||
const isMeta = selectors_1.tableFieldSelectors.isMetaField(field); | ||
const isRelation = selectors_1.tableFieldSelectors.isRelationField(field); | ||
let shouldIncludeField = withMeta ? true : !isMeta; | ||
if (isRelation) { | ||
const refTableName = tableFieldSelectors.getRelationTableName(field); | ||
const refTableName = selectors_1.tableFieldSelectors.getRelationTableName(field); | ||
const refTableAllowed = R.pathOr(true, ['data', refTableName, 'permission', 'read', 'allow'], permissions); | ||
@@ -39,9 +43,10 @@ shouldIncludeField = refTableAllowed && shouldIncludeField; | ||
let fieldContent = field.name; | ||
const isRelation = tableFieldSelectors.isRelationField(field); | ||
const isFile = tableFieldSelectors.isFileField(field); | ||
const isSmart = tableFieldSelectors.isSmartField(field); | ||
const isList = tableFieldSelectors.isListField(field); | ||
const refTableName = tableFieldSelectors.getRelationTableName(field); | ||
const refTable = getTableByName(tablesList, refTableName); | ||
const isSettingsRefTable = tableFieldSelectors.getRelationTableName(field) === constants_1.SYSTEM_TABLES.SETTINGS; | ||
const isRelation = selectors_1.tableFieldSelectors.isRelationField(field); | ||
const isFile = selectors_1.tableFieldSelectors.isFileField(field); | ||
const isSmart = selectors_1.tableFieldSelectors.isSmartField(field); | ||
const isList = selectors_1.tableFieldSelectors.isListField(field); | ||
const refTableId = selectors_1.tableFieldSelectors.getRelationTableId(field); | ||
const refTable = selectors_1.tablesListSelectors.getTableById(tablesList, refTableId); | ||
const isSettingsRefTable = selectors_1.tableFieldSelectors.isSystemField(field) && | ||
selectors_1.tableFieldSelectors.getRelationTableName(field) === constants_1.SYSTEM_TABLES.SETTINGS; | ||
const currentKeyString = prevKey ? `${prevKey}.${field.name}` : field.name; | ||
@@ -55,3 +60,3 @@ let isNotEmptyRelation = false; | ||
else if (isRelation) { | ||
if (deep <= 1 || !refTableName || !refTable) { | ||
if (deep <= 1 || !refTableId || !refTable) { | ||
fieldContent = `{ | ||
@@ -65,3 +70,3 @@ id | ||
const relationIncludeColumns = includeAllrelationFields ? null : includeColumns; | ||
const innerFields = exports.createQueryString(tablesList, refTableName, { | ||
const innerFields = exports.createQueryString(tablesList, refTableId, { | ||
deep: deep - 1, | ||
@@ -68,0 +73,0 @@ includeColumns: relationIncludeColumns, |
@@ -5,9 +5,9 @@ import { TableSchema, QueryGeneratorConfig } from '../types'; | ||
} & QueryGeneratorConfig; | ||
export declare const createTableFilterGraphqlTag: (tablesList: TableSchema[], tableName: string, config?: QueryTableFilterConfig) => string; | ||
export declare const createTableRowCreateTag: (tablesList: TableSchema[], tableName: string, config?: QueryGeneratorConfig) => string; | ||
export declare const createTableRowCreateManyTag: (tablesList: TableSchema[], tableName: string) => string; | ||
export declare const createTableRowUpdateTag: (tablesList: TableSchema[], tableName: string, config?: QueryGeneratorConfig) => string; | ||
export declare const createTableRowQueryTag: (tablesList: TableSchema[], tableName: string, config?: QueryGeneratorConfig) => string; | ||
export declare const createTableRowDeleteTag: (tablesList: TableSchema[], tableName: string) => string; | ||
export declare const createTableFilterGraphqlTag: (tablesList: TableSchema[], tableId: string, config?: QueryTableFilterConfig) => string; | ||
export declare const createTableRowCreateTag: (tablesList: TableSchema[], tableId: string, config?: QueryGeneratorConfig) => string; | ||
export declare const createTableRowCreateManyTag: (tablesList: TableSchema[], tableId: string) => string; | ||
export declare const createTableRowUpdateTag: (tablesList: TableSchema[], tableId: string, config?: QueryGeneratorConfig) => string; | ||
export declare const createTableRowQueryTag: (tablesList: TableSchema[], tableId: string, config?: QueryGeneratorConfig) => string; | ||
export declare const createTableRowDeleteTag: (tablesList: TableSchema[], tableId: string) => string; | ||
export {}; | ||
//# sourceMappingURL=queryGenerators.d.ts.map |
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
@@ -15,91 +26,119 @@ if (mod && mod.__esModule) return mod; | ||
const schema_name_generator_1 = require("@8base/schema-name-generator"); | ||
const selectors_1 = require("../selectors"); | ||
const graphql_prettier_1 = __importDefault(require("graphql-prettier")); | ||
const tableSelectors = __importStar(require("../selectors/tableSelectors")); | ||
const errors_1 = require("../errors"); | ||
const createQueryString_1 = require("./createQueryString"); | ||
const upperFirst = (str) => R.toUpper(R.head(str)) + R.tail(str); | ||
const getTableByName = (tablesList, tableName) => tablesList.find(({ name }) => tableName === name); | ||
exports.createTableFilterGraphqlTag = (tablesList, tableName, config = {}) => graphql_prettier_1.default(` | ||
query ${upperFirst(tableName)}TableContent( | ||
$filter: ${schema_name_generator_1.SchemaNameGenerator.getFilterInputTypeName(tableName)} | ||
$orderBy: [${schema_name_generator_1.SchemaNameGenerator.getOrderByInputTypeName(tableName)}] | ||
$after: String | ||
$before: String | ||
$first: Int | ||
$last: Int | ||
$skip: Int | ||
const getTable = (tablesList, tableId) => { | ||
const table = selectors_1.tablesListSelectors.getTableById(tablesList, tableId); | ||
if (!table) { | ||
throw new errors_1.SDKError(errors_1.ERROR_CODES.TABLE_NOT_FOUND, errors_1.PACKAGES.UTILS, `Table schema with ${tableId} id not found.`); | ||
} | ||
return table; | ||
}; | ||
exports.createTableFilterGraphqlTag = (tablesList, tableId, config = {}) => { | ||
const table = getTable(tablesList, tableId); | ||
const { withResultData = true } = config, restConfig = __rest(config, ["withResultData"]); | ||
return graphql_prettier_1.default(` | ||
query ${upperFirst(table.name)}TableContent( | ||
$filter: ${schema_name_generator_1.SchemaNameGenerator.getFilterInputTypeName(table.name)} | ||
$orderBy: [${schema_name_generator_1.SchemaNameGenerator.getOrderByInputTypeName(table.name)}] | ||
$after: String | ||
$before: String | ||
$first: Int | ||
$last: Int | ||
$skip: Int | ||
) { | ||
${config.tableContentName ? `${config.tableContentName}: ` : ''}${schema_name_generator_1.SchemaNameGenerator.getTableListFieldName(table.name)}( | ||
filter: $filter | ||
orderBy: $orderBy | ||
after: $after | ||
before: $before | ||
first: $first | ||
last: $last | ||
skip: $skip | ||
) { | ||
${config.tableContentName ? `${config.tableContentName}: ` : ''}${schema_name_generator_1.SchemaNameGenerator.getTableListFieldName(tableName)}( | ||
filter: $filter | ||
orderBy: $orderBy | ||
after: $after | ||
before: $before | ||
first: $first | ||
last: $last | ||
skip: $skip | ||
) { | ||
items { | ||
id | ||
${createQueryString_1.createQueryString(tablesList, tableName, Object.assign({}, config))} | ||
_description | ||
} | ||
count | ||
items { | ||
id | ||
${withResultData ? createQueryString_1.createQueryString(tablesList, table.id, Object.assign({}, restConfig)) : ''} | ||
_description | ||
} | ||
}`); | ||
exports.createTableRowCreateTag = (tablesList, tableName, config = {}) => { | ||
const table = getTableByName(tablesList, tableName); | ||
count | ||
} | ||
}`); | ||
}; | ||
exports.createTableRowCreateTag = (tablesList, tableId, config = {}) => { | ||
const table = getTable(tablesList, tableId); | ||
const hasNonMetaFields = tableSelectors.hasNonMetaFields(table); | ||
const { withResultData = true } = config, restConfig = __rest(config, ["withResultData"]); | ||
if (hasNonMetaFields) { | ||
return graphql_prettier_1.default(` | ||
mutation DataViewer${upperFirst(tableName)}RowCreate($data: ${schema_name_generator_1.SchemaNameGenerator.getCreateInputName(tableName)}!) { | ||
${schema_name_generator_1.SchemaNameGenerator.getCreateItemFieldName(tableName)}(data: $data) { | ||
id | ||
${createQueryString_1.createQueryString(tablesList, tableName, Object.assign({ withMeta: false }, config))} | ||
mutation ${upperFirst(table.name)}Create($data: ${schema_name_generator_1.SchemaNameGenerator.getCreateInputName(table.name)}!) { | ||
${schema_name_generator_1.SchemaNameGenerator.getCreateItemFieldName(table.name)}(data: $data) { | ||
id | ||
${withResultData ? createQueryString_1.createQueryString(tablesList, tableId, Object.assign({ withMeta: false }, restConfig)) : ''} | ||
} | ||
}`); | ||
} | ||
}`); | ||
} | ||
return graphql_prettier_1.default(` | ||
mutation DataViewer${upperFirst(tableName)}RowCreate { | ||
${schema_name_generator_1.SchemaNameGenerator.getCreateItemFieldName(tableName)} { | ||
id | ||
${createQueryString_1.createQueryString(tablesList, tableName, Object.assign({ withMeta: false }, config))} | ||
} | ||
}`); | ||
mutation ${upperFirst(table.name)}Create { | ||
${schema_name_generator_1.SchemaNameGenerator.getCreateItemFieldName(table.name)} { | ||
id | ||
${withResultData ? createQueryString_1.createQueryString(tablesList, tableId, Object.assign({ withMeta: false }, restConfig)) : ''} | ||
} | ||
}`); | ||
}; | ||
exports.createTableRowCreateManyTag = (tablesList, tableName) => { | ||
const table = getTableByName(tablesList, tableName); | ||
exports.createTableRowCreateManyTag = (tablesList, tableId) => { | ||
const table = getTable(tablesList, tableId); | ||
const hasNonMetaFields = tableSelectors.hasNonMetaFields(table); | ||
if (hasNonMetaFields) { | ||
return graphql_prettier_1.default(` | ||
mutation DataViewer${upperFirst(tableName)}RowCreateMany($data: [${schema_name_generator_1.SchemaNameGenerator.getCreateManyInputName(tableName)}]!) { | ||
${schema_name_generator_1.SchemaNameGenerator.getCreateManyItemFieldName(tableName)}(data: $data) { | ||
count | ||
mutation ${upperFirst(table.name)}CreateMany($data: [${schema_name_generator_1.SchemaNameGenerator.getCreateManyInputName(table.name)}]!) { | ||
${schema_name_generator_1.SchemaNameGenerator.getCreateManyItemFieldName(table.name)}(data: $data) { | ||
count | ||
} | ||
}`); | ||
} | ||
}`); | ||
} | ||
return graphql_prettier_1.default(` | ||
mutation DataViewer${upperFirst(tableName)}RowCreateMany { | ||
${schema_name_generator_1.SchemaNameGenerator.getCreateManyItemFieldName(tableName)} { | ||
return ` | ||
mutation ${upperFirst(table.name)}CreateMany { | ||
${schema_name_generator_1.SchemaNameGenerator.getCreateManyItemFieldName(table.name)} { | ||
count | ||
} | ||
}`); | ||
}`; | ||
}; | ||
exports.createTableRowUpdateTag = (tablesList, tableName, config = {}) => graphql_prettier_1.default(` | ||
mutation DataViewer${upperFirst(tableName)}RowUpdate($data: ${schema_name_generator_1.SchemaNameGenerator.getUpdateInputName(tableName)}!, $filter: ${schema_name_generator_1.SchemaNameGenerator.getKeyFilterInputTypeName(tableName)}) { | ||
${schema_name_generator_1.SchemaNameGenerator.getUpdateItemFieldName(tableName)}(data: $data, filter: $filter) { | ||
id | ||
${createQueryString_1.createQueryString(tablesList, tableName, Object.assign({ withMeta: false }, config))} | ||
} | ||
}`); | ||
exports.createTableRowQueryTag = (tablesList, tableName, config = {}) => graphql_prettier_1.default(` | ||
query DataViewer${upperFirst(tableName)}Row($id: ID!) { | ||
${schema_name_generator_1.SchemaNameGenerator.getTableItemFieldName(tableName)}(id: $id) { | ||
${createQueryString_1.createQueryString(tablesList, tableName, Object.assign({}, config))} | ||
} | ||
}`); | ||
exports.createTableRowDeleteTag = (tablesList, tableName) => graphql_prettier_1.default(` | ||
mutation DataViewer${upperFirst(tableName)}RowDelete($filter: ${schema_name_generator_1.SchemaNameGenerator.getKeyFilterInputTypeName(tableName)}!, $force: Boolean) { | ||
${schema_name_generator_1.SchemaNameGenerator.getDeleteItemFieldName(tableName)}(filter: $filter, force: $force) { | ||
success | ||
} | ||
}`); | ||
exports.createTableRowUpdateTag = (tablesList, tableId, config = {}) => { | ||
const table = getTable(tablesList, tableId); | ||
const { withResultData = true } = config, restConfig = __rest(config, ["withResultData"]); | ||
return graphql_prettier_1.default(` | ||
mutation ${upperFirst(table.name)}Update( | ||
$data: ${schema_name_generator_1.SchemaNameGenerator.getUpdateInputName(table.name)}!, | ||
$filter: ${schema_name_generator_1.SchemaNameGenerator.getKeyFilterInputTypeName(table.name)} | ||
) { | ||
${schema_name_generator_1.SchemaNameGenerator.getUpdateItemFieldName(table.name)}(data: $data, filter: $filter) { | ||
id | ||
${withResultData ? createQueryString_1.createQueryString(tablesList, tableId, Object.assign({ withMeta: false }, restConfig)) : ''} | ||
} | ||
}`); | ||
}; | ||
exports.createTableRowQueryTag = (tablesList, tableId, config = {}) => { | ||
const table = getTable(tablesList, tableId); | ||
const { withResultData = true } = config, restConfig = __rest(config, ["withResultData"]); | ||
return graphql_prettier_1.default(` | ||
query ${upperFirst(table.name)}Entity($id: ID!) { | ||
${schema_name_generator_1.SchemaNameGenerator.getTableItemFieldName(table.name)}(id: $id) { | ||
id | ||
${withResultData ? createQueryString_1.createQueryString(tablesList, tableId, Object.assign({}, restConfig)) : ''} | ||
} | ||
}`); | ||
}; | ||
exports.createTableRowDeleteTag = (tablesList, tableId) => { | ||
const table = getTable(tablesList, tableId); | ||
return graphql_prettier_1.default(` | ||
mutation ${upperFirst(table.name)}Delete($filter: ${schema_name_generator_1.SchemaNameGenerator.getKeyFilterInputTypeName(table.name)}!, $force: Boolean) { | ||
${schema_name_generator_1.SchemaNameGenerator.getDeleteItemFieldName(table.name)}(filter: $filter, force: $force) { | ||
success | ||
} | ||
}`); | ||
}; | ||
//# sourceMappingURL=queryGenerators.js.map |
import * as tableSelectors from './tableSelectors'; | ||
import * as tablesListSelectors from './tablesListSelectors'; | ||
import * as tableFieldSelectors from './tableFieldSelectors'; | ||
export { getTableSchemaByName } from './getTableSchemaByName'; | ||
export { getTableSchemaById } from './getTableSchemaById'; | ||
export { tableSelectors, tableFieldSelectors }; | ||
export { tableSelectors, tableFieldSelectors, tablesListSelectors }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -12,8 +12,6 @@ "use strict"; | ||
exports.tableSelectors = tableSelectors; | ||
const tablesListSelectors = __importStar(require("./tablesListSelectors")); | ||
exports.tablesListSelectors = tablesListSelectors; | ||
const tableFieldSelectors = __importStar(require("./tableFieldSelectors")); | ||
exports.tableFieldSelectors = tableFieldSelectors; | ||
var getTableSchemaByName_1 = require("./getTableSchemaByName"); | ||
exports.getTableSchemaByName = getTableSchemaByName_1.getTableSchemaByName; | ||
var getTableSchemaById_1 = require("./getTableSchemaById"); | ||
exports.getTableSchemaById = getTableSchemaById_1.getTableSchemaById; | ||
//# sourceMappingURL=index.js.map |
@@ -16,2 +16,3 @@ import { ParametricSelector } from 'reselect'; | ||
export declare const isListField: import("reselect").OutputSelector<FieldSchema, boolean, (res: FieldSchema) => boolean>; | ||
export declare const isSystemField: import("reselect").OutputSelector<FieldSchema, boolean, (res: FieldSchema) => boolean>; | ||
export declare const getFieldName: import("reselect").OutputSelector<FieldSchema, string, (res: FieldSchema) => string>; | ||
@@ -18,0 +19,0 @@ export declare const getRelationTableId: ParametricSelector<FieldSchema, void, string>; |
@@ -22,2 +22,3 @@ "use strict"; | ||
exports.isListField = reselect_1.createSelector(exports.getTableField, R.propEq('isList', true)); | ||
exports.isSystemField = reselect_1.createSelector(exports.getTableField, R.propEq('isSystem', true)); | ||
exports.getFieldName = reselect_1.createSelector(exports.getTableField, R.prop('name')); | ||
@@ -24,0 +25,0 @@ exports.getRelationTableId = reselect_1.createSelector(exports.getTableField, R.path(['relation', 'refTable', 'id'])); |
@@ -7,8 +7,9 @@ import { ParametricSelector } from 'reselect'; | ||
export declare const getFieldTypeById: ParametricSelector<TableSchema, string, any>; | ||
export declare const isSystemField: import("reselect").OutputSelector<TableSchema, boolean, (res: TableSchema) => boolean>; | ||
export declare const isRelationField: ParametricSelector<TableSchema, string, boolean>; | ||
export declare const isFileField: import("reselect").OutputParametricSelector<TableSchema, string, boolean, (res: "FILE") => boolean>; | ||
export declare const isSmartField: import("reselect").OutputParametricSelector<TableSchema, string, boolean, (res: "SMART") => boolean>; | ||
export declare const isMetaField: import("reselect").OutputParametricSelector<TableSchema, string, boolean, (res: void | FieldSchema) => boolean>; | ||
export declare const isListField: import("reselect").OutputParametricSelector<TableSchema, string, boolean, (res: void | FieldSchema) => boolean>; | ||
export declare const getFieldNameById: import("reselect").OutputParametricSelector<TableSchema, string, unknown, (res: void | FieldSchema) => unknown>; | ||
export declare const isMetaField: import("reselect").OutputParametricSelector<TableSchema, string, boolean, (res: {} | FieldSchema) => boolean>; | ||
export declare const isListField: import("reselect").OutputParametricSelector<TableSchema, string, boolean, (res: {} | FieldSchema) => boolean>; | ||
export declare const getFieldNameById: import("reselect").OutputParametricSelector<TableSchema, string, unknown, (res: {} | FieldSchema) => unknown>; | ||
export declare const hasNonMetaFields: (schema?: TableSchema) => boolean; | ||
@@ -15,0 +16,0 @@ export declare const hasListFields: (x0: TableSchema) => boolean; |
@@ -16,9 +16,11 @@ "use strict"; | ||
exports.getFieldByName = (tableSchema, fieldName) => R.find(R.propEq('name', fieldName), tableSchema.fields); | ||
exports.getFieldTypeById = reselect_1.createSelector(exports.getFieldById, R.propOr('', 'fieldType')); | ||
const getFieldByIdOrEmpty = reselect_1.createSelector(exports.getFieldById, field => field || {}); | ||
exports.getFieldTypeById = reselect_1.createSelector(getFieldByIdOrEmpty, R.propOr('', 'fieldType')); | ||
exports.isSystemField = reselect_1.createSelector(exports.getTable, R.propEq('isSystem', true)); | ||
exports.isRelationField = reselect_1.createSelector(exports.getFieldTypeById, R.equals(constants_1.FIELD_TYPE.RELATION)); | ||
exports.isFileField = reselect_1.createSelector(exports.getFieldTypeById, R.equals(constants_1.FIELD_TYPE.FILE)); | ||
exports.isSmartField = reselect_1.createSelector(exports.getFieldTypeById, R.equals(constants_1.FIELD_TYPE.SMART)); | ||
exports.isMetaField = reselect_1.createSelector(exports.getFieldById, R.propEq('isMeta', true)); | ||
exports.isListField = reselect_1.createSelector(exports.getFieldById, R.propEq('isList', true)); | ||
exports.getFieldNameById = reselect_1.createSelector(exports.getFieldById, R.propOr('', 'name')); | ||
exports.isMetaField = reselect_1.createSelector(getFieldByIdOrEmpty, R.propEq('isMeta', true)); | ||
exports.isListField = reselect_1.createSelector(getFieldByIdOrEmpty, R.propEq('isList', true)); | ||
exports.getFieldNameById = reselect_1.createSelector(getFieldByIdOrEmpty, R.propOr('', 'name')); | ||
exports.hasNonMetaFields = R.pipe(R.propOr([], 'fields'), R.any(R.propEq('isMeta', false))); | ||
@@ -25,0 +27,0 @@ exports.hasListFields = R.pipe(exports.getTable, ({ fields }) => !!fields.find(({ isList }) => isList)); |
@@ -77,2 +77,3 @@ import { FIELD_TYPE, SWITCH_FORMATS, TEXT_FORMATS, NUMBER_FORMATS, FILE_FORMATS, DATE_FORMATS, MUTATION_TYPE, SMART_FORMATS } from './constants'; | ||
withMeta?: boolean; | ||
withResultData?: boolean; | ||
relationItemsCount?: number; | ||
@@ -79,0 +80,0 @@ includeColumns?: null | string[]; |
{ | ||
"name": "@8base/utils", | ||
"version": "0.28.5", | ||
"version": "0.29.0", | ||
"repository": "https://github.com/8base/sdk", | ||
@@ -17,3 +17,3 @@ "homepage": "https://github.com/8base/sdk/tree/master/packages/utils#readme", | ||
"@8base/error-codes": "^1.0.12", | ||
"@8base/schema-name-generator": "^0.1.1", | ||
"@8base/schema-name-generator": "^0.1.5", | ||
"graphql-prettier": "^1.0.5", | ||
@@ -20,0 +20,0 @@ "ramda": "^0.26.1", |
import * as R from 'ramda'; | ||
import { tableSelectors, getTableSchemaByName, getTableSchemaById } from '../selectors'; | ||
import { tableSelectors, tablesListSelectors } from '../selectors'; | ||
import { isRelationField, isFileField, isListField, isMetaField } from '../verifiers'; | ||
@@ -9,2 +9,7 @@ | ||
interface IFormatDataAfterQueryMeta { | ||
tableName: string; | ||
schema: Schema; | ||
} | ||
/** | ||
@@ -18,8 +23,7 @@ * Remove unnecessary data after fetch entity data by query | ||
const formatDataAfterQuery = ( | ||
tableName: string, | ||
data: { [key: string]: any }, | ||
schema: Schema, | ||
{ tableName, schema }: IFormatDataAfterQueryMeta, | ||
options: FormatDataAfterQueryOptions = {}, | ||
) => { | ||
const tableSchema = getTableSchemaByName(schema, tableName); | ||
const tableSchema = tablesListSelectors.getTableByName(schema, tableName); | ||
@@ -62,3 +66,3 @@ if (!tableSchema) { | ||
} else { | ||
const relationTableSchema = getTableSchemaById(schema, fieldSchema.relation.refTable.id); | ||
const relationTableSchema = tablesListSelectors.getTableById(schema, fieldSchema.relation.refTable.id); | ||
@@ -75,6 +79,12 @@ if (!relationTableSchema) { | ||
result[fieldName] = result[fieldName].map((item: any) => | ||
formatDataAfterQuery(relationTableSchema.name, item, schema), | ||
formatDataAfterQuery(item, { | ||
tableName: relationTableSchema.name, | ||
schema, | ||
}), | ||
); | ||
} else { | ||
result[fieldName] = formatDataAfterQuery(relationTableSchema.name, result[fieldName], schema); | ||
result[fieldName] = formatDataAfterQuery(result[fieldName], { | ||
tableName: relationTableSchema.name, | ||
schema, | ||
}); | ||
} | ||
@@ -81,0 +91,0 @@ } |
import * as R from 'ramda'; | ||
import { MUTATION_TYPE, MUTATION_FILE_FIELDS } from '../constants'; | ||
import { tableSelectors, getTableSchemaByName } from '../selectors'; | ||
import { tableSelectors, tablesListSelectors } from '../selectors'; | ||
import { isMetaField, isFileField, isRelationField, isListField, isFilesTable } from '../verifiers'; | ||
@@ -17,2 +17,8 @@ import { formatFieldDataForMutation } from './formatFieldDataForMutation'; | ||
interface IFormatDataForMutationMeta { | ||
tableName: string; | ||
appName?: string; | ||
schema: Schema; | ||
} | ||
/** | ||
@@ -27,5 +33,4 @@ * Formats entity data for create or update mutation based on passed schema. | ||
type: MutationType, | ||
tableName: string, | ||
data: any, | ||
schema: Schema, | ||
{ tableName, schema }: IFormatDataForMutationMeta, | ||
options: IOptions = {}, | ||
@@ -41,3 +46,3 @@ ) => { | ||
const tableSchema = getTableSchemaByName(schema, tableName); | ||
const tableSchema = tablesListSelectors.getTableByName(schema, tableName); | ||
@@ -95,3 +100,3 @@ if (!tableSchema) { | ||
formatedFieldData = formatFieldDataForMutation(type, fieldSchema, formatedFieldData, schema); | ||
formatedFieldData = formatFieldDataForMutation(type, formatedFieldData, { fieldSchema, schema }); | ||
@@ -98,0 +103,0 @@ if (typeof mutate === 'function') { |
@@ -5,5 +5,10 @@ import { formatFieldDataListItem } from './formatFieldDataListItem'; | ||
export const formatFieldData = (type: MutationType, fieldSchema: FieldSchema, data: any, schema: Schema) => { | ||
const nextData = formatFieldDataListItem(type, fieldSchema, data, schema); | ||
interface IFormatFieldDataMeta { | ||
fieldSchema: FieldSchema; | ||
schema: Schema; | ||
} | ||
export const formatFieldData = (type: MutationType, data: any, { fieldSchema, schema }: IFormatFieldDataMeta) => { | ||
const nextData = formatFieldDataListItem(type, data, { fieldSchema, schema }); | ||
return { | ||
@@ -10,0 +15,0 @@ [nextData.type]: nextData.data, |
@@ -17,3 +17,12 @@ import * as R from 'ramda'; | ||
const formatFieldDataForMutation = (type: MutationType, fieldSchema: FieldSchema, data: any, schema: Schema) => { | ||
interface IFormatFieldDataForMutationMeta { | ||
fieldSchema: FieldSchema; | ||
schema: Schema; | ||
} | ||
const formatFieldDataForMutation = ( | ||
type: MutationType, | ||
data: any, | ||
{ fieldSchema, schema }: IFormatFieldDataForMutationMeta, | ||
) => { | ||
let nextData = data; | ||
@@ -23,5 +32,5 @@ | ||
if (verifiers.isListField(fieldSchema)) { | ||
nextData = formatFieldDataList(type, fieldSchema, data, schema); | ||
nextData = formatFieldDataList(type, data, { fieldSchema, schema }); | ||
} else { | ||
nextData = formatFieldData(type, fieldSchema, data, schema); | ||
nextData = formatFieldData(type, data, { fieldSchema, schema }); | ||
} | ||
@@ -28,0 +37,0 @@ } else if (verifiers.isAddressField(fieldSchema)) { |
@@ -8,5 +8,10 @@ import * as R from 'ramda'; | ||
export const formatFieldDataList = (type: MutationType, fieldSchema: FieldSchema, data: any, schema: Schema) => | ||
interface IFormatFieldDataListMeta { | ||
fieldSchema: FieldSchema; | ||
schema: Schema; | ||
} | ||
export const formatFieldDataList = (type: MutationType, data: any, { fieldSchema, schema }: IFormatFieldDataListMeta) => | ||
R.pipe( | ||
R.map(item => formatFieldDataListItem(type, fieldSchema, item, schema)), | ||
R.map(item => formatFieldDataListItem(type, item, { fieldSchema, schema })), | ||
R.groupBy(R.prop('type')), | ||
@@ -13,0 +18,0 @@ R.mapObjIndexed(R.map(R.prop('data'))), |
import * as R from 'ramda'; | ||
import { isRelationField, isFileField } from '../verifiers'; | ||
import { getTableSchemaById } from '../selectors'; | ||
import { tablesListSelectors } from '../selectors'; | ||
import { MUTATION_TYPE, SYSTEM_TABLES } from '../constants'; | ||
import { formatDataForMutation } from './formatDataForMutation'; | ||
import { SDKError, ERROR_CODES, PACKAGES } from '../errors'; | ||
import { MutationType, FieldSchema, Schema, TableSchema } from '../types'; | ||
import { MutationType, FieldSchema, Schema } from '../types'; | ||
export const formatFieldDataListItem = (type: MutationType, fieldSchema: FieldSchema, data: any, schema: Schema) => { | ||
interface IFormatFieldDataListItemMeta { | ||
fieldSchema: FieldSchema; | ||
schema: Schema; | ||
} | ||
export const formatFieldDataListItem = ( | ||
type: MutationType, | ||
data: any, | ||
{ fieldSchema, schema }: IFormatFieldDataListItemMeta, | ||
) => { | ||
let nextData = data; | ||
@@ -31,3 +40,3 @@ | ||
if (isRelationField(fieldSchema)) { | ||
const relationTableSchema = getTableSchemaById(schema, fieldSchema.relation.refTable.id); | ||
const relationTableSchema = tablesListSelectors.getTableById(schema, fieldSchema.relation.refTable.id); | ||
@@ -42,7 +51,13 @@ if (!relationTableSchema) { | ||
nextData = formatDataForMutation(MUTATION_TYPE.CREATE, relationTableSchema.name, nextData, schema); | ||
nextData = formatDataForMutation(MUTATION_TYPE.CREATE, nextData, { | ||
tableName: relationTableSchema.name, | ||
schema, | ||
}); | ||
} | ||
if (isFileField(fieldSchema)) { | ||
nextData = formatDataForMutation(MUTATION_TYPE.CREATE, SYSTEM_TABLES.FILES, nextData, schema); | ||
nextData = formatDataForMutation(MUTATION_TYPE.CREATE, nextData, { | ||
tableName: SYSTEM_TABLES.FILES, | ||
schema, | ||
}); | ||
} | ||
@@ -49,0 +64,0 @@ |
import * as R from 'ramda'; | ||
import * as tableFieldSelectors from '../selectors/tableFieldSelectors'; | ||
import { tablesListSelectors, tableFieldSelectors } from '../selectors'; | ||
import { TableSchema, QueryGeneratorConfig } from '../types'; | ||
@@ -10,5 +10,2 @@ | ||
const getTableByName = (tablesList: TableSchema[], tableName: string) => | ||
tablesList.find(({ name }) => tableName === name); | ||
const capitalizeFirstLetter = (str: string) => { | ||
@@ -20,7 +17,7 @@ return str.charAt(0).toUpperCase() + str.slice(1); | ||
tablesList: TableSchema[], | ||
tableName: string, | ||
tableId: string, | ||
config: CreateQueryColumnsListConfig = {}, | ||
prevKey: string = '', | ||
): Array<{ name: string; title: string; meta: { [key: string]: any } }> => { | ||
const { fields = [] } = getTableByName(tablesList, tableName) || {}; | ||
): Array<{ name: string; title: string; meta: object }> => { | ||
const { fields = [] } = tablesListSelectors.getTableById(tablesList, tableId) || {}; | ||
const { deep = 1, withMeta = true, flatten = true, includeColumns } = config; | ||
@@ -46,7 +43,7 @@ | ||
const isList = tableFieldSelectors.isListField(field); | ||
const refTableName = tableFieldSelectors.getRelationTableName(field); | ||
const refTableId = tableFieldSelectors.getRelationTableId(field); | ||
const refTable = tablesListSelectors.getTableById(tablesList, refTableId); | ||
const currentKeyString = prevKey ? `${prevKey}.${fieldName}` : fieldName; | ||
const title = capitalizeFirstLetter(fieldName); | ||
const refTable = getTableByName(tablesList, refTableName); | ||
@@ -70,11 +67,11 @@ const meta = { | ||
{ | ||
meta, | ||
name: currentKeyString, | ||
title, | ||
meta, | ||
}, | ||
]; | ||
} else if (isRelation && refTableName && refTable && deep > 1) { | ||
} else if (isRelation && refTableId && refTable && deep > 1) { | ||
const innerKeys = createQueryColumnsList( | ||
tablesList, | ||
refTableName, | ||
refTableId, | ||
{ deep: deep - 1, withMeta, includeColumns }, | ||
@@ -81,0 +78,0 @@ currentKeyString, |
import * as R from 'ramda'; | ||
import * as tableFieldSelectors from '../selectors/tableFieldSelectors'; | ||
import { SYSTEM_TABLES } from '../constants'; | ||
import { TableSchema, QueryGeneratorConfig } from '../types'; | ||
import { tablesListSelectors, tableFieldSelectors } from '../selectors'; | ||
import { SDKError, PACKAGES, ERROR_CODES } from '../errors'; | ||
@@ -15,12 +16,16 @@ export type CheckedRule = { | ||
const getTableByName = (tablesList: TableSchema[], tableName: string) => | ||
tablesList.find(({ name }) => tableName === name); | ||
export const createQueryString = ( | ||
tablesList: TableSchema[], | ||
tableName: string, | ||
tableId: string, | ||
queryObjectConfig: QueryGeneratorConfig = {}, | ||
prevKey: string = '', | ||
): string => { | ||
const { fields = [] } = getTableByName(tablesList, tableName) || {}; | ||
const table = tablesListSelectors.getTableById(tablesList, tableId); | ||
if (!table) { | ||
throw new SDKError(ERROR_CODES.TABLE_NOT_FOUND, PACKAGES.UTILS, `Table with id ${tableId} not found`); | ||
} | ||
const { fields, name: tableName } = table; | ||
const { | ||
@@ -66,5 +71,9 @@ deep = DEFAULT_DEPTH, | ||
const isList = tableFieldSelectors.isListField(field); | ||
const refTableName = tableFieldSelectors.getRelationTableName(field); | ||
const refTable = getTableByName(tablesList, refTableName); | ||
const isSettingsRefTable = tableFieldSelectors.getRelationTableName(field) === SYSTEM_TABLES.SETTINGS; | ||
const refTableId = tableFieldSelectors.getRelationTableId(field); | ||
const refTable = tablesListSelectors.getTableById(tablesList, refTableId); | ||
const isSettingsRefTable = | ||
tableFieldSelectors.isSystemField(field) && | ||
tableFieldSelectors.getRelationTableName(field) === SYSTEM_TABLES.SETTINGS; | ||
const currentKeyString = prevKey ? `${prevKey}.${field.name}` : field.name; | ||
@@ -78,3 +87,3 @@ let isNotEmptyRelation = false; | ||
} else if (isRelation) { | ||
if (deep <= 1 || !refTableName || !refTable) { | ||
if (deep <= 1 || !refTableId || !refTable) { | ||
fieldContent = `{ | ||
@@ -90,3 +99,3 @@ id | ||
tablesList, | ||
refTableName, | ||
refTableId, | ||
{ | ||
@@ -93,0 +102,0 @@ deep: deep - 1, |
import * as R from 'ramda'; | ||
import { SchemaNameGenerator } from '@8base/schema-name-generator'; | ||
import { tablesListSelectors } from '../selectors'; | ||
import gqlPrettier from 'graphql-prettier'; | ||
import * as tableSelectors from '../selectors/tableSelectors'; | ||
import { TableSchema, QueryGeneratorConfig } from '../types'; | ||
import { SDKError, PACKAGES, ERROR_CODES } from '../errors'; | ||
import { createQueryString } from './createQueryString'; | ||
@@ -15,75 +16,81 @@ | ||
const getTableByName = (tablesList: TableSchema[], tableName: string) => | ||
tablesList.find(({ name }) => tableName === name); | ||
const getTable = (tablesList: TableSchema[], tableId: string) => { | ||
const table = tablesListSelectors.getTableById(tablesList, tableId); | ||
if (!table) { | ||
throw new SDKError(ERROR_CODES.TABLE_NOT_FOUND, PACKAGES.UTILS, `Table schema with ${tableId} id not found.`); | ||
} | ||
return table; | ||
}; | ||
export const createTableFilterGraphqlTag = ( | ||
tablesList: TableSchema[], | ||
tableName: string, | ||
tableId: string, | ||
config: QueryTableFilterConfig = {}, | ||
) => | ||
gqlPrettier(` | ||
query ${upperFirst(tableName)}TableContent( | ||
$filter: ${SchemaNameGenerator.getFilterInputTypeName(tableName)} | ||
$orderBy: [${SchemaNameGenerator.getOrderByInputTypeName(tableName)}] | ||
$after: String | ||
$before: String | ||
$first: Int | ||
$last: Int | ||
$skip: Int | ||
) => { | ||
const table = getTable(tablesList, tableId); | ||
const { withResultData = true, ...restConfig } = config; | ||
return gqlPrettier(` | ||
query ${upperFirst(table.name)}TableContent( | ||
$filter: ${SchemaNameGenerator.getFilterInputTypeName(table.name)} | ||
$orderBy: [${SchemaNameGenerator.getOrderByInputTypeName(table.name)}] | ||
$after: String | ||
$before: String | ||
$first: Int | ||
$last: Int | ||
$skip: Int | ||
) { | ||
${config.tableContentName ? `${config.tableContentName}: ` : ''}${SchemaNameGenerator.getTableListFieldName( | ||
table.name, | ||
)}( | ||
filter: $filter | ||
orderBy: $orderBy | ||
after: $after | ||
before: $before | ||
first: $first | ||
last: $last | ||
skip: $skip | ||
) { | ||
${config.tableContentName ? `${config.tableContentName}: ` : ''}${SchemaNameGenerator.getTableListFieldName( | ||
tableName, | ||
)}( | ||
filter: $filter | ||
orderBy: $orderBy | ||
after: $after | ||
before: $before | ||
first: $first | ||
last: $last | ||
skip: $skip | ||
) { | ||
items { | ||
id | ||
${createQueryString(tablesList, tableName, { ...config })} | ||
_description | ||
} | ||
count | ||
items { | ||
id | ||
${withResultData ? createQueryString(tablesList, table.id, { ...restConfig }) : ''} | ||
_description | ||
} | ||
}`); | ||
count | ||
} | ||
}`); | ||
}; | ||
export const createTableRowCreateTag = ( | ||
tablesList: TableSchema[], | ||
tableName: string, | ||
tableId: string, | ||
config: QueryGeneratorConfig = {}, | ||
) => { | ||
const table = getTableByName(tablesList, tableName); | ||
const table = getTable(tablesList, tableId); | ||
const hasNonMetaFields = tableSelectors.hasNonMetaFields(table); | ||
const { withResultData = true, ...restConfig } = config; | ||
if (hasNonMetaFields) { | ||
return gqlPrettier(` | ||
mutation DataViewer${upperFirst(tableName)}RowCreate($data: ${SchemaNameGenerator.getCreateInputName(tableName)}!) { | ||
${SchemaNameGenerator.getCreateItemFieldName(tableName)}(data: $data) { | ||
id | ||
${createQueryString(tablesList, tableName, { | ||
withMeta: false, | ||
...config, | ||
})} | ||
} | ||
}`); | ||
mutation ${upperFirst(table.name)}Create($data: ${SchemaNameGenerator.getCreateInputName(table.name)}!) { | ||
${SchemaNameGenerator.getCreateItemFieldName(table.name)}(data: $data) { | ||
id | ||
${withResultData ? createQueryString(tablesList, tableId, { withMeta: false, ...restConfig }) : ''} | ||
} | ||
}`); | ||
} | ||
return gqlPrettier(` | ||
mutation DataViewer${upperFirst(tableName)}RowCreate { | ||
${SchemaNameGenerator.getCreateItemFieldName(tableName)} { | ||
id | ||
${createQueryString(tablesList, tableName, { | ||
withMeta: false, | ||
...config, | ||
})} | ||
} | ||
}`); | ||
mutation ${upperFirst(table.name)}Create { | ||
${SchemaNameGenerator.getCreateItemFieldName(table.name)} { | ||
id | ||
${withResultData ? createQueryString(tablesList, tableId, { withMeta: false, ...restConfig }) : ''} | ||
} | ||
}`); | ||
}; | ||
export const createTableRowCreateManyTag = (tablesList: TableSchema[], tableName: string) => { | ||
const table = getTableByName(tablesList, tableName); | ||
export const createTableRowCreateManyTag = (tablesList: TableSchema[], tableId: string) => { | ||
const table = getTable(tablesList, tableId); | ||
const hasNonMetaFields = tableSelectors.hasNonMetaFields(table); | ||
@@ -93,17 +100,15 @@ | ||
return gqlPrettier(` | ||
mutation DataViewer${upperFirst(tableName)}RowCreateMany($data: [${SchemaNameGenerator.getCreateManyInputName( | ||
tableName, | ||
)}]!) { | ||
${SchemaNameGenerator.getCreateManyItemFieldName(tableName)}(data: $data) { | ||
count | ||
} | ||
}`); | ||
mutation ${upperFirst(table.name)}CreateMany($data: [${SchemaNameGenerator.getCreateManyInputName(table.name)}]!) { | ||
${SchemaNameGenerator.getCreateManyItemFieldName(table.name)}(data: $data) { | ||
count | ||
} | ||
}`); | ||
} | ||
return gqlPrettier(` | ||
mutation DataViewer${upperFirst(tableName)}RowCreateMany { | ||
${SchemaNameGenerator.getCreateManyItemFieldName(tableName)} { | ||
return ` | ||
mutation ${upperFirst(table.name)}CreateMany { | ||
${SchemaNameGenerator.getCreateManyItemFieldName(table.name)} { | ||
count | ||
} | ||
}`); | ||
}`; | ||
}; | ||
@@ -113,38 +118,48 @@ | ||
tablesList: TableSchema[], | ||
tableName: string, | ||
tableId: string, | ||
config: QueryGeneratorConfig = {}, | ||
) => | ||
gqlPrettier(` | ||
mutation DataViewer${upperFirst(tableName)}RowUpdate($data: ${SchemaNameGenerator.getUpdateInputName( | ||
tableName, | ||
)}!, $filter: ${SchemaNameGenerator.getKeyFilterInputTypeName(tableName)}) { | ||
${SchemaNameGenerator.getUpdateItemFieldName(tableName)}(data: $data, filter: $filter) { | ||
id | ||
${createQueryString(tablesList, tableName, { | ||
withMeta: false, | ||
...config, | ||
})} | ||
} | ||
}`); | ||
) => { | ||
const table = getTable(tablesList, tableId); | ||
const { withResultData = true, ...restConfig } = config; | ||
return gqlPrettier(` | ||
mutation ${upperFirst(table.name)}Update( | ||
$data: ${SchemaNameGenerator.getUpdateInputName(table.name)}!, | ||
$filter: ${SchemaNameGenerator.getKeyFilterInputTypeName(table.name)} | ||
) { | ||
${SchemaNameGenerator.getUpdateItemFieldName(table.name)}(data: $data, filter: $filter) { | ||
id | ||
${withResultData ? createQueryString(tablesList, tableId, { withMeta: false, ...restConfig }) : ''} | ||
} | ||
}`); | ||
}; | ||
export const createTableRowQueryTag = ( | ||
tablesList: TableSchema[], | ||
tableName: string, | ||
tableId: string, | ||
config: QueryGeneratorConfig = {}, | ||
) => | ||
gqlPrettier(` | ||
query DataViewer${upperFirst(tableName)}Row($id: ID!) { | ||
${SchemaNameGenerator.getTableItemFieldName(tableName)}(id: $id) { | ||
${createQueryString(tablesList, tableName, { ...config })} | ||
} | ||
}`); | ||
) => { | ||
const table = getTable(tablesList, tableId); | ||
const { withResultData = true, ...restConfig } = config; | ||
export const createTableRowDeleteTag = (tablesList: TableSchema[], tableName: string) => | ||
gqlPrettier(` | ||
mutation DataViewer${upperFirst(tableName)}RowDelete($filter: ${SchemaNameGenerator.getKeyFilterInputTypeName( | ||
tableName, | ||
return gqlPrettier(` | ||
query ${upperFirst(table.name)}Entity($id: ID!) { | ||
${SchemaNameGenerator.getTableItemFieldName(table.name)}(id: $id) { | ||
id | ||
${withResultData ? createQueryString(tablesList, tableId, { ...restConfig }) : ''} | ||
} | ||
}`); | ||
}; | ||
export const createTableRowDeleteTag = (tablesList: TableSchema[], tableId: string) => { | ||
const table = getTable(tablesList, tableId); | ||
return gqlPrettier(` | ||
mutation ${upperFirst(table.name)}Delete($filter: ${SchemaNameGenerator.getKeyFilterInputTypeName( | ||
table.name, | ||
)}!, $force: Boolean) { | ||
${SchemaNameGenerator.getDeleteItemFieldName(tableName)}(filter: $filter, force: $force) { | ||
success | ||
} | ||
}`); | ||
${SchemaNameGenerator.getDeleteItemFieldName(table.name)}(filter: $filter, force: $force) { | ||
success | ||
} | ||
}`); | ||
}; |
import * as tableSelectors from './tableSelectors'; | ||
import * as tablesListSelectors from './tablesListSelectors'; | ||
import * as tableFieldSelectors from './tableFieldSelectors'; | ||
export { getTableSchemaByName } from './getTableSchemaByName'; | ||
export { getTableSchemaById } from './getTableSchemaById'; | ||
export { tableSelectors, tableFieldSelectors }; | ||
export { tableSelectors, tableFieldSelectors, tablesListSelectors }; |
@@ -48,2 +48,7 @@ import * as R from 'ramda'; | ||
export const isSystemField = createSelector( | ||
getTableField, | ||
R.propEq('isSystem', true), | ||
); | ||
export const getFieldName = createSelector( | ||
@@ -50,0 +55,0 @@ getTableField, |
@@ -25,7 +25,17 @@ import * as R from 'ramda'; | ||
const getFieldByIdOrEmpty: ParametricSelector<TableSchema, string, FieldSchema | {}> = createSelector( | ||
getFieldById, | ||
field => field || {}, | ||
); | ||
export const getFieldTypeById: ParametricSelector<TableSchema, string, any> = createSelector( | ||
getFieldById, | ||
getFieldByIdOrEmpty, | ||
R.propOr('', 'fieldType'), | ||
); | ||
export const isSystemField = createSelector( | ||
getTable, | ||
R.propEq('isSystem', true), | ||
); | ||
export const isRelationField: ParametricSelector<TableSchema, string, boolean> = createSelector( | ||
@@ -47,3 +57,3 @@ getFieldTypeById, | ||
export const isMetaField = createSelector( | ||
getFieldById, | ||
getFieldByIdOrEmpty, | ||
R.propEq('isMeta', true), | ||
@@ -53,3 +63,3 @@ ); | ||
export const isListField = createSelector( | ||
getFieldById, | ||
getFieldByIdOrEmpty, | ||
R.propEq('isList', true), | ||
@@ -59,3 +69,3 @@ ); | ||
export const getFieldNameById = createSelector( | ||
getFieldById, | ||
getFieldByIdOrEmpty, | ||
R.propOr('', 'name'), | ||
@@ -62,0 +72,0 @@ ); |
@@ -100,2 +100,3 @@ import { | ||
withMeta?: boolean; | ||
withResultData?: boolean; | ||
relationItemsCount?: number; | ||
@@ -102,0 +103,0 @@ includeColumns?: null | string[]; |
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
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
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
217607
3494
209