Comments (4)
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.
@MrLeebo Thank you, it works!
from prisma-ast.
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
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.
The issue with the
.then()
was confusing because it seems like thesubject
can getBlock
orProperty
Yeah, that type definition is too broad. It should be schema.Block | schema.Field
from prisma-ast.
Related Issues (14)
- Schema Builder API
- Throws when passing an empty array into an attribute HOT 3
- Doesn't extend existing model HOT 6
- Issue with documentation example HOT 2
- Bug with db.ObjectId HOT 1
- Add support for views HOT 4
- @@map in Enums can not be parsed properly HOT 1
- `getSchema` - model attributes are not parsed HOT 1
- feature request: Include source code location for each node HOT 9
- getSchema doesn't produce break-nodes HOT 3
- Thank you! HOT 2
- MismatchedTokenException: Expecting --> '}' <-- but found --> 'model' <--
- Trailing comments not handled properly HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prisma-ast.