Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit 217d8f1

Browse files
author
Dekel Barzilay
committed
Added example of querying JSON field with dot
1 parent c2c084d commit 217d8f1

File tree

8 files changed

+342
-255
lines changed

8 files changed

+342
-255
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ app.service('companies').find({
291291
arr: { '(0).objectField.object': 'string in arr[0].objectField.object' }
292292
}
293293
});
294+
295+
app.service('companies').find({
296+
query: {
297+
obj: { "(field.WithDot)": 'string' }
298+
}
299+
});
294300
```
295301
296302
### Graph upsert

example/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import feathers from '@feathersjs/feathers'
22
import express from '@feathersjs/express'
33
import rest from '@feathersjs/express/rest'
4-
import errorHandler from '@feathersjs/express/errors'
4+
import { errorHandler } from '@feathersjs/express'
55
import bodyParser from 'body-parser'
66
import { Model } from 'objection'
77
import createService from '../src'

example/todo.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ class Todo extends Model {
1212
properties: {
1313
id: { type: 'integer' },
1414
text: { type: 'string' },
15-
complete: { type: 'boolean', default: false }
15+
complete: { type: 'boolean', default: false },
16+
metadata: {
17+
type: ['object', 'null'],
18+
properties: {
19+
'written.by': { type: ['string', 'null'] }
20+
}
21+
}
1622
},
1723
options: {
1824
timestamps: true
@@ -43,6 +49,7 @@ module.exports = function (app) {
4349
table.increments('id')
4450
table.string('text')
4551
table.boolean('complete')
52+
table.json('metadata')
4653
table.timestamp('createdAt')
4754
table.timestamp('updatedAt')
4855
})

package-lock.json

Lines changed: 294 additions & 249 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "feathers-objection",
33
"description": "A service plugin for ObjectionJS an ORM based on KnexJS",
4-
"version": "4.6.4",
4+
"version": "4.6.5",
55
"homepage": "https://github.com/feathersjs-ecosystem/feathers-objection",
66
"keywords": [
77
"feathers",
@@ -65,6 +65,7 @@
6565
"devDependencies": {
6666
"@babel/cli": "^7.7.7",
6767
"@babel/core": "^7.7.7",
68+
"@babel/node": "^7.7.7",
6869
"@babel/preset-env": "^7.7.7",
6970
"@babel/register": "^7.7.7",
7071
"@feathersjs/adapter-tests": "^4.4.3",

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ class Service extends AdapterService {
214214
if (!methodKey && key[0] === '$') {
215215
refColumn = ref(`${this.Model.tableName}.${column}`);
216216
} else {
217-
refColumn = ref(`${this.Model.tableName}.${methodKey || column}:${(methodKey ? column : key).replace(/\(/g, '[').replace(/\)/g, ']')}`);
217+
const prop = (methodKey ? column : key)
218+
.replace(/\(/g, '[')
219+
.replace(/\)/g, ']');
220+
221+
refColumn = ref(`${this.Model.tableName}.${methodKey || column}:${prop}`);
218222
}
219223

220224
if (operator === '@>') {

test/company.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export default class Company extends Model {
2222
properties: {
2323
object: { type: 'string' }
2424
}
25-
}
25+
},
26+
'first.founder': { type: ['string', 'null'] }
2627
}
2728
},
2829
jsonArray: { type: ['array', 'null'] },

test/index.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,8 @@ describe('Feathers Objection Service', () => {
12541254
numberField: 1.5,
12551255
objectField: {
12561256
object: 'string in jsonObject.objectField.object'
1257-
}
1257+
},
1258+
'first.founder': 'John'
12581259
},
12591260
jsonArray: [
12601261
{
@@ -1267,6 +1268,10 @@ describe('Feathers Objection Service', () => {
12671268
]);
12681269
});
12691270

1271+
after(async () => {
1272+
await companies.remove(null);
1273+
});
1274+
12701275
it('object', () => {
12711276
return companies.find({ query: { jsonObject: { $ne: null } } }).then(data => {
12721277
expect(data[0].jsonObject.stringField).to.equal('string');
@@ -1320,6 +1325,18 @@ describe('Feathers Objection Service', () => {
13201325
expect(data[0].jsonArray[0].objectField.object).to.equal('I\'m string in jsonArray[0].objectField.object');
13211326
});
13221327
});
1328+
1329+
it('dot in property name', () => {
1330+
return companies.find({ query: { jsonObject: { '(first.founder)': 'John' } } }).then(data => {
1331+
expect(data[0].jsonObject['first.founder']).to.equal('John');
1332+
});
1333+
});
1334+
1335+
it('dot in property name with brackets', () => {
1336+
return companies.find({ query: { jsonObject: { '[first.founder]': 'John' } } }).then(data => {
1337+
expect(data[0].jsonObject['first.founder']).to.equal('John');
1338+
});
1339+
});
13231340
});
13241341

13251342
describe.skip('JSON operators (Postgres)', () => {
@@ -1464,6 +1481,12 @@ describe('Feathers Objection Service', () => {
14641481
]);
14651482
});
14661483

1484+
afterEach(async () => {
1485+
try {
1486+
await companies.remove(null);
1487+
} catch (err) {}
1488+
});
1489+
14671490
it('create with $noSelect', () => {
14681491
return companies.create({
14691492
name: 'Apple',

0 commit comments

Comments
 (0)