
Security News
MCP Steering Committee Launches Official MCP Registry in Preview
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.
loopback-n1ql-mixin
Advanced tools
Loopback Couchbase N1QL mixin
yarn add loopback-n1ql-mixin
Modify server/model-config.json
as following:
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"../node_modules/loopback-n1ql-mixin/lib",
"../common/mixins"
]
}
}
Add N1ql
into model config as following:
{
"name": "Book",
"properties": {
"name": {
"type": "string",
}
},
"mixins": {
"N1ql" : true
}
}
The mixin support create the primary index and specifc index that is defined in model definition json.
A example:
{
"name": "Example",
"base": "PersistedModel",
"mixins": {
"N1ql": {
"primary": false,
"drop": false,
"deferred": true
}
},
"indexes": {
"status_type": {
"keys": {
"type": 1,
"_type": 1,
"status": 1,
"createdAt": 1
}
},
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
Warning: Indexes will not be automatically created or updated for you. You must run autoupdate
to create/update indexes!
primary
: Create primary index, default: false.
Avoid Primary Keys in Production — CouchBase Index Best Practices
drop
: Drop old same name index, default: false.
If drop is false, the autoupdate will never update the index even you have changed the index fields
deferred
: Create index or primary index in defer queue.
With defer_build set to TRUE, then the CREATE INDEX operation queues the task for building the index but immediately pauses the building of the index of type GSI. Index building requires an expensive scan operation. Deferring building of the index with multiple indexes can optimize the expensive scan operation. Admins can defer building multiple indexes and, using the BUILD INDEX statement, multiple indexes to be built efficiently with one efficient scan of bucket data.
An known issue: The index may keep in created
status without any progress. You have to execute build index to kick off the building process.
const books = await Book.query({ where: { name: { like: '%For%' } }});
assert books[0].name === 'For bar';
const total = await Book.sum({ where: { name: { like: '%For%' } }});
assert total === 10;
const books = await Book.query({ where: { name: { like: '%For%' } }}, { index: 'name_createdAt_index'});
assert books[0].name === 'For bar';
The query
and count
accept the loopback filter parameter. Check the loopback filter doc. **Not all the filters are supportted.**The support query approachings as following.
filter | N1QL |
---|---|
Where Basic Filter | ✔️ |
AND & OR operator | ✔️ |
Range Query | ✔️ |
inq | ✔️ |
near | ✖️ |
not like | ✔️ |
regexp | ✔️* |
Like Query | ✔️ |
Limit Filter | ✔️ |
Skip Filter | ✔️ |
Order Filter | ✔️ |
Include Filter | Not yet |
Fields Filter | ✔️ |
Node API | ✔️ |
REST API | ✔️ |
SQL Inject | Safe * |
You may need a specific array filter for the array fields as follows:
{
"type": "shoes",
"materials": [ 991, 100]
}
You can use such a filter to find shoes who has 100
materials as follows:
Shoes.query({
where: {
"materials": {
"array_contains": 100
}
}
})
Couchbase Support a nested document. There will be a case, when the document looks like as follows:
{
"type": "book",
"name": "Galaxy Express 999",
"tags": [{
"name": "sf",
"id": 001
}, {
"name": "galaxy",
"id": 991
}]
}
You have to use any filter
to access the document. The filter will be like as follows:
Book.query({
where: {
"tags.*.id": "001"
}
})
Base on this post implemented a fuzzy search filter on text field.
First of all, you need to create a xlike index as follows:
{
"indexes": {
"status_type": {
"keys": {
"title": "xlike"
}
},
}
}
Then enjoy the xlike as follows:
Book.query({
where: {
title: {
xlike: 'galaxy'
}
}
})
Check out FAQ when you meet any issue.
Person.query({ where: { name: { regexp: '^C+.*' } } });
Person.query({ where: { name: { regexp: /^C+.*/ } } });
SQL injection: The n1ql is generated via concatenating the string. But the parameters do not include in the query. The parameters will be escaped by CouchBase itself. For the reason, it's free from SQL injection.
Before 1.1.2, this lib was using named parameters for query, which we've observed significant impact on the query execute response time, thus it will be dropped in future version.
To prevent n1ql injection, we are following the guidelines in https://blog.couchbase.com/couchbase-and-n1ql-security-centeredgesoftware/
Only ready
status index can be used.
FAQs
A mixin to use n1ql(couchbase) via loopback filter
We found that loopback-n1ql-mixin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.
Product
Socket’s new Pull Request Stories give security teams clear visibility into dependency risks and outcomes across scanned pull requests.
Research
/Security News
npm author Qix’s account was compromised, with malicious versions of popular packages like chalk-template, color-convert, and strip-ansi published.