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

amplify-graphql-typesense-transformer

v0.1.1

Published

Amplify GraphQL @typesense transformer

Downloads

21

Readme

Code Coverage

Amplify GraphQL Typesense Transformer

Enhance your Amplify API with serverless search capabilities using the Typesense transformer.

Overview

The Amplify GraphQL Typesense Transformer allows you to seamlessly integrate Typesense, a modern, open-source search engine designed with cutting-edge algorithms and machine learning, into your AWS Amplify API. It offers a privacy-friendly approach and is optimized for the latest hardware capabilities.

Getting Started

Installation

npm install amplify-graphql-typesense-transformer

Integration

  1. Update Configuration: Modify the transform.conf.json file located at /amplify/backend/api/<API_NAME>/.
{
    "transformers": [
        "amplify-graphql-typesense-transformer"
    ]
}
  1. Set Up Connection Parameters: Update /amplify/backend/api/<API_NAME>/parameters.json with your Typesense connection details, available in your Typesense Cloud dashboard.
{
  "TypesenseApiKey": "<API_KEY>",
  "TypesenseHost": "xxx.a1.typesense.net",
  "TypesensePort": "443",
  "TypesenseProtocol": "https"
}
  1. Add Directive: Attach the @typesense directive to the desired model for indexing. This will automatically create and manage a Typesense collection for the model.
type Todo @model @typesense {
  id: ID!
  name: String!
  description: String
}
  1. Deploy Changes: Apply the modifications to your Amplify API.
amplify push

Usage

  1. Create a Record: Create a record using the GraphQL API.
import { API } from "aws-amplify";
import * as mutations from "../graphql/mutations";
import { GraphQLQuery } from "@aws-amplify/api";
import { CreateBlogMutation } from "../API";

const blogDetails: CreateBlogInput = {
  name: text,
};

const newTodo = await API.graphql<GraphQLQuery<CreateBlogMutation>>({
  query: mutations.createBlog,
  variables: { input: blogDetails },
});
  1. Search Record with a Search Query: The transformer also creates your GraphQL queries with a search query for each model marked with the @typesense directive. You can learn about the query syntax from Typesense Search API.
import { API } from "aws-amplify";
import * as queries from "../graphql/queries";
import { GraphQLQuery } from "@aws-amplify/api";
import { SearchBlogsQuery } from "../API";

const results = await API.graphql<GraphQLQuery<SearchBlogsQuery>>({
    query: queries.searchBlogs,
    variables: {
        searchParameters: JSON.stringify({
          q: `*${text}*`,
          query_by: "name",
        }),
    },
});

GraphQL Schema changes

The transformer will automatically create a Typesense collection for each model with the @typesense directive, and the schema will be auto detected by Typesense. This means that any non distructive changes, like adding a new field to the GraphQL schema will be reflected in the Typesense collection.

Optional: Amplify Cloud CI/CD

If you encounter an error related to amplify-graphql-typesense-transformer not being found, you may need to add the following to your amplify.yml file in the cloud console:

backend:
  phases:
    build:
      commands:
        - npm install amplify-graphql-typesense-transformer
        - amplifyPush --simple

Example Projects

  • Check out this project for a searchable blog example.
  • Check out this post for an indepth tutorial on how to build a searchable low-code blog with AWS Amplify and Typesense.

How It Works

The @typesense directive establishes a dedicated Lambda function for each GraphQL API in your Amplify project. It then links DynamoDB streams from the corresponding tables to this function. Upon receiving a stream, the function processes the fields as defined, transforms the record into a Typesense payload, and updates (or creates if not present) the Typesense collection named after the model.

Read more about building custom directives here.

Contribution

We appreciate and welcome contributions! If you have improvements or features to suggest, please feel free to submit a pull request.

Development Workflow

Transformer

  • Set up an Amplify project and integrate an API.
  • Add the transformer using an absolute path in amplify/backend/api/<API_NAME>/transform.conf.json:
{
    "transformers": [
        "file:///absolute/path/to/graphql-Typesense-transform/"
    ]
}
  • Rebuild the transformer: npm run compile.
  • Use amplify api gql-compile to inspect stack outputs without initiating the push process.
  • Review amplify/backend/api/<API>/build/stacks/TypesenseStack for expected results.

License

Distributed under the Apache 2.0 License.