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

Wrong typings on addRelationship and where? #81

Closed
pedromtcosta opened this issue Oct 15, 2023 · 2 comments
Closed

Wrong typings on addRelationship and where? #81

pedromtcosta opened this issue Oct 15, 2023 · 2 comments
Labels
question A question about neogma or neo4j

Comments

@pedromtcosta
Copy link

I have the following snippet which works as expected for defining a model with a relationship to itself and then creating a node relating to an existing node:

const Blocks = ModelFactory({
  label: 'Block',
  schema: {
    hash: {
      type: 'string',
      required: true,
    },
    number: {
      type: 'number',
      required: true,
    }
  },
  primaryKeyField: 'hash',
}, neogma);

Blocks.addRelationships({
  Prev: {
    model: Blocks,
    direction: 'out',
    name: 'PREV',
  }
} as any);

async function main() {
  const block = await Blocks.createOne({
    hash: 'hash2',
    number: 2,
    Prev: {
      where: {
        params: {
          'hash': 'hash'
        }
      }
    } as any
  });

  console.log(block);
}

main();

The problem is that I had to add these two ugly as any casts, otherwise I get the following TypeScript errors respectively for each cast:

Argument of type '{ Prev: { model: NeogmaModel<Neo4jSupportedProperties, Object, Object, Object>; direction: string; name: string; }; }' is not assignable to parameter of type 'Partial<RelationshipsI<Object>>'.
  Object literal may only specify known properties, and 'Prev' does not exist in type 'Partial<RelationshipsI<Object>>'
Type '{ where: { params: { hash: string; }; }; }' is not assignable to type 'Neo4jSupportedTypes | undefined'.
  Object literal may only specify known properties, and 'where' does not exist in type 'Integer | Point<Integer> | Date<Integer> | Time<Integer> | LocalTime<Integer> | DateTime<Integer> | LocalDateTime<...> | Duration<...> | Neo4jSingleTypes[]'

is there a way to accomplish what I am doing without relying on these casts?

@pedromtcosta
Copy link
Author

ok, that's embarrassing but 10 minutes afterwards I figured out the problem, I had to explicitly provide the generic parameters for ModelFactory:

type BlockProperties = {
  hash: string;
  number: number;
};

type BlockRelationships = {
  Prev: ModelRelatedNodesI<
    NeogmaModel<BlockProperties, Object, Object, Object>,
    NeogmaInstance<BlockProperties, BlockRelationships, Object>
  >;
};

const Blocks = ModelFactory<BlockProperties, BlockRelationships>({
  label: 'Block',
  schema: {
    hash: {
      type: 'string',
      required: true,
    },
    number: {
      type: 'number',
      required: true,
    }
  },
  primaryKeyField: 'hash',
}, neogma);

Blocks.addRelationships({
  Prev: {
    model: Blocks,
    direction: 'out',
    name: 'PREV',
  }
});

async function main() {
  const block = await Blocks.createOne({
    hash: 'hash2',
    number: 2,
    Prev: {
      where: {
        params: {
          'hash': 'hash'
        }
      }
    }
  });

  console.log(block);
}

main();

@themetalfleece
Copy link
Owner

Glad you figured it out :)
Unrelated to the type, you can also use the self value to indicate that the model refers to itself: https://themetalfleece.github.io/neogma/docs/Models/Defining-a-Model#relating-a-model-to-itself

@themetalfleece themetalfleece added the question A question about neogma or neo4j label Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A question about neogma or neo4j
Projects
None yet
Development

No branches or pull requests

2 participants