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:
@@ -109,17 +109,25 @@ describe("MemoryShrimpCrud", () => {
|
||||
expect(result.total).toBe(0)
|
||||
})
|
||||
|
||||
it("populateItems 旁挂 _label", async () => {
|
||||
it("populateItems 旁挂 _label (通过 crudRegistry 解析)", async () => {
|
||||
const tagCrud = new MemoryShrimpCrud<{ _id: string; name: string }>()
|
||||
await tagCrud.add({ name: "热门" } as any)
|
||||
await tagCrud.add({ name: "促销" } as any)
|
||||
|
||||
const itemCrud = new MemoryShrimpCrud<IItem>()
|
||||
itemCrud.getRelatedCrud = (key) => key === "tag" ? tagCrud as any : undefined
|
||||
const a = await itemCrud.add({ name: "苹果", price: 5, tags: ["1", "2"] } as any)
|
||||
|
||||
const registry = new Map<string, any>([["tag", tagCrud]])
|
||||
const items = [{ ...a }] as any[]
|
||||
await itemCrud.populateItems(items, [{ moduleKey: "tag", key: "tags", labelKey: "name" }])
|
||||
await itemCrud.populateItems(items, [{ moduleKey: "tag", key: "tags", labelKey: "name" }], registry)
|
||||
expect(items[0].tags_label).toEqual(["热门", "促销"])
|
||||
})
|
||||
|
||||
it("populateItems 不传 crudRegistry 静默跳过(不抛错,不旁挂)", async () => {
|
||||
const itemCrud = new MemoryShrimpCrud<IItem>()
|
||||
const a = await itemCrud.add({ name: "苹果", price: 5, tags: ["1"] } as any)
|
||||
const items = [{ ...a }] as any[]
|
||||
await itemCrud.populateItems(items, [{ moduleKey: "tag", key: "tags", labelKey: "name" }])
|
||||
expect(items[0].tags_label).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user