Code Monkey home page Code Monkey logo

model's Introduction

travetto: Model

This module provides a clean interface to data model persistence, modification and retrieval. This module builds heavily upon the Schema, which is used for data model validation.

The module can be segmented into three main areas: Model declaration, access/storage, and querying

Declaration

Models are declared via the @Model decorator, which allows the system to know that this is a class that is compatible with the module.

@Model()
class User extends BaseModel {
  name: string;
  age: number;
  contact?: boolean;
}

The User model is now ready to be used with the model services.

Access/Storage

The ModelService is the foundation for all access to the storage layer, and provides a comprehensive set of functionality. The service includes support for modifying individual records, bulk update/insert/delete, partial updates, finding records, and more. This should be the expected set of functionality for storage and retrieval.

class UserManager {
  private service: ModelService;

  async register(user: User) {
    const created = await this.service.save(User, user);
    ... send welcome email ...
    return created;
  }

  async bulkCreate(users: User[]) {
    const res = await this.service.bulkProcess(User, {
      insert: users
    });
    ... notify administrator of completion ...
    return res; 
  }
}

The ModelService itself relies upon a ModelSource which is the driver for the storage layer. Currently the only ModelSource implementations are for mongodb and elasticsearch, with sql support on the roadmap.

During development, ModelSource supports the ability to respond to model changes in real-time, and to modify the underlying storage mechanism. An example of this would be elasticsearch schemas being updated as fields are added or removed from the Model class.

Additionally there is a class ClassModelService that provides a wrapper around ModelService that is tied to a specific Model class. This can be useful if you want to constrain the model access or if you have a high volume of function calls for the same model.

Querying

One of the complexities of abstracting multiple storage mechanisms, is providing a consistent query language. The query language the module uses is a derivation of mongodb's query language, with some restrictions, additions, and caveats. Additionally, given the nature of typescript, all queries are statically typed, and will catch type errors at compile time.

General Fields

  • field : { $eq : T } to determine if a field is equal to a value
  • field : { $ne: T } to determine if a field is not equal to a value
  • field : { $exists : boolean } to determine if a field exists or not
  • field : T to see if the field is equal to whatever value is passed in

General Single Valued Fields

  • field : { $in : T[] } to see if a record's value appears in the array provided to $in
  • field : { $nin: T[] } to see if a record's value does not appear in the array provided to $in

Ordered Fields

  • field : { $lt: T } checks if value is less than
  • field : { $lte: T } checks if value is less than or equal to
  • field : { $gt: T } checks if value is greater than
  • field : { $gte : T } checks if value is greater than or equal to

Array Fields

  • field : { $all: T[]] } checks to see if the records value contains everything within $all

String Fields

  • field : { $regex: RegExp; } checks the field against the regular expression

Geo Point Fields

  • field : { $geoWithin: Point[] } determines if the value is within the bounding region of the points
  • field : { $geoIntersects: Point[] } determines if the value intersects with the bounding region of the points

Groupings

  • { $and: [] } provides a grouping in which all sub clauses are required
  • { $or: [] } provides a grouping in which at least one of the sub clauses is required
  • { $not : {} } negates a clause

A sample query for Users might be:

this.service.getAllByQuery(User, {
  $and: [
    { 
      $not : {
        age : {
          $lt : 35
        }
      }
    },
    {
      contact : {
        $exists: true
      }
    }
  ]
})

This would find all users who are over 35 and that have the contact field specified.

model's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.