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
- sequelize/sequelize
- ORM
- Postgres, MySQL, MariaDB, SQLite, Microsoft SQL Server.
- 因为需要支持很多 DB 类型,丢失一定的特性
- Use better-sqlite3 #11400
- prisma
- SQL Builder/Typed Schema/Query
- 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
- kysely-org/kysely
- type-safe typescript SQL query builder
- RobinBlomberg/kysely-codegen
- schema -> ts
- knex/knex
- mikro-orm/mikro-orm
- MIT, Typescript
- Data Mapper, Unit of Work, Identity Map
- 使用注解定义模型
- 基于 knex
- 不支持多态关联 mikro-orm#706
- stephenh/joist-ts
- TypeScript ORM for Postgres
- drizzle-team/drizzle-orm
- Apache-2.0, TypeScript
- TypeScript ORM that feels like writing SQL.
- 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
- typeorm/typeorm
- Future of TypeORM typeorm#3267
- 基于 typescript decoration 的 ORM
- 🚧 开发缓慢
- bookshelf/bookshelf
- 基于 knex 的 ORM
- 🚧 开发停止
- 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
- 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