Skip to main content

Class: Query

Hierarchy

  • BaseQuery

    Query

Constructors

constructor

new Query(conditions, collection)

Summary

Create an instance of Query.

Name

Query

Example

const query = new Query({
$select: [{ $field: 'address' }],
$where: {
$nill: [
{ address: { $like: '%57-59%' } },
{ free_breakfast: true },
{ free_lunch: [1] }
]
}
},
'travel-sample');

Parameters

NameTypeDescription
conditionsIConditionExprList of SELECT clause conditions
collectionstringCollection name

Overrides

BaseQuery.constructor

Defined in

query/query.ts:118

Properties

_collection

Protected _collection: string

Inherited from

BaseQuery._collection

Defined in

query/base-query.ts:14


_conditions

Protected _conditions: IConditionExpr

Inherited from

BaseQuery._conditions

Defined in

query/base-query.ts:14


groupByExpr

Private Optional groupByExpr: IGroupBy[]

GROUP BY Expression.

Defined in

query/query.ts:50


havingExpr

Private Optional havingExpr: LogicalWhereExpr<any>

HAVING Expression.

Defined in

query/query.ts:58


indexName

Private Optional indexName: string

Index name.

Defined in

query/query.ts:83


indexOn

Private Optional indexOn: IIndexOnParams[]

INDEX ON Expression.

Defined in

query/query.ts:75


indexType

Private Optional indexType: IndexType

Types of supported Index statements.

Defined in

query/query.ts:79


indexUsingGSI

Private Optional indexUsingGSI: boolean

INDEX USING GSI Expression.

Defined in

query/query.ts:87


indexWith

Private Optional indexWith: IIndexWithParams

INDEX WITH Expression.

Defined in

query/query.ts:91


letExpr

Private Optional letExpr: LetExprType

LET Expression.

Defined in

query/query.ts:46


lettingExpr

Private Optional lettingExpr: LetExprType

LETTING Expression.

Defined in

query/query.ts:54


limitExpr

Private Optional limitExpr: number

LIMIT Expression.

Defined in

query/query.ts:38


offSetExpr

Private Optional offSetExpr: number

OFFSET Expression.

Defined in

query/query.ts:42


orderExpr

Private Optional orderExpr: Record<string, SortType>

ORDER BY Expression.

Defined in

query/query.ts:34


plainJoinExpr

Private Optional plainJoinExpr: string

Plain JOIN Expression.

Defined in

query/query.ts:63


queryType

Private Optional queryType: "INDEX" | "SELECT"

Available query types.

Defined in

query/query.ts:71


selectExpr

Private Optional selectExpr: string | ISelectType[]

SELECT Expression.

Defined in

query/query.ts:26


useKeysExpr

Private Optional useKeysExpr: string[]

USE Expression.

Defined in

query/query.ts:67


whereExpr

Private Optional whereExpr: LogicalWhereExpr<any>

WHERE Expression.

Defined in

query/query.ts:30

Accessors

collection

get collection(): string

Returns

string

Defined in

query/query.ts:735

set collection(value): void

Parameters

NameType
valuestring

Returns

void

Defined in

query/query.ts:739


conditions

get conditions(): IConditionExpr

Returns

IConditionExpr

Defined in

query/query.ts:727

set conditions(value): void

Parameters

NameType
valueIConditionExpr

Returns

void

Defined in

query/query.ts:731

Methods

build

build(options?): string

Build a N1QL query from the defined parameters.

Can also use ignoreCase as part of the build method, this will always prioritize the $ignoreCase value defined in clause.

Example

const expr_where = {
$or: [
{ address: { $like: '%57-59%', $ignoreCase: false } }, // ignoreCase will not be applied
{ free_breakfast: true },
{ name: 'John' } // ignoreCase will be applied
],
};
const query = new Query({}, 'travel-sample');
const result = query.select([{ $field: 'address' }])
.where(expr_where)
.build({ ignoreCase: true }); // ignore case is enabled for where clause elements
console.log(result)
SELECT address
FROM `travel-sample`
WHERE (address LIKE "%57-59%" OR free_breakfast = true OR `(LOWER(name) = LOWER("John"))`)

Method

Parameters

NameType
optionsQueryBuildOptionsType

Returns

string

Overrides

BaseQuery.build

Defined in

query/query.ts:668


compileFromConditions

compileFromConditions(conditionals): void

Converts the conditional parameters passed to the constructor to the properties of the N1QL Query.

Method

Parameters

NameType
conditionalsIConditionExpr

Returns

void

Defined in

query/query.ts:597


execute

execute<Result>(options?, queryOptions?, ottomanInstance?): Promise<QueryResult<Result>> | Promise<Result>

Type parameters

NameType
Resultany

Parameters

