Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 140 lines (101 sloc) 4.046 kb
66f813c @1602 README.md
authored
1 ## About
2
c7ee4b7 @1602 README.md
authored
3 JugglingDB -- cross-db ORM, providing **common interface** to access most popular database formats. Currently supported: mysql, mongodb, redis, neo4j and js-memory-storage (yep, self-written engine for test-usage only). You can add your favorite database adapter, checkout one of existing adapters to learn how, it's super-easy, I guarantee.
66f813c @1602 README.md
authored
4
5 ## Installation
6
7 git clone git://github.com/1602/jugglingdb.git
8
9 ## Usage
10
11 ```javascript
c7ee4b7 @1602 README.md
authored
12 var Schema = require('./jugglingdb').Schema;
66f813c @1602 README.md
authored
13 var s = new Schema('redis');
14 // define models
15 var Post = schema.define('Post', {
16 title: { type: String, length: 255 },
17 content: { type: Schema.Text },
18 date: { type: Date, detault: Date.now },
19 published: { type: Boolean, default: false }
20 });
098fa98 Edited README.md via GitHub
Anatoliy Chakkaev authored
21 // simplier way to describe model
66f813c @1602 README.md
authored
22 var User = schema.define('User', {
23 name: String,
098fa98 Edited README.md via GitHub
Anatoliy Chakkaev authored
24 bio: Schema.Text,
66f813c @1602 README.md
authored
25 approved: Boolean,
26 joinedAt: Date,
27 age: Number
28 });
29
30 // setup relationships
31 User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
32 // creates instance methods:
33 // user.posts(conds)
34 // user.buildPost(data) // like new Post({userId: user.id});
35 // user.createPost(data) // build and save
36
37 Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
38 // creates instance methods:
39 // post.author(callback) -- getter when called with function
40 // post.author() -- sync getter when called without params
41 // post.author(user) -- setter when called with object
42
43 s.automigrate(); // required only for mysql NOTE: it will drop User and Post tables
44
45 // work with models:
46 var user = new User;
47 user.save(function (err) {
48 var post = user.buildPost({title: 'Hello world'});
49 post.save(console.log);
50 });
51
52 // Common API methods
53
54 // just instantiate model
55 new Post
56 // save model (of course async)
57 Post.create(cb);
58 // all posts
59 Post.all(cb)
60 // all posts by user
61 Post.all({userId: user.id});
62 // the same as prev
63 user.posts(cb)
64 // same as new Post({userId: user.id});
65 user.buildPost
66 // save as Post.create({userId: user.id}, cb);
67 user.createPost(cb)
68 // find instance by id
69 User.find(1, cb)
70 // count instances
71 User.count(cb)
72 // destroy instance
73 user.destroy(cb);
74 // destroy all instances
75 User.destroyAll(cb);
76 ```
77
78 Read the tests for usage examples: ./test/common_test.js
79
80 ## Your own database adapter
81
82 To use custom adapter, pass it package name as first argument to Schema constructor:
83
84 mySchema = new Schema('couch-db-adapter', {host:.., port:...});
85
86 Make sure, your adapter can be required (just put it into ./node_modules):
87
88 require('couch-db-adapter');
89
90 ## Running tests
91
92 All tests written using nodeunit:
93
94 nodeunit test/common_test.js
95
96 If you run this line, of course it will fall, because it requres different databases to be up and running, but you can use js-memory-engine out of box! Specify ONLY env var:
97
98 ONLY=memory nodeunit test/common_test.js
99
100 of course, if you have redis running, you can run
101
102 ONLY=redis nodeunit test/common_test.js
103
104 ## Package structure
105
106 Now all common logic desribed in ./index.js, and database-specific stuff in ./lib/*.js. It super-tiny, yep?
107
108 ## Project status
109
110 This project written in one weekend (1,2 oct 2011), and of course it not pretend to be preduction-ready, but I plan to use this project as default ORM for RailwayJS in nearest future. So, if you familiar with some database engines - please help me to improve adapter for that database.
111
112 For example, I know, mysql implementation sucks now, 'cause I'm not digging too deep into SequelizeJS code, and I think would be better to replace sequelize with something low-level in nearest future, such as `mysql` package from npm.
113
114 ## Contributing
115
116 If you have found a bug please write unit test, and make sure all other tests still pass before pushing code to repo.
117
118 ## Roadmap
119
120 ### Common:
121
098fa98 Edited README.md via GitHub
Anatoliy Chakkaev authored
122 + transparent interface to APIs
66f813c @1602 README.md
authored
123 + validations
124 + -before and -after hooks on save, update, destroy
125 + default values
126 + more relationships stuff
127 + docs
128
129 ### Databases:
130
131 + riak
132 + couchdb
133 + low-level mysql
134 + postgres
135 + sqlite
136
137 ## License
138
139 MIT
Something went wrong with that request. Please try again.