npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reshuffle-wix-connector

v0.1.3

Published

Reshuffle WIX database connector

Downloads

38

Readme

reshuffle-wix-connector

Code | npm | Code sample

npm install reshuffle-wix-connector

Reshuffle Wix Connector

This package contains a Reshuffle connector to connect an external database collection for Wix as described in Wix External Collections.

The following example exposes an endpoint to return the number of items in the external collection. It uses a postgres database configured with a Reshuffle pgsql connector

const { Reshuffle } = require('reshuffle')
const { WixConnector } = require('@reshuffle/wix-connector')
const { PgsqlConnector } = require('reshuffle-pgsql-connector')

const app = new Reshuffle()

// The collection (or table) name
const COLLECTION = 'tasks'

const pg = new PgsqlConnector(app, {
  url: process.env.WIX_DB_URL
})

const wix = new WixConnector(app, {
  secret: process.env.RESHUFFLE_WIX_SECRET,
})

wix.on({ action: 'data/count' }, async (event, app) => {
  const { collectionName, filter } = event.request.body
  if (collectionName === COLLECTION) {
    const todos = await pg.query(`SELECT * from ${COLLECTION}`)
    event.response.status(200).json({ totalCount: todos.rowCount })
  } else {
    event.response.status(400).json({ 'message': 'Bad request. We only have todos' })
  }
})

app.start()

Table of Contents

Configuration Options

Events

Actions

Utility Functions

Configuration options

const app = new Reshuffle()
const wix = new WixConnector(app, {
  secret: process.env.RESHUFFLE_WIX_SECRET,
  webhookPath: process.env.RESHUFFLE_WIX_WEBHOOK,
})

Both secret and webhookPath are optional.

The secret is a string containing a shared secret as described in Wix Authentication

When configuring the external database collection on Wix, you can define a settings object that is sent from Wix to the connector with every request.

To use (the optional) secret, configure the settings object to include:

{
  "secret": ".....your secret....."
}

Wix then includes it in the incoming request like so:

{
    "requestContext": {
        "settings": {
            "secret": ".....your secret....."
        },
        "instanceId": "...",
        "installationId": "...",
        "memberId": "...",
        "role": "..."
  }
}

If you do not define a secret in the connector configuration, then it will not expect one.

You can use the webhookPath to configure the url that Wix hits when it makes its calls to your external database collection. The value of webhookPath will be appended to your runtime's base url. Left unprovided, webhookPath defaults to /webhooks/wix, so if your runtime runs at https://example.com/ then your full webhook path is https://example.com/webhooks/wix. This is the path you'll need to register with Wix when defining a new external collection. See Wix External Datbase instructions.

Events

When Wix makes a call to the external collection, the connector captures these calls. It then triggers the corresponding event.

To listen to events coming from Wix, you'll need to capture them with the connector's on function, providing a WixEventConfiguration to it.

interface WixConnectorEventOptions {
  action: WixAction // See below
}

// Where...
type WixAction =
  'provision'
  | 'schemas/find'
  | 'schemas/list'
  | 'data/get'
  | 'data/count'
  | 'data/find'
  | 'data/insert'
  | 'data/update'

The connector triggers events of the following type:

interface WixEvent {
  requestContext: WixRequestContext
  collectionName?: string
  filter?: string
  sort?: any
  skip?: number
  limit?: number
  itemId?: string
  item?: any
  body?: any
  action: WixAction
  request: Request // The http request from Wix
  response: Response // The http response object 
}

interface WixRequestContext {
  settings: Record<string, any>
  instanceId: string
  installationId: string
  memberId: string
  role: string
}

The description of all fields and events can be found here

Actions

This connector provides no actions.

Utility Functions

The Wix connector provides some utility functions to help converting data between Wix and a database. To use these functions, import them from the WixConnector package:

const { parseFilter, wrapDates, unwrapDates } = require('@reshuffle/wix-connector')

parseFilter

Used to convert a filter object from Wix See here to a PostgreSQL WHERE statement.

unwrapDates

Used to convert a date-structure coming in from Wix to a flat structure on the item object. For example:

// This is an item coming in from Wix:
const item = {
    "_id": "12345678-abcd-9876-fedc-a9876543210",
    "_owner": "77aa88bb-2c2c-d3d3-4e4e-ff55aa66bb77",
    "make": "BMW",
    "model": "i8",
    "year": 2020,
    "date_added": {
        "$date": "2020-01-01T21:00:00.000Z"
    }
}

const unwrapped = unwrapDates(item)
console.log(unwrapped)
//
{
    "_id": "12345678-abcd-9876-fedc-a9876543210",
    "_owner": "77aa88bb-2c2c-d3d3-4e4e-ff55aa66bb77",
    "make": "BMW",
    "model": "i8",
    "year": 2020,
    "date_added": "2020-01-01T21:00:00.000Z"
}

wrapDates

Used to convert a date-containing flat structure of an item object to the date-structure Wix is expecting.

For example:

// This is an item coming in from Wix:
const item = {
    "_id": "12345678-abcd-9876-fedc-a9876543210",
    "_owner": "77aa88bb-2c2c-d3d3-4e4e-ff55aa66bb77",
    "make": "BMW",
    "model": "i8",
    "year": 2020,
    "date_added": "2020-01-01T21:00:00.000Z"
}

const wrapped = wrapDates(item)
console.log(wrapped)
//
{
    "_id": "12345678-abcd-9876-fedc-a9876543210",
    "_owner": "77aa88bb-2c2c-d3d3-4e4e-ff55aa66bb77",
    "make": "BMW",
    "model": "i8",
    "year": 2020,
    "date_added": {
        "$date": "2020-01-01T21:00:00.000Z"
    }
}