@atscript/core 0.1.33 → 0.1.34

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/dist/index.cjs CHANGED
@@ -99,6 +99,7 @@ else this.annotationsCounter.set(name, 1);
99
99
  }
100
100
  define(node) {
101
101
  this.definition = node;
102
+ node.ownerNode = this;
102
103
  return this;
103
104
  }
104
105
  get def() {
@@ -152,6 +153,7 @@ else this.annotationsCounter.set(name, 1);
152
153
  _define_property$15(this, "annotations", void 0);
153
154
  _define_property$15(this, "annotationsCounter", void 0);
154
155
  _define_property$15(this, "__typeId", void 0);
156
+ _define_property$15(this, "ownerNode", void 0);
155
157
  _define_property$15(this, "_documentation", void 0);
156
158
  this.entity = entity;
157
159
  this.isGroup = false;
@@ -695,10 +697,12 @@ var SemanticStructureNode = class extends SemanticGroup {
695
697
  * Shortcut to set props, used as utility
696
698
  */ setProps(props$1) {
697
699
  this.nodes = props$1;
698
- for (const prop of props$1) this.props.set(prop.id, prop);
700
+ for (const prop of props$1) {
701
+ prop.ownerNode = this;
702
+ this.props.set(prop.id, prop);
703
+ }
699
704
  }
700
705
  registerAtDocument(doc) {
701
- super.registerAtDocument(doc);
702
706
  const block$2 = this.token("identifier");
703
707
  block$2.blockType = "structure";
704
708
  doc.blocksIndex.add(block$2);
@@ -721,8 +725,10 @@ var SemanticStructureNode = class extends SemanticGroup {
721
725
  doc.registerMessage(token, "Non-prop node");
722
726
  continue;
723
727
  }
728
+ node.ownerNode = this;
724
729
  this.props.set(name, node);
725
730
  }
731
+ super.registerAtDocument(doc);
726
732
  }
727
733
  addVirtualProp(opts) {
728
734
  const token = opts.refToken || this.token("identifier");
@@ -750,6 +756,7 @@ var SemanticStructureNode = class extends SemanticGroup {
750
756
  }
751
757
  prop.define(ref$1);
752
758
  } else prop.define(opts.type);
759
+ prop.ownerNode = this;
753
760
  this.nodes.push(prop);
754
761
  this.props.set(opts.name, prop);
755
762
  }
@@ -3207,8 +3214,149 @@ function createSyntheticRef(name) {
3207
3214
  return ref$1;
3208
3215
  }
3209
3216
 
3217
+ //#endregion
3218
+ //#region packages/core/src/defaults/db-utils.ts
3219
+ function findFKFieldsPointingTo(doc, iface, targetTypeName, alias) {
3220
+ const results = [];
3221
+ let struct;
3222
+ if (isInterface(iface) && iface.hasExtends) {
3223
+ const resolved = doc.resolveInterfaceExtends(iface);
3224
+ if (resolved && isStructure(resolved)) struct = resolved;
3225
+ }
3226
+ if (!struct) struct = isStructure(iface) ? iface : isInterface(iface) && isStructure(iface.getDefinition()) ? iface.getDefinition() : undefined;
3227
+ if (!struct) return results;
3228
+ for (const [name, prop] of struct.props) {
3229
+ if (prop.countAnnotations("db.rel.FK") === 0) continue;
3230
+ const def = prop.getDefinition();
3231
+ if (!def || !isRef(def)) continue;
3232
+ const ref$1 = def;
3233
+ if (!ref$1.hasChain) continue;
3234
+ const refTypeName = ref$1.id;
3235
+ const refField = ref$1.chain.map((c) => c.text).join(".");
3236
+ if (refTypeName !== targetTypeName) continue;
3237
+ if (alias !== undefined) {
3238
+ const fkAnnotations = prop.annotations?.filter((a) => a.name === "db.rel.FK");
3239
+ const hasMatchingAlias = fkAnnotations?.some((a) => a.args.length > 0 && a.args[0].text === alias);
3240
+ if (!hasMatchingAlias) continue;
3241
+ }
3242
+ results.push({
3243
+ name,
3244
+ prop,
3245
+ chainRef: {
3246
+ type: refTypeName,
3247
+ field: refField
3248
+ }
3249
+ });
3250
+ }
3251
+ return results;
3252
+ }
3253
+
3210
3254
  //#endregion
3211
3255
  //#region packages/core/src/defaults/db-annotations.ts
