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

Unable to register Typescript Model #142

Open
ghost opened this issue Jul 16, 2019 · 3 comments
Open

Unable to register Typescript Model #142

ghost opened this issue Jul 16, 2019 · 3 comments

Comments

@ghost
Copy link

ghost commented Jul 16, 2019

The Typescript example shows the following:

import { Nohm, NohmModel, TTypedDefinitions } from 'nohm';

// We're gonna assume the basics are clear and the connection is set up etc. - look at the ES6 example otherwise.
// This example highlights some of the typing capabilities in nohm.

interface IUserProperties {
  email: string;
  visits: number;
}

class UserModel extends NohmModel<IUserProperties> {
  public static modelName = 'User';

  protected static definitions: TTypedDefinitions<IUserProperties> = {
    // because of the TTypedDefinitions we can only define properties keys here that match our interface keys
    // the structure of the definitions is also typed
    email: {
      type: 'string', // the type value is currently not checked. If you put a wrong type here, no compile error will appear.
      unique: true,
      validations: ['email'],
    },
    visits: {
      defaultValue: 0,
      index: true,
      type: function incrVisitsBy(value, _key, old): number {
        return old + value; // TS Error: arguments are all strings, not assignable to number
      },
    },
  };

  public getVisitsAsString(): string {
    return this.property('visits'); // TS Error: visits is number and thus not assignable to string
  }

  public static async loadTyped(id: string): Promise<UserModel> {
    // see main() below for explanation
    return userModelStatic.load<UserModel>(id);
  }
}

const userModelStatic = nohm.register(UserModel);

However, when using this exact setup I receive the following error:
Error: Class is not extended properly. Use the return Nohm.register() instead of your class directly.

I am on Node v8.16.0

Tsconfig:

{
   "compilerOptions": {
      "lib": [
         "es7"
      ],
      "target": "es2017",
      "module": "commonjs",
      "outDir": "./build",
      "sourceMap": true
   }
}

Any help is appreciated.

@maritz
Copy link
Owner

maritz commented Jul 17, 2019

Maybe the example isn't quite clear here, since its only supposed to show the typing stuff, not how to generally use nohm. I'll look into fixing that.

nohm.register returns the class you are supposed to use, as explained here.

In the grand scheme a bigger refactoring to make this work with just the extended model would probably be good. I'm not sure anymore why I didn't change that back when I rewrote all of it for v2.

Here's the continuation of that example that will make use of the registered model:

const userModelStatic = Nohm.register(UserModel);

// some wrapping to make it work in a single file with async/await
const main = async () => {
  await redisConnector(); // a custom helper
  try {
    const user = new userModelStatic();

    user.property({ email: `foobar@example.com` });
    await user.save();

    try {
      const user2 = new UserModel(); // wrong invocation
      await user2.save(); // will not be reached
      console.error('This line should never appear!');
      process.exit(1);
    } catch (e) {
      console.info('As expected it failed:', e);
    }
  } finally {
    await redisCloser(); // a custom helper
  }
};

main().catch((err) => {
  console.error('Unexpected failure:', err);
});

@maritz
Copy link
Owner

maritz commented Jul 17, 2019

Sorry, I'm sick right now, my brain doesn't work quite right. The documentation is clearly missing that as well.

@ghost
Copy link
Author

ghost commented Jul 17, 2019

@maritz no problem at all, thank you for the speedy response and for maintaining this package! If I end up writing good documentation internally I may submit a PR.

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

No branches or pull requests

1 participant