catwalk.js

2.0.0 • Public • Published

Catwalk

Travis   npm   License MIT

  • npm: npm install catwalk

Catwalk is a CRUD interface that allows you to lazy-load your data. It's written in ES6 and transpiled to ES5 using Google's Traceur module. By using Catwalk you interact with simple CRUD events using promises to either resolve or reject a certain action – there are absolutely no convoluted methods or sequences that materialised in Ember Data.

---

Getting Started

import {collection} from 'catwalk/collection';
import {field, cast} from 'catwalk/field';
import {on, type} from 'catwalk/event';

// Define a collection specifying its fields and their data-types.
const pets = collection('pets', {
    name: field(cast.string()),
    age:  field(cast.integer())
});

// Subscribe to the updating of any collections.
on(type.SUBSCRIBE, store => {
    console.log(`We have ${store.pets.length} pet(s)!`);
});

// Consider the request successful when we create a pet.
on(type.CREATE, ({ model, resolve, reject }) => {
    resolve(model);
});

// Create some pets that need to be either resolved or rejected.
pets.create({ name: 'Miss Kittens', age: 4 });
pets.create({ name: 'Busters', age: 5 });

Event Types

Each operation has a corresponding event — type.CREATE, type.READ, type.UPDATE, type.DELETE — and can be registering using the on function from catwalk/event.

on(type.CREATE, () => // ...);

However there are instances where you'd rather listen for these events on a specific collection, in cases where you wish to specialise rather than abstract. In these instances you can use the for method on each event type.

on(type.CREATE.for(pets), () => // ...);

Note: When a specialised event type has been registered, then its abstract counterpart will not be invoked for that collection. In the above case, the type.CREATE.for(pets) event will be invoked, but type.CREATE for the pets collection will not.

Events can also be unregistered with the off function — passing in the symbol that was initially used to register it.

off(type.CREATE.for(pets));

Transactions (Future)

Note: Transactions are not yet supported, and the example below is how it may look once implemented.

import {atomic} from 'catwalk/transaction';

atomic(async function({ commit, rollback }) {
    await pets.create({ name: 'Miss Kittens', age: 4 });
    await pets.create({ name: 'Busters', age: 5 });
    rollback();
);

Readme

Keywords

none

Package Sidebar

Install

npm i catwalk.js

Weekly Downloads

1

Version

2.0.0

License

MIT

Last publish

Collaborators

  • wildhoney