3256
+ /**
3257
+ * Traverse from annotation token → prop → structure → interface
3258
+ * to check if the parent interface has @db.table.
3259
+ */ function getDbTableOwner(token) {
3260
+ const field = token.parentNode;
3261
+ const struct = field.ownerNode;
3262
+ if (!struct || !isStructure(struct)) return undefined;
3263
+ const iface = struct.ownerNode;
3264
+ return iface && isInterface(iface) ? iface : struct;
3265
+ }
3266
+ /**
3267
+ * Get the parent structure node from an annotation token.
3268
+ */ function getParentStruct(token) {
3269
+ const field = token.parentNode;
3270
+ const struct = field.ownerNode;
3271
+ return struct && isStructure(struct) ? struct : undefined;
3272
+ }
3273
+ /**
3274
+ * Get the parent interface name (for error messages and cross-type resolution).
3275
+ */ function getParentTypeName(token) {
3276
+ const struct = getParentStruct(token);
3277
+ if (!struct) return undefined;
3278
+ const iface = struct.ownerNode;
3279
+ return iface && isInterface(iface) ? iface.id : struct.id;
3280
+ }
3281
+ /**
3282
+ * Extract target type name from a navigational field definition.
3283
+ * Unwraps arrays (e.g., `Post[]` → `Post`).
3284
+ */ function getNavTargetTypeName(field) {
3285
+ let def = field.getDefinition();
3286
+ if (isArray(def)) def = def?.getDefinition();
3287
+ if (isRef(def)) return def.id;
3288
+ return undefined;
3289
+ }
3290
+ /**
3291
+ * Get the alias argument from an annotation on a field.
3292
+ */ function getAnnotationAlias(prop, annotationName) {
3293
+ const annotations$1 = prop.annotations?.filter((a) => a.name === annotationName);
3294
+ if (!annotations$1 || annotations$1.length === 0) return undefined;
3295
+ return annotations$1[0].args.length > 0 ? annotations$1[0].args[0].text : undefined;
3296
+ }
3297
+ /**
3298
+ * Factory for @db.rel.onDelete / @db.rel.onUpdate — identical validation logic,
3299
+ * only the annotation name and description verb differ.
3300
+ */ function refActionAnnotation(name) {
3301
+ return new AnnotationSpec({
3302
+ description: `Referential action when the target ${name === "onDelete" ? "row is deleted" : "key is updated"}. Only valid on @db.rel.FK fields.\n\n` + "**Example:**\n" + "```atscript\n" + "@db.rel.FK\n" + `@db.rel.${name} "cascade"\n` + "authorId: User.id\n" + "```\n",
3303
+ nodeType: ["prop"],
3304
+ argument: {
3305
+ name: "action",
3306
+ type: "string",
3307
+ values: [
3308
+ "cascade",
3309
+ "restrict",
3310
+ "noAction",
3311
+ "setNull",
3312
+ "setDefault"
3313
+ ],
3314
+ description: "Referential action: \"cascade\", \"restrict\", \"noAction\", \"setNull\", or \"setDefault\"."
3315
+ },
3316
+ validate(token, args, doc) {
3317
+ const errors = [];
3318
+ const field = token.parentNode;
3319
+ if (field.countAnnotations("db.rel.FK") === 0) errors.push({
3320
+ message: `@db.rel.${name} is only valid on @db.rel.FK fields`,
3321
+ severity: 1,
3322
+ range: token.range
3323
+ });
3324
+ if (args[0]) {
3325
+ const action = args[0].text;
3326
+ if (action === "setNull" && !field.has("optional")) errors.push({
3327
+ message: `@db.rel.${name} "setNull" requires the FK field to be optional (?)`,
3328
+ severity: 1,
3329
+ range: token.range
3330
+ });
3331
+ if (action === "setDefault" && field.countAnnotations("db.default.value") === 0 && field.countAnnotations("db.default.fn") === 0) errors.push({
3332
+ message: `@db.rel.${name} "setDefault" but no @db.default.* annotation — field will have no fallback value`,
3333
+ severity: 2,
3334
+ range: token.range
3335
+ });
3336
+ }
3337
+ const fkAlias = getAnnotationAlias(field, "db.rel.FK");
3338
+ if (fkAlias) {
3339
+ const struct = getParentStruct(token);
3340
+ if (struct) {
3341
+ const annotationName = `db.rel.${name}`;
3342
+ let count = 0;
3343
+ for (const [, prop] of struct.props) {
3344
+ if (prop.countAnnotations("db.rel.FK") === 0) continue;
3345
+ if (prop.countAnnotations(annotationName) === 0) continue;
3346
+ const propFkAlias = getAnnotationAlias(prop, "db.rel.FK");
3347
+ if (propFkAlias === fkAlias) count++;
3348
+ }
3349
+ if (count > 1) errors.push({
3350
+ message: `Composite FK '${fkAlias}' has @db.rel.${name} on multiple fields — declare it on exactly one`,
3351
+ severity: 1,
3352
+ range: token.range
3353
+ });
3354
+ }
3355
+ }
3356
+ return errors;
3357
+ }
3358
+ });
3359
+ }
3212
3360
  const dbAnnotations = {
3213
3361
  patch: { strategy: new AnnotationSpec({
3214
3362
  description: "Defines the **patching strategy** for updating nested objects.\n\n- **\"replace\"** → The field or object will be **fully replaced**.\n- **\"merge\"** → The field or object will be **merged recursively** (applies only to objects, not arrays).\n\n**Example:**\n```atscript\n@db.patch.strategy \"merge\"\nsettings: {\n notifications: boolean\n preferences: {\n theme: string\n }\n}\n```\n",
@@ -3411,7 +3559,277 @@ else if (fnName === "now" && !["number", "string"].includes(baseType)) errors.pu
3411
3559
  });
3412
3560
  return errors;
3413
3561
  }
3414
- })
3562
+ }),
3563
+ rel: {
3564
+ FK: new AnnotationSpec({
3565
+ description: "Declares a foreign key constraint on this field. The field must use a chain reference type (e.g., `User.id`) to specify the FK target.\n\n**Example:**\n```atscript\n@db.rel.FK\nauthorId: User.id\n\n// With alias (required when multiple FKs point to the same type)\n@db.rel.FK \"author\"\nauthorId: User.id\n```\n",
3566
+ nodeType: ["prop"],
3567
+ argument: {
3568
+ optional: true,
3569
+ name: "alias",
3570
+ type: "string",
3571
+ description: "Alias for pairing with @db.rel.to. Required when multiple FKs point to the same target type."
3572
+ },
3573
+ validate(token, args, doc) {
3574
+ const errors = [];
3575
+ const field = token.parentNode;
3576
+ const alias = args[0]?.text;
3577
+ const owner = getDbTableOwner(token);
3578
+ if (!owner || owner.countAnnotations("db.table") === 0) errors.push({
3579
+ message: "@db.rel.FK is only valid on fields of a @db.table interface",
3580
+ severity: 1,
3581
+ range: token.range
3582
+ });
3583
+ if (field.countAnnotations("db.rel.to") > 0 || field.countAnnotations("db.rel.from") > 0) errors.push({
3584
+ message: "A field cannot be both a foreign key and a navigational property",
3585
+ severity: 1,
3586
+ range: token.range
3587
+ });
3588
+ const definition$1 = field.getDefinition();
3589
+ if (!definition$1 || !isRef(definition$1) || !definition$1.hasChain) {
3590
+ errors.push({
3591
+ message: `@db.rel.FK requires a chain reference type (e.g. User.id), got scalar type`,
3592
+ severity: 1,
3593
+ range: token.range
3594
+ });
3595
+ return errors;
3596
+ }
3597
+ const ref$1 = definition$1;
3598
+ const refTypeName = ref$1.id;
3599
+ const chainFields = ref$1.chain.map((c) => c.text);
3600
+ const targetUnwound = doc.unwindType(refTypeName);
3601
+ if (targetUnwound) {
3602
+ const targetDef = targetUnwound.def;
3603
+ if (isInterface(targetDef) || isStructure(targetDef)) {
3604
+ const struct = isInterface(targetDef) ? targetDef.getDefinition() : targetDef;
3605
+ if (struct && isStructure(struct) && chainFields.length > 0) {
3606
+ const targetProp = struct.props.get(chainFields[0]);
3607
+ if (targetProp) {
3608
+ if (targetProp.countAnnotations("meta.id") === 0 && targetProp.countAnnotations("db.index.unique") === 0) errors.push({
3609
+ message: `@db.rel.FK target '${refTypeName}.${chainFields.join(".")}' is not a primary key (@meta.id) or unique (@db.index.unique) field`,
3610
+ severity: 1,
3611
+ range: token.range
3612
+ });
3613
+ const propDef = targetProp.getDefinition();
3614
+ if (propDef && isRef(propDef)) {
3615
+ const propUnwound = targetUnwound.doc.unwindType(propDef.id, propDef.chain);
3616
+ if (propUnwound && !isPrimitive(propUnwound.def)) errors.push({
3617
+ message: `Foreign key field must resolve to a scalar type (number, string, etc.), got '${propDef.id}'`,
3618
+ severity: 1,
3619
+ range: token.range
3620
+ });
3621
+ } else if (propDef && !isPrimitive(propDef)) errors.push({
3622
+ message: `Foreign key field must resolve to a scalar type (number, string, etc.)`,
3623
+ severity: 1,
3624
+ range: token.range
3625
+ });
3626
+ }
3627
+ }
3628
+ }
3629
+ }
3630
+ if (!alias) {
3631
+ const struct = getParentStruct(token);
3632
+ if (struct) {
3633
+ let sameTargetCount = 0;
3634
+ for (const [, prop] of struct.props) {
3635
+ if (prop.countAnnotations("db.rel.FK") === 0) continue;
3636
+ const def = prop.getDefinition();
3637
+ if (!def || !isRef(def)) continue;
3638
+ const r = def;
3639
+ if (!r.hasChain) continue;
3640
+ if (r.id === refTypeName) {
3641
+ const fkAnnotations = prop.annotations?.filter((a) => a.name === "db.rel.FK");
3642
+ const hasAlias = fkAnnotations?.some((a) => a.args.length > 0);
3643
+ if (!hasAlias) sameTargetCount++;
3644
+ }
3645
+ }
3646
+ if (sameTargetCount > 1) errors.push({
3647
+ message: `Multiple @db.rel.FK fields resolve to type '${refTypeName}' — add alias to disambiguate`,
3648
+ severity: 1,
3649
+ range: token.range
3650
+ });
3651
+ }
3652
+ }
3653
+ return errors;
3654
+ }
3655
+ }),
3656
+ to: new AnnotationSpec({
3657
+ description: "Forward navigational property — the FK is on **this** interface. The compiler resolves the matching @db.rel.FK by target type or alias.\n\n**Example:**\n```atscript\n@db.rel.to\nauthor?: User\n\n// With alias\n@db.rel.to \"author\"\nauthor?: User\n```\n",
3658
+ nodeType: ["prop"],
3659
+ argument: {
3660
+ optional: true,
3661
+ name: "alias",
3662
+ type: "string",
3663
+ description: "Match a local @db.rel.FK by alias."
3664
+ },
3665
+ validate(token, args, doc) {
3666
+ const errors = [];
3667
+ const field = token.parentNode;
3668
+ const alias = args[0]?.text;
3669
+ const owner = getDbTableOwner(token);
3670
+ if (!owner || owner.countAnnotations("db.table") === 0) errors.push({
3671
+ message: "@db.rel.to is only valid on fields of a @db.table interface",
3672
+ severity: 1,
3673
+ range: token.range
3674
+ });
3675
+ if (field.countAnnotations("db.rel.FK") > 0) errors.push({
3676
+ message: "A field cannot be both a foreign key and a navigational property",
3677
+ severity: 1,
3678
+ range: token.range
3679
+ });
3680
+ const targetTypeName = getNavTargetTypeName(field);
3681
+ if (!targetTypeName) return errors;
3682
+ const unwound = doc.unwindType(targetTypeName);
3683
+ if (unwound) {
3684
+ const targetDef = unwound.def;
3685
+ const targetNode = isInterface(targetDef) ? targetDef : undefined;
3686
+ if (!targetNode || targetNode.countAnnotations("db.table") === 0) errors.push({
3687
+ message: `@db.rel.to target '${targetTypeName}' is not a @db.table entity`,
3688
+ severity: 1,
3689
+ range: token.range
3690
+ });
3691
+ }
3692
+ const fieldDef = field.getDefinition();
3693
+ if (fieldDef && fieldDef.entity === "group" && fieldDef.op === "|") errors.push({
3694
+ message: "@db.rel.to does not support union types — use separate relations",
3695
+ severity: 1,
3696
+ range: token.range
3697
+ });
3698
+ const struct = getParentStruct(token);
3699
+ if (struct) {
3700
+ const fieldName = field.id;
3701
+ for (const [name, prop] of struct.props) {
3702
+ if (name === fieldName) continue;
3703
+ if (prop.countAnnotations("db.rel.to") === 0) continue;
3704
+ const propAlias = getAnnotationAlias(prop, "db.rel.to");
3705
+ if ((alias || undefined) === (propAlias || undefined)) {
3706
+ const otherTarget = getNavTargetTypeName(prop);
3707
+ if (otherTarget === targetTypeName) {
3708
+ errors.push({
3709
+ message: `Duplicate @db.rel.to '${alias || targetTypeName}' — only one forward navigational property per alias`,
3710
+ severity: 1,
3711
+ range: token.range
3712
+ });
3713
+ break;
3714
+ }
3715
+ }
3716
+ }
3717
+ if (alias) {
3718
+ const matches = findFKFieldsPointingTo(doc, struct, targetTypeName, alias);
3719
+ if (matches.length === 0) errors.push({
3720
+ message: `No @db.rel.FK '${alias}' found on this interface`,
3721
+ severity: 1,
3722
+ range: token.range
3723
+ });
3724
+ } else {
3725
+ const matches = findFKFieldsPointingTo(doc, struct, targetTypeName);
3726
+ if (matches.length === 0) errors.push({
3727
+ message: `No @db.rel.FK on this interface points to '${targetTypeName}' — did you mean @db.rel.from?`,
3728
+ severity: 1,
3729
+ range: token.range
3730
+ });
3731
+ else if (matches.length > 1) errors.push({
3732
+ message: `Multiple @db.rel.FK fields point to '${targetTypeName}' — add alias to disambiguate`,
3733
+ severity: 1,
3734
+ range: token.range
3735
+ });
3736
+ }
3737
+ }
3738
+ return errors;
3739
+ }
3740
+ }),
3741
+ from: new AnnotationSpec({
3742
+ description: "Inverse navigational property — the FK is on the **target** interface, pointing back to this one.\n\n**Example:**\n```atscript\n@db.rel.from\nposts: Post[]\n\n// With alias\n@db.rel.from \"original\"\ncomments: Comment[]\n```\n",
3743
+ nodeType: ["prop"],
3744
+ argument: {
3745
+ optional: true,
3746
+ name: "alias",
3747
+ type: "string",
3748
+ description: "Match a @db.rel.FK on the target interface by alias."
3749
+ },
3750
+ validate(token, args, doc) {
3751
+ const errors = [];
3752
+ const field = token.parentNode;
3753
+ const alias = args[0]?.text;
3754
+ const owner = getDbTableOwner(token);
3755
+ if (!owner || owner.countAnnotations("db.table") === 0) errors.push({
3756
+ message: "@db.rel.from is only valid on fields of a @db.table interface",
3757
+ severity: 1,
3758
+ range: token.range
3759
+ });
3760
+ if (field.countAnnotations("db.rel.FK") > 0) errors.push({
3761
+ message: "A field cannot be both a foreign key and a navigational property",
3762
+ severity: 1,
3763
+ range: token.range
3764
+ });
3765
+ const targetTypeName = getNavTargetTypeName(field);
3766
+ if (!targetTypeName) return errors;
3767
+ const unwound = doc.unwindType(targetTypeName);
3768
+ if (!unwound) return errors;
3769
+ const targetDef = unwound.def;
3770
+ const targetDoc = unwound.doc;
3771
+ if (!isInterface(targetDef) || targetDef.countAnnotations("db.table") === 0) {
3772
+ errors.push({
3773
+ message: `@db.rel.from target '${targetTypeName}' is not a @db.table entity`,
3774
+ severity: 1,
3775
+ range: token.range
3776
+ });
3777
+ return errors;
3778
+ }
3779
+ const struct = getParentStruct(token);
3780
+ if (struct) {
3781
+ const fieldName = field.id;
3782
+ for (const [name, prop] of struct.props) {
3783
+ if (name === fieldName) continue;
3784
+ if (prop.countAnnotations("db.rel.from") === 0) continue;
3785
+ const propAlias = getAnnotationAlias(prop, "db.rel.from");
3786
+ if ((alias || undefined) === (propAlias || undefined)) {
3787
+ const otherTarget = getNavTargetTypeName(prop);
3788
+ if (otherTarget === targetTypeName) {
3789
+ errors.push({
3790
+ message: `Duplicate @db.rel.from '${alias || targetTypeName}' — only one inverse navigational property per alias`,
3791
+ severity: 1,
3792
+ range: token.range
3793
+ });
3794
+ break;
3795
+ }
3796
+ }
3797
+ }
3798
+ }
3799
+ const thisTypeName = getParentTypeName(token);
3800
+ if (!thisTypeName) return errors;
3801
+ const matches = findFKFieldsPointingTo(targetDoc, targetDef, thisTypeName, alias);
3802
+ if (alias) {
3803
+ if (matches.length === 0) errors.push({
3804
+ message: `No @db.rel.FK '${alias}' found on '${targetTypeName}'`,
3805
+ severity: 1,
3806
+ range: token.range
3807
+ });
3808
+ } else if (matches.length === 0) errors.push({
3809
+ message: `No @db.rel.FK on '${targetTypeName}' points to '${thisTypeName}'`,
3810
+ severity: 1,
3811
+ range: token.range
3812
+ });
3813
+ else if (matches.length > 1) errors.push({
3814
+ message: `'${targetTypeName}' has multiple @db.rel.FK fields pointing to '${thisTypeName}' — add alias`,
3815
+ severity: 1,
3816
+ range: token.range
3817
+ });
3818
+ const fieldDef = field.getDefinition();
3819
+ if (!isArray(fieldDef) && matches.length === 1) {
3820
+ const fkProp = matches[0].prop;
3821
+ if (fkProp.countAnnotations("db.index.unique") === 0) errors.push({
3822
+ message: `@db.rel.from '${field.id}' has singular type '${targetTypeName}' (1:1) but the FK on '${targetTypeName}' is not @db.index.unique — did you mean '${targetTypeName}[]' (1:N)?`,
3823
+ severity: 2,
3824
+ range: token.range
3825
+ });
3826
+ }
3827
+ return errors;
3828
+ }
3829
+ }),
3830
+ onDelete: refActionAnnotation("onDelete"),
3831
+ onUpdate: refActionAnnotation("onUpdate")
3832
+ }
3415
3833
  };
