GithubHelp home page GithubHelp logo

ardentzeal / waterline-schema Goto Github PK

View Code? Open in Web Editor NEW

This project forked from balderdashy/waterline-schema

0.0 1.0 0.0 213 KB

This is the core schema builder used in the Waterline ORM.

License: MIT License

JavaScript 100.00%

waterline-schema's Introduction

Waterline Schema

This is the core schema builder used in the Waterline ORM. It is responsible for taking an attributes object from a Collection and turning it into a fully fledged schema object.

It's mainly tasked with figuring out and expanding associations between Collections.

Schema Format

A Waterline schema is a javascript object that maps to a generalized database schema format. An adapter should be able to take it and build out a schema definition including join tables in a relational database.

Belongs To

Belongs to relationships are defined by adding a property to a collection's attributes with a model property that points to another collection.

attributes: {
  user: { model: 'user' }
}

Should create the following after being run through the schema.

attributes: {
  user: {
    columnName: 'user_id',
    type: 'integer',
    foreignKey: true,
    references: 'user',
    on: 'id'
  }
}

Has Many

Has many relationships are defined by adding a property to a collection's attributes with a collection property that points to another collection. This isn't used for the actual database structure in a relational system but could be helpful in a nosql database. It is also used internally inside of Waterline. A via key is required to point to a foriegn key.

attributes: {
  users: {
    collection: 'user',
    via: 'foo'
  }
}

Should create the following after being run through the schema.

attributes: {
  users: {
    collection: 'user',
    references: 'user',
    on: 'user_id',
    via: 'foo'
  }
}

Many To Many

Many To Many relationships are defined by adding a collection property on two Collections that point to each other. This will create an additional collection in the schema that maps out the relationship between the two. It will rewrite the foreign keys on the two collections to reference the new join collections. A via key is required on both so that the relationships can be properly mapped.

// Foo Collection
attributes: {
  bars: {
    collection: 'bar',
    via: 'foos'
  }
}

// Bar Collection
attributes: {
  foos: {
    collection: 'foo',
    via: 'bars'
  }
}

Should create the following after being run through the schema.

// Foo Collection
attributes: {
  id: {
    type: 'integer',
    autoIncrement: true,
    primaryKey: true,
    unique: true
  },
  bars: {
    collection: 'bar_foos__foo_bars',
    references: 'bar_foos__foo_bars',
    on: 'foo__bars',
    via: 'bar_foos'
  }
}

// Bar Collection
attributes: {
  id: {
    type: 'integer',
    autoIncrement: true,
    primaryKey: true,
    unique: true
  },
  foos: {
    collection: 'bar_foos__foo_bars',
    references: 'bar_foos__foo_bars',
    on: 'bar_foos',
    via: 'foo_bars'
  }
}

// bar_foos__foo_bars Collection
attributes: {
  bar_foos: {
    columnName: 'bar_foos',
    type: 'integer',
    foreignKey: true,
    references: 'bar',
    on: 'id',
    groupKey: 'bar'
  },
  foo_bars: {
    columnName: 'foo_bars',
    type: 'integer',
    foreignKey: true,
    references: 'foo',
    on: 'id',
    groupKey: 'foo'
  }
}

waterline-schema's People

Contributors

particlebanana avatar ardentzeal avatar mikermcneil avatar

Watchers

James Cloos 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.