
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
mithril-postgrest
Advanced tools
What this library is supposed to do:
First we should init the library so that it will build the functions to access the API. The init function takes one argument which is the API endpoint prefix, containing the URI to which all addresses will be appended. If the API is being served from exactly the same location as the page running the JS you can just initialize without any argument.
To use an API available at http://api.foo.com/v1 you can use the code:
postgrest.init(
"http://api.foo.com/v1",
{method: "GET", url: "/authentication_endpoint"}
);
This will create three functions:
Both request functions are just proxies for mithril's m.request and will return in the same fashion.
However, the postgrest.requestWithToken stores the JWT for api authentication in a public getter: postgrest.token.
This token can be manipulated to implement session storage for the authentication token.
To generate a model you should call the model function passing its name. The name of the model should be the name of the endpoint in the PostgREST server.
For example, the following code:
var users = postgrest.model('users');
will generate an object with functions to operate the /users endpoint.
To get one record from the users table you could use:
var users = postgrest.model('users');
users.getRow({id: 'eq.1'}).then(function(row){
console.log('fecthed:', row);
});
Now if you don't want to fetch data as anonymous, and prefer to use your authentication token:
var users = postgrest.model('users');
users.getRowWithToken({id: 'eq.1'}).then(function(row){
console.log('fecthed:', row);
});
When updating, the model always fetches the updated record by default:
var users = postgrest.model('users');
users.patchWithToken({id: 'eq.1'}, {name: 'new name'}).then(function(row){
console.log('updated:', row);
});
The model will have the following property once it is created:
getPage call. Default is 10.There are some commom View-Model objects that can be generated automaticaly.
One of such cases is the filters VM. It is used to bind a HTML form to a set of getter/setter functions that will be used to generate a query string. You can use the function:
As in the example:
var filters = postgrest.filtersVM({id: 'eq'});
var users = postgrest.model('users');
users.getPage(filters.id(7).parameters()).then(function(data){
console.log('fetched:', data);
});
The filters.parameters() will return an object that can be fed directly to a request with filters and the order by.
If you want to apply any transformation to the value before it being fed to the parameters() function you have a toFilter function
that has been created in each property for you. So let's say we want to remove diacriticts from the name before sending the string:
filters.name.toFilter = function(){
return removeDiacritics(this());
};
Assuming that you have a removeDiacritics function defined within the scope of the above code, once you call the parameters() this function
will be applied to the property.
Another View-Model very convenient is one that can paginate a model and fetch pages from the API. To create such an object you can call:
var paginator = paginationVM(model, order, authenticate)
This would generate a pagination View-Model that loads pages using the given model, applying the order to the request. The third parameter is a boolean to choose between a getPage (passing false) or getPageWithToken (passing true, this is the default).
This can be used with the model and filters defined above like:
var userPages = postgrest.paginationVM(users);
// The firstPage function returns a mithril promise
userPages.firstPage(filters.parameters()).then(function(){
// Results are in collection
console.log(userPages.collection());
},
function(){
alert('Error loading users');
});
After the first call to .firstPage the parameters are stored for use in next pages. To change the filters you need to call .firstPage again.
// The nextPage function returns a mithril promise
userPages.nextPage().then(function(){
// Results are appended to collection
console.log(userPages.collection());
},
function(){
alert('Error loading next page');
});
FAQs
A Mithril.js plugin to authenticate requests against PostgREST
We found that mithril-postgrest demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.