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

link-query

v1.0.6

Published

A easier Multi-table Query (Collection-Join) for mongodb

Downloads

16

Readme

link-query · npm version

什么是 link-query?

一个易于使用且高效的连表查询方法,仅有2个Api,无侵入式修改,特别为初中级前端和初级后端使用,支持一对一,一对多,嵌套查询等业务中最常用的连表操作,可以极大程度的提高代码的可维护性和工作效率,使用形式上更像面向数据库的GraphQL

目录

数据库驱动支持

快速预览

Install

npm install link-query --save  

Query

const { decorator } = require('link-query');  
const UserEnhance = decorator(User);  
UserEnhance.addLinker({  
  ...  
})  
UserEnhance.linkQuery({  
  name: 1,  
  address: 1,  
  friends: {  
    name: 1,  
  },  
  blog: {  
    title: 1,  
    author: {  
      name: 1,  
    },  
    comments: {  
      content: 1,  
      createdAt: 1,  
    },  
    tags: {  
      title: 1,  
    }  
  }  
})  

result:

[{  
  name: 'people name',  
  address: 'people address',  
  friends: [{  
    name: 'friend name'  
  }],  
  blog: [{  
    title: 'blog title',  
    author: {  
      name: 'author name',  
    },  
    comments: [{  
      content: 'comment content',  
      createdAt: '2010-10-10',  
    }],  
    tags: [{  
      title: 'tag title',  
    }]  
  }]  
}]  

功能说明

decorator

增强原始collection,为其添加addLinker

const { decorator } = require('link-query');  
const CollectionEnhance = const decorator(Collection, linkConfig);  
  • Collection mongodb collection或者 mongoose model
  • linkConfig
    • [virtualFieldName] 虚拟的字段名字
    • type [default=one]
    • one 一对一关系
    • many 一对多关系
    • foreignField [default=_id] 对应的传入的collection的字段,支持深层属性,例如"user._id"
    • localField [default=_id] 对应添加虚拟字段的collection的字段 ,支持深层属性,例如"user.blog._id"
    • Collection 原始Collection或者增强后的Collection,如果是增强后的collection,可以实现嵌套查询

addLinker

添加关联关系

CollectionEnhance.addLinker(linkConfig)
  • linkConfig 同decorator(Collection, linkConfig)的linkConfig
  • 可以先增强后添加,使得属性可以嵌套查询

示例:

const UserEnhance = const decorator(User);
const BlogEnhance = const decorator(Blog);

UserEnhance.addLinker({
  blog: {
    collection: BlogEnhance,
    type: 'many',
    foreignField: 'userId'
  }
})
BlogEnhance.addLinker({
  user: {
    collection: UserEnhance,
      type: 'one',
      localField: 'userId'
  }
})

// 使用
UserEnhance.linkQuery({
  name: 1,
  blog: {
  title: 1,
  user: {
    name: 1,
    address: 1,
    blog: {
      ... // 继续嵌套
      }
    }
  }
}).fetch()

LinkQuery

使用关联关系进行查询

const handle = UserEnhance.linkQuery({
  $filters:{},
  $options:{},
  [projections]: 1,
  [virtualFieldName]: {
    $filters:{},
    $options:{},
    [projections]: 1
  }
})

await handle.fetch(); // 获取全部
await handle.fetchOne(); // 获取第一个
  • $filters Collection 的查询条件query,适用于当前调用的Collection,不支持在父级直接筛选子集的属性,~~例如{ [virtualFieldName.age] : 18 }~~
  • $options
  • projections 选取当前Collection需要的字段
  • virtualFieldName 定义在当前Collection的虚拟字段
    • $filters 同上,作用于被关联表
    • $options 同上,作用于被关联表
    • projections 同上,作用于被关联表

示例:

// 使用 
UserEnhance.linkQuery({
  $filters: {
    name: "Moyye"
  },
  $options:{
    limit: 1,
      sort: {
        createdAt: -1
      },
  },
  name: 1,
  blog: {
    title: 1,
    user: {
      name: 1,
      address: 1,
      blog: {
        ... // 继续嵌套
      }
  }
}}).fetch()

开发计划

addReducer