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

type-safe-select

v1.0.34

Published

For TypeORM repo:

Downloads

17

Readme

Usage

For TypeORM repo:

const repo = getRepository(Test1Entity);
const queryHelper = new QueryHelper(repo);

const result = await queryHelper.selectSpecific({
    select: {
        selected1: (el) => el.id,
        selected2: (el) => el.test2.id,
        selected3: (el) => el.test2.test1.id,
    },
    where: {
        condition: {
            pathGetter: (el) => el.test2.test1.id,
            operation: Equals(1),
        },
    },
});

Result will have type:

const result: {
    name1: typeof el.name,
    name2: typeof el.test1.name,
    name3: typeof el.test1.test2.name,
    id: typeof el.test1.id
    }[]

Example of getter:

(el)=> el.<association_1>.<association_2>.<column>

Last field of getter is column, and not association.

Getter fields that are not the last one are associations, and not getters.

Requirements:

  1. TypeORM
  2. Properly defined entities

Example:

export class Entity1 {
    @PrimaryGeneratedColumn()
    id: number

    // column
    @Column()
    field1: number

    // association
    @ManyToOne(() => Entity2, (el)=>el.return_field_1)
    field2: Entity2

    // association
    @OneToMany(() => Entity3, (el)=>el.return_field_2)
    field2: Entity3[]

    // association
    @OneToOne(() => Entity4, (el)=>el.return_field_3)
    field3: Entity4
}

Why?

  1. reason:

    If you use rename functionality (F2) of Visual Code Editor to rename field of an class of interface, it will not take strings into consideration. If some strings have parts that need to be matching those fields you will need to manualy change those parts of strings. Solution? Instead of using strings, use different types of writing down the query which will be updated by rename functionality (F2).

  2. reason:

    When you use query.getRawMany() the return type is unknown[], so you have to cast it. That requires to define your interface and make sure it maches fields specified in query. This code does not require that, because return type is defined in manner that it is being picked up from passed parameters.

Bonus

const path = getPath<Entity1>((el) => el.field1.field2.field3)

// path:  ["field1", "field2", "field3"]

Example 1

const repo = getRepository(Test1Entity);

const queryHelper = new QueryHelper(repo);

const found = await queryHelper.SelectSpecific(
    {
        field1: (el) => el.test2.test1.id,
        field2: (el) => el.test2.id,
        field3: (el) => el.id,
    },
    {
        and: [
            {
            or: [
                {
                condition: {
                    pathGetter: (el) => el.field1,
                    operation: {
                        value: 1,
                        stringMaker: (alias, field, varName) => `${alias}.${field} < ${varName}`,
                        },
                },
                },
                {
                condition: {
                    pathGetter: (el) => el.field1,
                    operation: {
                        value: 0,
                        stringMaker: (alias, field, varName) => `${alias}.${field} > ${varName}`,
                    },
                },
                },
            ],
            },
            {
            condition: {
                pathGetter: (el) => el.test2.field2,
                operation: {
                    value: 1,
                    stringMaker: (alias, field, varName) => `${alias}.${field} = ${varName}`,
                },
            },
            },
        ],
        }
    );
}

Example 2.

import { Equals, OperatorData } from "type-safe-select";

const operatorData1 = Equals(1);

// It returns same as the following:
const operatorData2: OperatorData<number> = {
    value: 1,
    stringMaker: (alias, field, varName) => `${alias}.${field} = ${varName}`,
};

Example 3.

const result = await queryHelper.selectGroupBy({
    select: {
        selected1: (el) => el.groupBy.field1,
        selected2: (el) => el.groupBy.field2,
        count1: (el) => el.count,
        sum1: (el) => el.sum.field2,
    },
    where: {
        condition: {
            pathGetter: (el) => el.field1,
            operation: Equals("D"),
        },
    },
});