Code Monkey home page Code Monkey logo

Comments (4)

MrLeebo avatar MrLeebo commented on May 18, 2024 1

Hi @abrl91,

You're right, the API isn't very clear about this. The .then() function is always going to return the current subject of the builder.

Subjects can only be nodes that can contain other nodes, such as models which can contain fields or attributes, or fields which can contain attributes. An attribute never becomes the current subject because it doesn't contain anything else. However, I agree that it isn't very satisfying from a user perspective for the builder to make this distinction. It would be nice for the TypeScript system to automatically assign the type of the current subject to .then() so that you would at least know what type you were receiving, but I couldn't figure out how to make that work.

These are the kinds of kinks that I want to make sure are worked out of the interface before releasing v1 of the library, and I'm open to suggestions for how it can be improved.

.removeAttribute() similarly, is expecting to target field attributes, not model attributes. Although I don't think there's any technical reason why it couldn't support both. That would be a good enhancement.

In the meantime, I think this would achieve the effect you're seeking:

    builder.model(model.name).then<Model>((model) => {
      for (const prop of model.properties) {
        if (prop.type === 'attribute' && prop.name === ID_ATTRIBUTE_NAME) {
          prop.name = UNIQUE_ATTRIBUTE_NAME;
          return;
        }
      }
    });

from prisma-ast.

abrl91 avatar abrl91 commented on May 18, 2024

@MrLeebo Thank you, it works!

from prisma-ast.

abrl91 avatar abrl91 commented on May 18, 2024

Hi @abrl91,

You're right, the API isn't very clear about this. The .then() function is always going to return the current subject of the builder.

Subjects can only be nodes that can contain other nodes, such as models which can contain fields or attributes, or fields which can contain attributes. An attribute never becomes the current subject because it doesn't contain anything else. However, I agree that it isn't very satisfying from a user perspective for the builder to make this distinction. It would be nice for the TypeScript system to automatically assign the type of the current subject to .then() so that you would at least know what type you were receiving, but I couldn't figure out how to make that work.

These are the kinds of kinks that I want to make sure are worked out of the interface before releasing v1 of the library, and I'm open to suggestions for how it can be improved.

.removeAttribute() similarly, is expecting to target field attributes, not model attributes. Although I don't think there's any technical reason why it couldn't support both. That would be a good enhancement.

In the meantime, I think this would achieve the effect you're seeking:

    builder.model(model.name).then<Model>((model) => {
      for (const prop of model.properties) {
        if (prop.type === 'attribute' && prop.name === ID_ATTRIBUTE_NAME) {
          prop.name = UNIQUE_ATTRIBUTE_NAME;
          return;
        }
      }
    });

The issue with the .then() was confusing because it seems like the subject can get Block or Property

image

About removeAttribute() - I know it is only for a field attribute. I just wrote it as a pseudo code to emphasize what I am trying to do.

BTW, we use your library for a very important feature (in Amplication), and I must say that it really helps us and it is very easy to use and understand.

from prisma-ast.

MrLeebo avatar MrLeebo commented on May 18, 2024

The issue with the .then() was confusing because it seems like the subject can get Block or Property

Yeah, that type definition is too broad. It should be schema.Block | schema.Field

from prisma-ast.

Related Issues (14)

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.