3416
3834
 
3417
3835
  //#endregion
@@ -4717,6 +5135,7 @@ exports.Token = Token
4717
5135
  exports.build = build
4718
5136
  exports.createAtscriptPlugin = createAtscriptPlugin
4719
5137
  exports.defineConfig = defineConfig
5138
+ exports.findFKFieldsPointingTo = findFKFieldsPointingTo
4720
5139
  exports.flattenInterfaceNode = flattenInterfaceNode
4721
5140
  exports.getRelPath = getRelPath
4722
5141
  exports.isAnnotate = isAnnotate
package/dist/index.d.ts CHANGED
@@ -292,6 +292,7 @@ declare class SemanticNode {
292
292
  annotations?: TAnnotationTokens[];
293
293
  annotationsCounter?: Map<string, number>;
294
294
  __typeId?: number;
295
+ ownerNode?: SemanticNode;
295
296
  registerAtDocument(doc: AtscriptDoc): void;
296
297
  protected _documentation?: string;
297
298
  setDocumentation(s: string): void;
@@ -827,5 +828,19 @@ interface TFlatFieldDescriptor {
827
828
  declare function flattenInterfaceNode(doc: AtscriptDoc, node: SemanticInterfaceNode): Map<string, TFlatFieldDescriptor>;
828
829
  declare function isPhantomNode(doc: AtscriptDoc, def?: SemanticNode): boolean;
829
830
 
830
- export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BuildRepo, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticPrimitiveNode, SemanticPropNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, build, createAtscriptPlugin, defineConfig, flattenInterfaceNode, getRelPath, isAnnotate, isAnnotationSpec, isArray, isConst, isGroup, isImport, isInterface, isPhantomNode, isPrimitive, isProp, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, resolveAnnotation, resolveAtscriptFromPath, resolveConfigFile };
831
- export type { TAnnotationArgument, TAnnotationSpecConfig, TAnnotationTokens, TAnnotationsTree, TAtscriptConfig, TAtscriptConfigInput, TAtscriptConfigOutput, TAtscriptDocConfig, TAtscriptPlugin, TAtscriptRenderFormat, TFlatFieldDescriptor, TMessages, TNodeEntity, TOutput, TOutputWithSource, TPluginManagers, TPluginOutput, TPrimitiveAnnotationArg, TPrimitiveAnnotationArgs, TPrimitiveAnnotationValue, TPrimitiveBaseConfig, TPrimitiveConfig, TPrimitiveTypeArray, TPrimitiveTypeComplex, TPrimitiveTypeDef, TPrimitiveTypeFinal, TPrimitiveTypeFinalOptional, TPrimitiveTypeObject, TSemanticToken };
831
+ interface TFKFieldMatch {
832
+ name: string;
833
+ prop: SemanticPropNode;
834
+ chainRef: {
835
+ type: string;
836
+ field: string;
837
+ };
838
+ }
839
+ /**
840
+ * Find all `@db.rel.FK` fields on a type that reference `targetTypeName`.
841
+ * Resolves `extends` to include inherited fields.
842
+ */
843
+ declare function findFKFieldsPointingTo(doc: AtscriptDoc, iface: SemanticInterfaceNode | SemanticStructureNode, targetTypeName: string, alias?: string): TFKFieldMatch[];
844
+
845
+ export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BuildRepo, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticPrimitiveNode, SemanticPropNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, build, createAtscriptPlugin, defineConfig, findFKFieldsPointingTo, flattenInterfaceNode, getRelPath, isAnnotate, isAnnotationSpec, isArray, isConst, isGroup, isImport, isInterface, isPhantomNode, isPrimitive, isProp, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, resolveAnnotation, resolveAtscriptFromPath, resolveConfigFile };
846
+ export type { TAnnotationArgument, TAnnotationSpecConfig, TAnnotationTokens, TAnnotationsTree, TAtscriptConfig, TAtscriptConfigInput, TAtscriptConfigOutput, TAtscriptDocConfig, TAtscriptPlugin, TAtscriptRenderFormat, TFKFieldMatch, TFlatFieldDescriptor, TMessages, TNodeEntity, TOutput, TOutputWithSource, TPluginManagers, TPluginOutput, TPrimitiveAnnotationArg, TPrimitiveAnnotationArgs, TPrimitiveAnnotationValue, TPrimitiveBaseConfig, TPrimitiveConfig, TPrimitiveTypeArray, TPrimitiveTypeComplex, TPrimitiveTypeDef, TPrimitiveTypeFinal, TPrimitiveTypeFinalOptional, TPrimitiveTypeObject, TSemanticToken };
package/dist/index.mjs CHANGED
@@ -75,6 +75,7 @@ else this.annotationsCounter.set(name, 1);
75
75
  }
