soma-core 1.1.1 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/soma-db.cjs +13 -0
- package/core/build/db/cli.ts +10 -0
- package/core/build/db/emit-entities.ts +45 -0
- package/core/build/db/emit-mappers.ts +175 -0
- package/core/build/db/emit-prisma-schema.ts +92 -0
- package/core/build/db/generate.ts +166 -0
- package/core/build/db/ir.ts +75 -0
- package/core/build/db/load-db-yaml.ts +241 -0
- package/core/server/index.ts +15 -0
- package/core/server/infra/db/errors.ts +11 -0
- package/core/server/infra/db/mapper/PrismaSomaMapper.ts +313 -0
- package/core/server/infra/db/mapper/SomaBaseMapper.ts +23 -0
- package/core/server/infra/db/mapper/SomaMapperHooks.ts +13 -0
- package/core/server/infra/db/query/SomaQuery.ts +149 -0
- package/core/server/infra/db/service/SomaDbService.ts +64 -0
- package/core/server/infra/db/soma-db.ts +56 -6
- package/core/server/infra/db/types.ts +21 -0
- package/core/server/mock/node/cli.mjs +4 -2
- package/dist/cli/soma.js +20 -1
- package/dist/types/build/db/cli.d.ts +2 -0
- package/dist/types/build/db/emit-entities.d.ts +5 -0
- package/dist/types/build/db/emit-mappers.d.ts +15 -0
- package/dist/types/build/db/emit-prisma-schema.d.ts +2 -0
- package/dist/types/build/db/generate.d.ts +7 -0
- package/dist/types/build/db/ir.d.ts +58 -0
- package/dist/types/build/db/load-db-yaml.d.ts +14 -0
- package/dist/types/server/index.d.ts +8 -1
- package/dist/types/server/infra/db/errors.d.ts +5 -0
- package/dist/types/server/infra/db/mapper/PrismaSomaMapper.d.ts +77 -0
- package/dist/types/server/infra/db/mapper/SomaBaseMapper.d.ts +20 -0
- package/dist/types/server/infra/db/mapper/SomaMapperHooks.d.ts +12 -0
- package/dist/types/server/infra/db/query/SomaQuery.d.ts +42 -0
- package/dist/types/server/infra/db/service/SomaDbService.d.ts +24 -0
- package/dist/types/server/infra/db/soma-db.d.ts +15 -1
- package/dist/types/server/infra/db/types.d.ts +18 -0
- package/next.config.js +22 -1
- package/package.json +6 -3
- package/docs//345/256/242/346/210/267/347/253/257/346/234/215/345/212/241/347/253/257/351/232/224/347/246/273/346/226/271/346/241/210.md +0 -399
- package/docs//351/232/224/347/246/273/346/216/245/345/205/245/345/217/230/346/233/264/346/226/271/346/241/210.md +0 -250
- package/docs//351/241/265/351/235/242JSON/346/250/241/345/274/217/346/216/245/345/205/245/346/214/207/345/215/227.md +0 -141
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chainable query builder (MyBatis-Plus QueryWrapper style).
|
|
3
|
+
* Compiles to a Prisma-compatible where / orderBy / select shape.
|
|
4
|
+
*/
|
|
5
|
+
import 'server-only';
|
|
6
|
+
import type { SomaFieldPath } from '../types';
|
|
7
|
+
export declare class SomaQuery<T = Record<string, unknown>> {
|
|
8
|
+
private readonly conditions;
|
|
9
|
+
private readonly orGroups;
|
|
10
|
+
private readonly order;
|
|
11
|
+
private selectFields;
|
|
12
|
+
private includeDeletedFlag;
|
|
13
|
+
eq(field: SomaFieldPath<T>, value: unknown): this;
|
|
14
|
+
ne(field: SomaFieldPath<T>, value: unknown): this;
|
|
15
|
+
in(field: SomaFieldPath<T>, values: unknown[]): this;
|
|
16
|
+
notIn(field: SomaFieldPath<T>, values: unknown[]): this;
|
|
17
|
+
like(field: SomaFieldPath<T>, value: string): this;
|
|
18
|
+
gt(field: SomaFieldPath<T>, value: unknown): this;
|
|
19
|
+
ge(field: SomaFieldPath<T>, value: unknown): this;
|
|
20
|
+
lt(field: SomaFieldPath<T>, value: unknown): this;
|
|
21
|
+
le(field: SomaFieldPath<T>, value: unknown): this;
|
|
22
|
+
between(field: SomaFieldPath<T>, start: unknown, end: unknown): this;
|
|
23
|
+
isNull(field: SomaFieldPath<T>): this;
|
|
24
|
+
isNotNull(field: SomaFieldPath<T>): this;
|
|
25
|
+
/** Nested AND group: q.and(sub => sub.eq(...).eq(...)) */
|
|
26
|
+
and(builder: (q: SomaQuery<T>) => void): this;
|
|
27
|
+
/** Nested OR group: q.or(sub => sub.eq(...).eq(...)) — OR among conditions in the subgroup */
|
|
28
|
+
or(builder: (q: SomaQuery<T>) => void): this;
|
|
29
|
+
orderByAsc(field: SomaFieldPath<T>): this;
|
|
30
|
+
orderByDesc(field: SomaFieldPath<T>): this;
|
|
31
|
+
select(...fields: Array<SomaFieldPath<T>>): this;
|
|
32
|
+
/** Include soft-deleted rows when mapper has logicDelete configured */
|
|
33
|
+
includeDeleted(flag?: boolean): this;
|
|
34
|
+
/** @internal */
|
|
35
|
+
shouldIncludeDeleted(): boolean;
|
|
36
|
+
/** @internal Prisma where */
|
|
37
|
+
toPrismaWhere(): Record<string, unknown> | undefined;
|
|
38
|
+
/** @internal Prisma orderBy */
|
|
39
|
+
toPrismaOrderBy(): Array<Record<string, 'asc' | 'desc'>> | undefined;
|
|
40
|
+
/** @internal Prisma select object */
|
|
41
|
+
toPrismaSelect(): Record<string, true> | undefined;
|
|
42
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Service layer over SomaBaseMapper (MyBatis-Plus IService style).
|
|
3
|
+
*/
|
|
4
|
+
import 'server-only';
|
|
5
|
+
import type { SomaBaseMapper } from '../mapper/SomaBaseMapper';
|
|
6
|
+
import type { SomaQuery } from '../query/SomaQuery';
|
|
7
|
+
import type { PageRequest, PageResult } from '../types';
|
|
8
|
+
export declare class SomaDbService<T extends object, ID = string> {
|
|
9
|
+
protected readonly baseMapper: SomaBaseMapper<T, ID>;
|
|
10
|
+
constructor(baseMapper: SomaBaseMapper<T, ID>);
|
|
11
|
+
getMapper(): SomaBaseMapper<T, ID>;
|
|
12
|
+
selectById(id: ID): Promise<T | null>;
|
|
13
|
+
selectBatchIds(ids: ID[]): Promise<T[]>;
|
|
14
|
+
selectOne(query?: SomaQuery<T>): Promise<T | null>;
|
|
15
|
+
selectList(query?: SomaQuery<T>): Promise<T[]>;
|
|
16
|
+
selectCount(query?: SomaQuery<T>): Promise<number>;
|
|
17
|
+
selectPage(page: PageRequest, query?: SomaQuery<T>): Promise<PageResult<T>>;
|
|
18
|
+
save(entity: Partial<T>): Promise<T>;
|
|
19
|
+
saveBatch(entities: Partial<T>[]): Promise<T[]>;
|
|
20
|
+
updateById(id: ID, patch: Partial<T>): Promise<T>;
|
|
21
|
+
update(query: SomaQuery<T>, patch: Partial<T>): Promise<number>;
|
|
22
|
+
removeById(id: ID): Promise<boolean>;
|
|
23
|
+
remove(query: SomaQuery<T>): Promise<number>;
|
|
24
|
+
}
|
|
@@ -1,21 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Server-only DB capability for Host `api/service`.
|
|
3
3
|
*
|
|
4
|
-
* Framework provides registration / singleton
|
|
4
|
+
* Framework provides registration / singleton / provider lifecycle.
|
|
5
5
|
* Host owns Prisma (or other) client, schema, and queries.
|
|
6
6
|
* Never import from Domain / `'use client'` code.
|
|
7
7
|
*/
|
|
8
8
|
import 'server-only';
|
|
9
|
+
export interface ISomaDbProvider<TClient = unknown> {
|
|
10
|
+
getClient(): TClient;
|
|
11
|
+
disconnect?(): Promise<void> | void;
|
|
12
|
+
}
|
|
9
13
|
/**
|
|
10
14
|
* Register the Host DB client (typically PrismaClient).
|
|
11
15
|
* Call once from a server-only Host module such as `lib/db.ts`.
|
|
12
16
|
*/
|
|
13
17
|
export declare function setSomaDb<T>(client: T): T;
|
|
18
|
+
/**
|
|
19
|
+
* Register a provider (client + optional disconnect).
|
|
20
|
+
*/
|
|
21
|
+
export declare function registerSomaDbProvider<T>(provider: ISomaDbProvider<T>): T;
|
|
14
22
|
/**
|
|
15
23
|
* Read the registered DB client.
|
|
16
24
|
* Throws if `setSomaDb` / `createSomaDbSingleton` has not run yet.
|
|
17
25
|
*/
|
|
18
26
|
export declare function getSomaDb<T = unknown>(): T;
|
|
27
|
+
/** Disconnect provider if registered, then clear state. */
|
|
28
|
+
export declare function disconnectSomaDb(): Promise<void>;
|
|
19
29
|
/** Test / hot-reload helper. */
|
|
20
30
|
export declare function resetSomaDb(): void;
|
|
21
31
|
/**
|
|
@@ -29,3 +39,7 @@ export declare function resetSomaDb(): void;
|
|
|
29
39
|
* export const db = createSomaDbSingleton(() => new PrismaClient());
|
|
30
40
|
*/
|
|
31
41
|
export declare function createSomaDbSingleton<T>(factory: () => T): T;
|
|
42
|
+
/**
|
|
43
|
+
* Scoped client for tests: set for duration of fn, then restore previous.
|
|
44
|
+
*/
|
|
45
|
+
export declare function withSomaDb<T, R>(client: T, fn: () => Promise<R> | R): Promise<R>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared DB paging types (MyBatis-Plus style).
|
|
3
|
+
* Server-only; never import from Domain / client code.
|
|
4
|
+
*/
|
|
5
|
+
import 'server-only';
|
|
6
|
+
export interface PageRequest {
|
|
7
|
+
/** 1-based page index */
|
|
8
|
+
page: number;
|
|
9
|
+
/** page size */
|
|
10
|
+
size: number;
|
|
11
|
+
}
|
|
12
|
+
export interface PageResult<T> {
|
|
13
|
+
records: T[];
|
|
14
|
+
total: number;
|
|
15
|
+
page: number;
|
|
16
|
+
size: number;
|
|
17
|
+
}
|
|
18
|
+
export type SomaFieldPath<T> = Extract<keyof T, string> | (string & {});
|
package/next.config.js
CHANGED
|
@@ -125,9 +125,30 @@ function readEnvTranspilePackages() {
|
|
|
125
125
|
.filter(Boolean);
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
+
function readHostSourcePackageNames() {
|
|
129
|
+
const packageJson = readJson(path.join(hostDir, 'package.json'));
|
|
130
|
+
const dependencies = {
|
|
131
|
+
...(packageJson?.dependencies ?? {}),
|
|
132
|
+
...(packageJson?.devDependencies ?? {}),
|
|
133
|
+
};
|
|
134
|
+
return Object.entries(dependencies)
|
|
135
|
+
.filter(([name]) => {
|
|
136
|
+
const packageRoot = path.join(hostDir, 'node_modules', ...name.split('/'));
|
|
137
|
+
const sourcePackage = readJson(path.join(packageRoot, 'package.json'));
|
|
138
|
+
return sourcePackage?.soma?.source === true
|
|
139
|
+
|| fs.existsSync(path.join(packageRoot, 'shell', 'html'));
|
|
140
|
+
})
|
|
141
|
+
.map(([name]) => name);
|
|
142
|
+
}
|
|
143
|
+
|
|
128
144
|
const hostFileDependencyEntries = readHostFileDependencyEntries();
|
|
129
145
|
const transpilePackages = [
|
|
130
|
-
...new Set([
|
|
146
|
+
...new Set([
|
|
147
|
+
'soma-core',
|
|
148
|
+
...hostFileDependencyEntries.map((entry) => entry.name),
|
|
149
|
+
...readHostSourcePackageNames(),
|
|
150
|
+
...readEnvTranspilePackages(),
|
|
151
|
+
]),
|
|
131
152
|
];
|
|
132
153
|
|
|
133
154
|
const nextConfig = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soma-core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "SOMA — Semantic Oriented Modular Architecture. A declarative frontend framework runtime built on Next.js 14+ App Router.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"soma",
|
|
@@ -78,11 +78,11 @@
|
|
|
78
78
|
},
|
|
79
79
|
"files": [
|
|
80
80
|
"bin/soma.cjs",
|
|
81
|
+
"bin/soma-db.cjs",
|
|
81
82
|
"bin/soma-lsp.cjs",
|
|
82
83
|
"bin/soma-sync.cjs",
|
|
83
84
|
"bin/soma-mock.mjs",
|
|
84
85
|
"core/",
|
|
85
|
-
"docs/",
|
|
86
86
|
"editor/",
|
|
87
87
|
"app/",
|
|
88
88
|
"dist/",
|
|
@@ -108,7 +108,6 @@
|
|
|
108
108
|
"dev": "next dev",
|
|
109
109
|
"build": "next build",
|
|
110
110
|
"start": "next start",
|
|
111
|
-
"lint": "next lint",
|
|
112
111
|
"test": "vitest run && npm run test:mock",
|
|
113
112
|
"test:mock": "node --test tests/mock/*.test.mjs",
|
|
114
113
|
"verify:mock-pack": "node scripts/verify-mock-pack.mjs",
|
|
@@ -152,5 +151,9 @@
|
|
|
152
151
|
"vite": "6.4.3",
|
|
153
152
|
"vitest": "^3.2.7",
|
|
154
153
|
"ws": "^8.21.0"
|
|
154
|
+
},
|
|
155
|
+
"publishConfig": {
|
|
156
|
+
"access": "public",
|
|
157
|
+
"registry": "https://registry.npmjs.org/"
|
|
155
158
|
}
|
|
156
159
|
}
|
|
@@ -1,399 +0,0 @@
|
|
|
1
|
-
# soma-core 客户端 / 服务端隔离方案
|
|
2
|
-
|
|
3
|
-
本文给出 `soma-core` 的完整隔离目标:哪些代码参与客户端编译、哪些仅服务端执行、公开包导出如何切分,以及**隔离后对使用方(Host 业务应用)是否可做到无感**。
|
|
4
|
-
|
|
5
|
-
状态:全量物理拆分已完成(无兼容 re-export)。使用方变更见 [隔离接入变更方案](./隔离接入变更方案.md)。
|
|
6
|
-
|
|
7
|
-
已完成:
|
|
8
|
-
|
|
9
|
-
- 目录落地为 `core/client` / `core/server` / `core/shared` / `core/build`。
|
|
10
|
-
- 公开 exports 名称不变;`build:all` / `dist/types` 已对齐新路径。
|
|
11
|
-
- `soma-core/server`:含 DB 可插拔能力(`setSomaDb` / `getSomaDb` / `createSomaDbSingleton`)。
|
|
12
|
-
- `npm run verify:client-boundary`:布局检查 + 从 `SomaPageClient` 出发的依赖图全量扫描。
|
|
13
|
-
|
|
14
|
-
---
|
|
15
|
-
|
|
16
|
-
## 1. 结论先说
|
|
17
|
-
|
|
18
|
-
| 问题 | 答案 |
|
|
19
|
-
|------|------|
|
|
20
|
-
| 隔离后使用方能否不受影响? | **正规 Host 可以零改业务代码**(公开 exports 名不变)。不保留旧深路径兼容层;深路径用户按 [隔离接入变更方案](./隔离接入变更方案.md) 改。 |
|
|
21
|
-
| 使用方要改什么? | 只用 `soma-core` / `soma-core/base` / mock / testing:**不用改**。用了未文档化深路径:按对照表改。 |
|
|
22
|
-
| 谁可能受影响? | 深路径 / 把服务端模块打进客户端的用法。 |
|
|
23
|
-
| DB 放哪? | 服务端能力层(给 `api/service`),不进客户端编译图。 |
|
|
24
|
-
| 兼容 re-export? | **不保留。** 少改动靠公开入口稳定 + 变更文档,不靠旧门牌转发。 |
|
|
25
|
-
|
|
26
|
-
产品视角不变:`soma-core` 仍是 **后端 Host 服务**,向浏览器下发 HTML + 客户端运行切片;隔离解决的是「哪些源码允许进浏览器包」。
|
|
27
|
-
|
|
28
|
-
---
|
|
29
|
-
|
|
30
|
-
## 2. 目标与非目标
|
|
31
|
-
|
|
32
|
-
### 2.1 目标
|
|
33
|
-
|
|
34
|
-
1. **编译图隔离**:客户端 bundle 只能依赖 `client/`(或等价白名单),不能静态/动态拉到 Node-only 模块。
|
|
35
|
-
2. **运行时隔离**:浏览器可跑页面引擎与 `_services`;DB、文件 IO、`IService` 加载、request hook 解析等只在服务端。
|
|
36
|
-
3. **使用方兼容**:公开 API、CLI、Host 目录约定保持稳定。
|
|
37
|
-
4. **可演进**:后续可在服务端能力层增加 `db`(如 Prisma 封装),且默认不暴露给 Domain。
|
|
38
|
-
|
|
39
|
-
### 2.2 非目标
|
|
40
|
-
|
|
41
|
-
- 不要求立刻拆成两个 npm 包(可先单包多入口)。
|
|
42
|
-
- 不改变「后端吐资产、客户端挂引擎」的产品模型。
|
|
43
|
-
- 不把 Host 业务代码迁进 `soma-core`。
|
|
44
|
-
|
|
45
|
-
---
|
|
46
|
-
|
|
47
|
-
## 3. 当前问题(为何要隔离)
|
|
48
|
-
|
|
49
|
-
今天概念上已有两套体系,但工程边界弱:
|
|
50
|
-
|
|
51
|
-
1. `core/infra` 同时含页面 `_services` 与 Node 模块(如 `ServerFileCache`、`soma-server-request-context`)。
|
|
52
|
-
2. 客户端入口 `app/SomaPageClient.tsx` 会 `import` `core/engine`、`core/host/*`,依赖靠自觉,无强制。
|
|
53
|
-
3. 根导出 `soma-core` 混有类型与运行时;仅对 request-hook 等做了局部防护。
|
|
54
|
-
4. `api/service` 没有对等的「服务端标准能力」槽位;加 DB 缺少正规落点。
|
|
55
|
-
|
|
56
|
-
---
|
|
57
|
-
|
|
58
|
-
## 4. 目标分层
|
|
59
|
-
|
|
60
|
-
建议把源码收成三层(物理目录可一次迁或分步迁):
|
|
61
|
-
|
|
62
|
-
```text
|
|
63
|
-
soma-core/
|
|
64
|
-
├── client/ # 允许进入浏览器编译图
|
|
65
|
-
│ ├── base/ # Domain 基类、装饰器(现 core/base)
|
|
66
|
-
│ ├── engine/ # Bootstrap / 渲染 / 事件(现 core/engine 浏览器侧)
|
|
67
|
-
│ ├── runtime/ # _services 组装(去掉 Node 专用)
|
|
68
|
-
│ ├── infra/ # 仅浏览器安全的 runtime services
|
|
69
|
-
│ ├── container/ # DOM 等适配器(浏览器侧)
|
|
70
|
-
│ ├── middleware/ # 页面事件中间件
|
|
71
|
-
│ ├── scheduler/
|
|
72
|
-
│ ├── quality/
|
|
73
|
-
│ └── diagnostics/ # 客户端可用子集
|
|
74
|
-
│
|
|
75
|
-
├── server/ # 禁止进入浏览器编译图
|
|
76
|
-
│ ├── api/ # IService 加载与路由辅助
|
|
77
|
-
│ ├── host/ # 读盘、资产、hook、origin、page assets
|
|
78
|
-
│ ├── infra/ # ServerFileCache、server request context、未来 db
|
|
79
|
-
│ ├── mock/ # MSW Node(已基本独立)
|
|
80
|
-
│ └── headless/ # 仅 Node 的 headless 页面执行(若与浏览器引擎拆分)
|
|
81
|
-
│
|
|
82
|
-
├── shared/ # 纯类型 / 纯函数,前后端都可依赖
|
|
83
|
-
│ ├── types/
|
|
84
|
-
│ ├── manifest/
|
|
85
|
-
│ ├── validation/ # 若无 Node API
|
|
86
|
-
│ └── contracts/ # IService 类型、Hook 类型等
|
|
87
|
-
│
|
|
88
|
-
├── build/ # 仅构建机 / CLI(现 compiler/parser/lsp)
|
|
89
|
-
│ ├── compiler/
|
|
90
|
-
│ ├── parser/
|
|
91
|
-
│ └── lsp/
|
|
92
|
-
│
|
|
93
|
-
└── app/ # Next 壳:再拆 server components vs client entry
|
|
94
|
-
├── SomaPageClient.tsx # 唯一主客户端入口
|
|
95
|
-
├── components/*Client* # 客户端组件
|
|
96
|
-
├── components/SomaHostPage.tsx # 服务端 RSC
|
|
97
|
-
├── api/** # 服务端 Route Handlers
|
|
98
|
-
└── soma/** # 服务端 Route Handlers
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
依赖规则(硬约束):
|
|
102
|
-
|
|
103
|
-
```text
|
|
104
|
-
client → shared
|
|
105
|
-
server → shared +(可)client 中的纯逻辑?默认禁止;需要则下沉到 shared
|
|
106
|
-
build → shared +(可读)client/server 源做分析,运行时不进浏览器
|
|
107
|
-
app 客户端入口 → 只允许 import client/ + shared/
|
|
108
|
-
app 服务端入口 → server/ + shared/;(必要时)动态加载 client 做 headless
|
|
109
|
-
Host Domain → soma-core/base(client)
|
|
110
|
-
Host api/service→ soma-core 或 soma-core/server(类型 + 服务端能力)
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
强制手段(落地时任选组合):
|
|
114
|
-
|
|
115
|
-
- 目录级 `import 'server-only'`(server 包入口)。
|
|
116
|
-
- ESLint `no-restricted-imports`:`app/**` 中 `'use client'` 文件禁止 `**/server/**`。
|
|
117
|
-
- 包导出:`soma-core/server` 单独入口;根入口不 re-export Node 实现。
|
|
118
|
-
- CI:对客户端入口做依赖图扫描,命中 `node:fs` / `jiti` / `server-only` 则失败。
|
|
119
|
-
|
|
120
|
-
---
|
|
121
|
-
|
|
122
|
-
## 5. 哪些参与客户端编译(白名单)
|
|
123
|
-
|
|
124
|
-
以下模块**允许**进入浏览器 bundle(经 `SomaPageClient` / Domain / `soma-core` / `soma-core/base` 引用链)。
|
|
125
|
-
|
|
126
|
-
### 5.1 Next 客户端入口(壳)
|
|
127
|
-
|
|
128
|
-
| 路径 | 说明 |
|
|
129
|
-
|------|------|
|
|
130
|
-
| `app/SomaPageClient.tsx` | 主客户端入口(`'use client'`) |
|
|
131
|
-
| `app/components/SomaHydrationShell.tsx` | 水合占位(若被 client 引用) |
|
|
132
|
-
|
|
133
|
-
### 5.2 页面运行时核心
|
|
134
|
-
|
|
135
|
-
| 现路径(迁移前) | 目标归属 | 说明 |
|
|
136
|
-
|------------------|----------|--------|------|
|
|
137
|
-
| `core/base/**` | `client/base` | Domain 基类、装饰器、`runAsync` |
|
|
138
|
-
| `core/engine/soma-bootstrap.ts` 及渲染/事件/绑定/代理等 | `client/engine` | 浏览器挂载引擎 |
|
|
139
|
-
| `core/runtime/SomaRuntimeServices.ts` | `client/runtime` | `_services` 工厂 |
|
|
140
|
-
| `core/runtime/SomaSnapshotStore.ts` | `client/runtime` | 快照(若仅客户端用) |
|
|
141
|
-
| `core/container/**`(至少 DOM 适配器) | `client/container` | 容器 |
|
|
142
|
-
| `core/middleware/**` | `client/middleware` | 页面中间件链 |
|
|
143
|
-
| `core/scheduler/**` | `client/scheduler` | 调度 |
|
|
144
|
-
| `core/quality/**` | `client/quality` | 质量登记 |
|
|
145
|
-
| `core/diagnostics/**`(无 Node API 子集) | `client/diagnostics` | 诊断 |
|
|
146
|
-
|
|
147
|
-
### 5.3 注入 Domain 的 `_services`(仅浏览器安全实现)
|
|
148
|
-
|
|
149
|
-
| 现路径 | 是否进客户端 | 说明 |
|
|
150
|
-
|--------|--------------|------|
|
|
151
|
-
| `infra/http/SomaHttpService.ts`、`IHttpClient.ts` | 是 | 页面调 `/api` |
|
|
152
|
-
| `infra/storage/**` | 是 | session/memory |
|
|
153
|
-
| `infra/cache/MemoryCache.ts`、`ICache.ts` | 是 | 内存缓存 |
|
|
154
|
-
| `infra/router/**` | 是 | 前端路由抽象 |
|
|
155
|
-
| `infra/notify/**` | 是 | toast/confirm |
|
|
156
|
-
| `infra/logger/**` | 是 | 日志 |
|
|
157
|
-
| `infra/config/**` | 是 | 配置 |
|
|
158
|
-
| `infra/auth/**` | 是 | Token 等(基于 http+storage) |
|
|
159
|
-
| `infra/permission/**` | 是 | 权限判断 |
|
|
160
|
-
| `infra/i18n/**` | 是 | 文案 |
|
|
161
|
-
| `infra/timer/**` | 是 | 定时器 |
|
|
162
|
-
| `infra/event-bus/**` | 是 | 事件总线 |
|
|
163
|
-
| `infra/realtime/**` | 是 | 客户端 SSE/WS |
|
|
164
|
-
|
|
165
|
-
### 5.4 客户端会间接带上的 Host 侧「薄适配」(需改造)
|
|
166
|
-
|
|
167
|
-
当前 `SomaPageClient` 还引用:
|
|
168
|
-
|
|
169
|
-
| 现路径 | 现状问题 | 隔离后做法 |
|
|
170
|
-
|--------|----------|------------|
|
|
171
|
-
| `core/host/load-host-domain-class.ts` | 挂在 host 下,易与读盘逻辑混淆 | 迁到 `client/host-bridge/load-domain-class.ts`,**只做 webpack 动态 import Domain**,不含 `fs` |
|
|
172
|
-
| `core/host/resolve-host-bootstrap.ts` | 同上 | 迁到 `client/host-bridge/resolve-bootstrap.ts`,只动态 import `@soma-host/bootstrap/soma-bootstrap` |
|
|
173
|
-
| `core/host/soma-domain-loaders.ts`(纯注册表逻辑) | 可前后端共用 | 下沉 `shared/` 或留在 client bridge |
|
|
174
|
-
|
|
175
|
-
### 5.5 Host 业务代码(不在 soma-core 内,但会进客户端)
|
|
176
|
-
|
|
177
|
-
| Host 路径 | 说明 |
|
|
178
|
-
|-----------|------|
|
|
179
|
-
| `domain/**/*Class.ts` | Domain,经客户端动态 import |
|
|
180
|
-
| `ui/*.yaml`、`event/*.yaml` 等预编译模块 | 经 webpack 编入或经 manifest 下发后在客户端消费 |
|
|
181
|
-
| `bootstrap/soma-bootstrap.ts` | 可选,客户端配置引擎中间件 |
|
|
182
|
-
|
|
183
|
-
**注意**:Host 的 `api/service/**`、`mock/**`、Prisma/schema **不得**被客户端入口引用。
|
|
184
|
-
|
|
185
|
-
---
|
|
186
|
-
|
|
187
|
-
## 6. 哪些不参与客户端编译(服务端黑名单)
|
|
188
|
-
|
|
189
|
-
以下模块**禁止**出现在客户端依赖图中。
|
|
190
|
-
|
|
191
|
-
### 6.1 Next 服务端壳
|
|
192
|
-
|
|
193
|
-
| 路径 | 说明 |
|
|
194
|
-
|------|------|
|
|
195
|
-
| `app/layout.tsx`、`app/page.tsx`、`app/[...path]/page.tsx` | RSC / 服务端页面 |
|
|
196
|
-
| `app/components/SomaHostPage.tsx` | 服务端组装,再 `dynamic(..., { ssr: false })` 加载 Client |
|
|
197
|
-
| `app/api/**`、`app/soma/**`、`app/favicon.ico/**` | Route Handlers |
|
|
198
|
-
| `middleware.ts` | Edge/Node 中间件 |
|
|
199
|
-
|
|
200
|
-
### 6.2 Host / API / 读盘
|
|
201
|
-
|
|
202
|
-
| 现路径 | 原因 |
|
|
203
|
-
|--------|------|
|
|
204
|
-
| `core/api/load-host-service.ts`、`service-path.ts` | `jiti` / 读 Host 文件 |
|
|
205
|
-
| `core/host/server-page-assets.ts` | `fs`、文件缓存 |
|
|
206
|
-
| `core/host/source-packages.ts`、`host-config-paths*`、`app-config-composer.ts` | Node path/fs |
|
|
207
|
-
| `core/host/invoke-host-request-hook.ts`、`resolve-host-request-hook.ts` | 服务端 Hook |
|
|
208
|
-
| `core/host/page-response-*`、`request-origin.ts` | 服务端请求语义 |
|
|
209
|
-
| `core/infra/cache/ServerFileCache.ts` | 服务端文件缓存 |
|
|
210
|
-
| `core/infra/http/soma-server-request-context.ts` | `AsyncLocalStorage` / Node |
|
|
211
|
-
| `core/mock/**` | Node MSW |
|
|
212
|
-
| 未来 `server/infra/db/**` | DB / Prisma,仅 `api/service` |
|
|
213
|
-
|
|
214
|
-
### 6.3 构建期(不进浏览器运行时)
|
|
215
|
-
|
|
216
|
-
| 路径 | 说明 |
|
|
217
|
-
|------|------|
|
|
218
|
-
| `core/compiler/**` | 预编译、webpack/vite 插件 |
|
|
219
|
-
| `core/parser/**` | 规格解析(构建/预览) |
|
|
220
|
-
| `core/lsp/**`、`bin/**`、`editor/**` | 工具链 |
|
|
221
|
-
|
|
222
|
-
### 6.4 Headless 特例
|
|
223
|
-
|
|
224
|
-
`core/engine/soma-headless-page.ts` 与 `serialize-page-state` 当前由 **服务端** `app/soma/page-object` 使用。隔离后:
|
|
225
|
-
|
|
226
|
-
- 方案 A:保留在 `server/headless`,内部依赖一份可在 Node 跑的引擎构建(JSDOM 等),**不**与浏览器 client 入口共用同一打包图。
|
|
227
|
-
- 方案 B:headless 动态 import `client/engine`,但仅在 server bundle 中;客户端入口仍禁止反向依赖 server。
|
|
228
|
-
|
|
229
|
-
推荐方案 A 或 B 均可,验收标准是:**客户端入口依赖图不含 headless/server-only**。
|
|
230
|
-
|
|
231
|
-
---
|
|
232
|
-
|
|
233
|
-
## 7. 公开包导出(兼容使用方)
|
|
234
|
-
|
|
235
|
-
### 7.1 保持不变(使用方零改动)
|
|
236
|
-
|
|
237
|
-
| 导出 | 用途 | 隔离后内部指向 |
|
|
238
|
-
|------|------|----------------|
|
|
239
|
-
| `soma-core` | `IService` 类型、Bootstrap、部分 runtime 类型等(按现文档) | re-export 自 `shared` + `client` 安全子集;**不**导出 Node 实现 |
|
|
240
|
-
| `soma-core/base` | Domain 基类 | `client/base` |
|
|
241
|
-
| `soma-core/mock/node` | Mock | `server/mock`(本就 Node) |
|
|
242
|
-
| `soma-core/mock/node/cli` | Mock CLI | 不变 |
|
|
243
|
-
| `soma-core/testing/mock-host` | 测试 Host | 不变 |
|
|
244
|
-
| `soma-core/host/request-hook` | 服务端 Hook resolver | `server/host/...`(保持子路径) |
|
|
245
|
-
| `soma-core/compiler/*` | 构建插件 | `build/compiler` |
|
|
246
|
-
| `soma-core/diagnostics` | 诊断 | client 安全子集或双实现 |
|
|
247
|
-
|
|
248
|
-
### 7.2 新增(可选,加能力时用)
|
|
249
|
-
|
|
250
|
-
| 导出 | 用途 | 谁用 |
|
|
251
|
-
|------|------|------|
|
|
252
|
-
| `soma-core/server` | 服务端标准能力(含未来 `db`) | 仅 `api/service`、Route Handler |
|
|
253
|
-
| `soma-core/server/db`(可选更细) | DB 能力 | 仅服务端 |
|
|
254
|
-
|
|
255
|
-
Host 业务 API 示例(隔离后,若启用 DB):
|
|
256
|
-
|
|
257
|
-
```ts
|
|
258
|
-
// Host: api/service/orders/List.ts
|
|
259
|
-
import type { IService } from 'soma-core';
|
|
260
|
-
import { getDb } from 'soma-core/server'; // 或 host 自管 prisma;框架只提供可选能力
|
|
261
|
-
|
|
262
|
-
export default class OrdersListService implements IService {
|
|
263
|
-
async get() {
|
|
264
|
-
const db = getDb();
|
|
265
|
-
const rows = await db.order.findMany();
|
|
266
|
-
return Response.json(rows);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
Domain 仍只允许:
|
|
272
|
-
|
|
273
|
-
```ts
|
|
274
|
-
import { SomaPageRootBase } from 'soma-core/base';
|
|
275
|
-
// this._services.http.get('/orders')
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
### 7.3 兼容承诺细则
|
|
279
|
-
|
|
280
|
-
**保证无感:**
|
|
281
|
-
|
|
282
|
-
1. Host 目录约定不变:`html/`、`ui/`、`event/`、`domain/`、`api/service/`、`config/`、`mock/`。
|
|
283
|
-
2. 文档推荐 import 不变:`soma-core`、`soma-core/base`、`soma-core/mock/node`、`soma-core/host/request-hook`。
|
|
284
|
-
3. CLI 不变:`soma` / `soma-mock` / `soma-lsp`。
|
|
285
|
-
4. 页面行为不变:`_services` 集合与语义不变(不新增 `_services.db`)。
|
|
286
|
-
5. 内部目录搬家时,用 `package.json` exports + 薄 re-export 保旧路径一段时间(若有人深引用 `core/...`,可给 deprecation 窗口;公开文档本就不鼓励深路径)。
|
|
287
|
-
|
|
288
|
-
**不保证无感(可接受破坏):**
|
|
289
|
-
|
|
290
|
-
1. 从客户端文件 `import` 了 `load-host-service`、`ServerFileCache`、`soma-server-request-context` 等。
|
|
291
|
-
2. 依赖未文档化的深路径且在搬家后未走兼容 re-export。
|
|
292
|
-
3. 自行把 Prisma Client 塞进 Domain。
|
|
293
|
-
|
|
294
|
-
---
|
|
295
|
-
|
|
296
|
-
## 8. 落地步骤(建议分 4 期)
|
|
297
|
-
|
|
298
|
-
### Phase 0 — 画线(不搬家,先防回归)
|
|
299
|
-
|
|
300
|
-
1. 列出客户端入口闭包:以 `app/SomaPageClient.tsx` 为 root 做依赖图。
|
|
301
|
-
2. CI 增加「客户端依赖图禁止 `node:fs` / `jiti` / `async_hooks`」检查。
|
|
302
|
-
3. 文档写明:参与客户端编译白名单(本文 §5)与服务端黑名单(§6)。
|
|
303
|
-
|
|
304
|
-
**使用方影响:无。**
|
|
305
|
-
|
|
306
|
-
### Phase 1 — 拆脏 `infra`(小步)
|
|
307
|
-
|
|
308
|
-
1. `ServerFileCache`、`soma-server-request-context` 迁到 `server/infra`。
|
|
309
|
-
2. 客户端 `infra` 仅保留 §5.3。
|
|
310
|
-
3. 旧路径留 1~2 个版本的 re-export + 警告。
|
|
311
|
-
|
|
312
|
-
**使用方影响:无(若只用公开导出)。**
|
|
313
|
-
|
|
314
|
-
### Phase 2 — 客户端 bridge 从 `host` 剥离
|
|
315
|
-
|
|
316
|
-
1. `load-host-domain-class`、`resolve-host-bootstrap` 的浏览器安全实现迁到 `client/host-bridge`。
|
|
317
|
-
2. `SomaPageClient` 只引用 bridge。
|
|
318
|
-
3. 真正的 `server/host/*` 仅 Route Handler / RSC 使用。
|
|
319
|
-
|
|
320
|
-
**使用方影响:无。** Host 的 `@soma-host/domain/...`、`bootstrap/soma-bootstrap` 约定不变。
|
|
321
|
-
|
|
322
|
-
### Phase 3 — 包导出硬化 + 可选 server 能力
|
|
323
|
-
|
|
324
|
-
1. 增加 `soma-core/server`,入口 `import 'server-only'`。
|
|
325
|
-
2. 根 `soma-core` 审计:禁止再导出 Node 实现(类型可留)。
|
|
326
|
-
3. (可选)增加 DB 能力:仅挂在 `soma-core/server`,由 Host `api/service` 使用;schema/业务查询仍在 Host。
|
|
327
|
-
|
|
328
|
-
**使用方影响:**
|
|
329
|
-
|
|
330
|
-
- 不用 DB:无。
|
|
331
|
-
- 要用 DB:新增依赖与 import(加法,非破坏)。
|
|
332
|
-
|
|
333
|
-
---
|
|
334
|
-
|
|
335
|
-
## 9. DB 在本方案中的位置
|
|
336
|
-
|
|
337
|
-
```text
|
|
338
|
-
浏览器 Domain
|
|
339
|
-
└─ _services.http → /api/...
|
|
340
|
-
└─ Host api/service (IService)
|
|
341
|
-
└─ soma-core/server (getDb / 注入的 Prisma)
|
|
342
|
-
└─ 真实数据库
|
|
343
|
-
|
|
344
|
-
不存在:_services.db
|
|
345
|
-
不存在:client/infra/db
|
|
346
|
-
```
|
|
347
|
-
|
|
348
|
-
框架提供的是**服务端访问能力**(连接单例、生命周期、可选接口);**表结构与查询**在 Host。这与「标准能力在框架、真实代码在 app」一致,且不进入客户端编译图。
|
|
349
|
-
|
|
350
|
-
---
|
|
351
|
-
|
|
352
|
-
## 10. 验收标准
|
|
353
|
-
|
|
354
|
-
隔离完成应同时满足:
|
|
355
|
-
|
|
356
|
-
1. **依赖图**:`SomaPageClient` 传递依赖不含 §6 黑名单文件。
|
|
357
|
-
2. **构建**:生产 `soma build` 的客户端 chunk 不含 `fs`/`jiti`/`PrismaClient` 等。
|
|
358
|
-
3. **契约测试**:现有 Host 最小示例、Mock 契约、页面 JSON/headless 测试通过。
|
|
359
|
-
4. **文档示例**:`external-docs` 中的 import 无需修改即可运行。
|
|
360
|
-
5. **可选**:新增 `soma-core/server` 示例仅出现在 `api/service` 文档,不出现在 Domain 文档。
|
|
361
|
-
|
|
362
|
-
---
|
|
363
|
-
|
|
364
|
-
## 11. 对使用方的一句话承诺
|
|
365
|
-
|
|
366
|
-
> 隔离是框架内部的编译与目录边界治理。
|
|
367
|
-
> 按官方文档使用 `soma-core` / `soma-core/base`、按约定写 Host 页面与 `api/service` 的业务应用,**升级后无需改代码**。
|
|
368
|
-
> 仅当业务主动使用新增的 `soma-core/server`(如 DB)时,才增加服务端侧写法;页面 Domain 路径保持不变。
|
|
369
|
-
|
|
370
|
-
---
|
|
371
|
-
|
|
372
|
-
## 12. 附录:当前客户端引用链(迁移前快照)
|
|
373
|
-
|
|
374
|
-
便于对照改造:
|
|
375
|
-
|
|
376
|
-
```text
|
|
377
|
-
app/SomaPageClient.tsx
|
|
378
|
-
├─ (type) core/engine/soma-bootstrap
|
|
379
|
-
├─ core/host/load-host-domain-class
|
|
380
|
-
│ └─ core/base / core/host/soma-domain-loaders
|
|
381
|
-
│ └─ 动态 import @soma-host/domain/... 或 domain-loaders
|
|
382
|
-
├─ 动态 import core/engine/soma-bootstrap
|
|
383
|
-
│ └─ container / diagnostics / middleware / scheduler
|
|
384
|
-
│ └─ runtime/SomaRuntimeServices → infra/*(含浏览器实现)
|
|
385
|
-
│ └─ engine/* 渲染与事件
|
|
386
|
-
└─ 动态 import core/host/resolve-host-bootstrap
|
|
387
|
-
└─ 动态 import @soma-host/bootstrap/soma-bootstrap
|
|
388
|
-
```
|
|
389
|
-
|
|
390
|
-
服务端主链(不应与上图交叉污染):
|
|
391
|
-
|
|
392
|
-
```text
|
|
393
|
-
app/api/[...path] → core/api + core/host/invoke-host-request-hook
|
|
394
|
-
app/soma/page → core/host/server-page-assets
|
|
395
|
-
app/soma/page-object → server assets + headless + server request context
|
|
396
|
-
app/components/SomaHostPage → request hook + dynamic(SomaPageClient)
|
|
397
|
-
```
|
|
398
|
-
|
|
399
|
-
改造完成后,上图「客户端链」只允许落入本文 §5;「服务端链」只允许落入 §6。
|