feat!: 拆分 core / server 入口,peer deps 全部 optional

让只用 Shrimp 核心(RPC + 模块声明 + 内存 CRUD)的项目不必装
express / mongoose / bcrypt / jsonwebtoken,显著降低 install 时间。

- src/core.ts:0 重依赖入口(Shrimp / MemoryShrimpCrud / 类型)
- src/server.ts:在 core 之上再导出 ShrimpServer + MongoShrimpCrud
- package.json:加 exports 字段 + peerDependenciesMeta.optional
- Shrimp.ts:bcrypt 改 lazy import,只有走 Password 字段才触发
- MemoryShrimpCrud.populateItems:接 crudRegistry 参数(对齐 Mongo)
- ShrimpTableModule 构造时自动登记 crudRegistry,业务无需手动 wire
- 新增 test/crudRegistry.test.ts 验证自动登记 + relation 自动 populate

BREAKING: import { ShrimpServer } from 'shrimp-server' 不再可用,
请改为 'shrimp-server/server'。
This commit is contained in:
2026-05-23 16:47:18 +08:00
parent 40f311796b
commit 8e4d5620b7
11 changed files with 207 additions and 99 deletions
+3 -4
View File
@@ -4,8 +4,6 @@ import { IShrimpQuery, IShrimpCondition, IShrimpPopulate, IPageResult, isShrimpC
export class MemoryShrimpCrud<T extends { _id: string }> implements ShrimpCrud<T> {
private data: Map<string, T> = new Map()
private autoId: number = 1
/** 获取关联模块的数据源(用于 populate) */
getRelatedCrud?: (moduleKey: string) => ShrimpCrud<any> | undefined
private isCondition = isShrimpCondition
@@ -80,9 +78,10 @@ export class MemoryShrimpCrud<T extends { _id: string }> implements ShrimpCrud<T
return { list, total, page, pageSize, totalPages }
}
async populateItems(items: any[], populates: IShrimpPopulate[]): Promise<void> {
async populateItems(items: any[], populates: IShrimpPopulate[], crudRegistry?: Map<string, ShrimpCrud<any>>): Promise<void> {
if (!crudRegistry) return
for (const pop of populates) {
const relatedCrud = this.getRelatedCrud?.(pop.moduleKey)
const relatedCrud = crudRegistry.get(pop.moduleKey)
if (!relatedCrud) continue
const allRelated = (await relatedCrud.list({})).list
const labelMap = new Map(allRelated.map(item => [String(item._id), (item as any)[pop.labelKey]]))