Skip to content

Commit

Permalink
Added example of querying JSON field with dot
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekel Barzilay committed Dec 27, 2019
1 parent c2c084d commit 217d8f1
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 255 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ app.service('companies').find({
arr: { '(0).objectField.object': 'string in arr[0].objectField.object' }
}
});

app.service('companies').find({
query: {
obj: { "(field.WithDot)": 'string' }
}
});
```
### Graph upsert
Expand Down
2 changes: 1 addition & 1 deletion example/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import feathers from '@feathersjs/feathers'
import express from '@feathersjs/express'
import rest from '@feathersjs/express/rest'
import errorHandler from '@feathersjs/express/errors'
import { errorHandler } from '@feathersjs/express'
import bodyParser from 'body-parser'
import { Model } from 'objection'
import createService from '../src'
Expand Down
9 changes: 8 additions & 1 deletion example/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ class Todo extends Model {
properties: {
id: { type: 'integer' },
text: { type: 'string' },
complete: { type: 'boolean', default: false }
complete: { type: 'boolean', default: false },
metadata: {
type: ['object', 'null'],
properties: {
'written.by': { type: ['string', 'null'] }
}
}
},
options: {
timestamps: true
Expand Down Expand Up @@ -43,6 +49,7 @@ module.exports = function (app) {
table.increments('id')
table.string('text')
table.boolean('complete')
table.json('metadata')
table.timestamp('createdAt')
table.timestamp('updatedAt')
})
Expand Down
543 changes: 294 additions & 249 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "feathers-objection",
"description": "A service plugin for ObjectionJS an ORM based on KnexJS",
"version": "4.6.4",
"version": "4.6.5",
"homepage": "https://github.com/feathersjs-ecosystem/feathers-objection",
"keywords": [
"feathers",
Expand Down Expand Up @@ -65,6 +65,7 @@
"devDependencies": {
"@babel/cli": "^7.7.7",
"@babel/core": "^7.7.7",
"@babel/node": "^7.7.7",
"@babel/preset-env": "^7.7.7",
"@babel/register": "^7.7.7",
"@feathersjs/adapter-tests": "^4.4.3",
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ class Service extends AdapterService {
if (!methodKey && key[0] === '$') {
refColumn = ref(`${this.Model.tableName}.${column}`);
} else {
refColumn = ref(`${this.Model.tableName}.${methodKey || column}:${(methodKey ? column : key).replace(/\(/g, '[').replace(/\)/g, ']')}`);
const prop = (methodKey ? column : key)
.replace(/\(/g, '[')
.replace(/\)/g, ']');

refColumn = ref(`${this.Model.tableName}.${methodKey || column}:${prop}`);
}

if (operator === '@>') {
Expand Down
3 changes: 2 additions & 1 deletion test/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export default class Company extends Model {
properties: {
object: { type: 'string' }
}
}
},
'first.founder': { type: ['string', 'null'] }
}
},
jsonArray: { type: ['array', 'null'] },
Expand Down
25 changes: 24 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,8 @@ describe('Feathers Objection Service', () => {
numberField: 1.5,
objectField: {
object: 'string in jsonObject.objectField.object'
}
},
'first.founder': 'John'
},
jsonArray: [
{
Expand All @@ -1267,6 +1268,10 @@ describe('Feathers Objection Service', () => {
]);
});

after(async () => {
await companies.remove(null);
});

it('object', () => {
return companies.find({ query: { jsonObject: { $ne: null } } }).then(data => {
expect(data[0].jsonObject.stringField).to.equal('string');
Expand Down Expand Up @@ -1320,6 +1325,18 @@ describe('Feathers Objection Service', () => {
expect(data[0].jsonArray[0].objectField.object).to.equal('I\'m string in jsonArray[0].objectField.object');
});
});

it('dot in property name', () => {
return companies.find({ query: { jsonObject: { '(first.founder)': 'John' } } }).then(data => {
expect(data[0].jsonObject['first.founder']).to.equal('John');
});
});

it('dot in property name with brackets', () => {
return companies.find({ query: { jsonObject: { '[first.founder]': 'John' } } }).then(data => {
expect(data[0].jsonObject['first.founder']).to.equal('John');
});
});
});

describe.skip('JSON operators (Postgres)', () => {
Expand Down Expand Up @@ -1464,6 +1481,12 @@ describe('Feathers Objection Service', () => {
]);
});

afterEach(async () => {
try {
await companies.remove(null);
} catch (err) {}
});

it('create with $noSelect', () => {
return companies.create({
name: 'Apple',
Expand Down

0 comments on commit 217d8f1

Please sign in to comment.