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

grand-connectors

v1.1.6

Published

A Grandjs package for applying repository pattern for nodejs and javascript applications using typescript

Downloads

22

Readme

Grand Connectors

A Grandjs Package helper for repository pattern and clean archeticutre for your nodejs application using typescript

Content

About

Grand Connectors is a typescript package for building server side or frontend application based on clean design archeticutre and repository pattern, it's basically depends on two main functionalities, the first one is Repository and the second thing is DataSource, it also uses some decorators to enable you inject your datasource into your repositories and inject your repositories inside your controllers

Features

  • Dependency injection
  • Repository Pattern
  • Isolated models, repositories, datasources controllers
  • For Typescript!

Prerequisites

  • install nodejs on your machine
  • install typescript
  • initialize a new typescript project

Dependencies

Grand Connectors uses another package called grand-model, this package enables you to build models an scheme your data easily, it enables you inject your entities and models inside your repository, please take a look at how to use it for building schema and modeling your data

Installing

npm i grand-connectors

Usage

grand-connectors exposes to you a set of functions and classes that you can use to build your repository and datasources, firstly import the following:

import {Repository, DataSource, InjectDataSource, InjectModel} from "grand-connectors";

Grand Connectors Classes

Grand Connectors exposes to you two classes, these classes as the following:

DataSource

This is an abstracted class that you can extend to build your own data source, datasource in grandjs is basically considered as a host for your database type, so if you are using mongoose, it will be the where that your data base exist and connect in.

Extend DataSource

To Extend the datasource you need to import from grand-connectors

import {DataSource} from "grand-connectors";

class MyDataSource extends DataSource{
}

DataSource Type

In extending the data source, you need to set one property called type this property specify the type of your data source it can be one of the following:

  • mongoose
  • sequelize

Example

import {DataSource} from "grand-connectors";

class MyDataSource extends DataSource{
    type = "mongoose"
}

DataSource Method

The second thing you need to define is a method called connect, this method will be called when you run the project to connect your database either it's a mongodb or sql or any database you prefer, also you can assign any hooks you want inside this function

Example


import {DataSource} from "grand-connectors";
import mongoose from "mongoose";
class MyDataSource extends DataSource{
    type = "mongoose"
    connect() {
        mongoose.connect("mongodb://localhost:27017/testDb").then(() => {
            console.log("connected");
        }).catch(err => {
            console.log(err);
        })
    }
}

Repository

After Extending your DataSource, you need now to extend your repository.

Example

import {Repository} from "grand-connectors";

class UserRepository extends Repository{
}

InjectDataSource

You need now to inject the datasource that you have created inside the repository, this can be done via a decorator called InjectDataSource

Example


import {DataSource, Repository, InjectDataSource} 
from "grand-connectors";

import mongoose from "mongoose";

class MyDataSource extends DataSource{
    type = "mongoose"
    
    connect() {
        
        mongoose.connect("mongodb://localhost:27017/testDb").then(() => {
            console.log("connected");
        
        }).catch(err => {
            console.log(err);
        
        })
    }
}

class UserRepository extends Repository{
InjectDataSource(MyDataSource)
MyDataSource: MyDataSource
}

As you can see above, the datasource is injected inside the repository

InjectModel

This is a second decorator that grand-connectors uses to inject the model you have created inside the repository

Create New Entity

To create a new entity, you need to import Entoty class from grand-model package

import {Entity, property, settings, method} from "grand-model"

class UserEntity extends Entity{
    @property({required: true, type: "string"})
    email:string
    @property({required: true, type: String})
    name
    @method
    getName() {
        return this.name;
    }
}

Create New Model

Now you need to create a new model using mongoose

The mongoose model can uses the grand-model entity to load your schema definition inside mongoose schema

import mongoose, {Document, Schema, Model} from "mongoose";
import {loadClass} from "grand-connectos";
import {Entity, property, settings, method} from "grand-model"

class UserEntity extends Entity{
    @property({required: true, type: "string"})
    email:string
    @property({required: true, type: String})
    name:string
    @method
    getName() {
        return this.name;
    }
}
interface IUser extends UserEntity, Document{}

const UserSchema = new mongoose.Schema(<any>UserEntity.prototype.Schema)

loadClass(UserSchema, UserEntity);

const User:Model<IUser> = mongoose.model<IUser>("User", UserSchema);

Now you need to inject the model you have created inside your repository

Example

import {Repository, DataSource, InjectDataSource, InjectModel} from "grand-connectors";
class UserRepository extends Repository{
    @InjectDataSource(MyDataSource)
    MyDataSource: MyDataSource
    @InjectModel({Entity:UserENtity, Mode: User, DataSource: "MyDataSource"})
}
InjectModel Properties

InjectModel is a decorator you use to inject a new model intor your repository, this decorator takes one parameter which is an object, this object has the following properties:

|property|type|required|description| |-|-|-|-| |Entity|Entity|false|This is the Entity Class that you have created| |Model|Model|false|this is a mongoose model or any model that you want to inject inside your repository| |DataSource|string|true|the DataSource name that you have injected inside the repository before|

InjectService

This decorator is exposed if you want to inject any class inside another class as a service, for example if you are using grandjs which is the core framework for building your routes, you can inject the repository you have created inside the grandjs Router as the following:

import {InjectService} from "grand-connectors";
import {Router} from "grandjs";

@InjectService("repositories", UserRepository)
class UserController{
    repositories: {
        UserRepository:UserRepository
    }
    constructor() {}
    createUser() {
        this.repositories.UserRepository.create();
    }
}

class UserRouter extends Router{
    services: {
        UserController:UserController
    }
    constructor(props) {
        super(props)
        console.log();
    }
    @GET({url: "/"})
    getUserPage() {
        this.services.UserController.createUser();
        return this.res.status(200).json({status: 200,message: "user page"})
    }
}

InjectService Parameters

This decorator uses the following parameters

|name|type|required|description| |-|-|-|-| |store|object|true|the store object that is exist inside the class that you want to add the service in as a property or it can be this which refers to the class itself| |Service|Service|true|The Service class that you want to inject|