Couchbase lite 2.0: how to query with an n1ql string

Hi, I’m using couchbase lite 2.0 for android, and I would like to query the database with n1ql queries as text, similar to what the java SDK permits mee to do:
N1qlQuery query = N1qlQuery.simple(statement);

At the moment I don’t see that possibility. Am I missing something or is this on the roadmap?

That’s not supported. Both because it would require reimplementing the N1QL parser, and to make the product more supportable — because the type-checked API eliminates lots of N1QL syntax errors and other mistakes (which often result in support tickets from customers.)

I understand, I’ve written a cordova plugin to use the new couchbase lite 2.0, so this will limit the queries we can do with it…

Under the hood, CBL generates the query in a JSON format and passes that to our query engine. It would be pretty ideal for a JavaScript API to just use that format; unfortunately CBL has no public API to create a Query object directly from JSON.

unfortunately CBL has no public API to create a Query object directly from JSON

Are there any plans to support that? It would be really useful for react native plugin I’m working on.

I’ll bring this to the attention of our project manager @priya.rajagopal :slight_smile:

1 Like

@jens This would be really helpful. The only feature currently missing in React Native Couchbase Lite 2 plugin is queries.

Any update on this? Even if we want to use couchbase lite with Kotlin multiplatform ( KMM). string based queries will be very usefull to share the code. otherwise all queries will need be to rewritten in Android ans iOS which should not be the case. And anyway under the hood couchbase anyway uses json to query. so if u can just expose the Query API which can execute from json directly it will be great and probably would not be very big effort.

The bad news is this API isn’t in the upcoming 2.7 release.

The good news is that it’s in Couchbase Lite For C, a version of CBL with a cross-platform pure C API. Instead of porting the entire 2.x query API to C, which would have been a very big task and awkward to use without OOP, it lets you use either the JSON syntax or [a subset of] N1QL.

The not-quite-so-good news is that CBL-C is not yet an official product, although we’ve had a lot of interest from customers. So it’s not finished yet and we don’t provide full support. Plus, of course, the C (and C++) API isn’t as easy to use in Android or iOS apps as Java/Swift/Obj-C are. But it ought to work pretty well for implementing a React or Cordova plugin…