NameType
optionsQueryBuildOptionsType
queryOptionsQueryOptions
ottomanInstanceOttoman

Returns

Promise<QueryResult<Result>> | Promise<Result>

Defined in

query/query.ts:719


groupBy

groupBy(value): Query

Add GROUP BY expression to SELECT clause.

Method

Example

const groupByExpr = [{ expr: 'COUNT(amount_val)', as: 'amount' }];
const query = new Query({}, 'travel-sample');
const result = query.select([{ $field: 'address' }])
.groupBy(groupByExpr)
.build()
console.log(result)
SELECT address
FROM `travel-sample`
GROUP BY COUNT(amount) AS amount

Parameters

NameType
valueIGroupBy[]

Returns

Query

Defined in

query/query.ts:508


having

having(value): Query

Add HAVING expression to GROUP BY clause.

Method

Example

const groupByExpr = [{ expr: 'COUNT(amount_val)', as: 'amount' }];
const having = { address: { $like: '%58%' } };
const query = new Query({}, 'travel-sample');
const result = query.select([{ $field: 'address' }])
.groupBy(groupByExpr)
.having(having)
.build()
console.log(result)
SELECT address
FROM `travel-sample`
GROUP BY COUNT(amount) AS amount
HAVING address LIKE '%58%'

Parameters

NameType
valueLogicalWhereExpr<any>

Returns

Query

Defined in

query/query.ts:563


index

index(type, name): Query

Add index type and name to INDEX clause.

Method

Example

const result = new Query({}, 'travel-sample')
.index('DROP', 'travel_sample_id_test')
.build();
console.log(result)
DROP INDEX `travel-sample`.`travel_sample_id_test`

Parameters

NameType
typeIndexType
namestring

Returns

Query

Defined in

query/query.ts:173


let

let(value): Query

Add LET expression to SELECT clause.

Method

Example

// SELECT expression definition
const selectExpr = 't1.airportname, t1.geo.lat, t1.geo.lon, t1.city, t1.type';

// LET expression definition
const letExpr: LetExprType = {
min_lat: 71,
max_lat: 'ABS(t1.geo.lon)*4+1',
place: '(SELECT RAW t2.country FROM `travel-sample` t2 WHERE t2.type = "landmark")',
};

// WHERE expression definition
const whereExpr: LogicalWhereExpr = {
$and: [
{ 't1.type': 'airport' },
{ 't1.geo.lat': { $gt: { $field: 'min_lat' } } },
{ 't1.geo.lat': { $lt: { $field: 'max_lat' } } },
{ 't1.country': { $in: { $field: 'place' } } },
],
};

// QUERY creation
const query = new Query({}, 'travel-sample t1')
.select(selectExpr)
.let(letExpr)
.where(whereExpr)
.build();

console.log(query);

// QUERY output
SELECT t1.airportname,
t1.geo.lat,
t1.geo.lon,
t1.city,
t1.type
FROM `travel-sample` t1
LET min_lat=71,
max_lat=ABS(t1.geo.lon)*4+1,
place=(
SELECT RAW t2.country
FROM `travel-sample` t2
WHERE t2.type = "landmark")
WHERE (t1.type="airport"
AND t1.geo.lat>min_lat
AND t1.geo.lat<max_lat
AND t1.country IN place);
// OTTOMAN initialization
const ottoman = getDefaultInstance();
await startInTest(ottoman);

// QUERY execution
const { rows } = await ottoman.query(query);

// RESULTS
console.log(rows)
[
{
airportname: 'Wiley Post Will Rogers Mem',
city: 'Barrow',
lat: 71.285446,
lon: -156.766003,
type: 'airport',
},
{
airportname: 'Dillant Hopkins Airport',
city: 'Keene',
lat: 72.270833,
lon: 42.898333,
type: 'airport',
},
]

Parameters

NameType
valueLetExprType

Returns

Query

Overrides

BaseQuery.let

Defined in

query/query.ts:483


letting

letting(value): Query

Add LETTING expression to GROUP BY clause.

Method

Example

const groupByExpr = [{ expr: 'COUNT(amount_val)', as: 'amount' }];
const letExpr = { amount_val: 10 };
const query = new Query({}, 'travel-sample');
const result = query.select([{ $field: 'address' }])
.groupBy(groupByExpr)
.let(letExpr)
.build()
console.log(result)
SELECT address
FROM `travel-sample`
GROUP BY COUNT(amount) AS amount LETTING amount = 10

Parameters

NameType
valueLetExprType

Returns

Query

Defined in

query/query.ts:535


limit

limit(value): Query

Add LIMIT expression to SELECT clause.

Method

Example

const query = new Query({}, 'travel-sample');
const result = query.select([{ $field: 'address' }])
.limit(10)
.build()
console.log(result)
SELECT address
FROM `travel-sample`
LIMIT 10

