-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 :) |
@@ -1397,6 +1451,30 @@ export class PostgresStorageAdapter { | |||
console.error(error); | |||
}); | |||
} | |||
|
|||
createIndexes(className, indexes, conn) { | |||
conn = conn || this._client; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know about the property names. Thats super helpful.
I really should read the pg-promise
documentation again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might do another round of review after this PR is merged along with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
much needed!
@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! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -2336,8 +2348,12 @@ describe('schemas', () => { | |||
headers: masterKeyHeaders, | |||
json: true, | |||
}, (error, response, body) => { | |||
console.log(body); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log here should be removed
@@ -1251,6 +1254,7 @@ export class PostgresStorageAdapter { | |||
} | |||
|
|||
find(className, schema, query, { skip, limit, sort, keys }) { | |||
console.log('find'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here too
@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?