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

@yjc/model-pattern

v0.5.3

Published

Divides Model layer into three parts: Page Service, Data Service and DAO. Inspired by Baidu ODP Framework.

Downloads

13

Readme

@yjc/model-pattern

用以规范模型层逻辑。
模型层即MVC中的“M”。对于一些纯后端的脚本,可能会没有“C”或“V”,但模型层逻辑很重,有必要划分清晰的业务层级。

分层参考了百度内部框架ODP。

当前只实现简单的功能,待实践充分后补充更多的功能。

用例1:使用数据库

src/demo/script/use-mysql.ts

用例2:扩展基类

src/demo/script/extend-base.ts

用例3:使用全局错误码

src/demo/script/use-error-codes.ts

模型层业务逻辑分层

src/demo/use-mysql目录结构为例。

  • pub model层通用类库,可同层调用。
  • dao 无业务逻辑原子操作,不可同层调用
    • 可调用pub
    • data-service调用。
    • 需继承基类ModelDao
  • data-service 主体业务逻辑封装。
    • 可调用pubdao
    • page-service调用。
    • 需继承基类ModelDataService
  • page-service 由外部调用的业务逻辑封装。
    • 可调用pubdata-service
    • 由外部逻辑调用。
    • 需继承基类ModelPageService

调用路径: dao > data-service > page-service >> External-Logic 不可跨层调用

目录结构生成脚本

cd @yjc/model-pattern
npm run mkdirs your-model-dir

ModelPageService.context

ModelPageService.context是跨层的上下文对象,在page-service实例化,透传至dao
典型的场景是在page-service创建数据库连接,这个连接全流程复用,最后在page-service销毁。

ModelDao.mysql

ModelDao.mysql是封装后的MySQL连接池对象(用法参考mysql)。

引用的模块@yjc/mysql,从mysql2/promise改造而来。

ErrorCodes, ExternalError

  • defineErrorCodes 定义全局错误码。
  • assert 断言函数,抛出的错误对象包含ExternalError.eCodeExternalError.eTip,用于隐藏出错细节、对用户提供友好提示。