Comparing version 1.1.0 to 1.2.0
const IMap = require('./imap'); | ||
const ISet = require('./iset'); | ||
const IArray = require('./iarray'); | ||
const Table = require('./table'); | ||
@@ -50,3 +51,3 @@ const Storage = require('./storage'); | ||
module.exports = {DB, IMap, ISet}; | ||
module.exports = {DB, IMap, ISet, IArray}; | ||
@@ -19,3 +19,3 @@ // const short = require('short-uuid'); | ||
if (value && ['ISet', 'IMap', 'Record'].includes(value.constructor.name)) { | ||
if (value && ['IArray', 'ISet', 'IMap', 'Record'].includes(value.constructor.name)) { | ||
value = value.constructor.name + ":" + value.id; | ||
@@ -81,6 +81,12 @@ } | ||
async empty () { | ||
switch (this.constructor.name) { | ||
case 'IMap': return this.db.iMap(); | ||
case 'ISet': return this.db.iSet(); | ||
case 'IArray': return this.db.iArray(); | ||
} | ||
} | ||
async remove (key) { | ||
return (await this._remove("" + key)) || ( | ||
this.constructor.name === 'IMap'? this.db.iMap():this.db.iSet() | ||
); // Send empty imap | ||
return (await this._remove("" + key)) || this.empty(); // Send empty imap | ||
} | ||
@@ -87,0 +93,0 @@ |
@@ -9,3 +9,3 @@ const IMap = require('./imap'); | ||
} | ||
else if (key.constructor.name === 'ISet' || key.constructor.name === 'IMap') { | ||
else if (key.constructor.name === 'ISet' || key.constructor.name === 'IMap' || key.constructor.name === 'IArray') { | ||
return key.id; | ||
@@ -12,0 +12,0 @@ } |
const { Level } = require('level'); | ||
const ISet = require('./iset'); | ||
const IMap = require('./imap'); | ||
const IArray = require('./iarray'); | ||
const Record = require('./record'); | ||
@@ -18,2 +19,3 @@ const Table = require('./table'); | ||
this.emptyISet = new ISet(this); | ||
this.emptyIArray = new IArray(this); | ||
@@ -32,3 +34,3 @@ this.data = new Map(); | ||
if (v instanceof IMap || v instanceof ISet) { | ||
if (v instanceof IMap || v instanceof ISet || v instanceof IArray) { | ||
v = this.labelType(v.constructor.name, v.id); | ||
@@ -117,3 +119,3 @@ } | ||
if (value instanceof ISet || value instanceof IMap) { | ||
if (value instanceof ISet || value instanceof IMap || value instanceof IArray) { | ||
// data[field] = this.encode(value).id; | ||
@@ -219,2 +221,6 @@ | ||
iArray () { | ||
return this.emptyIArray | ||
} | ||
async getMemNode (Type, id, create=false) { | ||
@@ -221,0 +227,0 @@ const key = this.labelCollection(Type.name, id); |
@@ -7,2 +7,3 @@ // Data Types: | ||
const ISet = require('./iset'); | ||
const IArray = require('./iarray'); | ||
@@ -35,2 +36,6 @@ const type = '_type'; | ||
}, | ||
[IArray.name]: { | ||
encode: iarray => ({[type]: IArray.name, data: iarray.id}), | ||
decode: async ({data: id}, db) => db.getMemNode(IArray, id, true) | ||
}, | ||
[Array.name]: { | ||
@@ -37,0 +42,0 @@ encode: array => { |
{ | ||
"name": "beastdb", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A persistence database specializing in state space search problems!", | ||
@@ -5,0 +5,0 @@ "main": "lib/db.js", |
@@ -347,3 +347,3 @@ # beastDB | ||
After completing our imap we can save it to a record, this will only save the last ISet | ||
After completing our iset we can save it to a record, this will only save the last ISet | ||
and discard all intermidiate states. | ||
@@ -414,4 +414,79 @@ | ||
# IArray | ||
Since IArray is created on every write operation we can start with an empty iarray and then | ||
add new values like this: | ||
```javascript | ||
const empty = await db.iArray(); | ||
let s1 = await empty.push(value1); | ||
s1 = await s1.push(value2); | ||
``` | ||
After completing our iarray we can save it to a record, this will only save the last IArray | ||
and discard all intermidiate states. | ||
```javascript | ||
await record.insert({ | ||
myarray: s1 | ||
}); | ||
``` | ||
## chain | ||
The IArray chain fields allows to chain async methods like this: | ||
```javascript | ||
await db.iArray().chain.push(1).push(2).push(3) | ||
``` | ||
Withoud the chain field the adds would have to be used sequentialy, like this. | ||
```javascript | ||
let a = await db.iArray().push(1); | ||
a = await a.push(2); | ||
a = await a.push(3); | ||
``` | ||
## async push(value) | ||
Pushes a value to the last position of the iarray and returns a new iarray. | ||
```javascript | ||
let a = await db.iArray().push(1); | ||
a = await a.push(2); | ||
a = await a.push(3); | ||
``` | ||
## async pop() | ||
Pops the last value of array, it returns the new iarray and value. | ||
```javascript | ||
let [newIArray, popedValue] = await db.iArray().chain.push(1).pop(); | ||
``` | ||
## async setIndex(index, value) | ||
## length | ||
The size of the array | ||
```javascript | ||
const size = iarray.length; | ||
``` | ||
## setIndex(index, value) | ||
Sets the index value to value, index must be inside of interval [0, this.size] | ||
```javascript | ||
let newIarray = await iarray.chain.push(1).push(2).push(3).setIndex(1, 2); | ||
``` | ||
## async toArray() | ||
It returns an array with all IArray values in inserted order, | ||
example: [1, 2, 3, 4] | ||
# Example | ||
@@ -418,0 +493,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44954
14
1033
498