npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

daybed.js

v0.1.6

Published

Daybed Javascript Developer Toolkit

Downloads

6

Readme

daybed.js

A library to use Daybed in your Javascript applications.

NPM Version Build Status

Usage

In the browser, just include the source file:

  <script src="//spiral-project.github.io/daybed.js/build/daybed.js"></script>

In node, just require the package:

  var Daybed = require('daybed.js');

Rationale

Note that Daybed is a REST backend, and does not require any particular library to be used in your applications.

daybed.js brings some helpers to ease the manipulation of sessions and asynchronous operations.

We heavily take advantage of Promises.

Example

Start a session

First, we start a new session on Daybed, and keep the token as if it was an admin password.


    var server = 'https://daybed.lolnet.org';

    var sessionPromise = Daybed.startSession(server)
      .then(function (session) {
        console.log("Keep this somewhere safe " + session.token);
        return session;
      });

Later, you will be able to re-use admin token you obtained :


    var sessionPromise = Daybed.startSession({ token: 'q89ryh..dcc' })
      .then(function (session) {
        console.log("Authenticated!");
        return session;
      })
      .catch(function (error) {
        console.log("Failed!");
      });

Note: The sessionPromise object is not a Session object.

You can build a Session object yourself:


    var sessionObject = new Daybed.Session(server, { token: 'q89ryh..dcc' });

Or get it out of the promise:


    var sessionObject;
    sessionPromise
      .then(function (session) {
          sessionObject = session;
      });

Setup

As the main developper of your app, it's common to start by defining a model on the Daybed server.

Let's say you want to build an app to list the books you would like to read.


    var books = {
      definition: {
        title: 'book',
        description: "The list of books to read",
        fields: [{
          name: "title",
          type: "string",
          label: "Title",
        },
        {
          name: "author",
          type: "string",
          label: "Author"
        },
        {
          name: "summary",
          type: "string",
          label: "Summary"
        }]
      }
    };

    var modelId = 'myapp:books';

    sessionPromise
      .then(function (session) {
        return session.saveModel(modelId, books);
      })
      .then(function (created) {
        console.log('Model created', created);
      })
      .catch(function (error) {
        console.error('An error occured', error);
      });

As a model owner, you can adjust the permissions. For example, you can decide that everyone can create, read, edit and delete their own records :


    sessionPromise
      .then(function (session) {
        return session.savePermissions(modelId, {
          'Everyone': ['create_record', 'read_own_records', 'delete_own_records',
                       'read_definition']
      })
      .then(function (permissions) {
        console.log('Permissions set', permissions)
      });

You're done !

You can now implement your application, and let your users manage their records !

In your application

Let's say, we want each user to store her session token in the local storage. The first time, a new token will be created :


    var token = localStorage.getItem('myapp:books:token');
    var sessionPromise = Daybed.startSession(server, { token: token })
      .then(function (session) {
        localStorage.setItem('myapp:books:token', session.token);
        return session;
      });

Fetch the list of records :


    var modelId = 'myapp:books';

    sessionPromise
      .then(function (session) {
        return session.getRecords(modelIds)
      })
      .then(function (records) {
        console.log(records.length + ' record(s).');
      });

Create new records :


    var book = {
      title: "Critique de la raison numérique",
      author: "Dominique Mazuet, Delga",
      summary: "http://www.librairie-quilombo.org/spip.php?article5532"
    };

    sessionPromise
      .then(function (session) {
        return session.saveRecord(modelId, book)
      })
      .catch(function (error) {
         console.error('An error occured', error);
      });

A word about tokens

Tokens are your authentication credentials, you can share yours, by using URL location hash for example (instead of localStorage) :


    var token = window.location.hash.slice(1);
    Daybed.startSession({ token: token })
      .then(function (session) {
        window.location.hash = session.token;
      });

This way, each user can share her identity on several devices, and even share her own privileges and collaborate with the entire world!

You can also create new ones and assign them to specific permissions:


    var sessionObject;
    sessionPromise
      .then(function (session) {
        sessionObject = session;
        return Daybed.getToken();
      })
      .then(function(infos) {
        console.log(infos.token + ' will be allowed to delete any record');

        var permissions = {};
        permissions[infos.credentials.id] = ['delete_all_records'];
        sessionObject.savePermissions(modelId, permissions);
      });

See also

Check out backbone-daybed, which brings Backbone.js helpers with jQuery instead of daybed.js.

References