@yrest/cli 0.10.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 +56 -25
- package/assets/svg/0..1.svg +3 -0
- package/assets/svg/0..n.svg +3 -0
- package/assets/svg/1..1.svg +3 -0
- package/assets/svg/1..n.svg +3 -0
- package/assets/svg/n.svg +3 -0
- package/assets/yrest-banner.png +0 -0
- package/assets/yrest-lockup.png +0 -0
- package/assets/yrest-lockup.svg +20 -0
- package/assets/yrest-logo.png +0 -0
- package/assets/yrest-monochrome.png +0 -0
- package/dist/cli/index.js +904 -131
- package/dist/cli/index.mjs +904 -131
- package/dist/index.d.mts +8 -10
- package/dist/index.d.ts +8 -10
- package/dist/index.js +864 -81
- package/dist/index.mjs +864 -81
- package/package.json +1 -1
- package/assets/logo-color.png +0 -0
- package/assets/logo-figure.png +0 -0
- package/assets/logo-text.png +0 -0
- package/assets/logo-white.png +0 -0
- package/assets/yRest-banner.png +0 -0
package/dist/cli/index.js
CHANGED
|
@@ -124,11 +124,11 @@ _rel:
|
|
|
124
124
|
userId: users
|
|
125
125
|
# many2many \u2014 posts can have multiple tags via the post_tags pivot
|
|
126
126
|
tags:
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
127
|
+
_type: many2many
|
|
128
|
+
_target: tags
|
|
129
|
+
_through: post_tags
|
|
130
|
+
_foreignKey: postId
|
|
131
|
+
_otherKey: tagId
|
|
132
132
|
comments:
|
|
133
133
|
postId: posts
|
|
134
134
|
userId: users
|
|
@@ -136,8 +136,8 @@ _rel:
|
|
|
136
136
|
# one2one \u2014 each user has exactly one profile
|
|
137
137
|
profiles:
|
|
138
138
|
userId:
|
|
139
|
-
|
|
140
|
-
|
|
139
|
+
_type: one2one
|
|
140
|
+
_target: users
|
|
141
141
|
|
|
142
142
|
users:
|
|
143
143
|
- id: 1
|
|
@@ -286,52 +286,52 @@ _rel:
|
|
|
286
286
|
# many2many \u2014 products belong to multiple categories via pivot
|
|
287
287
|
products:
|
|
288
288
|
categories:
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
289
|
+
_type: many2many
|
|
290
|
+
_target: categories
|
|
291
|
+
_through: product_categories
|
|
292
|
+
_foreignKey: productId
|
|
293
|
+
_otherKey: categoryId
|
|
294
294
|
|
|
295
295
|
_routes:
|
|
296
296
|
# Login with conditional scenarios
|
|
297
|
-
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
-
|
|
297
|
+
- _method: POST
|
|
298
|
+
_path: /auth/login
|
|
299
|
+
_scenarios:
|
|
300
|
+
- _when:
|
|
301
301
|
body.email: admin@example.com
|
|
302
302
|
body.password: secret
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
303
|
+
_response:
|
|
304
|
+
_status: 200
|
|
305
|
+
_body:
|
|
306
306
|
token: tok-admin-abc123
|
|
307
307
|
role: admin
|
|
308
308
|
userId: 1
|
|
309
|
-
-
|
|
309
|
+
- _when:
|
|
310
310
|
body.email: user@example.com
|
|
311
311
|
body.password: secret
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
312
|
+
_response:
|
|
313
|
+
_status: 200
|
|
314
|
+
_body:
|
|
315
315
|
token: tok-user-xyz789
|
|
316
316
|
role: user
|
|
317
317
|
userId: 2
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
318
|
+
_otherwise:
|
|
319
|
+
_status: 401
|
|
320
|
+
_body:
|
|
321
321
|
error: Invalid credentials
|
|
322
322
|
|
|
323
323
|
# Logout \u2014 always 204
|
|
324
|
-
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
324
|
+
- _method: POST
|
|
325
|
+
_path: /auth/logout
|
|
326
|
+
_response:
|
|
327
|
+
_status: 204
|
|
328
328
|
|
|
329
329
|
# Static featured products list
|
|
330
|
-
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
330
|
+
- _method: GET
|
|
331
|
+
_path: /store/featured
|
|
332
|
+
_response:
|
|
333
|
+
_status: 200
|
|
334
|
+
_body:
|
|
335
335
|
- id: 1
|
|
336
336
|
name: Laptop Pro 15
|
|
337
337
|
price: 1299.99
|
|
@@ -346,31 +346,31 @@ _routes:
|
|
|
346
346
|
badge: On Sale
|
|
347
347
|
|
|
348
348
|
# Template variables \u2014 echoes the requested product id
|
|
349
|
-
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
349
|
+
- _method: GET
|
|
350
|
+
_path: /products/:id/summary
|
|
351
|
+
_response:
|
|
352
|
+
_status: 200
|
|
353
|
+
_body:
|
|
354
354
|
productId: "{{params.id}}"
|
|
355
355
|
requestedAt: "{{now}}"
|
|
356
356
|
source: mock
|
|
357
357
|
|
|
358
358
|
# Cancel order with simulated latency and template vars
|
|
359
|
-
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
359
|
+
- _method: POST
|
|
360
|
+
_path: /orders/:id/cancel
|
|
361
|
+
_delay: 500
|
|
362
|
+
_response:
|
|
363
|
+
_status: 200
|
|
364
|
+
_body:
|
|
365
365
|
orderId: "{{params.id}}"
|
|
366
366
|
status: cancelled
|
|
367
367
|
cancelledAt: "{{now}}"
|
|
368
368
|
|
|
369
369
|
# Simulate a service outage for testing error handling
|
|
370
|
-
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
370
|
+
- _method: GET
|
|
371
|
+
_path: /store/inventory/sync
|
|
372
|
+
_error: 503
|
|
373
|
+
_errorBody:
|
|
374
374
|
message: Inventory service temporarily unavailable
|
|
375
375
|
retryAfter: 30
|
|
376
376
|
|
|
@@ -565,33 +565,143 @@ function parseRelations(raw) {
|
|
|
565
565
|
}
|
|
566
566
|
function normaliseRelationDef(key, value) {
|
|
567
567
|
if (typeof value === "string") {
|
|
568
|
-
return { type: "many2one", target: value };
|
|
568
|
+
return value.includes(":") ? parseDslString(value) : { type: "many2one", target: value };
|
|
569
569
|
}
|
|
570
570
|
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
571
571
|
const v = value;
|
|
572
|
-
const type = v["
|
|
573
|
-
const nested = v["
|
|
572
|
+
const type = v["_type"];
|
|
573
|
+
const nested = v["_nested"] === true ? true : void 0;
|
|
574
|
+
const carDirect = typeof v["_car-direct"] === "string" ? v["_car-direct"] : void 0;
|
|
575
|
+
const carInverse = typeof v["_car-inverse"] === "string" ? v["_car-inverse"] : void 0;
|
|
574
576
|
if (type === "many2one" || type === void 0) {
|
|
575
|
-
const target = v["
|
|
577
|
+
const target = v["_target"];
|
|
576
578
|
if (typeof target !== "string") return null;
|
|
577
|
-
|
|
579
|
+
const foreignKey = typeof v["_foreignKey"] === "string" ? v["_foreignKey"] : void 0;
|
|
580
|
+
return compact({ type: "many2one", target, foreignKey, nested, carDirect, carInverse });
|
|
578
581
|
}
|
|
579
582
|
if (type === "one2one") {
|
|
580
|
-
const target = v["
|
|
583
|
+
const target = v["_target"];
|
|
581
584
|
if (typeof target !== "string") return null;
|
|
582
|
-
|
|
585
|
+
const foreignKey = typeof v["_foreignKey"] === "string" ? v["_foreignKey"] : void 0;
|
|
586
|
+
return compact({ type: "one2one", target, foreignKey, nested, carDirect, carInverse });
|
|
583
587
|
}
|
|
584
588
|
if (type === "many2many") {
|
|
585
|
-
const target = typeof v["
|
|
586
|
-
const through = v["
|
|
587
|
-
const foreignKey = v["
|
|
588
|
-
const otherKey = v["
|
|
589
|
+
const target = typeof v["_target"] === "string" ? v["_target"] : key;
|
|
590
|
+
const through = v["_through"];
|
|
591
|
+
const foreignKey = v["_foreignKey"];
|
|
592
|
+
const otherKey = v["_otherKey"];
|
|
589
593
|
if (typeof through !== "string" || typeof foreignKey !== "string" || typeof otherKey !== "string")
|
|
590
594
|
return null;
|
|
591
|
-
return
|
|
595
|
+
return compact({
|
|
596
|
+
type: "many2many",
|
|
597
|
+
target,
|
|
598
|
+
through,
|
|
599
|
+
foreignKey,
|
|
600
|
+
otherKey,
|
|
601
|
+
nested,
|
|
602
|
+
carDirect,
|
|
603
|
+
carInverse
|
|
604
|
+
});
|
|
592
605
|
}
|
|
593
606
|
return null;
|
|
594
607
|
}
|
|
608
|
+
var TYPE_ALIASES = {
|
|
609
|
+
many2one: "many2one",
|
|
610
|
+
m2o: "many2one",
|
|
611
|
+
one2one: "one2one",
|
|
612
|
+
o2o: "one2one",
|
|
613
|
+
many2many: "many2many",
|
|
614
|
+
m2m: "many2many"
|
|
615
|
+
};
|
|
616
|
+
function parseDslString(raw) {
|
|
617
|
+
const nested = raw.endsWith("+nested") ? true : void 0;
|
|
618
|
+
let s = nested ? raw.slice(0, -7) : raw;
|
|
619
|
+
let carDirect;
|
|
620
|
+
let carInverse;
|
|
621
|
+
const carMatch = s.match(/\[([^[\]]+)->([^[\]]+)\]$/);
|
|
622
|
+
if (carMatch) {
|
|
623
|
+
carDirect = carMatch[1];
|
|
624
|
+
carInverse = carMatch[2];
|
|
625
|
+
s = s.slice(0, s.length - carMatch[0].length);
|
|
626
|
+
}
|
|
627
|
+
const colonIdx = s.indexOf(":");
|
|
628
|
+
if (colonIdx === -1) return null;
|
|
629
|
+
const typeAlias = s.slice(0, colonIdx);
|
|
630
|
+
const body = s.slice(colonIdx + 1);
|
|
631
|
+
const type = TYPE_ALIASES[typeAlias];
|
|
632
|
+
if (!type) return null;
|
|
633
|
+
if (type === "many2many") {
|
|
634
|
+
const m = body.match(/^([^@()[\]]+)@([^@()[\]]+)\(([^,)]+),\s*([^,)]+)\)$/);
|
|
635
|
+
if (!m) return null;
|
|
636
|
+
const [, target2, through, foreignKey2, otherKey] = m;
|
|
637
|
+
return compact({
|
|
638
|
+
type: "many2many",
|
|
639
|
+
target: target2,
|
|
640
|
+
through,
|
|
641
|
+
foreignKey: foreignKey2,
|
|
642
|
+
otherKey,
|
|
643
|
+
nested,
|
|
644
|
+
carDirect,
|
|
645
|
+
carInverse
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
const atIdx = body.indexOf("@");
|
|
649
|
+
const target = atIdx === -1 ? body : body.slice(0, atIdx);
|
|
650
|
+
const foreignKey = atIdx === -1 ? void 0 : body.slice(atIdx + 1);
|
|
651
|
+
if (!target) return null;
|
|
652
|
+
return compact({ type, target, foreignKey, nested, carDirect, carInverse });
|
|
653
|
+
}
|
|
654
|
+
function compact(obj) {
|
|
655
|
+
return Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== void 0));
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// src/storage/parseRoutes.ts
|
|
659
|
+
function parseRoutes(raw) {
|
|
660
|
+
if (!Array.isArray(raw)) return [];
|
|
661
|
+
return raw.map(normaliseRoute).filter((r) => r !== null);
|
|
662
|
+
}
|
|
663
|
+
function normaliseRoute(raw) {
|
|
664
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
665
|
+
const v = raw;
|
|
666
|
+
const method = v["_method"];
|
|
667
|
+
const path = v["_path"];
|
|
668
|
+
if (typeof method !== "string" || typeof path !== "string") return null;
|
|
669
|
+
const route = { method, path };
|
|
670
|
+
if (typeof v["_handler"] === "string") route.handler = v["_handler"];
|
|
671
|
+
if (typeof v["_delay"] === "number") route.delay = v["_delay"];
|
|
672
|
+
if (typeof v["_error"] === "number") route.error = v["_error"];
|
|
673
|
+
if (v["_errorBody"] !== void 0) route.errorBody = v["_errorBody"];
|
|
674
|
+
const response = normaliseResponse(v["_response"]);
|
|
675
|
+
if (response) route.response = response;
|
|
676
|
+
const otherwise = normaliseResponse(v["_otherwise"]);
|
|
677
|
+
if (otherwise) route.otherwise = otherwise;
|
|
678
|
+
const scenarios = normaliseScenarios(v["_scenarios"]);
|
|
679
|
+
if (scenarios.length > 0) route.scenarios = scenarios;
|
|
680
|
+
return route;
|
|
681
|
+
}
|
|
682
|
+
function normaliseResponse(raw) {
|
|
683
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return void 0;
|
|
684
|
+
const v = raw;
|
|
685
|
+
const response = {};
|
|
686
|
+
if (typeof v["_status"] === "number") response.status = v["_status"];
|
|
687
|
+
if (v["_body"] !== void 0) response.body = v["_body"];
|
|
688
|
+
if (v["_headers"] && typeof v["_headers"] === "object" && !Array.isArray(v["_headers"]))
|
|
689
|
+
response.headers = v["_headers"];
|
|
690
|
+
return Object.keys(response).length > 0 ? response : void 0;
|
|
691
|
+
}
|
|
692
|
+
function normaliseScenarios(raw) {
|
|
693
|
+
if (!Array.isArray(raw)) return [];
|
|
694
|
+
return raw.map(normaliseScenario).filter((s) => s !== null);
|
|
695
|
+
}
|
|
696
|
+
function normaliseScenario(raw) {
|
|
697
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
698
|
+
const v = raw;
|
|
699
|
+
const when = v["_when"];
|
|
700
|
+
if (!when || typeof when !== "object") return null;
|
|
701
|
+
const response = normaliseResponse(v["_response"]);
|
|
702
|
+
if (!response) return null;
|
|
703
|
+
return { when, response };
|
|
704
|
+
}
|
|
595
705
|
|
|
596
706
|
// src/storage/parseSchema.ts
|
|
597
707
|
function parseSchema(raw) {
|
|
@@ -613,13 +723,13 @@ function normaliseFieldDef(value) {
|
|
|
613
723
|
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
614
724
|
const v = value;
|
|
615
725
|
const def = {};
|
|
616
|
-
if (v["
|
|
617
|
-
if (typeof v["
|
|
618
|
-
def.type = v["
|
|
619
|
-
if (typeof v["
|
|
620
|
-
if (Array.isArray(v["
|
|
621
|
-
if (typeof v["
|
|
622
|
-
if (v["
|
|
726
|
+
if (v["_required"] === true || v["_required"] === false) def.required = v["_required"];
|
|
727
|
+
if (typeof v["_type"] === "string" && ["string", "integer", "number", "boolean", "object", "array"].includes(v["_type"]))
|
|
728
|
+
def.type = v["_type"];
|
|
729
|
+
if (typeof v["_format"] === "string") def.format = v["_format"];
|
|
730
|
+
if (Array.isArray(v["_enum"])) def.enum = v["_enum"];
|
|
731
|
+
if (typeof v["_description"] === "string") def.description = v["_description"];
|
|
732
|
+
if (v["_default"] !== void 0) def.default = v["_default"];
|
|
623
733
|
return def;
|
|
624
734
|
}
|
|
625
735
|
|
|
@@ -631,12 +741,66 @@ function deepCopyData(source) {
|
|
|
631
741
|
}
|
|
632
742
|
|
|
633
743
|
// src/storage/yrestStorage.ts
|
|
744
|
+
function serializeRelations(relations) {
|
|
745
|
+
const out = {};
|
|
746
|
+
for (const [col, fields] of Object.entries(relations)) {
|
|
747
|
+
out[col] = {};
|
|
748
|
+
for (const [key, def] of Object.entries(fields)) {
|
|
749
|
+
out[col][key] = serializeRelationDef(def);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
return out;
|
|
753
|
+
}
|
|
754
|
+
function serializeRelationDef(def) {
|
|
755
|
+
if (def.type === "many2many") {
|
|
756
|
+
const entry2 = {
|
|
757
|
+
_type: def.type,
|
|
758
|
+
_target: def.target,
|
|
759
|
+
_through: def.through,
|
|
760
|
+
_foreignKey: def.foreignKey,
|
|
761
|
+
_otherKey: def.otherKey
|
|
762
|
+
};
|
|
763
|
+
if (def.nested) entry2._nested = true;
|
|
764
|
+
if (def.carDirect) entry2["_car-direct"] = def.carDirect;
|
|
765
|
+
if (def.carInverse) entry2["_car-inverse"] = def.carInverse;
|
|
766
|
+
return entry2;
|
|
767
|
+
}
|
|
768
|
+
const entry = { _type: def.type, _target: def.target };
|
|
769
|
+
if (def.foreignKey) entry._foreignKey = def.foreignKey;
|
|
770
|
+
if (def.nested) entry._nested = true;
|
|
771
|
+
if (def.carDirect) entry["_car-direct"] = def.carDirect;
|
|
772
|
+
if (def.carInverse) entry["_car-inverse"] = def.carInverse;
|
|
773
|
+
return entry;
|
|
774
|
+
}
|
|
775
|
+
function serializeRoutes(routes) {
|
|
776
|
+
return routes.map((r) => {
|
|
777
|
+
const entry = { _method: r.method, _path: r.path };
|
|
778
|
+
if (r.handler) entry._handler = r.handler;
|
|
779
|
+
if (r.delay !== void 0) entry._delay = r.delay;
|
|
780
|
+
if (r.error !== void 0) entry._error = r.error;
|
|
781
|
+
if (r.errorBody !== void 0) entry._errorBody = r.errorBody;
|
|
782
|
+
if (r.response) entry._response = serializeResponse(r.response);
|
|
783
|
+
if (r.otherwise) entry._otherwise = serializeResponse(r.otherwise);
|
|
784
|
+
if (r.scenarios?.length) entry._scenarios = r.scenarios.map(serializeScenario);
|
|
785
|
+
return entry;
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
function serializeResponse(res) {
|
|
789
|
+
const out = {};
|
|
790
|
+
if (res.status !== void 0) out._status = res.status;
|
|
791
|
+
if (res.body !== void 0) out._body = res.body;
|
|
792
|
+
if (res.headers) out._headers = res.headers;
|
|
793
|
+
return out;
|
|
794
|
+
}
|
|
795
|
+
function serializeScenario(s) {
|
|
796
|
+
return { _when: s.when, _response: serializeResponse(s.response) };
|
|
797
|
+
}
|
|
634
798
|
function createYrestStorage(filePath) {
|
|
635
799
|
const absPath = (0, import_node_path2.resolve)(filePath);
|
|
636
800
|
const raw = (0, import_yaml.parse)((0, import_node_fs2.readFileSync)(absPath, "utf8")) ?? {};
|
|
637
801
|
const RESERVED = /* @__PURE__ */ new Set(["_rel", "_routes", "_schema"]);
|
|
638
802
|
const relations = parseRelations(raw["_rel"]);
|
|
639
|
-
const routes =
|
|
803
|
+
const routes = parseRoutes(raw["_routes"]);
|
|
640
804
|
const schema = parseSchema(raw["_schema"]);
|
|
641
805
|
const data = Object.fromEntries(
|
|
642
806
|
Object.entries(raw).filter(([key]) => !RESERVED.has(key))
|
|
@@ -667,8 +831,8 @@ function createYrestStorage(filePath) {
|
|
|
667
831
|
},
|
|
668
832
|
persist() {
|
|
669
833
|
const payload = {};
|
|
670
|
-
if (Object.keys(relations).length > 0) payload._rel = relations;
|
|
671
|
-
if (routes.length > 0) payload._routes = routes;
|
|
834
|
+
if (Object.keys(relations).length > 0) payload._rel = serializeRelations(relations);
|
|
835
|
+
if (routes.length > 0) payload._routes = serializeRoutes(routes);
|
|
672
836
|
Object.assign(payload, data);
|
|
673
837
|
const tmp = (0, import_node_path2.resolve)((0, import_node_path2.dirname)(absPath), `.yrest-${(0, import_node_crypto.randomUUID)()}.tmp`);
|
|
674
838
|
(0, import_node_fs2.writeFileSync)(tmp, (0, import_yaml.stringify)(payload), "utf8");
|
|
@@ -1045,12 +1209,582 @@ curl ${curlFlag}${fullPath}`);
|
|
|
1045
1209
|
return `<pre>${highlighted}</pre>`;
|
|
1046
1210
|
}
|
|
1047
1211
|
|
|
1212
|
+
// src/router/templates/about.diagram.ts
|
|
1213
|
+
function inferType(value) {
|
|
1214
|
+
if (value === null || value === void 0) return "string";
|
|
1215
|
+
if (typeof value === "boolean") return "boolean";
|
|
1216
|
+
if (typeof value === "number") return Number.isInteger(value) ? "integer" : "number";
|
|
1217
|
+
if (Array.isArray(value)) return "array";
|
|
1218
|
+
if (typeof value === "object") return "object";
|
|
1219
|
+
return "string";
|
|
1220
|
+
}
|
|
1221
|
+
function generateERData(storage) {
|
|
1222
|
+
const data = storage.getData();
|
|
1223
|
+
const relations = storage.getRelations();
|
|
1224
|
+
const schema = storage.getSchema();
|
|
1225
|
+
const pivotTables = /* @__PURE__ */ new Set();
|
|
1226
|
+
const pivotFKs = /* @__PURE__ */ new Map();
|
|
1227
|
+
for (const fields of Object.values(relations)) {
|
|
1228
|
+
for (const def of Object.values(fields)) {
|
|
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
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
const fkFields = /* @__PURE__ */ new Map();
|
|
1238
|
+
for (const [source, fields] of Object.entries(relations)) {
|
|
1239
|
+
for (const [key, def] of Object.entries(fields)) {
|
|
1240
|
+
if (def.type !== "many2many") {
|
|
1241
|
+
if (!fkFields.has(source)) fkFields.set(source, /* @__PURE__ */ new Set());
|
|
1242
|
+
fkFields.get(source).add(key);
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
const entities = Object.entries(data).map(([name, items]) => {
|
|
1247
|
+
const sample = items[0] ?? {};
|
|
1248
|
+
const schemaFields = schema[name] ?? {};
|
|
1249
|
+
const allFields = /* @__PURE__ */ new Set([...Object.keys(sample), ...Object.keys(schemaFields)]);
|
|
1250
|
+
const fields = [...allFields].map((fieldName) => ({
|
|
1251
|
+
name: fieldName,
|
|
1252
|
+
type: schemaFields[fieldName]?.type ?? inferType(sample[fieldName]),
|
|
1253
|
+
pk: fieldName === "id",
|
|
1254
|
+
fk: fkFields.get(name)?.has(fieldName) ?? false,
|
|
1255
|
+
required: schemaFields[fieldName]?.required ?? false
|
|
1256
|
+
}));
|
|
1257
|
+
fields.sort((a, b) => {
|
|
1258
|
+
if (a.pk) return -1;
|
|
1259
|
+
if (b.pk) return 1;
|
|
1260
|
+
if (a.fk && !b.fk) return 1;
|
|
1261
|
+
if (!a.fk && b.fk) return -1;
|
|
1262
|
+
return 0;
|
|
1263
|
+
});
|
|
1264
|
+
return { name, fields, isPivot: pivotTables.has(name) };
|
|
1265
|
+
});
|
|
1266
|
+
const erRelations = [];
|
|
1267
|
+
for (const [source, fields] of Object.entries(relations)) {
|
|
1268
|
+
for (const [key, def] of Object.entries(fields)) {
|
|
1269
|
+
if (def.type === "many2many") {
|
|
1270
|
+
const throughRels = relations[def.through] ?? {};
|
|
1271
|
+
const fkRel = throughRels[def.foreignKey];
|
|
1272
|
+
const otherRel = throughRels[def.otherKey];
|
|
1273
|
+
erRelations.push({
|
|
1274
|
+
source,
|
|
1275
|
+
target: def.target,
|
|
1276
|
+
type: "many2many",
|
|
1277
|
+
label: def.through,
|
|
1278
|
+
through: def.through,
|
|
1279
|
+
foreignKey: def.foreignKey,
|
|
1280
|
+
otherKey: def.otherKey,
|
|
1281
|
+
fkCarDirect: fkRel?.carDirect,
|
|
1282
|
+
fkCarInverse: fkRel?.carInverse,
|
|
1283
|
+
otherCarDirect: otherRel?.carDirect,
|
|
1284
|
+
otherCarInverse: otherRel?.carInverse
|
|
1285
|
+
});
|
|
1286
|
+
} else {
|
|
1287
|
+
if (pivotFKs.get(source)?.has(key)) continue;
|
|
1288
|
+
erRelations.push({
|
|
1289
|
+
source,
|
|
1290
|
+
target: def.target,
|
|
1291
|
+
type: def.type,
|
|
1292
|
+
label: key,
|
|
1293
|
+
carDirect: def.carDirect,
|
|
1294
|
+
carInverse: def.carInverse
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
return { entities, relations: erRelations };
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
// src/router/templates/about.diagram.canvas.ts
|
|
1303
|
+
function erDiagramSection(data) {
|
|
1304
|
+
if (!data.entities.length) return "";
|
|
1305
|
+
const json = JSON.stringify(data);
|
|
1306
|
+
return (
|
|
1307
|
+
/*html*/
|
|
1308
|
+
`
|
|
1309
|
+
<h2>Data Model</h2>
|
|
1310
|
+
|
|
1311
|
+
<div class="card" style="padding:0;overflow:hidden;position:relative;">
|
|
1312
|
+
<canvas id="er-canvas" style="width:100%;display:block;cursor:grab;"></canvas>
|
|
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>
|
|
1322
|
+
</div>
|
|
1323
|
+
|
|
1324
|
+
<script>${/*js*/
|
|
1325
|
+
`
|
|
1326
|
+
(function(){
|
|
1327
|
+
|
|
1328
|
+
var D=${json};
|
|
1329
|
+
|
|
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
|
|
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;
|
|
1332
|
+
|
|
1333
|
+
var C={bg:'#0d1117',nb:'#161b22',nh:'#1c2128',pb:'#0d1117',ph:'#161b22',
|
|
1334
|
+
bo:'#30363d',bs:'#58a6ff',tx:'#e6edf3',mu:'#7d8590',
|
|
1335
|
+
pk:'#e3b341',fk:'#79c0ff',m2o:'#58a6ff',o2o:'#34d399',m2m:'#818cf8'};
|
|
1336
|
+
|
|
1337
|
+
// \u2500\u2500 Canvas \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
|
|
1338
|
+
var cv = document.getElementById('er-canvas');
|
|
1339
|
+
var dpr = window.devicePixelRatio||1;
|
|
1340
|
+
var ctx = cv.getContext('2d');
|
|
1341
|
+
|
|
1342
|
+
function resize(h){
|
|
1343
|
+
if(h)cv.style.height = h + 'px';
|
|
1344
|
+
var r = cv.getBoundingClientRect();
|
|
1345
|
+
cv.width = Math.round(r.width*dpr);
|
|
1346
|
+
cv.height = Math.round(r.height*dpr);
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
// \u2500\u2500 Nodes & adjacency \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
|
|
1350
|
+
var nodes = D.entities.map(function(e){
|
|
1351
|
+
return{e:e,x:0,y:0,w:NW,h:HH+e.fields.length*FH+6};
|
|
1352
|
+
});
|
|
1353
|
+
|
|
1354
|
+
var nIdx={};D.entities.forEach(function(e,i){nIdx[e.name]=i;});
|
|
1355
|
+
|
|
1356
|
+
var adj=nodes.map(function(){return[];}); // forward
|
|
1357
|
+
var radj=nodes.map(function(){return[];}); // reverse
|
|
1358
|
+
|
|
1359
|
+
D.relations.forEach(function(r){
|
|
1360
|
+
if(r.type==='many2many'&&r.through&&nIdx[r.through]!=null){
|
|
1361
|
+
var s=nIdx[r.source],p=nIdx[r.through],t=nIdx[r.target];
|
|
1362
|
+
if(s!=null&&p!=null&&s!==p){adj[s].push(p);radj[p].push(s);}
|
|
1363
|
+
if(t!=null&&p!=null&&t!==p){adj[t].push(p);radj[p].push(t);}
|
|
1364
|
+
}else{
|
|
1365
|
+
var s=nIdx[r.source],t=nIdx[r.target];
|
|
1366
|
+
if(s==null||t==null||s===t)return;
|
|
1367
|
+
adj[s].push(t);radj[t].push(s);
|
|
1368
|
+
}
|
|
1369
|
+
});
|
|
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
|
+
|
|
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
|
|
1396
|
+
var groups=[];
|
|
1397
|
+
|
|
1398
|
+
function median(arr){
|
|
1399
|
+
if(!arr.length)return 0;
|
|
1400
|
+
var s=[].concat(arr).sort(function(a,b){return a-b;});
|
|
1401
|
+
return s[Math.floor(s.length/2)];
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
function computeLayout(W,H){
|
|
1405
|
+
var layer=nodes.map(function(){return 0;});
|
|
1406
|
+
var inDeg=nodes.map(function(){return 0;});
|
|
1407
|
+
adj.forEach(function(ts){ts.forEach(function(t){inDeg[t]++;});});
|
|
1408
|
+
var q=[],qi=0;
|
|
1409
|
+
for(var i=0;i<nodes.length;i++)if(inDeg[i]===0)q.push(i);
|
|
1410
|
+
if(!q.length)for(var i=0;i<nodes.length;i++)q.push(i);
|
|
1411
|
+
while(qi<q.length){
|
|
1412
|
+
var u=q[qi++];
|
|
1413
|
+
adj[u].forEach(function(v){if(layer[v]<=layer[u]){layer[v]=layer[u]+1;q.push(v);}});
|
|
1414
|
+
}
|
|
1415
|
+
var maxL=layer.reduce(function(m,l){return Math.max(m,l);},0);
|
|
1416
|
+
groups=[];
|
|
1417
|
+
for(var l=0;l<=maxL;l++)groups.push([]);
|
|
1418
|
+
for(var i=0;i<nodes.length;i++)groups[layer[i]].push(i);
|
|
1419
|
+
for(var l=1;l<=maxL;l++){
|
|
1420
|
+
var pp={};groups[l-1].forEach(function(i,p){pp[i]=p;});
|
|
1421
|
+
groups[l].sort(function(a,b){
|
|
1422
|
+
return median(radj[a].map(function(x){return pp[x]!=null?pp[x]:0;}))-
|
|
1423
|
+
median(radj[b].map(function(x){return pp[x]!=null?pp[x]:0;}));
|
|
1424
|
+
});
|
|
1425
|
+
}
|
|
1426
|
+
for(var l=maxL-1;l>=0;l--){
|
|
1427
|
+
var np={};groups[l+1].forEach(function(i,p){np[i]=p;});
|
|
1428
|
+
groups[l].sort(function(a,b){
|
|
1429
|
+
return median(adj[a].map(function(x){return np[x]!=null?np[x]:0;}))-
|
|
1430
|
+
median(adj[b].map(function(x){return np[x]!=null?np[x]:0;}));
|
|
1431
|
+
});
|
|
1432
|
+
}
|
|
1433
|
+
var maxNW=nodes.reduce(function(m,n){return Math.max(m,n.w);},NW);
|
|
1434
|
+
var colW=maxNW+COL_GAP;
|
|
1435
|
+
var totalW=(maxL+1)*colW-COL_GAP;
|
|
1436
|
+
var startX=Math.max(20,(W-totalW)/2);
|
|
1437
|
+
for(var l=0;l<=maxL;l++){
|
|
1438
|
+
var g=groups[l];
|
|
1439
|
+
var totalH=g.reduce(function(s,i){return s+nodes[i].h;},0)+(g.length-1)*ROW_GAP;
|
|
1440
|
+
var y=Math.max(20,(H-totalH)/2);
|
|
1441
|
+
g.forEach(function(i){nodes[i].x=startX+l*colW;nodes[i].y=y;y+=nodes[i].h+ROW_GAP;});
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
function requiredH(){
|
|
1446
|
+
return Math.max(340,Math.min(720,groups.reduce(function(m,g){
|
|
1447
|
+
return Math.max(m,g.reduce(function(s,i){return s+nodes[i].h;},0)+(g.length-1)*ROW_GAP+LGH+70);
|
|
1448
|
+
},300)));
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
// \u2500\u2500 View \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
|
|
1452
|
+
var pan={x:0,y:0},zm=1;
|
|
1453
|
+
var origPos=[];
|
|
1454
|
+
|
|
1455
|
+
function savePos(){origPos=nodes.map(function(n){return{x:n.x,y:n.y};});}
|
|
1456
|
+
|
|
1457
|
+
function fitView(W,H){
|
|
1458
|
+
var x0=1e9,y0=1e9,x1=-1e9,y1=-1e9;
|
|
1459
|
+
nodes.forEach(function(n){x0=Math.min(x0,n.x);y0=Math.min(y0,n.y);x1=Math.max(x1,n.x+n.w);y1=Math.max(y1,n.y+n.h);});
|
|
1460
|
+
var pad=28,gW=x1-x0+pad*2,gH=y1-y0+pad*2;
|
|
1461
|
+
zm=Math.min(W/gW,(H-LGH)/gH,1);
|
|
1462
|
+
pan.x=(W-gW*zm)/2+pad*zm-x0*zm;
|
|
1463
|
+
pan.y=((H-LGH)-gH*zm)/2+pad*zm-y0*zm;
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
// \u2500\u2500 Rendering \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
|
|
1467
|
+
function rr(x,y,w,h,r){
|
|
1468
|
+
if(ctx.roundRect){ctx.beginPath();ctx.roundRect(x,y,w,h,r);return;}
|
|
1469
|
+
ctx.beginPath();ctx.moveTo(x+r,y);ctx.arcTo(x+w,y,x+w,y+h,r);
|
|
1470
|
+
ctx.arcTo(x+w,y+h,x,y+h,r);ctx.arcTo(x,y+h,x,y,r);ctx.arcTo(x,y,x+w,y,r);ctx.closePath();
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
function edgePt(n,tx,ty){
|
|
1474
|
+
var cx=n.x+n.w/2,cy=n.y+n.h/2,dx=tx-cx,dy=ty-cy;
|
|
1475
|
+
if(!dx&&!dy)return{x:cx,y:cy};
|
|
1476
|
+
var s=Math.min((n.w/2)/Math.abs(dx||.001),(n.h/2)/Math.abs(dy||.001));
|
|
1477
|
+
return{x:cx+dx*s,y:cy+dy*s};
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
var badges=[];
|
|
1481
|
+
|
|
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});
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
function drawBadge(b){
|
|
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');
|
|
1506
|
+
ctx.restore();
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
function ecol(t){return t==='one2one'?C.o2o:t==='many2many'?C.m2m:C.m2o;}
|
|
1510
|
+
|
|
1511
|
+
// offset: pixels to shift perpendicular to the edge direction (for parallel edges)
|
|
1512
|
+
function drawEdgeLine(a,b,col,dashed,label,carA,carB,off){
|
|
1513
|
+
var p1=edgePt(a,b.x+b.w/2,b.y+b.h/2);
|
|
1514
|
+
var p2=edgePt(b,a.x+a.w/2,a.y+a.h/2);
|
|
1515
|
+
var dx=p2.x-p1.x,dy=p2.y-p1.y,d=Math.sqrt(dx*dx+dy*dy)||1;
|
|
1516
|
+
var ux=dx/d,uy=dy/d;
|
|
1517
|
+
var px=-uy*off,py=ux*off; // perpendicular shift
|
|
1518
|
+
var q1x=p1.x+px,q1y=p1.y+py,q2x=p2.x+px,q2y=p2.y+py;
|
|
1519
|
+
ctx.strokeStyle=col+'55';ctx.lineWidth=1.5;
|
|
1520
|
+
ctx.setLineDash(dashed?[5,4]:[]);
|
|
1521
|
+
ctx.beginPath();ctx.moveTo(q1x,q1y);ctx.lineTo(q2x,q2y);ctx.stroke();
|
|
1522
|
+
ctx.setLineDash([]);
|
|
1523
|
+
pushBadge(q1x,q1y,ux,uy,carA,col);
|
|
1524
|
+
pushBadge(q2x,q2y,-ux,-uy,carB,col);
|
|
1525
|
+
if(label){
|
|
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);
|
|
1533
|
+
ctx.fillStyle=C.mu;ctx.font='10px monospace';ctx.textAlign='center';ctx.textBaseline='middle';
|
|
1534
|
+
ctx.fillText(label,0,ls);
|
|
1535
|
+
ctx.restore();
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
// canonical key for a node pair regardless of direction
|
|
1540
|
+
function pairKey(a,b){
|
|
1541
|
+
var ai=nodes.indexOf(a),bi=nodes.indexOf(b);
|
|
1542
|
+
return ai<bi?ai+'-'+bi:bi+'-'+ai;
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
function collectEdges(){
|
|
1546
|
+
var edges=[];
|
|
1547
|
+
D.relations.forEach(function(r){
|
|
1548
|
+
var col=ecol(r.type);
|
|
1549
|
+
if(r.type==='many2many'&&r.through&&nIdx[r.through]!=null){
|
|
1550
|
+
var sn=nodes[nIdx[r.source]],pn=nodes[nIdx[r.through]],tn=nodes[nIdx[r.target]];
|
|
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});
|
|
1553
|
+
}else{
|
|
1554
|
+
var sn=nodes[nIdx[r.source]],tn=nodes[nIdx[r.target]];
|
|
1555
|
+
if(sn&&tn&&sn!==tn){
|
|
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});
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
});
|
|
1562
|
+
// assign perpendicular offsets to parallel edges (same node pair)
|
|
1563
|
+
var cnt={},idx={};
|
|
1564
|
+
edges.forEach(function(e){var k=pairKey(e.a,e.b);cnt[k]=(cnt[k]||0)+1;});
|
|
1565
|
+
edges.forEach(function(e){
|
|
1566
|
+
var k=pairKey(e.a,e.b),n=cnt[k];
|
|
1567
|
+
var i=idx[k]=(idx[k]||0);
|
|
1568
|
+
e.off=n>1?(i-(n-1)/2)*16:0;
|
|
1569
|
+
idx[k]++;
|
|
1570
|
+
});
|
|
1571
|
+
return edges;
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
function drawNode(n){
|
|
1575
|
+
var x=n.x,y=n.y,w=n.w,h=n.h,e=n.e;
|
|
1576
|
+
var bg=e.isPivot?C.pb:C.nb,hd=e.isPivot?C.ph:C.nh;
|
|
1577
|
+
ctx.save();ctx.shadowColor='rgba(0,0,0,0.45)';ctx.shadowBlur=14;ctx.shadowOffsetY=4;
|
|
1578
|
+
ctx.fillStyle=bg;rr(x,y,w,h,7);ctx.fill();ctx.restore();
|
|
1579
|
+
ctx.fillStyle=bg;rr(x,y,w,h,7);ctx.fill();
|
|
1580
|
+
ctx.strokeStyle=n===drag?C.bs:C.bo;ctx.lineWidth=1.2;rr(x,y,w,h,7);ctx.stroke();
|
|
1581
|
+
ctx.fillStyle=hd;rr(x,y,w,HH,7);ctx.fill();ctx.fillRect(x,y+HH-7,w,7);
|
|
1582
|
+
ctx.strokeStyle=C.bo;ctx.lineWidth=1;
|
|
1583
|
+
ctx.beginPath();ctx.moveTo(x,y+HH);ctx.lineTo(x+w,y+HH);ctx.stroke();
|
|
1584
|
+
ctx.fillStyle=e.isPivot?C.mu:C.tx;
|
|
1585
|
+
ctx.font='600 12px -apple-system,BlinkMacSystemFont,sans-serif';
|
|
1586
|
+
ctx.textAlign='left';ctx.textBaseline='middle';
|
|
1587
|
+
ctx.fillText(e.name,x+FP,y+HH/2);
|
|
1588
|
+
if(e.isPivot){
|
|
1589
|
+
ctx.fillStyle='#3d444d';ctx.font='9px monospace';ctx.textAlign='right';
|
|
1590
|
+
ctx.fillText('pivot',x+w-FP,y+HH/2);
|
|
1591
|
+
}
|
|
1592
|
+
e.fields.forEach(function(f,fi){
|
|
1593
|
+
var fy=y+HH+fi*FH+FH/2+3;
|
|
1594
|
+
ctx.fillStyle=C.mu;ctx.font='10px monospace';ctx.textAlign='right';ctx.textBaseline='middle';
|
|
1595
|
+
ctx.fillText(f.type,x+w-FP-(f.required&&!f.pk?14:0),fy);
|
|
1596
|
+
if(f.pk||f.fk){
|
|
1597
|
+
var tag=f.pk?'PK':'FK',tc=f.pk?C.pk:C.fk;
|
|
1598
|
+
ctx.fillStyle=tc+'22';ctx.strokeStyle=tc+'66';ctx.lineWidth=1;
|
|
1599
|
+
rr(x+FP,fy-6,12,12,2);ctx.fill();ctx.stroke();
|
|
1600
|
+
ctx.fillStyle=tc;ctx.font='7px monospace';ctx.textAlign='center';
|
|
1601
|
+
ctx.fillText(tag,x+FP+6,fy+0.5);
|
|
1602
|
+
}
|
|
1603
|
+
ctx.fillStyle=f.pk?C.pk:f.fk?C.fk:C.tx;
|
|
1604
|
+
ctx.font=(f.required&&!f.pk?'600 ':'')+'11px monospace';
|
|
1605
|
+
ctx.textAlign='left';ctx.textBaseline='middle';
|
|
1606
|
+
ctx.fillText(f.name,x+FP+(f.pk||f.fk?16:0),fy);
|
|
1607
|
+
if(f.required&&!f.pk){
|
|
1608
|
+
ctx.fillStyle='#f85149';ctx.beginPath();
|
|
1609
|
+
ctx.arc(x+w-FP-6,fy,2.5,0,Math.PI*2);ctx.fill();
|
|
1610
|
+
}
|
|
1611
|
+
});
|
|
1612
|
+
ctx.textBaseline='alphabetic';
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
function drawLegend(){
|
|
1616
|
+
var cvW=cv.width/dpr,cvH=cv.height/dpr;
|
|
1617
|
+
var yL=cvH-LGH,y1=yL+13,y2=yL+35,y3=yL+62;
|
|
1618
|
+
|
|
1619
|
+
ctx.fillStyle='#161b22';
|
|
1620
|
+
ctx.fillRect(0,yL,cvW,LGH);
|
|
1621
|
+
ctx.strokeStyle='#30363d';ctx.lineWidth=1;
|
|
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
|
+
}
|
|
1635
|
+
|
|
1636
|
+
// Row 1: relation line types
|
|
1637
|
+
var types=[
|
|
1638
|
+
{c:C.m2o,l:'many2one',d:false},
|
|
1639
|
+
{c:C.o2o,l:'one2one',d:false},
|
|
1640
|
+
{c:C.m2m,l:'many2many',d:true}
|
|
1641
|
+
];
|
|
1642
|
+
ctx.textAlign='left';ctx.textBaseline='middle';ctx.font='10px monospace';
|
|
1643
|
+
var x=14;
|
|
1644
|
+
types.forEach(function(it){
|
|
1645
|
+
ctx.strokeStyle=it.c;ctx.lineWidth=1.5;ctx.setLineDash(it.d?[4,3]:[]);
|
|
1646
|
+
ctx.beginPath();ctx.moveTo(x,y1);ctx.lineTo(x+14,y1);ctx.stroke();
|
|
1647
|
+
ctx.setLineDash([]);
|
|
1648
|
+
ctx.fillStyle=C.mu;
|
|
1649
|
+
ctx.fillText(it.l,x+18,y1);
|
|
1650
|
+
x+=18+ctx.measureText(it.l).width+22;
|
|
1651
|
+
});
|
|
1652
|
+
|
|
1653
|
+
// Row 2: relation type examples (pair of badges + line)
|
|
1654
|
+
x=14;
|
|
1655
|
+
var pairs=[
|
|
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)'}
|
|
1659
|
+
];
|
|
1660
|
+
pairs.forEach(function(p){
|
|
1661
|
+
ctx.strokeStyle=p.col+'55';ctx.lineWidth=1.2;ctx.setLineDash(p.col===C.m2m?[4,3]:[]);
|
|
1662
|
+
ctx.beginPath();ctx.moveTo(x+8,y2);ctx.lineTo(x+56,y2);ctx.stroke();
|
|
1663
|
+
ctx.setLineDash([]);
|
|
1664
|
+
legendBadge(x+8,y2,p.carA,p.col,0);
|
|
1665
|
+
legendBadge(x+56,y2,p.carB,p.col,Math.PI);
|
|
1666
|
+
ctx.fillStyle=C.mu;ctx.font='10px monospace';ctx.textAlign='left';ctx.textBaseline='middle';
|
|
1667
|
+
ctx.fillText(p.l,x+66,y2);
|
|
1668
|
+
x+=66+ctx.measureText(p.l).width+24;
|
|
1669
|
+
});
|
|
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
|
+
|
|
1688
|
+
ctx.fillStyle='#3d444d';ctx.font='10px monospace';ctx.textAlign='right';ctx.textBaseline='middle';
|
|
1689
|
+
ctx.fillText('drag \xB7 scroll to zoom \xB7 dbl-click to reset',cvW-14,y1);
|
|
1690
|
+
ctx.textBaseline='alphabetic';
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
function draw(){
|
|
1694
|
+
var W=cv.width/dpr,H=cv.height/dpr;
|
|
1695
|
+
ctx.setTransform(dpr,0,0,dpr,0,0);
|
|
1696
|
+
ctx.clearRect(0,0,W,H);
|
|
1697
|
+
ctx.fillStyle=C.bg;ctx.fillRect(0,0,W,H);
|
|
1698
|
+
ctx.save();ctx.translate(pan.x,pan.y);ctx.scale(zm,zm);
|
|
1699
|
+
badges=[];
|
|
1700
|
+
var edges=collectEdges();
|
|
1701
|
+
edges.forEach(function(e){drawEdgeLine(e.a,e.b,e.col,e.dashed,e.label,e.carA,e.carB,e.off);});
|
|
1702
|
+
nodes.forEach(function(n){drawNode(n);});
|
|
1703
|
+
badges.forEach(function(b){drawBadge(b);});
|
|
1704
|
+
ctx.restore();
|
|
1705
|
+
drawLegend();
|
|
1706
|
+
}
|
|
1707
|
+
|
|
1708
|
+
// \u2500\u2500 Interaction \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
|
|
1709
|
+
var drag=null,dox=0,doy=0,isPan=false,ps={x:0,y:0};
|
|
1710
|
+
|
|
1711
|
+
function toW(mx,my){return{x:(mx-pan.x)/zm,y:(my-pan.y)/zm};}
|
|
1712
|
+
function hitTest(wx,wy){
|
|
1713
|
+
for(var i=nodes.length-1;i>=0;i--){var n=nodes[i];if(wx>=n.x&&wx<=n.x+n.w&&wy>=n.y&&wy<=n.y+n.h)return n;}
|
|
1714
|
+
return null;
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
cv.addEventListener('mousedown',function(ev){
|
|
1718
|
+
var r=cv.getBoundingClientRect(),mx=ev.clientX-r.left,my=ev.clientY-r.top;
|
|
1719
|
+
var w=toW(mx,my),h=hitTest(w.x,w.y);
|
|
1720
|
+
if(h){drag=h;dox=w.x-h.x;doy=w.y-h.y;}else{isPan=true;ps={x:mx-pan.x,y:my-pan.y};}
|
|
1721
|
+
cv.style.cursor='grabbing';
|
|
1722
|
+
});
|
|
1723
|
+
window.addEventListener('mousemove',function(ev){
|
|
1724
|
+
if(!drag&&!isPan)return;
|
|
1725
|
+
var r=cv.getBoundingClientRect(),mx=ev.clientX-r.left,my=ev.clientY-r.top;
|
|
1726
|
+
if(drag){var w=toW(mx,my);drag.x=w.x-dox;drag.y=w.y-doy;}
|
|
1727
|
+
else{pan.x=mx-ps.x;pan.y=my-ps.y;}
|
|
1728
|
+
draw();
|
|
1729
|
+
});
|
|
1730
|
+
window.addEventListener('mouseup',function(){drag=null;isPan=false;cv.style.cursor='grab';});
|
|
1731
|
+
cv.addEventListener('wheel',function(ev){
|
|
1732
|
+
ev.preventDefault();
|
|
1733
|
+
var r=cv.getBoundingClientRect(),mx=ev.clientX-r.left,my=ev.clientY-r.top;
|
|
1734
|
+
var d=ev.deltaY<0?1.1:0.91,nz=Math.max(0.25,Math.min(3,zm*d));
|
|
1735
|
+
pan.x=mx-(mx-pan.x)*(nz/zm);pan.y=my-(my-pan.y)*(nz/zm);zm=nz;draw();
|
|
1736
|
+
},{passive:false});
|
|
1737
|
+
cv.addEventListener('dblclick',function(){
|
|
1738
|
+
nodes.forEach(function(n,i){n.x=origPos[i].x;n.y=origPos[i].y;});
|
|
1739
|
+
var r=cv.getBoundingClientRect();fitView(r.width,r.height);draw();
|
|
1740
|
+
});
|
|
1741
|
+
window.addEventListener('resize',function(){
|
|
1742
|
+
resize();var r=cv.getBoundingClientRect();
|
|
1743
|
+
computeLayout(r.width,r.height);savePos();fitView(r.width,r.height);draw();
|
|
1744
|
+
});
|
|
1745
|
+
|
|
1746
|
+
document.getElementById('er-dl').addEventListener('click',function(){
|
|
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;
|
|
1755
|
+
var oc=document.createElement('canvas');
|
|
1756
|
+
oc.width=Math.round(cw*SCALE);oc.height=Math.round(ch*SCALE);
|
|
1757
|
+
var sav={cv:cv,ctx:ctx,dpr:dpr,px:pan.x,py:pan.y,zm:zm};
|
|
1758
|
+
cv=oc;ctx=oc.getContext('2d');dpr=SCALE;
|
|
1759
|
+
pan.x=pad-bx0;pan.y=pad-by0;zm=1;
|
|
1760
|
+
draw();
|
|
1761
|
+
cv=sav.cv;ctx=sav.ctx;dpr=sav.dpr;pan.x=sav.px;pan.y=sav.py;zm=sav.zm;
|
|
1762
|
+
var a=document.createElement('a');
|
|
1763
|
+
a.download='er-diagram.png';a.href=oc.toDataURL('image/png');a.click();
|
|
1764
|
+
});
|
|
1765
|
+
|
|
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
|
|
1767
|
+
requestAnimationFrame(function(){
|
|
1768
|
+
resize();
|
|
1769
|
+
computeNodeWidths();
|
|
1770
|
+
var r=cv.getBoundingClientRect();
|
|
1771
|
+
computeLayout(r.width,r.height);
|
|
1772
|
+
var h=requiredH();
|
|
1773
|
+
resize(h);r=cv.getBoundingClientRect();
|
|
1774
|
+
computeLayout(r.width,r.height);
|
|
1775
|
+
savePos();fitView(r.width,r.height);draw();
|
|
1776
|
+
});
|
|
1777
|
+
})();
|
|
1778
|
+
`}</script>`
|
|
1779
|
+
);
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1048
1782
|
// src/router/templates/about.template.ts
|
|
1049
1783
|
var _dir = (0, import_node_path3.dirname)((0, import_node_url.fileURLToPath)(importMetaUrl));
|
|
1050
1784
|
var LOGO_SRC = (() => {
|
|
1051
1785
|
try {
|
|
1052
|
-
const buf = (0, import_node_fs3.readFileSync)((0, import_node_path3.join)(_dir, "../../assets/
|
|
1053
|
-
return `data:image/
|
|
1786
|
+
const buf = (0, import_node_fs3.readFileSync)((0, import_node_path3.join)(_dir, "../../assets/yrest-lockup.svg"));
|
|
1787
|
+
return `data:image/svg+xml;base64,${buf.toString("base64")}`;
|
|
1054
1788
|
} catch {
|
|
1055
1789
|
return "";
|
|
1056
1790
|
}
|
|
@@ -1061,6 +1795,7 @@ function generateAboutHtml(storage, options, handlers = /* @__PURE__ */ new Map(
|
|
|
1061
1795
|
const customRoutes = storage.getRoutes();
|
|
1062
1796
|
const base = options.base;
|
|
1063
1797
|
const host = `http://${options.host}:${options.port}`;
|
|
1798
|
+
const swaggerUrl = `https://petstore.swagger.io/?url=${encodeURIComponent(`${host}/_openapi.json`)}`;
|
|
1064
1799
|
const modes = [];
|
|
1065
1800
|
if (options.watch) modes.push(badge("watch", "#38bdf8", "#38bdf818"));
|
|
1066
1801
|
if (options.readonly) modes.push(badge("readonly", "#94a3b8", "#94a3b818"));
|
|
@@ -1071,9 +1806,12 @@ function generateAboutHtml(storage, options, handlers = /* @__PURE__ */ new Map(
|
|
|
1071
1806
|
if (handlers.size > 0) modes.push(badge(`handlers \xB7 ${handlers.size}`, "#f0883e", "#f0883e18"));
|
|
1072
1807
|
if (options.idStrategy !== "increment")
|
|
1073
1808
|
modes.push(badge(`id \xB7 ${options.idStrategy}`, "#a371f7", "#a371f718"));
|
|
1809
|
+
const erDiagram = erDiagramSection(generateERData(storage));
|
|
1074
1810
|
const accordions = collections.map((col, i) => resourceAccordion(col, base, i === 0)).join("");
|
|
1075
1811
|
const paginationDesc = options.pageable.enabled ? `Pageable mode active \u2014 default limit <code>${options.pageable.limit}</code>. Response wrapped in <code>{ data, pagination }</code>.` : `Returns the requested slice. <code>X-Total-Count</code> header reflects the total before pagination.`;
|
|
1076
|
-
return
|
|
1812
|
+
return (
|
|
1813
|
+
/*html*/
|
|
1814
|
+
`<!DOCTYPE html>
|
|
1077
1815
|
<html lang="en">
|
|
1078
1816
|
<head>
|
|
1079
1817
|
<meta charset="UTF-8">
|
|
@@ -1100,20 +1838,30 @@ function generateAboutHtml(storage, options, handlers = /* @__PURE__ */ new Map(
|
|
|
1100
1838
|
width: 100%;
|
|
1101
1839
|
background: linear-gradient(135deg, #0d1117 0%, #161b22 40%, #1a2332 100%);
|
|
1102
1840
|
border-bottom: 1px solid var(--border);
|
|
1103
|
-
padding:
|
|
1841
|
+
padding: 20px 32px 16px;
|
|
1104
1842
|
}
|
|
1105
|
-
.banner-inner {
|
|
1106
|
-
.banner
|
|
1843
|
+
.banner-inner { margin: 0 auto; display:flex; align-items:center; justify-content:space-between; gap:16px; flex-wrap:wrap; }
|
|
1844
|
+
.banner-left { flex:1; min-width:0; }
|
|
1845
|
+
.banner h1 { font-size: clamp(24px, 4vw, 40px); font-weight: 800; letter-spacing: -1.5px; line-height: 1; }
|
|
1107
1846
|
.banner h1 .y { color: var(--text); }
|
|
1108
1847
|
.banner h1 .rest { color: var(--accent); }
|
|
1109
|
-
.banner p { color: var(--text-muted); margin-top:
|
|
1110
|
-
.banner-meta { display: flex; gap:
|
|
1111
|
-
.banner-meta span { color: var(--text-muted); font-size:
|
|
1848
|
+
.banner p { color: var(--text-muted); margin-top: 6px; font-size: 14px; }
|
|
1849
|
+
.banner-meta { display: flex; gap: 20px; margin-top: 10px; flex-wrap: wrap; }
|
|
1850
|
+
.banner-meta span { color: var(--text-muted); font-size: 12px; }
|
|
1112
1851
|
.banner-meta span strong { color: var(--text); font-family: monospace; }
|
|
1852
|
+
.api-links { display:flex; gap:8px; flex-wrap:wrap; align-items:center; }
|
|
1853
|
+
.btn-api { display:inline-block; padding:5px 12px; border-radius:6px; border:1px solid var(--border); color:var(--text-muted); text-decoration:none; font-size:12px; font-weight:500; background:var(--bg-card); white-space:nowrap; transition:border-color .15s,color .15s; }
|
|
1854
|
+
.btn-api:hover { border-color:var(--border-hi); color:var(--text); }
|
|
1855
|
+
.btn-swagger { border-color:var(--accent); color:var(--accent); }
|
|
1856
|
+
.btn-swagger:hover { background:#58a6ff18; }
|
|
1113
1857
|
|
|
1114
1858
|
/* \u2500\u2500 Layout \u2500\u2500 */
|
|
1115
|
-
.wrap {
|
|
1859
|
+
.wrap { padding: 24px 32px 48px; }
|
|
1116
1860
|
h2 { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--text-muted); margin: 32px 0 12px; }
|
|
1861
|
+
.about-grid { display:grid; grid-template-columns:5fr 7fr; gap:28px; align-items:start; }
|
|
1862
|
+
@media(max-width:960px) { .about-grid { grid-template-columns:1fr; } }
|
|
1863
|
+
.col-left, .col-right { min-width:0; }
|
|
1864
|
+
.col-left > h2:first-child, .col-right > h2:first-child { margin-top:0; }
|
|
1117
1865
|
|
|
1118
1866
|
/* \u2500\u2500 Cards \u2500\u2500 */
|
|
1119
1867
|
.card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px 24px; }
|
|
@@ -1129,10 +1877,8 @@ function generateAboutHtml(storage, options, handlers = /* @__PURE__ */ new Map(
|
|
|
1129
1877
|
.modes { display: flex; gap: 8px; flex-wrap: wrap; min-height: 28px; align-items: center; }
|
|
1130
1878
|
.badge { display: inline-block; padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 600; white-space: nowrap; }
|
|
1131
1879
|
|
|
1132
|
-
/* \u2500\u2500 Endpoints
|
|
1133
|
-
.endpoints-grid { display:
|
|
1134
|
-
@media (max-width: 860px) { .endpoints-grid { grid-template-columns: 1fr; } }
|
|
1135
|
-
.nested-card { grid-column: 1 / -1; }
|
|
1880
|
+
/* \u2500\u2500 Endpoints list \u2500\u2500 */
|
|
1881
|
+
.endpoints-grid { display: flex; flex-direction: column; gap: 10px; }
|
|
1136
1882
|
|
|
1137
1883
|
/* \u2500\u2500 Accordion (details/summary) \u2500\u2500 */
|
|
1138
1884
|
.resource-card {
|
|
@@ -1197,64 +1943,80 @@ function generateAboutHtml(storage, options, handlers = /* @__PURE__ */ new Map(
|
|
|
1197
1943
|
|
|
1198
1944
|
<div class="banner">
|
|
1199
1945
|
<div class="banner-inner">
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
<
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1946
|
+
<div class="banner-left">
|
|
1947
|
+
${LOGO_SRC ? `<img src="${LOGO_SRC}" alt="yRest" height="36" style="display:block;margin-bottom:4px" />` : `<h1><span class="y">y</span><span class="rest">Rest</span></h1>`}
|
|
1948
|
+
<p>Zero-config REST API mock server</p>
|
|
1949
|
+
<div class="banner-meta">
|
|
1950
|
+
<span>URL <strong>${host}</strong></span>
|
|
1951
|
+
<span>Base <strong>${base || "/"}</strong></span>
|
|
1952
|
+
<span>File <strong>${options.file}</strong></span>
|
|
1953
|
+
<span>Collections <strong>${collections.length}</strong></span>
|
|
1954
|
+
</div>
|
|
1955
|
+
</div>
|
|
1956
|
+
<div class="api-links">
|
|
1957
|
+
<a href="/_openapi" target="_blank" class="btn-api">OpenAPI YAML</a>
|
|
1958
|
+
<a href="/_openapi.json" target="_blank" class="btn-api">OpenAPI JSON</a>
|
|
1959
|
+
<a href="${swaggerUrl}" target="_blank" class="btn-api btn-swagger">Swagger UI \u2197</a>
|
|
1207
1960
|
</div>
|
|
1208
1961
|
</div>
|
|
1209
1962
|
</div>
|
|
1210
1963
|
|
|
1211
1964
|
<div class="wrap">
|
|
1965
|
+
<div class="about-grid">
|
|
1966
|
+
|
|
1967
|
+
<div class="col-left">
|
|
1968
|
+
<h2>Active Modes</h2>
|
|
1969
|
+
<div class="card">
|
|
1970
|
+
<div class="modes">${modes.length ? modes.join(" ") : `<span style="color:var(--text-muted);font-size:13px">none</span>`}</div>
|
|
1971
|
+
${options.watch ? `<div class="warn">\u26A0 <strong>Watch mode:</strong> data changes in existing collections reload automatically. Adding or removing entire collections requires a server restart.</div>` : ""}
|
|
1972
|
+
</div>
|
|
1973
|
+
|
|
1974
|
+
<h2>Endpoints</h2>
|
|
1975
|
+
<div class="endpoints-grid">
|
|
1976
|
+
${accordions}
|
|
1977
|
+
${nestedRoutesAccordion(relations, base)}
|
|
1978
|
+
${options.snapshot ? snapshotAccordion() : ""}
|
|
1979
|
+
${customRoutesAccordion(customRoutes, base, handlers)}
|
|
1980
|
+
${handlersAccordion(handlers, customRoutes, base)}
|
|
1981
|
+
</div>
|
|
1982
|
+
|
|
1983
|
+
<h2>Query Parameters</h2>
|
|
1984
|
+
<div class="card">
|
|
1985
|
+
<table class="param-table">
|
|
1986
|
+
<thead><tr><th>Param</th><th>Example</th><th>Description</th></tr></thead>
|
|
1987
|
+
<tbody>
|
|
1988
|
+
<tr><td><code>?field=value</code></td><td><code>?name=Ana&role=admin</code></td><td>Filter by any field. Multiple params are ANDed.</td></tr>
|
|
1989
|
+
<tr><td><code>?field_gte / _lte</code></td><td><code>?price_gte=10&price_lte=50</code></td><td>Numeric or lexicographic range. Works with any comparable field.</td></tr>
|
|
1990
|
+
<tr><td><code>?field_ne</code></td><td><code>?status_ne=inactive</code></td><td>Exclude items where the field equals the value.</td></tr>
|
|
1991
|
+
<tr><td><code>?field_like</code></td><td><code>?name_like=ana</code></td><td>Case-insensitive substring match.</td></tr>
|
|
1992
|
+
<tr><td><code>?field_start</code></td><td><code>?name_start=A</code></td><td>Case-insensitive prefix match.</td></tr>
|
|
1993
|
+
<tr><td><code>?field_regex</code></td><td><code>?email_regex=gmail</code></td><td>Case-insensitive regular expression match.</td></tr>
|
|
1994
|
+
<tr><td><code>?_q</code></td><td><code>?_q=ana</code></td><td>Full-text search across all scalar fields (case-insensitive substring).</td></tr>
|
|
1995
|
+
<tr><td><code>?_sort & ?_order</code></td><td><code>?_sort=name&_order=desc</code></td><td>Sort by field. <code>_order</code>: <code>asc</code> (default) or <code>desc</code>.</td></tr>
|
|
1996
|
+
<tr><td><code>?_page & ?_limit</code></td><td><code>?_page=2&_limit=10</code></td><td>${paginationDesc}</td></tr>
|
|
1997
|
+
<tr><td><code>?_expand</code></td><td><code>?_expand=user</code></td><td>Embed a related parent object inline. Requires <code>_rel</code> in the YAML file.</td></tr>
|
|
1998
|
+
<tr><td><code>?_embed</code></td><td><code>?_embed=posts</code></td><td>Embed child collections into each parent item. Requires <code>_rel</code> in the YAML file.</td></tr>
|
|
1999
|
+
<tr><td><code>?_fields</code></td><td><code>?_fields=id,name</code></td><td>Return only the specified fields. Applied last \u2014 can include embedded/expanded keys.</td></tr>
|
|
2000
|
+
</tbody>
|
|
2001
|
+
</table>
|
|
2002
|
+
</div>
|
|
2003
|
+
</div>
|
|
1212
2004
|
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
</div>
|
|
1218
|
-
|
|
1219
|
-
<h2>Endpoints</h2>
|
|
1220
|
-
<div class="endpoints-grid">
|
|
1221
|
-
${accordions}
|
|
1222
|
-
${nestedRoutesAccordion(relations, base)}
|
|
1223
|
-
${options.snapshot ? snapshotAccordion() : ""}
|
|
1224
|
-
${customRoutesAccordion(customRoutes, base, handlers)}
|
|
1225
|
-
${handlersAccordion(handlers, customRoutes, base)}
|
|
1226
|
-
</div>
|
|
2005
|
+
<div class="col-right">
|
|
2006
|
+
${erDiagram}
|
|
2007
|
+
${collections.length ? `<h2>Examples</h2><div class="card">${examplesBlock(collections, relations, base, host, options, customRoutes[0])}</div>` : ""}
|
|
2008
|
+
</div>
|
|
1227
2009
|
|
|
1228
|
-
<h2>Query Parameters</h2>
|
|
1229
|
-
<div class="card">
|
|
1230
|
-
<table class="param-table">
|
|
1231
|
-
<thead><tr><th>Param</th><th>Example</th><th>Description</th></tr></thead>
|
|
1232
|
-
<tbody>
|
|
1233
|
-
<tr><td><code>?field=value</code></td><td><code>?name=Ana&role=admin</code></td><td>Filter by any field. Multiple params are ANDed.</td></tr>
|
|
1234
|
-
<tr><td><code>?field_gte / _lte</code></td><td><code>?price_gte=10&price_lte=50</code></td><td>Numeric or lexicographic range. Works with any comparable field.</td></tr>
|
|
1235
|
-
<tr><td><code>?field_ne</code></td><td><code>?status_ne=inactive</code></td><td>Exclude items where the field equals the value.</td></tr>
|
|
1236
|
-
<tr><td><code>?field_like</code></td><td><code>?name_like=ana</code></td><td>Case-insensitive substring match.</td></tr>
|
|
1237
|
-
<tr><td><code>?field_start</code></td><td><code>?name_start=A</code></td><td>Case-insensitive prefix match.</td></tr>
|
|
1238
|
-
<tr><td><code>?field_regex</code></td><td><code>?email_regex=gmail</code></td><td>Case-insensitive regular expression match.</td></tr>
|
|
1239
|
-
<tr><td><code>?_q</code></td><td><code>?_q=ana</code></td><td>Full-text search across all scalar fields (case-insensitive substring).</td></tr>
|
|
1240
|
-
<tr><td><code>?_sort & ?_order</code></td><td><code>?_sort=name&_order=desc</code></td><td>Sort by field. <code>_order</code>: <code>asc</code> (default) or <code>desc</code>.</td></tr>
|
|
1241
|
-
<tr><td><code>?_page & ?_limit</code></td><td><code>?_page=2&_limit=10</code></td><td>${paginationDesc}</td></tr>
|
|
1242
|
-
<tr><td><code>?_expand</code></td><td><code>?_expand=user</code></td><td>Embed a related parent object inline. Requires <code>_rel</code> in the YAML file.</td></tr>
|
|
1243
|
-
<tr><td><code>?_embed</code></td><td><code>?_embed=posts</code></td><td>Embed child collections into each parent item. Requires <code>_rel</code> in the YAML file.</td></tr>
|
|
1244
|
-
<tr><td><code>?_fields</code></td><td><code>?_fields=id,name</code></td><td>Return only the specified fields. Applied last \u2014 can include embedded/expanded keys.</td></tr>
|
|
1245
|
-
</tbody>
|
|
1246
|
-
</table>
|
|
1247
2010
|
</div>
|
|
1248
2011
|
|
|
1249
|
-
${collections.length ? `<h2>Examples</h2><div class="card">${examplesBlock(collections, relations, base, host, options, customRoutes[0])}</div>` : ""}
|
|
1250
|
-
|
|
1251
2012
|
<footer>
|
|
1252
2013
|
Powered by <a href="https://github.com/aggiovato/yRest" target="_blank">@yrest/cli</a> \xB7 <a href="/_about">/_about</a>
|
|
1253
2014
|
</footer>
|
|
1254
2015
|
|
|
1255
2016
|
</div>
|
|
1256
2017
|
</body>
|
|
1257
|
-
</html
|
|
2018
|
+
</html>`
|
|
2019
|
+
);
|
|
1258
2020
|
}
|
|
1259
2021
|
|
|
1260
2022
|
// src/router/routes/about.routes.ts
|
|
@@ -1842,12 +2604,23 @@ var NestedRouteCommand = class {
|
|
|
1842
2604
|
relations;
|
|
1843
2605
|
base;
|
|
1844
2606
|
register(server) {
|
|
2607
|
+
const registered = /* @__PURE__ */ new Set();
|
|
1845
2608
|
for (const [source, fields] of Object.entries(this.relations)) {
|
|
1846
2609
|
for (const [key, def] of Object.entries(fields)) {
|
|
1847
2610
|
if (def.type === "many2many") {
|
|
1848
|
-
this.
|
|
2611
|
+
const fwd = `${this.base}/${source}/:id/${key}`;
|
|
2612
|
+
const inv = `${this.base}/${def.target}/:id/${source}`;
|
|
2613
|
+
if (!registered.has(fwd) && !registered.has(inv)) {
|
|
2614
|
+
registered.add(fwd);
|
|
2615
|
+
registered.add(inv);
|
|
2616
|
+
this.registerMany2Many(server, source, key, def);
|
|
2617
|
+
}
|
|
1849
2618
|
} else {
|
|
1850
|
-
|
|
2619
|
+
const collPath = `${this.base}/${def.target}/:id/${source}`;
|
|
2620
|
+
if (!registered.has(collPath)) {
|
|
2621
|
+
registered.add(collPath);
|
|
2622
|
+
this.registerFkRelation(server, source, key, def.target, def.type);
|
|
2623
|
+
}
|
|
1851
2624
|
}
|
|
1852
2625
|
}
|
|
1853
2626
|
}
|