Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

roots-db

Package Overview
Dependencies
Maintainers
1
Versions
383
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

roots-db - npm Package Compare versions

Comparing version 0.0.0 to 0.0.1

test/roots-db-old-test.js

382

lib/Collection.js

@@ -103,3 +103,2 @@ /*

collection.meta = schema.meta || {} ;
collection.documentBase = {} ;
collection.suspectedBase = {} ;

@@ -130,11 +129,6 @@

/*
// Properties
for ( key in collection.properties )
{
Object.defineProperty( collection.documentBase , key , {
value: collection.properties[ key ].default ,
writable: true , // needed, or derivative object cannot set it
enumerable: true
} ) ;
Object.defineProperty( collection.suspectedBase , key , {

@@ -146,4 +140,4 @@ value: undefined ,

}
*/
// Meta

@@ -189,154 +183,2 @@ for ( key in collection.meta )

Collection.prototype.addMetaLink = function collectionAddMetaLink( key , property , collectionName )
{
var self = this ;
// Be careful: this.world.collections does not contains this collection at this time: we are still in the constructor!
var getter = function getter()
{
var witness , document = this[''] , linkCollection = self.world.collections[ collectionName ] ;
if ( ! document.suspected || this[ property ] !== undefined )
{
if ( ! ( document.meta[ property ] instanceof rootsDb.Document ) || this[ property ] != document.meta[ property ].id ) // jshint ignore:line
{
//console.log( '### Link get ###' ) ;
document.meta[ property ] = linkCollection.get( this[ property ] ) ;
}
}
else
{
// Here we have no ID -- erf, idea :) -- about what we will get
if (
! ( document.meta[ property ] instanceof rootsDb.Document ) ||
! ( witness = document.meta[ property ].witness ) ||
witness.property !== property ||
witness.document !== document ||
witness.type !== 'link'
)
{
//console.log( '### Link describe suspect ###' ) ;
document.meta[ property ] = rootsDb.Document( linkCollection , null , {
suspected: true ,
witness: {
document: document ,
property: property ,
type: 'link'
}
} ) ;
}
}
return document.meta[ property ] ;
} ;
var setter = function setter( linkDocument )
{
//var linkCollection = self.world.collections[ collectionName ] ;
// Throw error or not?
if ( ! ( linkDocument instanceof rootsDb.Document ) || linkDocument.collection.name !== collectionName ) { return ; }
var document = this[''] ;
this[ property ] = linkDocument.id ;
document.meta[ property ] = linkDocument ;
} ;
Object.defineProperty( this.documentBase , key , {
configurable: true ,
get: getter ,
set: setter
} ) ;
Object.defineProperty( this.suspectedBase , key , {
configurable: true ,
get: getter ,
set: setter
} ) ;
} ;
Collection.prototype.addMetaBacklink = function collectionAddMetaBacklink( key , property , collectionName )
{
var self = this ;
// Be careful: this.world.collections does not contains this collection at this time: we are still in the constructor!
var getter = function getter()
{
var witness , fingerprint = {} , document = this[''] , backlinkCollection = self.world.collections[ collectionName ] ;
if ( ! document.suspected || document.id )
{
fingerprint[ property ] = document.id ;
fingerprint = self.createFingerprint( fingerprint , { from: rootsDb.INTERNAL } ) ;
if ( ! ( document.meta[ property ] instanceof rootsDb.Batch ) || document.meta[ property ].fingerprint.$ != fingerprint.$ ) // jshint ignore:line
{
//console.log( '### Backlink collect ###' , fingerprint ) ;
document.meta[ property ] = backlinkCollection.collect( fingerprint ) ;
}
}
else
{
// Here we have no ID -- erf, idea :) -- about what we will get
if (
! ( document.meta[ property ] instanceof rootsDb.Batch ) ||
! ( witness = document.meta[ property ].witness ) ||
witness.property !== property ||
witness.document !== document ||
witness.type !== 'backlink'
)
{
//console.log( '### Backlink describe suspect ###' ) ;
document.meta[ property ] = rootsDb.Batch( backlinkCollection , null , {
suspected: true ,
witness: {
document: document ,
property: property ,
type: 'backlink'
}
} ) ;
}
}
return document.meta[ property ] ;
} ;
var setter = function setter() // linkDocument )
{
throw new Error( 'Not done ATM!' ) ;
//var backlinkCollection = self.world.collections[ collectionName ] ;
/*
// Throw error or not?
if ( ! ( linkDocument instanceof rootsDb.Document ) || linkDocument.collection.name !== collectionName ) { return ; }
var document = this[''] ;
this[ property ] = linkDocument.id ;
document.meta[ property ] = linkDocument ;
*/
} ;
Object.defineProperty( this.documentBase , key , {
configurable: true ,
get: getter ,
set: setter
} ) ;
Object.defineProperty( this.suspectedBase , key , {
configurable: true ,
get: getter ,
set: setter
} ) ;
} ;
Collection.prototype.initDriver = function collectionInitDriver()

