ORM
Keywords
Data
The data
keyword match the structure for sending data to the database.
It aligns with the defined schema, allowing the input of data in correspondence
with the specified field types.
For example with a schema having a 'string' field named 'name':
const data: ORM.DatabaseDataArg<MyTableSchema> = { name: 'John Doe' };
client.db.create({
// Others params...
data: data,
});
Select
Defines the structure for specifying which fields to select from a table. It facilitates the retrieval of specific fields. If the select argument is not provided, all fields are retreived.:
Find the first entry and returns the id and the name:
client.db.findFirst({
// Others params...
select: {
id: true,
name: true,
age: false,
email: false,
},
});
Where
Defines the conditions for querying the database. It supports conditional
keywords such as AND
and OR
, numerical comparators like
eq
, lt
, gt
, lte
,gte
, as well as contains
for strings.
Logical Operators: AND
and OR
The where
argument allows combining multiple conditions using logical
operators AND and OR for complex queries. When no logical operators such
as AND or OR are used explicitly, the default behavior assumes AND.
AND
: When using AND, multiple conditions are combined, and all conditions must be satisfied for a record to match.OR
: With OR, any of the specified conditions must be met for a record to match.
You can find many entries with name "bob" or an age equal to 50:
client.db.findMany({
// Others params...
where: {
OR: [{ name: 'bob' }, { age: 50 }],
},
});
Negation with isNot
The isNot
keyword negates a condition, filtering records that do not meet the
specified criterion. Mostly used to match fields that's not equals to null
.
You can find many entries where the name is "Bob":
client.db.findMany({
// Others params...
where: {
name: 'Bob',
},
});
You can find many entries where the name doesn't contains "Bob":
client.db.findMany({
// Others params...
where: {
name: { isNot: { contains: 'Bob' } },
},
});
Handling String Fields
For string fields in the schema:
Use contains
within the Where argument to filter records where the field
contains a specific string. Utilize isNot
to filter records where the field
does not meet a specified string condition.
client.db.findMany({
// Others params...
where: {
name: { contains: 'Bob' },
},
});
Handling Numeric Fields
For numeric fields in the schema:
Employ eq
, gt
, gte
, lt
, lte
within where for comparisons such as equal to,
greater than, greater than or equal to, less than, and less than or equal to,
respectively. Combine these with isNot
to filter records that do not satisfy
the specified numerical conditions.
You can find many entries where age is not equal to 50:
client.db.findMany({
// Others params...
where: {
age: { isNot: { eq: 50 } },
},
});
You can find many entries where age is lower than 50:
client.db.findMany({
// Others params...
where: {
age: { lt: 50 },
},
});
You can find many entries where age is lower than or equal to 50:
client.db.findMany({
// Others params...
where: {
age: { lte: 50 },
},
});
Ensuring Schema Consistency
The where
argument enforces adherence to the schema's defined field types
and structures:
Validates that conditions specified align with the field types (string, number, boolean, object) defined in the schema. Guarantees that logical operators (AND, OR) are used in a manner consistent with the schema's field structures and query requirements.
By leveraging the Where argument and its various components, your application can perform advanced and nuanced queries while ensuring compatibility and compliance with the schema's defined structures and field types.
OrderBy
Specifies the ordering of results, enabling the retrieval of data in ascending or descending order based on certain field types.
You can find many entries ordered by the age from the greater to the lower:
client.db.findMany({
// Others params...
orderBy: { age: 'DESC' },
});
Skip
Determines the number of records to skip when querying the database, useful in pagination.
You can find many entries but skip the 10 firsts entries like this:
client.db.findMany({
// Others params...
skip: 10,
});
Take
Specifies the maximum number of records to retrieve from a query, facilitating pagination.
You can find 10 first entries like this:
client.db.findMany({
// Others params...
take: 10,
});