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-query

v0.0.3

Published

typeorm query

Downloads

10

Readme

typeorm-query

The typeorm querier is based on the select query bulider like json and graphql, which can perform multi-level complex queries and precise field queries in a simple manner.

Languages

Install

You must have typeorm installed, as well as write entity classes and get connections before using it.

npm install typeorm-query --save

Usage

Simple usage example

import createTreeQueryBuilder from 'typeorm-query';

const user = await createTreeQueryBuilder()
  .query('user(id = $id): {*}')
  .param('id', 1)
  .getOne();

select query builder

const user = await connection
  .getRepository(User)
  .createQueryBuilder('user')
  .where('user.id = :id', {id: 1})
  .select('user.id', 'user.firstName', 'user.lastName', 'user.isActive')
  .getOne();

Create and use TreeQueryBuilder

  • Use default connection
createTreeQueryBuilder()
  • Use connection
createTreeQueryBuilder(connection)
  • Declare types
const user = await createTreeQueryBuilder<User>()
  .query('user: {*}')
  .getOne();
// Return type Promise<User|undefined>
console.log('firstName', user!.firstName);

Builder option

  • param

Use $ as a prefix to represent a parameter in a query statement, such as $id for a parameter named id.

Via the param method to pass in a single parameter.

createTreeQueryBuilder().param('id', id)
  • params

Via the params method to pass in all parameters.

createTreeQueryBuilder().params({id: id})
  • limit
createTreeQueryBuilder().limit(limit)
  • offset
createTreeQueryBuilder().offset(offset)
  • orderBy

Use entity.column name to get column entity,such as user.id or user.photos.id.

Sort can be empty, the default is ASC, you can use DESC.

createTreeQueryBuilder().orderBy(column, sort)

Query

  • getOne

Get a single value.

const user = await createTreeQueryBuilder()
  .query(query)
  .getOne();
  • getMany

Get multiple values and return an array.

const list = await createTreeQueryBuilder()
  .query(query)
  .getMany();
  • getCount

Get the amount of result sets.

const count = await createTreeQueryBuilder()
  .query(query)
  .getCount();

Query statement

Syntax

Use the tree structure of the entity to describe all the data that needs to be queried. Similar to the json syntax, only the part of the key is needed. After the query, the value is filled with the structure of the current key.

Entity name(conditions): {key1, key1 ... keyn(Subconditions): {child1, child2 ... childn}}

Query the firstName, lastName and profile's gender and photo of the user according to id.

user(id = $id): {firstName, lastName, profile: {gender, photo}}

elect query builder

createQueryBuilder(User)
  .leftJoinAndSelect('user.profile', 'user_profile')
  .select('user.firstName', 'user.lastName', 'user_profile.gender', 'user_profile.photo')

Condition

Conditional expressions are basically the same as sql, except for a few differences.

  • Column comparison
column = column  // Column comparison
column >= 10  // Number
column like 'str'  // String
column = true  // Bool
column = $id  // Parameter

Support comparators such as=,>=,<,<=,!=, like, etc.

  • Logical expression
id = 1 && isActive = true
id = 1 || id = 2
(id = 1 && isActive = true) || (id = 1 || id = 2)

Select All

Use * to indicate that all columns of the current entity are queried, not including associated objects, and use ! to exclude the corresponding columns.

Query all user information except password.

user: {*, !password}

Associated query

The associated query only needs to pass in the object that needs to be queried, and there is no need to manually perform select and join operations.

  • oneToOne

Associate:user -> profile

Query user and get the photo of its profile.

user: {id, profile: {photo}}

select query builder

createQueryBuilder(User)
  .leftJoinAndSelect('user.profile', 'user_profile')
  .select('user.id', 'user_profile.photo')
  • oneToMany

Associate:user -> photos

Query user and get all its photos.

user: {id, photos: {url}}

select query builder

createQueryBuilder('User')
  .leftJoinAndSelect('user.photos', 'user_photos')
  .select('user.id', 'user_photos.url')
  • manyToMany

Associate:question -> categories

Query question title and question categories.

question: {title, categories: {id, name}}

select query builder

createQueryBuilder(Question)
  .leftJoinAndSelect('question.categories', 'question_categories')
  .select('question.title', 'question_categories.id', 'question_categories.name')