How can I write a N1QL

Yes, Views are available directly via REST API and SDK.

In general, to build an index into data, the right tools would be GSI (Global Secondary Index) and access path to them would be through Query (N1QL) service.

To write programmatic logic that runs in response to data mutations, or needs to do something based on wallclock time, you should look at Eventing service. https://developer.couchbase.com/documentation/server/5.5/eventing/eventing-overview.html