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

@aijs/apijson-builder

v1.2.6

Published

能够比较方便的构建常用的ApiJson请求参数,并发起请求

Downloads

67

Readme

@aijs/apijson-builder

fork自https://github.com/pengxianggui/apijson-builder
简单包装apijson,相比直接构造查询json更好记一些,使用ts编写,并且调整了一些参数和使用方式

简单查询

分页

await QueryBuilder
    // 指定要查询的表
    .by({ table: 表名称 })
    // 需要注意分页是从0开始
    .page(0, 20)
    .send();

获取多条数据

await QueryBuilder
    // 指定要查询的表
    .by({ table: 表名称 })
    // 默认多个条件 && 
    .condition(Condition.eq(字段名1, 值1))
    .condition(Condition.eq(字段名2, 值2))
    // 获取符合条件的多条数据,true默认10条
    .multi(true)
    // 可以指定条数
    // .multi(5)
    .send();

调试

await QueryBuilder
    // 指定要查询的表
    .by({ table: 表名称 })
    .debug(true)
    // 需要注意分页是从0开始
    .page(0, 20)
    .send();

搜索+分页

await QueryBuilder
    // 指定要查询的表
    .by({ table: 表名称 })
    // 查询字段名=值的数据
    .condition(Condition.eq(字段名, 值))
    // 带查询条件并分页返回,需要注意分页是从0开始
    .page(0, 20)
    .send();

模糊搜索

await QueryBuilder
    // 指定要查询的表
    .by({ table: 表名称 })
    // 查询字段名=值的数据
    .condition(Condition.like(字段名, 值))
    // 带查询条件并分页返回,需要注意分页是从0开始
    .page(0, 20)
    .send();

排序

await QueryBuilder
    // 指定要查询的表
    .by({ table: 表名称 })
    // 降序desc
    .order('create_time', true)
    // 或者增序asc
    .order('update_time', false)
    // 带查询条件并分页返回,需要注意分页是从0开始
    .page(0, 20)
    .send();

多个条件

await QueryBuilder
    // 指定要查询的表
    .by({ table: 表名称 })
    // 默认多个条件 && 
    .condition(Condition.eq(字段名1, 值1))
    .condition(Condition.eq(字段名2, 值2))
    // 获取符合条件的分页
    .page(0, 20)
    .send();

多个条件and/or

await QueryBuilder
    // 指定要查询的表
    .by({ table: 表名称 })
    // 这样返回符合 字段名1=值1 || 字段名2=值2
    .condition(Condition.eq(字段名1, 值1, true))
    .condition(Condition.eq(字段名2, 值2, true))
    .send();

// 这样返回符合 (字段名3=值3 && 字段名4=值4) && (字段名1=值1 || 字段名2=值2)
await QueryBuilder
    .by({ table: 表名称 })
    .condition(Condition.eq(字段名1, 值1, true))
    .condition(Condition.eq(字段名2, 值2, true))
    .condition(Condition.eq(字段名3, 值3))
    .condition(Condition.eq(字段名4, 值4))
    .send();

获取指定字段

不指定字段默认*获取所有字段,如果指定字段默认会附加id

await QueryBuilder
    .by({ table: 表名称 })
    .get(字段名1)
    .get(字段名2)
    .page(0, 20)
    .send();
// 或者
await QueryBuilder
    .by({ table: 表名称 })
    .get([字段名1, 字段名2])
    .get(字段名3)
    .page(0, 20)
    .send();

单表新增

await CrudBuilder
    .post(表名称)
    .set(字段1, 值1)
    .set(字段2, 值2)
    .send();
// 或者
await CrudBuilder
    .post(表名称)
    .setData({
        字段1: 值1,
        字段2: 值2,
    })
    .send();
await CrudBuilder
    .by({ table: 表名称, method: 'post' })
    .setData({
        字段1: 值1,
        字段2: 值2,
    })
    .send();

单表根据id删除

await CrudBuilder
    .delete(表名称)
    .id(id)
    .send();
// 或者批量删除
await CrudBuilder
    .delete(表名称)
    .id([id1, id2])
    .send();
// 或者批量删除
await CrudBuilder
    .delete(表名称)
    .id(id1)
    .id(id2)
    .send();
await CrudBuilder
    .by({ table: 表名称, method: 'delete' })
    .id(id)
    .send();

单表根据id修改

await CrudBuilder
    .put(表名称)
    .id(id)
    .set(字段1, 值1)
    .set(字段2, 值2)
    .send();
