Execute PostgreSQL function in Prisma without using $queryRaw - postgresql

I've searched in the documentation of Prisma, but I couldn't find any information on how is possible to execute a PostgreSQL function like "to_char" using the prisma client.
In short, I would like to execute a query in which I order the data by month and day excluding the year, and all this done by the prisma client, without using raw query.
The question would be, if is there any possibility to implement this without using raw query?
While searching on the internet for solution, found this answer in raw query.
Select YourDate
From YourTable
Where YourConditions
order by to_char(YourDate,'MMDD')
I've managed to implement this by using $queryRaw method from prisma:
this.prismaService.$queryRaw(
Prisma.sql`
SELECT *
FROM User
WHERE to_char(birthdate, 'MMDD')>=to_char(NOW(),'MMDD')
ORDER BY to_char(birthdate,'MMDD')`
)
I was hoping to have some kind of function/operation available by Prisma to be able to make the query somehow in this way:
this.prismaService.user.findMany({
where: { birthdate: to_char(new Date(), 'MMDD') },
orderBy: { birthdate: { gte: { to_char(birthdate, 'MMDD') } } },
})
Appreciate any information!

Related

String queries Spring Data MongoDB

Is there a way with Spring Data MongoDB to execute string queries directly on DB without knowing the query that will be executed?
Example:
I want to execute this query:
db.users.find({ 'name': 'John' })
And imagine that this query is not always the same, and maybe it can change to:
db.clients.find({ 'company': 'XYZ' })
I want to know if it's possible to execute those queries directly on DB with the MongoTemplate, or possibly build a query from the string and then executing it.

How to sample a PostgreSQL database using Prisma?

Suppose I have several millions of statements in my PostgreSQL database and I want to get only 10000 of them. But not the first 10000, rather, a random selection of 10000 (it would be best if I could also choose the logic, e.g. select every 4th statement).
How could I do this using Prisma, or — if it's not possible using Prisma — using a good old PostgreSQL request?
For now, I'm using this code to limit the number of results I'm getting:
const statements = await this.prisma.statement.findMany({
where: {
OR: conditions,
},
orderBy: {
createdAt: 'asc',
},
take: 10000,
});
This will use the conditions I have, then order them in ascending order, and "take" or limit the first 10000 results.
What could I use in place of the "take" or what request I could make directly in PostgreSQL to randomly sample my DB for records?
Prisma doesn't natively support fetching random data as of now.
There is a Feature Request that discusses the exact same scenario as you need.
The alternative could be to use queryRaw for raw database access and use PostgreSQL's random function as described in the above mentioned Feature Request.

Does Mongo DB have functions similar to Oracle triggers

Dose mongo has some functions similar to oracle trigger? When i insert a document it can automatic update the fields createdTimestamp and modifiedTimestamp.
Excample:
After inserting/updating data:
{
'name': 'bobo',
'age': 17
}
final data will be updated as below. It means trigger dose time fields updating for me.
{
'name': 'bobo',
'age': 17,
'createdTimestamp': 2020-09-17T09:31:14.416+00:00,
'modifiedTimestamp': 2020-09-17T09:31:14.440+00:00
}
My solution is to use $currentDate and $setOnInsert, to update set upsert=true, code as below:
created_modified_timestamp_operation = {
'$currentDate': {
modifiedTimestamp: true
},
'$setOnInsert': {
createdTimestamp: new Date()
}
}
But for this solution I need to modify a lot of data operations,so I want to know is there any functions is similar to Oracle trigger, i just need to write a trigger to monitor whether the database is modified.
Thanks~~
Well triggers are only available in MongoDB atlas. But you can create triggers if you are using something like mongoose since it supports creating pre/post save, update triggers which I believe though are application level but can be of great help. And yes there are Change Streams you can read about them as well.

mongoDB spring-boot custom query in uppercase, with LIKE

I need to search the items by their names via spring boot on a db implemented by mongodb. For a normal SQL database I would've done as follows:
#Query("SELECT * FROM customer WHERE UPPER(name) LIKE UPPER(CONCAT('%', :name, '%'))")
List<Customer> findByName(#Param("name") String name);
Yet it does not accept a normal 'SELECT' query, because it is a NoSQL database. Thus I need to query this collection by using JSON format, yet I have no idea of the implementation on spring-boot.. This is my first time using mongodb too.
Going through the posts on stackoverflow, I have found an example such as:
#Query("{'name':?0}")
And on this site there are examples. Also here there is an explanation of the query above. Yet still I have no idea how to convert the former query I have pasted above into a "json based query".
Note: I am extending MongoRepository on my repository.
Note2: The above query is case sensitive, that's the reason I am trying to query in upper case along with converting the column data into upper case.
UPDATE
#Query(" { $text: { $search: ?0 , $caseSensitive: false } }")
works perfectly(I had to index my collection by launching this command:db.customerCollection.createIndex({name: "text"}) ) to ignore case sensitivity, but I still need to implement 'LIKE'; because when I search 'user', it is able to find 'UsEr' etc. But if I search 'se', it doesn't bring me any result. Any help will be appreciated!
Thank you for your time!
I have resolved my issue by using $regex and writing the query as:
#Query(" { companyName: { $regex : '(?i)?0'} } }")
I would've never guessed to spend that much time for such a simple thing. But I am glad that it is resolved anyway !
[ref]

FeathersJS REST Multiple Query String

With the FeathersJs REST client - how can I query a single field with multiple values?
Eg. if I have a Books service and I want to retrieve all books written in the year 1990, 1991 and 1992. I'd assume I'd call:
/books?year[]=1990&year[]=1991&year[]=1992
Although this doesn't work.
Have a look at the documentation for the common database adapter querying syntax. The correct way to query for what you are looking for is:
year: {
$in: [ 1990, 1991, 1992 ]
}
The corresponding query would be
/books?year[$in]=1990&year[$in]=1991&year[$in]=1992
Parsing this query string requires using the extend query parser qs which you can enable with
app.set('query parser', 'extended');
Since the query is a string, if you need the actual number values for the year you might also have to convert it the query in a before hook (although most database usually allow both).