GithubHelp home page GithubHelp logo

base's People

Contributors

jlenon7 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

tamyrescso

base's Issues

Add PrismaRepository

Add PrismaRepository to repositories base:

Example:

import { User } from 'app/Models/User'
import { PrismaRepository } from '@secjs/base/repositories/PrismaRepository'

class UserRepository extends PrismaRepository<User> {
  protected wheres: ['id', 'name'] // What wheres can be executed by client.
  protected relations: ['contacts'] // What relations can be get by client.
  
  // Both, wheres and relations will only work for external requests.

  protected Model = User // Give the Model value to Lucid, so he knows what to work with.

  // You can subscribe PrismaRepository methods in here if you want!
}

Add casts to NULL, IS NOT NULL, IN and BETWEEN to MongooseRepository

Example:

Object.keys(where).forEach(key => {
    let value = where[key];

    if (value === 'null') value = null
    if (value === '!null') value = { $ne: null }

    if (Array.isArray(value)) {
        value = { $in: value }
    }

    if (value.includes('->')) {
        const firstValue = value.split('->')[0].replace(/\s/g, '')
        const secondValue = value.split('->')[1].replace(/\s/g, '')

        value = { $gte: firstValue, $lte: secondValue }
    }

    query.where(key, value);
});

Add updateOneToMany method to MongooseRepository only

In MongoDB sometimes you want to create a simple relationship between two schemas. It's very easy when you want to create OneToMany and populate the side of ManyToOne. The problem is when you want to populate the other side.

Explanation:

class UserSchema {
    products: ProductSchema[]
}

class ProductSchema {
    user: UserSchema
}

UserSchema.populate('products') // Will bring an empty array
ProductSchema.populate('user') // Will bring the user object owner from that product

To solve this empty array updateOneToMany function needs to be implemented and will receive three parameters:

  • The id of the main model that is going to be updated
  • The relationId of the relation that needs to be pushed to the array
  • The relationName inside the main model schema to access the object

Example:

class UserSchema {
    products: ProductSchema[]
}

class ProductSchema {
    user: UserSchema
}

const user = new UserSchema()
const product = new ProductSchema({ user })

await this.userRepository.updateOneToMany(user.id, product.id, 'products')

UserSchema.populate('products') // Will bring an array with all user products
ProductSchema.populate('user') // Will bring the user object owner from that product

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.