query.cjs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var query_exports = {};
  20. __export(query_exports, {
  21. useLiveQuery: () => useLiveQuery
  22. });
  23. module.exports = __toCommonJS(query_exports);
  24. var import_expo_sqlite = require("expo-sqlite");
  25. var import_react = require("react");
  26. var import_entity = require("../entity.cjs");
  27. var import_sql = require("../sql/sql.cjs");
  28. var import_sqlite_core = require("../sqlite-core/index.cjs");
  29. var import_query = require("../sqlite-core/query-builders/query.cjs");
  30. var import_subquery = require("../subquery.cjs");
  31. const useLiveQuery = (query, deps = []) => {
  32. const [data, setData] = (0, import_react.useState)(
  33. (0, import_entity.is)(query, import_query.SQLiteRelationalQuery) && query.mode === "first" ? void 0 : []
  34. );
  35. const [error, setError] = (0, import_react.useState)();
  36. const [updatedAt, setUpdatedAt] = (0, import_react.useState)();
  37. (0, import_react.useEffect)(() => {
  38. const entity = (0, import_entity.is)(query, import_query.SQLiteRelationalQuery) ? query.table : query.config.table;
  39. if ((0, import_entity.is)(entity, import_subquery.Subquery) || (0, import_entity.is)(entity, import_sql.SQL)) {
  40. setError(new Error("Selecting from subqueries and SQL are not supported in useLiveQuery"));
  41. return;
  42. }
  43. let listener;
  44. const handleData = (data2) => {
  45. setData(data2);
  46. setUpdatedAt(/* @__PURE__ */ new Date());
  47. };
  48. query.then(handleData).catch(setError);
  49. if ((0, import_entity.is)(entity, import_sqlite_core.SQLiteTable) || (0, import_entity.is)(entity, import_sqlite_core.SQLiteView)) {
  50. const config = (0, import_entity.is)(entity, import_sqlite_core.SQLiteTable) ? (0, import_sqlite_core.getTableConfig)(entity) : (0, import_sqlite_core.getViewConfig)(entity);
  51. listener = (0, import_expo_sqlite.addDatabaseChangeListener)(({ tableName }) => {
  52. if (config.name === tableName) {
  53. query.then(handleData).catch(setError);
  54. }
  55. });
  56. }
  57. return () => {
  58. listener?.remove();
  59. };
  60. }, deps);
  61. return {
  62. data,
  63. error,
  64. updatedAt
  65. };
  66. };
  67. // Annotate the CommonJS export names for ESM import in node:
  68. 0 && (module.exports = {
  69. useLiveQuery
  70. });
  71. //# sourceMappingURL=query.cjs.map