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

pouch-collection

v1.0.2

Published

PouchDB ORM for Typescript. Work with Multiple databases or multiple collections in a single database. Supports all platforms PouchDB supports.

Downloads

2

Readme

pouch-orm

A solid ORM for working with PouchDB.

  • Typescript is a first class citizen.
    • Will work with raw javascript, but you'll be missing out on the cool Typescript dev perks.
  • Work with the concept of collections and pouch databases
    • Multiple collections in a single Database
    • Multiple collections in multiple Databases
  • Supports web, electron, react-native, and anything else pouchdb supports.

To install

npm i pouch-collection

or if you prefer yarn: yarn add pouch-collection

How to Use

Consider this definition of a model and it's collection.

// Person.ts

    import {IModel, PouchCollection} from "pouch-collection";
    
    export interface IPerson extends IModel {
        name: string;
        age: number;
        otherInfo: { [key: string]: any };
    }
    
    export class PersonCollection extends PouchCollection<IPerson> {
        
        // Optional. Overide to define collection-specific indexes.
        async beforeInit(): Promise<void> {
            
            await this.addIndex(['age']); // be sure to create an index for what you plan to filter by.
        }

        // Optional. Overide to perform actions after all the necessary indexes have been created.
        async afterInit(): Promise<void> {
           
        }
    
    }
    

IModel contains the meta fields needed by pouchdb and pouch-collections to operate so every model interface definition needs to extend it. Only supports the same field types as pouchDB does.

PouchCollection is a generic abstract class that should be given your model type. This helps it guide you later and give you suggestions of how to work with your model.

If you need to do things before and after initialization, you can override the async hook functions: beforeInit or afterInit;

Now that we have defined our Model and a Collection for that model, Here is how we instantiate collections. You should probably define and export collection instances somewhere in your codebase that you can easily import anywhere in your app.


    // instantiate a collection by giving it the dbname it should use
    export const personCollection: PersonCollection = new Person('db1');

    // Another collection. Notice how it shares the same dbname we passed into the previous collection instance.
    export const someOtherCollection: SomeOtherCollection = new SomeOtherCollection('db1'); 
    
    // In case we needed the same model but for a different database
    export const personCollection: PersonCollection = new Person('db2');

From this point:

  • We have our definitions
  • We have our collection instances

We are ready to start CRUDing!

    import {personCollection} from '...'

    // Using collections
    let somePerson: IPerson = {
        name: 'Basket Mouth',
        age: 99,
    }
    let anotherPerson: IPerson = {
        name: 'Bovi',
        age: 45,
    }

    somePerson = await personCollection.upsert(somePerson);
    anotherPerson = await personCollection.upsert(anotherPerson);
    
    // somePerson has been persisted and will now also have some metafields like _id, _rev, etc.

    somePerson.age = 45;
    somePerson = await personCollection.upsert(somePerson);

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

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

Collection API reference

Considering that T is the provided type definition of your model:

  • find(criteria: Partial<T>): Promise<T[]>

  • findOrFail(criteria: Partial<T>): Promise<T[]>

  • findOne(criteria: Partial<T>): Promise<T>

  • findOneOrFail(criteria: Partial<T>): Promise<T>

  • findById(_id: string): Promise<T>

  • findByIdOrFail(_id: string): Promise<T>

  • removeById(id: string): Promise<void>

  • upsert(item: T): Promise<T>

  • bulkUpsert(items: T[]): Promise<T>

  • bulkRemove(items: T[]): Promise<T>

You also have access to a collection instance's internal pouchdb reference e.g collectionInstance.db.find, but that should NEVER be used to manipulate data. For data manipulation, it is best to rely on the exposed functions provided by the collection instance because of the way pouch-collections wraps the pouch data.

If you want more pouchdb feature support, feel free to open an issue. This library is also very simple to grok, so feel free to send in a PR!

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