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

tramway-core-connection

v2.2.0

Published

A simple connection library for the Tramway framework

Downloads

59

Readme

Tramway Connection is a simple connection library for the tramway framework. It includes:

  1. An adaptive Connection system
  2. Repositories and Entities which are separate but when used together with Providers can speed up workflow.
  3. Introduces async/await support to replace callbacks

Installation:

  1. npm install tramway-core-connection

Example project

https://gitlab.com/tramwayjs/tramway-example

Documentation

Recommended Folder Structure

  • config
  • providers
  • entities
  • repositories
  • factories

Config

Here is where you can put all the parameters for your connection.

Providers

Providers are your way to access data sets from a data source, be it a database or API

To create a provider, import the class and implement a derived class with the abstracted stubs to get the most out of it.

import {Provider} from 'tramway-core-connection';

| Function | Usage | | ----- | ----- | | constructor() | Handles database configuration | | getOne(id: any, collection: string) | Returns item from the connection | | getMany(ids: any[], collection: string) | Passes an array of items for an array of ids. | | find(conditions: string/Object, collection: string) | Returns an array of items for a query on specific conditions. This may be done by object or query string depending on your implementation | | has(id: any, collection: string) | Checks if item exists | | hasThese(ids : any[], collection: string) | Checks if a set of items exists | | count(conditions: any, collection: string) | Gets a count of items that meet the conditions. | | create(item: Entity, collection: string): Entity | Creates an object in the database from an Entity or standard object | | update(id: any, item: Entity, collection: string): Entity | Updates the item found with the given id | | delete(id: any, collection: string) | Removes an item from the datastore and returns it | | deleteMany(ids : any[], collection: string) | Removes items from the datastore and returns them | | query(query: string/Object, values: Object, collection: string) | Meant as an override based on your datastore because we can't always rely on simple CRUD | | createCollection(collection: string) | A method encompassing the steps to create a collection programatically |

Repositories

Repositories allow you to interact with a given connection specifically for the purposes of handling a given entity. This usually implies persisting to a database but with the flexibility of Tramway, it can also mean local storage or via an API connection. The default Repository already links to a connection and utilizes the existing methods that were implemented when the Provider was implemented or imported. The following demonstrates how a Repository could be used with a Provider and Entity. It is recommended, however, to instantiate providers and repositories using tramway-core-dependency-injector such to make more efficient use of memory and simplify usage, in this case, no class would be required unless it was overriding default connection logic.

import {Repository} from 'tramway-core-connection';
import TestConnection from '../connections/TestConnection';
import TestEntity from '../entities/TestEntity';
export default class SampleRepository extends Repository {
    constructor(item, factory) {
        super(new TestConnection(), factory);
    }

Note, using dependency injection negates the need for the above code.

Summary of Repository Spec

| Function | Usage | | --- | --- | | constructor(Provider, Factory, collection) | Constructor takes a Provider, Factory, and the name of the collection |

Exposed methods to use

All of these methods rely on the Provider's implementation and will just interact with the Provider.

| Function | Usage | | --- | --- | | exists(id: string/int): boolean | Calls Provider's exist function | | getOne(id: string/int): Entity | Gets entity with id | | get(): Collection | Gets collection of all objects from the entity's set | | create(entity: Entity) | Sends the entity to the provider to be created and returns the persisted result.| | update(entity: Entity) | Updates the entity via the provider and returns the persisted result. | | delete(id: string/int) | Deletes the item with the Model's set id.| | find(condtions: string/Object): Collection | Finds a collection of objects in the entity's set with given conditions | | getMany(ids: any[]): Collection | Gets objects tied to a list of ids | | count(conditions): number | Gets a count of objects for given conditons | | setup() | Handles programatic initialization of the data source |

Factory

The factory facilitates the creation of standardized Entity and Collection objects and is primarily used by the Repository. Based on different use cases, it can be extended to support common formats like HATEAOS and decorate entities.

To create a factory, extend the class.

import {Factory} from 'tramway-core-connection';

| Function | Usage | | --- | --- | | create(item: Object): Entity | Creates entity from object | | createCollection(items: Object[]): Collection | Returns collection of entities from array of objects. |

Entity

An entity is a simple class that contains the getters and setters for its properties. It also comes with validation and serialization.

| Function | Usage | | --- | --- | | constructor(obj: Object) | Converts a passed object into the proper attributes | | hasAttribute(attribute: string): boolean | Checks if attribute is in the Entity | | serialize(): string | Returns stringified JSON | | unserialize(item): Entity | Sets own attributes based on serialized string |

To create an entity, extend the class.

import {Entity} from 'tramway-core-connection';

Collection

A collection stores a group of entities in a Map to facilitate bulk operations and validation.

| Function | Type | Usage | | --- | --- | --- | | from(entities: Entity[]) | static | Creates a collection from an array of entities. let collection = Collection.from(entities) | | add(entity: Entity) | instance | Adds entity to collection at its id as key | | has(id) | instance | Checks if entity with given id exists | | get(id) | instance | Gets entity with given id | | forEach(cb: function(value: Entity, key: number/string, map: Map)) | instance | Iterates collection of entities | | getSize(): number | instance | Returns size of collection | | isEmpty(): boolean | instance | Checks if collection is empty | | getEntities(): Iterator | instance | Gets entities as an Iterator | | getFirst(): Entity | instance | Gets first entity of collection | | getLast(): Entity | instance | Gets last entity of collection | | getIds(): [] | instance | Gets ordered array of ids of the collection |

To create an collection, extend the class.

import {Collection} from 'tramway-core-connection';