Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
791 views
in Technique[技术] by (71.8m points)

sequelize.js - Sequelize QueryGenerator and scope

const model = MyModel.scope(scopes);
const { queryGenerator } = model;
const queryOptions = {
  scopes,
  attributes,
  include,
  where,
  order,
  offset,
  limit,
};
model._validateIncludedElements.bind(model)(queryOptions);

const cte = queryGenerator.selectQuery(
  model.getTableName(),
  queryOptions,
  model
)
  .slice(0, -1);

I have two issue with the query generator

  1. The scope is ignored (Including default scope), How can I apply scopes in the query generator?
  2. The attributes are in camel case instead of snack case
question from:https://stackoverflow.com/questions/65913708/sequelize-querygenerator-and-scope

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By reading source code on the sequelize I found the solution, Utils.mapFinderOptions and model._injectScope

const model = MyModel.scope(scopes);
const { queryGenerator } = model;
const queryOptions = {
  scopes,
  attributes,
  include,
  where,
  order,
  offset,
  limit,
};
model._injectScope(queryOptions);
model._validateIncludedElements.bind(model)(queryOptions);

Utils.mapFinderOptions(queryOptions, model);

const cte = queryGenerator.selectQuery(
  model.getTableName(),
  queryOptions,
  model
)
  .slice(0, -1);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...