-
-
Notifications
You must be signed in to change notification settings - Fork 226
Add RelationRegistry API for custom relations #1186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Melchyore
wants to merge
5
commits into
adonisjs:22.x
Choose a base branch
from
Melchyore:feat/custom-relations-api
base: 22.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+703
β24
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66bc92e
feat: add RelationRegistry API for custom relations
Melchyore 0296b58
fix: use js extension
Melchyore 23f278f
fix: lint errors
Melchyore 7da07a6
fix: store relation multiplicity on instances instead of registry lookup
Melchyore 4edf3cf
fix: reject registration of built-in relation type names
Melchyore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * @adonisjs/lucid | ||
| * | ||
| * (c) Harminder Virk <virk@adonisjs.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| import type { LucidModel } from '../../types/model.js' | ||
| import type { ModelRelationTypes } from '../../types/relations.js' | ||
|
|
||
| /** | ||
| * Utility to create a custom relation decorator. | ||
| * Useful for third-party packages that want to add custom relation types. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // In your package | ||
| * export const myRelation: MyRelationDecoratorType = createRelationDecorator( | ||
| * 'myRelation' | ||
| * ) | ||
| * | ||
| * // Usage | ||
| * class SomeModel extends BaseModel { | ||
| * @myRelation(() => SomeModel, { ...someOptions }) | ||
| * declare someRelation: MyRelation<typeof SomeModel> | ||
| * } | ||
| * ``` | ||
| */ | ||
| export function createRelationDecorator< | ||
| TRelationType extends ModelRelationTypes['__opaque_type'] = ModelRelationTypes['__opaque_type'], | ||
| TOptions = any, | ||
| >( | ||
| relationType: TRelationType | ||
| ): (relatedModel: () => LucidModel, options?: TOptions) => PropertyDecorator { | ||
| return function decorator(relatedModel, options?) { | ||
| return function decorateAsRelation(target, property: string | symbol) { | ||
| const Model = target.constructor as LucidModel | ||
| Model.boot() | ||
| const propertyName = typeof property === 'symbol' ? property.toString() : property | ||
| Model.$addRelation( | ||
| propertyName, | ||
| relationType, | ||
| relatedModel, | ||
| Object.assign({ relatedModel }, options) as any | ||
| ) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * @adonisjs/lucid | ||
| * | ||
| * (c) Harminder Virk <virk@adonisjs.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| import type { RelationFactory, RelationFactoryConfig } from '../../types/relations.js' | ||
|
|
||
| /** | ||
| * Registry for managing custom relation types. | ||
| * Allows third-party packages to register new relation types. | ||
| */ | ||
| export class RelationRegistry { | ||
| private static relations = new Map<string, RelationFactory>() | ||
|
|
||
| /** | ||
| * Registers a new relation type | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * RelationRegistry.register('myRelation', { | ||
| * create(relationName, relatedModel, options, model) { | ||
| * return new MyRelation(relationName, relatedModel, options, model) | ||
| * } | ||
| * }) | ||
| * ``` | ||
| */ | ||
| static register(type: string, factoryConfig: RelationFactoryConfig): void { | ||
| const BUILT_IN_TYPES = ['hasOne', 'hasMany', 'belongsTo', 'manyToMany', 'hasManyThrough'] | ||
|
|
||
| if (BUILT_IN_TYPES.includes(type)) { | ||
| throw new Error( | ||
| `Cannot register "${type}": it is a built-in relation type. ` + | ||
| `Built-in types (${BUILT_IN_TYPES.join(', ')}) cannot be overridden. ` + | ||
| `Please use a different name for your custom relation.` | ||
| ) | ||
| } | ||
|
|
||
| if (this.relations.has(type)) { | ||
| throw new Error(`Relation type "${type}" is already registered`) | ||
| } | ||
|
|
||
| // Inject the type into the factory config | ||
| const factory: RelationFactory = { | ||
| ...factoryConfig, | ||
| type, | ||
| } | ||
|
|
||
| this.relations.set(type, factory) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Retrieves a relation factory by type | ||
| */ | ||
| static get(type: string): RelationFactory | undefined { | ||
| return this.relations.get(type) | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a relation type is registered | ||
| */ | ||
| static has(type: string): boolean { | ||
| return this.relations.has(type) | ||
| } | ||
|
|
||
| /** | ||
| * Unregisters a relation type (mainly for testing) | ||
| */ | ||
| static unregister(type: string): void { | ||
| this.relations.delete(type) | ||
| } | ||
|
|
||
| /** | ||
| * Clears all registered relations (mainly for testing) | ||
| */ | ||
| static clear(): void { | ||
| this.relations.clear() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.