couch [x] lightweight
npm install xcouch
var Store = require'xcouch'Storeconnect''iferr throw errStorecreateUser'name' 'pass'iferr throw err// Hooray, we created a new user
Create a file
var util = require'util'Store = require'xcouch';// Add the model to the model-registry of the storeStoreaddModel'MyModel' MyModel// Create a new modelif!this instanceof MyModel return id revStorecallthis id revutilinheritsMyModel Store // The model must inherit from Store
MyModel do the following:
var Store = require'xcouch'// Load the modelrequire'./mymodel'// Connects to the store (CouchDB)Storeconnect''iferr throw err// Connects as a user of the storeStoreconnectUser'name' 'pass'iferr throw err// Creates a new objectvar myObject = getObject'MyModel'// Saves the new objectmyObjectsaveiferr throw errifwritten// changes savedelse// nothing changed, so nothing written :-)// Loads an existing object (document)var myObject2 = getObject'MyModel' 'idOfTheDocument'myObject2loadiferr// Not found// We could save it here under the id 'idOfTheDocument'// But we'll throw:throw err// Print the dataconsole.logmyObject2get
- One database per user.
- By default, the user owns its database exclusively. No other
user can read from or write to it.
- Admins can do everything.
- xCouch will never create accounts with administrative privileges.
- Only admins can create users. Direct sign-up to the CouchDB
will be disabled per '_security' document.
Store.createUser(name, pass, callback)
callback(err, id, rev)
Store.connectUser(name, pass, callback)
callback(err, doc, getObject, db, nano)
Store.destroyUser(name, pass, callback)
.set([hash] || [field, value])
.save([force, ] callback)
.addMany(obj || array of objects)
WARNING: The tests and xCouch will make changes to your CouchDB
and those changes will not be reverted automatically. Please make
backups if you have to undo the changes xCouch made to your
Understood? Ready? Then:
test/options.json and edit
"dsn": "http://[admin]:[pass]@localhost:5984""user":"name": "xcouchtestuser""pass": "acb123""user_one":"name": "xcouchtestuserone""pass": "acb123""user_two":"name": "xcouchtestusertwo""pass": "acb123"
mocha -R spec --ignore-leaks and hit enter.
And again, running the tests will change your CouchDB in a way
you might not like. Don't blame the author for lost data or ask
the author to clean up your database afterwards.
Here is the output of the tests taken from a Win7Starter Netbook
D:\Workspace\couchapps\q3\_xcouch>mocha -R spec --ignore-leaks --bailxCouch: StoreV needs to connect (82ms)The Store UserV must have a valid username (^[a-z][a-z0-9\_\$()\+\-\/]*$)V must be created (896ms)V and needs to connect tooThe connect of the user gave usV the user-documentV a function getObject() to get objects from the db of the userV the db of the user as an nano-db-objectV a nano-object to interact with the couch as the userA Store ObjectV will be created with getObject()V inherits from StoreV is some .type()V has .get() to get and .set() to set fields of dataV is .dirty after a changecan be saved with .save()V will be written if dirty (108ms)V will not be written if not dirtyV can be forced to be written even if not dirtyAfter a save, the dataV has a _id fieldV has a _rev fieldV has a type fieldV the type field equals the return value of .type()can be loaded from the store with .load()V will hold the same data (47ms)can be removed from the store with .remove()V .A new Store ObjectV will always be written on .save()xCouch: SecurityV we need to connect (52ms)V someone creates UserOne (86ms)V someone creates UserTwo (193ms)V UserOne connectsV UserOne tries put a doc into the db of UserTwo: Not allowed. (65ms)V UserOne tries to read a doc from the db of UserTwo: Not allowed.V UserOne tries to remove a doc from the db of UserTwo: Not allowed.V UserOne tries to list db "_users": Not allowed.V UserOne tries to list the db of UserTwo: Not allowed.? 32 tests complete (2235ms)
Copyright (c) 2012 Oliver Leics firstname.lastname@example.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.