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

@tastypie/mongoose

v1.1.1

Published

Tastypie resource and fields for mongodb backends

Downloads

7

Readme

Codeship Status for esatterwhite/tastypie-mongo

node-mongoose

A Tastypie resource for mongoose.

Install Mongoose Resource
npm install mongoose tastypie-mongoose
Make A mongoose Model
// Make A Mongoose Model
var Schema = new mongoose.Schema({ 
	name:{
		first:{type:String}
		,last:{type:String}
	}
	,index:{type:Number, required:false}
	,guid:{type:String, requierd:false}
	,tags:[{type:String}]
}, {collection:'tastypie'})

var Test = connection.model('Test', Schema)
Define A Resource
var tastypie = require("tastypie");
var MongoseResource = require('tastypie-mongoose');

// Default Query
var queryset = Test.find().lean().toConstructor()

// Define A Mongo Resource
var Mongo = MongoseResource.extend({
	options:{
		queryset: queryset
	}
	,fields:{
		firstName: {type:'CharField', attribute:'name.first'} // Remaps name.first to firstName
		,lastName: {type:'CharField', attribute:'name.last'} // Remaps name.last to lastName
	}
})

Paging

You can use a number of special query string params to control how data is paged on the list endpoint. Namely -

  • limit - Page size ( default 25 )
  • offset - The starting point in the list

limit=25&offset=50 would be the start of page 3

Sorting

sorting is handled query param orderby where you can pass it the name of a field to sort on. Sorting is descending by default. Specifying a negetive field ( - ) would flip the order

Advanced Filtering

You might have noticed the filtering field on the schema. One of the things that makes an API "Good" is the ability to use query and filter the data to get very specific subsets of data. Tastypie exposes this through the query string as field and filter combinations. By default, the resource doesn't have anything enabled, you need to specify which filters are allowed on which fields, or specify 1 to allow everything

Filter Types

| Filter | function | | ------------|------------------------------------------ | | gt | greater than | | gte | greater than or equal to | | lt | less than | | lte | less than or equal to | | in | Value in set ( [ 1,2,3 ]) | | nin | Value Not in set | | size | Size of set ( array length ) | | startswith | Case Sensitive string match | | istartswith | Case Insensitive string match | | endswith | Case Sensitive string match | | iendswith | Case Insensitive string match | | contains | Case Sensitive global string match | | icontains | Case Insensitive global string match | | exact ( = ) | Exact Case Sensitive string match | | iexact | Exact Case Insensitive string match | | match | Matches an item in an array ( elemMatch ) |

Filters are added by appending a double underscore __ and the filter type to the end of a field name. Given our example, if we wanted to find people who were older than 25, we would use the following URI syntax

http://localhost:3000/api/v1/user?age__gt=25

Filters are additive for a given field. For example, if we we only wanted people where we between 25 and 45, we would just add another filter

http://localhost:3000/api/v1/user?age__gt=25&age__lt=45

The same double underscore __ syntax is used to access nested fields where the filter is always the last parameter. So we could find people whos age was greater than 25, less than 45 and whose Company Name starts with W

http://localhost:3000/api/v1/user?age__gt=25&age__lt=45&company__name__istartswith=w

Remember, remapped fields still work for filtering, so the same would also be true for companyName

http://localhost:3000/api/v1/user?age__gt=25&age__lt=45&companyName__istartswith=w

Resources provide a simple and expressive syntax to query for very specific subsets of data without any of the boilerplate work to set it up. And as you would expect, regular params will map back to exact where applicable

http://localhost:3000/api/v1/user?age=44

Serialization

Tastypie supports multiple serialization formats out of the box as well as a way to define your own custom formats. The base serializer class supports xml, json & jsonp by default. You add formats or create your own serialization formats by subclassing the Serializer class and defining the approriate methods. Each format must define a to_<FORMAT> and a from_<FORMAT>. For example, tastypie defines the to_xml and from_xml methods. JSON is defined by to_json, from_json

To get back xml just change the Accept header NOTE: Hapi deals with most application/foo formats, but is blind to text/foo. So the safe bet here is to use text/xml

// curl -H "Accept: text/xml" http://localhost:3000/api/v1/user

<?xml version="1.0" encoding="UTF-8"?>
<response>
 <meta type="object">
  <count type="number">100</count>
  <limit type="number">1</limit>
  <offset type="number">0</offset>
  <previous type="null">null</previous>
  <next type="string">/api/v1/meth?limit=25&offset=25</next>
 </meta>
 <data type="array">
  <object type="object">
   <name type="string">Dejesus Zimmerman</name>
   <age type="number">31</age>
   <companyName type="string">AVENETRO</companyName>
   <id type="string">557af820f3c3008b415de02c</id>
   <uri type="string">/api/v1/meth/557af820f3c3008b415de02c</uri>
  </object>
 </data>
</response>