1.15.1 • Published 5 years ago

webiny-model v1.15.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

npm.io npm.io 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()
1.15.1

5 years ago

1.15.0

5 years ago

1.14.2

5 years ago

1.14.1

5 years ago

1.14.0

5 years ago

1.13.1

5 years ago

1.13.0

5 years ago

1.12.7

5 years ago

1.12.6

5 years ago

1.12.5

5 years ago

1.12.4

5 years ago

1.12.3

5 years ago

1.12.2

5 years ago

1.12.1

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.5

5 years ago

1.10.4

5 years ago

1.10.3

5 years ago

1.10.2

5 years ago

1.10.1

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago