@aws-amplify/data-schema
Advanced tools
Comparing version 0.12.13 to 0.12.14
@@ -5,3 +5,3 @@ import { type Prettify, __modelMeta__ } from '@aws-amplify/data-schema-types'; | ||
import type { ResolveFieldProperties } from './MappedTypes/ResolveFieldProperties'; | ||
import type { ModelIdentifier, RelationalMetadata } from './MappedTypes/ModelMetadata'; | ||
import type { ModelIdentifier, ModelSecondaryIndexes, RelationalMetadata } from './MappedTypes/ModelMetadata'; | ||
import type { ExtractNonModelTypes, NonModelTypesShape } from './MappedTypes/ExtractNonModelTypes'; | ||
@@ -25,5 +25,5 @@ export type ClientSchema<Schema extends ModelSchema<any, any>> = InternalClientSchema<Schema>; | ||
*/ | ||
type InternalClientSchema<Schema extends ModelSchema<any, any>, NonModelTypes extends NonModelTypesShape = ExtractNonModelTypes<Schema>, ResolvedSchema = ResolveSchema<Schema>, IdentifierMeta extends Record<string, any> = ModelIdentifier<SchemaTypes<Schema>>, ResolvedFields extends Record<string, unknown> = ResolveFieldProperties<Schema, NonModelTypes>, RelationshipMeta = RelationalMetadata<ResolvedSchema, ResolvedFields, IdentifierMeta>, Meta = IdentifierMeta & RelationshipMeta & NonModelTypes> = Prettify<ResolvedFields & { | ||
type InternalClientSchema<Schema extends ModelSchema<any, any>, NonModelTypes extends NonModelTypesShape = ExtractNonModelTypes<Schema>, ResolvedSchema = ResolveSchema<Schema>, IdentifierMeta extends Record<string, any> = ModelIdentifier<SchemaTypes<Schema>>, SecondaryIndexes extends Record<string, any> = ModelSecondaryIndexes<SchemaTypes<Schema>>, ResolvedFields extends Record<string, unknown> = ResolveFieldProperties<Schema, NonModelTypes>, RelationshipMeta = RelationalMetadata<ResolvedSchema, ResolvedFields, IdentifierMeta>, Meta = IdentifierMeta & SecondaryIndexes & RelationshipMeta & NonModelTypes> = Prettify<ResolvedFields & { | ||
[__modelMeta__]: Meta; | ||
}>; | ||
export {}; |
import { schema } from './ModelSchema'; | ||
import { model } from './ModelType'; | ||
import { modelIndex } from './ModelIndex'; | ||
import { id, string, integer, float, boolean, date, time, datetime, timestamp, email, json, phone, url, ipAddress } from './ModelField'; | ||
@@ -10,2 +11,2 @@ import { ref } from './RefType'; | ||
import { query, mutation, subscription } from './CustomOperation'; | ||
export { schema, model, ref, customType, enumType as enum, query, mutation, subscription, hasOne, hasMany, belongsTo, manyToMany, allow, id, string, integer, float, boolean, date, time, datetime, timestamp, email, json, phone, url, ipAddress, }; | ||
export { schema, model, modelIndex as index, ref, customType, enumType as enum, query, mutation, subscription, hasOne, hasMany, belongsTo, manyToMany, allow, id, string, integer, float, boolean, date, time, datetime, timestamp, email, json, phone, url, ipAddress, }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ipAddress = exports.url = exports.phone = exports.json = exports.email = exports.timestamp = exports.datetime = exports.time = exports.date = exports.boolean = exports.float = exports.integer = exports.string = exports.id = exports.allow = exports.manyToMany = exports.belongsTo = exports.hasMany = exports.hasOne = exports.subscription = exports.mutation = exports.query = exports.enum = exports.customType = exports.ref = exports.model = exports.schema = void 0; | ||
exports.ipAddress = exports.url = exports.phone = exports.json = exports.email = exports.timestamp = exports.datetime = exports.time = exports.date = exports.boolean = exports.float = exports.integer = exports.string = exports.id = exports.allow = exports.manyToMany = exports.belongsTo = exports.hasMany = exports.hasOne = exports.subscription = exports.mutation = exports.query = exports.enum = exports.customType = exports.ref = exports.index = exports.model = exports.schema = void 0; | ||
const ModelSchema_1 = require("./ModelSchema"); | ||
@@ -8,2 +8,4 @@ Object.defineProperty(exports, "schema", { enumerable: true, get: function () { return ModelSchema_1.schema; } }); | ||
Object.defineProperty(exports, "model", { enumerable: true, get: function () { return ModelType_1.model; } }); | ||
const ModelIndex_1 = require("./ModelIndex"); | ||
Object.defineProperty(exports, "index", { enumerable: true, get: function () { return ModelIndex_1.modelIndex; } }); | ||
const ModelField_1 = require("./ModelField"); | ||
@@ -10,0 +12,0 @@ Object.defineProperty(exports, "id", { enumerable: true, get: function () { return ModelField_1.id; } }); |
@@ -5,6 +5,11 @@ import { type UnionToIntersection, type ExcludeEmpty } from '@aws-amplify/data-schema-types'; | ||
export type ModelIdentifier<T> = { | ||
[Property in keyof T]: T[Property] extends ModelType<infer R, any> ? R['identifier'] extends any[] ? { | ||
[Property in keyof T]: T[Property] extends ModelType<infer R, any, any> ? R['identifier'] extends any[] ? { | ||
identifier: R['identifier'][number]; | ||
} : never : never; | ||
}; | ||
export type ModelSecondaryIndexes<T> = { | ||
[Property in keyof T]: T[Property] extends ModelType<infer R, any, any> ? R['secondaryIndexes'] extends any[] ? { | ||
secondaryIndexes: R['secondaryIndexes']; | ||
} : never : never; | ||
}; | ||
export type RelationalMetadata<ResolvedSchema, ResolvedFields extends Record<string, unknown>, IdentifierMeta extends Record<string, any>> = UnionToIntersection<ExcludeEmpty<{ | ||
@@ -11,0 +16,0 @@ [ModelName in keyof ResolvedSchema]: { |
@@ -56,3 +56,3 @@ import type { UnionToIntersection, LazyLoader, ExcludeEmpty } from '@aws-amplify/data-schema-types'; | ||
export type ModelImpliedAuthFields<Schema extends ModelSchema<any, any>> = { | ||
[ModelKey in keyof Schema['data']['types'] as Schema['data']['types'][ModelKey] extends EnumType<EnumTypeParamShape> ? never : Schema['data']['types'][ModelKey] extends CustomType<CustomTypeParamShape> ? never : Schema['data']['types'][ModelKey] extends CustomOperation<CustomOperationParamShape, any> ? never : ModelKey]: Schema['data']['types'][ModelKey] extends ModelType<infer Model, any> ? AllAuthFieldsForModel<Schema, Model> : object; | ||
[ModelKey in keyof Schema['data']['types'] as Schema['data']['types'][ModelKey] extends EnumType<EnumTypeParamShape> ? never : Schema['data']['types'][ModelKey] extends CustomType<CustomTypeParamShape> ? never : Schema['data']['types'][ModelKey] extends CustomOperation<CustomOperationParamShape, any> ? never : ModelKey]: Schema['data']['types'][ModelKey] extends ModelType<infer Model, any, any> ? AllAuthFieldsForModel<Schema, Model> : object; | ||
}; | ||
@@ -59,0 +59,0 @@ type AllAuthFieldsForModel<Schema extends ModelSchema<any, any>, Model extends Schema['data']['types'][keyof Schema['data']['types']]> = (Model['authorization'][number] extends never ? Schema['data']['authorization'][number] extends never ? object : ImpliedAuthFields<Schema['data']['authorization'][number]> : ImpliedAuthFields<Model['authorization'][number]>) & ImpliedAuthFieldsFromFields<Model>; |
@@ -19,3 +19,3 @@ import type { ModelType } from '../ModelType'; | ||
export type ModelTypes<Schema> = { | ||
[Model in keyof Schema as Schema[Model] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> | CustomOperation<CustomOperationParamShape, any> ? never : Model]: Schema[Model] extends ModelType<infer R, any> ? R['fields'] : never; | ||
[Model in keyof Schema as Schema[Model] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> | CustomOperation<CustomOperationParamShape, any> ? never : Model]: Schema[Model] extends ModelType<infer R, any, any> ? R['fields'] : never; | ||
}; | ||
@@ -22,0 +22,0 @@ /** |
@@ -8,3 +8,3 @@ import type { DerivedApiDefinition, SetTypeSubArg } from '@aws-amplify/data-schema-types'; | ||
import { Authorization } from './Authorization'; | ||
type SchemaContent = ModelType<ModelTypeParamShape, any> | CustomType<CustomTypeParamShape> | EnumType<EnumTypeParamShape> | CustomOperation<CustomOperationParamShape, any>; | ||
type SchemaContent = ModelType<ModelTypeParamShape, any, any> | CustomType<CustomTypeParamShape> | EnumType<EnumTypeParamShape> | CustomOperation<CustomOperationParamShape, any>; | ||
type ModelSchemaContents = Record<string, SchemaContent>; | ||
@@ -11,0 +11,0 @@ type InternalSchemaModels = Record<string, InternalModel | EnumType<any> | CustomType<any> | InternalCustom>; |
@@ -1,2 +0,2 @@ | ||
import type { Brand, SetTypeSubArg } from '@aws-amplify/data-schema-types'; | ||
import type { Brand, SetTypeSubArg, SecondaryIndexIrShape } from '@aws-amplify/data-schema-types'; | ||
import { ModelField, InternalField } from './ModelField'; | ||
@@ -8,2 +8,4 @@ import type { ModelRelationalField, InternalRelationalField } from './ModelRelationalField'; | ||
import { CustomType, CustomTypeParamShape } from './CustomType'; | ||
import { ModelIndexType, InternalModelIndexType } from './ModelIndex'; | ||
import { SecondaryIndexToIR } from './MappedTypes/MapSecondaryIndexes'; | ||
declare const brand = "modelType"; | ||
@@ -15,2 +17,3 @@ type ModelFields = Record<string, ModelField<any, any, any> | ModelRelationalField<any, string, any, any> | RefType<any, any, any> | EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape>>; | ||
identifier: string[]; | ||
secondaryIndexes: ReadonlyArray<ModelIndexType<any, any, any, any, any>>; | ||
authorization: Authorization<any, any, any>[]; | ||
@@ -21,2 +24,3 @@ }; | ||
identifier: string[]; | ||
secondaryIndexes: ReadonlyArray<InternalModelIndexType>; | ||
authorization: Authorization<any, any, any>[]; | ||
@@ -27,6 +31,10 @@ }; | ||
identifier: string[]; | ||
secondaryIndexes: ReadonlyArray<SecondaryIndexIrShape>; | ||
authorization: Authorization<any, any, any>[]; | ||
}; | ||
type SecondaryIndexFields<T extends Record<string, unknown>> = keyof { | ||
[Field in keyof T as NonNullable<T[Field]> extends string | number ? Field : never]: T[Field]; | ||
} & string; | ||
type ExtractType<T extends ModelTypeParamShape> = { | ||
[FieldProp in keyof T['fields']]: T['fields'][FieldProp] extends ModelField<infer R, any> ? R : never; | ||
[FieldProp in keyof T['fields'] as T['fields'][FieldProp] extends ModelField<any, any> ? FieldProp : never]: T['fields'][FieldProp] extends ModelField<infer R, any> ? R : never; | ||
}; | ||
@@ -39,5 +47,6 @@ type GetRequiredFields<T> = { | ||
type IdentifierType<T extends ModelTypeParamShape, Fields extends string = IdentifierFields<T>> = Array<Fields>; | ||
export type ModelType<T extends ModelTypeParamShape, K extends keyof ModelType<T> = never> = Omit<{ | ||
identifier<ID extends IdentifierType<T> = []>(identifier: ID): ModelType<SetTypeSubArg<T, 'identifier', ID>, K | 'identifier'>; | ||
authorization<AuthRuleType extends Authorization<any, any, any>>(rules: AuthRuleType[]): ModelType<SetTypeSubArg<T, 'authorization', AuthRuleType[]>, K | 'authorization'>; | ||
export type ModelType<T extends ModelTypeParamShape, K extends keyof ModelType<T> = never, ResolvedModelFields extends Record<string, unknown> = ExtractType<T>, IndexFieldKeys extends string = SecondaryIndexFields<ResolvedModelFields>> = Omit<{ | ||
identifier<ID extends IdentifierType<T> = []>(identifier: ID): ModelType<SetTypeSubArg<T, 'identifier', ID>, K | 'identifier', ResolvedModelFields>; | ||
secondaryIndexes<const Indexes extends readonly ModelIndexType<IndexFieldKeys, IndexFieldKeys, unknown, never, any>[] = readonly [], const IndexesIR extends readonly any[] = SecondaryIndexToIR<Indexes, ResolvedModelFields>>(indexes: Indexes): ModelType<SetTypeSubArg<T, 'secondaryIndexes', IndexesIR>, K | 'secondaryIndexes', ResolvedModelFields>; | ||
authorization<AuthRuleType extends Authorization<any, any, any>>(rules: AuthRuleType[]): ModelType<SetTypeSubArg<T, 'authorization', AuthRuleType[]>, K | 'authorization', ResolvedModelFields>; | ||
}, K> & Brand<object, typeof brand>; | ||
@@ -61,4 +70,5 @@ /** | ||
identifier: Array<'id'>; | ||
secondaryIndexes: []; | ||
authorization: []; | ||
}>; | ||
export {}; |
@@ -9,2 +9,3 @@ "use strict"; | ||
identifier: ['id'], | ||
secondaryIndexes: [], | ||
authorization: [], | ||
@@ -17,2 +18,6 @@ }; | ||
}, | ||
secondaryIndexes(indexes) { | ||
data.secondaryIndexes = indexes; | ||
return this; | ||
}, | ||
authorization(rules) { | ||
@@ -36,1 +41,3 @@ data.authorization = rules; | ||
exports.model = model; | ||
// TODO: rename and extract into separate file; | ||
// Will breaking apart SecondaryIndexToIR optimize it? |
@@ -52,3 +52,3 @@ "use strict"; | ||
} | ||
function scalarFieldToGql(fieldDef, identifier) { | ||
function scalarFieldToGql(fieldDef, identifier, secondaryIndexes = []) { | ||
const { fieldType, required, array, arrayRequired, default: _default, } = fieldDef; | ||
@@ -60,3 +60,5 @@ let field = fieldType; | ||
const [_pk, ...sk] = identifier; | ||
field += ` @primaryKey(sortKeyFields: ${JSON.stringify(sk)})`; | ||
field += ` @primaryKey(sortKeyFields: [${sk | ||
.map((sk) => `"${sk}"`) | ||
.join(', ')}])`; | ||
} | ||
@@ -66,2 +68,5 @@ else { | ||
} | ||
for (const index of secondaryIndexes) { | ||
field += ` ${index}`; | ||
} | ||
return field; | ||
@@ -81,2 +86,5 @@ } | ||
} | ||
for (const index of secondaryIndexes) { | ||
field += ` ${index}`; | ||
} | ||
return field; | ||
@@ -428,3 +436,3 @@ } | ||
} | ||
function processFields(fields, fieldLevelAuthRules, identifier, partitionKey) { | ||
function processFields(fields, fieldLevelAuthRules, identifier, partitionKey, secondaryIndexes = {}) { | ||
const gqlFields = []; | ||
@@ -441,3 +449,3 @@ const models = []; | ||
if (fieldName === partitionKey) { | ||
gqlFields.push(`${fieldName}: ${scalarFieldToGql(fieldDef.data, identifier)}${fieldAuth}`); | ||
gqlFields.push(`${fieldName}: ${scalarFieldToGql(fieldDef.data, identifier, secondaryIndexes[fieldName])}${fieldAuth}`); | ||
} | ||
@@ -458,3 +466,3 @@ else if (isRefField(fieldDef)) { | ||
else { | ||
gqlFields.push(`${fieldName}: ${scalarFieldToGql(fieldDef.data)}${fieldAuth}`); | ||
gqlFields.push(`${fieldName}: ${scalarFieldToGql(fieldDef.data, undefined, secondaryIndexes[fieldName])}${fieldAuth}`); | ||
} | ||
@@ -468,2 +476,29 @@ } | ||
} | ||
/** | ||
* Given InternalModelIndexType[] returns a map where the key is the model field to be annotated with an @index directive | ||
* and the value is an array of transformed Amplify @index directives with all supplied attributes | ||
*/ | ||
const transformedSecondaryIndexesForModel = (secondaryIndexes) => { | ||
const indexDirectiveWithAttributes = (sortKeys, indexName, queryField) => { | ||
if (!sortKeys.length && !indexName && !queryField) { | ||
return '@index'; | ||
} | ||
const attributes = []; | ||
if (indexName) { | ||
attributes.push(`name: "${indexName}"`); | ||
} | ||
if (sortKeys.length) { | ||
attributes.push(`sortKeyFields: [${sortKeys.map((sk) => `"${sk}"`).join(', ')}]`); | ||
} | ||
if (queryField) { | ||
attributes.push(`queryField: "${queryField}"`); | ||
} | ||
return `@index(${attributes.join(', ')})`; | ||
}; | ||
return secondaryIndexes.reduce((acc, { data: { partitionKey, sortKeys, indexName, queryField } }) => { | ||
acc[partitionKey] = acc[partitionKey] || []; | ||
acc[partitionKey].push(indexDirectiveWithAttributes(sortKeys, indexName, queryField)); | ||
return acc; | ||
}, {}); | ||
}; | ||
const schemaPreprocessor = (schema) => { | ||
@@ -516,2 +551,3 @@ const gqlModels = []; | ||
const [partitionKey] = identifier; | ||
const transformedSecondaryIndexes = transformedSecondaryIndexesForModel(typeDef.data.secondaryIndexes); | ||
const mostRelevantAuthRules = typeDef.data.authorization.length > 0 | ||
@@ -532,3 +568,3 @@ ? typeDef.data.authorization | ||
...mergeFieldObjects(fields, authFields, implicitTimestampFields(typeDef)), | ||
}, fieldLevelAuthRules, identifier, partitionKey); | ||
}, fieldLevelAuthRules, identifier, partitionKey, transformedSecondaryIndexes); | ||
topLevelTypes.push(...models); | ||
@@ -535,0 +571,0 @@ const joined = gqlFields.join('\n '); |
{ | ||
"name": "@aws-amplify/data-schema", | ||
"version": "0.12.13", | ||
"version": "0.12.14", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "repository": { |
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
211372
48
2779