76
76
  define(node) {
77
77
  this.definition = node;
78
+ node.ownerNode = this;
78
79
  return this;
79
80
  }
80
81
  get def() {
@@ -128,6 +129,7 @@ else this.annotationsCounter.set(name, 1);
128
129
  _define_property$15(this, "annotations", void 0);
129
130
  _define_property$15(this, "annotationsCounter", void 0);
130
131
  _define_property$15(this, "__typeId", void 0);
132
+ _define_property$15(this, "ownerNode", void 0);
131
133
  _define_property$15(this, "_documentation", void 0);
132
134
  this.entity = entity;
133
135
  this.isGroup = false;
@@ -671,10 +673,12 @@ var SemanticStructureNode = class extends SemanticGroup {
671
673
  * Shortcut to set props, used as utility
672
674
  */ setProps(props$1) {
673
675
  this.nodes = props$1;
674
- for (const prop of props$1) this.props.set(prop.id, prop);
676
+ for (const prop of props$1) {
677
+ prop.ownerNode = this;
678
+ this.props.set(prop.id, prop);
679
+ }
675
680
  }
676
681
  registerAtDocument(doc) {
677
- super.registerAtDocument(doc);
678
682
  const block$2 = this.token("identifier");
679
683
  block$2.blockType = "structure";
680
684
  doc.blocksIndex.add(block$2);
@@ -697,8 +701,10 @@ var SemanticStructureNode = class extends SemanticGroup {
697
701
  doc.registerMessage(token, "Non-prop node");
698
702
  continue;
699
703
  }
704
+ node.ownerNode = this;
700
705
  this.props.set(name, node);
701
706
  }
707
+ super.registerAtDocument(doc);
702
708
  }
703
709
  addVirtualProp(opts) {
704
710
  const token = opts.refToken || this.token("identifier");
@@ -726,6 +732,7 @@ var SemanticStructureNode = class extends SemanticGroup {
726
732
  }
727
733
  prop.define(ref$1);
728
734
  } else prop.define(opts.type);
735
+ prop.ownerNode = this;
729
736
  this.nodes.push(prop);
730
737
  this.props.set(opts.name, prop);
731
738
  }
