type User = { id: number; name: string; age: number, city: string, gender: string };
const users: User[] = [
{ id: 1, name: 'Alice', age: 20, city: 'New York', gender: 'female' },
{ id: 2, name: 'Bob', age: 25, city: 'New York' , gender: 'male' },
{ id: 3, name: 'Charlie', age: 30, city: 'Los Angeles', gender: 'female' },
{ id: 4, name: 'David', age: 25, city: 'Los Angeles', gender: 'male' },
{ id: 5, name: 'Ethan', age: 30, city: 'New York' , gender: 'male' },
{ id: 6, name: 'Frank', age: 20, city: 'New York', gender: 'male' },
];
console.log('=======where1');
const filteredUsers = new SQLike(users).where(item => item.age > 25).value();
console.log(filteredUsers);
console.log('=======group1');
const groupedUsers1 = new SQLike(users).groupBy('age').value();
console.log(groupedUsers1);
console.log('=======group2');
const groupedUsers2 = new SQLike(users).groupBy(['age', 'city']).value();
console.log(groupedUsers2);
console.log('=======innerJoin');
const joinUsersAndClass = new SQLike<User>(users)
.innerJoin(new SQLike(classes), (a,b) => a.class_id === b.id)
.merge(([a, b]) => {
return {
...a,
...b,
id: a.id,
class_id: b.id
}
})
.value()
console.log(joinUsersAndClass);
console.log('=======sort1');
const sortedUsers1 = new SQLike<User>(users).orderBy((a, b) => a.age - b.age).value();
console.log(sortedUsers1);
console.log('=======sort2');
const sortedUsers2 = new SQLike<User>(users).orderBy([
(a, b) => a.city.localeCompare(b.city), // First sort by city
(a, b) => a.age - b.age// Then sort by age if city is the same
]).value();
console.log(sortedUsers2);
console.log('=======limit');
const limitedUsers = new SQLike<User>(users).limit(3).value();
console.log(limitedUsers)
- class
- where
- pick
- omit
- sort
- limit
- groupBy
- join
- innerJoin
- outerJoin
- merge
- unit tests