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

Objects in silicon are very easy to create. Create a class like you normally would but make sure to extend SiObject. By extending SiObject you get a lot of great functionality write at your fingertips.

You will notice that SiObject takes a type parameter. All you need to do is provide an interface or type for the properties of your object. For example, if you are making a user, your type might include properties for name and email.

Creating a Class extending SiObject

Create a interface for your properties:

interface UserProps { 
    name: string;
    age: number;
}

Create a class that extends SiObject passing in your interface:

class User extends SiObject<UserProps> {

    public constructor() {
        super("table-name");
    }

}

Creating an Object

const u: User = new User();

u.props.name = "Elijah";
u.props.age = 20;

await u.create();

Updating an Object

Calling update with keys will update the keys specified but if you call update without specifying any keys all keys will be updated.

const u: User = new User();

await u.fetch("id-of-user");
u.props.age ++;
console.log(`User ${u.getId()} is now ${u.props.age} years old.`);
await u.update("age");

Methods

await u.create();
await u.update();
await u.fetch();
await u.delete();

Listeners

Objects can take control of the flow calls to the database with listeners. There are protected methods on SiObject that you can override to listen for things, log, or really anything you want. The methods are below:

protected objectWillEncode(): void {}
protected objectDidEncode(): void {}
protected objectWillDecode(): void {}
protected objectDidDecode(): void {}
protected objectWillUpdate(): void {}
protected objectDidUpdate(): void {}
protected objectWillCreate(): void {}
protected objectDidCreate(): void {}
protected objectWillDelete(): void {}
protected objectDidDelete(): void {}
protected objectWillFetch(): void {}
protected objectDidFetch(): void {}

Example:

class User extends SiObject<UserProps> {

    public constructor() {
        super("table-name");
    }
    
    protected objectDidCreate(): void {
        console.log("CREATED!! :) ")
    }

}

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