Requelize
Installation
yarn add requelize joi
joi is a peer dependency to create schemas
Configuration
const requelize = host: 'localhost' db: 'myApp'
Options are passed to rethinkdbdash options
Debugging
requelize uses debug to debug your apps.
You can use the DEBUG
environment variable as follows:
DEBUG=requelize:* yarn start
Model definition
const joi = const User = requelize Userindex'email'
You might also pass a third argument to requelize.model that looks like this: { primaryKey: string }
Primary keys allowed are: guid and integers
Querying
requelize
First you need to sync database with requelize.sync
. To query documents, you can use any rql commands.
User
is a sugar syntax for r.db('...').table('User')
.
Create / Update / Delete
You can instanciate models to create documents
const user = email: 'john.doe@example.com' firstname: 'John' lastname: 'Doe' user
You can update users with subsequent save
calls
useremail = 'john@doe.com' user
You can delete documents with the delete
model method.
user
Relationships
hasOne
User
Arguments are:
- Relation Model name (the first argument of
requelize.model
) - The virutal field on the instance (so that you have
user.role.id
) - The foreign key to use. requelize defaults to
SelfModel_id
hasMany
User
Arguments are:
- Relation Model name (the first argument of
requelize.model
) - The virutal field on the instance (so that you have
user.posts[0].id
) - The foreign key to use. requelize defaults to
SelfModel_id
The foreign key have to be equal to the local key used on belongsTo
method
belongsTo
RolePost
Arguments are:
- Relation Model name (the first argument of
requelize.model
) - The virutal field on the instance (so that you have
role.user.id
orpost.user.id
) - The local key to use (usually:
model_id
ormodelId
orModel_id
). requelize defaults toModel_id
belongsToMany
UserRight
Arguments are:
- Relation Model name (the first argument of
requelize.model
) - The virutal field on the instance (so that you have
right.users[0].id
oruser.rights[0].id
) - An optional join table name. Default is
ModelA_ModelB
with ModelA and ModelB being sorted
embed and saveAll
To query an instance with its relationships, you can use embed
:
User
You may have nested relations with sub object (as in group: { admin: true }
)
To save an instance with its relationships, you can use saveAll
:
userrole = roleAuserrights = rightA rightB user
You can also save an array of ids with hasMany or belongsToMany:
userrole = roleAuserrights = rightAid rightBid user
Note: be careful that user.rights will be repopulated with rightA and rightB, but those objects will not
be the same as the original rightA and rightB.
That means rightA.User_id
is undefined (because saveAll knows nothing about it), but user.rights[0].User_id
will be set.
Tree has the same possibilities that you have in embed
pivot data
If you want to save pivot data (data you store in a belongsToMany relationship), use setPivot
and getPivot
userrights = rightA rightB rightArightB user
The data will be stored in the joint table. It is shared between the user instance and the right instance
Right User
Note: a race condition exists here because of users[0]
and rights[0]
.
A proper way to do it would be to use: rights.find(right => right.id === rightA.id)
Custom joins tables — three-way relationships
You can provide your own model to join tables:
User = requelizeRole = requelizePeriod = requelizeUserRole = requelize // This is important (creates indexes for relation fields)UserRole PeriodUserRole UserRole
To save a three-way document:
user = name: 'John Doe' role = name: 'Admin' period = name: 'Infinity' userRole = userroles = // this is the related document document: role // this is the join document through: userRole // this will be used when saving the join document saveAll: period: true userRoleperiod = period return user
To retrieve a three-way document:
User
Changefeeds
requelize provide a changefeed structure with the library rxjs and Observables.
You are free to use original cursors if you want (by using changes((err, cursor) => { ... })
and not .feed()
)
let feed = User feed
event
is a simple object that contains:
- type (string): 'create', 'update' or 'delete'
- from (Object): the original value (null when event.type === 'create')
- to (Object): the target value (null when event.type === 'delete')
You should unsubscribe when you do not have any use of it by using the custom Observable method close()
:
let feed = User let subscription = feed
Note: in the example above, the subscription is also closed
Hooks
A few hooks are available in requelize: validating
, validated
, saving
, saved
, creating
, created
, updating
and updated
.
User User
Hooks also support promises
User
Parse and validation
If you plan to use queries that do not resolve to a Model instance, but starts from a model, use parse()
:
User
Note: you need to call run()
You can also disable validation when saving a document. Documents are only validated at insert, not at retrieval
usersomeInvalidField = 'foo' user
Virtuals
If you need virtual fields, you can use virtual
method from model:
const User = requelize User let user = firstname: 'John' lastname: 'Doe' assert
Access to requelize
If you need r
for any use (r.row
, etc.) you can find it under requelize
: requelize.r