Skip to content

Commit 190c017

Browse files
committed
edit: bump npm, fix tests
1 parent 86090d6 commit 190c017

File tree

6 files changed

+53
-51
lines changed

6 files changed

+53
-51
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
"typescript"
2929
],
3030
"dependencies": {
31-
"arangojs": "^7.7.0"
31+
"arangojs": "^8.0.0"
3232
},
3333
"devDependencies": {
3434
"@types/chai": "^4.3.0",
35-
"@types/mocha": "^9.1.0",
36-
"@types/node": "^17.0.18",
35+
"@types/mocha": "^10.0.0",
36+
"@types/node": "^18.11.9",
3737
"chai": "^4.3.6",
38-
"mocha": "^9.2.1",
38+
"mocha": "^10.1.0",
3939
"ts-node": "^10.5.0",
40-
"typedoc": "^0.22.12",
40+
"typedoc": "^0.23.20",
4141
"typedoc-plugin-markdown": "^3.11.14",
4242
"typescript": "^4.5.5"
4343
},

src/filter.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { aql } from 'arangojs'
1+
import { aql, literal, join } from 'arangojs/aql'
22
import { Filter } from './lib/structs'
33

44
export function buildFilters(filters: Filter[]) {
55
if (!filters.length) return
66

77
let _filters = filters.map(
8-
(f: Filter) => aql`doc.${f.field} ${aql.literal(f.op)} ${f.val}`,
8+
(f: Filter) => aql`doc.${f.field} ${literal(f.op)} ${f.val}`,
99
)
1010

11-
return aql`FILTER ${aql.join(_filters, ' && ')}`
11+
return aql`FILTER ${join(_filters, ' && ')}`
1212
}

