Repositories

Repositories are associated with an entity and have several methods to interact with the entity.

Example:

import { orm } from 'lambdaorm'
import { ProductRepository } from './models/northwind'

(async () => {
    await orm.init()
    const productRepository = new ProductRepository('mydb')
    const country = 'USA'
    const result = await productRepository.query().filter(p => (p.price > 5 && p.supplier.country === country) || (p.inStock < 3))
            .having(p => max(p.price) > 50)
            .map(p => ({ category: p.category.name, largestPrice: max(p.price) }))
            .sort(p => desc(p.largestPrice))
            .execute({ country: country })

    console.log(JSON.stringify(result, null, 2))
    await orm.end()
})()
method Description
insert To insert one record
bulkInsert To insert records
update To update one record
updateAll to be able to update all the records of an entity
delete To delete one record
deleteAll delete all records of an entity
get get one record
first returns the first record
last returns the last record
take returns one record
query permite realizar una consulta personalizada

The repositories are generated by lambdaorm by executing the update command.

When executing this command, among other tasks, a repository is created for each entity of the defined schemas.

These files are only generated the first time, so it is safe to add custom methods to these repositories.

Example:

import { Repository, IOrm } from 'lambdaorm'
import { Product, QryProduct } from './model'

export class ProductRepository extends Repository<Product, QryProduct> {
    constructor (database?: string, Orm?:IOrm) {
        super('Products', database, Orm)
    }
    // Add your methods here
}