-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement $jsonArray() dialect (#108)
* implement $jsonArray() dialect * use jsonArray() for selecting values * validate $jsonArray dialect * serialize array of objects * update tests * 2.9.2
- Loading branch information
1 parent
c918edf
commit ec4b7c2
Showing
8 changed files
with
246 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,9 @@ async function createSimpleOrders(db) { | |
const { source } = SimpleOrderSchema; | ||
const exists = await db.table(source).existsAsync(); | ||
if (!exists) { | ||
await db.table(source).createAsync(SimpleOrderSchema.fields); | ||
await db.table(source).createAsync(SimpleOrderSchema.fields); | ||
} else { | ||
return; | ||
} | ||
// get some orders | ||
const orders = await db.executeAsync( | ||
|
@@ -61,7 +63,19 @@ async function createSimpleOrders(db) { | |
return {id, streetAddress, postalCode, addressLocality, addressCountry, telephone }; | ||
}), [] | ||
); | ||
// get | ||
|
||
const shuffleArray = (array) => { | ||
for (let i = array.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[array[i], array[j]] = [array[j], array[i]]; | ||
} | ||
return array; | ||
}; | ||
|
||
const getRandomItems = (array, numItems) => { | ||
const shuffledArray = shuffleArray([...array]); | ||
return shuffledArray.slice(0, numItems); | ||
}; | ||
const items = orders.map((order) => { | ||
const { orderDate, discount, discountCode, orderNumber, paymentDue, | ||
dateCreated, dateModified, createdBy, modifiedBy } = order; | ||
|
@@ -73,6 +87,8 @@ async function createSimpleOrders(db) { | |
customer.address = postalAddresses.find((x) => x.id === customer.address); | ||
delete customer.address?.id; | ||
} | ||
// get 2 random payment methods | ||
const additionalPaymentMethods = getRandomItems(paymentMethods, 2); | ||
return { | ||
orderDate, | ||
discount, | ||
|
@@ -82,6 +98,7 @@ async function createSimpleOrders(db) { | |
orderStatus, | ||
orderedItem, | ||
paymentMethod, | ||
additionalPaymentMethods, | ||
customer, | ||
dateCreated, | ||
dateModified, | ||
|
@@ -601,4 +618,98 @@ describe('SqlFormatter', () => { | |
} | ||
}); | ||
|
||
|
||
it('should return json arrays', async () => { | ||
// set context user | ||
context.user = { | ||
name: '[email protected]' | ||
}; | ||
|
||
const queryPeople = context.model('Person').asQueryable().select( | ||
'id', 'familyName', 'givenName', 'jobTitle', 'email' | ||
).flatten(); | ||
await beforeExecuteAsync({ | ||
model: queryPeople.model, | ||
emitter: queryPeople, | ||
query: queryPeople.query, | ||
}); | ||
const { viewAdapter: People } = queryPeople.model; | ||
const queryOrders = context.model('Order').asQueryable().select( | ||
'id', 'orderDate', 'orderStatus', 'orderedItem', 'customer' | ||
).flatten(); | ||
const { viewAdapter: Orders } = queryOrders.model; | ||
// prepare query for each customer | ||
queryOrders.query.where( | ||
new QueryField('customer').from(Orders) | ||
).equal( | ||
new QueryField('id').from(People) | ||
); | ||
const selectPeople = queryPeople.query.$select[People]; | ||
// add orders as json array | ||
selectPeople.push({ | ||
orders: { | ||
$jsonArray: [ | ||
queryOrders.query | ||
] | ||
} | ||
}); | ||
const start= new Date().getTime(); | ||
const items = await queryPeople.take(50).getItems(); | ||
const end = new Date().getTime(); | ||
TraceUtils.log('Elapsed time: ' + (end-start) + 'ms'); | ||
expect(items.length).toBeTruthy(); | ||
for (const item of items) { | ||
expect(Array.isArray(item.orders)).toBeTruthy(); | ||
for (const order of item.orders) { | ||
expect(order.customer).toEqual(item.id); | ||
} | ||
|
||
} | ||
}); | ||
|
||
it('should parse string as json array', async () => { | ||
// set context user | ||
context.user = { | ||
name: '[email protected]' | ||
}; | ||
const { viewAdapter: People } = context.model('Person'); | ||
const query = new QueryExpression().select( | ||
'id', 'familyName', 'givenName', 'jobTitle', 'email', | ||
new QueryField({ | ||
tags: { | ||
$jsonArray: [ | ||
new QueryField({ | ||
$value: '[ "user", "customer", "admin" ]' | ||
}) | ||
] | ||
} | ||
}) | ||
).from(People).where('email').equal(context.user.name); | ||
const [item] = await context.db.executeAsync(query); | ||
expect(item).toBeTruthy(); | ||
}); | ||
|
||
it('should parse array as json array', async () => { | ||
// set context user | ||
context.user = { | ||
name: '[email protected]' | ||
}; | ||
const { viewAdapter: People } = context.model('Person'); | ||
const query = new QueryExpression().select( | ||
'id', 'familyName', 'givenName', 'jobTitle', 'email', | ||
new QueryField({ | ||
tags: { | ||
$jsonArray: [ | ||
{ | ||
$value: [ 'user', 'customer', 'admin' ] | ||
} | ||
] | ||
} | ||
}) | ||
).from(People).where('email').equal(context.user.name); | ||
const [item] = await context.db.executeAsync(query); | ||
expect(item).toBeTruthy(); | ||
expect(Array.isArray(item.tags)).toBeTruthy(); | ||
expect(item.tags).toEqual([ 'user', 'customer', 'admin' ]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import isPlainObject from 'lodash/isPlainObject'; | ||
import isObjectLike from 'lodash/isObjectLike'; | ||
import isNative from 'lodash/isNative'; | ||
|
||
const objectToString = Function.prototype.toString.call(Object); | ||
|
||
function isObjectDeep(any) { | ||
// check if it is a plain object | ||
let result = isPlainObject(any); | ||
if (result) { | ||
return result; | ||
} | ||
// check if it's object | ||
if (isObjectLike(any) === false) { | ||
return false; | ||
} | ||
// get prototype | ||
let proto = Object.getPrototypeOf(any); | ||
// if prototype exists, try to validate prototype recursively | ||
while(proto != null) { | ||
// get constructor | ||
const Ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') | ||
&& proto.constructor; | ||
// check if constructor is native object constructor | ||
result = (typeof Ctor == 'function') && (Ctor instanceof Ctor) | ||
&& Function.prototype.toString.call(Ctor) === objectToString; | ||
// if constructor is not object constructor and belongs to a native class | ||
if (result === false && isNative(Ctor) === true) { | ||
// return false | ||
return false; | ||
} | ||
// otherwise. get parent prototype and continue | ||
proto = Object.getPrototypeOf(proto); | ||
} | ||
// finally, return result | ||
return result; | ||
} | ||
|
||
export { | ||
isObjectDeep | ||
} |