// 或者
await CrudBuilder
    .put(表名称)
    .id(id)
    .setData({
        字段1: 值1,
        字段2: 值2,
    })
    .send();
// 或者
await CrudBuilder
    .by({ table: 表名称, method: 'put' })
    .id(id)
    .setData({
        字段1: 值1,
        字段2: 值2,
    })
    .send();

一对一

await QueryBuilder
    .by({ table: 主表名称 })
    .debug(true)
    .condition(Condition.eq('id', 2))
    // 可以接多个child以查询多张子表
    .child(AssociativeCondition.by({
        primaryKey: 主表字段,
        table: 子表名称,
        foreignKey: 子表字段
    }))
    .send();
// 或者
await QueryBuilder
    .by({ table: 主表名称 })
    .debug(true)
    .condition(Condition.eq('id', 2))
    // 可以接多个child以查询多张子表
    .child(
        AssociativeCondition
            .table(子表名称)
            .link(主表字段, 子表字段)
    )
    .child(
        AssociativeCondition
            .table(子表名称2)
            .link(主表字段2, 子表字段2)
    )
    .send();
// => 得到的json如下
const json = {
    'Test_a2': {
        'id': 2
    },
    'Test_a1': {
        'column1@': '/Test_a2/column1'
    },
    '@explain': true
};

一对多

await QueryBuilder
    .by({ table: 主表名称 })
    .debug(true)
    .condition(Condition.eq('id', 2))
    // 可以接多个children以查询多张子表
    .children(AssociativeCondition.by({
        primaryKey: 主表字段,
        table: 子表名称,
        foreignKey: 子表字段
    }))
    .send();
// 或者
await QueryBuilder
    .by({ table: 主表名称 })
    .debug(true)
    .condition(Condition.eq('id', 2))
    // 可以接多个children以查询多张子表
    .children(
        AssociativeCondition
            .table(子表名称)
            .link(主表字段, 子表字段)
    )
    .children(
        AssociativeCondition
            .table(子表名称2)
            .link(主表字段2, 子表字段2)
    )
    .send();
// => 得到的json如下
const json = {
    'Test_a2': {
        'id': 2
    },
    '[]': {
        'Test_a1': {
            'column1@': 'Test_a2/column1'
        }
    },
    // 多张一对多子表会使用别名
    '[]:Test_a21': {
        'Test_a21': {
            'column1@': 'Test_a2/column1'
        },
        'count': 100
    },
    '@explain': true
};

一对一,一对多指定查询字段

不指定字段默认*,如果指定字段默认会附加id

await QueryBuilder
    .by({ table: 主表名称 })
    .debug(true)
    .condition(Condition.eq('id', 2))
    // 可以接多个children以查询多张子表
    .children(
        AssociativeCondition
            .table(子表名称)
            .link(主表字段, 子表字段)
            .get([字段1, 字段2])
    )
    .send();

join连表

注意join连表即使不.page(0,10)也一定是查询多条数据结果

await QueryBuilder
    .by({ table: 主表名称 })
    .debug(true)
    .condition(Condition.eq('id', 2))
    .join(
        AssociativeCondition
            .table(子表名称)
            .leftJoin(主表字段, 子表字段)
            .get([字段1, 字段2])
    )
    .page(0, 10)
    .send();
// 使用关联表
await QueryBuilder
    .by({ table: 主表名称 })
    .debug(true)
    .condition(Condition.eq('id', 2))
    .join(
        AssociativeCondition
            .table(关联表)
            .leftJoin(主表主键, 关联主表字段)
    )
    // 可以2个join,比如使用单独的关联表实现多对多
    // 第一个join是关联表,然后第二个join通过setMain关联到第一个join
    .join(
        AssociativeCondition
            .table(子表名称)
            .setMain(关联表)
            .leftJoin(关联子表字段, 子表主键)
            .get([字段1, 字段2])
    )
    .page(0, 10)
    .send();

配置

可以自定义http client,这样可以方便设置token和拦截器

全局设置

GlobalBuildConfig.setHttp(axios.create());
GlobalBuildConfig.setQueryRestUrl('/api/json/get');
GlobalBuildConfig.setCrudRestUrl('/api/json/crud');

单独配置

await QueryBuilder
    .by({
        table: 表名称,
        http: axios.create(),
        // 默认为/get
        restUrl: '/api/json/get'
    })
    .page(0, 20)
    .send();
await CrudBuilder
    .by({
        table: 表名称,
        method: 'delete',
        http: axios.create(),
        // 默认为/crud
        restUrl: '/api/json/crud'
    })
    .id(id)
    .send();