|
@@ -20,6 +20,7 @@ export const useQuery = (params: {
|
|
const [total, setTotal] = useState<number>(0);
|
|
const [total, setTotal] = useState<number>(0);
|
|
const [expr, setExpr] = useState<string>('');
|
|
const [expr, setExpr] = useState<string>('');
|
|
const [queryResult, setQueryResult] = useState<any>({ data: [], latency: 0 });
|
|
const [queryResult, setQueryResult] = useState<any>({ data: [], latency: 0 });
|
|
|
|
+ const lastQuery = useRef<any>();
|
|
|
|
|
|
// build local cache for pk ids
|
|
// build local cache for pk ids
|
|
const pageCache = useRef(new Map());
|
|
const pageCache = useRef(new Map());
|
|
@@ -59,23 +60,26 @@ export const useQuery = (params: {
|
|
page: number = currentPage,
|
|
page: number = currentPage,
|
|
consistency_level = collection.consistencyLevel
|
|
consistency_level = collection.consistencyLevel
|
|
) => {
|
|
) => {
|
|
- if (!collection.primaryKey.value || !collection.loaded) {
|
|
|
|
- // console.info('[skip running query]: no key yet');
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
const _expr = getPageExpr(page);
|
|
const _expr = getPageExpr(page);
|
|
// console.log('query expr', _expr);
|
|
// console.log('query expr', _expr);
|
|
params.onQueryStart(_expr);
|
|
params.onQueryStart(_expr);
|
|
|
|
|
|
try {
|
|
try {
|
|
- // execute query
|
|
|
|
- const res = await Collection.queryData(params.collectionName, {
|
|
|
|
|
|
+ const queryParams = {
|
|
expr: _expr,
|
|
expr: _expr,
|
|
output_fields: collection.fields.map((i: any) => i.name),
|
|
output_fields: collection.fields.map((i: any) => i.name),
|
|
limit: pageSize || 10,
|
|
limit: pageSize || 10,
|
|
consistency_level,
|
|
consistency_level,
|
|
// travel_timestamp: timeTravelInfo.timestamp,
|
|
// travel_timestamp: timeTravelInfo.timestamp,
|
|
- });
|
|
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ // cache last query
|
|
|
|
+ lastQuery.current = queryParams;
|
|
|
|
+ // execute query
|
|
|
|
+ const res = await Collection.queryData(
|
|
|
|
+ params.collectionName,
|
|
|
|
+ queryParams
|
|
|
|
+ );
|
|
|
|
|
|
// get last item of the data
|
|
// get last item of the data
|
|
const lastItem = res.data[res.data.length - 1];
|
|
const lastItem = res.data[res.data.length - 1];
|
|
@@ -136,9 +140,6 @@ export const useQuery = (params: {
|
|
};
|
|
};
|
|
|
|
|
|
const count = async (consistency_level = collection.consistency_level) => {
|
|
const count = async (consistency_level = collection.consistency_level) => {
|
|
- if (!collection.primaryKey.value || !collection.loaded) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
const count = 'count(*)';
|
|
const count = 'count(*)';
|
|
const res = await Collection.queryData(params.collectionName, {
|
|
const res = await Collection.queryData(params.collectionName, {
|
|
expr: expr,
|
|
expr: expr,
|
|
@@ -163,7 +164,10 @@ export const useQuery = (params: {
|
|
|
|
|
|
// query if expr is changed
|
|
// query if expr is changed
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
- // reset
|
|
|
|
|
|
+ if (!collection.primaryKey.value || !collection.loaded) {
|
|
|
|
+ console.info('[skip running query]: no key yet');
|
|
|
|
+ return;
|
|
|
|
+ } // reset
|
|
reset();
|
|
reset();
|
|
// get count;
|
|
// get count;
|
|
count();
|
|
count();
|
|
@@ -173,6 +177,10 @@ export const useQuery = (params: {
|
|
|
|
|
|
// query if collection is changed
|
|
// query if collection is changed
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
|
+ if (!collection.primaryKey.value || !collection.loaded) {
|
|
|
|
+ // console.info('[skip running query]: no key yet');
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
// reset
|
|
// reset
|
|
reset();
|
|
reset();
|
|
// get count;
|
|
// get count;
|
|
@@ -183,6 +191,10 @@ export const useQuery = (params: {
|
|
|
|
|
|
// query if page size is changed
|
|
// query if page size is changed
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
|
+ if (!collection.primaryKey.value || !collection.loaded) {
|
|
|
|
+ // console.info('[skip running query]: no key yet');
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
// reset
|
|
// reset
|
|
reset();
|
|
reset();
|
|
// do the query
|
|
// do the query
|