Skip to content

Instantly share code, notes, and snippets.

@endel
Last active February 17, 2016 20:31
Show Gist options
  • Save endel/36c0d82a02cc2e16ecd1 to your computer and use it in GitHub Desktop.
Save endel/36c0d82a02cc2e16ecd1 to your computer and use it in GitHub Desktop.
hook: Example configuration using `belongs_to` / `has_many` relationships and eager-loading. http://github.com/doubleleft/hook
# hook-ext/seeds/authors.yaml
# Generated through command `hook generate:seed authors`
#
# Seed for authors
#
truncate: true
data:
- _id: 1
name: Author one
- _id: 2
name: Author two
# hook-ext/seeds/books.yaml
# Generated through command `hook generate:seed books`
#
# Seed for books
#
truncate: true
data:
- _id: 1
author_id: 1
name: Book one
genre: programming
- _id: 2
author_id: 1
name: Book two
genre: programming
client: javascript> hook.collection('authors').join('books').then()
┌─────┬──────────────┬──────────────────────────────────────────────────────────┐
│ _id │ name │ books │
├─────┼──────────────┼──────────────────────────────────────────────────────────┤
│ '1' │ 'Author one' │ [ Object { │
│ │ │ _id: '1', │
│ │ │ created_at: Wed Feb 17 2016 20:19:48 GMT+0000 (GMT), │
│ │ │ updated_at: Wed Feb 17 2016 20:19:48 GMT+0000 (GMT), │
│ │ │ name: 'Book one', │
│ │ │ isbn: '', │
│ │ │ genre: 'programming', │
│ │ │ author_id: '1' }, │
│ │ │ Object { │
│ │ │ _id: '2', │
│ │ │ created_at: Wed Feb 17 2016 20:19:48 GMT+0000 (GMT), │
│ │ │ updated_at: Wed Feb 17 2016 20:19:48 GMT+0000 (GMT), │
│ │ │ name: 'Book two', │
│ │ │ isbn: '', │
│ │ │ genre: 'programming', │
│ │ │ author_id: '1' } ] │
├─────┼──────────────┼──────────────────────────────────────────────────────────┤
│ '2' │ 'Author two' │ [] │
└─────┴──────────────┴──────────────────────────────────────────────────────────┘
# hook-ext/schema.yaml
#
# 'books' schema
#
books:
lock_attributes: true
attributes:
- name: name
type: string
- name: isbn
type: string
unique: true
- name: genre
type: string
relationships:
belongs_to: authors
#
# 'authors' schema
#
authors:
attributes:
- name: name
type: string
relationships:
has_many: books
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment