Skip to content

HTTPS clone URL

Subversion checkout URL

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