Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

objection-utils

1.0.6 • Public • Published

objection-utils

Utils and helper classes for Objection.js ORM

Repositories

Repositories are wrappers around specific DB connection and Objectino.js Model that expose a number of methods to perform CRUD operations: NOTE: repositories in objection-utils are deprecated, use dedicated "objection-repositories" library instead.

**Create new entity without persisting

  /**
   * @param {Object} [attributeValues] - attributes of a new entity
   * @returns {Object} entity instance of a repository Model
   */
  fromJson(attributeValues) {

**Persist new entity or an array of entities non-recursively (without relations). Note that batch insert only works on PostgreSQL

  /**
   * @param {Object} entity - model instance or parameters for a new entity
   * @param {Object} [trx] - knex transaction instance. If not specified, new implicit transaction will be used.
   * @returns {Promise<Object>} - created entity
   */
  create(instance, trx) {

**Persist updated entity. If previously set fields are not present, performs an incremental update (does not remove fields unless explicitly set to null)

  /**
   * @param {Object} entity - single entity instance
   * @param {Object} [trx] - knex transaction instance. If not specified, new implicit transaction will be used.
   * @returns {integer} number of affected rows
   */
  update(entity, trx) {

**Find list of entities with specified attributes

  /**
   * @param {Object} attributeValues - values to filter retrieved entities by
   * @param {string || string[]} [withRelations] - name of relation(s) to eagerly retrieve, as defined in model relationMappings()
   * @returns {Promise<Object[]>} - search result
   */
  find(attributeValues = {}, withRelations) {

**Find first entity with specified attributes

  /**
   * @param {Object} attributeValues - values to filter retrieved entities by
   * @param {string || string[]} [withRelations] - name of relation(s) to eagerly retrieve, as defined in model relationMappings()
   * @returns {Promise<Object>} 
   */
  findOne(attributeValues = {}, withRelations) {

Recommended repository initialization and exposal pattern (JSDoc is used to assist with autocompletion):

const { repositoryFactory } = require('objection-utils');
const knex = require('../db/application.db').getKnexInstance(); //replace with how you provide knex in your application
const SomeEntityModel = require('../models/some-entity.model');
const AnotherEntityModel = require('../models/some-entity.model');
 
const someEntityRepository = repositoryFactory.getRepository(knex, SomeEntityModel);
const anotherEntityRepository = repositoryFactory.getRepository(knex, AnotherEntityModel);
 
module.exports = {
    /**
     * @type EntityRepository
     */
    someEntityRepository,
 
    /**
     * @type EntityRepository
     */
    anotherEntityRepository
};
 

install

npm i objection-utils

Downloadsweekly downloads

1

version

1.0.6

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability