- Documentation
- Quickstart
- Introduction
- Importing thinky
- Schemas
- Relations
- Virtuals
- Feeds
- FAQ
- API
-
- Model
- - getTableName
- - define
- - defineStatic
- - ensureIndex
- - hasOne
- - belongsTo
- - hasMany
- - hasAndBelongsToMany
- - save
- - pre
- - post
- - Query's methods
- - EventEmitter's methods
- - EventEmitter's methods
for documents
- Document
- - getModel
- - merge
- - validate
- - validateAll
- - save
- - saveAll
- - getOldValue
- - isSaved
- - setSaved
- - delete
- - deleteAll
- - purge
- - addRelation
- - removeRelation
- - getFeed
- - closeFeed
- - EventEmitter's methods
- Query
- - getJoin
- - addRelation
- - removeRelation
- - run
- - execute
- - ReQL methods
- - Overwritten ReQL methods
Importing thinky
Introduction
This document suggests how to import and use the thinky
module. This article
just gives hint on how to architect your application. You are free to try
another architecture if it works better for your needs.
Architecture
- We recommend users to create a file
util/thinky.js
with the following content:
// file: util/thinky.js
var thinky = require('thinky')({
// thinky's options
})
module.exports = thinky;
- Then for each model, create a
.js
file in amodels
directory. For example to declare a modelUser
:
// file: models/user.js
var thinky = require(__dirname+'/util/thinky.js');
var type = thinky.type;
var User = thinky.createModel("User", {
id: type.string(),
name: type.string(),
age: type.number()
});
module.exports = User;
If you need to create a relation, add it after calling createModel
.
Suppose that a User
has one Account
. You would have two files:
// file: models/account.js
var thinky = require(__dirname+'/util/thinky.js');
var type = thinky.type;
var Account = thinky.createModel("Account", {
id: type.string(),
sold: type.number(),
userId: type.string()
});
module.exports = Account;
var User = require(__dirname+'/models/user.js');
Account.belongsTo(User, "user", "userId", "id");
// file: models/user.js
var thinky = require(__dirname+'/util/thinky.js');
var type = thinky.type;
var User = thinky.createModel("User", {
id: type.string(),
name: type.string(),
age: type.number()
});
module.exports = User;
var Account = require(__dirname+'/models/account.js');
User.hasOne(Account, "user", "id", "userId");
- Then you can import the model you need anywhere you need.
If you find yourselves importing all your models in multiple files, you can create a
file models/all.js
with the following content:
// file: models/all.js
module.exports = {
Account: require(__dirname+'/models/account.js');
User: require(__dirname+'/models/user.js');
};
Explanations
The reason behind the architecture suggested above comes from the fact that when you execute
the module thinky
, you create a new instance of thinky
.
You should not import and call the module mulitple times as it will result in multiple
instances of thinky
that will not share the same models.