Parameters

NameType
valuenumber

Returns

Query

Overrides

BaseQuery.limit

Defined in

query/query.ts:366


offset

offset(value): Query

Add OFFSET expression to SELECT clause.

Method

Example

const query = new Query({}, 'travel-sample');
const result = query.select([{ $field: 'address' }])
.offset(10)
.build()
console.log(result)
SELECT address
FROM `travel-sample`
OFFSET 10

Parameters

NameType
valuenumber

Returns

Query

Overrides

BaseQuery.offset

Defined in

query/query.ts:390


on

on(value): Query

Add items to ON clause in INDEX clause.

Method

Example

const on = [{ name: 'travel-sample.callsing', sort: 'ASC' }];
const result = new Query({}, 'travel-sample')
.index('CREATE', 'travel_sample_id_test')
.on(on)
.build();
console.log(result)
CREATE INDEX `travel_sample_id_test` ON `travel-sample` (`travel-sample.callsing`['ASC'])

Parameters

NameType
valueIIndexOnParams[]

Returns

Query

Defined in

query/query.ts:206


orderBy

orderBy(value): Query

Add ORDER BY expression to SELECT clause.

Method

Example

const query = new Query({}, 'travel-sample');
const result = query.select([{ $field: 'address' }])
.orderBy({ size: 'DESC' })
.build()
console.log(result)
SELECT address
FROM `travel-sample`
ORDER BY size = 'DESC'

Parameters

NameType
valueRecord<string, SortType>

Returns

Query

Overrides

BaseQuery.orderBy

Defined in

query/query.ts:342


plainJoin

plainJoin(value): Query

Add JOIN expression to SELECT clause.

Method

Example

const query = new Query({}, 'beer-sample brewery');
const result = query.select([{ $field: 'address' }])
.plainJoin(
'JOIN `beer-sample` beer ON beer.brewery_id = LOWER(REPLACE(brewery.name, " ", "_"))'
)
.build()
console.log(result)
SELECT beer.name
FROM `beer-sample` brewery
JOIN `beer-sample` beer ON beer.brewery_id = LOWER(REPLACE(brewery.name, " ", "_"))

Parameters

NameType
valuestring

Returns

Query

Defined in

query/query.ts:318


select

select(value?): Query

Add result selectors to SELECT clause.

Method

Example

const query = new Query({}, 'travel-sample');
const result = query.select([{ $field: 'address' }]).build()
console.log(result)
SELECT address
FROM `travel-sample`

Parameters

NameType
value?string | ISelectType[]

Returns

Query

Overrides

BaseQuery.select

Defined in

query/query.ts:141


useKeys

useKeys(value): Query

Add USE KEYS expression to SELECT clause.

Method

Example

const query = new Query({}, 'travel-sample');
const result = query.select([{ $field: 'address' }])
.useKeys(['airlineR_8093'])
.build()
console.log(result)
SELECT address
FROM `travel-sample` USE KEYS ['airlineR_8093']

Parameters

NameType
valuestring[]

Returns

Query

Overrides

BaseQuery.useKeys

Defined in

query/query.ts:586


usingGSI

usingGSI(): Query

Create INDEX using General Secondary Index (GSI).

Method

Example

const result = new Query({}, 'travel-sample')
.index('CREATE', 'travel_sample_id_test')
.usingGSI()
.build();
console.log(result)
CREATE INDEX `travel_sample_id_test` USING GSI

Returns

Query

Defined in

query/query.ts:231


where

where(value): Query

Add WHERE expression to SELECT clause.

Method

Example

const expr_where = {
$or: [
{ address: { $like: '%57-59%' } },
{ free_breakfast: true },
{ name: { $eq: 'John', $ignoreCase: true } }
]
};
const query = new Query({}, 'travel-sample');
const result = query.select([{ $field: 'address' }])
.where(expr_where)
.build()
console.log(result)
SELECT address
FROM `travel-sample`
WHERE (address LIKE "%57-59%" OR free_breakfast = true OR (LOWER(name) = LOWER("John")))

Parameters

NameType
valueLogicalWhereExpr<any>

Returns

Query

Overrides

BaseQuery.where

Defined in

query/query.ts:292


with

with(value): Query

Add items to WITH clause in INDEX clause.

Method

Example

const withExpr = { nodes: ['192.168.1.1:8078'], defer_build: true, num_replica: 2 };
const result = new Query({}, 'travel-sample')
.index('CREATE', 'travel_sample_id_test')
.with(withExpr)
.build();
console.log(result)
CREATE INDEX `travel_sample_id_test`
WITH {'nodes': ['192.168.1.1:8078'],'defer_build': true,'num_replica': 2}

Parameters

NameType
valueIIndexWithParams

Returns

Query

Defined in

query/query.ts:258