Sequelize is one of the most used ORM libraries in Node, and there are plans to upgrade Sequelize to V5 in the project.
According to the update documentation, one of these is to disable String Based Operators and use Symbol Operators such as sequelize.op instead.
Operator is mainly used in query conditions to generate query conditions, for example
const where = {
age: {
$lte: 10}}/ / replace
const replaceWhere = {
age: {
[Op.lte]: 10}}Copy the code
const operatorsAliases = {
$eq: Op.eq,
$ne: Op.ne,
$gte: Op.gte,
$all: Op.all,
$in: Op.in, ... moreAliase }Copy the code
In short, all query conditions in this project need to be replaced from the left side of operatorsAliases to the right side. This is the main content of this article
Shanyue. tech/post/sequel…
The preparatory work
Before you start working on git, you need to clean up your workspace and staging area so that you don’t have to go back during the replacement process.
Clearing out your workspace and stash means removing all stash commits and stash ones that can’t be committed. Even better, create a new branch.
VS Code global substitution
After manually replacing a few at first, I decided that this was not an option and decided to use VS Code’s global replacement.
First think about where the operator of a query will appear
where.age = { $lte: 10 }
where.age.$lte = 10
where.age['$lte'] = 10
Copy the code
In addition, order is important, and the order from most concrete to abstract is as follows
['$lte', Op.lte],
['.$lte', [Op.lte]],
['$lte', [Op.lte]]
Copy the code
Then, I replaced them one by one, but after replacing a few zhihuis, I found… My patience is limited
> Object.keys(operatorsAliases).length
34
Copy the code
I need to make 34 * 3 = 102 substitutions. It’s not my fault. No one has patience
Replace the file with the sed command
How important it is to have one more command
Using the hello, world version of sed, replace hello with word
Well, the syntax of sed is exactly the same as vim, which tells us how important it is to master vim…
$ echo hello | sed "s/hello/world/g"
world
Copy the code
Using the rules described in the previous section, write a sed file (replace.sed) to test the example (test.js)
# replace.sed
s/'$lte'/Op.lte/g
s/.$lte/[Op.lte]/g
s/$lte/[Op.lte]/g
Copy the code
where.age = { $lte: 10 }
where.age.$lte = 10
where.age['$lte'] = 10
Copy the code
After a simple test, enter the following command and it seems to work fine
$ sed -f replace.sed test.js
where.age = {[Op.lte]: 10 }
where.age[Op.lte] = 10
where.age[Op.lte] = 10
Copy the code
But there are 34 aliases that need to be replaced, using the browser console to generate sed files
> Object.keys(operatorsAliases).map(op= > op.slice(1)).flatMap(op= > [`s/'\$${op}\b'/Op.${op}/g`.`s/\.\$${op}\b/[Op.${op}]/g`.`s/\$${op}\b/[Op.${op}]/g`]).join('\n')
s/'$eq'/Op.eq/g
s/.$eq/[Op.eq]/g
s/$eq/[Op.eq]/g
s/'$ne'/Op.ne/g s/.$ne/[Op.ne]/g s/$ne/[Op.ne]/g ... .Copy the code
While the generated command is a bit crude… “But simple and crude things work
Replace all files under the project
Only one question remains, how do I list all the files in the current path
How important it is to have one more command
I made a list of all the commands I could think of
find .
It should be possible to exclude the.gitignores file, but it seems a bit cumbersome and I’ve never used it.ls -R
Not formatted friendly enoughtree
Readability is good, but machine readability is poor
How to exclude a directory in find.command
None of the above commands works well. Here’s a simpler and more appropriate order
git ls-files
Copy the code
For more git commands, see Git Cheat Sheets
In this case, the shell command is as follows: -i means to replace the file directly, -i “” means to replace the file name without adding the suffix, why must write an empty string, because the MAC sed command is so paranoid.
$ sed -i "" -f replace.sed $(git ls-files)
Copy the code
Git diff: [op. in]dex: [op. in]dex: [op. in]dex: [op. in]dex: [op. in]dex: [op. in]dex: [op. in]dex
git checkout .
Copy the code
Using regular matching
Use \b to match words and solve the problem perfectly.
s/'\$eq\b'/Op.eq/g
s/\.\$eq\b/[Op.eq]/g
s/\$eq\b/[Op.eq]/g
Copy the code
However, \ B is not supported on the MAC, so you can try the following command. In this case, you need to install gnu-sed on the MAC. Finally, sed has been replaced on the MAC
$ echo "hello" | sed "s/\bhello\b/world/g"
hello
$ brew install gnu-sed
$ echo "hello" | gsed "s/\bhello\b/world/g"
world
#Always use double quotation marks
$ echo "hello" | gsed s/\bhello\b/world/g
hello
Copy the code
One important point here is that the sed command must be enclosed in double quotation marks
Use js to generate a new sed command
Object.keys(operatorsAliases).map(op= > op.slice(1)).flatMap(op= > [`s/'\\$${op}\\b'/Op.${op}/g`.`s/\\.\\$${op}\\b/[Op.${op}]/g`.`s/\\$${op}\\b/[Op.${op}]/g`]).join('\n')
Copy the code
Finally, run the command to replace all characters successfully
#-i stands for direct replacement of files, and -r stands for regular expressions that support extensions
$ gsed -i -r -f r.sed $(git ls-files| grep -v src/data)
$ git diff --shortstat
63 files changed, 293 insertions(+), 293 deletions(-)
Copy the code
Welcome to pay attention to my public number shanyuexixing, here records my technical growth, welcome to exchange