@pylonts/dsl 1.1.12 → 1.1.13

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.
Files changed (48) hide show
  1. package/dist/convert.d.ts +6 -8
  2. package/dist/curd.js +1 -1
  3. package/dist/dao.d.ts +10 -7
  4. package/dist/dao.js +20 -7
  5. package/dist/dsl.d.ts +18 -1
  6. package/dist/dsl.js +40 -0
  7. package/dist/dto.d.ts +13 -8
  8. package/dist/dto.js +68 -13
  9. package/dist/entity.d.ts +4 -3
  10. package/dist/entity.js +1 -1
  11. package/dist/filter.d.ts +6 -4
  12. package/dist/filter.js +1 -1
  13. package/dist/flow-script.js +8 -2
  14. package/dist/flow.d.ts +10 -2
  15. package/dist/flow.js +44 -4
  16. package/dist/mermaid-driver.js +2 -2
  17. package/dist/service.d.ts +13 -8
  18. package/dist/service.js +1 -1
  19. package/dist/third-service.d.ts +10 -53
  20. package/dist/third-service.js +3 -78
  21. package/dist/typebox-driver.d.ts +0 -6
  22. package/dist/typebox-driver.js +8 -36
  23. package/dist/utils.d.ts +2 -2
  24. package/docs/curd.md +146 -146
  25. package/docs/dao-generation.md +477 -477
  26. package/docs/project.md +31 -31
  27. package/docs/token.md +326 -326
  28. package/package.json +1 -1
  29. package/src/action.ts +51 -51
  30. package/src/controller.ts +53 -53
  31. package/src/convert.ts +76 -78
  32. package/src/curd.ts +104 -104
  33. package/src/dao.ts +504 -485
  34. package/src/dsl.ts +296 -257
  35. package/src/dto.ts +323 -266
  36. package/src/entity.ts +43 -42
  37. package/src/expr.ts +64 -64
  38. package/src/filter.ts +71 -69
  39. package/src/flow-script.ts +702 -695
  40. package/src/flow.ts +1272 -1226
  41. package/src/index.ts +46 -46
  42. package/src/mermaid-driver.ts +339 -339
  43. package/src/mysql-driver.ts +108 -108
  44. package/src/project.ts +138 -138
  45. package/src/service.ts +112 -107
  46. package/src/third-service.ts +68 -191
  47. package/src/typebox-driver.ts +234 -268
  48. package/src/utils.ts +74 -74
