-
Notifications
You must be signed in to change notification settings - Fork 0
Query
Querying with silicon uses the MongoDB package. However, there is a type-checking layer on top that takes the types from your object's property interface. A few helper methods are provided but you can make a query as complicated or simple as you wish. Some examples are provided below.
To create a query instance you need to provide the class and props for the corresponding class. So from the User
class example from the Object page you would provide the User and the first generic parameter and UserProps as
the second generic parameter.
In the actual constructor, pass the class. It will be treated as a factory so that objects generated from the query will have the correct structure. The second parameter in the constructor is the query itself.
const query: SiQuery<User, UserProps> = new SiQuery<User, UserProps>(User, {});The query below would return all users whose name is "Sarah" and are between the aged 21 and 49.
new SiQuery<User, UserProps>(User, {
age: { $lt: 50, $gte: 21 },
name: "Sarah"
});const users: User[] = await query.getAll();
const firstUser: User = await query.getFirst();const amountOfUsers: number = await query.count();
const usersExistForQuery: boolean = await query.exists();Since silicon is built on top of mongodb's query system, view mongodb's query documentation.
Created by Elijah Cobb provided under the MIT license.
Element is a group of developer tools I have built in TypeScript. Check them out at: element-ts.com.
Author: Elijah Cobb
NPM: @element-ts/silicon
Issue Reporting: Github Issues