Comments (6)
Can you show the original TaskScript definition in your prisma schema? Also, please check if version 0.4.3 resolves it for you.
from prisma-ast.
There is an improvement with 0.4.3. Now the lib doesn't duplicate my models but it does duplicate fields inside models
I tested on different models and the result is the same so the body of TaskScript is not important here
from prisma-ast.
@niba OK. I didn't originally intend for fields to have the same "Add or update" behavior that models get, but I agree it makes sense for them to do that too. Since with models it was an expected behavior that wasn't working, I published that fix as a patch, but since this is changing the expected behavior for fields, I've upgraded this update to a minor release (v0.5.0) and added an example about it to the README.
Note that attributes WON'T behave this way, and you will still see duplicate attributes if you call builder.attribute("foo")
twice. Reason is that multiple instances of the same attribute is theoretically a valid use case.
from prisma-ast.
@MrLeebo okay, my bad. I thought that it supposed to work in that way
So is there a way to rewrite an existing field? My case is that I have schema generated from existing DB and all of createdAt and updatedAt DateTime fields have wrong attributes. I wanted to use your tool to write a script that replaces all attributes of createdAt and updatedAt. So basically change createdat DateTime? @db.Timestamptz(6)
to createdat DateTime? @default(now())
My result right now is what you said: createdat DateTime? @db.Timestamptz(6) @default(now())
I would like to remove this @db.Timestamptz(6)
attribute but dont know how.
from prisma-ast.
I've added builder.removeField('fieldName')
and builder.removeAttribute('attributeName')
helpers to help with these kinds of edits, so you can change it by dropping and re-creating the field or attribute.
model TaskScript {
createdAt DateTime? @db.Timestamptz(6)
}
builder
.model('TaskScript')
.field('createdAt')
.removeAttribute('db.Timestamptz')
.attribute('default', [{ name: 'now' }]);
model TaskScript {
createdAt DateTime? @default(now())
}
But, it isn't exactly scalable to just keep adding new helpers for every interaction with the builder, so another thing you can do is programmatically access the underlying schema object that prisma-ast builds. This is a good way to make custom changes to the schema.
import { Field, Attribute } from '@mrleebo/prisma-ast'
builder
.model('TaskScript')
.field('createdAt')
.then<Field>((field) => {
const attribute: Attribute = {
type: 'attribute',
kind: 'field',
name: 'default',
args: [{ type: 'attributeArgument', value: 'now()' }],
};
field.attributes = [attribute];
})
model TaskScript {
createdAt DateTime? @default(now())
}
It's a little bit more verbose, but it gives you more flexibility to write your own custom logic. You can chain .then()
calls on fields, models, and other block-level elements like generators, datasources, and enums. And this is the same underlying object that the existing helpers manipulate.
from prisma-ast.
Really thank you for this awesome support.
I tried your new API and had some problems with removing my 'db.Timestamptz(6)'
attribute. I dont know why but it didn't want to remove it. Maybe because of this argument (6)
? You can try to test it again because someone can have this problem in the future.
The second approach was much better for me. I managed to do everything what I want and now I have full control over my fields. Thanks for that.
Happy new year 🎉!
from prisma-ast.
Related Issues (14)
- Schema Builder API
- Throws when passing an empty array into an attribute HOT 3
- 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
- Renaming a model attribute or deleting an attribute and creating a new attribute with the same args of the deleted one HOT 4
- 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.