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

pouchorm-auth

v1.0.0

Published

Authentication methods for PouchORM collections, using the PouchDB Server Auth module.

Downloads

1

Readme

PouchORM-Auth

A plugin for PouchORM - the definitive ORM for working with PouchDB.

This plugin makes it easy to use a PouchORM collection as an authentication backend. This leverages the great PouchDB-Auth plugin written for PouchDB Server.

To install

npm i pouchorm-auth

or if you prefer yarn: yarn add pouchorm-auth

Make sure to also install pouchorm as a dependency of your project using npm or yarn.

How to Use

Consider this definition of a collection which uses the built-in exported model PouchUser.

// Person.ts

    import { PouchCollection, PouchORM } from 'pouchorm';
    import { PouchUser, createPouchAuthCollection } from 'pouchorm-auth'

    PouchORM.LOGGING = true; // enable diagnostic logging if desired

    export class UserCollection extends PouchCollection<PouchUser> {
    }
    

Now that we have defined our Collection for that model, here is how we instantiate authentication collections.


    // instantiate a collection by giving it the dbname it should use
    let userCollection = new UserCollection('usersdb');

    // Transform the collection.
    userCollection = createPouchAuthCollection<PouchUser>(userCollection)

    export userCollection

From this point:

  • We have our definitions
  • We have our collection instances

We are ready to start authenticating!

    import { PouchUser } from 'pouchorm-auth'
    import { userCollection } from '...'

    // Using collections
    let somePerson: PouchUser = await userCollection.signUp(
        '[email protected]',
        'Darmok and Jalad at Tanagra',
        {
            age: 24,
            email: '[email protected]'
        }
    )

    somePerson = await personCollection.logIn('[email protected]', 'Darmok and Jalad at Tanagra');
    
    // somePerson has been persisted and will now also have some metafields like _id, _rev, etc.

    // Some data to be updated, which would need an upsert merge deltaFunction
    // so that the password is not lost from the existing record
    somePerson.age = 45;
    somePerson = await personCollection.upsert(somePerson, (existing) => { ...existing, ...somePerson });

    // changes to somePerson has been persisted. _rev would have also changed.

    const result: PouchUser[] = await personCollection.find({age: 45})
    
    // result.length === 1

PouchCollection instance API reference

Consider that T is the provided type or class definition of your model. It is recommended that User models extend from class PouchUser to retain special logic designed to work with Pouch and Couch.

Constructor

createPouchAuthCollection<T>(collection: PouchCollection<T>)

Methods dynamically added to collection

These methods were added to provide collection-specific functionality

  • userId(username: string) => string
  • checkAuthInit() => Promise<void>
  • useAsAuthCollection() => Promise<void>
  • stopUsingAsAuthCollection() => void

These methods are wrappers for functionality provided by pouchdb-auth; see their documentation for specifics

  • signUp(username: string, password: string, options?: PouchAuth.SignUpOptions) => Promise<T>
  • logIn(username: string, password: string) => Promise<T> | Promise<void>
  • logOut() => Promise<PouchAuth.LoginResponse>
  • session() => Promise<PouchAuth.SessionResponse>

Supporting the Project

PRs are welcome. NOTE: Tests required for new PR acceptance. Those are easy to make as well.

Contributors

  • Aaron Huggins