Comparing version 1.0.4 to 1.0.5
import Arweave from 'arweave'; | ||
import { fieldType } from './faces/fields'; | ||
import { GQLEdgeBlockInterface, GQLEdgeTransactionInterface } from './faces/gql'; | ||
@@ -14,3 +15,6 @@ import { IGlobalOptions, RequestType } from './faces/options'; | ||
private after; | ||
private afterRegex; | ||
private readonly afterRegex; | ||
private readonly emptyLinesRegex; | ||
private readonly fields; | ||
private includes; | ||
/** | ||
@@ -24,3 +28,3 @@ * | ||
* Search is the first function called before doing a find. | ||
* @param col What type of search are we going to do. | ||
* @param type What type of search are we going to do. | ||
*/ | ||
@@ -44,2 +48,4 @@ search(type?: RequestType): this; | ||
cursor(after: string): this; | ||
only(fields: fieldType | fieldType[]): this; | ||
exclude(fields: fieldType | fieldType[]): this; | ||
find(filters?: IGlobalOptions): Promise<import("./faces/gql").GQLBlockInterface | import("./faces/gql").GQLTransactionInterface | GQLEdgeTransactionInterface[] | GQLEdgeBlockInterface[]>; | ||
@@ -59,2 +65,3 @@ findOne(filters?: IGlobalOptions): Promise<import("./faces/gql").GQLBlockInterface | import("./faces/gql").GQLTransactionInterface | GQLEdgeTransactionInterface[] | GQLEdgeBlockInterface[]>; | ||
private log; | ||
private validateIncludes; | ||
} |
349
lib/ardb.js
@@ -28,8 +28,39 @@ "use strict"; | ||
this.afterRegex = /after: *"([^"]*)"/gi; | ||
this.emptyLinesRegex = /^\s*[\r\n]/gm; | ||
this.fields = [ | ||
'id', | ||
'anchor', | ||
'signature', | ||
'recipient', | ||
'owner', | ||
'owner.address', | ||
'owner.key', | ||
'fee', | ||
'fee.winston', | ||
'fee.ar', | ||
'quantity', | ||
'quantity.winston', | ||
'quantity.ar', | ||
'data', | ||
'data.size', | ||
'data.type', | ||
'tags', | ||
'tags.name', | ||
'tags.value', | ||
'block', | ||
'block.id', | ||
'block.timestamp', | ||
'block.height', | ||
'block.previous', | ||
'parent', | ||
'parent.id', | ||
]; | ||
this.includes = new Set(); | ||
this.arweave = arweave; | ||
this.logs = logs; | ||
this.includes = new Set(this.fields); | ||
} | ||
/** | ||
* Search is the first function called before doing a find. | ||
* @param col What type of search are we going to do. | ||
* @param type What type of search are we going to do. | ||
*/ | ||
@@ -133,2 +164,36 @@ search(type = 'transactions') { | ||
} | ||
only(fields) { | ||
// Empty the included fields. | ||
this.includes = new Set(); | ||
if (typeof fields === 'string' && this.fields.indexOf(fields) !== -1) { | ||
this.includes.add(fields); | ||
return this; | ||
} | ||
const toInclude = []; | ||
for (const field of fields) { | ||
// @ts-ignore | ||
if (this.fields.indexOf(field) !== -1) { | ||
// @ts-ignore | ||
toInclude.push(field); | ||
} | ||
} | ||
if (toInclude.length) { | ||
this.includes = new Set(toInclude); | ||
} | ||
this.validateIncludes(); | ||
return this; | ||
} | ||
exclude(fields) { | ||
// To make only() and exclude() work the same, re-add all fields to includes. | ||
this.includes = new Set(this.fields); | ||
if (typeof fields === 'string') { | ||
this.includes.delete(fields); | ||
return this; | ||
} | ||
for (const field of fields) { | ||
this.includes.delete(field); | ||
} | ||
this.validateIncludes(); | ||
return this; | ||
} | ||
// Ready to run | ||
@@ -290,107 +355,134 @@ find(filters = {}) { | ||
params = params.substring(1, params.length - 1); | ||
let returnParams = ''; | ||
switch (this.reqType) { | ||
case 'transaction': | ||
returnParams = ` | ||
let fields = ''; | ||
if (this.reqType === 'transaction' || this.reqType === 'transactions') { | ||
let owner = ''; | ||
if (this.includes.has('owner')) { | ||
owner = `owner { | ||
${this.includes.has('owner.address') ? 'address' : ''} | ||
${this.includes.has('owner.key') ? 'key' : ''} | ||
}`; | ||
} | ||
let fee = ''; | ||
if (this.includes.has('fee')) { | ||
fee = `fee { | ||
${this.includes.has('fee.winston') ? 'winston' : ''} | ||
${this.includes.has('fee.ar') ? 'ar' : ''} | ||
}`; | ||
} | ||
let quantity = ''; | ||
if (this.includes.has('quantity')) { | ||
quantity = `quantity { | ||
${this.includes.has('quantity.winston') ? 'winston' : ''} | ||
${this.includes.has('quantity.ar') ? 'ar' : ''} | ||
}`; | ||
} | ||
let data = ''; | ||
if (this.includes.has('data')) { | ||
data = `data { | ||
${this.includes.has('data.size') ? 'size' : ''} | ||
${this.includes.has('data.type') ? 'type' : ''} | ||
}`; | ||
} | ||
let tags = ''; | ||
if (this.includes.has('tags')) { | ||
tags = `tags { | ||
${this.includes.has('tags.name') ? 'name' : ''} | ||
${this.includes.has('tags.value') ? 'value' : ''} | ||
}`; | ||
} | ||
let block = ''; | ||
if (this.includes.has('block')) { | ||
block = `block { | ||
${this.includes.has('block.id') ? 'id' : ''} | ||
${this.includes.has('block.timestamp') ? 'timestamp' : ''} | ||
${this.includes.has('block.height') ? 'height' : ''} | ||
${this.includes.has('block.previous') ? 'previous' : ''} | ||
}`; | ||
} | ||
let parent = ''; | ||
if (this.includes.has('parent') || this.includes.has('parent.id')) { | ||
// Parent only has an ID, so if one of them is included, add both. | ||
parent = `parent { | ||
id | ||
anchor | ||
signature | ||
recipient | ||
owner { | ||
address | ||
key | ||
} | ||
fee { | ||
winston | ||
ar | ||
} | ||
quantity { | ||
winston | ||
ar | ||
} | ||
data { | ||
size | ||
type | ||
} | ||
tags { | ||
name, | ||
value | ||
} | ||
block { | ||
id | ||
timestamp | ||
height | ||
previous | ||
} | ||
parent { | ||
id | ||
}`; | ||
break; | ||
case 'transactions': | ||
returnParams = ` | ||
pageInfo { | ||
hasNextPage | ||
} | ||
edges { | ||
cursor | ||
node { | ||
id | ||
anchor | ||
signature | ||
recipient | ||
owner { | ||
address | ||
key | ||
} | ||
fee { | ||
winston | ||
ar | ||
} | ||
quantity { | ||
winston | ||
ar | ||
} | ||
data { | ||
size | ||
type | ||
} | ||
tags { | ||
name | ||
value | ||
} | ||
block { | ||
id | ||
timestamp | ||
height | ||
previous | ||
} | ||
parent { | ||
id | ||
} | ||
} | ||
}`; | ||
break; | ||
case 'block': | ||
returnParams = ` | ||
}`; | ||
} | ||
fields = ` | ||
${this.includes.has('id') ? 'id' : ''} | ||
${this.includes.has('anchor') ? 'anchor' : ''} | ||
${this.includes.has('signature') ? 'signature' : ''} | ||
${this.includes.has('recipient') ? 'recipient' : ''} | ||
${owner} | ||
${fee} | ||
${quantity} | ||
${data} | ||
${tags} | ||
${block} | ||
${parent} | ||
`; | ||
fields = fields.replace(this.emptyLinesRegex, '').trim(); | ||
if (!fields.length) { | ||
fields = ` | ||
id | ||
anchor | ||
signature | ||
recipient | ||
owner { | ||
address | ||
key | ||
} | ||
fee { | ||
winston | ||
ar | ||
} | ||
quantity { | ||
winston | ||
ar | ||
} | ||
data { | ||
size | ||
type | ||
} | ||
tags { | ||
name | ||
value | ||
} | ||
block { | ||
id | ||
timestamp | ||
height | ||
previous`; | ||
break; | ||
case 'blocks': | ||
returnParams = ` | ||
pageInfo { | ||
hasNextPage | ||
} | ||
edges { | ||
cursor | ||
node { | ||
id | ||
timestamp | ||
height | ||
previous | ||
previous | ||
} | ||
parent { | ||
id | ||
}`; | ||
} | ||
}`; | ||
break; | ||
} | ||
else { | ||
fields = ` | ||
${this.includes.has('block.id') ? 'id' : ''} | ||
${this.includes.has('block.timestamp') ? 'timestamp' : ''} | ||
${this.includes.has('block.height') ? 'height' : ''} | ||
${this.includes.has('block.previous') ? 'previous' : ''} | ||
`; | ||
fields = fields.replace(this.emptyLinesRegex, '').trim(); | ||
if (!fields.length) { | ||
fields = ` | ||
id | ||
timestamp | ||
height | ||
previous`; | ||
} | ||
} | ||
if (this.reqType === 'transactions' || this.reqType === 'blocks') { | ||
fields = ` | ||
pageInfo { | ||
hasNextPage | ||
} | ||
edges { | ||
cursor | ||
node { | ||
${fields} | ||
} | ||
}`; | ||
} | ||
if (!this.reqType || !params) { | ||
@@ -403,3 +495,3 @@ throw new Error('Invalid options. You need to first set your options!'); | ||
){ | ||
${returnParams} | ||
${fields} | ||
} | ||
@@ -413,3 +505,58 @@ }`; | ||
} | ||
validateIncludes() { | ||
// Add all children if all of them are missing but a parent is present. | ||
if (this.includes.has('owner') && !this.includes.has('owner.address') && !this.includes.has('owner.key')) { | ||
this.includes.add('owner.address'); | ||
this.includes.add('owner.key'); | ||
} | ||
else if (this.includes.has('fee') && !this.includes.has('fee.winston') && !this.includes.has('fee.ar')) { | ||
this.includes.add('fee.winston'); | ||
this.includes.add('fee.ar'); | ||
} | ||
else if (this.includes.has('quantity') && | ||
!this.includes.has('quantity.winston') && | ||
!this.includes.has('quantity.ar')) { | ||
this.includes.add('quantity.winston'); | ||
this.includes.add('quantity.ar'); | ||
} | ||
else if (this.includes.has('data') && !this.includes.has('data.size') && !this.includes.has('data.type')) { | ||
this.includes.add('data.size'); | ||
this.includes.add('data.type'); | ||
} | ||
else if (this.includes.has('tags') && !this.includes.has('tags.name') && !this.includes.has('tags.value')) { | ||
this.includes.add('tags.name'); | ||
this.includes.add('tags.value'); | ||
} | ||
else if (this.includes.has('block') && | ||
!this.includes.has('block.timestamp') && | ||
!this.includes.has('block.height') && | ||
!this.includes.has('block.previous')) { | ||
this.includes.add('block.id'); | ||
this.includes.add('block.timestamp'); | ||
this.includes.add('block.height'); | ||
this.includes.add('block.previous'); | ||
} | ||
// Add a parent if one of the children is present but the parent is not | ||
if (!this.includes.has('owner') && (this.includes.has('owner.address') || this.includes.has('owner.key'))) { | ||
this.includes.add('owner'); | ||
} | ||
else if (!this.includes.has('fee') && (this.includes.has('fee.winston') || this.includes.has('fee.ar'))) { | ||
this.includes.add('fee'); | ||
} | ||
else if (!this.includes.has('quantity') && | ||
(this.includes.has('quantity.winston') || this.includes.has('quantity.ar'))) { | ||
this.includes.add('quantity'); | ||
} | ||
else if (!this.includes.has('data') && (this.includes.has('data.size') || this.includes.has('data.type'))) { | ||
this.includes.add('data'); | ||
} | ||
else if (!this.includes.has('tags') && (this.includes.has('tags.name') || this.includes.has('tags.value'))) { | ||
this.includes.add('tags'); | ||
} | ||
else if (!this.includes.has('block') && | ||
(this.includes.has('block.timestamp') || this.includes.has('block.height') || this.includes.has('block.previous'))) { | ||
this.includes.add('block'); | ||
} | ||
} | ||
} | ||
exports.default = ArDB; |
{ | ||
"name": "ardb", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"main": "lib/ardb.js", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/cedriking/ardb.git", |
@@ -97,2 +97,12 @@ # ArDB | ||
```js | ||
only(fields: string|string[]) | ||
``` | ||
- Only return the specified return fields from your GQL query. | ||
```js | ||
exclude(fields: string|string[]) | ||
``` | ||
- Excludes the specified return fields from your GQL query. | ||
```js | ||
limit(limit: number) | ||
@@ -99,0 +109,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
31983
11
740
202