Table Join
Algebric functions like Sum,Min,Max etc are implemented here.
This page uses the following methods:
await presql.join(<filter>)
await presql.hasOne('').{function}
-Comming Soon
id
is the default foreign key then do not use joinOn
field, it will done automatically.
If you have custom foreign key enable by you or you want to use own foreign key only then use joinOn
and define columns name
Incorrect Use if id
is the foreign key
relation: {
joinType: "inner", // inner,left,right,self,full
joinOn: {
fromColumn: "id",
joinColumn: "id",
},
where: { email: "[email protected]" },
sortBy: {
id: true,
asc: true,
},
}
Correct Use if id
is the foreign key
relation: {
joinType: "inner", // inner,left,right,self,full
where: { email: "[email protected]" },
sortBy: {
id: true,
asc: true,
},
},
Correct Use if custom/user foreign key
relation: {
joinType: "inner",
where: { email: "[email protected]" },
joinOn: {
fromColumn: "from_table_foreign_key_column",
joinColumn: "join_with_table_foreign_key_column",
},
sortBy: {
id: true,
asc: true,
},
},
import presql from "./connection";
async function hasOne() {
const data = await presql.join({
table: "cart",
from: "products", // larger table
joinWith: "cart", // smalller table
relation: {
joinType: "inner", // inner,left,right,self,full
joinOn: {
fromColumn: "product_id",
joinColumn: "product_id",
}, //optional field if id is the foreign key, if not then specify columns here
where: { email: "[email protected]" },
sortBy: {
id: true, // use this syntax
asc: true,
},
},
});
console.log(data); // if showResults is true, it will print results
}
Query Preparing Before Join:
SELECT * FROM "from" joinType JOIN joinWith ON joinOn.fromColumn = joinOn.joinColumn WHERE `email` ='[email protected]' ;
Query Preparing After :
SELECT * FROM `products` INNER JOIN `cart` ON products.product_id = cart.product_id WHERE `email` ='[email protected]' ORDER BY id ASC;
table
,from
,joinWith
,relation
, is required parameter ,select,where,sortBy are optional, use them to filter the data as per your own use
Write coloumn name: if table column join by some special characters please use cammelCase instead writing same as it is name
Original Table Name : product_info
Correct Syntax :
productInfo
InCorrect Syntax :product_info
. Definetype
property to use Math Functions.
relation: {
joinType: "", // inner,left,right,self,full
joinOn: {
fromColumn: "product_id",
joinColumn: "product_id",
},
}