package/src/service.ts CHANGED
@@ -1,108 +1,113 @@
1
- import type { SchemaBase } from './dsl.js';
2
- import type { FrontAppSchema, ProjectApiSchema } from './project.js';
3
- import type { DtoArrayField, DtoField, DtoMessage, DtoObjectField } from './dto.js';
4
- import type { ExceptionSchema } from './exception.js';
5
- import type { FlowSchema } from './flow.js';
6
- import { exceptionEndNames } from './flow.js';
7
-
8
- /** A backend service serving exactly one frontend app (1:1 module). */
9
- export interface ServiceSchema extends SchemaBase {
10
- type: 'service';
11
- /** The backend api module this service belongs to (shared instance from
12
- * project.config.ts apis). Services are always backend-side, so storage is
13
- * service_schema/{api.name}/{app.name}/service/. */
14
- api: ProjectApiSchema;
15
- /** The frontend app this service serves (shared instance from project.config). */
16
- app: FrontAppSchema;
17
- /** Methods keyed by name the map key is written back as the method name. */
18
- methods: Record<string, ServiceMethodSchema>;
19
- }
20
-
21
- /** Method input for defineService: type/schema/name are set by the builder. */
22
- export type ServiceMethodDef = Omit<ServiceMethodSchema, 'type' | 'schema' | 'name'>;
23
-
24
- export function defineService(options: {
25
- name: string;
26
- api: ProjectApiSchema;
27
- app: FrontAppSchema;
28
- methods: Record<string, ServiceMethodDef>;
29
- description?: string;
30
- }): ServiceSchema {
31
- if (!options.api.apps.includes(options.app)) {
32
- throw new Error(`service ${options.name}: api '${options.api.name}' does not serve app '${options.app.name}'`);
33
- }
34
- const schema: ServiceSchema = {
35
- type: 'service',
36
- name: options.name,
37
- description: options.description,
38
- api: options.api,
39
- app: options.app,
40
- methods: {},
41
- };
42
- for (const key of Object.keys(options.methods)) {
43
- const method = options.methods[key];
44
- schema.methods[key] = { type: 'method', schema, ...method, name: key };
45
- }
46
- validateFlowBindings(schema);
47
- return schema;
48
- }
49
-
50
- // A method bound to a flow: the flow's escape set (its exception ends) must
51
- // equal the method's declared throws, the flow's own args/results (when
52
- // present) must be the same objects as the method's, and one flow may serve
53
- // only one method.
54
- //
55
- // Notes: (1) the unique-binding check is per defineService call a flow
56
- // shared across two service schemas is not detected; (2) escape-set equality
57
- // means exceptions swallowed by an internal catch (or routed through a
58
- // catch-all end) disappear from the contract, so the method must not declare
59
- // them.
60
- function validateFlowBindings(schema: ServiceSchema): void {
61
- const seenFlows = new Set<FlowSchema>();
62
- for (const key of Object.keys(schema.methods)) {
63
- const m = schema.methods[key];
64
- if (!m.flow) continue;
65
- if (seenFlows.has(m.flow)) {
66
- throw new Error(`service ${schema.name}: flow "${m.flow.name}" is bound to more than one method`);
67
- }
68
- seenFlows.add(m.flow);
69
- if (m.flow.args !== undefined && m.flow.args !== m.args) {
70
- throw new Error(
71
- `service ${schema.name}: method "${key}" args and its flow "${m.flow.name}" args must be the same object`,
72
- );
73
- }
74
- if (m.flow.results !== undefined && m.flow.results !== m.results) {
75
- throw new Error(
76
- `service ${schema.name}: method "${key}" results and its flow "${m.flow.name}" results must be the same object`,
77
- );
78
- }
79
- const escaped = exceptionEndNames(m.flow);
80
- const declared = new Set((m.throws ?? []).map((t) => t.name));
81
- for (const e of escaped) {
82
- if (!declared.has(e)) {
83
- throw new Error(`service ${schema.name}: method "${key}" flow escapes ${e} but the method does not declare it`);
84
- }
85
- }
86
- for (const d of declared) {
87
- if (!escaped.has(d)) {
88
- throw new Error(`service ${schema.name}: method "${key}" declares throw ${d} but its flow never escapes it`);
89
- }
90
- }
91
- if (m.flow.name !== key) {
92
- console.warn(`service ${schema.name}: method "${key}" flow name "${m.flow.name}" differs from the method key`);
93
- }
94
- }
95
- }
96
-
97
- /** A method exposed by a service. */
98
- export interface ServiceMethodSchema extends SchemaBase {
99
- type: 'method';
100
- schema : ServiceSchema;
101
- args: DtoMessage,
102
- results : DtoMessage
103
- /** Exceptions this method may throw (e.g. IOException, CodeException). */
104
- throws?: ExceptionSchema[];
105
- /** Implementation flow — the method's logic graph. The method carries the
106
- * contract (args/results/throws), the flow carries the structure. */
107
- flow?: FlowSchema;
1
+ import type { CollectionSchemaBase, SchemaBase } from './dsl.js';
2
+ import type { FrontAppSchema, ProjectApiSchema } from './project.js';
3
+ import type { DtoArrayField, DtoField, DtoMessage, DtoObjectField } from './dto.js';
4
+ import type { ExceptionSchema } from './exception.js';
5
+ import type { FlowSchema } from './flow.js';
6
+ import { exceptionEndNames } from './flow.js';
7
+
8
+ /** A backend service serving one frontend app (1:1 module), or a
9
+ * platform-shared domain service (app unset — shared across modules). */
10
+ export interface ServiceSchema extends CollectionSchemaBase {
11
+ type: 'service';
12
+ /** The backend api module this service belongs to (shared instance from
13
+ * project.config.ts apis). Services are always backend-side, so storage is
14
+ * service_schema/{api.name}/{app.name}/service/ — app unset = the api-level
15
+ * common domain layer, stored at service_schema/{api.name}/common/service/. */
16
+ api: ProjectApiSchema;
17
+ /** The frontend app this service serves (shared instance from project.config).
18
+ * Unset = api-level common domain service shared by all modules of the api. */
19
+ app?: FrontAppSchema;
20
+ /** Methods keyed by name — the map key is written back as the method name. */
21
+ methods: Record<string, ServiceMethodSchema>;
22
+ }
23
+
24
+ /** Method input for defineService: type/schema/name are set by the builder. */
25
+ export type ServiceMethodDef = Omit<ServiceMethodSchema, 'type' | 'schema' | 'name'>;
26
+
27
+ export function defineService(options: {
28
+ name: string;
29
+ api: ProjectApiSchema;
30
+ app?: FrontAppSchema;
31
+ methods: Record<string, ServiceMethodDef>;
32
+ description?: string;
33
+ }): ServiceSchema {
34
+ if (options.app && !options.api.apps.includes(options.app)) {
35
+ throw new Error(`service ${options.name}: api '${options.api.name}' does not serve app '${options.app.name}'`);
36
+ }
37
+ const schema: ServiceSchema = {
38
+ type: 'service',
39
+ name: options.name,
40
+ description: options.description,
41
+ api: options.api,
42
+ app: options.app,
43
+ methods: {},
44
+ };
45
+ for (const key of Object.keys(options.methods)) {
46
+ const method = options.methods[key];
47
+ schema.methods[key] = { type: 'method', schema, ...method, name: key };
48
+ }
49
+ validateFlowBindings(schema);
50
+ return schema;
51
+ }
52
+
53
+ // A method bound to a flow: the flow's escape set (its exception ends) must
54
+ // equal the method's declared throws, the flow's own args/results (when
55
+ // present) must be the same objects as the method's, and one flow may serve
56
+ // only one method.
57
+ //
58
+ // Notes: (1) the unique-binding check is per defineService call a flow
59
+ // shared across two service schemas is not detected; (2) escape-set equality
60
+ // means exceptions swallowed by an internal catch (or routed through a
61
+ // catch-all end) disappear from the contract, so the method must not declare
62
+ // them.
63
+ function validateFlowBindings(schema: ServiceSchema): void {
64
+ const seenFlows = new Set<FlowSchema>();
65
+ for (const key of Object.keys(schema.methods)) {
66
+ const m = schema.methods[key];
67
+ if (!m.flow) continue;
68
+ if (seenFlows.has(m.flow)) {
69
+ throw new Error(`service ${schema.name}: flow "${m.flow.name}" is bound to more than one method`);
70
+ }
71
+ seenFlows.add(m.flow);
72
+ if (m.flow.args !== undefined && m.flow.args !== m.args) {
73
+ throw new Error(
74
+ `service ${schema.name}: method "${key}" args and its flow "${m.flow.name}" args must be the same object`,
75
+ );
76
+ }
77
+ if (m.flow.results !== undefined && m.flow.results !== m.results) {
78
+ throw new Error(
79
+ `service ${schema.name}: method "${key}" results and its flow "${m.flow.name}" results must be the same object`,
80
+ );
81
+ }
82
+ const escaped = exceptionEndNames(m.flow);
83
+ const declared = new Set((m.throws ?? []).map((t) => t.name));
84
+ for (const e of escaped) {
85
+ if (!declared.has(e)) {
86
+ throw new Error(`service ${schema.name}: method "${key}" flow escapes ${e} but the method does not declare it`);
87
+ }
88
+ }
89
+ for (const d of declared) {
90
+ if (!escaped.has(d)) {
91
+ throw new Error(`service ${schema.name}: method "${key}" declares throw ${d} but its flow never escapes it`);
92
+ }
93
+ }
94
+ if (m.flow.name !== key) {
95
+ console.warn(`service ${schema.name}: method "${key}" flow name "${m.flow.name}" differs from the method key`);
96
+ }
97
+ }
98
+ }
99
+
100
+ /** A method exposed by a business service — the contract with the calling
101
+ * frontend. Third-party integration methods are a separate contract
102
+ * (ThirdServiceMethodSchema): they can never bind a flow. */
103
+ export interface ServiceMethodSchema extends SchemaBase {
104
+ type: 'method';
105
+ schema: ServiceSchema;
106
+ args: DtoMessage,
107
+ results : DtoMessage
108
+ /** Exceptions this method may throw (e.g. IOException, CodeException). */
109
+ throws?: ExceptionSchema[];
110
+ /** Implementation flow — the method's logic graph. The method carries the
111
+ * contract (args/results/throws), the flow carries the structure. */
112
+ flow?: FlowSchema;
108
113
  }
@@ -1,192 +1,69 @@
1
- import { CollectionSchemaBase, Field, SchemaBase } from './dsl.js';
2
- import type { ExceptionSchema } from './exception.js';
3
- import type { FieldRuleEnd, FieldRuleSchema } from './field-rule.js';
4
- import type { ThirdApiSchema } from './project.js';
5
-
6
- // Third-party integration services.
7
- // A ThirdMethodSchema is the Field-collection counterpart of a DtoMessage
8
- // like TableSchema.columns but for wire-format fields instead of DB columns.
9
- // DTOs project from it via from(thirdMethod), and its fields may either share
10
- // a local entity column instance (same fact, same type) or declare their own
11
- // wire type plus a refs link (same fact, different type/format).
12
-
13
- /** A third-party integration service (e.g. tenpay wechat pay).
14
- * Distinct from ServiceSchema (backend service bound to an app) and
15
- * ThirdApiSchema (project topology: the dir the integration lives in).
16
- * Describes the adapter class contract: constructor config + methods. */
17
- export interface ThirdServiceSchema extends SchemaBase {
18
- type: 'thirdService';
19
- /** The third-party system this service integrates (topology reference). */
20
- schema: ThirdApiSchema;
21
- /** Methods keyed by name — the map key is written back as the method name. */
22
- methods: Record<string, ThirdServiceMethodSchema>;
23
- }
24
-
25
- /** A method exposed by a third-party service. */
26
- export interface ThirdServiceMethodSchema extends SchemaBase {
27
- type: 'method';
28
- schema: ThirdServiceSchema;
29
- /** Input message the Field-collection counterpart of a DtoMessage. */
30
- args: ThirdMethodSchema;
31
- /** Output message. */
32
- results: ThirdMethodSchema;
33
- /** Exceptions this method may throw (e.g. IOException, CodeException). */
34
- throws?: ExceptionSchema[];
35
- }
36
-
37
- /** Field binding to a rule: which end the local wire field stands on.
38
- * The ref always stands on the other end — from/to carry no extra information. */
39
- export interface ConvertFieldSchema {
40
- /** The rule binding field and ref (rule = name + two ends). */
41
- rule: FieldRuleSchema;
42
- /** The end instance the local wire field stands on. */
43
- end: FieldRuleEnd;
44
- }
45
-
46
- /** Same-fact variant link: a wire field carrying the same fact as a local
47
- * entity column under a different type/format (e.g. total_fee in fen vs amount in yuan). */
48
- export interface ThirdFieldRef {
49
- /** Wire field defined in this message (local definition). */
50
- field: Field;
51
- /** Field in another schema (table column or another message). */
52
- ref: Field;
53
- /** Optional rule binding — omitted when the fact is merely linked, not converted. */
54
- convert?: ConvertFieldSchema;
55
- }
56
-
57
- /** A directional message of a third-party method: a Field collection mirroring
58
- * TableSchema.columns, but fields hold wire-format names/types. A field may be
59
- * a shared instance of a local entity column (same fact, same type) — its
60
- * name/schema keep pointing at the table and the DTO projection inherits
61
- * type/semantics from the entity, exactly like from(table). */
62
- export interface ThirdMethodSchema extends CollectionSchemaBase {
63
- type: 'thirdMethod';
64
- /** The method this message belongs to (direction implied by args/results slot). */
65
- schema: ThirdServiceMethodSchema;
66
- /** Wire-format fields. */
67
- fields: Record<string, Field>;
68
- /** Same-fact variant links: wire field -> local entity column. */
69
- refs?: ThirdFieldRef[];
70
- }
71
-
72
- /** Message input for defineThirdMethod: type/schema are set by the builder. */
73
- export type ThirdMethodDef = Omit<ThirdMethodSchema, 'type' | 'schema'>;
74
-
75
- /** Build a third-party method message. Writes back name/schema on own fields
76
- * (top-level and nested); shared entity columns keep their table identity and
77
- * must be keyed by their column name. */
78
- export function defineThirdMethod(def: ThirdMethodDef): ThirdMethodSchema {
79
- const message: ThirdMethodSchema = {
80
- type: 'thirdMethod',
81
- name: def.name,
82
- description: def.description,
83
- // Filled by defineThirdService.
84
- schema: undefined as unknown as ThirdServiceMethodSchema,
85
- fields: def.fields,
86
- refs: def.refs,
87
- };
88
- for (const key of Object.keys(message.fields)) {
89
- const field = message.fields[key] as Field;
90
- if (field.schema === undefined) {
91
- field.name = key;
92
- field.schema = message;
93
- } else if (field.schema.type === 'table') {
94
- // Shared entity column: from() names the projection after field.name, so
95
- // a mismatched key would silently rename the wire field. Same-fact fields
96
- // with different names go through refs instead.
97
- if (field.name !== key) {
98
- throw new Error(
99
- `thirdMethod '${message.name}': shared column key '${key}' must match the column name '${field.name}' — ` +
100
- `same-fact fields with different names go through refs instead`,
101
- );
102
- }
103
- } else if (field.schema !== message) {
104
- throw new Error(
105
- `thirdMethod '${message.name}': field '${key}' already belongs to ${field.schema.type} '${field.schema.name}', cannot reuse`,
106
- );
107
- }
108
- writeBackNested(message, field);
109
- }
110
- if (message.refs !== undefined) {
111
- const ownFields = Object.values(message.fields);
112
- for (const link of message.refs) {
113
- if (!ownFields.includes(link.field)) {
114
- throw new Error(`thirdMethod '${message.name}': ref field must be one of its fields`);
115
- }
116
- if (link.ref.schema === undefined || link.ref.schema === message) {
117
- throw new Error(`thirdMethod '${message.name}': ref target '${link.ref.name}' must be defined in another schema`);
118
- }
119
- if (link.convert !== undefined) {
120
- const ends = Object.values(link.convert.rule.ends);
121
- if (!ends.includes(link.convert.end)) {
122
- throw new Error(
123
- `thirdMethod '${message.name}': convert end '${link.convert.end.name}' must be one of rule '${link.convert.rule.name}' ends`,
124
- );
125
- }
126
- }
127
- }
128
- }
129
- return message;
130
- }
131
-
132
- /** Write back name/schema on nested wire fields (array items, object properties);
133
- * shared entity columns keep their table identity. */
134
- function writeBackNested(message: ThirdMethodSchema, field: Field): void {
135
- if (field.type === 'array') {
136
- writeBackNested(message, field.items);
137
- return;
138
- }
139
- if (field.type !== 'object') return;
140
- for (const key of Object.keys(field.properties)) {
141
- const child = field.properties[key] as Field;
142
- if (child.schema === undefined) {
143
- child.name = key;
144
- child.schema = message;
145
- }
146
- writeBackNested(message, child);
147
- }
148
- }
149
-
150
- /** Method input for defineThirdService: name is written back from the methods map key. */
151
- export interface ThirdServiceMethodDef {
152
- /** Input message. */
153
- args: ThirdMethodDef;
154
- /** Output message. */
155
- results: ThirdMethodDef;
156
- /** Exceptions this method may throw (e.g. IOException, CodeException). */
157
- throws?: ExceptionSchema[];
158
- description?: string;
159
- }
160
-
161
- export function defineThirdService(options: {
162
- schema: ThirdApiSchema;
163
- name: string;
164
- methods: Record<string, ThirdServiceMethodDef>;
165
- description?: string;
166
- }): ThirdServiceSchema {
167
- const schema: ThirdServiceSchema = {
168
- type: 'thirdService',
169
- name: options.name,
170
- description: options.description,
171
- schema: options.schema,
172
- methods: {},
173
- };
174
- for (const key of Object.keys(options.methods)) {
175
- const method = options.methods[key] as ThirdServiceMethodDef;
176
- const methodSchema: ThirdServiceMethodSchema = {
177
- type: 'method',
178
- name: key,
179
- description: method.description,
180
- schema,
181
- args: undefined as unknown as ThirdMethodSchema,
182
- results: undefined as unknown as ThirdMethodSchema,
183
- throws: method.throws,
184
- };
185
- methodSchema.args = defineThirdMethod(method.args);
186
- methodSchema.results = defineThirdMethod(method.results);
187
- methodSchema.args.schema = methodSchema;
188
- methodSchema.results.schema = methodSchema;
189
- schema.methods[key] = methodSchema;
190
- }
191
- return schema;
1
+ import type { SchemaBase } from './dsl.js';
2
+ import type { DtoMessage } from './dto.js';
3
+ import type { ExceptionSchema } from './exception.js';
4
+ import type { ThirdApiSchema } from './project.js';
5
+
6
+ // Third-party integration services.
7
+ // A third-party method's args/results are plain DtoMessages the same POJO
8
+ // aggregate as any business DTO. Fields reference each other across messages
9
+ // by sharing the Field instance (dtoField(sharedField)): a wire field may be
10
+ // the same object as a local entity column (same fact, same type) or a field
11
+ // of another DtoMessage (same fact, possibly different protocol name).
12
+
13
+ /** A third-party integration service (e.g. tenpay wechat pay).
14
+ * Distinct from ServiceSchema (backend service bound to an app) and
15
+ * ThirdApiSchema (project topology: the dir the integration lives in).
16
+ * Describes the adapter class contract: constructor config + methods. */
17
+ export interface ThirdServiceSchema extends SchemaBase {
18
+ type: 'thirdService';
19
+ /** The third-party system this service integrates (topology reference). */
20
+ schema: ThirdApiSchema;
21
+ /** Methods keyed by name — the map key is written back as the method name. */
22
+ methods: Record<string, ThirdServiceMethodSchema>;
23
+ }
24
+
25
+ /** A method of a third-party integration service. Same contract shape as
26
+ * ServiceMethodSchema minus flow the implementation lives in the external
27
+ * system, there is nothing to model. Kept separate so a third method can
28
+ * never bind a flow. */
29
+ export interface ThirdServiceMethodSchema extends SchemaBase {
30
+ type: 'method';
31
+ schema: ThirdServiceSchema;
32
+ /** Input message. */
33
+ args: DtoMessage;
34
+ /** Output message. */
35
+ results: DtoMessage;
36
+ /** Exceptions this method may throw (e.g. IOException, CodeException). */
37
+ throws?: ExceptionSchema[];
38
+ }
39
+
40
+ /** Method input for defineThirdService: name/schema are set by the builder. */
41
+ export type ThirdServiceMethodDef = Omit<ThirdServiceMethodSchema, 'type' | 'schema' | 'name'>;
42
+
43
+ export function defineThirdService(options: {
44
+ schema: ThirdApiSchema;
45
+ name: string;
46
+ methods: Record<string, ThirdServiceMethodDef>;
47
+ description?: string;
48
+ }): ThirdServiceSchema {
49
+ const schema: ThirdServiceSchema = {
50
+ type: 'thirdService',
51
+ name: options.name,
52
+ description: options.description,
53
+ schema: options.schema,
54
+ methods: {},
55
+ };
56
+ for (const key of Object.keys(options.methods)) {
57
+ const method = options.methods[key];
58
+ schema.methods[key] = {
59
+ type: 'method',
60
+ name: key,
61
+ description: method.description,
62
+ schema,
63
+ args: method.args,
64
+ results: method.results,
65
+ throws: method.throws,
66
+ };
67
+ }
68
+ return schema;
192
69
  }