2.0.1 • Published 1 month ago

fundering v2.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
1 month ago

Description

Fundering acts as a ground layer of your service architecture and helps you keep your application structured by offering helper methods and reactive hooks. Rather than reinventing the wheel, the framework uses Mongoose ORM for communication with MongoDB and extends upon the functionality that is already there. A few examples of these functionalities are; dynamic population, recursive authorization, query type casting and service based document middleware.

Installation

$ npm install fundering mongoose

Getting started

Initializing a service is as easy as extending the abstract CrudService class and passing your schema model in the constructor. This class registers your service with fundering for cross-service authorization and casting, and offers a set of methods which help you with basic CRUD actions.

import { CrudService } from 'fundering';
import { IUser } from './user.interface';
import { model } from 'mongoose';
import { userSchema } from './user.schema';

export class UsersService extends CrudService<IUser> {
  constructor() {
    super(model('User', userSchema));
  }
}

Examples

Querying

MongoDB aggregations are great, powerful and fast, but they can also be complex, hard to maintain and (really) slow. The find methods in the CrudService help you keep the positive set of adjectives by utilizing common aggregations for you. The features vary from adding referenced documents to your conditions to randomly sorting the collection before selecting results. The IQueryOptions object also allows you to extend existing methods with extra query conditions for a more dynamic codebase.

class UsersService extends CrudService<IUser> {
  constructor() {
    super(model('User', userSchema));
  }

  getPopulatedGroups() {
    // get all users with their group fields populated
    return this.find({}, { populate: ['group'] });
  }

  getByGroupName(name: string) {
    // find users based on the name of their referenced group
    // sort the users on the createdAt date of their group descending
    return this.find({ 'group.name': name }, { sort: ['-group.createdAt'] });
  }

  getFiveUniqueNames(options?: IQueryOptions) {
    // find 5 randomly sorted unique first names while supporting extending the query
    return this.find({}, { ...options, distinct: 'firstName', random: true, limit: 5 });
  }
}

Authorization

Authorization plays a huge part in most production applications and implementing it properly can come at the cost of readability and/or performance. You can implement the IOnAuthorization interface to utilize the onAuthorization() method in your CrudService. This method is triggered on every find query called through the CrudService and allows you to return a MongoDB expression object to which the returned documents must comply.

Note: you can imbue the options object with user data to create rules based on context. This is elaborated upon in the wiki.

class UsersService extends CrudService<IUser> implements IOnAuthorization {
  constructor() {
    super(model('User', userSchema));
  }

  async onAuthorization(options: IAuthOptions): Promise<Expression> {
    // limit the user's access to just his/her own account
    return { $eq: ['$_id', options.user?._id] };
  }
}

Document middleware

Fundering offers opt-ins for document middleware through service methods. By moving these methods to the service level you are able to keep your logic more centralized and make use of patterns like dependency injection more easily. Fundering also extends the functionality by allowing context injection and keeping all arguments neatly typed.

class UsersService extends CrudService<IUser> implements IPreSave {
  constructor() {
    super(model('User', userSchema));
  }

  async preSave(payload: IUserModel, options?: IQueryOptions<IUser>) {
    // encrypt modified passwords
    if(payload.isModified('password')) {
        payload.password = encrypt(payload.password)
    }
  }
}

Documentation

You can consult the wiki for more detailed information about the following subjects:

Using Fundering with Nestjs?

Make sure you check out the nest-utilities package for flexible endpoints and a great developer experience for front-enders and back-enders alike.


Made by Martin Drost - Buy me a ☕

2.0.1

1 month ago

2.0.0

1 month ago

1.4.8

4 months ago

1.4.6

9 months ago

1.4.7

9 months ago

1.4.5

11 months ago

1.4.4

11 months ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.9

2 years ago

1.3.8

2 years ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.2.12

2 years ago

1.2.11

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.10

3 years ago

1.2.9

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.0

3 years ago

1.2.1

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.0-alpha.41

3 years ago

0.0.0-alpha.40

3 years ago

0.0.0-alpha.39

3 years ago

0.0.0-alpha.38

3 years ago

0.0.0-alpha.37

3 years ago

0.0.0-alpha.36

3 years ago

0.0.0-alpha.35

3 years ago

0.0.0-alpha.34

4 years ago

0.0.0-alpha.33

4 years ago

0.0.0-alpha.31

4 years ago

0.0.0-alpha.30

4 years ago

0.0.0-alpha.29

4 years ago

0.0.0-alpha.28

4 years ago

0.0.0-alpha.27

4 years ago

0.0.0-alpha.26

4 years ago

0.0.0-alpha.25

4 years ago

0.0.0-alpha.24

4 years ago

0.0.0-alpha.23

4 years ago

0.0.0-alpha.22

4 years ago

0.0.0-alpha.21

4 years ago

0.0.0-alpha.20

4 years ago

0.0.0-alpha.19

4 years ago

0.0.0-alpha.18

4 years ago

0.0.0-alpha.17

4 years ago

0.0.0-alpha.16

4 years ago

0.0.0-alpha.15

4 years ago

0.0.0-alpha.14

4 years ago

0.0.0-alpha.13

4 years ago

0.0.0-alpha.12

4 years ago

0.0.0-alpha.11

4 years ago

0.0.0-alpha.10

4 years ago

0.0.0-alpha.9

4 years ago

0.0.0-alpha.8

4 years ago

0.0.0-alpha.7

4 years ago

0.0.0-alpha.6

4 years ago

0.0.0-alpha.3

4 years ago

0.0.0-alpha.4

4 years ago

0.0.0-alpha.2

4 years ago

0.0.0-alpha.5

4 years ago

0.0.0-alpha.1

4 years ago

0.0.0-alpha.0

4 years ago