Skip to content
Elijah Cobb edited this page Apr 20, 2020 · 1 revision

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.

Creating Query Instance

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, {});

Providing a Query

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"
});

Fetching

const users: User[] = await query.getAll();
const firstUser: User = await query.getFirst();

Counting

const amountOfUsers: number = await query.count();
const usersExistForQuery: boolean = await query.exists();

More Information

Since silicon is built on top of mongodb's query system, view mongodb's query documentation.

Pages

element-ts

Element is a group of developer tools I have built in TypeScript. Check them out at: element-ts.com.

About

Author: Elijah Cobb

NPM: @element-ts/silicon

Issue Reporting: Github Issues

Clone this wiki locally