@yanqirenshi/model.tm 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +143 -2
- package/dist/check.d.ts +18 -0
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +107 -0
- package/dist/cjs/check.js +111 -0
- package/dist/cjs/defaults.js +145 -0
- package/dist/cjs/fields.js +77 -0
- package/dist/cjs/index.js +55 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/patch.js +274 -0
- package/dist/cjs/schema.js +179 -0
- package/dist/cjs/types.js +8 -0
- package/dist/defaults.d.ts +67 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +133 -0
- package/dist/fields.d.ts +19 -0
- package/dist/fields.d.ts.map +1 -0
- package/dist/fields.js +72 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/patch.d.ts +31 -0
- package/dist/patch.d.ts.map +1 -0
- package/dist/patch.js +264 -0
- package/dist/schema.d.ts +768 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +176 -0
- package/dist/types.d.ts +140 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/package.json +54 -5
- package/src/check.ts +137 -0
- package/src/defaults.ts +172 -0
- package/src/fields.ts +82 -0
- package/src/index.ts +30 -0
- package/src/patch.ts +338 -0
- package/src/schema.ts +194 -0
- package/src/types.ts +165 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAkDA,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAI9B,CAAC;AAEF,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAI7B,CAAC;AAoBF,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsClB,CAAC;AAEX,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiEvB,CAAC;AAEX,qBAAqB;AACrB,eAAO,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAKV,CAAC"}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// TM(d3.ter)の入力データの JSON Schema(2020-12)。コレクションごとに 1 つ。
|
|
2
|
+
//
|
|
3
|
+
// iHub の server/schemas/tm/*.schema.json を移したもの。iHub 版と違い additionalProperties を閉じていない
|
|
4
|
+
// (利用側の項目を落とさないため)。検査するのは、図の構造にかかわる項目(id・type・参照・position・size・
|
|
5
|
+
// 関係の端点・subset・mapping)。name は文字列でも { logical, physical } でもよい。
|
|
6
|
+
//
|
|
7
|
+
// x-id-start はスキーマの検証には使わない注釈(コレクションが空のときに最初に振る id)。
|
|
8
|
+
// Ajv で使うときは addKeyword({ keyword: 'x-id-start', schemaType: 'number' }) を足す。
|
|
9
|
+
import { CARDINALITIES, ENTITY_TYPES, OPTIONALITIES, SUBSET_KINDS } from './defaults.js';
|
|
10
|
+
const ID = { type: ['integer', 'string'] };
|
|
11
|
+
const NAME = {
|
|
12
|
+
description: '文字列、または論理名と物理名の組',
|
|
13
|
+
oneOf: [
|
|
14
|
+
{ type: 'string' },
|
|
15
|
+
{
|
|
16
|
+
type: 'object',
|
|
17
|
+
properties: {
|
|
18
|
+
logical: { type: 'string' },
|
|
19
|
+
physical: { type: 'string' },
|
|
20
|
+
description: { type: 'string' },
|
|
21
|
+
},
|
|
22
|
+
additionalProperties: true,
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
};
|
|
26
|
+
// マスタ(identifiers / attributes)の共通形
|
|
27
|
+
function masterSchema(title, description, idStart) {
|
|
28
|
+
return {
|
|
29
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
30
|
+
title,
|
|
31
|
+
description,
|
|
32
|
+
'x-id-start': idStart,
|
|
33
|
+
type: 'array',
|
|
34
|
+
items: {
|
|
35
|
+
type: 'object',
|
|
36
|
+
properties: {
|
|
37
|
+
id: ID,
|
|
38
|
+
name: NAME,
|
|
39
|
+
description: { type: ['string', 'null'] },
|
|
40
|
+
},
|
|
41
|
+
required: ['id', 'name'],
|
|
42
|
+
additionalProperties: true,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export const IDENTIFIERS_SCHEMA = masterSchema('TM の識別子のマスタ', '識別子 (個体指定子) のマスタ。entities[].identifiers[].identifier から id で参照される。', 10000);
|
|
47
|
+
export const ATTRIBUTES_SCHEMA = masterSchema('TM の属性のマスタ', '属性のマスタ。entities[].attributes[].attribute から id で参照される。', 20000);
|
|
48
|
+
// エンティティが持つインスタンス(識別子・属性)の共通形。ref はマスタを指す項目名
|
|
49
|
+
function instanceSchema(ref) {
|
|
50
|
+
return {
|
|
51
|
+
type: 'array',
|
|
52
|
+
items: {
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: {
|
|
55
|
+
id: ID,
|
|
56
|
+
[ref]: ID,
|
|
57
|
+
name: NAME,
|
|
58
|
+
description: { type: ['string', 'null'] },
|
|
59
|
+
},
|
|
60
|
+
required: ['id', ref],
|
|
61
|
+
additionalProperties: true,
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export const ENTITIES_SCHEMA = {
|
|
66
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
67
|
+
title: 'TM のエンティティ',
|
|
68
|
+
description: 'TM (T字形ER) のエンティティ。type は 10 種の種別 (これ以外は d3.ter が描画時に例外にする)。識別子・属性はマスタの id を参照するインスタンスとして持ち、インスタンスの id は全エンティティを通して一意 (関係の details がこれを指す)。name は文字列か論理名・物理名の組。position はドラッグで書き換わる。size の w / h が 0 のときは内容に合わせる。',
|
|
69
|
+
'x-id-start': 30000,
|
|
70
|
+
type: 'array',
|
|
71
|
+
items: {
|
|
72
|
+
type: 'object',
|
|
73
|
+
properties: {
|
|
74
|
+
id: ID,
|
|
75
|
+
type: { enum: [...ENTITY_TYPES] },
|
|
76
|
+
name: NAME,
|
|
77
|
+
description: { type: ['string', 'null'] },
|
|
78
|
+
position: {
|
|
79
|
+
type: 'object',
|
|
80
|
+
properties: {
|
|
81
|
+
x: { type: 'number' },
|
|
82
|
+
y: { type: 'number' },
|
|
83
|
+
z: { type: 'number' },
|
|
84
|
+
},
|
|
85
|
+
required: ['x', 'y'],
|
|
86
|
+
additionalProperties: false,
|
|
87
|
+
},
|
|
88
|
+
size: {
|
|
89
|
+
type: 'object',
|
|
90
|
+
properties: {
|
|
91
|
+
w: { type: 'number', minimum: 0 },
|
|
92
|
+
h: { type: 'number', minimum: 0 },
|
|
93
|
+
},
|
|
94
|
+
required: ['w', 'h'],
|
|
95
|
+
additionalProperties: false,
|
|
96
|
+
},
|
|
97
|
+
identifiers: instanceSchema('identifier'),
|
|
98
|
+
attributes: instanceSchema('attribute'),
|
|
99
|
+
},
|
|
100
|
+
required: ['id', 'type', 'name'],
|
|
101
|
+
additionalProperties: true,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
export const RELATIONSHIPS_SCHEMA = {
|
|
105
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
106
|
+
title: 'TM のリレーションシップ',
|
|
107
|
+
description: 'エンティティの間の結線。from.entity / to.entity はエンティティの id (同じエンティティを指すと自己結線 = 再帰のループ)。端点の cardinality (1 / 3) と optionality (0 / 1) は無ければ記号を描かない。position は接続位置の角度 (0=真下・時計回り)。details は線を取り付けるインスタンス (省略すると先頭の識別子)。subset はサブセット結線の区分コード (same は =、different は ×)。mapping は結線の中点から垂下する対応表・対照表・再帰表 (再帰表は自己結線にだけ)。',
|
|
108
|
+
'x-id-start': 1,
|
|
109
|
+
type: 'array',
|
|
110
|
+
items: {
|
|
111
|
+
type: 'object',
|
|
112
|
+
properties: {
|
|
113
|
+
id: ID,
|
|
114
|
+
type: { type: ['string', 'null'] },
|
|
115
|
+
from: { $ref: '#/$defs/end' },
|
|
116
|
+
to: { $ref: '#/$defs/end' },
|
|
117
|
+
details: {
|
|
118
|
+
type: 'array',
|
|
119
|
+
items: {
|
|
120
|
+
type: 'object',
|
|
121
|
+
properties: {
|
|
122
|
+
from: { $ref: '#/$defs/detail_end' },
|
|
123
|
+
to: { $ref: '#/$defs/detail_end' },
|
|
124
|
+
},
|
|
125
|
+
required: ['from', 'to'],
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
subset: {
|
|
129
|
+
type: ['object', 'null'],
|
|
130
|
+
properties: {
|
|
131
|
+
kind: { enum: [...SUBSET_KINDS] },
|
|
132
|
+
code: { type: 'string' },
|
|
133
|
+
},
|
|
134
|
+
required: ['kind', 'code'],
|
|
135
|
+
},
|
|
136
|
+
mapping: {
|
|
137
|
+
type: ['object', 'null'],
|
|
138
|
+
properties: { entity: ID },
|
|
139
|
+
required: ['entity'],
|
|
140
|
+
},
|
|
141
|
+
label: { type: ['string', 'null'] },
|
|
142
|
+
description: { type: ['string', 'null'] },
|
|
143
|
+
},
|
|
144
|
+
required: ['id', 'from', 'to'],
|
|
145
|
+
additionalProperties: true,
|
|
146
|
+
},
|
|
147
|
+
$defs: {
|
|
148
|
+
end: {
|
|
149
|
+
type: 'object',
|
|
150
|
+
properties: {
|
|
151
|
+
entity: ID,
|
|
152
|
+
cardinality: { enum: [...CARDINALITIES], description: '1 か 多 (3)。無ければ記号を描かない' },
|
|
153
|
+
optionality: { enum: [...OPTIONALITIES], description: '0 は任意 (○)、1 は必須 (横棒)。無ければ記号を描かない' },
|
|
154
|
+
position: { type: 'number', description: '接続位置の角度。0=真下・時計回り (90=左 / 180=真上 / 270=右)' },
|
|
155
|
+
id: { ...ID, description: '旧来の項目。端点の識別子のインスタンスの id (details に置き換わった)' },
|
|
156
|
+
},
|
|
157
|
+
required: ['entity'],
|
|
158
|
+
additionalProperties: true,
|
|
159
|
+
},
|
|
160
|
+
detail_end: {
|
|
161
|
+
type: 'object',
|
|
162
|
+
properties: {
|
|
163
|
+
id: ID,
|
|
164
|
+
description: { type: ['string', 'null'] },
|
|
165
|
+
},
|
|
166
|
+
required: ['id'],
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
/** コレクション名 → スキーマ */
|
|
171
|
+
export const SCHEMAS = {
|
|
172
|
+
identifiers: IDENTIFIERS_SCHEMA,
|
|
173
|
+
attributes: ATTRIBUTES_SCHEMA,
|
|
174
|
+
entities: ENTITIES_SCHEMA,
|
|
175
|
+
relationships: RELATIONSHIPS_SCHEMA,
|
|
176
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { Name, Position, Size } from '@yanqirenshi/types';
|
|
2
|
+
/** レコードの id(コレクションの中で一意。インスタンス(identifiers / attributes の行)の id は全エンティティを通して一意) */
|
|
3
|
+
export type TmId = number | string;
|
|
4
|
+
/** 論理名・物理名の組(共有型 Name) */
|
|
5
|
+
export type TmName = Name;
|
|
6
|
+
/**
|
|
7
|
+
* TM(T字形ER)のエンティティ種別(10 種)。これ以外は d3.ter が描画時に例外にする。
|
|
8
|
+
* CLASS は「クラスで整える」の箱で、種別バッジを持たず、切り出し元(親)としか結線できない。
|
|
9
|
+
*/
|
|
10
|
+
export type TmEntityType = 'RESOURCE' | 'RESOURCE-SUBSET' | 'EVENT' | 'EVENT-SUBSET' | 'COMPARATIVE' | 'CORRESPONDENCE' | 'RECURSION' | 'MANY-VALUED-OR' | 'MANY-VALUED-AND' | 'CLASS';
|
|
11
|
+
/** マスタ(identifiers / attributes)のレコード。name は文字列でも Name でもよい */
|
|
12
|
+
export interface TmMasterRecord {
|
|
13
|
+
id: TmId;
|
|
14
|
+
name?: string | TmName | null;
|
|
15
|
+
description?: string | null;
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
}
|
|
18
|
+
/** エンティティが持つ識別子のインスタンス(identifier は identifiers[].id)。name は表示名の上書き(TM の (R) 表記など) */
|
|
19
|
+
export interface TmIdentifierRef {
|
|
20
|
+
id: TmId;
|
|
21
|
+
identifier: TmId;
|
|
22
|
+
name?: string | TmName | null;
|
|
23
|
+
description?: string | null;
|
|
24
|
+
[key: string]: unknown;
|
|
25
|
+
}
|
|
26
|
+
/** エンティティが持つ属性のインスタンス(attribute は attributes[].id) */
|
|
27
|
+
export interface TmAttributeRef {
|
|
28
|
+
id: TmId;
|
|
29
|
+
attribute: TmId;
|
|
30
|
+
name?: string | TmName | null;
|
|
31
|
+
description?: string | null;
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
}
|
|
34
|
+
/** エンティティのレコード */
|
|
35
|
+
export interface TmEntityRecord {
|
|
36
|
+
id: TmId;
|
|
37
|
+
type?: TmEntityType | string;
|
|
38
|
+
name?: string | TmName | null;
|
|
39
|
+
description?: string | null;
|
|
40
|
+
position?: Position;
|
|
41
|
+
/** w / h が 0 のときは view が内容に合わせて決める */
|
|
42
|
+
size?: Size;
|
|
43
|
+
identifiers?: TmIdentifierRef[];
|
|
44
|
+
attributes?: TmAttributeRef[];
|
|
45
|
+
[key: string]: unknown;
|
|
46
|
+
}
|
|
47
|
+
/** 1 か 多(3) */
|
|
48
|
+
export type TmCardinality = 1 | 3;
|
|
49
|
+
/** 0 は任意(○)、1 は必須(横棒) */
|
|
50
|
+
export type TmOptionality = 0 | 1;
|
|
51
|
+
/**
|
|
52
|
+
* 関係の端点。position はエンティティの中心から見た角度(0=真下・時計回り。90=左 / 180=真上 / 270=右)。
|
|
53
|
+
* cardinality / optionality が無いときは記号を描かない。
|
|
54
|
+
* id は旧来の項目(端点の識別子のインスタンス。details に置き換わった)で、あれば検査だけする。
|
|
55
|
+
*/
|
|
56
|
+
export interface TmRelationshipEnd {
|
|
57
|
+
entity: TmId;
|
|
58
|
+
cardinality?: TmCardinality;
|
|
59
|
+
optionality?: TmOptionality;
|
|
60
|
+
position?: number;
|
|
61
|
+
id?: TmId;
|
|
62
|
+
[key: string]: unknown;
|
|
63
|
+
}
|
|
64
|
+
/** 関係の線を取り付けるインスタンス(from.id / to.id はそれぞれの端のエンティティの識別子・属性のインスタンス) */
|
|
65
|
+
export interface TmRelationshipDetail {
|
|
66
|
+
from: {
|
|
67
|
+
id: TmId;
|
|
68
|
+
description?: string | null;
|
|
69
|
+
};
|
|
70
|
+
to: {
|
|
71
|
+
id: TmId;
|
|
72
|
+
description?: string | null;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** サブセットへの結線に添える区分コード。same は「=区分コード」、different は「×区分コード」 */
|
|
76
|
+
export interface TmSubset {
|
|
77
|
+
kind: 'same' | 'different';
|
|
78
|
+
code: string;
|
|
79
|
+
}
|
|
80
|
+
/** 関係(リレーションシップ)のレコード */
|
|
81
|
+
export interface TmRelationshipRecord {
|
|
82
|
+
id: TmId;
|
|
83
|
+
type?: string | null;
|
|
84
|
+
from: TmRelationshipEnd;
|
|
85
|
+
to: TmRelationshipEnd;
|
|
86
|
+
details?: TmRelationshipDetail[];
|
|
87
|
+
subset?: TmSubset | null;
|
|
88
|
+
/** 結線の中点から垂下する対応表・対照表・再帰表(エンティティの id) */
|
|
89
|
+
mapping?: {
|
|
90
|
+
entity: TmId;
|
|
91
|
+
} | null;
|
|
92
|
+
label?: string | null;
|
|
93
|
+
description?: string | null;
|
|
94
|
+
[key: string]: unknown;
|
|
95
|
+
}
|
|
96
|
+
export type TmRecord = TmMasterRecord | TmEntityRecord | TmRelationshipRecord;
|
|
97
|
+
/** 図全体(d3.ter の data() に渡すもの) */
|
|
98
|
+
export interface TmGraph {
|
|
99
|
+
identifiers?: TmMasterRecord[];
|
|
100
|
+
attributes?: TmMasterRecord[];
|
|
101
|
+
entities?: TmEntityRecord[];
|
|
102
|
+
relationships?: TmRelationshipRecord[];
|
|
103
|
+
}
|
|
104
|
+
/** マスタのコレクション */
|
|
105
|
+
export type TmMasterCollection = 'identifiers' | 'attributes';
|
|
106
|
+
/** レコードを置くコレクション */
|
|
107
|
+
export type TmCollection = TmMasterCollection | 'entities' | 'relationships';
|
|
108
|
+
/** レコードの種類(どの形か) */
|
|
109
|
+
export type TmKind = 'master' | 'entity' | 'relationship';
|
|
110
|
+
/** JSON Merge Patch(RFC 7386)。null は「その項目を除く」 */
|
|
111
|
+
export type MergePatch = {
|
|
112
|
+
[key: string]: unknown;
|
|
113
|
+
};
|
|
114
|
+
/** 保存する変更。利用側は図の種類を知らずにこれを保存すればよい */
|
|
115
|
+
export interface TmChange {
|
|
116
|
+
collection: TmCollection;
|
|
117
|
+
id: TmId;
|
|
118
|
+
patch: MergePatch;
|
|
119
|
+
}
|
|
120
|
+
/** ドラッグの移動量(世界座標) */
|
|
121
|
+
export interface TmMove {
|
|
122
|
+
dx: number;
|
|
123
|
+
dy: number;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 編集できる項目。colonoscope の ColonoscopeField(path / label / type)と同じ形で、
|
|
127
|
+
* 取りうる値の一覧 options だけ足している(型は構造的に互換。colonoscope には依存しない)。
|
|
128
|
+
*/
|
|
129
|
+
export type TmFieldType = 'text' | 'textarea' | 'number' | 'readonly';
|
|
130
|
+
export interface TmField {
|
|
131
|
+
/** レコードの項目のパス(ドット区切り。例: 'from.position')。写し先ではなく実際の項目を指す */
|
|
132
|
+
path: string;
|
|
133
|
+
label: string;
|
|
134
|
+
type: TmFieldType;
|
|
135
|
+
/** 取りうる値の一覧(種別・カーディナリティなど。文字列で持つ) */
|
|
136
|
+
options?: readonly string[];
|
|
137
|
+
}
|
|
138
|
+
/** インスペクタの「適用」が渡す値(path → 入力した文字列) */
|
|
139
|
+
export type TmValues = Record<string, string>;
|
|
140
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE/D,mFAAmF;AACnF,MAAM,MAAM,IAAI,GAAG,MAAM,GAAG,MAAM,CAAC;AAEnC,0BAA0B;AAC1B,MAAM,MAAM,MAAM,GAAG,IAAI,CAAC;AAE1B;;;GAGG;AACH,MAAM,MAAM,YAAY,GACpB,UAAU,GAAG,iBAAiB,GAC9B,OAAO,GAAG,cAAc,GACxB,aAAa,GAAG,gBAAgB,GAAG,WAAW,GAC9C,gBAAgB,GAAG,iBAAiB,GACpC,OAAO,CAAC;AAEZ,+DAA+D;AAC/D,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,IAAI,CAAC;IACT,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,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
3
|
+
// ここで言う「レコード」は、d3.ter の data() に渡す入力そのものの 1 要素。
|
|
4
|
+
// { identifiers, attributes, entities, relationships }
|
|
5
|
+
// 形は model.er(ER 図)と同じ階層形式で、TM 固有の項目(エンティティの type、関係の subset / mapping / label)が足されている。
|
|
6
|
+
// d3.ter が正規化したあとの datum(_id / _core を持つ Atman)ではない。
|
|
7
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@yanqirenshi/model.tm",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Diagram model for TM (T-shaped ER method) diagrams (d3.ter) — JSON Schema, consistency rules, drawing defaults, editable fields and patch conversion. Pure data and functions: no React / d3 dependency",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/cjs/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc && tsc -p tsconfig.cjs.json && node scripts/cjs-package.cjs",
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"prepare": "npm run build",
|
|
25
|
+
"prepublishOnly": "npm run build"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/Foolsgolds/Assholes.git",
|
|
30
|
+
"directory": "packages/model.tm"
|
|
31
|
+
},
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/Foolsgolds/Assholes/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/Foolsgolds/Assholes/tree/main/packages/model.tm#readme",
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@yanqirenshi/types": "^0.1.2"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"typescript": "~5.9.3",
|
|
44
|
+
"vitest": "^2.1.8"
|
|
45
|
+
},
|
|
46
|
+
"author": "yanqirenshi@gmail.com",
|
|
47
|
+
"keywords": [
|
|
48
|
+
"diagram",
|
|
49
|
+
"model",
|
|
50
|
+
"json-schema",
|
|
51
|
+
"tm",
|
|
52
|
+
"d3.ter"
|
|
53
|
+
],
|
|
54
|
+
"license": "MIT"
|
|
55
|
+
}
|
package/src/check.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// コレクションをまたぐ整合性の検査(id の重複・参照先の存在・TM の結線の規則)。
|
|
2
|
+
// スキーマ(schema.ts)に合っているデータを渡す。戻り値はエラーのメッセージの配列(空なら問題なし)。
|
|
3
|
+
//
|
|
4
|
+
// iHub の server/validation/rules.mjs の checkTm を移したもの(文言も同じ)。
|
|
5
|
+
// それに加えて、d3.ter がデータ構築時に例外にしている規則(Rectum.assertClassLink / resolveMapping と
|
|
6
|
+
// Entity.entityTypeContents)も検査する。描く前に分かるほうがよい知識で、d3.ter 側の例外は残る。
|
|
7
|
+
//
|
|
8
|
+
// メッセージの先頭はコレクション名で、prefix を渡すと 'tm/entities: ...' のように付けられる。
|
|
9
|
+
|
|
10
|
+
import { MAPPING_TYPES, isEntityType } from './defaults.js';
|
|
11
|
+
import type { TmEntityRecord, TmGraph, TmId, TmRecord } from './types.js';
|
|
12
|
+
|
|
13
|
+
// list の中で id が重複していればエラーにする。id の集合を返す。
|
|
14
|
+
function checkUnique(errors: string[], label: string, list: readonly { id: TmId }[]): Set<TmId> {
|
|
15
|
+
const values = new Set<TmId>();
|
|
16
|
+
|
|
17
|
+
for (const d of list) {
|
|
18
|
+
if (values.has(d.id))
|
|
19
|
+
errors.push(`${label}: id が重複しています。(id=${String(d.id)})`);
|
|
20
|
+
|
|
21
|
+
values.add(d.id);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return values;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// エンティティが持つ識別子のマスタの集合(CLASS の親判定に使う)
|
|
28
|
+
function identifierMasters(entity: TmEntityRecord): Set<TmId> {
|
|
29
|
+
return new Set((entity.identifiers || []).map(d => d.identifier));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 図全体を検査する。
|
|
34
|
+
* - identifiers / attributes / entities / relationships の id が、それぞれのコレクションの中で重複していない
|
|
35
|
+
* - エンティティのインスタンス(identifiers[] / attributes[] の行)の id が、全エンティティを通して重複していない
|
|
36
|
+
* - entities[].type が 10 種のどれか
|
|
37
|
+
* - entities[].identifiers[].identifier が identifiers に、attributes[].attribute が attributes にある
|
|
38
|
+
* - relationships[].from.entity / to.entity がエンティティにある
|
|
39
|
+
* - 端点の旧 id(あれば)がその端のエンティティの識別子に、details[].from.id / to.id が識別子・属性にある
|
|
40
|
+
* - mapping.entity がエンティティにあり、種別が対応表・対照表・再帰表のどれか。再帰表は自己結線にだけ
|
|
41
|
+
* - CLASS 同士は結線できず、CLASS は切り出し元(親 = クラスの識別子のマスタを持つ側)としか結線できない
|
|
42
|
+
*
|
|
43
|
+
* @param prefix メッセージのコレクション名の前に付ける文字列(例: 'tm/')
|
|
44
|
+
*/
|
|
45
|
+
export function check(data: TmGraph, prefix = ''): string[] {
|
|
46
|
+
const errors: string[] = [];
|
|
47
|
+
|
|
48
|
+
const identifiers = data.identifiers || [];
|
|
49
|
+
const attributes = data.attributes || [];
|
|
50
|
+
const entities = data.entities || [];
|
|
51
|
+
const relationships = data.relationships || [];
|
|
52
|
+
|
|
53
|
+
const identifier_ids = checkUnique(errors, `${prefix}identifiers`, identifiers);
|
|
54
|
+
const attribute_ids = checkUnique(errors, `${prefix}attributes`, attributes);
|
|
55
|
+
|
|
56
|
+
checkUnique(errors, `${prefix}entities`, entities);
|
|
57
|
+
checkUnique(errors, `${prefix}relationships`, relationships);
|
|
58
|
+
|
|
59
|
+
// インスタンスの id は、エンティティをまたいで一意にする(d3.ter はインスタンスを id だけで探すため)
|
|
60
|
+
checkUnique(errors, `${prefix}entities の identifiers・attributes`,
|
|
61
|
+
entities.flatMap(e => [...(e.identifiers || []), ...(e.attributes || [])]));
|
|
62
|
+
|
|
63
|
+
for (const e of entities) {
|
|
64
|
+
if (!isEntityType(e.type))
|
|
65
|
+
errors.push(`${prefix}entities: type が種別のどれでもありません。(id=${String(e.id)}, type=${String(e.type)})`);
|
|
66
|
+
|
|
67
|
+
for (const d of e.identifiers || [])
|
|
68
|
+
if (!identifier_ids.has(d.identifier))
|
|
69
|
+
errors.push(`${prefix}entities: identifier が identifiers に見つかりません。(entity=${String(e.id)}, id=${String(d.id)}, identifier=${String(d.identifier)})`);
|
|
70
|
+
|
|
71
|
+
for (const d of e.attributes || [])
|
|
72
|
+
if (!attribute_ids.has(d.attribute))
|
|
73
|
+
errors.push(`${prefix}entities: attribute が attributes に見つかりません。(entity=${String(e.id)}, id=${String(d.id)}, attribute=${String(d.attribute)})`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const index = new Map(entities.map(e => [e.id, e]));
|
|
77
|
+
|
|
78
|
+
for (const r of relationships) {
|
|
79
|
+
const ends: Partial<Record<'from' | 'to', TmEntityRecord>> = {};
|
|
80
|
+
|
|
81
|
+
for (const end of ['from', 'to'] as const) {
|
|
82
|
+
const entity = index.get(r[end]?.entity);
|
|
83
|
+
|
|
84
|
+
if (!entity) {
|
|
85
|
+
errors.push(`${prefix}relationships: ${end}.entity のエンティティが見つかりません。(id=${String(r.id)}, entity=${String(r[end]?.entity)})`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
ends[end] = entity;
|
|
90
|
+
|
|
91
|
+
const identifiers_of = entity.identifiers || [];
|
|
92
|
+
const instances = [...identifiers_of, ...(entity.attributes || [])];
|
|
93
|
+
|
|
94
|
+
if (r[end].id !== undefined && !identifiers_of.some(d => d.id === r[end].id))
|
|
95
|
+
errors.push(`${prefix}relationships: ${end}.id がエンティティの識別子に見つかりません。(id=${String(r.id)}, entity=${String(entity.id)}, ${end}.id=${String(r[end].id)})`);
|
|
96
|
+
|
|
97
|
+
for (const detail of r.details || [])
|
|
98
|
+
if (!instances.some(d => d.id === detail[end]?.id))
|
|
99
|
+
errors.push(`${prefix}relationships: details の ${end}.id がエンティティの識別子・属性に見つかりません。(id=${String(r.id)}, entity=${String(entity.id)}, ${end}.id=${String(detail[end]?.id)})`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (r.mapping) {
|
|
103
|
+
const target = index.get(r.mapping.entity);
|
|
104
|
+
|
|
105
|
+
if (!target)
|
|
106
|
+
errors.push(`${prefix}relationships: mapping.entity のエンティティが見つかりません。(id=${String(r.id)}, entity=${String(r.mapping.entity)})`);
|
|
107
|
+
else if (!(MAPPING_TYPES as readonly string[]).includes(target.type as string))
|
|
108
|
+
errors.push(`${prefix}relationships: mapping に指定できるのは対応表 (CORRESPONDENCE)・対照表 (COMPARATIVE)・再帰表 (RECURSION) だけです。(id=${String(r.id)}, entity=${String(target.id)}, type=${String(target.type)})`);
|
|
109
|
+
else if (target.type === 'RECURSION' && r.from.entity !== r.to.entity)
|
|
110
|
+
errors.push(`${prefix}relationships: 再帰表 (RECURSION) は自己結線 (from と to が同じエンティティ) にだけ指定できます。(id=${String(r.id)}, from=${String(r.from.entity)}, to=${String(r.to.entity)})`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// TM の「クラス」は切り出し元(親)としか結線できない(d3.ter の Rectum.assertClassLink と同じ判定)
|
|
114
|
+
const from = ends.from;
|
|
115
|
+
const to = ends.to;
|
|
116
|
+
|
|
117
|
+
if (from && to && (from.type === 'CLASS' || to.type === 'CLASS')) {
|
|
118
|
+
if (from.type === 'CLASS' && to.type === 'CLASS') {
|
|
119
|
+
errors.push(`${prefix}relationships: クラス (CLASS) 同士は結線できません。(id=${String(r.id)})`);
|
|
120
|
+
} else {
|
|
121
|
+
const klass = from.type === 'CLASS' ? from : to;
|
|
122
|
+
const other = from.type === 'CLASS' ? to : from;
|
|
123
|
+
const masters = identifierMasters(klass);
|
|
124
|
+
|
|
125
|
+
if (!(other.identifiers || []).some(d => masters.has(d.identifier)))
|
|
126
|
+
errors.push(`${prefix}relationships: クラス (CLASS) は切り出し元 (親) としか結線できません。(id=${String(r.id)}, entity=${String(other.id)})`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return errors;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** コレクションの中からレコードを id で探す(無ければ null) */
|
|
135
|
+
export function findById<T extends TmRecord>(list: readonly T[] | undefined, id: TmId): T | null {
|
|
136
|
+
return (list || []).find(d => d.id === id) || null;
|
|
137
|
+
}
|