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

macmodel

v0.0.9

Published

model generation helper for macmongo

Downloads

21

Readme

macmodel

model generation helpers for macmongo

installation

npm install macmodel

usage

MongoDoc					= require 'macmodel'
events						= require 'events'
Seq 							= require 'seq'

# Account is a fictional model object used to illustrate. MongoDoc is essentially an abstract object that is only useful when subclasses

class Account extends MongoDoc

Account.init = ->
	console.log @data()

Account.nameHasValidationError = (username)->
	if username.length < 2 or not username
		return "Username must be 2+ characters"
	return false
Account.passwordHasValidationError = (password)->
	if password.length < 3 or not password
		return "Password must be 3+ characters"
	return false
Account.createAccount = (accountData, fn)->
	if errorText = Account.nameHasValidationError(accountData.username)
		return fn(new Error(errorText))
	else if errorText = Account.passwordHasValidationError(accountData.password)
		return fn(new Error(errorText))
	else
		Seq().seq ->
			Account.fetchOne {username: accountData.username}, this
		.seq (account)->
			if account
				this(new Error("Username already exists"))
			else
				Account.fetchOne {accountId: accountData.accountId}, this
		.seq (account)->
			if account
				this(new Error("Cannot create multiple accounts from this device"))
			else
				accountData.created = new Date()
				new Account accountData, {save: true}, this
		.seq (account)->
			fn(null, account)
		.catch (error)->
			fn(error)

_.extend Account, events.EventEmitter.prototype

Account.collectionName = 'Account'
MongoDoc.register(Account)


# APIs

	########################################################################################################
	#																											Class APIs
	##########################################################################################################



# Constructor()

# @param doc			the data object that should be wrapped by the MongoDoc instance 
# @param fn				(optional) callback function with (err, MongoDoc) signature, returns after the MongoDoc was instantiated.
# 									The object wont' be saved to storage unless the callback is passed as argument
account = new Account doc, (err, doc)->
	if doc
		console.log "OK created and saved"



# fetch() to fetch all matches: Account.fetch(query, options, fn)

# @see http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#find
#
# @param query			mongodb query object e.g {color: 'blue'}, see 
# @param options		(optional) mongodb options object e.g. {fields: {color: true}}
# @param fn					callback with (error, MongoDocs<Array>) signature
#
# @return a mongodb stream that can be iterated over
Account.fetch(query, options, fn)


# fetchOne() fetches the first match: Account.fetchOne(query, options, fn)

# @see http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#find
#
# @param query			mongodb query object e.g {color: 'blue'}, see 
# @param options		mongodb options object e.g. {fields: {color: true}}
# @param fn					callback with (error, MongoDoc) signature
#
# @return N/A
Account.fetchOne(query, options, fn)

# The db.property maps to the underlying macmongo instance 

Account.db

	##########################################################################################################
	#																											Instance APIs
	##########################################################################################################

	# 

# Supplements known data with data from the storage (this assumes a matching object exists in the storage)
account = new Account(_id: '123')
account.fillFromStorage, (err, doc)->
	if err
		console.error "No matching object found?"
	else
		account.getData() # This should yield not just the _id but also all the data we could fetch from the storage 

# Save current data object to mongodb
account.save(fn)

# Attach a new set of data and save
account.setData(data, fn)

# Extends current data object (does not save)
account.extend(data)

# Update data in storage
account.update(modifier, options, fn)

# Remove from storage
account.remove()

# Get a copy of the data object attached to the instance
account.data()