@@ -369,33 +211,2 @@ {

Collection.prototype.createDocument = function collectionCreateDocument( properties , options )
{
var i , document ;
// Do not move hooks into rootsDb.Document()!!!
// It should not be triggered by internal usage
//console.log( "before hook of rootsDb.Document()" , properties ) ;
if ( typeof properties === 'object' )
{
for ( i = 0 ; i < this.hooks.beforeCreateDocument.length ; i ++ )
{
properties = this.hooks.beforeCreateDocument[ i ]( properties ) ;
}
}
//console.log( "before rootsDb.Document()" , properties ) ;
document = rootsDb.Document( this , properties , options ) ;
//console.log( "after rootsDb.Document()" , document.$ ) ;
for ( i = 0 ; i < this.hooks.afterCreateDocument.length ; i ++ )
{
this.hooks.afterCreateDocument[ i ]( document ) ;
}
return document ;
} ;
Collection.prototype.createId = function collectionCreateId( rawDocument , id )

@@ -442,3 +253,3 @@ {

var key , index , obsoleteIndexes = [] , missingIndexes = [] ;
var key , obsoleteIndexes = [] , missingIndexes = [] ;

@@ -488,2 +299,35 @@ for ( key in upstreamIndexes )

/* Document-oriented method */
Collection.prototype.createDocument = function collectionCreateDocument( rawDocument , options )
{
var i ;
if ( ! rawDocument || typeof rawDocument !== 'object' ) { rawDocument = {} ; }
// Do not move hooks into rootsDb.Document()!!!
// It should not be triggered by internal usage
//console.log( "before hook of rootsDb.Document()" , rawDocument ) ;
for ( i = 0 ; i < this.hooks.beforeCreateDocument.length ; i ++ )
{
this.hooks.beforeCreateDocument[ i ]( rawDocument ) ;
}
//console.log( "before rootsDb.Document()" , rawDocument ) ;
rootsDb.Document( this , rawDocument , options ) ;
//console.log( "after rootsDb.Document()" , document.$ ) ;
for ( i = 0 ; i < this.hooks.afterCreateDocument.length ; i ++ )
{
this.hooks.afterCreateDocument[ i ]( rawDocument ) ;
}
return rawDocument ;
} ;
/*

@@ -501,3 +345,3 @@ There are 2 modes:

{
var rawDocument , document , self = this ;
var self = this ;

@@ -524,8 +368,5 @@ // Managing function's arguments

if ( options.raw ) { callback( undefined , rawDocument ) ; return ; }
if ( ! options.raw ) { rootsDb.Document( self , rawDocument , { fromUpstream: true } ) ; }
callback(
undefined ,
rootsDb.Document( self , rawDocument , { from: rootsDb.UPSTREAM } )
) ;
callback( undefined , rawDocument ) ;
} ) ;

@@ -603,3 +444,3 @@ } ;

var i , batch , length = rawBatch.length , idKey = self.driver.idKey ;
var i , batch , length = rawBatch.length ;

@@ -625,1 +466,144 @@ if ( options.raw )

// Should check if those features are still working...
Collection.prototype.addMetaLink = function collectionAddMetaLink( key , property , collectionName )
{
var self = this ;
// Be careful: this.world.collections does not contains this collection at this time: we are still in the constructor!
var getter = function getter()
{
var witness , document = this[''] , linkCollection = self.world.collections[ collectionName ] ;
if ( ! document.suspected || this[ property ] !== undefined )
{
if ( ! ( document.meta[ property ] instanceof rootsDb.Document ) || this[ property ] != document.meta[ property ].id ) // jshint ignore:line
{
//console.log( '### Link get ###' ) ;
document.meta[ property ] = linkCollection.get( this[ property ] ) ;
}
}
else
{
// Here we have no ID -- erf, idea :) -- about what we will get
if (
! ( document.meta[ property ] instanceof rootsDb.Document ) ||
! ( witness = document.meta[ property ].witness ) ||
witness.property !== property ||
witness.document !== document ||
witness.type !== 'link'
)
{
//console.log( '### Link describe suspect ###' ) ;
document.meta[ property ] = rootsDb.Document( linkCollection , null , {
suspected: true ,
witness: {
document: document ,
property: property ,
type: 'link'
}
} ) ;
}
}
return document.meta[ property ] ;
} ;
var setter = function setter( linkDocument )
{
//var linkCollection = self.world.collections[ collectionName ] ;
// Throw error or not?
if ( ! ( linkDocument instanceof rootsDb.Document ) || linkDocument.collection.name !== collectionName ) { return ; }
var document = this[''] ;
this[ property ] = linkDocument.id ;
document.meta[ property ] = linkDocument ;
} ;
Object.defineProperty( this.suspectedBase , key , {
configurable: true ,
get: getter ,
set: setter
} ) ;
} ;
Collection.prototype.addMetaBacklink = function collectionAddMetaBacklink( key , property , collectionName )
{
var self = this ;
// Be careful: this.world.collections does not contains this collection at this time: we are still in the constructor!
var getter = function getter()
{
var witness , fingerprint = {} , document = this[''] , backlinkCollection = self.world.collections[ collectionName ] ;
if ( ! document.suspected || document.id )
{
fingerprint[ property ] = document.id ;
fingerprint = self.createFingerprint( fingerprint , { from: rootsDb.INTERNAL } ) ;
if ( ! ( document.meta[ property ] instanceof rootsDb.Batch ) || document.meta[ property ].fingerprint.$ != fingerprint.$ ) // jshint ignore:line
{
//console.log( '### Backlink collect ###' , fingerprint ) ;
document.meta[ property ] = backlinkCollection.collect( fingerprint ) ;
}
}
else
{
// Here we have no ID -- erf, idea :) -- about what we will get
if (
! ( document.meta[ property ] instanceof rootsDb.Batch ) ||
! ( witness = document.meta[ property ].witness ) ||
witness.property !== property ||
witness.document !== document ||
witness.type !== 'backlink'
)
{
//console.log( '### Backlink describe suspect ###' ) ;
document.meta[ property ] = rootsDb.Batch( backlinkCollection , null , {
suspected: true ,
witness: {
document: document ,
property: property ,
type: 'backlink'
}
} ) ;
}
}
return document.meta[ property ] ;
} ;
var setter = function setter() // linkDocument )
{
throw new Error( 'Not done ATM!' ) ;
//var backlinkCollection = self.world.collections[ collectionName ] ;
/*
// Throw error or not?
if ( ! ( linkDocument instanceof rootsDb.Document ) || linkDocument.collection.name !== collectionName ) { return ; }
var document = this[''] ;
this[ property ] = linkDocument.id ;
document.meta[ property ] = linkDocument ;
*/
} ;
Object.defineProperty( this.suspectedBase , key , {
configurable: true ,
get: getter ,
set: setter
} ) ;
} ;

