Collections

Collections are arrays of Models, and to be more precise, they contain Models of a specific single class.

For example a Todos collection consisting of only Todo models.

Create a class

import { createCollection } from 'tydel';

const Todos = createCollection(Todo); // passing the Todo model class

Create an instance

// empty instance
const todos = new Todos();

// instance with some models data
const todos = new Todos([
  { title: 'First task' }, // will be converted to Todo instances
  { title: 'Second task'}
]);

// instance with some model instances
const todos = new Todos([
  new Todo({ title: 'First task' }),
  new Todo({ title: 'Second task' })
]);

Methods

You can make new methods available to your collection instances as follows:

const Todos = createCollection(Todo, {
  doSomething() {
    return true;
  }
});

Collection instances also come with built-in methods like map, filter, reduce just like Array. See more in API Reference.

Initializers

createCollection() accepts a third argument, where you can pass an array of functions accepting the collection instance, allowing you to perform some actions when the collection class is first instantiated.

const Todos = createCollection(Todo, {}, [
  function myInitializer(collection) {
    // collection is the `Todos` instance here
  }
]);