Objection.js: How to omit a field?

Created on 10 Sep 2020  路  6Comments  路  Source: Vincit/objection.js

Howdy! How do I omit fields when query? For instance, I'd like to hide password and is_admin fields.

GET http://localhost:4000/users:

[
  {
    "email": "[email protected]",
    "is_admin": true,
    "name": "Some One",
    "password": "this_is_the_password"
  },
  {
    "email": "[email protected]",
    "is_admin": false,
    "name": "Any One",
    "password": "this_is_the_password"
  }
]

GET http://localhost:4000/users/1:

{
    "email": "[email protected]",
    "is_admin": true,
    "name": "Some One",
    "password": "this_is_the_password"
}

Objection.js (v2.2.3), TypeScript (v4.0.2), Koa (v2.13.0), Postgres (v12)

Most helpful comment

@Kadeluxe You are probably really young and new to open source and programming in general, so I'll give you the benefit of a doubt and assume you didn't really want to be mean. But for future reference comments like that will destroy people's willingness to write open source and help you and others by spending their free time answering questions like this.

Projects like this are written by people for free on their spare time without any compensation or other benefits. For example in my case, that message really fucking hurt and I won't be answering any questions, or fixing any bugs for a while. Thanks to you.

All 6 comments

@fookit I used this plugin: https://www.npmjs.com/package/objection-visibility

And my model looks like this:

import { Model } from "objection";
import ObjectionVisibility from "objection-visibility";

export class UserModel extends ObjectionVisibility(Model) {
    id!: string;
    name!: string;

    static tableName = "users";
    static hidden = ["password"];
}

Jesus... Why all Node ORMs look like a giant hack

You can simply select the fields you want. But for making sure password hashes and other sensitive stuff never leave the server, you can override the $formatJson method that gets called each time a model instance is serialized to json

export class UserModel extends Model {
    id!: string;
    name!: string;
    password?: string;
    is_admin?: boolean;

    static tableName = "users";

    $formatJson(json) {
      json = super.$formatJson(json);
      delete json.password;
      delete json.is_admin;
      return json;
    }
}

I hope password field contains a hash of the password and not the real password though :wink:

@Kadeluxe You are probably really young and new to open source and programming in general, so I'll give you the benefit of a doubt and assume you didn't really want to be mean. But for future reference comments like that will destroy people's willingness to write open source and help you and others by spending their free time answering questions like this.

Projects like this are written by people for free on their spare time without any compensation or other benefits. For example in my case, that message really fucking hurt and I won't be answering any questions, or fixing any bugs for a while. Thanks to you.

Thanks @koskimas :)

@koskimas Dude u r a friggin legend for creating this project. Thanks for writing this great piece of software. Dont get disheartened, we appreciate u.

Was this page helpful?
0 / 5 - 0 ratings