Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k 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 OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...