-
Notifications
You must be signed in to change notification settings - Fork 0
Object
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.
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");
}
}
const u: User = new User();
u.props.name = "Elijah";
u.props.age = 20;
await u.create();
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");
await u.create();
await u.update();
await u.fetch();
await u.delete();
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!! :) ")
}
}
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