Code Monkey home page Code Monkey logo

adonis-lucid-polymorphic's People

Contributors

enniel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

adonis-lucid-polymorphic's Issues

Confusing whereHas functionality

Hey, I've been using adonis-lucid-polymorphic on a new project and just encountered a confusing issues. Briefly, the MorphTo's whereHas query (seems like also true for has query) only considers the first model in the relatedModels array, resulting in incomplete results.

Example:
Model Setup

class Video extends Model {
}

class Post extends Model {
}

class Comment extends Model {
  static get traits () {
    return ['@provider:Morphable']
  }

  commentable () {
    return this.morphTo([
      Video,
      Post
    ], 'id', 'id', 'commentable_id', 'commentable_type')
  }
}

When I run this code

let _query = null;
Comment.onQuery((query) => (_query = query));
await Comment.query().whereHas('commentable', b=>b.where('id', '>', 2)).fetch();

The resulting SQL query (_query.sql) is
'select * from "comments" where exists (select * from "videos" where "id" > ? and "comments"."commentable_id" = "videos"."id")'

The problem
The query should return all the comments for the commentables with id>2. Instead, it considers only the Video model and neglects the Post model. So for example, if I had no videos but only 10 posts (ids 1-10), the query would return zero results which isn't correct (should have returned 8 posts, with ids 3-10).

Thanks for your help.

morphToMany / morphedByMany ?

As far as I can tell this is not already supported...

Can we add the ability to define a pivot table for polymorphic relations?

For the use case of; a single shared list of tags, that can be applied to multiple content types.

ie:

tags:

  • technology
  • science
  • entertainment

content types:

  • blog post
  • video
  • article

multiple blog posts, videos and articles might all be tagged with 'science' but 'science' should only exist as a tag one time, and simply be referenced multiple times as one does with a regular many to many relationship utilizing a pivot table.

I suppose a workaround might be just to make the 'taggable' pivot table an actual model, and then setup 1:many relationships between it and tags, and regular polymorphic relations between taggable and the various content types.

Would be nice to not have to make taggable a model though. Here's an example of how it's done in laravel for reference: https://itsolutionstuff.com/post/laravel-many-to-many-polymorphic-relationship-tutorialexample.html

Trying to eagerload relationship twice by using multiple with(), different relationships

Package version

6.1.3

Node.js and npm version

9.1.0 and 6.4.1

Sample Code (to reproduce the issue)

Getting this error:
E_CANNOT_OVERRIDE_RELATION: Trying to eagerload headChef relationship twice

When I try to fetch multiple with:
await Kitchen.query().with('headChef').with('chef').fetch()

both headChef() and chef() belongs to the same model App/Models/Chef but using different id (head_chef_id, chef_id)

Cannot read property of 'ioc' of null

Getting this error just after adding adonis-lucid-polymorphic/providers/PolymorphicProvider to the app providers.

Debug:
`warning:
WARNING: Adonis has detected an unhandled promise rejection, which may
cause undesired behavior in production.
To stop this warning, use catch() on promises or wrap await
calls inside try/catch.

TypeError: Cannot read property 'ioc' of null
at IocResolver.get ioc [as ioc] (/app/node_modules/@adonisjs/lucid/lib/iocResolver.js:58:23)
at Object. (/app/node_modules/adonis-lucid-polymorphic/src/Traits/Morphable.js:4:17)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Module.require (internal/modules/cjs/loader.js:636:17)
at require (internal/modules/cjs/helpers.js:20:18)
at PolymorphicProvider.register (/app/node_modules/adonis-lucid-polymorphic/providers/PolymorphicProvider.js:7:23)`

New 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 this issue or send me email.

Support for Morph Mapping.

This is similar to laravels morphMap.

Relation::morphMap([
'posts' => 'App\Post',
'videos' => 'App\Video',
]);

[Enhancement] support to work with additional fields on pivot table

Appreciate to your great job.
As like laravel on PHP, can you support to handle pivot data?

I need to utilize pivot data as below workaround in my application

eg. (pseudo code)
File {
  ...
}

Attachment {
  ...
  file_id
  as (String)
  attachable_id
  attachable_type
}

User {
  attachments() {
    return morphMany('Files', 'attachable')
  }
  avatarImage() {
    return this.attachments().where('as', 'avatarImage')
  }
}

Thanks, if you don't mind i will fork and make a PR for this

Create

Can I store a register with this sintax?

post.comments().create({ ... })

The console show this error

post.comments is not a function

Is there will be a AdonisJS 5 support for this package?

I'm wondering this situation.
We'd used this package on AdonisJS 4.1 project but now we'd upgraded it from 4.1 to 5 and we need to this package so much that you have no idea.

Are you planning to going to write a lucid polymorphic package for AdonisJS v5?

Thanks..

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.