-
Notifications
You must be signed in to change notification settings - Fork 0
Example
Elijah Cobb edited this page Apr 20, 2020
·
1 revision
Below is an example of a simple silicon setup you can make. If you have any other requests for example documentation feel free to make an issue and label it as "documentation".
In the example below, a user is being two users are created and a message is created from "Madison" to "Elijah". Then, a birthday occurs on the user "Elijah". As you can see, once you lay the ground work with making your own classes, it is extremely easy to create anything you might need.
import {SiDatabase, SiObject} from "@elijahjcobb/silicon";
interface UserProps { name: string; age: number; }
class User extends SiObject<UserProps> {
public constructor() { super("user"); }
public async triggerBirthday(): Promise<void> {
if (this.props.age) this.props.age++; await this.update("age");
}
}
interface MessageProps { content: string; fromId: string; toId: string}
class Message extends SiObject<MessageProps> {
public constructor(user?: User) {
super("message");
this.props.fromId = user?.getId();
}
public async send(recipient: User, content: string): Promise<void> {
this.props.toId = recipient.getId();
this.props.content = content;
await this.create();
}
}
(async (): Promise<void> => {
await SiDatabase.init({
address: "mongodb://localhost:27017",
database: "silicon",
verbose: false
});
const user: User = new User();
user.props.name = "Elijah";
user.props.age = 20;
await user.create();
const user2: User = new User();
user2.props.name = "Madison";
user2.props.age = 21;
await user2.create();
const message: Message = new Message(user2);
await message.send(user, "Happy birthday!");
await user.triggerBirthday();
})()
.then((): void => SiDatabase.close())
.catch((err: any): void => console.error(err));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