Skip to content
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

A better way to declare a resource. #87

Open
zackyang000 opened this issue Jun 1, 2017 · 3 comments
Open

A better way to declare a resource. #87

zackyang000 opened this issue Jun 1, 2017 · 3 comments

Comments

@zackyang000
Copy link
Owner

We want to make some changes in the next version. Defining a resource in the following way should be clearer. If you have any suggestions, please comment on this issue.

import { Resource, queryable, action } from 'node-odata';
import createError from 'http-errors';

const bookInfo = {
  title: String,
  price: Number,
};

function async checkUserAuth(req) {
   // Check the permissions of the user role
  const user = await User.get(req.query.id);
  if (user.role === 'guest') return false;
}

// Resource contains five query methods: `list/get/create/update/remove`, you can selectively cover it as needed
// In addition, you can also declare the action to do some special operations, Use `@action` decorator
export default class Book extends Resource {
  constructor() {
    super('book', bookInfo);
  }

  // `queryable` is used to make some restrictions on the list request,
  // such as default pageSize / maxTop / allowed orderby field.
  @queryable({ pageSize: 10, maxTop: 50 })
  async list(next) {
    // You can manually call the query method `next()`, so you can do something before or after of the query.
    await next();
  }

  async get(next) {
    // Check if resource meets the requirements
    const entity = req.body;
    if (entity.title === undefined) {
      throw createError.UnprocessableEntity({...});
    }
    const entity = await next();
  }

  // If you do not need to do anything, you can not declare the method
  // async create(next) {
  //   next();
  // }

  @checkUserAuth
  async remove(next) {
    next();
  }

  @checkUserAuth
  async update(next) {
    next();
  }

  @action['/50off']
  @checkUserAuth
  async halfPriceAction(id, query) {
    const entity = await Book.findOneById(id);
    entity.price /= 2;
    await entity.save();
    return entity;
  }
}
@pavelseverov
Copy link

Hi @TossShinHwa, thanks for informing!

Here are the features, that current version doesn't has, and which I have to implement bypassing node-odata:

  1. Case insensitive search in many fields at once.
    Similar to this: $filter=substringof('foo',article) eq true or substringof('foo',mail) eq true
  2. Async calls inside .post.before() or so.
    I need to perform different async mongo calls while processing the requests. It will be great to have possibility to do return next() inside before()

@zackyang000
Copy link
Owner Author

Hi @pavelseverov
correct, this 2 features I will add to next version, thanks for feedback.

@r1mar
Copy link
Contributor

r1mar commented Sep 1, 2022

Hello Zack,

I like it. This also defines the structure of a project. You have to know a little more here, but you also have more features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants