@liquicode/lib-managed-storage v0.0.18
lib-managed-storage
(v0.0.18)
A storage engine for managed objects. Tracks object identity, ownership, and permissions.
+--------------------------+
| MongoProvider |
==> +--------------------------+
/ | Reads and writes objects |
+-----------------+ +------------------------+ +--------------------+ / | to a MongoDB instance. |
| Application | | ManagedObject | | ManagedStorage | / +--------------------------+
+-----------------+ <==> +------------------------+ <==> +--------------------+ <==
| Works with user | | Combines user identity | | Controls access | \
| owned objects. | | with object identity. | | by user identity. | \ +--------------------------+
+-----------------+ +------------------------+ +--------------------+ \ | JsonProvider |
==> +--------------------------+
| Reads and writes objects |
| to an in-memory array. |
+--------------------------+NOTE: This project has been superceded by the newer project lib-user-storage. Primary changes are:
- The
_mfield of storage objects, containing the object metadata and ownership information, is now configurable and can be given any valid javascript name. - The
_ofield has been removed. Storage objects are now treated as plain javascript objects
Getting Started
Install via NPM:
npm install @liquicode/lib-managed-storageInclude the library in your source code:
const Lib = require( '@liquicode/lib-managed-storage' );Simple Usage
const LIB_MANAGED_STORAGE = require( '@liquicode/lib-managed-storage' );
// Make some fake users to work with.
let Alice = { user_id: 'alice@fake.com', user_role: 'admin' };
let Bob = { user_id: 'bob@fake.com', user_role: 'user' };
let Eve = { user_id: 'eve@fake.com', user_role: 'user' };
// Get the managed storage.
let storage = LIB_MANAGED_STORAGE.NewManagedStorage(); // Defaults to an in-memory json array.
let doc = null;
// Alice creates a public document that anyone can read.
doc = await storage.CreateOne( Alice, { name: 'Public Document', text: 'This is a public document.' } );
await storage.Share( Alice, doc, null, null, true ); // Share this doc with everyone.
// Alice creates a restricted document that only specific users have read or write access to.
doc = await storage.CreateOne( Alice, { name: 'Internal Document', text: 'This is an internal document.' } );
await storage.Share( Alice, doc, null, Bob.user_id ); // Give read and write access to Bob.
await storage.Share( Alice, doc, Eve.user_id ); // Give read-only access to Eve.
// Alice creates a restricted document that only specific users have read or write access to.
doc = await storage.CreateOne( Alice, { name: 'Secret Document', text: 'This is a secret document.' } );
await storage.Share( Alice, doc, Bob.user_id ); // Give read-only access to Bob.
// Create some private documents for Bob.
doc = await storage.CreateOne( Bob, { name: 'My Document', text: 'This is my document.' } );
doc = await storage.CreateOne( Bob, { name: 'My Document 2', text: 'This is my other document.' } );
// Create a private document for Eve.
doc = await storage.CreateOne( Eve, { name: 'Evil Plans', text: 'Step 1: Take over the world.' } );API Summary
To use this library, first install into your project with NPM:
npm install @liquicode/lib-managed-storageThen, include it into your application code like this:
const LIB_MANAGED_STORAGE = require( '@liquicode/lib-managed-storage' );Library Functions
DefaultConfiguration ( ): Returns a default storage configuration.NewManagedObject ( Owner, Prototype ): Returns a newManagedObjectcontaining management data in the_mfield and the contents ofPrototypein the_ofield.NewManagedStorage ( Configuration ): Returns aManagedStorageobject that exports the functions below.
Discovery Functions
Count ( User, Criteria ): Returns the number of objects available toUseras specified byCriteria.FindOne ( User, Criteria ): Returns a single object as specified byCriteria.FindMany ( User, Criteria ): Returns an array of objects as specified byCriteria.
Manipulation Functions
CreateOne ( User, Prototype ): Creates a new object in the collection that is owned byUser.WriteOne ( User, Criteria, DataObject ): Replaces a single object in the collection.DeleteOne ( User, Criteria ): Deletes a single object in the collection.DeleteMany ( User, Criteria ): Deletes multiple objects in the collection.
Permissions Functions
SetOwner ( User, OwnerID, Criteria ): SetsOwnerIDas the owner of the objects specified byCriteria. Only users with auser_roleof'admin'or'super'can call this function.Share ( User, Criteria, Readers, Writers, MakePublic ): Shares objects to other users.Readersis a string or array of strings, containing theuser_ids of the users that can read these objects. The readers list is updated and not overwritten. Everyuser_idprovided will be added to from the readers list.Writersis a string or array of strings, containing theuser_ids of the users that can write these objects. The writers list is updated and not overwritten. Everyuser_idprovided will be added to from the writers list.MakePublicis an optional boolean value. If a boolean value is provided it will mark the matched objects as public or not public accordingly.Unshare ( User, Criteria, NotReaders, NotWriters, MakeUnpublic ): Unshares objects from other users.NotReadersis a string or array of strings, containing theuser_ids of the users that cannnot read these objects. The readers list is updated and not overwritten. Everyuser_idprovided will be removed from the readers list.Writersis a string or array of strings, containing theuser_ids of the users that cannot write these objects. The writers list is updated and not overwritten. Everyuser_idprovided will be removed from the writers list.MakeUnpublicis an optional boolean value. If a boolean value is provided it will unmark the matched objects as public or not public accordingly.
Common Function Parameters
User: A json object containing the fieldsuser_idanduser_role.Criteriacan be one of: - If missing/undefined or null, then all objects are matched. - A regular json object whose values are matched against the_ofield of managed objects in storage. This is a MongoDB-like object query to specify one or more objects. See json-criteria for more information. - A string representing the value of_m.idto find in storage. - A managed object with_mand/or_ofields. If_mis present, then the value of_m.idwill be matched. If_mor_m.idis missing, then_owill be matched as a regular json object.The
DataObjectparameter in theWriteOnefunction can be: - A managed object with_mand/or_ofields. If the_ofield exists, it will be used to overwrite the_ofield of the stored object. - A json object that will be used to overwrite the_ofield of the stored object.
Notices
- Dedicated to my family, without whom, this work would not be possible.
- Source code ASCII art banners generated using https://patorjk.com/software/taag with the "Univers" font.
- The
JsonProviderimplementation was partly inspired by the project jsondbfs.
Dependencies
- uuid
: Used by
ManagedStorageandJsonProviderto generate unique identifiers. - mongodb
: Used by the
MongoProviderimplementation. - json-criteria : Used to provide MongoDB-like query functionality.
- babel-polyfill
: A dependency of the
json-criteriapackage. - lockfile
: Used by
JsonProviderwhen flushing in-memory objects to disk.