Skip to content

Instantly share code, notes, and snippets.

@vmakhaev
Created December 23, 2013 03:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmakhaev/8091292 to your computer and use it in GitHub Desktop.
Save vmakhaev/8091292 to your computer and use it in GitHub Desktop.
Idea of new racer-access api
var racerAccess = require('racer-access');
derby.use(racerAccess);
var store = derby.createStore();
// This is current racer-access api
store.allow('change', 'users', function(docName, changeTo, snapshotData, connectSession) {
return;
});
// This is an idea of new api
// Actions are the same as it is ('change', 'create', etc), but also can be 'read' action
store.allow('change', 'users', {
// load runs in async submit hook and load data to use later
// do we need extra argument here?
load: function(action, collection, docId, session, origin, next) {
asyncGetSomeData(function(data) {
next(null, data);
})
};
// assert runs in sync preValidate hook and actually do the job
// same arguments for all actions and patterns, that would be more flexible
assert: function(action, collection, docId, extra, session, origin) {
if (isProhibited) {
return 'Error';
}
return;
};
});
// If no need for load function, then short form can be used (only assert):
store.allow('change', 'users', function(action, collection, docId, extra, session, origin) {
if (isProhibited) {
return 'Error';
}
return;
});
// Example of extra argument
// Actual extra depends on action type
var extra = {
segments: ['names', '2', 'value'], // segments after docId
value: {}, // new value
loadedData: null, // data form load function
opData: {},
snapshotData: {},
// for list insert
from: 1,
to: 3,
howMany: 1
};
// Example of origin argument
var origin = {
isServer: true,
isIsomorphic: true
};
/**
* Isomorphic code - code that runs on server and on client
* isIsomorphic = true if it`s request from client or if it`s client app router that runs on server
* We need it because model from client app router on server can be serialized to the client with all data
* We can distinguish model from client app router on server like this:
*/
app.on('model', function(model) {
model.socket.stream.isIsomorphic = true;
});
/**
* A little bit more:
* We can use ShareJS doc filters to restrict returned fields of document
* Api here is similar to Mongo projections
*/
store.fields('users', function(collection, docId, session, origin, next) {
if (isAdmin) {
// All fields except password and hash
next({password: false, hash: false});
} else {
// Just name field
next({name: true});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment