0

I have mysql db with next fields

id - number
name - string
type - string enum (admin, user, guest)
parentTransactionId - number
payDetails - JSON field, array of objects

examples of payDetails:
[{"type": "stripe_fee", "amount": 1.09, "currency": "usd"}, {"type": "inner_fee", "amount": 20, "currency": "eur"}],
[{"type": "stripe_fee", "amount": 1.09, "currency": "usd"}]

I have function with arguments

function myQuery(existedQuery, payerType) {
 query.where = {
  type: {
    [sequelize.Op.notIn]: [PSPBalanceTransactionType.Guest],
  },
  ...(payerType === EPayerType.Admin
    ? {}
    : payerType === EPayerType.User
    ? { internalParentTransactionId: null }
    : { internalParentTransactionId: { [sequelize.Op.ne]: null } }),
  ...query.where,
 };
}

This query works good, but I should add sorting by payDetails, field amount where type = 'inner_fee'.

This type can not exists in row payDetails fee

db for test:

  CREATE TABLE `test`.`testTransactions` (
  `id` INT NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  `parentTransactionId` VARCHAR(45) NULL,
  `payDetails` JSON NULL,
  `type` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC));

data for test:

INSERT INTO `test`.`testTransactions` (`id`, `name`, `payDetails`, `type`) VALUES ('1', 'first', '[]', 'admin');
INSERT INTO `test`.`testTransactions` (`id`, `name`, `parentTransactionId`, `payDetails`, `type`) VALUES ('2', 'second', '15', '[{\"type\": \"stripe_fee\", \"amount\": 1.09, \"currency\": \"usd\"}, {\"type\": \"inner_fee\", \"amount\": 20, \"currency\": \"eur\"}]', 'user');
INSERT INTO `test`.`testTransactions` (`id`, `name`, `parentTransactionId`, `payDetails`, `type`) VALUES ('3', 'third', '2', '[{\"type\": \"stripe_fee\", \"amount\": 1.09, \"currency\": \"usd\"}, {\"type\": \"inner_fee\", \"amount\": 25, \"currency\": \"eur\"}]', 'user');
INSERT INTO `test`.`testTransactions` (`id`, `name`, `parentTransactionId`, `payDetails`, `type`) VALUES ('4', 'forth', '125', '[{\"type\": \"stripe_fee\", \"amount\": 1.09, \"currency\": \"usd\"}]', 'admin');
INSERT INTO `test`.`testTransactions` (`id`, `name`, `parentTransactionId`, `payDetails`, `type`) VALUES ('5', 'fifth', '3', '[]', 'admin');

expected result (by ids)

3 ..name ...other all fields
2 ..name ...other all fields
...other rows

I will happy if you answer in mysql syntax, but really - I need solution on sequelize - it will be amazing! because I have many other logic in sequelize in this method which I add after to this request

I use mysql 5.7

4

0 回答 0