How to define a validation regular expression pattern for a prisma model attribute type? - prisma

In prisma schema, I want to define a simple model like:
model NaturalPerson {
id String ##id
cpf String?
}
but here is the catch: I want the attribute cpf to match a certain regular expression ^[0-9]{11}$.
Is it possible to define such validation?

You cannot define this validation in the schema file, you can enforce it while creating records.
Before invoking the prisma create query you could use Joi to validate the value which is being inserted.
Here's how you can match your regex pattern: Joi pattern regex

Related

Does Prisma support composite keys with one of the attribute being NULL for PostgreSQL?

In PostgreSQL you can make a table with 2 columns as the composite key of that table, with one of them being NULL-able. I was just wondering how I can achieve this with Prisma.
In the current version of Prisma that I have (3.14.0), Prisma does allow composite key using ##id([column1,column2]), but only if those two columns are mandatory.
No, Prisma doesn't support composite keys with optional columns. ID Definitions are required.
Example:
This would work
model Post {
title String
content String
##id([title, content])
}
But this wouldn't as column content is defined as optional
model Post {
title String
content String?
##id([title, content])
}
You can create a Feature Request here to support allowing nullable columns in composite id.

JPA Criteria API: entity type expression

JPQL has entity type expression, e.g.
SELECT e
FROM Employee e
WHERE TYPE(e) IN (Exempt, Contractor)
Does criteria API have entity type expression?
Yes. Path.type() creates an Expression corresponding to the type of the given path. You can then create class literals using CriteriaBuilder.literal() for use in comparison expressions.

How to reconcile Relay/GraphQL's globalId with mobgodb's _id?

I'm planning to use mobgodb as my backend storage and graphql + relay for the client-server communication.
How can I reconcile Relay's globalId and Mongo id? Should they even be the same, if not how can I connect one to another?
I think there are two options:
Use mongoose and set the id option to true on your models, it will generate an id attribute with the hex string
or on your graphql schemas add an id field and resolve it this way (not tested)
resolve(me) {
return me._id.toString()
}
globalIdField is usually used to define the id field for the graphql entity and internally it uses toGlobalId function which accepts the id as the 2nd argument. fromGlobalId function could then be used in the node interface definition to extract both id and the defined type.
Here is a mongodb example of how to define the id field, and then use it.

mongoengine - Ignore extra fields for schema validation

I am trying to query my database. Some records currently have extra fields that are not included in my model schema (by error, but I want to handle these cases). When I try to query the DB and transform the records into the schema, I get the following error:
FieldDoesNotExist
The field 'X' does not exist on the document 'Y'
Because of the extra fields in the database that differ from the schema.
Is there a way to ignore this schema validation for extra fields in mongoengine?
For ignoring this error when having extra fields while data loading, set strict to False in your meta dictionary.
class User(Document):
email = StringField(required=True, unique=True)
password = StringField()
meta = {'strict': False}
I believe you want to use a DynamicDocument instead of a Document when defining your model and that will allow extra fields in the db schema to be ignored.
I think you want skip schema validation, so when you save your document
document_name.save(validate=False)
You can extend from mon.DynamicDocument.
class AF(mon.DynamicDocument):
meta = {
'collection': 'af'
}
user_id = mon.StringField(db_field='customer_user_id')
You can see from the document. A Dynamic Document class is allowing flexible, expandable and uncontrolled schemas.
Just define your class with DynamicDocument
class Y(DynamicDocument):
pass
Add whatever attrs you want
o=Y()
o.attr1="abc"
Save it ;-) without error
o.save()

Something I don't quite understand about Conditional Mappings

In most cases, we can map a field in a table either to a property or we can map it using conditional mapping, but not both. Only exception is if condition is set to Is NotNull, since then we can also map to a column.
a) Is this the reason why we are able to map a DB column only once - namely, if field was allowed to have both a property mapping and a conditional mapping, then property mapping would tell EF to retrieve all table rows, while conditional mapping would tell EF to retrieve only those rows that satisfy the condition?!
b) If my reasoning under a) is correct, then why is field allowed to have both mappings when condition is set to Is NotNull? Why won't that create a conflict?
Thank you
Mapping with condition Is NotNull has special meaning because it requires subsequent change in your model. The mapped property in the model must not be nullable. So your column in the database is nullable, your mapping condition filters all records with null value and your property always receives only records with non-null values. Also you can never assign null to the property.
In case of common condition with value equality this special behaviour is not possible.