src/index.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { aql } from 'arangojs'
2-
import { query } from './lib/structs'
1+
import { aql, literal } from 'arangojs/aql'
2+
import { Query } from './lib/structs'
33
import { buildSearch } from './search'
44
import { buildFilters } from './filter'
55
export { buildFilters } from './filter'
@@ -14,7 +14,7 @@ export { parseQuery } from './parse'
1414
* <https://www.arangodb.com/docs/stable/aql/operations-limit.html> @param
1515
* limit is an object with keys `start` @default 0, and `count` @default 20 */
1616
export function buildAQL(
17-
query: query,
17+
query: Query,
1818
limit: any = { start: 0, count: 20 },
1919
): any {
2020
validateQuery(query)
@@ -25,21 +25,21 @@ export function buildAQL(
2525

2626
/* FOR doc IN ${query.view} */
2727
return aql`
28-
FOR doc IN ${aql.literal(query.view)}
28+
FOR doc IN ${literal(query.view)}
2929
${SEARCH}
3030
${FILTER}
3131
LIMIT ${limit.start}, ${limit.count}
3232
RETURN doc`
3333
}
3434

35-
function validateQuery(query: query) {
35+
function validateQuery(query: Query) {
3636
if (!query.view.length)
3737
throw new Error('query.view must be a valid ArangoSearch View name')
3838
if (!query.collections.length)
3939
throw new Error('query.collections must have at least one name')
4040
}
4141

42-
function collectKeys(query: query) {
42+
function collectKeys(query: Query) {
4343
/* unify query.key */
4444
let _keys: string[]
4545
if (typeof query.key == 'string') {

src/lib/structs.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* passed to buildAQL, i.e. `let generatedAQL = buildAQL(query)`. Properties
33
* mimic or match those familiar to AQL.
44
* */
5-
export interface query {
5+
export interface Query {
66
/**
77
* the name of the ArangoSearch View the query will be run against
88
* */
@@ -11,7 +11,7 @@ export interface query {
1111
* the names and analyzers of the collections indexed by @param
1212
* view to query
1313
* */
14-
collections: collection[]
14+
collections: Collection[]
1515
/**
1616
* either an array of @param term interfaces or a string to be parsed by `parseQuery()`
1717
* */
@@ -40,7 +40,7 @@ export interface query {
4040
* In either case all desired collection/analyzer combinations must be
4141
* specified.
4242
* */
43-
export interface collection {
43+
export interface Collection {
4444
/** the name of the collection */
4545
name: string
4646
/** the name of the text analyzer */

src/search.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { aql } from 'arangojs'
2-
import { query, collection, Term, Type } from './lib/structs'
1+
import { aql, literal, join } from 'arangojs/aql'
2+
import { Query, Collection, Term, Type } from './lib/structs'
33
import { parseQuery } from './parse'
44

5-
export function buildSearch(query: query): any {
5+
export function buildSearch(query: Query): any {
66
/* parse string query */
77
query.terms =
88
typeof query.terms == 'string' ? parseQuery(query.terms) : query.terms
@@ -18,9 +18,9 @@ export function buildSearch(query: query): any {
1818
ORS = undefined
1919
}
2020
if (!!NOTS) {
21-
NOTS = aql`${ANDS || ORS ? aql.literal(' AND ') : undefined}
22-
${NOTS.phrases ? aql.literal(' NOT ') : undefined} ${NOTS.phrases}
23-
${NOTS.phrases && NOTS.tokens ? aql.literal(' AND ') : undefined} ${
21+
NOTS = aql`${ANDS || ORS ? literal(' AND ') : undefined}
22+
${NOTS.phrases ? literal(' NOT ') : undefined} ${NOTS.phrases}
23+
${NOTS.phrases && NOTS.tokens ? literal(' AND ') : undefined} ${
2424
NOTS.tokens
2525
}`
2626
}
@@ -36,7 +36,7 @@ export function buildSearch(query: query): any {
3636
OPTIONS { collections: ${cols} }
3737
SORT TFIDF(doc) DESC`
3838
}
39-
function buildOps(collections: collection[], terms: Term[], op: string): any {
39+
function buildOps(collections: Collection[], terms: Term[], op: string): any {
4040
const opWord: string = op == '+' ? ' AND ' : ' OR '
4141

4242
let queryTerms: any = terms.filter((t: Term) => t.op == op)
@@ -52,24 +52,24 @@ function buildOps(collections: collection[], terms: Term[], op: string): any {
5252

5353
/* if (!phrases && !tokens) return */
5454
if (op == '-') return { phrases, tokens }
55-
if (phrases && tokens) return aql.join([phrases, tokens], opWord)
55+
if (phrases && tokens) return join([phrases, tokens], opWord)
5656
return tokens || phrases
5757
}
5858

5959
function buildPhrases(
6060
phrases: Term[],
61-
collections: collection[],
61+
collections: Collection[],
6262
opWord: string,
6363
): any {
6464
if (!phrases.length) return undefined
6565

66-
return aql.join(
66+
return join(
6767
phrases.map((phrase: any) => buildPhrase(phrase, collections)),
6868
opWord,
6969
)
7070
}
7171

72-
function buildPhrase(phrase: Term, collections: collection[]): any {
72+
function buildPhrase(phrase: Term, collections: Collection[]): any {
7373
const phrases = []
7474
collections.forEach((coll) =>
7575
coll.keys.forEach((k: string) =>
@@ -78,10 +78,10 @@ function buildPhrase(phrase: Term, collections: collection[]): any {
7878
),
7979
),
8080
)
81-
return aql`(${aql.join(phrases, ' OR ')})`
81+
return aql`(${join(phrases, ' OR ')})`
8282
}
8383

84-
function buildTokens(tokens: Term[], collections: collection[]): any {
84+
function buildTokens(tokens: Term[], collections: Collection[]): any {
8585
if (!tokens.length) return
8686

8787
const opWordMap = {
@@ -102,12 +102,12 @@ function buildTokens(tokens: Term[], collections: collection[]): any {
102102
analyzer: string,
103103
keys: string[],
104104
) => {
105-
return aql.join(
105+
return join(
106106
keys.map(
107107
(k) => aql`
108108
ANALYZER(
109109
TOKENS(${tokens}, ${analyzer})
110-
${aql.literal(op)} IN doc.${k}, ${analyzer})`,
110+
${literal(op)} IN doc.${k}, ${analyzer})`,
111111
),
112112
' OR ',
113113
)
@@ -122,6 +122,6 @@ function buildTokens(tokens: Term[], collections: collection[]): any {
122122
)
123123
})
124124

125-
return aql`MIN_MATCH(${aql.join(remapped, ', ')},
125+
return aql`MIN_MATCH(${join(remapped, ', ')},
126126
${tokens[0].op === '-' ? collections.length : 1})`
127127
}

tests/bool.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
import { expect } from 'chai'
2-
import { Database, aql } from 'arangojs'
2+
import { Database } from 'arangojs/database'
3+
import { aql, literal } from 'arangojs/aql'
34

45
import { buildAQL } from '../src/index'
6+
import { Query } from '../src/lib/structs'
7+
import { View } from 'arangojs/view'
58

69
const ARANGO_URL = process.env.TEST_ARANGODB_URL || 'http://127.0.0.1:8529'
710

811
describe('boolean search logic', () => {
912
let db: Database
10-
let collection
11-
let view
13+
let collection: string | number | boolean | Record<string, any> | null | undefined
14+
let view: View
1215
let date = new Date().valueOf()
1316
const dbName = `testdb_${date}`
1417
const collectionName = `coll_${date}`
1518

1619
before(async () => {
1720
/* create db */
1821
db = new Database({ url: ARANGO_URL, databaseName: '_system' })
19-
try {
20-
await db.createDatabase(dbName)
21-
} catch (e) {
22-
// console.error('could not create test db', e)
23-
console.error(e)
24-
}
2522

26-
db = new Database({ url: ARANGO_URL, databaseName: dbName })
23+
await db.createDatabase(dbName)
24+
db.database(dbName)
25+
2726
/* create coll */
2827
collection = db.collection(collectionName)
2928
await collection.create()
@@ -37,7 +36,10 @@ describe('boolean search logic', () => {
3736
text_es: { analyzers: ['text_es'] },
3837
},
3938
}
40-
await view.create({ links })
39+
await view.create({
40+
links,
41+
type: 'arangosearch'
42+
})
4143
let info = await view.get()
4244
expect(info.name).to.equal(view.name)
4345

@@ -83,7 +85,7 @@ describe('boolean search logic', () => {
8385
await wait(1600)
8486

8587
const getAllInViewQuery = aql`
86-
FOR doc in ${aql.literal(info.name)}
88+
FOR doc in ${literal(info.name)}
8789
RETURN doc`
8890
let allResults = await db.query(getAllInViewQuery)
8991
let all = await allResults.batches.all()
@@ -93,7 +95,7 @@ describe('boolean search logic', () => {
9395

9496
after(async () => {
9597
try {
96-
db.useDatabase('_system')
98+
db = db.database('_system')
9799
await db.dropDatabase(dbName)
98100
/* should get deleted when dropping db */
99101
/* await view.drop() */
@@ -179,7 +181,7 @@ describe('boolean search logic', () => {
179181
await cursor.next()
180182

181183
/* english */
182-
query.key = ['text_en', 'text_es']
184+
query.key = ['text_en', 'text_es'] as any
183185
query.terms = '+"in document"'
184186
aqlQuery = buildAQL(query)
185187
expect(aqlQuery.bindVars.value0).to.equal('text_en')
@@ -195,7 +197,7 @@ describe('boolean search logic', () => {
195197

196198
/* should match 1 document */
197199
query.terms = '+"across " '
198-
query.key = ['text_en', 'text_es']
200+
query.key = ['text_en', 'text_es'] as any
199201
cursor = await db.query(buildAQL(query))
200202

201203
result = await cursor.all()
@@ -211,7 +213,7 @@ describe('boolean search logic', () => {
211213
cursor = await db.query(buildAQL(query))
212214

213215
/* multikey */
214-
query.key = ['text', 'text_es']
216+
query.key = ['text', 'text_es'] as any
215217
query.terms = '+"buscar"'
216218

217219
let q = buildAQL(query)
@@ -371,7 +373,7 @@ describe('boolean search logic', () => {
371373
describe('NOTS', () => {
372374
it(`should bring back results *not* matching NOT'ed values
373375
when -'ed PHRASE's are passed`, async () => {
374-
const query = {
376+
const query: Query = {
375377
view: view.name,
376378
collections: [
377379
{
@@ -387,7 +389,7 @@ describe('boolean search logic', () => {
387389
],
388390
/* should bring back 1 result */
389391
terms: ' -"sample " word ',
390-
key: ['text_en', , 'text_es'],
392+
key: ['text_en', , 'text_es'] as any,
391393
}
392394

393395
/* should bring back 1 results */

0 commit comments

Comments
 (0)