@@ -32,3 +32,3 @@ /*

var tree = require( 'tree-kit' ) ;
//var tree = require( 'tree-kit' ) ;
var ErrorStatus = require( 'error-status' ) ;

@@ -38,21 +38,56 @@

// ( collection , [properties] , [options] )
function Document( collection , properties , options )
// ( collection , [data] , [options] )
function Document( collection , data , options )
{
if ( ! ( collection instanceof rootsDb.Collection ) ) { throw new TypeError( '[roots-db] Argument #0 of rootsDb.Document() should be an instance of rootsDb.Collection' ) ; }
if ( ! data || typeof data !== 'object' ) { data = {} ; }
if ( ! options || typeof options !== 'object' ) { options = {} ; }
var deepInherit = collection.deepInherit ;
// First, validate the data
try {
collection.validate( data ) ;
}
catch ( error ) {
error.validatorMessage = error.message ;
error.message = '[roots-db] validator error: ' + error.message ;
//console.log( "error!!!!!!" , data ) ;
//console.log( "error!!!!!!" , collection.dataSchema ) ;
throw error ;
}
var id = collection.driver.createId( data , options.id ) ;
var document = Object.create( Document.prototype , {
data: { value: data } ,
id: { value: id , enumerable: true } ,
world: { value: collection.world } ,
collection: { value: collection } ,
meta: { value: {} } ,
suspected: { writable: true, value: false } ,
loaded: { writable: true, value: false } ,
saved: { writable: true, value: false } ,
deleted: { writable: true, value: false } ,
upstreamExists: { writable: true, value: false }
meta: { value: {} , enumerable: true } ,
//suspected: { value: false , writable: true , enumerable: true } ,
loaded: { value: options.fromUpstream ? true : false , writable: true , enumerable: true } ,
saved: { value: false , writable: true , enumerable: true } ,
deleted: { value: false , writable: true , enumerable: true } ,
upstreamExists: { value: options.fromUpstream ? true : false , writable: true , enumerable: true }
} ) ;
if ( options.fromUpstream )
{
document.loaded = true ;
document.upstreamExists = true ;
}
Object.defineProperty( data , '$' , { value: document } ) ;
return document ;
// Should probably have its instance type of its own
/*
// Suspect is set when the object is in a state where it may exist upstream but should be loaded first

@@ -104,65 +139,3 @@ if ( options.suspected )

}
// Hydrating the document as fast as possible
if ( typeof properties === 'object' )
{
switch ( options.from )
{
case rootsDb.UPSTREAM :
case rootsDb.MEMPROXY :
// it is safe to get directly 'properties' from upstream: every object is already unique (cloned)
document.upstream = deepInherit( properties , collection.documentBase ) ;
document.$ = deepInherit( null , document.upstream ) ;
Object.defineProperty( document.$ , '' , { value: document } ) ; // link to the parent
document.loaded = true ;
document.upstreamExists = true ;
break ;
//case rootsDb.NONE :
default :
document.upstream = deepInherit( null , collection.documentBase ) ;
document.$ = deepInherit( null , document.upstream ) ;
// It is probably unsafe to reference directly userland properties
// Here we clone 'property' and fix prototype accordingly,
// also we have to trust userland prototype
//tree.extend( { deep: true, proto: true, skipRoot: true } , document.$ , properties ) ;
//------------------------------------------------------------------------------ WIP -----------------------------------------
//console.log( "bob: " , collection.validate( properties ) ) ;
//*
try {
tree.extend(
{ deep: true, proto: true, skipRoot: true } ,
document.$ ,
collection.validate( properties )
) ;
}
catch ( error ) {
error.validatorMessage = error.message ;
error.message = '[roots-db] validator error: ' + error.message ;
//console.log( "error!!!!!!" , properties ) ;
//console.log( "error!!!!!!" , collection.dataSchema ) ;
throw error ;
}
//*/
Object.defineProperty( document.$ , '' , { value: document } ) ; // link to the parent
}
}
else
{
document.upstream = deepInherit( null , collection.documentBase ) ;
document.$ = deepInherit( null , document.upstream ) ;
Object.defineProperty( document.$ , '' , { value: document } ) ; // link to the parent
}
var id = collection.driver.createId( document.$ , options.id ) ;
Object.defineProperty( document , 'id' , { value: id , enumerable: true } ) ;
return document ;
*/
}

