Memberstack & Databases

Article author
Memberstack Team

If your app is complex or you need to create relational records for your members, consider using a database/data store.

You can use:

  • SQL
  • NoSQL
  • Redis
  • and a variety of BaaS solutions like Firebase, Supabase, Appwrite, and more.

When it comes to logic for generating new records based on signups in Memberstack, the approach is essentially the same across the board. You’ll want to use:

  1. Webhooks, Zapier or Make to send payloads of member data to external services.
  2. A resource that can “catch” and process the webhook’s data - a Zapier / Make action, or an API endpoint (you can use serverless functions too.)
  3. If you’re not using Zapier or Make actions, you’ll need an Object Relational Mapping (ORM) or SDK to help you retrieve or move data into your database. If using MongoDB or SQL databases, Prisma is an excellent tool. If using Firebase, Supabase, etc., you can use their SDKs directly.

ℹ️ Always use the Memberstack ID as a primary key or unique identifier when storing members and creating relations in a database. IDs never change throughout the lifetime of a user. Emails can change over time which can lead to unexpected results like duplicated user records or broken connections.

Prisma / Express Example

The following code snippets demonstrate using Prisma with a simple express REST API boilerplate to catch webhook payloads from Memberstack and create new members in your database.

// lib/prisma.js

const { PrismaClient } = require("@prisma/client");

let prisma: PrismaClient;

if (process.env.NODE_ENV === "production") {
	prisma = new PrismaClient();
} else {
	// to improve performance with multiple connections in development servers
	if (!global.prisma) {
		global.prisma = new PrismaClient();
	}
	prisma = global.prisma;
}

export default prisma;
// server.js
const express = require('express');
const app = express();
const prisma = require("./lib/prisma");
const PORT = 8000

async function createNewMemberRecord(payload){
	let user await prisma.user.create({
	  data: { id: payload.id, email: payload.auth.email },
	})
return user
}
app.use(express.static('static'));

// ... other endpoints here

app.post('/member-created-hook', (req, res) => {
	let { payload, timestamp, event } = req.body
	if(event !== 'member.created') {
			let message = "Only member.created webhook payloads allowed"
			return res.status(400).json({ message })
		}
	const user = await createNewMemberRecord(payload)
  res.status(200).send('ok')
});

app.listen(port, () => {
  console.log(`Server listening at <http://localhost>:${port} 🚀`);
});

Serverless / Airtable Example

This in-depth guide will teach you how to use Memberstack webhooks to send new member data to a serverless function every time a member signs up and write that data to an Airtable record.

We’re using Napkin.io as a simple, serverless solution to process webhook payloads. If you’re working with tools like Vercel / Next.js, Netlify or AWS Lambda, you can follow the tutorial and use those tools in place of Napkin.io.


No Code Solution (coming soon)


Other Ways to Connect Memberstack to a 3rd Party DB

Was this article helpful?

Comments

1 comment

  • Comment author
    MJ Petroni

    Hi there! I think there is a link missing for the Airtable/Napkin example. 

     

     

    0

Please sign in to leave a comment.