webiny-model

1.15.1 • Public • Published

code style: prettier PRs Welcome

Define rich data models that are part of your business logic. Once a model is defined, you can instantiate and populate them with data. Models can also be validated.

Install

npm install --save webiny-model

Or if you prefer yarn:

yarn add webiny-model

Usage



class Company extends Model {
    constructor() {
        super();
        this.attr("name")
            .char()
            .setValidators("required");
        this.attr("city")
            .char()
            .onGet((value, lowerCase) => {
                if (lowerCase && value) {
                    return value.toLowerCase();
                }
                return value;
            });

        this.attr("image")
            .model(Image)
            .setValidators("required");
    }
}

class User extends Model {
    constructor() {
        super();
        this.attr("firstName")
            .char()
            .setValidators("required");
        this.attr("lastName")
            .char()
            .setValidators("required");
        this.attr("age")
            .integer()
            .onGet((value, operation, number) => {
                if (operation === "add") {
                    return value + Number(number);
                }

                if (operation === "sub") {
                    return value - Number(number);
                }

                return value;
            });
        this.attr("company")
            .model(Company)
            .setValidators("required");
    }
}

const user = new User();
user.populate({
    firstName: "John",
    lastName: "Doe",
    age: 15,
    company: {
        name: "Webiny LTD",
        city: "London",
        image: {
            file: "webiny.jpg",
            size: { width: 12.5, height: 44 },
            visible: false
        }
    }
});

await user.validate()

/webiny-model/

    Package Sidebar

    Install

    npm i webiny-model

    Weekly Downloads

    2

    Version

    1.15.1

    License

    MIT

    Unpacked Size

    181 kB

    Total Files

    66

    Last publish

    Collaborators

    • webiny