@yrest/cli 0.11.0 → 0.11.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/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
  [![CI](https://github.com/aggiovato/yRest/actions/workflows/ci.yml/badge.svg)](https://github.com/aggiovato/yRest/actions)
11
11
  [![Node](https://img.shields.io/node/v/@yrest/cli)](https://www.npmjs.com/package/@yrest/cli)
12
12
  [![TypeScript](https://img.shields.io/badge/TypeScript-ready-blue)](https://www.typescriptlang.org/)
13
- [![Socket](https://badge.socket.dev/npm/package/@yrest/cli/0.11.0)](https://socket.dev/npm/package/@yrest/cli)
13
+ [![Socket](https://badge.socket.dev/npm/package/@yrest/cli/0.11.1)](https://socket.dev/npm/package/@yrest/cli)
14
14
 
15
15
  YAML-powered json-server alternative. Zero-config REST API mock server with full CRUD, relations, filters and snapshots from a `db.yml` file.
16
16
 
@@ -0,0 +1,3 @@
1
+ <svg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M75 40C75 59.33 59.33 75 40 75C20.67 75 5 59.33 5 40C5 20.67 20.67 5 40 5C59.33 5 75 20.67 75 40ZM23 57C24.6569 57 26 55.6569 26 54V43H33.3781C34.7102 48.1757 39.4085 52 45 52C50.5915 52 55.2898 48.1757 56.6219 43H66C67.6569 43 69 41.6569 69 40C69 38.3431 67.6569 37 66 37H56.6219C55.2898 31.8243 50.5915 28 45 28C39.4085 28 34.7102 31.8243 33.3781 37H26V26C26 24.3431 24.6569 23 23 23C21.3431 23 20 24.3431 20 26V37H15C13.3431 37 12 38.3431 12 40C12 41.6569 13.3431 43 15 43H20V54C20 55.6569 21.3431 57 23 57ZM51 40C51 41.0929 50.7078 42.1175 50.1973 43C49.1599 44.7934 47.2208 46 45 46C42.7792 46 40.8401 44.7934 39.8027 43C39.2922 42.1175 39 41.0929 39 40C39 38.9071 39.2922 37.8825 39.8027 37C40.8401 35.2066 42.7792 34 45 34C47.2208 34 49.1599 35.2066 50.1973 37C50.7078 37.8825 51 38.9071 51 40Z" fill="black"/>
3
+ </svg>
@@ -0,0 +1,3 @@
1
+ <svg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M40 75C59.33 75 75 59.33 75 40C75 20.67 59.33 5 40 5C20.67 5 5 20.67 5 40C5 59.33 20.67 75 40 75ZM17 42.9025C15.3431 42.9025 14 41.5594 14 39.9025C14 38.2457 15.3431 36.9025 17 36.9025H32.9862L15.5981 26.5986C14.1632 25.7702 13.6716 23.9354 14.5 22.5005C15.3284 21.0656 17.1632 20.574 18.5981 21.4024L45.4091 36.8818L45.4036 36.9025H45.445L45.4091 36.8818C46.7819 31.7662 51.4511 28 57 28C62.579 28 67.2687 31.8072 68.6129 36.9652C69.9758 37.2481 71 38.4557 71 39.9025C71 41.331 70.0017 42.5262 68.6647 42.8286C67.3931 48.091 62.6531 52 57 52C51.3734 52 46.6513 48.1276 45.3534 42.9025H44.9862L18.6801 58.0904C17.2452 58.9188 15.4105 58.4272 14.582 56.9923C13.7536 55.5574 14.2452 53.7227 15.6801 52.8942L32.9862 42.9025H17ZM62.1397 36.9025C61.0891 35.163 59.1804 34 57 34C54.8196 34 52.9109 35.163 51.8603 36.9025C51.3142 37.8067 51 38.8667 51 40C51 41.0529 51.2712 42.0424 51.7475 42.9025C52.7704 44.7496 54.7392 46 57 46C59.2608 46 61.2296 44.7496 62.2525 42.9025C62.7288 42.0424 63 41.0529 63 40C63 38.8667 62.6858 37.8067 62.1397 36.9025Z" fill="black"/>
3
+ </svg>
@@ -0,0 +1,3 @@
1
+ <svg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M75 40C75 59.33 59.33 75 40 75C20.67 75 5 59.33 5 40C5 20.67 20.67 5 40 5C59.33 5 75 20.67 75 40ZM41 26C41 24.3431 39.6569 23 38 23C36.3431 23 35 24.3431 35 26V37H26V26C26 24.3431 24.6569 23 23 23C21.3431 23 20 24.3431 20 26V37H15C13.3431 37 12 38.3431 12 40C12 41.6569 13.3431 43 15 43H20V54C20 55.6569 21.3431 57 23 57C24.6569 57 26 55.6569 26 54V43H35V54C35 55.6569 36.3431 57 38 57C39.6569 57 41 55.6569 41 54V43H66C67.6569 43 69 41.6569 69 40C69 38.3431 67.6569 37 66 37H41V26Z" fill="black"/>
3
+ </svg>
@@ -0,0 +1,3 @@
1
+ <svg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M40 75C59.33 75 75 59.33 75 40C75 20.67 59.33 5 40 5C20.67 5 5 20.67 5 40C5 59.33 20.67 75 40 75ZM44.9862 42.9025L18.6801 58.0904C17.2452 58.9188 15.4105 58.4272 14.582 56.9923C13.7536 55.5574 14.2452 53.7227 15.6801 52.8942L32.9862 42.9025H17C15.3431 42.9025 14 41.5594 14 39.9025C14 38.2457 15.3431 36.9025 17 36.9025H32.9862L15.5981 26.5986C14.1632 25.7702 13.6716 23.9354 14.5 22.5005C15.3284 21.0656 17.1632 20.574 18.5981 21.4024L44.9862 36.9025H46V26C46 24.3431 47.3431 23 49 23C50.6569 23 52 24.3431 52 26V36.9025H68C69.6569 36.9025 71 38.2457 71 39.9025C71 41.5594 69.6569 42.9025 68 42.9025H52V53C52 54.6569 50.6569 56 49 56C47.3431 56 46 54.6569 46 53V42.9025H44.9862Z" fill="black"/>
3
+ </svg>
@@ -0,0 +1,3 @@
1
+ <svg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M40 75C59.33 75 75 59.33 75 40C75 20.67 59.33 5 40 5C20.67 5 5 20.67 5 40C5 59.33 20.67 75 40 75ZM17 36.9025H33.0677L15.5981 26.5986C14.1632 25.7702 13.6716 23.9354 14.5 22.5005C15.3284 21.0656 17.1632 20.574 18.5981 21.4024L44.9862 36.9025H68C69.6569 36.9025 71 38.2457 71 39.9025C71 41.5594 69.6569 42.9025 68 42.9025H44.9862L18.6801 58.0904C17.2452 58.9188 15.4105 58.4272 14.582 56.9923C13.7536 55.5574 14.2452 53.7227 15.6801 52.8942L32.9862 42.9025H17C15.3431 42.9025 14 41.5594 14 39.9025C14 38.2457 15.3431 36.9025 17 36.9025Z" fill="black"/>
3
+ </svg>
package/dist/cli/index.js CHANGED
@@ -1223,9 +1223,15 @@ function generateERData(storage) {
1223
1223
  const relations = storage.getRelations();
1224
1224
  const schema = storage.getSchema();
1225
1225
  const pivotTables = /* @__PURE__ */ new Set();
1226
+ const pivotFKs = /* @__PURE__ */ new Map();
1226
1227
  for (const fields of Object.values(relations)) {
1227
1228
  for (const def of Object.values(fields)) {
1228
- if (def.type === "many2many") pivotTables.add(def.through);
1229
+ if (def.type === "many2many") {
1230
+ pivotTables.add(def.through);
1231
+ if (!pivotFKs.has(def.through)) pivotFKs.set(def.through, /* @__PURE__ */ new Set());
1232
+ pivotFKs.get(def.through).add(def.foreignKey);
1233
+ pivotFKs.get(def.through).add(def.otherKey);
1234
+ }
1229
1235
  }
1230
1236
  }
1231
1237
  const fkFields = /* @__PURE__ */ new Map();
@@ -1261,6 +1267,9 @@ function generateERData(storage) {
1261
1267
  for (const [source, fields] of Object.entries(relations)) {
1262
1268
  for (const [key, def] of Object.entries(fields)) {
1263
1269
  if (def.type === "many2many") {
1270
+ const throughRels = relations[def.through] ?? {};
1271
+ const fkRel = throughRels[def.foreignKey];
1272
+ const otherRel = throughRels[def.otherKey];
1264
1273
  erRelations.push({
1265
1274
  source,
1266
1275
  target: def.target,
@@ -1268,9 +1277,14 @@ function generateERData(storage) {
1268
1277
  label: def.through,
1269
1278
  through: def.through,
1270
1279
  foreignKey: def.foreignKey,
1271
- otherKey: def.otherKey
1280
+ otherKey: def.otherKey,
1281
+ fkCarDirect: fkRel?.carDirect,
1282
+ fkCarInverse: fkRel?.carInverse,
1283
+ otherCarDirect: otherRel?.carDirect,
1284
+ otherCarInverse: otherRel?.carInverse
1272
1285
  });
1273
1286
  } else {
1287
+ if (pivotFKs.get(source)?.has(key)) continue;
1274
1288
  erRelations.push({
1275
1289
  source,
1276
1290
  target: def.target,
@@ -1296,7 +1310,15 @@ function erDiagramSection(data) {
1296
1310
 
1297
1311
  <div class="card" style="padding:0;overflow:hidden;position:relative;">
1298
1312
  <canvas id="er-canvas" style="width:100%;display:block;cursor:grab;"></canvas>
1299
- <button id="er-dl" title="Download PNG" style="position:absolute;top:10px;right:14px;background:#161b22;border:1px solid #30363d;color:#7d8590;padding:4px 10px;border-radius:6px;font-size:11px;cursor:pointer;line-height:1.4;">\u2193 PNG</button>
1313
+ <div style="position:absolute;top:10px;right:14px;display:flex;gap:6px;align-items:center;">
1314
+ <select id="er-scale" title="Download quality" style="background:#161b22;border:1px solid #30363d;color:#7d8590;padding:3px 6px;border-radius:6px;font-size:11px;cursor:pointer;">
1315
+ <option value="1">1\xD7</option>
1316
+ <option value="2">2\xD7</option>
1317
+ <option value="3" selected>3\xD7</option>
1318
+ <option value="4">4\xD7</option>
1319
+ </select>
1320
+ <button id="er-dl" title="Download PNG" style="background:#161b22;border:1px solid #30363d;color:#7d8590;padding:4px 10px;border-radius:6px;font-size:11px;cursor:pointer;line-height:1.4;">\u2193 PNG</button>
1321
+ </div>
1300
1322
  </div>
1301
1323
 
1302
1324
  <script>${/*js*/
@@ -1306,7 +1328,7 @@ function erDiagramSection(data) {
1306
1328
  var D=${json};
1307
1329
 
1308
1330
  // \u2500\u2500 Constants \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
1309
- var NW=178,HH=34,FH=22,FP=10,ROW_GAP=44,COL_GAP=100,BR=8,BO=14,LGH=56;
1331
+ var NW=178,HH=34,FH=22,FP=10,ROW_GAP=44,COL_GAP=100,BR=12,BO=16,LGH=84,MIN_GAP=10;
1310
1332
 
1311
1333
  var C={bg:'#0d1117',nb:'#161b22',nh:'#1c2128',pb:'#0d1117',ph:'#161b22',
1312
1334
  bo:'#30363d',bs:'#58a6ff',tx:'#e6edf3',mu:'#7d8590',
@@ -1346,6 +1368,30 @@ function erDiagramSection(data) {
1346
1368
  }
1347
1369
  });
1348
1370
 
1371
+ // \u2500\u2500 Dynamic node width \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
1372
+ function computeNodeWidths(){
1373
+ nodes.forEach(function(n){
1374
+ var e=n.e;
1375
+ ctx.font='600 12px -apple-system,BlinkMacSystemFont,sans-serif';
1376
+ var hdrW=FP+ctx.measureText(e.name).width+FP;
1377
+ if(e.isPivot){
1378
+ ctx.font='9px monospace';
1379
+ hdrW+=ctx.measureText('pivot').width+FP;
1380
+ }
1381
+ var maxRow=hdrW;
1382
+ e.fields.forEach(function(f){
1383
+ var leftPad=FP+(f.pk||f.fk?16:0);
1384
+ var rightPad=(f.required&&!f.pk?14:0)+FP;
1385
+ ctx.font=(f.required&&!f.pk?'600 ':'')+'11px monospace';
1386
+ var nw=ctx.measureText(f.name).width;
1387
+ ctx.font='10px monospace';
1388
+ var tw=ctx.measureText(f.type).width;
1389
+ maxRow=Math.max(maxRow,leftPad+nw+MIN_GAP+tw+rightPad);
1390
+ });
1391
+ n.w=Math.max(NW,Math.ceil(maxRow));
1392
+ });
1393
+ }
1394
+
1349
1395
  // \u2500\u2500 Layered layout (simplified Sugiyama) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
1350
1396
  var groups=[];
1351
1397
 
@@ -1384,7 +1430,8 @@ function erDiagramSection(data) {
1384
1430
  median(adj[b].map(function(x){return np[x]!=null?np[x]:0;}));
1385
1431
  });
1386
1432
  }
1387
- var colW=NW+COL_GAP;
1433
+ var maxNW=nodes.reduce(function(m,n){return Math.max(m,n.w);},NW);
1434
+ var colW=maxNW+COL_GAP;
1388
1435
  var totalW=(maxL+1)*colW-COL_GAP;
1389
1436
  var startX=Math.max(20,(W-totalW)/2);
1390
1437
  for(var l=0;l<=maxL;l++){
@@ -1432,37 +1479,37 @@ function erDiagramSection(data) {
1432
1479
 
1433
1480
  var badges=[];
1434
1481
 
1435
- function pushBadge(x,y,ux,uy,many,col){
1436
- badges.push({x:x,y:y,ux:ux,uy:uy,many:many,col:col});
1482
+ // \u2500\u2500 SVG cardinality badges via Path2D \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
1483
+ // SVG orientation: LEFT side touches the node, RIGHT side connects to the line.
1484
+ // Rotation = Math.atan2(uy,ux) aligns local +x with the outward (away-from-node) direction.
1485
+ var P2D={
1486
+ '1..1':new Path2D('M75 40C75 59.33 59.33 75 40 75C20.67 75 5 59.33 5 40C5 20.67 20.67 5 40 5C59.33 5 75 20.67 75 40ZM41 26C41 24.3431 39.6569 23 38 23C36.3431 23 35 24.3431 35 26V37H26V26C26 24.3431 24.6569 23 23 23C21.3431 23 20 24.3431 20 26V37H15C13.3431 37 12 38.3431 12 40C12 41.6569 13.3431 43 15 43H20V54C20 55.6569 21.3431 57 23 57C24.6569 57 26 55.6569 26 54V43H35V54C35 55.6569 36.3431 57 38 57C39.6569 57 41 55.6569 41 54V43H66C67.6569 43 69 41.6569 69 40C69 38.3431 67.6569 37 66 37H41V26Z'),
1487
+ '1..n':new Path2D('M40 75C59.33 75 75 59.33 75 40C75 20.67 59.33 5 40 5C20.67 5 5 20.67 5 40C5 59.33 20.67 75 40 75ZM44.9862 42.9025L18.6801 58.0904C17.2452 58.9188 15.4105 58.4272 14.582 56.9923C13.7536 55.5574 14.2452 53.7227 15.6801 52.8942L32.9862 42.9025H17C15.3431 42.9025 14 41.5594 14 39.9025C14 38.2457 15.3431 36.9025 17 36.9025H32.9862L15.5981 26.5986C14.1632 25.7702 13.6716 23.9354 14.5 22.5005C15.3284 21.0656 17.1632 20.574 18.5981 21.4024L44.9862 36.9025H46V26C46 24.3431 47.3431 23 49 23C50.6569 23 52 24.3431 52 26V36.9025H68C69.6569 36.9025 71 38.2457 71 39.9025C71 41.5594 69.6569 42.9025 68 42.9025H52V53C52 54.6569 50.6569 56 49 56C47.3431 56 46 54.6569 46 53V42.9025H44.9862Z'),
1488
+ '0..1':new Path2D('M75 40C75 59.33 59.33 75 40 75C20.67 75 5 59.33 5 40C5 20.67 20.67 5 40 5C59.33 5 75 20.67 75 40ZM23 57C24.6569 57 26 55.6569 26 54V43H33.3781C34.7102 48.1757 39.4085 52 45 52C50.5915 52 55.2898 48.1757 56.6219 43H66C67.6569 43 69 41.6569 69 40C69 38.3431 67.6569 37 66 37H56.6219C55.2898 31.8243 50.5915 28 45 28C39.4085 28 34.7102 31.8243 33.3781 37H26V26C26 24.3431 24.6569 23 23 23C21.3431 23 20 24.3431 20 26V37H15C13.3431 37 12 38.3431 12 40C12 41.6569 13.3431 43 15 43H20V54C20 55.6569 21.3431 57 23 57ZM51 40C51 41.0929 50.7078 42.1175 50.1973 43C49.1599 44.7934 47.2208 46 45 46C42.7792 46 40.8401 44.7934 39.8027 43C39.2922 42.1175 39 41.0929 39 40C39 38.9071 39.2922 37.8825 39.8027 37C40.8401 35.2066 42.7792 34 45 34C47.2208 34 49.1599 35.2066 50.1973 37C50.7078 37.8825 51 38.9071 51 40Z'),
1489
+ '0..n':new Path2D('M40 75C59.33 75 75 59.33 75 40C75 20.67 59.33 5 40 5C20.67 5 5 20.67 5 40C5 59.33 20.67 75 40 75ZM17 42.9025C15.3431 42.9025 14 41.5594 14 39.9025C14 38.2457 15.3431 36.9025 17 36.9025H32.9862L15.5981 26.5986C14.1632 25.7702 13.6716 23.9354 14.5 22.5005C15.3284 21.0656 17.1632 20.574 18.5981 21.4024L45.4091 36.8818L45.4036 36.9025H45.445L45.4091 36.8818C46.7819 31.7662 51.4511 28 57 28C62.579 28 67.2687 31.8072 68.6129 36.9652C69.9758 37.2481 71 38.4557 71 39.9025C71 41.331 70.0017 42.5262 68.6647 42.8286C67.3931 48.091 62.6531 52 57 52C51.3734 52 46.6513 48.1276 45.3534 42.9025H44.9862L18.6801 58.0904C17.2452 58.9188 15.4105 58.4272 14.582 56.9923C13.7536 55.5574 14.2452 53.7227 15.6801 52.8942L32.9862 42.9025H17ZM62.1397 36.9025C61.0891 35.163 59.1804 34 57 34C54.8196 34 52.9109 35.163 51.8603 36.9025C51.3142 37.8067 51 38.8667 51 40C51 41.0529 51.2712 42.0424 51.7475 42.9025C52.7704 44.7496 54.7392 46 57 46C59.2608 46 61.2296 44.7496 62.2525 42.9025C62.7288 42.0424 63 41.0529 63 40C63 38.8667 62.6858 37.8067 62.1397 36.9025Z'),
1490
+ 'n':new Path2D('M40 75C59.33 75 75 59.33 75 40C75 20.67 59.33 5 40 5C20.67 5 5 20.67 5 40C5 59.33 20.67 75 40 75ZM17 36.9025H33.0677L15.5981 26.5986C14.1632 25.7702 13.6716 23.9354 14.5 22.5005C15.3284 21.0656 17.1632 20.574 18.5981 21.4024L44.9862 36.9025H68C69.6569 36.9025 71 38.2457 71 39.9025C71 41.5594 69.6569 42.9025 68 42.9025H44.9862L18.6801 58.0904C17.2452 58.9188 15.4105 58.4272 14.582 56.9923C13.7536 55.5574 14.2452 53.7227 15.6801 52.8942L32.9862 42.9025H17C15.3431 42.9025 14 41.5594 14 39.9025C14 38.2457 15.3431 36.9025 17 36.9025Z')
1491
+ };
1492
+
1493
+ function pushBadge(x,y,ux,uy,car,col){
1494
+ badges.push({x:x,y:y,ux:ux,uy:uy,car:car,col:col});
1437
1495
  }
1438
1496
 
1439
- // Badge: solid colored circle; crow's foot (many) or double-bar (one) in dark ink inside.
1440
- // ux,uy points outward from the node \u2014 badge is offset BO px into the gap.
1441
- // Crow's foot tines open toward the node (-x in rotated frame), tip toward line (+x).
1442
1497
  function drawBadge(b){
1443
- var bx=b.x+b.ux*BO,by=b.y+b.uy*BO;
1444
- ctx.fillStyle=b.col;ctx.strokeStyle=b.col;ctx.lineWidth=1;
1445
- ctx.beginPath();ctx.arc(bx,by,BR,0,Math.PI*2);ctx.fill();ctx.stroke();
1446
- ctx.save();ctx.translate(bx,by);ctx.rotate(Math.atan2(b.uy,b.ux));
1447
- ctx.strokeStyle=C.bg;ctx.lineWidth=1.4;ctx.lineCap='round';
1448
- ctx.beginPath();
1449
- if(b.many){
1450
- ctx.moveTo(-5,-4);ctx.lineTo(5,0); // upper tine \u2192 tip
1451
- ctx.moveTo(-5,4);ctx.lineTo(5,0); // lower tine \u2192 tip
1452
- ctx.moveTo(-5,0);ctx.lineTo(5,0); // center line
1453
- ctx.moveTo(-5,-4);ctx.lineTo(-5,4); // base bar (near node)
1454
- }else{
1455
- ctx.moveTo(-3,-5);ctx.lineTo(-3,5); // near bar
1456
- ctx.moveTo(2,-5);ctx.lineTo(2,5); // far bar
1457
- }
1458
- ctx.stroke();
1498
+ var p=P2D[b.car]||P2D['n'],sc=BR/40;
1499
+ ctx.save();
1500
+ ctx.translate(b.x+b.ux*BO,b.y+b.uy*BO);
1501
+ ctx.rotate(Math.atan2(b.uy,b.ux));
1502
+ ctx.scale(sc,sc);
1503
+ ctx.translate(-40,-40);
1504
+ ctx.fillStyle=b.col;
1505
+ ctx.fill(p,'evenodd');
1459
1506
  ctx.restore();
1460
1507
  }
1461
1508
 
1462
1509
  function ecol(t){return t==='one2one'?C.o2o:t==='many2many'?C.m2m:C.m2o;}
1463
1510
 
1464
1511
  // offset: pixels to shift perpendicular to the edge direction (for parallel edges)
1465
- function drawEdgeLine(a,b,col,dashed,label,manyA,manyB,off){
1512
+ function drawEdgeLine(a,b,col,dashed,label,carA,carB,off){
1466
1513
  var p1=edgePt(a,b.x+b.w/2,b.y+b.h/2);
1467
1514
  var p2=edgePt(b,a.x+a.w/2,a.y+a.h/2);
1468
1515
  var dx=p2.x-p1.x,dy=p2.y-p1.y,d=Math.sqrt(dx*dx+dy*dy)||1;
@@ -1473,13 +1520,19 @@ function erDiagramSection(data) {
1473
1520
  ctx.setLineDash(dashed?[5,4]:[]);
1474
1521
  ctx.beginPath();ctx.moveTo(q1x,q1y);ctx.lineTo(q2x,q2y);ctx.stroke();
1475
1522
  ctx.setLineDash([]);
1476
- pushBadge(q1x,q1y,ux,uy,manyA,col);
1477
- pushBadge(q2x,q2y,-ux,-uy,manyB,col);
1523
+ pushBadge(q1x,q1y,ux,uy,carA,col);
1524
+ pushBadge(q2x,q2y,-ux,-uy,carB,col);
1478
1525
  if(label){
1479
1526
  var mx=(q1x+q2x)/2,my=(q1y+q2y)/2;
1527
+ var la=Math.atan2(uy,ux);
1528
+ if(la>Math.PI/2||la<-Math.PI/2)la+=Math.PI; // keep text readable
1529
+ var ls=off>0?11:-11; // alternate above/below for parallel edges
1530
+ ctx.save();
1531
+ ctx.translate(mx,my);
1532
+ ctx.rotate(la);
1480
1533
  ctx.fillStyle=C.mu;ctx.font='10px monospace';ctx.textAlign='center';ctx.textBaseline='middle';
1481
- ctx.fillText(label,mx-uy*14,my+ux*14);
1482
- ctx.textBaseline='alphabetic';
1534
+ ctx.fillText(label,0,ls);
1535
+ ctx.restore();
1483
1536
  }
1484
1537
  }
1485
1538
 
@@ -1495,14 +1548,14 @@ function erDiagramSection(data) {
1495
1548
  var col=ecol(r.type);
1496
1549
  if(r.type==='many2many'&&r.through&&nIdx[r.through]!=null){
1497
1550
  var sn=nodes[nIdx[r.source]],pn=nodes[nIdx[r.through]],tn=nodes[nIdx[r.target]];
1498
- if(sn&&pn)edges.push({a:sn,b:pn,col:col,dashed:true,label:r.foreignKey||'',manyA:false,manyB:true,off:0});
1499
- if(tn&&pn)edges.push({a:tn,b:pn,col:col,dashed:true,label:r.otherKey||'',manyA:false,manyB:true,off:0});
1551
+ if(sn&&pn)edges.push({a:sn,b:pn,col:col,dashed:true,label:r.foreignKey||'',carA:r.fkCarDirect||'1..1',carB:r.fkCarInverse||'0..n',off:0});
1552
+ if(tn&&pn)edges.push({a:tn,b:pn,col:col,dashed:true,label:r.otherKey||'',carA:r.otherCarDirect||'1..1',carB:r.otherCarInverse||'0..n',off:0});
1500
1553
  }else{
1501
1554
  var sn=nodes[nIdx[r.source]],tn=nodes[nIdx[r.target]];
1502
1555
  if(sn&&tn&&sn!==tn){
1503
- var manyA=r.carInverse?r.carInverse.endsWith('n'):r.type!=='one2one';
1504
- var manyB=r.carDirect?r.carDirect.endsWith('n'):r.type==='many2many';
1505
- edges.push({a:sn,b:tn,col:col,dashed:false,label:r.label,manyA:manyA,manyB:manyB,off:0});
1556
+ var carA=r.carInverse||(r.type!=='one2one'?'1..n':'1..1');
1557
+ var carB=r.carDirect||(r.type==='many2many'?'1..n':'1..1');
1558
+ edges.push({a:sn,b:tn,col:col,dashed:false,label:r.label,carA:carA,carB:carB,off:0});
1506
1559
  }
1507
1560
  }
1508
1561
  });
@@ -1561,12 +1614,24 @@ function erDiagramSection(data) {
1561
1614
 
1562
1615
  function drawLegend(){
1563
1616
  var cvW=cv.width/dpr,cvH=cv.height/dpr;
1564
- var y0=cvH-LGH,y1=cvH-LGH+18,y2=cvH-LGH+38;
1617
+ var yL=cvH-LGH,y1=yL+13,y2=yL+35,y3=yL+62;
1565
1618
 
1566
1619
  ctx.fillStyle='#161b22';
1567
- ctx.fillRect(0,y0,cvW,LGH);
1620
+ ctx.fillRect(0,yL,cvW,LGH);
1568
1621
  ctx.strokeStyle='#30363d';ctx.lineWidth=1;
1569
- ctx.beginPath();ctx.moveTo(0,y0);ctx.lineTo(cvW,y0);ctx.stroke();
1622
+ ctx.beginPath();ctx.moveTo(0,yL);ctx.lineTo(cvW,yL);ctx.stroke();
1623
+
1624
+ function legendBadge(cx,cy,car,col,angle){
1625
+ var p=P2D[car]||P2D['n'],sc=8/40;
1626
+ ctx.save();
1627
+ ctx.translate(cx,cy);
1628
+ ctx.rotate(angle);
1629
+ ctx.scale(sc,sc);
1630
+ ctx.translate(-40,-40);
1631
+ ctx.fillStyle=col;
1632
+ ctx.fill(p,'evenodd');
1633
+ ctx.restore();
1634
+ }
1570
1635
 
1571
1636
  // Row 1: relation line types
1572
1637
  var types=[
@@ -1585,42 +1650,43 @@ function erDiagramSection(data) {
1585
1650
  x+=18+ctx.measureText(it.l).width+22;
1586
1651
  });
1587
1652
 
1588
- // Row 2: cardinality symbols
1589
- function badge2(cx,cy,many,col){
1590
- ctx.fillStyle=col;ctx.strokeStyle=col;ctx.lineWidth=1;
1591
- ctx.beginPath();ctx.arc(cx,cy,6,0,Math.PI*2);ctx.fill();
1592
- ctx.save();ctx.translate(cx,cy);
1593
- ctx.strokeStyle=C.bg;ctx.lineWidth=1.2;ctx.lineCap='round';
1594
- ctx.beginPath();
1595
- if(many){
1596
- ctx.moveTo(-4,-3);ctx.lineTo(4,0);ctx.moveTo(-4,3);ctx.lineTo(4,0);
1597
- ctx.moveTo(-4,0);ctx.lineTo(4,0);ctx.moveTo(-4,-3);ctx.lineTo(-4,3);
1598
- }else{
1599
- ctx.moveTo(-2,-3);ctx.lineTo(-2,3);ctx.moveTo(2,-3);ctx.lineTo(2,3);
1600
- }
1601
- ctx.stroke();ctx.restore();
1602
- }
1603
-
1604
- ctx.font='10px monospace';ctx.textAlign='left';ctx.textBaseline='middle';
1653
+ // Row 2: relation type examples (pair of badges + line)
1605
1654
  x=14;
1606
1655
  var pairs=[
1607
- {a:false,b:true,col:C.m2o,l:'many2one (N:1)'},
1608
- {a:false,b:false,col:C.o2o,l:'one2one (1:1)'},
1609
- {a:false,b:true,col:C.m2m,l:'pivot edge (1:N)'}
1656
+ {carA:'1..n',carB:'1..1',col:C.m2o,l:'many2one (N:1)'},
1657
+ {carA:'1..1',carB:'1..1',col:C.o2o,l:'one2one (1:1)'},
1658
+ {carA:'1..1',carB:'0..n',col:C.m2m,l:'pivot edge (1:0..N)'}
1610
1659
  ];
1611
1660
  pairs.forEach(function(p){
1612
1661
  ctx.strokeStyle=p.col+'55';ctx.lineWidth=1.2;ctx.setLineDash(p.col===C.m2m?[4,3]:[]);
1613
1662
  ctx.beginPath();ctx.moveTo(x+8,y2);ctx.lineTo(x+56,y2);ctx.stroke();
1614
1663
  ctx.setLineDash([]);
1615
- badge2(x+8,y2,p.a,p.col);
1616
- badge2(x+56,y2,p.b,p.col);
1664
+ legendBadge(x+8,y2,p.carA,p.col,0);
1665
+ legendBadge(x+56,y2,p.carB,p.col,Math.PI);
1617
1666
  ctx.fillStyle=C.mu;ctx.font='10px monospace';ctx.textAlign='left';ctx.textBaseline='middle';
1618
1667
  ctx.fillText(p.l,x+66,y2);
1619
1668
  x+=66+ctx.measureText(p.l).width+24;
1620
1669
  });
1621
1670
 
1671
+ // Row 3: individual symbol meanings
1672
+ var symbols=[
1673
+ {car:'1..1',l:'exactly one'},
1674
+ {car:'0..1',l:'zero or one'},
1675
+ {car:'1..n',l:'one or more'},
1676
+ {car:'0..n',l:'zero or more'},
1677
+ {car:'n', l:'many (pivot)'}
1678
+ ];
1679
+ x=14;
1680
+ ctx.font='10px monospace';ctx.textAlign='left';ctx.textBaseline='middle';
1681
+ symbols.forEach(function(s){
1682
+ legendBadge(x+8,y3,s.car,C.mu,0);
1683
+ ctx.fillStyle=C.mu;
1684
+ ctx.fillText(s.l,x+20,y3);
1685
+ x+=20+ctx.measureText(s.l).width+18;
1686
+ });
1687
+
1622
1688
  ctx.fillStyle='#3d444d';ctx.font='10px monospace';ctx.textAlign='right';ctx.textBaseline='middle';
1623
- ctx.fillText('drag \xB7 scroll to zoom \xB7 dbl-click to reset',cvW-14,y2);
1689
+ ctx.fillText('drag \xB7 scroll to zoom \xB7 dbl-click to reset',cvW-14,y1);
1624
1690
  ctx.textBaseline='alphabetic';
1625
1691
  }
1626
1692
 
@@ -1632,7 +1698,7 @@ function erDiagramSection(data) {
1632
1698
  ctx.save();ctx.translate(pan.x,pan.y);ctx.scale(zm,zm);
1633
1699
  badges=[];
1634
1700
  var edges=collectEdges();
1635
- edges.forEach(function(e){drawEdgeLine(e.a,e.b,e.col,e.dashed,e.label,e.manyA,e.manyB,e.off);});
1701
+ edges.forEach(function(e){drawEdgeLine(e.a,e.b,e.col,e.dashed,e.label,e.carA,e.carB,e.off);});
1636
1702
  nodes.forEach(function(n){drawNode(n);});
1637
1703
  badges.forEach(function(b){drawBadge(b);});
1638
1704
  ctx.restore();
@@ -1678,14 +1744,21 @@ function erDiagramSection(data) {
1678
1744
  });
1679
1745
 
1680
1746
  document.getElementById('er-dl').addEventListener('click',function(){
1681
- var SCALE=3;
1682
- var cw=cv.width/dpr,ch=cv.height/dpr;
1747
+ var SCALE=parseInt(document.getElementById('er-scale').value,10)||3;
1748
+ var pad=36;
1749
+ var bx0=1e9,by0=1e9,bx1=-1e9,by1=-1e9;
1750
+ nodes.forEach(function(n){
1751
+ bx0=Math.min(bx0,n.x);by0=Math.min(by0,n.y);
1752
+ bx1=Math.max(bx1,n.x+n.w);by1=Math.max(by1,n.y+n.h);
1753
+ });
1754
+ var cw=bx1-bx0+pad*2,ch=by1-by0+pad*2+LGH;
1683
1755
  var oc=document.createElement('canvas');
1684
1756
  oc.width=Math.round(cw*SCALE);oc.height=Math.round(ch*SCALE);
1685
- var sav={cv:cv,ctx:ctx,dpr:dpr};
1757
+ var sav={cv:cv,ctx:ctx,dpr:dpr,px:pan.x,py:pan.y,zm:zm};
1686
1758
  cv=oc;ctx=oc.getContext('2d');dpr=SCALE;
1759
+ pan.x=pad-bx0;pan.y=pad-by0;zm=1;
1687
1760
  draw();
1688
- cv=sav.cv;ctx=sav.ctx;dpr=sav.dpr;
1761
+ cv=sav.cv;ctx=sav.ctx;dpr=sav.dpr;pan.x=sav.px;pan.y=sav.py;zm=sav.zm;
1689
1762
  var a=document.createElement('a');
1690
1763
  a.download='er-diagram.png';a.href=oc.toDataURL('image/png');a.click();
1691
1764
  });
@@ -1693,6 +1766,7 @@ function erDiagramSection(data) {
1693
1766
  // \u2500\u2500 Init \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
1694
1767
  requestAnimationFrame(function(){
1695
1768
  resize();
1769
+ computeNodeWidths();
1696
1770
  var r=cv.getBoundingClientRect();
1697
1771
  computeLayout(r.width,r.height);
1698
1772
  var h=requiredH();