Skip to content

Instantly share code, notes, and snippets.

@digitalsadhu
Created January 19, 2017 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalsadhu/034f6617e9e2360774bae4a9c53b46e0 to your computer and use it in GitHub Desktop.
Save digitalsadhu/034f6617e9e2360774bae4a9c53b46e0 to your computer and use it in GitHub Desktop.
Proposal for ORM data access options

3 layers of data access (proposal)

Model methods: (layer 3)

These mirror ember data methods. They are high level and aim to be useable by the API via the url query string etc. They follow jsonapi spec where appliable. They are:

findAll

this.store.findAll('post') // returns all posts

query

this.store.query('post', {
  include: 'comments, author',
  fields: {
    post: 'title',
    comments: 'comment',
    author: 'firstName, lastName'
  },
  sort: '-title',
  page: {number: 1, size: 20},
  filter: {
    title: {$like: '%tips for%'},
    id: {$gt: 10}
  }
}) // returns posts based on various criteria

findRecord

this.store.findRecord('post', 1) // returns a single post by id

queryRecord

this.store.queryRecord('post', options) // returns a single query, otherwise same as .query

Model querying: (layer 2)

This is a single method that allows for building more advanced queries.

This type of querying allows for more advanced scenarios querying accross multiple tables via joins etc while still allowing Ash to help you out by building model objects from the returned data for you.

Eg.

this.store.modelQuery('post')
  .leftJoin('author', 'post.author_id', 'author.id')
  .where('author.first_name', 'Jo')

Raw sql querying: (layer 1)

There will undoubtedly be scenarios that fall outside the scope of full ORM queries or even the more advanced modelQueries. This single method allows for building raw sql queries that will return the raw data for you to handle as you like.

this.store.rawQuery('SELECT * FROM post WHERE title = ?', ['My Post Title'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment