Nodejs Awesome
tip
- 最好选择 TypeScript 开发的或支持 TypeScript 的
- TypeScript 的 decorator 比 Java 的 Annotation 弱得多
- 不支持获取字段实际类型信息 - 因为不存在这样的信息
Nodejs 后端开发不太活跃
最近一两年 (2020-2021),可能是因为 Go 和 Rust 的盛行,导致 Nodejs 的后端开发弱化了,很多项目开发都不太活跃。
DB
driver pkg | db | notes |
---|---|---|
pg pg-hstore | PostgreSQL | |
mysql2 | MySQL | |
mariadb | MariaDB | |
sqlite3 | SQLite | |
better-sqlite3 | SQLite | 同步接口 |
tedious | Microsoft SQL Server | |
ibm_db | DB2 |
- better-sqlite3
- 支持自定义函数
- 支持迭代 cursor
- 支持 int64
- Convince me to use better-sqlite3 #262
- ORM
- sequelize/sequelize
- Postgres, MySQL, MariaDB, SQLite, Microsoft SQL Server.
- 因为需要支持很多 DB 类型,丢失一定的特性
- Use better-sqlite3 #11400
- prisma
- mikro-orm/mikro-orm
- MIT, Typescript
- Data Mapper, Unit of Work, Identity Map
- 使用注解定义模型
- 基于 knex
- 不支持多态关联 mikro-orm#706
- drizzle-team/drizzle-orm
- Apache-2.0, TypeScript
- TypeScript ORM that feels like writing SQL.
- typeorm/typeorm
- Future of TypeORM typeorm#3267
- 基于 typescript decoration 的 ORM
- 🚧 开发缓慢
- bookshelf/bookshelf
- 基于 knex 的 ORM
- 🚧 开发停止
- stephenh/joist-ts
- TypeScript ORM for Postgres
- sequelize/sequelize
- SQL Builder/Typed Schema/Query
- kysely-org/kysely
- type-safe typescript SQL query builder
- 替代 knex
- knex/knex
- SQL Builder
- Composite - 解耦构建最终 query 的过程
- 对于基础的访问模式提供跨库支持
- Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, Amazon Redshift
- 🚧 开发缓慢
- gajus/slonik
- PostgreSQL client with strict types
- string tag
- adelsz/pgtyped
- RobinBlomberg/kysely-codegen
- schema -> ts
- kysely-org/kysely
- aerotoad/neboa
- AGPL-3.0, TS
- Type-safe NoSQL with Node & SQLite
- Vincit/objection.js
- The future of Objection.js Vincit/objection.js#2335
- 🚧 开发停滞
- SQL-friendly ORM
- 基于 knex
- balderdashy/waterline
- 🚧 开发停止 - 2021
- dmfay/massive-js
- data mapper for Node.js and PostgreSQL
- MongoDB
- Automattic/mongoose
- object modeling
- Automattic/mongoose
- 有趣
- tndrle/node-sqlite3-wasm
- WebAssembly port of SQLite3 for Node.js with file system access
- oguimbal/pg-mem
- oguimbal/pgsql-ast-parser
- share/sharedb
- MIT, JS
- Realtime database backend based on Operational Transformation (OT)
- derbyjs/derby
- MIT, JS
- MVC framework making it easy to write realtime, collaborative applications that run in both Node.js and browsers
- tndrle/node-sqlite3-wasm
// PostgreSQL
// 支持的环境变量 https://www.postgresql.org/docs/current/libpq-envars.html
// PGHOST PGPORT PGUSER PGPASSWORD PGDATABASE PGSSLMODE PGOPTIONS
// 修改 search_path PGOPTIONS=-csearch_path=public
const { Client } = require('pg');
const client = new Client();
await client.connect();
const res = await client.query('SELECT $1::text as message', ['Hello world!']);
console.log(res.rows[0].message); // Hello world!
await client.end();
Env
- nvm
- 独立 shell 脚本
- Alpine https://github.com/nvm-sh/nvm/issues/1102#issuecomment-683291852
- n
- node module
- 👍 推荐
- pnpm setup
- Could not infer shell type.
SHELL=bash pnpm setup
export PNPM_HOME="/root/.local/share/pnpm"
curl -fsSL https://get.pnpm.io/install.sh | bash -
pnpm env use --global 16
- 不支持 MUSL
- Could not infer shell type.
- FNM Fast Node Manager
- GPLv3, Rust
- volta
- BSD-2, Rust
- asdf-vm/asdf
- MIT, Shell
- Elixir、Erlang、Node.js、Ruby
# n on alpine
apk add bash curl ca-certificates libstdc++ libgcc
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
chmod u+x n
install -t /usr/local/bin n
# 官方镜像
# N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/release ./n --arch x64-musl lts
# 国内镜像
N_NODE_MIRROR=https://npmmirror.com/mirrors/node-unofficial-builds ./n --arch x64-musl lts
# 非 ROOT
export PATH=$HOME/n/bin:$PATH
export N_PREFIX=$HOME/n
- N_PREFIX=/usr/local
- 缓存 n/versions
- bin, include, lib, share
- 非 root 可以考虑 $HOME/n
- https://npmmirror.com/mirrors/node-unofficial-builds/
- https://registry.npmmirror.com/binary.html?path=node-unofficial-builds/
Library
- job/cron/task/queue
- bull
- node-schedule
- time-based scheduling, not interval-based scheduling
- node-cron
- cron
- graphile-worker
- MIT, TS
- PostgreSQL LISTEN/NOTIFY
- timgit/pg-boss
- MIT, JS
- PostgreSQL SKIP LOCKED
- Queueing jobs in Node.js using PostgreSQL like a boss
- breejs/bree
- MIT, JS
- scheduler with worker threads
agenda/agenda- mongodb
- 独立服务
- Lightweight job scheduling
- kibertoad/toad-scheduler
- MIT, TS
- In-memory Node.js and browser job scheduler
SGrondin/bottleneck
- migration
- @mikro-orm/migrations
- knex/kysely
- sequelize/umzug
- MIT, SQL/DSL
- used by
- sequelize
- @mikro-orm/migrations https://mikro-orm.io/docs/migrations
- salsita/node-pg-migrate
- MIT, DSL
- graphile/migrate
- MIT, SQL
- 支持 current 概念
- tj/node-migrate
- MIT, Code
- WebSocket
- ws
- JS 实现
- isomorphic-ws 可用于 web
- uWebSockets.js
- 基于 uWebSockets C++ 库
- 性能好
- 针对 Linux 优化
- ws
- KV/Cache
- Performance
- runk/node-chardet
- Character encoding detection
- Concurret/Thread/Worker/async
- piscinajs/piscina
- Worker Thread Pool
- parallel-js/parallel.js
- multi-core processing utilities for Node
- greenlet
- Move an async function into its own thread
- piscinajs/piscina
- Game Server/Server
- colyseus/colyseus
- MIT, TS
- Multiplayer Framework for Node.js
- colyseus/colyseus
- RPC/Network
- grpc
- buf
- SocketCluster/socketcluster
- moleculerjs/moleculer
- MIT, JS
- Progressive microservices framework for Node.js
Web
- [hono]
- fastify
- 🌟 推荐
- @whatwg-node/server
- adapter
- express
- koa
- 🚧 开发停滞
Framework
- nestjs
- trpc -> nestjs
- TypeScript
- ts-rest
- trpc
- ecyrbe/zodios
- zod, axio, nextjs, express
- t3-oss/create-t3-app
Network
- node-fetch - NodeJS v18 后内置 fetch
- https-proxy-agent
- socks-proxy-agent
- proxy-from-env
- websocket
- ws
- [µWebSockets.js]
Server
- 日志
- Runtime
- bytenode/bytenode
- bytecode compiler for Node.js
- V8 bytecode
- bytenode/bytenode
- Scraper/爬虫
- JQuery/DOM
- CUI/Command Line/CLI/Arguement Parser
- CLI Builder
- tj/commander.js
- privatenumber/cleye
- yargs
- MIT, JS
- oclif
- MIT, TS
- by Salesforce
- 生成
- used by
- CLI Helper/terminal ui/tui/prompt
- chalk
- Color
- larswaechter/voici.js
- Print table
- execa
- winston
- logging
- sindresorhus/get-stdin
- sindresorhus/conf
- SBoudrias/Inquirer.js
- collection of common interactive command line user interfaces
- chalk
- shell/script
- sindresorhus/execa
- MIT, JS+TS
- shelljs/shelljs
- BSD-3, JS
- Portable Unix shell commands for Node.js
- google/zx
- sindresorhus/execa
- sindresorhus/awesome-nodejs
FFI
- node-ffi
- ffi-napi
- ref-napi
- sbffi
- napi-addon
- napi-addon-sb
- bun:ffi
- 🌟 最佳选择 - 使用简单,性能好,使用 tinycc jit,但是需要 bun
- bun 只能在 glibc os 运行 - 不支持 musl
bitair-org/linker.js- Access Python & C-shared from Node.js
@bitair/linker.js
- https://dyncall.org/
Sandbox/VM
- vm
- justjake/quickjs-emscripten
- v8 Isolate
- google/nsjail
- light-weight process isolation tool
- windmill-labs/windmill