Socket
Socket
Sign inDemoInstall

objection

Package Overview
Dependencies
Maintainers
2
Versions
200
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

objection

An SQL-friendly ORM for Node.js


Version published
Weekly downloads
114K
decreased by-15.75%
Maintainers
2
Weekly downloads
 
Created

What is objection?

Objection.js is an ORM (Object-Relational Mapper) for Node.js, built on top of the SQL query builder Knex.js. It aims to provide a powerful and flexible way to interact with SQL databases using JavaScript, while maintaining a clean and intuitive API.

What are objection's main functionalities?

Model Definition

Defines a model class that maps to a database table. In this example, the `Person` class represents the `persons` table in the database.

const { Model } = require('objection');

class Person extends Model {
  static get tableName() {
    return 'persons';
  }
}

Querying

Performs a query to fetch all persons older than 30 years. Objection.js provides a fluent API for building SQL queries.

const people = await Person.query().where('age', '>', 30);

Relations

Defines relationships between models. In this example, a `Person` can have many `Animal` pets, establishing a one-to-many relationship.

class Person extends Model {
  static get tableName() {
    return 'persons';
  }

  static get relationMappings() {
    const Animal = require('./Animal');
    return {
      pets: {
        relation: Model.HasManyRelation,
        modelClass: Animal,
        join: {
          from: 'persons.id',
          to: 'animals.ownerId'
        }
      }
    };
  }
}

Eager Loading

Fetches related data in a single query. This example fetches persons along with their pets using eager loading.

const peopleWithPets = await Person.query().withGraphFetched('pets');

Validation

Defines a JSON schema for model validation. This example ensures that `firstName` and `lastName` are required fields and must be strings of a certain length.

class Person extends Model {
  static get jsonSchema() {
    return {
      type: 'object',
      required: ['firstName', 'lastName'],
      properties: {
        id: { type: 'integer' },
        firstName: { type: 'string', minLength: 1, maxLength: 255 },
        lastName: { type: 'string', minLength: 1, maxLength: 255 }
      }
    };
  }
}

Other packages similar to objection

Keywords

FAQs

Package last updated on 11 May 2016

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc