Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/enniel/adonis-lucid-polymorphic
https://github.com/enniel/adonis-lucid-polymorphic
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/enniel/adonis-lucid-polymorphic
- Owner: enniel
- License: mit
- Created: 2017-05-05T13:11:56.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-08-05T12:08:29.000Z (about 3 years ago)
- Last Synced: 2024-07-24T11:57:55.156Z (about 2 months ago)
- Language: JavaScript
- Size: 57.6 KB
- Stars: 26
- Watchers: 2
- Forks: 18
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-adonisjs-packages - Adonis Lucid Polymorphic - Addon adds support polymorphic relationships into Adonis Lucid. (Databases, ORMs, Migrations & Seeding)
README
## !!! Maintainer Needed !!!
I currently don't have the resources to maintain this project. If someone is willing to put in the time to do it, I'm willing to assign the necessary privileges to that person. If you're willing to do this, reply on [issue](https://github.com/enniel/adonis-lucid-polymorphic/issues/13) or send me [email](mailto:[email protected]).
# Adonis Lucid Polymorphic
[![Build Status](https://travis-ci.org/enniel/adonis-lucid-polymorphic.svg?branch=master)](https://travis-ci.org/enniel/adonis-lucid-polymorphic)
[![Coverage Status](https://coveralls.io/repos/github/enniel/adonis-lucid-polymorphic/badge.svg)](https://coveralls.io/github/enniel/adonis-lucid-polymorphic)Polymorphic Relations support for [Adonis Lucid ^4.1.0](http://adonisjs.com/docs/4.1/lucid).
## Installation
1. Add package:
```bash
$ npm i adonis-lucid-polymorphic --save
```
or```bash
$ yarn add adonis-lucid-polymorphic
```2. Register providers inside the your bootstrap/app.js file.
```js
const providers = [
...
'adonis-lucid-polymorphic/providers/PolymorphicProvider',
...
]
```
## Examples### Table Structure
```
posts
id - integer
title - string
body - textvideos
id - integer
title - string
url - stringcomments
id - integer
body - text
commentable_id - integer
commentable_type - string
```### Model Structure
```js
// App/Model/Post
'use strict'const Model = use('Lucid')
class Post extends Model {
static get traits () {
return ['@provider:Morphable']
}comments () {
return this.morphMany('App/Models/Comment', 'id', 'commentable_id', 'commentable_type')
}
}module.exports = Post
``````js
// App/Model/Video
'use strict'const Model = use('Lucid')
class Video extends Model {
static get traits () {
return ['@provider:Morphable']
}comments () {
return this.morphMany('App/Models/Comment', 'id', 'commentable_id', 'commentable_type')
}
}module.exports = Video
``````js
// App/Model/Comment
'use strict'const Model = use('Lucid')
class Comment extends Model {
static get traits () {
return ['@provider:Morphable']
}commentable () {
return this.morphTo([
'App/Models/Post', 'App/Models/Video'
], 'id', 'id', 'commentable_id', 'commentable_type')
}
}module.exports = Video
```## API
### morphTo(relatedModels, [primaryKey], [relatedPrimaryKey], [morphIdKey], [morphTypeKey])
```js
...class Comment extends Model {
static get traits () {
return ['@provider:Morphable']
}commentable () {
return this.morphTo([
'App/Models/Post', 'App/Models/Video'
], 'id', 'id', 'commentable_id', 'commentable_type')
}
}...
```### morphMany(relatedModel, [primaryKey], [morphIdKey], [morphTypeKey])
```js
...class Post extends Model {
static get traits () {
return ['@provider:Morphable']
}comments () {
return this.morphMany('App/Models/Comment', 'id', 'commentable_id', 'commentable_type')
}
}...
```### morphOne(relatedModel, [primaryKey], [morphIdKey], [morphTypeKey])
```js
...class Publication extends Model {
static get traits () {
return ['@provider:Morphable']
}content () {
return this.morphOne('App/Models/Content', 'id', 'contentable_id', 'contentable_type')
}
}...
```## Credits
- [Evgeni Razumov](https://github.com/enniel)
## Support
Having trouble? [Open an issue](https://github.com/enniel/adonis-lucid-polymorphic/issues/new)!
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.