Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@json-api/knex-adapter

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@json-api/knex-adapter

Knex adapter for the json-api library

  • 1.0.0-beta.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

@json-api/knex-adapter

Serve resources from your SQL database using json-api@3.

Installation

npm i --save json-api@latest @json-api/knex-adapter

How to Use

// index.js
const { ResourceTypeRegistry, ResourceController } = require('json-api');
const KnexAdapter = require('@json-api/knex-adapter');
const express = require('express');
const knex = require('knex');
const models = require('./models');

const connection = knex(config);
const dbAdapter = new Adapter(models, connection);

const defaults: ResourceTypeDescription = { dbAdapter, urlTemplates };
const registry = new ResourceTypeRegistry(resourceTypes, defaults);
const controller = new APIController(registry);

// This library doesn't yet support reflection of model data for documentation, but json-api requires
// that we provide a DocumentationController regardless. The solution for now is to provide a
// fake controller with an empty ResourceTypeRegistery.
const docsController = new DocumentationController(new ResourceTypeRegistry({}), { name: 'My API' });

const Front = new ExpressStrategy(controller, docsController, { host: 'http://localhost:3000' });
const handler = Front.apiRequest;

const app = express();

app.get('/:type', handler);

app.listen(3000);
// models.js
module.exports = {
  posts: {
    table: 'post',
    idKey: '_id',
    attrs: [ 'title', 'date' ],
    relationships: [
      // Direct many-to-one relationship, with the foreign key stored in this resource's row.
      {
        type: 'authors',
        key: 'author'
      },

      // Linked many-to-many relationship, with the foreign keys stored in a linking table.
      {
        type: 'tags',
        key: 'tags',
        via: {
          table: 'post_tag',
          fk: 'post',
          pk: 'tag'
        }
      },

      // Linked one-to-many relationship, with the foreign keys stored in a normal table.
      {
        type: 'readers',
        key: 'favouriteOf',
        via: {
          table: 'reader',
          fk: 'favouritePost',
          pk: '_id'
        }
      }
    ]
  }
}

FAQs

Package last updated on 30 Jun 2020

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