Created
June 20, 2011 05:44
-
-
Save chrisdickinson/1035178 to your computer and use it in GitHub Desktop.
ormnomnom rewrite.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{models, Author, Book} = require './models' | |
{connection} = require 'ormnomnom' | |
conn = connection.Connection.get_connection 'default' | |
models.db_creation 'default', yes, -> | |
create_book = Book.objects.create | |
author:Author.objects.get_or_create {first_name:'Cormac', last_name:'McCarthy', slug:'cormac-mccarthy'} | |
title:'Blood Meridian' | |
slug:'blood-meridian' | |
desc:'A book about violence and cowboys.' | |
# `create_book` is an EventEmitter AND a function. | |
create_book.on 'data', (book)-> | |
console.log "Created #{book} with id of #{book.pk}" | |
# likewise, so is `book.author()`. | |
book.author() (err, author)-> | |
console.log "using #{author} with id of #{author.pk}" | |
# you can join across foreign keys in filters | |
books = Book.objects.filter | |
author__first_name:'Cormac' | |
# for each book returned, log its name. | |
books.each (book)-> | |
console.log "Got #{book}, #{book.pk}" | |
# we're done here. | |
books conn.close.bind conn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('coffee-script'); | |
require('./example'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{models} = require 'ormnomnom' | |
models.configure 'default', | |
backend:'ormnomnom/lib/backends/postgres' | |
name:'testdb' | |
exports.models = | |
models.namespace 'authors', (ns)-> | |
Author = ns.create 'Author' | |
Author.schema | |
first_name:models.CharField {max_length:255} | |
last_name:models.CharField {max_length:255} | |
slug:models.CharField {max_length:255} | |
Author.meta | |
order_by:['last_name', 'first_name'] | |
Author::toString =-> | |
"<Author: \"#{@get_full_name()}\">" | |
Author::get_full_name = -> | |
[@first_name, @last_name].join ' ' | |
Book = ns.create 'Book' | |
Book.schema | |
author: models.ForeignKey Author | |
title: models.CharField {max_length:255} | |
slug: models.CharField {max_length:255} | |
desc: models.TextField | |
Book::toString =-> | |
"<Book: \"#{@title}\">" | |
exports.Author = Author | |
exports.Book = Book |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node index.js | |
Created <Book: "Blood Meridian"> with id of 6 | |
using <Author: "Cormac McCarthy"> with id of 1 | |
Got <Book: "Blood Meridian">, 1 | |
Got <Book: "Blood Meridian">, 2 | |
Got <Book: "Blood Meridian">, 3 | |
Got <Book: "Blood Meridian">, 4 | |
Got <Book: "Blood Meridian">, 5 | |
Got <Book: "Blood Meridian">, 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment