Add Indexes to Schema API#4240
Conversation
Codecov Report
@@ Coverage Diff @@
## 3.x #4240 +/- ##
==========================================
+ Coverage 92.45% 92.57% +0.12%
==========================================
Files 118 118
Lines 8218 8341 +123
==========================================
+ Hits 7598 7722 +124
+ Misses 620 619 -1
Continue to review full report at Codecov.
|
|
@vitaly-t Can you tell me if I used connections correctly on Postgres? |
| } | ||
|
|
||
| getIndexes(className) { | ||
| const qs = `SELECT * FROM pg_indexes WHERE tablename = '${className}'`; |
There was a problem hiding this comment.
Do not use ES6 strings to format values, replace this with:
const qs = 'SELECT * FROM pg_indexes WHERE tablename = ${className}';
return this._client.any(qs, {className});There was a problem hiding this comment.
This is just one example, u have other places that can be improved the same ;)
|
|
||
| dropIndexes(className, indexes, conn) { | ||
| conn = conn || this._client; | ||
| return conn.tx(t => { |
There was a problem hiding this comment.
Much simpler code:
return conn.tx(t => t.batch(indexes.map(i => t.none('DROP INDEX $1', i))));|
@dplewis it appears to be correct, at first sight anyway. I left some comments there for you, as examples of:
|
|
@vitaly-t Thanks a lot. Why is it dangerous to use ES6 Strings? |
|
@dplewis ES6 strings know nothing about how to correctly escape values for PostgreSQL, only the |
|
@dplewis worth noting that some of the code there can probably be improved due to the following recent update in the driver: https://github.com/vitaly-t/pg-promise/releases/tag/v.6.10.0 |
|
@vitaly-t I know the formatting but I have a bad habit of not using |
|
Agreed, it is a bad habit :) |
|
@flovilmart can you look this over? |
|
Kinda busy today, gotta have a look tomorrow :) |
| } | ||
|
|
||
| createIndexes(className, indexes, conn) { | ||
| conn = conn || this._client; |
There was a problem hiding this comment.
Better implementation:
return (conn || this._client).tx(t => t.batch(indexes.map(i => {
return t.none(`CREATE INDEX $1:name ON $2:name($3:name)`, [i.name, className, i.key]);
})));There was a problem hiding this comment.
Note that filter :name is very smart...
Filter :name, for SQL Names, has a special logic to it, and it can be:
- a simple text string, which is normal, to represent a single column name
- string
*(asterisks), which is detected automatically - array of strings, in which case it is considered a list of column names
- any object, to pull all property names automatically.
This is why in our case we can simply pass in an object, and :name will automatically pull all property names from it.
There was a problem hiding this comment.
I didn't know about the property names. Thats super helpful.
I really should read the pg-promise documentation again
|
I might do another round of review after this PR is merged along with the |
|
@dplewis quick question, does it reads/loads all existing indexes? |
|
@flovilmart No, only if someone has a compound index existing for full text search. Should we load all indexes? |
|
That would be neat, this way we bridge fully compared to the schema don't you think? |
|
I agree. Should we get the indexes on initialization? |
|
Seems like a good idea! |
montymxb
left a comment
There was a problem hiding this comment.
Much better with the history, this looks good now!
Before I give this a thumbs-up, and I'm fairly certain, but this does load up pre-existing indexes, compound or not, on startup right? I'm pretty sure that it got added in but I wanted to be clear that it is present & accounted for.
|
@montymxb Indexes are available on startup but they aren't transformed. I'll have to add a transform for every index. |
montymxb
left a comment
There was a problem hiding this comment.
Alright, @flovilmart we're ready here. Any thoughts before we merge this?
|
I found a few issues I’ll submit a fix for shortly |
|
Awesome thanks @dplewis ! |
|
@dplewis just marking this as WIP until you finish the stuff you're working on . |
spec/schemas.spec.js
Outdated
| headers: masterKeyHeaders, | ||
| json: true, | ||
| }, (error, response, body) => { | ||
| console.log(body); |
There was a problem hiding this comment.
log here should be removed
| } | ||
|
|
||
| find(className, schema, query, { skip, limit, sort, keys }) { | ||
| console.log('find'); |
|
@flovilmart I added a fix for #4212 Also the indexes saved and returned from mongo are in the same format. |
|
Alright let's go with it!. |
This reverts commit d5c4324.
* Add Indexes to Schema API * error handling * ci errors * postgres support * full text compound indexes * pg clean up * get indexes on startup * test compound index on startup * add default _id to index, full Text index on startup * lint * fix test
|
@flovilmart something up here? |
|
Ir’s Merged :) |
* Add Indexes to Schema API * error handling * ci errors * postgres support * full text compound indexes * pg clean up * get indexes on startup * test compound index on startup * add default _id to index, full Text index on startup * lint * fix test
Adds an optional indexes field to the Schema API.
Proposal: #4212 (comment)
Just a first pass though to make sure we're on the right track.
Thoughts?