Giter Site home page Giter Site logo

Refactor .find() about aurelia-orm HOT 11 CLOSED

spoonx avatar spoonx commented on August 16, 2024
Refactor .find()

from aurelia-orm.

Comments (11)

RWOverdijk avatar RWOverdijk commented on August 16, 2024

@Nexbit Hello again :) Looking at the code, I don't think this makes sense. .find() is a public method used to fetch (find) data for a specific resource. If (for instance) you want to get the delegates you have a couple options:

Association:

entity.find('user').then(user => {
  // you can use `user.delegates` if you set up the association in the entity
});

Repository:

class MyRepository extends Repository {
  findDelegates() {
    return this.findPath(`${this.resource}/delegates`);
  }
}

The repository way is a custom implementation. This has been kept open to allow flexibility. Therefor, I'd say no to refactoring find() (which has a very specific purpose).

Something like this could work, but is a breaking change:

findPath(path, criteria, raw) {
  if (path[0] !== '/') {
    path = `${this.resource}/${path}`;
  }
}

This follows the convention of absolute and relative paths. I'd need a vote on this though.

from aurelia-orm.

pfurini avatar pfurini commented on August 16, 2024

@RWOverdijk I agree with your reasoning, and maybe the findPath refactoring you suggest could be added in the next major version.

from aurelia-orm.

doktordirk avatar doktordirk commented on August 16, 2024

just to make sure: you do know that criteria is used as part of the path if it is a number or string only?

so, find(1) translates to eg api/users/1
or findPath('users',1) to api/users/1

from aurelia-orm.

pfurini avatar pfurini commented on August 16, 2024

@doktordirk Yeah I used that to retrieve single resources directly, but my original issue was with findPath when retrieving a related resource like orders/1/items, as described in the comment I pointed out in my first post.
I know that libraries like sails or loopback are more plain, given that they are auto-generated for the most part, but other hand-crafted apis can make good use of sub-resources (without abusing it, or it will result in a nesting nightmare).

from aurelia-orm.

doktordirk avatar doktordirk commented on August 16, 2024

so you.d need a entity.find then?

from aurelia-orm.

gregoryagu avatar gregoryagu commented on August 16, 2024

I think it would be nice to have a repository method similar to .findPath(), but .call(). Same parameters, but it would call an action on top of the root path.

In other words a User repository would call "user/list/1" for .call("list",1).

I really need this for ASPNetCore where the Controller is "user" but it typically takes an action name as well.

call(path:string, criteria:any) {

        let fullPath = this.getResource() + "/" + path;

        return this.findPath(fullPath, criteria);
    }

I think this is the same issue that @Nexbit was having.

from aurelia-orm.

RWOverdijk avatar RWOverdijk commented on August 16, 2024

If it does exactly the same, you can just extend it in your application.

from aurelia-orm.

gregoryagu avatar gregoryagu commented on August 16, 2024

@RWOverdijk Ok. I actually tried to extend it, but I am missing something in how I set it up.

I Subclassed Repository to AppRespository.

Then then set it as the Repository using the decorator on the Contact Entity Type.

import {AppRepository} from 'repository/app-repository';
@repository(CustomRepository)
...

But then when I attempted to get an instance in the viewmodel:

entityManager.getRepository('contact');

It returned a regular repository, not an AppRepository so I could call my custom methods.

Am I doing something wrong in the wireup?

from aurelia-orm.

RWOverdijk avatar RWOverdijk commented on August 16, 2024

@gregoryagu Did you specify the @resource on the entity as 'contact'?

Also, it should be possible to overwrite the default repository. Simply register your custom repository as Repository on DI.

from aurelia-orm.

RWOverdijk avatar RWOverdijk commented on August 16, 2024

Closing this due to inactivity. Feel free to reopen if this question still exists :)

from aurelia-orm.

gregoryagu avatar gregoryagu commented on August 16, 2024

Yes, this was resolved. Sorry, forgot to update this. Thanks very much.

from aurelia-orm.

Related Issues (20)

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.