Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
grand-connectors
Advanced tools
A Grandjs package for applying repository pattern for nodejs and javascript applications using typescript
A Grandjs Package helper for repository pattern and clean archeticutre for your nodejs application using typescript
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
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
npm i grand-connectors
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 exposes to you two classes, these classes as the following:
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.
To Extend the datasource you need to import from grand-connectors
import {DataSource} from "grand-connectors";
class MyDataSource extends DataSource{
}
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:
Example
import {DataSource} from "grand-connectors";
class MyDataSource extends DataSource{
type = "mongoose"
}
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);
})
}
}
After Extending your DataSource, you need now to extend your repository.
Example
import {Repository} from "grand-connectors";
class UserRepository extends Repository{
}
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
This is a second decorator that grand-connectors
uses to inject the model you have created inside the repository
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;
}
}
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 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 |
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 |
FAQs
A Grandjs package for applying repository pattern for nodejs and javascript applications using typescript
The npm package grand-connectors receives a total of 1 weekly downloads. As such, grand-connectors popularity was classified as not popular.
We found that grand-connectors demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.