@@ -179,2 +152,3 @@

// Useful?
// Return a one-value state

@@ -202,2 +176,105 @@ Document.prototype.state = function documentState()

// callback( error )
Document.prototype.save = function documentSave( options , callback )
{
var self = this , method ;
if ( ! options || typeof options !== 'object' )
{
if ( typeof options === 'function' ) { callback = options ; }
options = {} ;
}
if ( typeof callback !== 'function' ) { callback = noop ; }
//if ( this.suspected ) { throw new Error( '[roots-db] cannot save a suspected document - it is on the TODO LIST already' ) ; }
if ( this.deleted ) { throw new Error( 'Current document is deleted' ) ; }
if ( this.upstreamExists )
{
if ( options.fullSave )
{
method = 'update' ;
}
else
{
method = 'patch' ;
//rawDocument = tree.extend( { own: true, deep: true, flat: this.collection.driver.pathSeparator } , {} , this.$ ) ;
throw new Error( "The 'patch' feature is not coded ATM!" ) ;
}
// Full save (update) or simple patch
this.collection.driver[ method ]( this.id , this.data , function( error ) {
if ( error ) { callback( error ) ; return ; }
self.saved = true ;
callback() ;
} ) ;
}
else if ( options.overwrite )
{
// overwrite needed
this.collection.driver.overwrite( this.data , function( error )
{
if ( error ) { callback( error ) ; return ; }
self.saved = true ;
self.upstreamExists = true ;
callback() ;
} ) ;
}
else
{
// create (insert) needed
this.collection.driver.create( this.data , function( error )
{
if ( error ) { callback( error ) ; return ; }
self.saved = true ;
self.upstreamExists = true ;
callback() ;
} ) ;
}
} ;
Document.prototype.delete = function documentDelete( options , callback )
{
var self = this ;
if ( ! options || typeof options !== 'object' )
{
if ( typeof options === 'function' ) { callback = options ; }
options = {} ;
}
if ( typeof callback !== 'function' ) { callback = noop ; }
if ( this.suspected ) { throw new Error( '[roots-db] cannot delete a suspected document - it is on the TODO LIST already' ) ; }
if ( this.deleted ) { throw new Error( 'Current document is already deleted' ) ; }
this.collection.driver.delete( this.id , function( error )
{
if ( error ) { callback( error ) ; return ; }
self.deleted = true ;
self.upstreamExists = false ;
callback() ;
} ) ;
} ;
/* Old 'Suspect' document... */
// Reveal a suspected Document

