Use either Item#onCreated or Item#onUpdate and add the enchantment that way. If you use Item#onUpdate, make sure you check that the sword isn't enchanted before adding the enchantment as it will give it an enchantment every tick sorta spamming you out.
I have a sword in my mod so what code d oI need to add enchant to it?