Skip to content

Commit

Permalink
select from multiple tables (#91)
Browse files Browse the repository at this point in the history
* select from multiple objects

* validate select from multiple entities

* update multiple tables testing

* 2.13.0

* update codacy config

* update codacy config

* remove unused codacy config

* restore codacy config
  • Loading branch information
kbarbounakis authored Jul 5, 2024
1 parent d5a2adf commit 26dd7f0
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 99 deletions.
7 changes: 4 additions & 3 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
engines:
duplication:
enabled: true
exclude_paths:
- "docs/**"
- "spec/**/*"
- "spec/**"
exclude_paths:
- "docs/**"
- "spec/**/*"
- ".github/**/*"
- "spec/**"
- ".github/**"
- "babel.config.js"
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@themost/query",
"version": "2.12.0",
"version": "2.13.0",
"description": "MOST Web Framework Codename ZeroGravity - Query Module",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
154 changes: 154 additions & 0 deletions spec/QueryExpression.selectFrom.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { QueryEntity, QueryExpression, QueryField } from '../src/index';
import { MemoryAdapter } from './test/TestMemoryAdapter';
import { MemoryFormatter } from './test/TestMemoryFormatter';

describe('QueryExpression.from', () => {

/**
* @type {MemoryAdapter}
*/
let db;
beforeAll(() => {
db = new MemoryAdapter();
});
afterAll(async () => {
if (db) {
await db.closeAsync();
}
});
it('should use select from multiple tables', async () => {
const orders = await db.executeAsync(new QueryExpression().select('id').from('OrderData'));
const q = new QueryExpression().select(
new QueryField('id').from('OrderData'),
new QueryField('customer').from('OrderData'),
new QueryField('familyName').from('PersonData'),
new QueryField('givenName').from('PersonData'),
).from('OrderData', 'PersonData').where(
new QueryField('customer').from('OrderData')
).equal(
new QueryField('id').from('PersonData')
);
expect(Array.isArray(q.$additionalSelect)).toBeTruthy();
const [additionalSelect] = q.$additionalSelect;
expect(additionalSelect instanceof QueryEntity).toBeTruthy();
const sql = new MemoryFormatter().format(q);
expect(sql).toBe('SELECT `OrderData`.`id` AS `id`, `OrderData`.`customer` AS `customer`, `PersonData`.`familyName` AS `familyName`, `PersonData`.`givenName` AS `givenName` FROM `OrderData`, `PersonData` WHERE (`OrderData`.`customer`=`PersonData`.`id`)');
const items = await db.executeAsync(q);
expect(items.length).toEqual(orders.length);
for (let i = 0; i < items.length; i++) {
const item = items[i];
expect(item.familyName).toBeTruthy();
expect(orders.find((x) => x.id === item.id)).toBeTruthy();
}
});

it('should use multiple "from" expressions', async () => {
const orders = await db.executeAsync(new QueryExpression().select('id').from('OrderData'));

const people = new QueryExpression()
.select('id', 'familyName', 'givenName').from('PersonData')
.as('People');

const q = new QueryExpression().select(
new QueryField('id').from('OrderData'),
new QueryField('customer').from('OrderData'),
new QueryField('familyName').from('People'),
new QueryField('givenName').from('People'),
).from('OrderData', people).where(
new QueryField('customer').from('OrderData')
).equal(
new QueryField('id').from('People')
);
expect(Array.isArray(q.$additionalSelect)).toBeTruthy();
const [additionalSelect] = q.$additionalSelect;
expect(additionalSelect instanceof QueryExpression).toBeTruthy();
const items = await db.executeAsync(q);
expect(items.length).toEqual(orders.length);
for (let i = 0; i < items.length; i++) {
const item = items[i];
expect(item.familyName).toBeTruthy();
expect(orders.find((x) => x.id === item.id)).toBeTruthy();
}
});

it('should use multiple "from" expressions with closures', async () => {
const orders = new QueryEntity('OrderData');
const people = new QueryEntity('PersonData');
const q = new QueryExpression().select(
(x, y) => {
return {
id: x.id,
customer: x.customer,
familyName: y.familyName,
givenName: y.givenName
}
}, people // pass additional entity as query parameter
).from(orders, people) // add multiple entities
.where(
(x, y) => {
return x.customer === y.id;
}, people // pass additional entity as query parameter
);
expect(Array.isArray(q.$additionalSelect)).toBeTruthy();
const items = await db.executeAsync(q);
for (let i = 0; i < items.length; i++) {
const item = items[i];
expect(item.familyName).toBeTruthy();
}
});

it('should use multiple "from" expressions with object destructuring', async () => {
const orders = new QueryEntity('OrderData');
const people = new QueryEntity('PersonData');
const q = new QueryExpression().select(
({id, customer}, {familyName, givenName}) => {
return {
id,
customer,
familyName,
givenName
}
}, people // pass additional entity as query parameter
).from(orders, people) // add multiple entities
.where(
(x, {id}) => {
return x.customer === id;
}, people // pass additional entity as query parameter
);
expect(Array.isArray(q.$additionalSelect)).toBeTruthy();
const items = await db.executeAsync(q);
for (let i = 0; i < items.length; i++) {
const item = items[i];
expect(item.familyName).toBeTruthy();
}
});

it('should use multiple "from" expressions with full object destructuring', async () => {
const orders = new QueryEntity('OrderData');
const people = new QueryEntity('PersonData');
const q = new QueryExpression().select(
({id, customer}, {familyName, givenName}) => {
return {
id,
customer,
familyName,
givenName
}
}, people // pass additional entity as query parameter
).from(orders, people) // add multiple entities
.where(
({customer}, {id}) => {
return customer === id;
}, people // pass additional entity as query parameter
).orderBy(
({customer}) => customer
);
expect(Array.isArray(q.$additionalSelect)).toBeTruthy();
const items = await db.executeAsync(q);
for (let i = 0; i < items.length; i++) {
const item = items[i];
expect(item.familyName).toBeTruthy();
}
});

});
Loading

0 comments on commit 26dd7f0

Please sign in to comment.