@nexes/mongo-knex
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -234,3 +234,3 @@ const _ = require('lodash'); | ||
let innerJoinValue = reference.config.tableName; | ||
let innerJoinOn = `${reference.config.tableName}.id`; | ||
let innerJoinOn = `${reference.config.tableName}.${reference.config.joinToForeign || 'id'}`; | ||
@@ -240,8 +240,14 @@ // CASE: you can define a name for the join table | ||
innerJoinValue = `${reference.config.tableName} as ${reference.config.tableNameAs}`; | ||
innerJoinOn = `${reference.config.tableNameAs}.id`; | ||
innerJoinOn = `${reference.config.tableNameAs}.${reference.config.joinToForeign || 'id'}`; | ||
} | ||
const innerQB = this | ||
let query = this | ||
.select(`${reference.config.joinTable}.${reference.config.joinFrom}`) | ||
.from(`${reference.config.joinTable}`) | ||
.from(`${reference.config.joinTable}`); | ||
if (reference.config.virtualTable) { | ||
query = query.with(reference.config.tableName, reference.config.virtualTableDefinition); | ||
} | ||
const innerQB = query | ||
.innerJoin(innerJoinValue, function () { | ||
@@ -322,5 +328,11 @@ this.on(innerJoinOn, '=', `${reference.config.joinTable}.${reference.config.joinTo}`); | ||
const innerQB = this | ||
let query = this | ||
.select(`${tableName}.id`) | ||
.from(`${tableName}`) | ||
.from(`${tableName}`); | ||
if (reference.config.virtualTable) { | ||
query = query.with(reference.config.tableName, reference.config.virtualTableDefinition); | ||
} | ||
const innerQB = query | ||
.leftJoin(innerJoinValue, function () { | ||
@@ -327,0 +339,0 @@ this.on(innerJoinOn, '=', `${tableName}.id`); |
{ | ||
"name": "@nexes/mongo-knex", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "tbc", | ||
@@ -5,0 +5,0 @@ "repository": "git@github.com:NexesJS/mongo-knex.git", |
@@ -32,2 +32,26 @@ const _ = require('lodash'); | ||
joinFrom: 'post_id' | ||
}, | ||
views: { | ||
tableName: 'views', | ||
type: 'oneToOne', | ||
joinFrom: 'post_id', | ||
virtualTable: true, | ||
virtualTableDefinition: knex.raw(` | ||
SELECT | ||
posts_view_events.post_id, | ||
SUM(posts_view_events.count) as total | ||
FROM posts | ||
JOIN posts_view_events | ||
ON posts.id = posts_view_events.post_id | ||
GROUP BY | ||
posts.id | ||
`) | ||
}, | ||
comments: { | ||
tableName: 'comments', | ||
type: 'manyToMany', | ||
joinTable: 'posts_comments', | ||
joinFrom: 'post_id', | ||
joinTo: 'comment_id', | ||
joinToForeign: 'comment_provider_id' | ||
} | ||
@@ -49,5 +73,41 @@ } | ||
describe('Virtual Table', function () { | ||
it('Should allow relations with virtual tables', function () { | ||
const mongoJSON = { | ||
'views.total': { | ||
$gt: 10 | ||
} | ||
}; | ||
const query = makeQuery(mongoJSON); | ||
return query | ||
.select() | ||
.then((result) => { | ||
result.should.be.an.Array().with.lengthOf(2); | ||
result.should.matchIds([1, 2]); | ||
}); | ||
}); | ||
}); | ||
describe('Many-to-Many', function () { | ||
before(utils.db.init('many-to-many')); | ||
describe('joinToForeign', function () { | ||
it('Allows you to join an a customer foreign key in the resulting row', function () { | ||
const mongoJSON = { | ||
'comments.content': 'Hello, world' | ||
}; | ||
const query = makeQuery(mongoJSON); | ||
return query | ||
.select() | ||
.then((result) => { | ||
result.should.be.an.Array().with.lengthOf(1); | ||
result.should.matchIds([1]); | ||
}); | ||
}); | ||
}); | ||
describe('EQUALS $eq', function () { | ||
@@ -54,0 +114,0 @@ it('tags.slug equals "animal"', function () { |
@@ -123,2 +123,14 @@ { | ||
], | ||
"comments": [ | ||
{ | ||
"id": 1, | ||
"comment_provider_id": "external_1", | ||
"content": "Hello, world" | ||
}, | ||
{ | ||
"id": 2, | ||
"comment_provider_id": "external_2", | ||
"content": "Learn forex trading today and make millions, only a 1000 USD up front payment! bit.ly/scam" | ||
} | ||
], | ||
"posts_meta": [ | ||
@@ -146,3 +158,59 @@ { | ||
} | ||
], | ||
"posts_view_events": [ | ||
{ | ||
"id": 1, | ||
"post_id": 1, | ||
"count": 2, | ||
"date": "2021-01-01" | ||
}, | ||
{ | ||
"id": 2, | ||
"post_id": 2, | ||
"count": 4, | ||
"date": "2021-01-01" | ||
}, | ||
{ | ||
"id": 3, | ||
"post_id": 3, | ||
"count": 0, | ||
"date": "2021-01-01" | ||
}, | ||
{ | ||
"id": 4, | ||
"post_id": 1, | ||
"count": 6, | ||
"date": "2021-01-02" | ||
}, | ||
{ | ||
"id": 5, | ||
"post_id": 2, | ||
"count": 3, | ||
"date": "2021-01-02" | ||
}, | ||
{ | ||
"id": 6, | ||
"post_id": 3, | ||
"count": 0, | ||
"date": "2021-01-02" | ||
}, | ||
{ | ||
"id": 7, | ||
"post_id": 1, | ||
"count": 5, | ||
"date": "2021-01-03" | ||
}, | ||
{ | ||
"id": 8, | ||
"post_id": 2, | ||
"count": 7, | ||
"date": "2021-01-03" | ||
}, | ||
{ | ||
"id": 9, | ||
"post_id": 3, | ||
"count": 10, | ||
"date": "2021-01-03" | ||
} | ||
] | ||
} |
@@ -25,3 +25,8 @@ { | ||
{"post_id": 8, "author_id": 3, "sort_order": 1} | ||
], | ||
"posts_comments": [ | ||
{"post_id": 1, "comment_id": "external_1"}, | ||
{"post_id": 2, "comment_id": "external_2"} | ||
] | ||
} |
const Promise = require('bluebird'); | ||
const TABLES = [ | ||
'posts_comments', | ||
'comments', | ||
'posts_view_events', | ||
'posts_tags', | ||
@@ -54,2 +57,18 @@ 'posts_authors', | ||
table.integer('sort_order').defaultTo(0); | ||
})) | ||
.then(() => knex.schema.createTable('posts_view_events', (table) => { | ||
table.increments('id').primary(); | ||
table.integer('post_id').unsigned().references('posts.id'); | ||
table.integer('count'); | ||
table.dateTime('date'); | ||
})) | ||
.then(() => knex.schema.createTable('comments', (table) => { | ||
table.increments('id').primary(); | ||
table.string('comment_provider_id'); | ||
table.string('content'); | ||
})) | ||
.then(() => knex.schema.createTable('posts_comments', (table) => { | ||
table.increments('id').primary(); | ||
table.integer('post_id').unsigned().references('posts.id'); | ||
table.string('comment_id').references('comments.comment_provider_id'); | ||
})); | ||
@@ -56,0 +75,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
109055
2682