@@ -227,3 +304,3 @@ Document.prototype.reveal = function documentReveal( options , callback )

{
var idString , rawDocument , deepInherit = this.collection.deepInherit , self = this ;
var idString , deepInherit = this.collection.deepInherit , self = this ;

@@ -335,117 +412,1 @@ if ( typeof this.id === 'string' ) { idString = this.id ; }

} ;
Document.prototype.export = function documentExport()
{
return tree.extend( { deep: true , deepFilter: this.collection.driver.objectFilter } , {} , this.$ ) ;
} ;
// callback( error )
Document.prototype.save = function documentSave( options , callback )
{
var rawDocument , method , deepInherit = this.collection.deepInherit , self = this ;
if ( ! options || typeof options !== 'object' )
{
if ( typeof options === 'function' ) { callback = options ; }
options = {} ;
}
if ( typeof callback !== 'function' ) { callback = noop ; }
if ( this.suspected ) { throw new Error( '[roots-db] cannot save a suspected document - it is on the TODO LIST already' ) ; }
if ( this.deleted ) { throw new Error( 'Current document is deleted' ) ; }
if ( this.upstreamExists )
{
if ( options.fullSave )
{
method = 'update' ;
rawDocument = tree.extend( { own: true, deep: true } , {} , this.upstream , this.$ ) ;
}
else
{
method = 'patch' ;
rawDocument = tree.extend( { own: true, deep: true, flat: this.collection.driver.pathSeparator } , {} , this.$ ) ;
}
// Full save (update) or simple patch
this.collection.driver[ method ]( this.id , rawDocument , function( error ) {
if ( error ) { callback( error ) ; return ; }
// merge $ back into upstream, then create a fresh new $
tree.extend( { own: true, move: true } , self.upstream , self.$ ) ;
self.saved = true ;
callback() ;
} ) ;
}
else if ( options.overwrite )
{
// create (insert) needed
this.collection.driver.overwrite( tree.extend( { own: true, deep: true } , {} , this.$ ) , function( error )
{
if ( error ) { callback( error ) ; return ; }
// now that it exists in DB, $ become DB and a fresh $ should be created
self.upstream = self.$ ;
self.$ = deepInherit( null , self.upstream ) ;
Object.defineProperty( self.$ , '' , { value: self } ) ; // link to the parent
// mark this document as saved and trigger the callback
self.saved = true ;
self.upstreamExists = true ;
callback() ;
} ) ;
}
else
{
// create (insert) needed
this.collection.driver.create( tree.extend( { own: true, deep: true } , {} , this.$ ) , function( error )
{
if ( error ) { callback( error ) ; return ; }
// now that it exists in DB, $ become DB and a fresh $ should be created
self.upstream = self.$ ;
self.$ = deepInherit( null , self.upstream ) ;
Object.defineProperty( self.$ , '' , { value: self } ) ; // link to the parent
// mark this document as saved and trigger the callback
self.saved = true ;
self.upstreamExists = true ;
callback() ;
} ) ;
}
} ;
Document.prototype.delete = function documentDelete( options , callback )
{
var self = this ;
if ( ! options || typeof options !== 'object' )
{
if ( typeof options === 'function' ) { callback = options ; }
options = {} ;
}
if ( typeof callback !== 'function' ) { callback = noop ; }
if ( this.suspected ) { throw new Error( '[roots-db] cannot delete a suspected document - it is on the TODO LIST already' ) ; }
if ( this.deleted ) { throw new Error( 'Current document is already deleted' ) ; }
this.collection.driver.delete( this.id , function( error )
{
if ( error ) { callback( error ) ; return ; }
self.deleted = true ;
self.upstreamExists = false ;
callback() ;
} ) ;
} ;
{
"name": "roots-db",
"version": "0.0.0",
"version": "0.0.1",
"description": "Even more minimalistic ODM",

@@ -5,0 +5,0 @@ "main": "lib/rootsDb.js",

@@ -84,5 +84,5 @@ /*

afterCreateDocument: //[
function( document ) {
function( data ) {
//console.log( "- Users afterCreateDocument 'after' hook -" ) ;
document.$.memberSid = '' + document.$.firstName + ' ' + document.$.lastName ;
data.memberSid = '' + data.firstName + ' ' + data.lastName ;
}

@@ -93,3 +93,3 @@ //]

var expectedDefaultUser = { firstName: 'Joe', lastName: 'Doe' , godfatherId: undefined , jobId: undefined , memberSid: 'Joe Doe' } ;
var expectedDefaultUser = { firstName: 'Joe', lastName: 'Doe' , memberSid: 'Joe Doe' } ;

@@ -218,5 +218,5 @@ var jobsDescriptor = {

expect( user ).to.be.an( rootsDb.Document ) ;
expect( user.$._id ).to.be.an( mongodb.ObjectID ) ;
expect( tree.extend( null , {} , user.$ ) ).to.eql( tree.extend( null , { _id: user.$._id } , expectedDefaultUser ) ) ;
expect( user.$ ).to.be.an( rootsDb.Document ) ;
expect( user._id ).to.be.an( mongodb.ObjectID ) ;
expect( user ).to.eql( tree.extend( null , { _id: user._id } , expectedDefaultUser ) ) ;
} ) ;

@@ -231,11 +231,9 @@

expect( user ).to.be.an( rootsDb.Document ) ;
expect( user.$._id ).to.be.an( mongodb.ObjectID ) ;
expect( tree.extend( null , {} , user.$ ) ).to.eql( {
_id: user.$._id ,
expect( user.$ ).to.be.an( rootsDb.Document ) ;
expect( user._id ).to.be.an( mongodb.ObjectID ) ;
expect( user ).to.eql( {
_id: user._id ,
firstName: 'Bobby' ,
lastName: 'Fischer' ,
memberSid: 'Bobby Fischer' ,
godfatherId: undefined ,
jobId: undefined
memberSid: 'Bobby Fischer'
} ) ;

@@ -242,0 +240,0 @@ } ) ;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc