UNPKG

@aws-amplify/graphql-transformer-core

Version:

A framework to transform from GraphQL SDL to AWS CloudFormation.

45 lines 2.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getStrategyDbTypeFromTypeNode = exports.getSortKeyFieldNames = exports.getTable = exports.getKeySchema = void 0; const graphql_transformer_common_1 = require("graphql-transformer-common"); const model_datasource_strategy_utils_1 = require("./model-datasource-strategy-utils"); const getKeySchema = (table, indexName) => { var _a, _b, _c, _d, _e; const globalSecondaryIndexes = (_a = table['_globalSecondaryIndexes']) !== null && _a !== void 0 ? _a : table.globalSecondaryIndexes; const localSecondaryIndexes = (_b = table['_localSecondaryIndexes']) !== null && _b !== void 0 ? _b : table.localSecondaryIndexes; return ((_e = (_d = ((_c = globalSecondaryIndexes.find((gsi) => gsi.indexName === indexName)) !== null && _c !== void 0 ? _c : localSecondaryIndexes.find((gsi) => gsi.indexName === indexName))) === null || _d === void 0 ? void 0 : _d.keySchema) !== null && _e !== void 0 ? _e : table.keySchema); }; exports.getKeySchema = getKeySchema; const getTable = (ctx, object) => { const ddbDataSource = ctx.dataSources.get(object); const tableName = graphql_transformer_common_1.ModelResourceIDs.ModelTableResourceID(object.name.value); const table = ddbDataSource.ds.stack.node.findChild(tableName); if (!table) { throw new Error(`Table not found for name ${tableName}`); } return table; }; exports.getTable = getTable; const getSortKeyFieldNames = (type) => { const sortKeyFieldNames = []; type.fields.forEach((field) => { field.directives.forEach((directive) => { var _a, _b; if (directive.name.value === 'primaryKey') { const values = (_b = (_a = directive.arguments) === null || _a === void 0 ? void 0 : _a.find((arg) => arg.name.value === 'sortKeyFields')) === null || _b === void 0 ? void 0 : _b.value; if (values) { sortKeyFieldNames.push(...values.values.map((it) => it.value)); } } }); }); return sortKeyFieldNames; }; exports.getSortKeyFieldNames = getSortKeyFieldNames; const getStrategyDbTypeFromTypeNode = (type, ctx) => { const baseTypeName = (0, graphql_transformer_common_1.getBaseType)(type); const strategy = (0, model_datasource_strategy_utils_1.getModelDataSourceStrategy)(ctx, baseTypeName); return strategy.dbType; }; exports.getStrategyDbTypeFromTypeNode = getStrategyDbTypeFromTypeNode; //# sourceMappingURL=schema-utils.js.map