@@ -3183,8 +3190,149 @@ function createSyntheticRef(name) {
3183
3190
  return ref$1;
3184
3191
  }
3185
3192
 
3193
+ //#endregion
3194
+ //#region packages/core/src/defaults/db-utils.ts
3195
+ function findFKFieldsPointingTo(doc, iface, targetTypeName, alias) {
3196
+ const results = [];
3197
+ let struct;
3198
+ if (isInterface(iface) && iface.hasExtends) {
3199
+ const resolved = doc.resolveInterfaceExtends(iface);
3200
+ if (resolved && isStructure(resolved)) struct = resolved;
3201
+ }
3202
+ if (!struct) struct = isStructure(iface) ? iface : isInterface(iface) && isStructure(iface.getDefinition()) ? iface.getDefinition() : undefined;
3203
+ if (!struct) return results;
3204
+ for (const [name, prop] of struct.props) {
3205
+ if (prop.countAnnotations("db.rel.FK") === 0) continue;
3206
+ const def = prop.getDefinition();
3207
+ if (!def || !isRef(def)) continue;
3208
+ const ref$1 = def;
3209
+ if (!ref$1.hasChain) continue;
3210
+ const refTypeName = ref$1.id;
3211
+ const refField = ref$1.chain.map((c) => c.text).join(".");
3212
+ if (refTypeName !== targetTypeName) continue;
3213
+ if (alias !== undefined) {
3214
+ const fkAnnotations = prop.annotations?.filter((a) => a.name === "db.rel.FK");
3215
+ const hasMatchingAlias = fkAnnotations?.some((a) => a.args.length > 0 && a.args[0].text === alias);
3216
+ if (!hasMatchingAlias) continue;
3217
+ }
3218
+ results.push({
3219
+ name,
3220
+ prop,
3221
+ chainRef: {
3222
+ type: refTypeName,
3223
+ field: refField
3224
+ }
3225
+ });
3226
+ }
3227
+ return results;
3228
+ }
3229
+
3186
3230
  //#endregion
3187
3231
  //#region packages/core/src/defaults/db-annotations.ts
