opral / inlang-message-sdk

0 stars 0 forks source link

sdk query builder API #82

Open jldec opened 3 weeks ago

jldec commented 3 weeks ago

Context

Fink is able to render a filtered list of messages based on criteria selected by the user e.g. to limit the list to include only specific languages and show only those messages with lint-rule reports.

Proposal

Move this query logic into the SDK to simplify Fink and allow other apps to leverage as well.

samuelstroschein commented 3 weeks ago

Mingo https://github.com/kofrasa/mingo seems to be the best solution for query API:

cc @martin.lysk1 who discovered mingo

jldec commented 3 weeks ago

nice - thanks for the pointer - pasting example usage from the mingo README here for tl;dr

import { Query } from "mingo";

// input is either an Array or any iterable source (i.e Object{next:Function}) including ES6 generators.
let criteria = { score: { $gt: 10 } };

let query = new Query(criteria);

// filter collection with find()
let cursor = query.find(collection);

// alternatively use shorthand
// cursor = mingo.find(collection, criteria)

// sort, skip and limit by chaining
cursor.sort({ student_id: 1, score: -1 }).skip(100).limit(100);

// count matches. exhausts cursor
cursor.count();

// classic cursor iterator (old school)
while (cursor.hasNext()) {
  console.log(cursor.next());
}

// ES6 iterators (new cool)
for (let value of cursor) {
  console.log(value);
}

// all() to retrieve matched objects. exhausts cursor
cursor.all();