Skip to main content
Version: 1.2.4

Read Operations

Read/Get records from the table This page uses the following methods:

  • await presql.findById(<filter>)
  • await presql.findOne(<filter>)
  • await presql.findMany(<filter>)
  • await presql.findFirst(<filter>)
  • await presql.table('').*(<filter>) -Comming Soon

findById()

Create a file at index.js:

index.js
import presql from './connection';
async function FetchData() {
const data = await presql.findById({
table: "users", // records from the table
select: {
firstName:true // use this syntax
},// select fields from the table
id:1,// must be string | number | object id:productIid
// no need to pass where filed, or where is optional
// where:{ id : 1},

})
console.log(data); // if showResults is true, it will print results
}

If id is directly specified then no need to pass where field.sortBy is optional field.
Query Output:

SELECT first_name FROM `users` WHERE `id` ='1';

findOne()

index.js
import presql from './connection';
async function FetchData() {
const data = await presql.findOne({
table: "users",
select: {
firstName:true // use this syntax
},
where:{ email:"[email protected]"},
})
console.log(data); // if showResults is true, it will print results
}

Query Output:

SELECT first_name FROM `users` WHERE `email` ='[email protected]';

findMany()

This function returns a array of objects of all records exist in table.
Incase of multiple records with filter where field can be passed otherewise it is optional

index.js
import presql from './connection';
async function FetchData() {
const data = await presql.findMany({
table: "users",
select: {
firstName:true // use this syntax
},
// where:{ email:"[email protected]"},

})
console.log(data); // if showResults is true, it will print results
}

If Select Field is not specified
Query Output:

SELECT * FROM `users`;

Query Output:

SELECT first_name FROM `users`;

findFirst()

findFirst() will retrive only one record that encountered in table first

index.js
import presql from './connection';
async function FetchData() {
const data = await presql.findFirst({
table: "users",
select: {
firstName:true // use this syntax
},
where:{ email:"[email protected]"},
sortBy: {
id:true,// use this syntax
asc:true
},
})
console.log(data); // if showResults is true, it will print results
}

Query Output:

SELECT first_name FROM `users` WHERE `email` ='[email protected]' ORDER BY id,asc;

Sort Results

To Sort the results , use this syntax

index.js
  sortBy: {
type:"BY ORDER" // "BY GROUP","BY ORDER"
orderType:"ASC" // "ASC" , "DESC"
limit:10,
field:["id"] // id is default , please column name that you want
},//provides a sort option.

Query Output:

SELECT * FROM `users` ORDER BY id, ASC LIMIT 10;

AND in where clause

To Find with multiple conditions to be satisfied.

index.js
    where: {
AND: { email: "this", phone: "en" },
},

Query Output:

SELECT * FROM `users` WHERE `email` ='this' AND `phone` ='en';

OR in where clause

To Find with multiple conditions to be exist.

index.js
   where: {
OR: { name: "English", code: "en" },
},

Query Output:

SELECT * FROM `users` WHERE `name` ='English' AND `code` ='en';