Sqlite3'ten Json çıktısını nasıl alabilirim

Oct 05 2020

Merhaba 2 tablodan bilgi almak için bir SQL yazdım ama veriyi döndürmek istediğimde bunu doğru zamanda yapacak dizini bilmiyorum. ne yapmalıyım ve her yöntem yerine ilkinden Json yanıtı almak için bir şekilde var mı?

işlevim:

const searchAntibodies = (
  index: number,
  amount: number,
  information: string,
  startDate: string,
  endDate: string,
) => {
  return new Promise<Antibodies[]>((resolve, reject) => {
    let antibodies: Antibodies[] = [];
    db.serialize(() => {
      db.each(`SELECT id, name as antibodyName FROM Antibodies WHERE
              id IN 
              (SELECT id FROM Antibodies WHERE name LIKE ?
              UNION all
              SELECT antiId FROM AssignedColors WHERE name LIKE ?
              UNION all
              SELECT antiId FROM AssignedReactivities WHERE name LIKE ?)
              AND dateOfCreation >= ? AND dateOfCreation <= ?
              ORDER BY dateOfCreation DESC LIMIT ? OFFSET ?;`
        , [`%${information}%`, `%${information}%`, `%${information}%`, startDate, endDate, amount, index]
        , (err, antibody: Antibodies) => {
          if (err) {
            reject(err.message);
          } else {
            db.all('SELECT name, locations, colorId FROM AssignedColors WHERE antiId = ?', [antibody.id], (err, colors) => {
              if (err) {
                reject(err.message);
              } else {
                antibody.colors = colors;
                antibodies.push(antibody);
                if (antibodies.length === 10) {
                  resolve(antibodies)
                }
              }
            });
          }
        });
    });
  });
}

beklediğim sonuç:

[   {
    id: 1999,
    antibodyName: 'Antibody 1999',
    colors: [ [Object], [Object], [Object], [Object], [Object] ]   },   {
    id: 1995,
    antibodyName: 'Antibody 1995',
    colors: [ [Object], [Object], [Object], [Object], [Object] ]   },   {
    id: 1994,
    antibodyName: 'Antibody 1994',
    colors: [ [Object], [Object], [Object], [Object], [Object] ]   },   {
    id: 1993,
    antibodyName: 'Antibody 1998',
    colors: [ [Object], [Object], [Object], [Object], [Object] ]   },   {
    id: 1997,
    antibodyName: 'Antibody 1997',   } ]

Yanıtlar

MjEbrahimzadeh Oct 06 2020 at 02:09

Tamam, çok aradıktan sonra cevabı buldum. Aslında, ilk geri arama işlevi için tür ayarlamazsanız, tamamlandığında ikinci bir geri arama işlevi elde edebilirsiniz. İşte sonucum:

const getAntibodies = (
  index: number,
  amount: number,
  startDate: number,
  endDate: number,
  orderBy: string,
  orderType: string,
) => {
  return new Promise<Antibodies[]>((resolve, reject) => {
    let antibodies: Antibodies[] = [];
    let totalCount: number;
    let sql = 'SELECT id, name as antibodyName, dateOfCreation FROM Antibodies ';
    let params = [amount, index];
    if (startDate !== 0 || endDate !== 0) {
      sql += `WHERE dateOfCreation >= ? AND dateOfCreation <= ? 
                ORDER BY ${orderBy} ${orderType} LIMIT ? OFFSET ?;`;
      params.unshift(startDate, endDate);
    } else {
      sql += `ORDER BY ${orderBy} ${orderType} LIMIT ? OFFSET ?;`;
    }
    db.serialize(() => {
      db.each(sql,
        params
        , async (err, antibody) => {
          if (err) {
            reject(err.message);
          } else {
            await getColors(antibody.id).then((colors) => {
              antibody.colors = colors;
              antibodies.push(antibody);
              if (antibodies.length === totalCount) {
                resolve(antibodies);
              }
            }).catch((err) => {
              reject(err);
            });
          }
        }, (err, count) => {
          if (err) {
            reject(err.message)
          } else {
            if (count === 0) {
              resolve(antibodies);
            } else {
              totalCount = count;
            }
          }
        });
    });
  });
}

const getColors = (id: number) => {
  return new Promise<Color[]>((resolve, reject) => {
    db.serialize(() => {
      db.all('SELECT name, locations, colorId FROM AssignedColors WHERE antiId = ?', [id], (err, colors: Color[]) => {
        if (err) {
          reject(err.message)
        } else {
          resolve(colors);
        }
      });
    });
  });
}