Link Search Menu Expand Document

Custom Field

Table of contents

  1. Simple
    1. Example Usage
  2. Extend DB value
    1. Example Usage
  3. Extend Field Value
    1. Example Usage
  4. Create attributes
    1. Method to create field attributes
    2. Example Usage

Create your own custom fields, extend the class from base BaseField or you can extend any existing field also.

Simple

You can create simplest field just by extending base BaseField

Example Usage

const {Model, BaseField} = require("fireo");

class WeekDays extends BaseField{

}

class User extends Model{
    day = new WeekDays();
}

const u = User.init();
u.day = 1;
await u.save()

Extend DB value

Control how the value of field will be save in Firestore. Override method setValue() to change the value.

Example Usage

const {Model, BaseField} = require("fireo");

class WeekDays extends BaseField{
    days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

    setValue(value){
        return this.days[value];
    }
}

const u = User.init();
u.day = 0;
await u.save();

// This will save "Mon" instead of "0" in Firestore
console.log(u.day)  // Mon

Extend Field Value

Control how value represent when coming for Firestore. Override method getDBValue() to control this behavior.

Example Usage

const {Model, BaseField} = require("fireo");

class WeekDays extends BaseField{
    days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

    setValue(value){
        return this.days[value];
    }

    async getDBValue() {
        return this.val + "-mod";
    }
}

const u = User.init();
u.day = 0;
await u.save()

// This will save "Mon" instead of "0" in Firestore
// But when you get value it will return "0" instead of "Mon"

const user = await User.collection.get({key: u.key});
console.log(user.day)  // 0

Create attributes

Default, Required and Name attributes are allowed in every field. But you can create more attributes for your field.

Method to create field attributes

Add filed attribute in the fieldOptions list and then create method for each attribute. Method name must be start from option_ and then the name of the attribute. Method should return the value otherwise undefined value set for field

Attribute Method run in the same order as they are specify in fieldOptions

Example Usage

const {Model, BaseField} = require("fireo");

class EmailGenerator extends BaseField {
    static fieldOptions = ["prefix", "domain"];

    option_prefix({ optionValue, fieldValue }) {
    return optionValue + "." + fieldValue;
    }

    option_domain({ optionValue, fieldValue }) {
    return fieldValue + "@" + optionValue;
    }
}

class User extends Model {
    email = new EmailGenerator({ prefix: "prefix", domain: "example.com" });
}

const user = User.init();
user.email = "my_email";
await user.save();

// This will save email in Firestore like this "prefix.my_email@example.com"
console.log(u.email)  #  prefix.my_email@example.com

Copyright © 2019 FireO All rights reserved. Powered by OctaByte