npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@amplifiers/amplify-graphql-create-model-transformer

v1.3.5

Published

Amplify GraphQL @createModel transformer. This directive is intended to be used for creating a new model once a Cognito Event is fired.

Downloads

44

Readme

amplify-graphql-create-model-transformer

Description

This directive is intended to be used for creating a new model once a Cognito Event is fired, significantly speeding up the development of your AWS Amplify projects.

@createModel

The @createModel GraphQL directive creates a new model on cognito trigger event (ex. Cognito Post Confirmation) initializing the model from the event's fields.

Definition

directive @createModel( trigger:String="postConfirmation", fieldMap: [FieldMap]) on OBJECT
input FieldMap {
    cognitoField: String!
    modelField: String!
}

Installation

npm install --save @amplifiers/amplify-graphql-create-model-transformer

Import

/amplify/backend/api/<API_NAME>/transform.conf.json

{
  "transformers": [
    "@amplifiers/amplify-graphql-create-model-transformer"
  ]
}

Usage

Make sure your project has authorization added.

Append directive to target model and specify the trigger event and the field mapping. Trigger is optional and the default value is postConfirmation. The values are mapped from the cognito event field to the model fields. Custom fields are also supported ex: {modelField:"age", cognitoField:'custom:age'}.

type User
  @model
  @auth(
    rules: [
      { allow: public, operations: [read] }
      { allow: owner, operations: [read, update], ownerField: "id" }
    ]
  )
  @createModel(
    fieldMap: [
      { modelField: "id", cognitoField: "sub" }
      { modelField: "firstName", cognitoField: "given_name" }
      { modelField: "lastName", cognitoField: "family_name" }
      { modelField: "email", cognitoField: "email" }
    ]
  ) {
  id: ID!
  firstName: String
  lastName: String
  email: AWSEmail
  todos: [Todo] @hasMany
}

type Todo
  @model
  @auth(rules: [{ allow: owner, ownerField: "owner" }])
  @createModel(
    fieldMap: [
      { modelField: "owner", cognitoField: "sub" }
      { modelField: "userTodosId", cognitoField: "sub" }
    ]
  ) {
  id: ID!
  name: String! @default(value: "My first Todo")
  description: String! @default(value: "Enter text here...")

  owner: String
  user: User @belongsTo
}

In the example above we create a new User and Todo models after user finishes the sign up flow in the front end. An important thing to note is that the User id field is also the record owner, and the value is set from the cognito sub field. In this example our sign up page has extra attributes signUpAttributes={["family_name", "given_name"]} :

alt text

All possible cognito fields: "username", "email", "phone_number", "birthdate", "email", "family_name", "given_name", "middle_name", "name", "nickname", "phone_number", "preferred_username", "profile", "website", "confirmation_code", "password", "confirm_password", "address", "gender", "locale", "picture", "updated_at", "zoneinfo"

Deploy the project and test it with a new user.

NOTE: Right now you will need to manually link the lambda function to the cognito trigger event, by going to Cognito -> Manage User Pools -> Select the pool -> Triggers -> Post confirmation -> select amplify-{shortpojectname}-CreateModelTransformer-{id}

In the future we are looking to make this automatic.

alt text

Development and Contributions

Contributions are more than welcome! Please feel free to open an issue or a pull request. Developer docs are here

License

MIT License