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

typeorm-graph-select

v1.0.0

Published

A toolset for selecting records using a graph with TypeORM.

Downloads

3

Readme

typeorm-graph-select

A lightweight layer between GraphQL & TypeORM to enable more flexible efficient queries powered by GraphQL.

Table of Contents

Motivation

When using TypeORM along side GraphQL for personal projects, I found myself needing a way dynamically query data based on GraphQL requests. The answer was typeorm-graph-select.

With typeorm-graph-select I can quickly pass my GraphQL selection and select query to mapGraph and have the GraphQL selection applied to my TypeORM query.

What It Does

  • Maps GraphQL selections to TypeORM Queries.
  • Joins nested types automatically.
  • Supports joining the same nested type more than once.
  • Selects only the fields requested.*

*Currently there are some caveats with nested selections.

What It Does Not

  • Execute queries.
  • Handle mutations.

How It Works

Naming Conventions

At the heart of typeorm-graph-select, it uses naming conventions to attempt to map the requested selection. If you have a GraphQL schema that does not match the properties or relations of your TypeORM entities, then this library may not be for you.

Assigning a Selection

typeorm-graph-select simply applies a selection to a pre-existing TypeORM query. In order to assign a selection, we first need two things, a fieldMap and a SelectQueryBuilder.

A fieldMap can be generated from GraphQLResolveInfo using libraries like graphql-fields-list. As the generation of this field map is outside the scope of this doc, I will simply provide an example of one below.

*Currently I am working on adding additional functionality to this library to assist with automating this process.

var fieldMap = [
    'users',
    'users.roles',
    'users.roles.actions',
    'name',
];

Now all we need to do is initialize a SelectQueryBuilder and pass our fieldMap to typeorm-graph-select.

// Create a SelectQueryBuilder<Organization>
const queryBuilder = await getConnection().createQueryBuilder(Organization);

// Apply a selection to the query.
let uniqueRelationalMap = mapGraph(fieldMap, queryBuilder, Organization);

// Execute the query.
const organization = queryBuilder.where("id = :id", {id: 1}).getOne();

In the above example, organization will have the requested nested relations hydrated, as well as the requested fields.

It is worth noting the uniqueRelationalMap that is returned by mapGraph, as this will contain all relational mapping information used within the query selection. This can be useful for tracking or debugging.

Contributing

All contributions are welcome, however I would ask that any and all pull requests have a corresponding issue first, just so we can figure out any details prior to work being performed.