naokia / commodo

Commodo is a set of higher order functions that let you define and compose rich data models.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Commodo - composable model objects

Prettier license SemVer lerna Commitizen friendly Gitter

Commodo is a set of higher order functions that let you define and compose rich data model objects.

Quick example

// Import needed higher order functions.
import { withFields, string, number, boolean, object, onSet } from "@commodo/fields";
import { withName } from "@commodo/name";
import { withHooks } from "@commodo/hooks";
import { withStorage } from "@commodo/fields-storage";
import { MongoDbDriver, withId } from "@commodo/fields-storage-mongodb";
import { compose } from "ramda";

// Define User and Verification models.
const Verification = compose(
  withFields({
    verified: boolean(),
    verifiedOn: string()
  })
)();

const User = compose(
  withFields({
    firstName: string(),
    lastName: string(),
    email: compose(
      onSet(value => value.toLowerCase())
    )(string()),
    age: number(),
    scores: number({ list: true }),
    enabled: boolean({ value: false }),
    verification: fields({ instanceOf: Verification })
  }),
  withHooks({
    async beforeCreate() {
      if (await User.count({ query: { email: this.email } })) {
        throw Error("User with same e-mail already exists.");
      }
    }
  }),
  withName("User"), // Utilized by storage layer, to determine collection / table name.
  withId(),
  withStorage({
    driver: new MongoDbDriver({ database })
  })
)();

const user = new User();
user.populate({
  firstName: "Adrian",
  lastName: "Smith",
  email: "aDrIan@google.com",
  enabled: true,
  scores: [34, 66, 99],
  verification: {
    verified: true,
    verifiedOn: "2019-01-01"
  }
});

await user.save();

Core packages:

Package Short Description Version
@commodo/fields The starting point of every model. Provides base string, number, boolean and model fields.
@commodo/name Assign a name to your models.
@commodo/hooks Provides methods for defining and triggering hooks on your models.
@commodo/fields-storage Enables saving models to a database (with an appropriate driver, e.g. MySQL).

Additional packages:

Package Short Description Version
@commodo/fields-storage-ref Provides ref field, for saving references to other models saved in database.
@commodo/fields-storage-mongodb A MongoDB driver for @commodo/fields-storage package.
@commodo/fields-storage-soft-delete Introduces deleted boolean field to mark whether a model was deleted or not, instead of physically deleting the entry in the storage.

Community packages:

Package Short Description Version
commodo-fields-date Provides date field, for saving dates.
commodo-fields-object Provides object field, for saving plain objects.
commodo-fields-storage-crud-logs Adds and automatically manages createdOn, updatedOn, savedOn fields.

Contributing

Please see our Contributing Guideline which explains repo organization, linting, testing, and other steps.

License

This project is licensed under the terms of the MIT license.

Contributors

Thanks goes to these wonderful people (emoji key):


Adrian Smijulj

💻 📖 💡 👀 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

About

Commodo is a set of higher order functions that let you define and compose rich data models.

License:MIT License


Languages

Language:JavaScript 100.0%