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

bailiwick

v0.3.0

Published

This is an experimental model toolkit for web applications. It's meant to allow for a model that is closer to the Domain Model pattern.

Downloads

8

Readme

Bailiwick

Build Status

This is an experimental model toolkit for web applications. It's meant to allow for a model that is closer to the Domain Model pattern. It's been factored out of working domain models in several web applications to make it as practical and useful as possible while avoiding chrome plating and the geegaws that sometimes accompany such frameworks or toolkits.

The code is getting close to being useful for production code. There are a few things left to work out before I'd feel comfortable saying it's 1.0, but I'm using it in a few applications now and really enjoying it.

I think I've ironed out the testing and packaging, but I'm still unsure of what the best way to package an ES6 library for NPM is.

The things left to work on are:

  • Association mutators (e.g., addAssociatedObject, removeAssociatedObject, etc.)
  • Consider adding additional association types (manyToMany, oneToOne, ?). I think these will be necessary once associations add mutator methods.
  • Test coverage
  • Documentation

Requirements

  1. fetch -- this library uses the isomorphic-fetch library for testing.

Current API

/* acme/models.js */
import config from './config.js!';
import {Model, RESTService, validator, ValidationError} from 'bailiwick';

export class AcmeModel extends Model {
    static get datastore() {
        return new RESTService( config.serviceEndpoint );
    }
}

export class Role extends AcmeModel {
    @validator('name')
    validateName() {
        if ( !this.name || this.name === '' ) {
            throw new ValidationError( 'is not present', 'name' );
        }
        
        return Role.where({ name: this.name }).get().
            then( existing => {
                if ( existing.id !== this.id ) {
                    throw new ValidationError('is not unique', 'name');
                }
            }).
            catch( () => true ); // Simplified
    }
}

export class User extends AcmeModel {

    @validator('firstName')
    validateFirstName() {
        if ( !this.firstName || this.firstName === '' ) {
            throw new ValidationError( 'is not present', 'firstName' );
        }
        return true;
    }

    @validator('lastName')
    validateLastName() {
        if ( !this.lastName || this.lastName === '' ) {
            throw new ValidationError( 'is not present', 'lastName' );
        }
        return true;
    }

    @validator('login')
    validateLastName() {
        if ( !this.login || this.login === '' ) {
            throw new ValidationError( 'is not present', 'login' );
        }
        else if ( this.login.length < 6 ) {
            throw new ValidationError( 'is too short (min 6 characters)', 'login' );
        }
        return User.where({ login: this.login }).get().
            then( existing => {
                if ( existing.id !== this.id ) {
                    throw new ValidationError('is not unique', 'login');
                }
            }).
            catch( () => true ); // Simplified
    }

}


let smiths = [],
    adminRole = null;

User.where({ lastName: 'Smith' }).get().
    then( users => smiths.push(...users) ).
    catch( err => console.error("Couldn't fetch Smiths: ", err) );
Role.where({ name: 'admin' }).get().
    then( role => adminRole = role ).
    catch( err => console.error("Couldn't fetch admin role: ", err) );

let newUser = new User({
    firstName: 'Jen',
    lastName: 'Smith',
    login: 'jsmith'
});
newUser.save().
    then( savedUser => smiths.push(savedUser) ).
    catch( err => {
        console.error( "Couldn't save the user: ", err );
        if ( err instanceof ValidationError ) {
            form.errors = newUser.errors; // Instance of bailiwick.ValidationErrors
        }
    })

Associations

This allows auto-generation of methods on model objects that return other related model objects.

import {oneToMany, manyToOne} from 'bailiwick';

@oneToMany('orders', Order)
export class User extends AcmeModel {}

@manyToOne('customer', () => User)
export class Order extends AcmeModel {


}

let user = null, orders = [];

User.get( 23 ).then( result => {
    user = result;
    return user.getOrders().
        then( results => orders.push(...results) );
});

Schema Declaration

This will allow model classes to (optionally) pre-define their API without needing to fetch a record from the datastore first. This is used when constructing new instances so you don't have to provide every field:

import {schema} from 'bailiwick';

@schema({
    firstName: null,
    lastName: null,
    login: null,
    roles: []
})
export class User extends AcmeModel {}

let u = new User({ firstName: 'Lana', lastName: 'Del Rey' });

Validation Utilities

This provides decorators that can be used to declare validation functions for attributes.

import {validator} from 'bailiwick';

export class User extends AcmeModel {
  @validator('type')
  validateLogin() {
      if ( !this.login ) {
        throw new Error("login must be set");
      } else if ( this.login !~ /^[a-z]\w{2,16}$/ ) {
          throw new Error( `invalid login: ${this.login}` );
      }
  }    
}

Validator methods are called by the validate method, which returns a Promise the resolves iff all of its validators run with no errors being raised.

The validate method is called automatically by create, update, and replace.

I'm planning on adding some canned validation functions for common checks:

import {
    validatesPresence,
    validatesMinLength,
    validatesType,
    validatesUnique
} from 'bailiwick';

export class User extends AcmeModel {
    @validatesPresence( 'firstName', 'lastName', 'login' );
    @validatesType( String, 'firstName', 'lastName', 'login' );
    @validatesMinLength( 6, 'login' );
    @validatesUnique( 'login' );
}

Author

License

Copyright (c) 2015-2020, Michael Granger All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the author/s, nor the names of the project's contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.