Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
enniel committed Jun 20, 2017
0 parents commit d072ae2
Show file tree
Hide file tree
Showing 18 changed files with 3,603 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
73 changes: 73 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Created by https://www.gitignore.io/api/node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# Yarn lock file
yarn.lock

# Package lock file
package-lock.json

# dotenv environment variables file
.env

# sqlite storage
test/unit/storage


# End of https://www.gitignore.io/api/node
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bin
node_modules
npm-debug.log
npm-error.log
yarn-debug.log
yarn-error.log
yarn.lock
package-lock.json
.editorconfig
.env.example
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2017 Evgeny Razumov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
178 changes: 178 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Adonis Lucid Polymorphic (WORK IN PROGRESS)

Polymorphic Relations support for [Adonis Lucid](http://adonisjs.com/docs/3.2/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 - text
videos
id - integer
title - string
url - string
comments
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 ['Adonis/Lucid/MorphTrait']
}

comments () {
return this.morphMany('App/Model/Comment', 'commentable')
}
}

module.exports = Post
```

```js
// App/Model/Video
'use strict'

const Model = use('Lucid')

class Video extends Model {
static get traits () {
return ['Adonis/Lucid/MorphTrait']
}

comments () {
return this.morphMany('App/Model/Comment', 'commentable')
}
}

module.exports = Video
```

```js
// App/Model/Comment
'use strict'

const Model = use('Lucid')

class Comment extends Model {
static get traits () {
return ['Adonis/Lucid/MorphTrait']
}

commentable () {
return this.morphTo('commentable', [
'App/Model/Post', 'App/Model/Video'
])
}
}

module.exports = Video
```

## API

### morphTo(determiner, morphMap, [primaryKey])

```js
...

class Comment extends Model {
static get traits () {
return ['Adonis/Lucid/MorphTrait']
}

commentable () {
return this.morphTo('commentable', [
'App/Model/Post', 'App/Model/Video'
], 'id')
}
}

...
```

### morphMany(relatedModel, determiner, [primaryKey])

```js
...

class Post extends Model {
static get traits () {
return ['Adonis/Lucid/MorphTrait']
}

comments () {
return this.morphMany('App/Model/Comment', 'commentable', 'id')
}
}

...
```

### morphOne(relatedModel, determiner, [primaryKey])

```js
...

class Publication extends Model {
static get traits () {
return ['Adonis/Lucid/MorphTrait']
}

content () {
return this.morphOne('App/Model/Content', 'contentable', 'id')
}
}

...
```

## 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.
73 changes: 73 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "adonis-lucid-polymorphic",
"version": "0.0.1",
"description": "The Adonis Lucid Polymorphic Relations Support.",
"main": "src/Relations",
"directories": {
"test": "test"
},
"scripts": {
"test": "npm run test:pg && npm run test:mysql && npm run test:sqlite3",
"test:pg": "DB=pg _mocha test/unit",
"test:mysql": "DB=mysql _mocha test/unit",
"test:sqlite3": "DB=sqlite3 _mocha test/unit",
"lint": "standard",
"lint:fix": "standard --fix",
"precommit": "npm run test && lint-staged",
"prepush": "npm run test && lint-staged"
},
"lint-staged": {
"*.js": [
"npm run lint:fix",
"git add"
]
},
"author": "Evgeny Razumov (enniel)",
"license": "MIT",
"dependencies": {
"cat-log": "^1.0.2",
"lodash": "^4.17.4"
},
"repository": {
"type": "git",
"url": "git+https://github.com/enniel/adonis-lucid-polymorphic.git"
},
"keywords": [
"adonis",
"lucid",
"adonis-lucid",
"polymorphic",
"relations"
],
"bugs": {
"url": "https://github.com/enniel/adonis-lucid-polymorphic/issues"
},
"homepage": "https://github.com/enniel/adonis-lucid-polymorphic#readme",
"devDependencies": {
"adonis-fold": "^3.0.3",
"adonis-lucid": "^3.0.16",
"bluebird": "^3.5.0",
"chai": "^3.5.0",
"co-fs-extra": "^1.2.1",
"co-mocha": "^1.2.0",
"dirty-chai": "^1.2.2",
"dotenv": "^4.0.0",
"husky": "^0.13.4",
"lint-staged": "^3.6.1",
"mocha": "^3.3.0",
"mysql": "^2.13.0",
"pg": "^6.1.5",
"semver": "^5.3.0",
"sqlite3": "^3.1.8",
"standard": "^10.0.2"
},
"standard": {
"global": [
"describe",
"it",
"after",
"before",
"context"
]
}
}
20 changes: 20 additions & 0 deletions providers/PolymorphicProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

/**
* adonis-lucid-polymorphic
* Copyright(c) 2017 Evgeny Razumov
* MIT Licensed
*/

const ServiceProvider = require('adonis-fold').ServiceProvider

class PolymorphicProvider extends ServiceProvider {
* register () {
this.app.bind('Adonis/Lucid/MorphTrait', function () {
const MorphTrait = require('../src/Traits/MorphTrait')
return new MorphTrait()
})
}
}

module.exports = PolymorphicProvider
Loading

0 comments on commit d072ae2

Please sign in to comment.