Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
georgepalmer-couch_foo
Advanced tools
= CouchFoo
== Introduction
CouchDB (http://couchdb.apache.org/) works slightly differently to relational databases. First, and foremost, it is a document-oriented database. That is, data is stored in documents each of which have a unique id that is used to access and modify it. The contents of the documents are free from structure (or schema free) and bear no relation to one another (unless you encode that within the documents themselves). So in many ways documents are like records within a relational database except there are no tables to keep documents of the same type in.
CouchDB interfaces with the external world via a RESTful interface. This allows document creation, updating, deletion etc. The contents of a document are specified in JSON so its possible to serialise objects within the database record efficiently as well as store all the normal types natively.
As a consequence of its free form structure there is no SQL to query the database. Instead you define (table-oriented) views that emit certain bits of data from the record and apply conditions, sorting etc to those views. For example if you were to emit the colour attribute you could find all documents with a certain colour. This is similar to indexed lookups on a relational table (both in terms of concept and performance).
CouchDB has been designed from the ground up to operate in a distributed way. It provides robust, incremental replication with bi-directional conflict detection and resolution. It's an excellent choice for unstructed data, large datasets that need sharding efficiently and situations where you wish to run local copies of the database.
== Overview
CouchFoo provides an ActiveRecord styled interface to CouchDB. The external API is nearly identical to ActiveRecord so it should be possible to migrate your applications quite easily. That said, there are a few minor differences to the way CouchDB works. In particular:
It is recommend that you read the quick start and performance sections in the rdoc for a full overview of differences and points to be aware of when developing. The Changelog file shows the differences between gem versions and should be checked when upgrading gem versions.
== Getting started
CouchFoo::Base.set_database(:host => "http://localhost:5984", :database => "mydatabase") CouchFoo::Base.logger = Rails.logger
If using with Rails you will need to create an initializer to do this (until proper integration is added). Also note depending on your version of CouchDB will depend on the version of the CouchREST gem you will require - CouchDB 0.9 requires CouchREST greater than 0.2 and CouchDB 0.8 requires CouchREST between 0.16 and 0.2. You will be warned on CouchFoo initialization if this is wrong.
== Examples of usage
Basic operations are the same as ActiveRecord: class Address < CouchFoo::Base property :number, Integer property :street, String property :postcode # Any generic type is fine as long as .to_json and class.from_json(json) can be called on it end
address1 = Address.create(:number => 3, :street => "My Street", :postcode => "secret") # Create address address2 = Address.create(:number => 27, :street => "Another Street", :postcode => "secret") Address.all # = [address1, address2] or maybe [address2, address1] depending on key generation Address.first # = address1 or address2 depending on keys so probably isn't as expected Address.find_by_street("My Street") # = address1
As key generation is through a UUID scheme, the order can't be predicted. However you can order the results by default: class Address < CouchFoo::Base property :number, Integer property :street, String property :postcode # Any generic type is fine as long as .to_json can be called on it property :created_at, DateTime
default_sort :created_at
end
Address.all # = [address1, address2] Address.first # = address1 or address2, sorting is applied after results Address.first(:use_key => :created_at) # = address1 but at the price of creating a new index
Conditions work slightly differently: Address.find(:all, :conditions {:street => "My Street"}) # = address1, creates index on :street Address.find(:all, :conditions {:created_at => "sometime"}) # Uses same index as :use_key => :created_at Address.find(:all, :use_key => :street, :startkey => 'p') # All streets from p in alphabet, reuses the index created 2 lines up
As well as providing support for people using relational databases, CouchFoo attempts to provide a library for those wanting to use CouchDB as a document-oriented database: class Document < CouchFoo::Base property :number, Integer property :street, String
view :number_ordered, "function(doc) {emit([doc.number , doc.street], doc); }", nil, :descending => true
end
Document.number_ordered(:limit => 75) # Will get the last 75 documents in the database ordered by number, street attributes
Associations work as expected but you must to remember to add the properties required for an association (we'll make this automatic soon): class House < CouchFoo::Base has_many :windows end
class Window < CouchFoo::Base property :house_id, String belongs_to :house end
== Credits
This gem was inspired some excellent work on CouchPotato, CouchREST, ActiveCouch and RelaxDB gems. Each offered its own benefits and own challenges. After hacking with each I couldn't get a library was happy with. So I started with ActiveRecord and modified it to work with CouchDB. Some areas required more work than others but a lot of features were achieved for free once the base level of functionality had been achieved. Credit to DHH, the rails core guys and the CouchDB gems that inspired this work.
== What's left to do?
Please feel free to fork and hit me with a request to merge back in. At the moment, the following areas need addressing:
FAQs
Unknown package
We found that georgepalmer-couch_foo demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.