3232
+ /**
3233
+ * Traverse from annotation token → prop → structure → interface
3234
+ * to check if the parent interface has @db.table.
3235
+ */ function getDbTableOwner(token) {
3236
+ const field = token.parentNode;
3237
+ const struct = field.ownerNode;
3238
+ if (!struct || !isStructure(struct)) return undefined;
3239
+ const iface = struct.ownerNode;
3240
+ return iface && isInterface(iface) ? iface : struct;
3241
+ }
3242
+ /**
3243
+ * Get the parent structure node from an annotation token.
3244
+ */ function getParentStruct(token) {
3245
+ const field = token.parentNode;
3246
+ const struct = field.ownerNode;
3247
+ return struct && isStructure(struct) ? struct : undefined;
3248
+ }
3249
+ /**
3250
+ * Get the parent interface name (for error messages and cross-type resolution).
3251
+ */ function getParentTypeName(token) {
3252
+ const struct = getParentStruct(token);
3253
+ if (!struct) return undefined;
3254
+ const iface = struct.ownerNode;
3255
+ return iface && isInterface(iface) ? iface.id : struct.id;
3256
+ }
3257
+ /**
3258
+ * Extract target type name from a navigational field definition.
3259
+ * Unwraps arrays (e.g., `Post[]` → `Post`).
3260
+ */ function getNavTargetTypeName(field) {
3261
+ let def = field.getDefinition();
3262
+ if (isArray(def)) def = def?.getDefinition();
3263
+ if (isRef(def)) return def.id;
3264
+ return undefined;
3265
+ }
3266
+ /**
3267
+ * Get the alias argument from an annotation on a field.
3268
+ */ function getAnnotationAlias(prop, annotationName) {
3269
+ const annotations$1 = prop.annotations?.filter((a) => a.name === annotationName);
3270
+ if (!annotations$1 || annotations$1.length === 0) return undefined;
3271
+ return annotations$1[0].args.length > 0 ? annotations$1[0].args[0].text : undefined;
3272
+ }
3273
+ /**
3274
+ * Factory for @db.rel.onDelete / @db.rel.onUpdate — identical validation logic,
3275
+ * only the annotation name and description verb differ.
3276
+ */ function refActionAnnotation(name) {
3277
+ return new AnnotationSpec({
3278
+ description: `Referential action when the target ${name === "onDelete" ? "row is deleted" : "key is updated"}. Only valid on @db.rel.FK fields.\n\n` + "**Example:**\n" + "```atscript\n" + "@db.rel.FK\n" + `@db.rel.${name} "cascade"\n` + "authorId: User.id\n" + "```\n",
3279
+ nodeType: ["prop"],
3280
+ argument: {
3281
+ name: "action",
3282
+ type: "string",
3283
+ values: [
3284
+ "cascade",
3285
+ "restrict",
3286
+ "noAction",
3287
+ "setNull",
3288
+ "setDefault"
3289
+ ],
3290
+ description: "Referential action: \"cascade\", \"restrict\", \"noAction\", \"setNull\", or \"setDefault\"."
3291
+ },
3292
+ validate(token, args, doc) {
3293
+ const errors = [];
3294
+ const field = token.parentNode;
3295
+ if (field.countAnnotations("db.rel.FK") === 0) errors.push({
3296
+ message: `@db.rel.${name} is only valid on @db.rel.FK fields`,
3297
+ severity: 1,
3298
+ range: token.range
3299
+ });
3300
+ if (args[0]) {
3301
+ const action = args[0].text;
3302
+ if (action === "setNull" && !field.has("optional")) errors.push({
3303
+ message: `@db.rel.${name} "setNull" requires the FK field to be optional (?)`,
3304
+ severity: 1,
3305
+ range: token.range
3306
+ });
3307
+ if (action === "setDefault" && field.countAnnotations("db.default.value") === 0 && field.countAnnotations("db.default.fn") === 0) errors.push({
3308
+ message: `@db.rel.${name} "setDefault" but no @db.default.* annotation — field will have no fallback value`,
3309
+ severity: 2,
3310
+ range: token.range
3311
+ });
3312
+ }
3313
+ const fkAlias = getAnnotationAlias(field, "db.rel.FK");
3314
+ if (fkAlias) {
3315
+ const struct = getParentStruct(token);
3316
+ if (struct) {
3317
+ const annotationName = `db.rel.${name}`;
3318
+ let count = 0;
3319
+ for (const [, prop] of struct.props) {
3320
+ if (prop.countAnnotations("db.rel.FK") === 0) continue;
3321
+ if (prop.countAnnotations(annotationName) === 0) continue;
3322
+ const propFkAlias = getAnnotationAlias(prop, "db.rel.FK");
3323
+ if (propFkAlias === fkAlias) count++;
3324
+ }
3325
+ if (count > 1) errors.push({
3326
+ message: `Composite FK '${fkAlias}' has @db.rel.${name} on multiple fields — declare it on exactly one`,
3327
+ severity: 1,
3328
+ range: token.range
3329
+ });
3330
+ }
3331
+ }
3332
+ return errors;
3333
+ }
3334
+ });
3335
+ }
3188
3336
  const dbAnnotations = {
3189
3337
  patch: { strategy: new AnnotationSpec({
3190
3338
  description: "Defines the **patching strategy** for updating nested objects.\n\n- **\"replace\"** → The field or object will be **fully replaced**.\n- **\"merge\"** → The field or object will be **merged recursively** (applies only to objects, not arrays).\n\n**Example:**\n```atscript\n@db.patch.strategy \"merge\"\nsettings: {\n notifications: boolean\n preferences: {\n theme: string\n }\n}\n```\n",
@@ -3387,7 +3535,277 @@ else if (fnName === "now" && !["number", "string"].includes(baseType)) errors.pu
3387
3535
  });
3388
3536
  return errors;
3389
3537
  }
3390
- })
3538
+ }),
3539
+ rel: {
3540
+ FK: new AnnotationSpec({
3541
+ description: "Declares a foreign key constraint on this field. The field must use a chain reference type (e.g., `User.id`) to specify the FK target.\n\n**Example:**\n```atscript\n@db.rel.FK\nauthorId: User.id\n\n// With alias (required when multiple FKs point to the same type)\n@db.rel.FK \"author\"\nauthorId: User.id\n```\n",
3542
+ nodeType: ["prop"],
3543
+ argument: {
3544
+ optional: true,
3545
+ name: "alias",
3546
+ type: "string",
3547
+ description: "Alias for pairing with @db.rel.to. Required when multiple FKs point to the same target type."
3548
+ },
3549
+ validate(token, args, doc) {
3550
+ const errors = [];
3551
+ const field = token.parentNode;
3552
+ const alias = args[0]?.text;
3553
+ const owner = getDbTableOwner(token);
3554
+ if (!owner || owner.countAnnotations("db.table") === 0) errors.push({
3555
+ message: "@db.rel.FK is only valid on fields of a @db.table interface",
3556
+ severity: 1,
3557
+ range: token.range
3558
+ });
3559
+ if (field.countAnnotations("db.rel.to") > 0 || field.countAnnotations("db.rel.from") > 0) errors.push({
3560
+ message: "A field cannot be both a foreign key and a navigational property",
3561
+ severity: 1,
3562
+ range: token.range
3563
+ });
3564
+ const definition$1 = field.getDefinition();
3565
+ if (!definition$1 || !isRef(definition$1) || !definition$1.hasChain) {
3566
+ errors.push({
3567
+ message: `@db.rel.FK requires a chain reference type (e.g. User.id), got scalar type`,
3568
+ severity: 1,
3569
+ range: token.range
3570
+ });
3571
+ return errors;
3572
+ }
3573
+ const ref$1 = definition$1;
3574
+ const refTypeName = ref$1.id;
3575
+ const chainFields = ref$1.chain.map((c) => c.text);
3576
+ const targetUnwound = doc.unwindType(refTypeName);
3577
+ if (targetUnwound) {
3578
+ const targetDef = targetUnwound.def;
3579
+ if (isInterface(targetDef) || isStructure(targetDef)) {
3580
+ const struct = isInterface(targetDef) ? targetDef.getDefinition() : targetDef;
3581
+ if (struct && isStructure(struct) && chainFields.length > 0) {
3582
+ const targetProp = struct.props.get(chainFields[0]);
3583
+ if (targetProp) {
3584
+ if (targetProp.countAnnotations("meta.id") === 0 && targetProp.countAnnotations("db.index.unique") === 0) errors.push({
3585
+ message: `@db.rel.FK target '${refTypeName}.${chainFields.join(".")}' is not a primary key (@meta.id) or unique (@db.index.unique) field`,
3586
+ severity: 1,
3587
+ range: token.range
3588
+ });
3589
+ const propDef = targetProp.getDefinition();
3590
+ if (propDef && isRef(propDef)) {
3591
+ const propUnwound = targetUnwound.doc.unwindType(propDef.id, propDef.chain);
3592
+ if (propUnwound && !isPrimitive(propUnwound.def)) errors.push({
3593
+ message: `Foreign key field must resolve to a scalar type (number, string, etc.), got '${propDef.id}'`,
3594
+ severity: 1,
3595
+ range: token.range
3596
+ });
3597
+ } else if (propDef && !isPrimitive(propDef)) errors.push({
3598
+ message: `Foreign key field must resolve to a scalar type (number, string, etc.)`,
3599
+ severity: 1,
3600
+ range: token.range
3601
+ });
3602
+ }
3603
+ }
3604
+ }
3605
+ }
3606
+ if (!alias) {
3607
+ const struct = getParentStruct(token);
3608
+ if (struct) {
3609
+ let sameTargetCount = 0;
3610
+ for (const [, prop] of struct.props) {
3611
+ if (prop.countAnnotations("db.rel.FK") === 0) continue;
3612
+ const def = prop.getDefinition();
3613
+ if (!def || !isRef(def)) continue;
3614
+ const r = def;
3615
+ if (!r.hasChain) continue;
3616
+ if (r.id === refTypeName) {
3617
+ const fkAnnotations = prop.annotations?.filter((a) => a.name === "db.rel.FK");
3618
+ const hasAlias = fkAnnotations?.some((a) => a.args.length > 0);
3619
+ if (!hasAlias) sameTargetCount++;
3620
+ }
3621
+ }
3622
+ if (sameTargetCount > 1) errors.push({
3623
+ message: `Multiple @db.rel.FK fields resolve to type '${refTypeName}' — add alias to disambiguate`,
3624
+ severity: 1,
3625
+ range: token.range
3626
+ });
3627
+ }
3628
+ }
3629
+ return errors;
3630
+ }
3631
+ }),
3632
+ to: new AnnotationSpec({
3633
+ description: "Forward navigational property — the FK is on **this** interface. The compiler resolves the matching @db.rel.FK by target type or alias.\n\n**Example:**\n```atscript\n@db.rel.to\nauthor?: User\n\n// With alias\n@db.rel.to \"author\"\nauthor?: User\n```\n",
3634
+ nodeType: ["prop"],
3635
+ argument: {
3636
+ optional: true,
3637
+ name: "alias",
3638
+ type: "string",
3639
+ description: "Match a local @db.rel.FK by alias."
3640
+ },
3641
+ validate(token, args, doc) {
3642
+ const errors = [];
3643
+ const field = token.parentNode;
3644
+ const alias = args[0]?.text;
3645
+ const owner = getDbTableOwner(token);
3646
+ if (!owner || owner.countAnnotations("db.table") === 0) errors.push({
3647
+ message: "@db.rel.to is only valid on fields of a @db.table interface",
3648
+ severity: 1,
3649
+ range: token.range
3650
+ });
3651
+ if (field.countAnnotations("db.rel.FK") > 0) errors.push({
3652
+ message: "A field cannot be both a foreign key and a navigational property",
3653
+ severity: 1,
3654
+ range: token.range
3655
+ });
3656
+ const targetTypeName = getNavTargetTypeName(field);
3657
+ if (!targetTypeName) return errors;
3658
+ const unwound = doc.unwindType(targetTypeName);
3659
+ if (unwound) {
3660
+ const targetDef = unwound.def;
3661
+ const targetNode = isInterface(targetDef) ? targetDef : undefined;
3662
+ if (!targetNode || targetNode.countAnnotations("db.table") === 0) errors.push({
3663
+ message: `@db.rel.to target '${targetTypeName}' is not a @db.table entity`,
3664
+ severity: 1,
3665
+ range: token.range
3666
+ });
3667
+ }
3668
+ const fieldDef = field.getDefinition();
3669
+ if (fieldDef && fieldDef.entity === "group" && fieldDef.op === "|") errors.push({
3670
+ message: "@db.rel.to does not support union types — use separate relations",
3671
+ severity: 1,
3672
+ range: token.range
3673
+ });
3674
+ const struct = getParentStruct(token);
3675
+ if (struct) {
3676
+ const fieldName = field.id;
3677
+ for (const [name, prop] of struct.props) {
3678
+ if (name === fieldName) continue;
3679
+ if (prop.countAnnotations("db.rel.to") === 0) continue;
3680
+ const propAlias = getAnnotationAlias(prop, "db.rel.to");
3681
+ if ((alias || undefined) === (propAlias || undefined)) {
3682
+ const otherTarget = getNavTargetTypeName(prop);
3683
+ if (otherTarget === targetTypeName) {
3684
+ errors.push({
3685
+ message: `Duplicate @db.rel.to '${alias || targetTypeName}' — only one forward navigational property per alias`,
3686
+ severity: 1,
3687
+ range: token.range
3688
+ });
3689
+ break;
3690
+ }
3691
+ }
3692
+ }
3693
+ if (alias) {
3694
+ const matches = findFKFieldsPointingTo(doc, struct, targetTypeName, alias);
3695
+ if (matches.length === 0) errors.push({
3696
+ message: `No @db.rel.FK '${alias}' found on this interface`,
3697
+ severity: 1,
3698
+ range: token.range
3699
+ });
3700
+ } else {
3701
+ const matches = findFKFieldsPointingTo(doc, struct, targetTypeName);
3702
+ if (matches.length === 0) errors.push({
3703
+ message: `No @db.rel.FK on this interface points to '${targetTypeName}' — did you mean @db.rel.from?`,
3704
+ severity: 1,
3705
+ range: token.range
3706
+ });
3707
+ else if (matches.length > 1) errors.push({
3708
+ message: `Multiple @db.rel.FK fields point to '${targetTypeName}' — add alias to disambiguate`,
3709
+ severity: 1,
3710
+ range: token.range
3711
+ });
3712
+ }
3713
+ }
3714
+ return errors;
3715
+ }
3716
+ }),
3717
+ from: new AnnotationSpec({
3718
+ description: "Inverse navigational property — the FK is on the **target** interface, pointing back to this one.\n\n**Example:**\n```atscript\n@db.rel.from\nposts: Post[]\n\n// With alias\n@db.rel.from \"original\"\ncomments: Comment[]\n```\n",
3719
+ nodeType: ["prop"],
3720
+ argument: {
3721
+ optional: true,
3722
+ name: "alias",
3723
+ type: "string",
3724
+ description: "Match a @db.rel.FK on the target interface by alias."
3725
+ },
3726
+ validate(token, args, doc) {
3727
+ const errors = [];
3728
+ const field = token.parentNode;
3729
+ const alias = args[0]?.text;
3730
+ const owner = getDbTableOwner(token);
3731
+ if (!owner || owner.countAnnotations("db.table") === 0) errors.push({
3732
+ message: "@db.rel.from is only valid on fields of a @db.table interface",
3733
+ severity: 1,
3734
+ range: token.range
3735
+ });
3736
+ if (field.countAnnotations("db.rel.FK") > 0) errors.push({
3737
+ message: "A field cannot be both a foreign key and a navigational property",
3738
+ severity: 1,
3739
+ range: token.range
3740
+ });
3741
+ const targetTypeName = getNavTargetTypeName(field);
3742
+ if (!targetTypeName) return errors;
3743
+ const unwound = doc.unwindType(targetTypeName);
3744
+ if (!unwound) return errors;
3745
+ const targetDef = unwound.def;
3746
+ const targetDoc = unwound.doc;
3747
+ if (!isInterface(targetDef) || targetDef.countAnnotations("db.table") === 0) {
3748
+ errors.push({
3749
+ message: `@db.rel.from target '${targetTypeName}' is not a @db.table entity`,
3750
+ severity: 1,
3751
+ range: token.range
3752
+ });
3753
+ return errors;
3754
+ }
3755
+ const struct = getParentStruct(token);
3756
+ if (struct) {
3757
+ const fieldName = field.id;
3758
+ for (const [name, prop] of struct.props) {
3759
+ if (name === fieldName) continue;
3760
+ if (prop.countAnnotations("db.rel.from") === 0) continue;
3761
+ const propAlias = getAnnotationAlias(prop, "db.rel.from");
3762
+ if ((alias || undefined) === (propAlias || undefined)) {
3763
+ const otherTarget = getNavTargetTypeName(prop);
3764
+ if (otherTarget === targetTypeName) {
3765
+ errors.push({
3766
+ message: `Duplicate @db.rel.from '${alias || targetTypeName}' — only one inverse navigational property per alias`,
3767
+ severity: 1,
3768
+ range: token.range
3769
+ });
3770
+ break;
3771
+ }
3772
+ }
3773
+ }
3774
+ }
3775
+ const thisTypeName = getParentTypeName(token);
3776
+ if (!thisTypeName) return errors;
3777
+ const matches = findFKFieldsPointingTo(targetDoc, targetDef, thisTypeName, alias);
3778
+ if (alias) {
3779
+ if (matches.length === 0) errors.push({
3780
+ message: `No @db.rel.FK '${alias}' found on '${targetTypeName}'`,
3781
+ severity: 1,
3782
+ range: token.range
3783
+ });
3784
+ } else if (matches.length === 0) errors.push({
3785
+ message: `No @db.rel.FK on '${targetTypeName}' points to '${thisTypeName}'`,
3786
+ severity: 1,
3787
+ range: token.range
3788
+ });
3789
+ else if (matches.length > 1) errors.push({
3790
+ message: `'${targetTypeName}' has multiple @db.rel.FK fields pointing to '${thisTypeName}' — add alias`,
3791
+ severity: 1,
3792
+ range: token.range
3793
+ });
3794
+ const fieldDef = field.getDefinition();
3795
+ if (!isArray(fieldDef) && matches.length === 1) {
3796
+ const fkProp = matches[0].prop;
3797
+ if (fkProp.countAnnotations("db.index.unique") === 0) errors.push({
3798
+ message: `@db.rel.from '${field.id}' has singular type '${targetTypeName}' (1:1) but the FK on '${targetTypeName}' is not @db.index.unique — did you mean '${targetTypeName}[]' (1:N)?`,
3799
+ severity: 2,
3800
+ range: token.range
3801
+ });
3802
+ }
3803
+ return errors;
3804
+ }
3805
+ }),
3806
+ onDelete: refActionAnnotation("onDelete"),
3807
+ onUpdate: refActionAnnotation("onUpdate")
3808
+ }
3391
3809
  };
3392
3810
 
3393
3811
  //#endregion
@@ -4669,4 +5087,4 @@ else if (isGroup(def) && def.entity !== "structure") {
4669
5087
  }
4670
5088
 
4671
5089
  //#endregion
4672
- export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BuildRepo, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticPrimitiveNode, SemanticPropNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, build, createAtscriptPlugin, defineConfig, flattenInterfaceNode, getRelPath, isAnnotate, isAnnotationSpec, isArray, isConst, isGroup, isImport, isInterface, isPhantomNode, isPrimitive, isProp, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, resolveAnnotation, resolveAtscriptFromPath, resolveConfigFile };
5090
+ export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BuildRepo, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticPrimitiveNode, SemanticPropNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, build, createAtscriptPlugin, defineConfig, findFKFieldsPointingTo, flattenInterfaceNode, getRelPath, isAnnotate, isAnnotationSpec, isArray, isConst, isGroup, isImport, isInterface, isPhantomNode, isPrimitive, isProp, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, resolveAnnotation, resolveAtscriptFromPath, resolveConfigFile };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.1.33",
3
+ "version": "0.1.34",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",