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

@ctsy/model

v2.0.47

Published

> 封装的Sequelize作为数据库操作底层,对Sequelize的部分操作进行了简化,语法结构类似于ThinkPHP3.2结构。 需要依赖于 castle-config/castle-controller/sequelize,其它依赖请参见相关库的依然范围 # 典型基础用法 ```typescript //获取模型对象 let model = await this.M('Sex'); //启动事务 =1 await this.startTrans(); //添加数据 // INSERT INTO se

Downloads

216

Readme

CastleModel数据库操作类

封装的Sequelize作为数据库操作底层,对Sequelize的部分操作进行了简化,语法结构类似于ThinkPHP3.2结构。 需要依赖于 castle-config/castle-controller/sequelize,其它依赖请参见相关库的依然范围

典型基础用法

//获取模型对象
let model = await this.M('Sex');
//启动事务 =1
await this.startTrans();
//添加数据
// INSERT INTO sex(UID,Sex) VALUES(6,1);
await model.add({ UID: 6, Sex: 1 });
//批量添加
// INSERT INTO sex(UID,Sex) VALUES(6,1),(8,1);
await model.addAll([
    {
        UID: 6, Sex: 1
    },
    {
        UID: 8, Sex: 1
    },
])
//更新数据
// UPDATE sex SET Sex=100 WHERE UID > 7 LIMIT 1;
await model.where({ UID: { gt: 7 } }).limit(1).save({ Sex: 100 });
//自增自减处理,UID>7的Sex全部-1,UID+1
// UPDATE sex SET Sex=Sex-1,UID=UID+1 WHERE UID > 7;
await model.where({ UID: { gt: 7 } }).incOrDec({ Sex: -1, UID: 1 })
//当存在DTime时自动做软删除,否则就是硬删除
// DELETE FROM sex WHERE UID > 8;
// UPDATE sex SET DTime = now() WHERE UID > 8;
await model.where({ UID: { gt: 8 } }).del()
//查询单个
// SELECT * FROM sex WHERE UID > 8;
await model.where({ UID: { gt: 8 } }).find()
//分页查询多个
// SELECT * FROM sex WHERE UID > 1 LIMIT 1,10;
await model.where({ UID: { gt: 1 } }).page(1, 10).select()
// 查询并统计
// SELECT * FROM sex WHERE UID > 1;
// SELECT COUNT(*) FROM sex WHERE UID > 1;
await model.where({ UID: { gt: 1 } }).selectAndCount();
//指定字段查询
// SELECT UID FROM sex LIMIT 1;
await model.fields('UID').find()
//排除字段查询
// SELECT Sex FROM sex LIMIT 1;
await model.fields('UID', true).find()
//批量条件更新,仅支持MySQL
// UPDATE sex SET Sex = CASE UID WHEN 1 THEN 2 WHEN 5 THEN 10 WHEN 7 THEN Sex+5 ELSE Sex END WHERE UID IN (1,5,7);
await model.caseSave([{ field: { case: 'UID', save: "Sex" }, data: { 1: 2, 5: 10, 7: "`Sex`+5" } }])
//执行自定义SQL查询,通过__DB_PREFIX__注入表前缀
await model.query(`SELECT * FROM __DB_PREFIX__sex`)
//执行自定义SQL,
await model.exec(`UPDATE Sex SET UID=UID+1`, 'UPDATE')
//执行存储过程或函数
await model.exec(`CALL reset();`, 'RAW')
//查询单个字段且只要一个,返回值为单个字段的值
// SELECT Sex FROM sex LIMIT 1;
await model.getFields('Sex');
//查询单个字段且返回数组,返回值为该字段的数据
// SELECT Sex FROM sex;
await model.getFields('Sex', true);
//查询多个字段,以第一个字段为键返回,
// SELECT UID,Sex FROM sex;
let rs:{
    [index:string]:{
        UID:number,Sex:number
        }
} = await model.getFields('UID,Sex', true);
//支持排序
// SELECT * FROM sex ORDER BY UID DESC;
await model.order('UID DESC').select();
//支持group操作
// SELECT SUM(UID) AS UID FROM sex GROUP BY UID;
await model.group(['UID']).fields([[Sequelize.fn('sum', Sequelize.col('UID')), 'UID']]).select()
//支持SUM等统计函数处理
// SELECT SUM(UID) AS UID FROM sex GROUP BY Sex;
await model.fnField(DbFn.SUM, 'UID', 'UID').group(['Sex']).select();
//支持limit,不适用page方法时
// SELECT * FROM sex LIMIT 1;
await model.limit(1).select();
//支持直接封装的SUM操作
// SELECT SUM(UID) AS UID FROM sex GROUP BY UID;
await model.group(['UID']).sum('UID')
//支持自动检测是否存在,若不存在则自动添加
// SELECT * FROM sex WHERE UID=10 AND Sex=1;
// INSERT INTO sex(UID,Sex) VALUE(10,1);
await model.addIfNotExist({ UID: 10, Sex: 1 })
//也可以自定义存在检测条件
// SELECT * FROM sex WHERE UID=11;
// INSERT INTO sex(UID,Sex) VALUE(10,1);
await model.addIfNotExist({ UID: 11, Sex: 1 }, { UID: 11 })
//提交事务,两种方式都行,此处的this指向 BaseController
// -1 == 0 具体执行commit,否则直接跳过
await this.commit();
await model.commit()
//回滚事务,两种方式都行
// 具体执行rollback,否则直接跳过
await this.rollback();
await model.rollback()
//支持嵌套事务 +1
await this.startTrans();
await this.startTrans();
await this.startTrans();
await this.commit();
await this.commit();
await this.commit();
//当提交次数=开起次数时最后一次提交,之后的commit会报错
await this.commit();
//若中途发生一次rollback调用则会直接抛出错误
await this.rollback()