@ragable/sdk 0.8.2 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +437 -14
- package/dist/index.d.ts +437 -14
- package/dist/index.js +1050 -28
- package/dist/index.mjs +1042 -28
- package/package.json +1 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1749,6 +1749,824 @@ var PostgrestTableApi = class {
|
|
|
1749
1749
|
}
|
|
1750
1750
|
};
|
|
1751
1751
|
|
|
1752
|
+
// src/collection-query.ts
|
|
1753
|
+
function flattenRecord(record) {
|
|
1754
|
+
return {
|
|
1755
|
+
...record.data,
|
|
1756
|
+
id: record.id,
|
|
1757
|
+
createdAt: record.createdAt,
|
|
1758
|
+
updatedAt: record.updatedAt
|
|
1759
|
+
};
|
|
1760
|
+
}
|
|
1761
|
+
function parseColumns(columns) {
|
|
1762
|
+
const trimmed = (columns ?? "*").trim();
|
|
1763
|
+
if (trimmed.includes("(")) {
|
|
1764
|
+
throw new RagableError(
|
|
1765
|
+
"Embeds and aggregates are only supported in .select() queries, not in a mutation's returning .select(). Use plain column names here.",
|
|
1766
|
+
400,
|
|
1767
|
+
{ code: "SDK_COLLECTION_SELECT_PLAIN_ONLY" }
|
|
1768
|
+
);
|
|
1769
|
+
}
|
|
1770
|
+
if (!trimmed || trimmed === "*") return null;
|
|
1771
|
+
const fields = trimmed.split(",").map((c) => c.trim()).filter(Boolean).filter((c) => c !== "*");
|
|
1772
|
+
return fields.length > 0 ? fields : null;
|
|
1773
|
+
}
|
|
1774
|
+
var IDENT = "[A-Za-z_][A-Za-z0-9_]*";
|
|
1775
|
+
var AGGREGATE_RE = new RegExp(
|
|
1776
|
+
`^(?:(${IDENT}):)?(?:(${IDENT})\\.)?(count|sum|avg|min|max)\\(\\)$`
|
|
1777
|
+
);
|
|
1778
|
+
var EMBED_RE = new RegExp(`^(?:(${IDENT}):)?(${IDENT})(?:!(${IDENT}))?\\(([^()]*)\\)$`);
|
|
1779
|
+
var FIELD_RE = new RegExp(`^${IDENT}$`);
|
|
1780
|
+
function parseSelectExpression(columns) {
|
|
1781
|
+
const trimmed = (columns ?? "*").trim();
|
|
1782
|
+
const result = { fields: null, embeds: [], aggregates: [] };
|
|
1783
|
+
if (!trimmed || trimmed === "*") return result;
|
|
1784
|
+
const fields = [];
|
|
1785
|
+
let sawStar = false;
|
|
1786
|
+
for (const segment of splitTopLevel(trimmed)) {
|
|
1787
|
+
if (segment === "*") {
|
|
1788
|
+
sawStar = true;
|
|
1789
|
+
continue;
|
|
1790
|
+
}
|
|
1791
|
+
const agg = AGGREGATE_RE.exec(segment);
|
|
1792
|
+
if (agg) {
|
|
1793
|
+
const [, alias, field, fn] = agg;
|
|
1794
|
+
if (fn !== "count" && !field) {
|
|
1795
|
+
throw new RagableError(
|
|
1796
|
+
`.select(): "${segment}" \u2014 ${fn}() needs a field, e.g. "amount.${fn}()".`,
|
|
1797
|
+
400,
|
|
1798
|
+
{ code: "SDK_COLLECTION_AGGREGATE_FIELD_REQUIRED" }
|
|
1799
|
+
);
|
|
1800
|
+
}
|
|
1801
|
+
const resolvedAlias = alias ?? (fn === "count" && !field ? "count" : fn);
|
|
1802
|
+
if (result.aggregates.some((a) => a.alias === resolvedAlias)) {
|
|
1803
|
+
throw new RagableError(
|
|
1804
|
+
`.select(): duplicate aggregate key "${resolvedAlias}" \u2014 alias each function, e.g. "total:amount.sum()".`,
|
|
1805
|
+
400,
|
|
1806
|
+
{ code: "SDK_COLLECTION_AGGREGATE_DUPLICATE_ALIAS" }
|
|
1807
|
+
);
|
|
1808
|
+
}
|
|
1809
|
+
result.aggregates.push({
|
|
1810
|
+
fn,
|
|
1811
|
+
...field ? { field } : {},
|
|
1812
|
+
alias: resolvedAlias
|
|
1813
|
+
});
|
|
1814
|
+
continue;
|
|
1815
|
+
}
|
|
1816
|
+
const embed = EMBED_RE.exec(segment);
|
|
1817
|
+
if (embed) {
|
|
1818
|
+
const [, alias, collection, hint, cols] = embed;
|
|
1819
|
+
const inner = (cols ?? "").trim();
|
|
1820
|
+
const embedColumns = !inner || inner === "*" ? null : inner.split(",").map((c) => c.trim()).filter(Boolean).filter((c) => c !== "*");
|
|
1821
|
+
result.embeds.push({
|
|
1822
|
+
alias: alias ?? collection,
|
|
1823
|
+
collection,
|
|
1824
|
+
...hint ? { hint } : {},
|
|
1825
|
+
columns: embedColumns && embedColumns.length > 0 ? embedColumns : null
|
|
1826
|
+
});
|
|
1827
|
+
continue;
|
|
1828
|
+
}
|
|
1829
|
+
if (FIELD_RE.test(segment)) {
|
|
1830
|
+
fields.push(segment);
|
|
1831
|
+
continue;
|
|
1832
|
+
}
|
|
1833
|
+
throw new RagableError(
|
|
1834
|
+
`.select(): could not parse "${segment}". Expected a column name, an embed like "author:users!author_id(name)", or an aggregate like "count()" / "total:amount.sum()".`,
|
|
1835
|
+
400,
|
|
1836
|
+
{ code: "SDK_COLLECTION_SELECT_SYNTAX" }
|
|
1837
|
+
);
|
|
1838
|
+
}
|
|
1839
|
+
if (result.aggregates.length > 0) {
|
|
1840
|
+
if (result.embeds.length > 0) {
|
|
1841
|
+
throw new RagableError(
|
|
1842
|
+
".select(): aggregates and embeds cannot be combined \u2014 run them as separate queries.",
|
|
1843
|
+
400,
|
|
1844
|
+
{ code: "SDK_COLLECTION_AGGREGATE_WITH_EMBED" }
|
|
1845
|
+
);
|
|
1846
|
+
}
|
|
1847
|
+
if (sawStar) {
|
|
1848
|
+
throw new RagableError(
|
|
1849
|
+
'.select(): "*" cannot be combined with aggregates \u2014 list the group-by fields explicitly, e.g. .select("category, count()").',
|
|
1850
|
+
400,
|
|
1851
|
+
{ code: "SDK_COLLECTION_AGGREGATE_WITH_STAR" }
|
|
1852
|
+
);
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
result.fields = sawStar || fields.length === 0 ? null : fields;
|
|
1856
|
+
if (result.aggregates.length > 0) result.fields = fields.length > 0 ? fields : null;
|
|
1857
|
+
return result;
|
|
1858
|
+
}
|
|
1859
|
+
function projectRow(row, fields) {
|
|
1860
|
+
if (!fields) return row;
|
|
1861
|
+
const out = {
|
|
1862
|
+
id: row.id,
|
|
1863
|
+
createdAt: row.createdAt,
|
|
1864
|
+
updatedAt: row.updatedAt
|
|
1865
|
+
};
|
|
1866
|
+
for (const field of fields) {
|
|
1867
|
+
if (field in row) out[field] = row[field];
|
|
1868
|
+
}
|
|
1869
|
+
return out;
|
|
1870
|
+
}
|
|
1871
|
+
var FILTER_OPS = /* @__PURE__ */ new Set([
|
|
1872
|
+
"eq",
|
|
1873
|
+
"neq",
|
|
1874
|
+
"gt",
|
|
1875
|
+
"gte",
|
|
1876
|
+
"lt",
|
|
1877
|
+
"lte",
|
|
1878
|
+
"like",
|
|
1879
|
+
"ilike",
|
|
1880
|
+
"startsWith",
|
|
1881
|
+
"endsWith",
|
|
1882
|
+
"in",
|
|
1883
|
+
"nin",
|
|
1884
|
+
"contains",
|
|
1885
|
+
"is",
|
|
1886
|
+
"exists"
|
|
1887
|
+
]);
|
|
1888
|
+
function splitTopLevel(input) {
|
|
1889
|
+
const parts = [];
|
|
1890
|
+
let depth = 0;
|
|
1891
|
+
let inQuotes = false;
|
|
1892
|
+
let current = "";
|
|
1893
|
+
for (const ch of input) {
|
|
1894
|
+
if (ch === '"') inQuotes = !inQuotes;
|
|
1895
|
+
if (!inQuotes) {
|
|
1896
|
+
if (ch === "(") depth++;
|
|
1897
|
+
else if (ch === ")") depth = Math.max(0, depth - 1);
|
|
1898
|
+
if (ch === "," && depth === 0) {
|
|
1899
|
+
parts.push(current);
|
|
1900
|
+
current = "";
|
|
1901
|
+
continue;
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
current += ch;
|
|
1905
|
+
}
|
|
1906
|
+
parts.push(current);
|
|
1907
|
+
return parts.map((p) => p.trim()).filter(Boolean);
|
|
1908
|
+
}
|
|
1909
|
+
function coerceOrValue(raw) {
|
|
1910
|
+
const v = raw.trim();
|
|
1911
|
+
if (v === "true") return true;
|
|
1912
|
+
if (v === "false") return false;
|
|
1913
|
+
if (v === "null") return null;
|
|
1914
|
+
if (/^-?\d+(\.\d+)?$/.test(v)) return Number(v);
|
|
1915
|
+
if (v.length >= 2 && v.startsWith('"') && v.endsWith('"')) return v.slice(1, -1);
|
|
1916
|
+
return v;
|
|
1917
|
+
}
|
|
1918
|
+
function orSyntaxError(segment) {
|
|
1919
|
+
return new RagableError(
|
|
1920
|
+
`.or(): could not parse "${segment}". Expected "field.op.value" segments separated by commas, e.g. .or('done.eq.true,priority.gte.3') or .or('and(a.eq.1,b.eq.2),c.is.null'). Ops: ${[...FILTER_OPS].join(", ")} (prefix with "not." to negate).`,
|
|
1921
|
+
400,
|
|
1922
|
+
{ code: "SDK_COLLECTION_OR_SYNTAX" }
|
|
1923
|
+
);
|
|
1924
|
+
}
|
|
1925
|
+
function mergeCondition(group, field, op, value, negate) {
|
|
1926
|
+
const condition = negate ? { not: { [op]: value } } : { [op]: value };
|
|
1927
|
+
const existing = group[field];
|
|
1928
|
+
if (existing && typeof existing === "object" && !Array.isArray(existing)) {
|
|
1929
|
+
const prev = existing;
|
|
1930
|
+
if (negate && prev.not && typeof prev.not === "object" && !Array.isArray(prev.not)) {
|
|
1931
|
+
group[field] = {
|
|
1932
|
+
...prev,
|
|
1933
|
+
not: { ...prev.not, [op]: value }
|
|
1934
|
+
};
|
|
1935
|
+
return;
|
|
1936
|
+
}
|
|
1937
|
+
group[field] = { ...prev, ...condition };
|
|
1938
|
+
return;
|
|
1939
|
+
}
|
|
1940
|
+
group[field] = condition;
|
|
1941
|
+
}
|
|
1942
|
+
function parseOrCondition(segment, group) {
|
|
1943
|
+
const firstDot = segment.indexOf(".");
|
|
1944
|
+
if (firstDot <= 0) throw orSyntaxError(segment);
|
|
1945
|
+
const field = segment.slice(0, firstDot);
|
|
1946
|
+
let rest = segment.slice(firstDot + 1);
|
|
1947
|
+
let negate = false;
|
|
1948
|
+
if (rest.startsWith("not.")) {
|
|
1949
|
+
negate = true;
|
|
1950
|
+
rest = rest.slice(4);
|
|
1951
|
+
}
|
|
1952
|
+
const opDot = rest.indexOf(".");
|
|
1953
|
+
if (opDot <= 0) throw orSyntaxError(segment);
|
|
1954
|
+
const op = rest.slice(0, opDot);
|
|
1955
|
+
const rawValue = rest.slice(opDot + 1);
|
|
1956
|
+
if (!FILTER_OPS.has(op)) throw orSyntaxError(segment);
|
|
1957
|
+
let value;
|
|
1958
|
+
if (op === "in" || op === "nin") {
|
|
1959
|
+
const inner = rawValue.replace(/^\(/, "").replace(/\)$/, "");
|
|
1960
|
+
value = inner.trim().length === 0 ? [] : inner.split(",").map(coerceOrValue);
|
|
1961
|
+
} else {
|
|
1962
|
+
value = coerceOrValue(rawValue);
|
|
1963
|
+
}
|
|
1964
|
+
mergeCondition(group, field, op, value, negate);
|
|
1965
|
+
}
|
|
1966
|
+
function parseOrString(input) {
|
|
1967
|
+
const groups = [];
|
|
1968
|
+
for (const segment of splitTopLevel(input)) {
|
|
1969
|
+
if (/^and\(/.test(segment) && segment.endsWith(")")) {
|
|
1970
|
+
const inner = segment.slice(segment.indexOf("(") + 1, -1);
|
|
1971
|
+
const group = {};
|
|
1972
|
+
for (const condition of splitTopLevel(inner)) parseOrCondition(condition, group);
|
|
1973
|
+
if (Object.keys(group).length === 0) throw orSyntaxError(segment);
|
|
1974
|
+
groups.push(group);
|
|
1975
|
+
} else {
|
|
1976
|
+
const group = {};
|
|
1977
|
+
parseOrCondition(segment, group);
|
|
1978
|
+
groups.push(group);
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
if (groups.length === 0) throw orSyntaxError(input);
|
|
1982
|
+
return groups;
|
|
1983
|
+
}
|
|
1984
|
+
var CollectionConditionBuilder = class {
|
|
1985
|
+
constructor() {
|
|
1986
|
+
__publicField(this, "filters", []);
|
|
1987
|
+
__publicField(this, "orGroups", null);
|
|
1988
|
+
__publicField(this, "signal");
|
|
1989
|
+
}
|
|
1990
|
+
/** `field = value` (or `IS NULL` when value is null). */
|
|
1991
|
+
eq(field, value) {
|
|
1992
|
+
return this.push(field, "eq", value);
|
|
1993
|
+
}
|
|
1994
|
+
/** `field != value` (null-aware). */
|
|
1995
|
+
neq(field, value) {
|
|
1996
|
+
return this.push(field, "neq", value);
|
|
1997
|
+
}
|
|
1998
|
+
gt(field, value) {
|
|
1999
|
+
return this.push(field, "gt", value);
|
|
2000
|
+
}
|
|
2001
|
+
gte(field, value) {
|
|
2002
|
+
return this.push(field, "gte", value);
|
|
2003
|
+
}
|
|
2004
|
+
lt(field, value) {
|
|
2005
|
+
return this.push(field, "lt", value);
|
|
2006
|
+
}
|
|
2007
|
+
lte(field, value) {
|
|
2008
|
+
return this.push(field, "lte", value);
|
|
2009
|
+
}
|
|
2010
|
+
/** SQL LIKE — you supply the `%` wildcards, e.g. `.like("title", "%report%")`. */
|
|
2011
|
+
like(field, pattern) {
|
|
2012
|
+
return this.push(field, "like", pattern);
|
|
2013
|
+
}
|
|
2014
|
+
/** Case-insensitive LIKE. */
|
|
2015
|
+
ilike(field, pattern) {
|
|
2016
|
+
return this.push(field, "ilike", pattern);
|
|
2017
|
+
}
|
|
2018
|
+
/** Prefix match; wildcard characters in `value` are matched literally. */
|
|
2019
|
+
startsWith(field, value) {
|
|
2020
|
+
return this.push(field, "startsWith", value);
|
|
2021
|
+
}
|
|
2022
|
+
/** Suffix match; wildcard characters in `value` are matched literally. */
|
|
2023
|
+
endsWith(field, value) {
|
|
2024
|
+
return this.push(field, "endsWith", value);
|
|
2025
|
+
}
|
|
2026
|
+
/** Value is one of `values`. */
|
|
2027
|
+
in(field, values) {
|
|
2028
|
+
return this.push(field, "in", values);
|
|
2029
|
+
}
|
|
2030
|
+
/** Value is NOT one of `values`. */
|
|
2031
|
+
nin(field, values) {
|
|
2032
|
+
return this.push(field, "nin", values);
|
|
2033
|
+
}
|
|
2034
|
+
/**
|
|
2035
|
+
* JSONB containment: array fields contain the given element(s), object
|
|
2036
|
+
* fields contain the given key/value pairs.
|
|
2037
|
+
* `.contains("tags", ["urgent"])`, `.contains("meta", { plan: "pro" })`.
|
|
2038
|
+
*/
|
|
2039
|
+
contains(field, value) {
|
|
2040
|
+
return this.push(field, "contains", value);
|
|
2041
|
+
}
|
|
2042
|
+
/** Strict null / boolean check: `.is("archivedAt", null)`, `.is("done", true)`. */
|
|
2043
|
+
is(field, value) {
|
|
2044
|
+
return this.push(field, "is", value);
|
|
2045
|
+
}
|
|
2046
|
+
/** Whether the JSON key is present at all: `.exists("avatarUrl", false)`. */
|
|
2047
|
+
exists(field, present = true) {
|
|
2048
|
+
return this.push(field, "exists", present);
|
|
2049
|
+
}
|
|
2050
|
+
/** Negated condition: `.not("status", "eq", "archived")`. */
|
|
2051
|
+
not(field, op, value) {
|
|
2052
|
+
this.assertOp(op);
|
|
2053
|
+
this.filters.push({ field: String(field), op, value, not: true });
|
|
2054
|
+
return this;
|
|
2055
|
+
}
|
|
2056
|
+
/** Generic escape hatch, mirroring Supabase `.filter(column, op, value)`. */
|
|
2057
|
+
filter(field, op, value) {
|
|
2058
|
+
this.assertOp(op);
|
|
2059
|
+
return this.push(field, op, value);
|
|
2060
|
+
}
|
|
2061
|
+
/** Equality on every key of `query` (null values become IS NULL). */
|
|
2062
|
+
match(query) {
|
|
2063
|
+
for (const [field, value] of Object.entries(query)) {
|
|
2064
|
+
this.push(field, "eq", value);
|
|
2065
|
+
}
|
|
2066
|
+
return this;
|
|
2067
|
+
}
|
|
2068
|
+
/**
|
|
2069
|
+
* OR conditions, ANDed with the other filters. Accepts the Supabase string
|
|
2070
|
+
* grammar — `.or('done.eq.true,priority.gte.3')`, with `and(...)` for a
|
|
2071
|
+
* multi-condition branch — or an array of where-style objects:
|
|
2072
|
+
* `.or([{ done: true }, { priority: { gte: 3 } }])`.
|
|
2073
|
+
*
|
|
2074
|
+
* One `.or()` per query: the wire format carries a single disjunction.
|
|
2075
|
+
*/
|
|
2076
|
+
or(conditions) {
|
|
2077
|
+
if (this.orGroups) {
|
|
2078
|
+
throw new RagableError(
|
|
2079
|
+
".or() can only be used once per query. Combine branches in a single call: .or('a.eq.1,b.eq.2').",
|
|
2080
|
+
400,
|
|
2081
|
+
{ code: "SDK_COLLECTION_OR_TWICE" }
|
|
2082
|
+
);
|
|
2083
|
+
}
|
|
2084
|
+
this.orGroups = typeof conditions === "string" ? parseOrString(conditions) : conditions;
|
|
2085
|
+
if (!Array.isArray(this.orGroups) || this.orGroups.length === 0) {
|
|
2086
|
+
throw new RagableError(
|
|
2087
|
+
".or() requires at least one condition.",
|
|
2088
|
+
400,
|
|
2089
|
+
{ code: "SDK_COLLECTION_OR_EMPTY" }
|
|
2090
|
+
);
|
|
2091
|
+
}
|
|
2092
|
+
return this;
|
|
2093
|
+
}
|
|
2094
|
+
abortSignal(signal) {
|
|
2095
|
+
this.signal = signal;
|
|
2096
|
+
return this;
|
|
2097
|
+
}
|
|
2098
|
+
get hasConditions() {
|
|
2099
|
+
return this.filters.length > 0 || (this.orGroups?.length ?? 0) > 0;
|
|
2100
|
+
}
|
|
2101
|
+
wireFilters() {
|
|
2102
|
+
return this.filters.map((f) => ({
|
|
2103
|
+
field: f.field,
|
|
2104
|
+
op: f.op,
|
|
2105
|
+
value: f.value,
|
|
2106
|
+
...f.not ? { not: true } : {}
|
|
2107
|
+
}));
|
|
2108
|
+
}
|
|
2109
|
+
assertOp(op) {
|
|
2110
|
+
if (!FILTER_OPS.has(op)) {
|
|
2111
|
+
throw new RagableError(
|
|
2112
|
+
`Unknown filter operator "${op}". Use one of: ${[...FILTER_OPS].join(", ")}.`,
|
|
2113
|
+
400,
|
|
2114
|
+
{ code: "SDK_COLLECTION_BAD_OP" }
|
|
2115
|
+
);
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
push(field, op, value) {
|
|
2119
|
+
this.filters.push({ field: String(field), op, value });
|
|
2120
|
+
return this;
|
|
2121
|
+
}
|
|
2122
|
+
};
|
|
2123
|
+
var CollectionSelectBuilder = class extends CollectionConditionBuilder {
|
|
2124
|
+
constructor(request, columns, options) {
|
|
2125
|
+
super();
|
|
2126
|
+
this.request = request;
|
|
2127
|
+
__publicField(this, "_limit");
|
|
2128
|
+
__publicField(this, "_offset");
|
|
2129
|
+
__publicField(this, "_order", []);
|
|
2130
|
+
__publicField(this, "parsed");
|
|
2131
|
+
__publicField(this, "wantCount");
|
|
2132
|
+
__publicField(this, "headOnly");
|
|
2133
|
+
this.parsed = parseSelectExpression(columns);
|
|
2134
|
+
this.wantCount = options?.count === "exact";
|
|
2135
|
+
this.headOnly = options?.head === true;
|
|
2136
|
+
if (this.parsed.aggregates.length > 0 && (this.wantCount || this.headOnly)) {
|
|
2137
|
+
throw new RagableError(
|
|
2138
|
+
'Aggregate selects compute their own results \u2014 drop { count: "exact" } / { head: true }.',
|
|
2139
|
+
400,
|
|
2140
|
+
{ code: "SDK_COLLECTION_AGGREGATE_WITH_COUNT" }
|
|
2141
|
+
);
|
|
2142
|
+
}
|
|
2143
|
+
}
|
|
2144
|
+
/** Sort by a field. Call again to add secondary sort keys (max 4). */
|
|
2145
|
+
order(field, options) {
|
|
2146
|
+
this._order.push({
|
|
2147
|
+
field: String(field),
|
|
2148
|
+
direction: options?.ascending === true ? "asc" : "desc"
|
|
2149
|
+
});
|
|
2150
|
+
return this;
|
|
2151
|
+
}
|
|
2152
|
+
limit(n) {
|
|
2153
|
+
this._limit = n;
|
|
2154
|
+
return this;
|
|
2155
|
+
}
|
|
2156
|
+
offset(n) {
|
|
2157
|
+
this._offset = n;
|
|
2158
|
+
return this;
|
|
2159
|
+
}
|
|
2160
|
+
/** Rows `from`..`to` inclusive (zero-based), like Supabase `.range()`. */
|
|
2161
|
+
range(from, to) {
|
|
2162
|
+
this._offset = from;
|
|
2163
|
+
this._limit = Math.max(0, to - from + 1);
|
|
2164
|
+
return this;
|
|
2165
|
+
}
|
|
2166
|
+
then(onfulfilled, onrejected) {
|
|
2167
|
+
return this.execute().then(onfulfilled, onrejected);
|
|
2168
|
+
}
|
|
2169
|
+
/**
|
|
2170
|
+
* Exactly one row. 0 or >1 matching rows is an error (code PGRST116),
|
|
2171
|
+
* mirroring Supabase `.single()`.
|
|
2172
|
+
*/
|
|
2173
|
+
async single() {
|
|
2174
|
+
const res = await this.executeRows(2);
|
|
2175
|
+
if (res.error) return { data: null, error: res.error };
|
|
2176
|
+
const rows = res.rows;
|
|
2177
|
+
if (rows.length !== 1) {
|
|
2178
|
+
return {
|
|
2179
|
+
data: null,
|
|
2180
|
+
error: new RagableError(
|
|
2181
|
+
"JSON object requested, multiple (or no) rows returned",
|
|
2182
|
+
406,
|
|
2183
|
+
{ code: "PGRST116", details: `The result contains ${rows.length} rows` }
|
|
2184
|
+
)
|
|
2185
|
+
};
|
|
2186
|
+
}
|
|
2187
|
+
return { data: rows[0], error: null };
|
|
2188
|
+
}
|
|
2189
|
+
/** One row or `null` — only >1 matching rows is an error. */
|
|
2190
|
+
async maybeSingle() {
|
|
2191
|
+
const res = await this.executeRows(2);
|
|
2192
|
+
if (res.error) return { data: null, error: res.error };
|
|
2193
|
+
const rows = res.rows;
|
|
2194
|
+
if (rows.length > 1) {
|
|
2195
|
+
return {
|
|
2196
|
+
data: null,
|
|
2197
|
+
error: new RagableError(
|
|
2198
|
+
"JSON object requested, multiple (or no) rows returned",
|
|
2199
|
+
406,
|
|
2200
|
+
{ code: "PGRST116", details: `The result contains ${rows.length} rows` }
|
|
2201
|
+
)
|
|
2202
|
+
};
|
|
2203
|
+
}
|
|
2204
|
+
return { data: rows[0] ?? null, error: null };
|
|
2205
|
+
}
|
|
2206
|
+
buildBody(limitOverride) {
|
|
2207
|
+
const body = {};
|
|
2208
|
+
const filters = this.wireFilters();
|
|
2209
|
+
if (filters.length > 0) body.filters = filters;
|
|
2210
|
+
if (this.orGroups) body.or = this.orGroups;
|
|
2211
|
+
const limit = limitOverride ?? (this.headOnly ? 0 : this._limit);
|
|
2212
|
+
if (limit !== void 0) body.limit = limit;
|
|
2213
|
+
if (this._offset !== void 0 && this._offset > 0) body.offset = this._offset;
|
|
2214
|
+
if (this._order.length === 1) {
|
|
2215
|
+
body.orderBy = this._order[0].field;
|
|
2216
|
+
body.orderDirection = this._order[0].direction;
|
|
2217
|
+
} else if (this._order.length > 1) {
|
|
2218
|
+
body.orderBy = this._order.map((o) => ({
|
|
2219
|
+
field: o.field,
|
|
2220
|
+
direction: o.direction
|
|
2221
|
+
}));
|
|
2222
|
+
}
|
|
2223
|
+
if (this.wantCount) body.count = true;
|
|
2224
|
+
if (this.parsed.embeds.length > 0) {
|
|
2225
|
+
body.embed = this.parsed.embeds.map((e) => ({
|
|
2226
|
+
alias: e.alias,
|
|
2227
|
+
collection: e.collection,
|
|
2228
|
+
...e.hint ? { hint: e.hint } : {}
|
|
2229
|
+
}));
|
|
2230
|
+
}
|
|
2231
|
+
if (this.parsed.aggregates.length > 0) {
|
|
2232
|
+
body.aggregate = {
|
|
2233
|
+
groupBy: this.parsed.fields ?? [],
|
|
2234
|
+
functions: this.parsed.aggregates.map((a) => ({
|
|
2235
|
+
fn: a.fn,
|
|
2236
|
+
...a.field ? { field: a.field } : {},
|
|
2237
|
+
alias: a.alias
|
|
2238
|
+
}))
|
|
2239
|
+
};
|
|
2240
|
+
}
|
|
2241
|
+
return body;
|
|
2242
|
+
}
|
|
2243
|
+
/** Flatten the envelope and apply column + embedded-column projection. */
|
|
2244
|
+
shapeRow(record) {
|
|
2245
|
+
const flat = flattenRecord(record);
|
|
2246
|
+
for (const embed of this.parsed.embeds) {
|
|
2247
|
+
if (!embed.columns) continue;
|
|
2248
|
+
const value = flat[embed.alias];
|
|
2249
|
+
if (Array.isArray(value)) {
|
|
2250
|
+
flat[embed.alias] = value.map(
|
|
2251
|
+
(v) => projectEmbeddedObject(v, embed.columns)
|
|
2252
|
+
);
|
|
2253
|
+
} else if (value && typeof value === "object") {
|
|
2254
|
+
flat[embed.alias] = projectEmbeddedObject(value, embed.columns);
|
|
2255
|
+
}
|
|
2256
|
+
}
|
|
2257
|
+
if (!this.parsed.fields) return flat;
|
|
2258
|
+
const out = {
|
|
2259
|
+
id: flat.id,
|
|
2260
|
+
createdAt: flat.createdAt,
|
|
2261
|
+
updatedAt: flat.updatedAt
|
|
2262
|
+
};
|
|
2263
|
+
for (const field of this.parsed.fields) {
|
|
2264
|
+
if (field in flat) out[field] = flat[field];
|
|
2265
|
+
}
|
|
2266
|
+
for (const embed of this.parsed.embeds) {
|
|
2267
|
+
if (embed.alias in flat) out[embed.alias] = flat[embed.alias];
|
|
2268
|
+
}
|
|
2269
|
+
return out;
|
|
2270
|
+
}
|
|
2271
|
+
async executeRows(limitOverride) {
|
|
2272
|
+
if (this.parsed.aggregates.length > 0) {
|
|
2273
|
+
const result2 = await asPostgrestResponse(
|
|
2274
|
+
() => this.request(
|
|
2275
|
+
"POST",
|
|
2276
|
+
"/find",
|
|
2277
|
+
this.buildBody(limitOverride),
|
|
2278
|
+
this.signal
|
|
2279
|
+
)
|
|
2280
|
+
);
|
|
2281
|
+
if (result2.error) return { error: result2.error, rows: null, total: null };
|
|
2282
|
+
return { error: null, rows: result2.data ?? [], total: null };
|
|
2283
|
+
}
|
|
2284
|
+
const result = await asPostgrestResponse(
|
|
2285
|
+
() => this.request(
|
|
2286
|
+
"POST",
|
|
2287
|
+
"/find",
|
|
2288
|
+
this.buildBody(limitOverride),
|
|
2289
|
+
this.signal
|
|
2290
|
+
)
|
|
2291
|
+
);
|
|
2292
|
+
if (result.error) return { error: result.error, rows: null, total: null };
|
|
2293
|
+
const payload = result.data;
|
|
2294
|
+
const records = Array.isArray(payload) ? payload : payload?.records ?? [];
|
|
2295
|
+
const total = Array.isArray(payload) ? null : payload?.total ?? null;
|
|
2296
|
+
const rows = records.map((r) => this.shapeRow(r));
|
|
2297
|
+
return { error: null, rows, total };
|
|
2298
|
+
}
|
|
2299
|
+
async execute() {
|
|
2300
|
+
const res = await this.executeRows();
|
|
2301
|
+
if (res.error) return { data: null, error: res.error, count: null };
|
|
2302
|
+
return {
|
|
2303
|
+
data: this.headOnly ? [] : res.rows,
|
|
2304
|
+
error: null,
|
|
2305
|
+
count: res.total
|
|
2306
|
+
};
|
|
2307
|
+
}
|
|
2308
|
+
};
|
|
2309
|
+
function projectEmbeddedObject(value, columns) {
|
|
2310
|
+
const source = value;
|
|
2311
|
+
const out = {};
|
|
2312
|
+
for (const key of ["id", "createdAt", "updatedAt"]) {
|
|
2313
|
+
if (key in source) out[key] = source[key];
|
|
2314
|
+
}
|
|
2315
|
+
for (const column of columns) {
|
|
2316
|
+
if (column in source) out[column] = source[column];
|
|
2317
|
+
}
|
|
2318
|
+
return out;
|
|
2319
|
+
}
|
|
2320
|
+
var CollectionMutationReturning = class {
|
|
2321
|
+
constructor(run, columns) {
|
|
2322
|
+
this.run = run;
|
|
2323
|
+
this.columns = columns;
|
|
2324
|
+
}
|
|
2325
|
+
then(onfulfilled, onrejected) {
|
|
2326
|
+
return this.executeMany().then(onfulfilled, onrejected);
|
|
2327
|
+
}
|
|
2328
|
+
async executeMany() {
|
|
2329
|
+
const res = await this.run();
|
|
2330
|
+
if (res.error) return { data: null, error: res.error };
|
|
2331
|
+
const rows = (res.data ?? []).map(
|
|
2332
|
+
(r) => projectRow(flattenRecord(r), this.columns)
|
|
2333
|
+
);
|
|
2334
|
+
return { data: rows, error: null };
|
|
2335
|
+
}
|
|
2336
|
+
async single() {
|
|
2337
|
+
const many = await this.executeMany();
|
|
2338
|
+
if (many.error) return { data: null, error: many.error };
|
|
2339
|
+
const rows = many.data ?? [];
|
|
2340
|
+
if (rows.length !== 1) {
|
|
2341
|
+
return {
|
|
2342
|
+
data: null,
|
|
2343
|
+
error: new RagableError(
|
|
2344
|
+
"JSON object requested, multiple (or no) rows returned",
|
|
2345
|
+
406,
|
|
2346
|
+
{ code: "PGRST116", details: `The result contains ${rows.length} rows` }
|
|
2347
|
+
)
|
|
2348
|
+
};
|
|
2349
|
+
}
|
|
2350
|
+
return { data: rows[0], error: null };
|
|
2351
|
+
}
|
|
2352
|
+
async maybeSingle() {
|
|
2353
|
+
const many = await this.executeMany();
|
|
2354
|
+
if (many.error) return { data: null, error: many.error };
|
|
2355
|
+
const rows = many.data ?? [];
|
|
2356
|
+
if (rows.length > 1) {
|
|
2357
|
+
return {
|
|
2358
|
+
data: null,
|
|
2359
|
+
error: new RagableError(
|
|
2360
|
+
"JSON object requested, multiple (or no) rows returned",
|
|
2361
|
+
406,
|
|
2362
|
+
{ code: "PGRST116", details: `The result contains ${rows.length} rows` }
|
|
2363
|
+
)
|
|
2364
|
+
};
|
|
2365
|
+
}
|
|
2366
|
+
return { data: rows[0] ?? null, error: null };
|
|
2367
|
+
}
|
|
2368
|
+
};
|
|
2369
|
+
var CollectionUpdateBuilder = class extends CollectionConditionBuilder {
|
|
2370
|
+
constructor(request, patch) {
|
|
2371
|
+
super();
|
|
2372
|
+
this.request = request;
|
|
2373
|
+
this.patch = patch;
|
|
2374
|
+
__publicField(this, "_limit");
|
|
2375
|
+
}
|
|
2376
|
+
limit(n) {
|
|
2377
|
+
this._limit = n;
|
|
2378
|
+
return this;
|
|
2379
|
+
}
|
|
2380
|
+
select(columns = "*") {
|
|
2381
|
+
return new CollectionMutationReturning(
|
|
2382
|
+
() => this.execute(),
|
|
2383
|
+
parseColumns(columns)
|
|
2384
|
+
);
|
|
2385
|
+
}
|
|
2386
|
+
then(onfulfilled, onrejected) {
|
|
2387
|
+
return this.execute().then(
|
|
2388
|
+
(res) => res.error ? { data: null, error: res.error } : { data: null, error: null }
|
|
2389
|
+
).then(onfulfilled, onrejected);
|
|
2390
|
+
}
|
|
2391
|
+
async execute() {
|
|
2392
|
+
if (!this.hasConditions) {
|
|
2393
|
+
return {
|
|
2394
|
+
data: null,
|
|
2395
|
+
error: new RagableError(
|
|
2396
|
+
"update() requires at least one filter \u2014 add .eq()/.match()/.or() before awaiting. To update every record, filter on a condition that always holds, e.g. .neq('id', null).",
|
|
2397
|
+
400,
|
|
2398
|
+
{ code: "SDK_COLLECTION_UPDATE_NO_FILTER" }
|
|
2399
|
+
)
|
|
2400
|
+
};
|
|
2401
|
+
}
|
|
2402
|
+
return asPostgrestResponse(
|
|
2403
|
+
() => this.request(
|
|
2404
|
+
"PATCH",
|
|
2405
|
+
"/records",
|
|
2406
|
+
{
|
|
2407
|
+
where: {},
|
|
2408
|
+
filters: this.wireFilters(),
|
|
2409
|
+
...this.orGroups ? { or: this.orGroups } : {},
|
|
2410
|
+
patch: this.patch,
|
|
2411
|
+
...this._limit !== void 0 ? { limit: this._limit } : {}
|
|
2412
|
+
},
|
|
2413
|
+
this.signal
|
|
2414
|
+
)
|
|
2415
|
+
);
|
|
2416
|
+
}
|
|
2417
|
+
};
|
|
2418
|
+
var CollectionDeleteBuilder = class extends CollectionConditionBuilder {
|
|
2419
|
+
constructor(request) {
|
|
2420
|
+
super();
|
|
2421
|
+
this.request = request;
|
|
2422
|
+
__publicField(this, "_limit");
|
|
2423
|
+
}
|
|
2424
|
+
limit(n) {
|
|
2425
|
+
this._limit = n;
|
|
2426
|
+
return this;
|
|
2427
|
+
}
|
|
2428
|
+
select(columns = "*") {
|
|
2429
|
+
return new CollectionMutationReturning(
|
|
2430
|
+
() => this.execute(),
|
|
2431
|
+
parseColumns(columns)
|
|
2432
|
+
);
|
|
2433
|
+
}
|
|
2434
|
+
then(onfulfilled, onrejected) {
|
|
2435
|
+
return this.execute().then(
|
|
2436
|
+
(res) => res.error ? { data: null, error: res.error } : { data: null, error: null }
|
|
2437
|
+
).then(onfulfilled, onrejected);
|
|
2438
|
+
}
|
|
2439
|
+
async execute() {
|
|
2440
|
+
if (!this.hasConditions) {
|
|
2441
|
+
return {
|
|
2442
|
+
data: null,
|
|
2443
|
+
error: new RagableError(
|
|
2444
|
+
"delete() requires at least one filter \u2014 add .eq()/.match()/.or() before awaiting. To clear a collection, filter on a condition that always holds, e.g. .neq('id', null).",
|
|
2445
|
+
400,
|
|
2446
|
+
{ code: "SDK_COLLECTION_DELETE_NO_FILTER" }
|
|
2447
|
+
)
|
|
2448
|
+
};
|
|
2449
|
+
}
|
|
2450
|
+
const res = await asPostgrestResponse(
|
|
2451
|
+
() => this.request(
|
|
2452
|
+
"DELETE",
|
|
2453
|
+
"/records",
|
|
2454
|
+
{
|
|
2455
|
+
where: {},
|
|
2456
|
+
filters: this.wireFilters(),
|
|
2457
|
+
...this.orGroups ? { or: this.orGroups } : {},
|
|
2458
|
+
...this._limit !== void 0 ? { limit: this._limit } : {}
|
|
2459
|
+
},
|
|
2460
|
+
this.signal
|
|
2461
|
+
)
|
|
2462
|
+
);
|
|
2463
|
+
if (res.error) return { data: null, error: res.error };
|
|
2464
|
+
return { data: res.data.records ?? [], error: null };
|
|
2465
|
+
}
|
|
2466
|
+
};
|
|
2467
|
+
var CollectionInsertChain = class {
|
|
2468
|
+
constructor(request, rows, single) {
|
|
2469
|
+
this.request = request;
|
|
2470
|
+
this.rows = rows;
|
|
2471
|
+
this.single = single;
|
|
2472
|
+
__publicField(this, "_signal");
|
|
2473
|
+
}
|
|
2474
|
+
abortSignal(signal) {
|
|
2475
|
+
this._signal = signal;
|
|
2476
|
+
return this;
|
|
2477
|
+
}
|
|
2478
|
+
select(columns = "*") {
|
|
2479
|
+
return new CollectionMutationReturning(
|
|
2480
|
+
async () => {
|
|
2481
|
+
const res = await this.executeEnvelopes();
|
|
2482
|
+
if (res.error) return { data: null, error: res.error };
|
|
2483
|
+
return { data: res.data, error: null };
|
|
2484
|
+
},
|
|
2485
|
+
parseColumns(columns)
|
|
2486
|
+
);
|
|
2487
|
+
}
|
|
2488
|
+
then(onfulfilled, onrejected) {
|
|
2489
|
+
return this.executeEnvelopes().then((res) => {
|
|
2490
|
+
if (res.error) return { data: null, error: res.error };
|
|
2491
|
+
const envelopes = res.data;
|
|
2492
|
+
const resolved = this.single ? envelopes[0] ?? null : envelopes;
|
|
2493
|
+
return { data: resolved, error: null };
|
|
2494
|
+
}).then(onfulfilled, onrejected);
|
|
2495
|
+
}
|
|
2496
|
+
async executeEnvelopes() {
|
|
2497
|
+
if (this.rows.length === 0) return { data: [], error: null };
|
|
2498
|
+
if (this.single) {
|
|
2499
|
+
const res = await asPostgrestResponse(
|
|
2500
|
+
() => this.request(
|
|
2501
|
+
"POST",
|
|
2502
|
+
"/records",
|
|
2503
|
+
{ data: this.rows[0] },
|
|
2504
|
+
this._signal
|
|
2505
|
+
)
|
|
2506
|
+
);
|
|
2507
|
+
if (res.error) return { data: null, error: res.error };
|
|
2508
|
+
return { data: [res.data], error: null };
|
|
2509
|
+
}
|
|
2510
|
+
return asPostgrestResponse(
|
|
2511
|
+
() => this.request(
|
|
2512
|
+
"POST",
|
|
2513
|
+
"/records/batch",
|
|
2514
|
+
{ items: this.rows },
|
|
2515
|
+
this._signal
|
|
2516
|
+
)
|
|
2517
|
+
);
|
|
2518
|
+
}
|
|
2519
|
+
};
|
|
2520
|
+
var CollectionUpsertBuilder = class {
|
|
2521
|
+
constructor(request, rows, options) {
|
|
2522
|
+
this.request = request;
|
|
2523
|
+
this.rows = rows;
|
|
2524
|
+
this.options = options;
|
|
2525
|
+
__publicField(this, "_signal");
|
|
2526
|
+
}
|
|
2527
|
+
abortSignal(signal) {
|
|
2528
|
+
this._signal = signal;
|
|
2529
|
+
return this;
|
|
2530
|
+
}
|
|
2531
|
+
select(columns = "*") {
|
|
2532
|
+
return new CollectionMutationReturning(
|
|
2533
|
+
async () => {
|
|
2534
|
+
const res = await this.execute();
|
|
2535
|
+
if (res.error) return { data: null, error: res.error };
|
|
2536
|
+
return { data: res.data.records, error: null };
|
|
2537
|
+
},
|
|
2538
|
+
parseColumns(columns)
|
|
2539
|
+
);
|
|
2540
|
+
}
|
|
2541
|
+
then(onfulfilled, onrejected) {
|
|
2542
|
+
return this.execute().then((res) => {
|
|
2543
|
+
if (res.error) return { data: null, error: res.error };
|
|
2544
|
+
const { inserted, updated, skipped } = res.data;
|
|
2545
|
+
return { data: { inserted, updated, skipped }, error: null };
|
|
2546
|
+
}).then(onfulfilled, onrejected);
|
|
2547
|
+
}
|
|
2548
|
+
async execute() {
|
|
2549
|
+
if (this.rows.length === 0) {
|
|
2550
|
+
return {
|
|
2551
|
+
data: { records: [], inserted: 0, updated: 0, skipped: 0 },
|
|
2552
|
+
error: null
|
|
2553
|
+
};
|
|
2554
|
+
}
|
|
2555
|
+
return asPostgrestResponse(
|
|
2556
|
+
() => this.request(
|
|
2557
|
+
"POST",
|
|
2558
|
+
"/records/upsert",
|
|
2559
|
+
{
|
|
2560
|
+
items: this.rows,
|
|
2561
|
+
onConflict: this.options.onConflict ?? "id",
|
|
2562
|
+
...this.options.ignoreDuplicates ? { ignoreDuplicates: true } : {}
|
|
2563
|
+
},
|
|
2564
|
+
this._signal
|
|
2565
|
+
)
|
|
2566
|
+
);
|
|
2567
|
+
}
|
|
2568
|
+
};
|
|
2569
|
+
|
|
1752
2570
|
// src/auth-storage.ts
|
|
1753
2571
|
var LocalStorageAdapter = class {
|
|
1754
2572
|
getItem(key) {
|
|
@@ -2043,6 +2861,109 @@ var RagableAuth = class {
|
|
|
2043
2861
|
this.emit("SIGNED_OUT", null);
|
|
2044
2862
|
return { error: null };
|
|
2045
2863
|
}
|
|
2864
|
+
// ── Password recovery & magic links ────────────────────────────────────────
|
|
2865
|
+
/**
|
|
2866
|
+
* Email a password-recovery link. The link points at `redirectTo` (or the
|
|
2867
|
+
* site's deployed domain when omitted) with `?token_hash=…&type=recovery`
|
|
2868
|
+
* appended. On that page, either:
|
|
2869
|
+
* - call {@link resetPassword} with the token and the new password, or
|
|
2870
|
+
* - call {@link verifyOtp} to sign the user in, then `updateUser({ password })`.
|
|
2871
|
+
*
|
|
2872
|
+
* Always resolves successfully for well-formed requests — whether the email
|
|
2873
|
+
* has an account is never revealed.
|
|
2874
|
+
*/
|
|
2875
|
+
async resetPasswordForEmail(email, options) {
|
|
2876
|
+
return asPostgrestResponse(async () => {
|
|
2877
|
+
await this.fetchAuth("/forgot-password", "POST", {
|
|
2878
|
+
email,
|
|
2879
|
+
...options?.redirectTo ? { redirectTo: options.redirectTo } : {}
|
|
2880
|
+
});
|
|
2881
|
+
return {};
|
|
2882
|
+
});
|
|
2883
|
+
}
|
|
2884
|
+
/**
|
|
2885
|
+
* Email a one-click sign-in (magic) link — passwordless auth. New addresses
|
|
2886
|
+
* get an account automatically unless `shouldCreateUser: false`. The emailed
|
|
2887
|
+
* link carries `?token_hash=…&type=magiclink`; complete sign-in on that page
|
|
2888
|
+
* with {@link verifyOtp}.
|
|
2889
|
+
*/
|
|
2890
|
+
async signInWithOtp(params) {
|
|
2891
|
+
return asPostgrestResponse(async () => {
|
|
2892
|
+
await this.fetchAuth("/magic-link", "POST", {
|
|
2893
|
+
email: params.email,
|
|
2894
|
+
...params.options?.emailRedirectTo ? { redirectTo: params.options.emailRedirectTo } : {},
|
|
2895
|
+
...params.options?.shouldCreateUser === false ? { createUser: false } : {}
|
|
2896
|
+
});
|
|
2897
|
+
return { user: null, session: null };
|
|
2898
|
+
});
|
|
2899
|
+
}
|
|
2900
|
+
/**
|
|
2901
|
+
* Exchange an emailed `token_hash` for a session. Reads the token from the
|
|
2902
|
+
* URL your recovery / magic-link page receives:
|
|
2903
|
+
*
|
|
2904
|
+
* ```ts
|
|
2905
|
+
* const qs = new URLSearchParams(window.location.search);
|
|
2906
|
+
* const { data, error } = await client.auth.verifyOtp({
|
|
2907
|
+
* token_hash: qs.get("token_hash")!,
|
|
2908
|
+
* type: qs.get("type") as "recovery" | "magiclink",
|
|
2909
|
+
* });
|
|
2910
|
+
* ```
|
|
2911
|
+
*
|
|
2912
|
+
* Emits `SIGNED_IN` (and `PASSWORD_RECOVERY` for recovery tokens — listen
|
|
2913
|
+
* for it to route to your "choose a new password" screen).
|
|
2914
|
+
*/
|
|
2915
|
+
async verifyOtp(params) {
|
|
2916
|
+
return asPostgrestResponse(async () => {
|
|
2917
|
+
const token = (params.token_hash ?? params.tokenHash ?? "").trim();
|
|
2918
|
+
if (!token) {
|
|
2919
|
+
throw new RagableError(
|
|
2920
|
+
"verifyOtp requires the token_hash from the emailed link.",
|
|
2921
|
+
400,
|
|
2922
|
+
{ code: "SDK_AUTH_MISSING_TOKEN_HASH" }
|
|
2923
|
+
);
|
|
2924
|
+
}
|
|
2925
|
+
const type = params.type === "email" ? "magiclink" : params.type;
|
|
2926
|
+
const raw = await this.fetchAuth("/verify-token", "POST", { token, type });
|
|
2927
|
+
const session = this.rawToSession(raw);
|
|
2928
|
+
await this.setSessionInternal(session, "SIGNED_IN");
|
|
2929
|
+
if (type === "recovery") this.emit("PASSWORD_RECOVERY", session);
|
|
2930
|
+
return { user: session.user, session };
|
|
2931
|
+
});
|
|
2932
|
+
}
|
|
2933
|
+
/**
|
|
2934
|
+
* One-shot recovery: verify the emailed recovery token, set the new
|
|
2935
|
+
* password, and sign the user in — no intermediate session juggling.
|
|
2936
|
+
*
|
|
2937
|
+
* ```ts
|
|
2938
|
+
* const qs = new URLSearchParams(window.location.search);
|
|
2939
|
+
* const { data, error } = await client.auth.resetPassword({
|
|
2940
|
+
* tokenHash: qs.get("token_hash")!,
|
|
2941
|
+
* newPassword: form.password,
|
|
2942
|
+
* });
|
|
2943
|
+
* ```
|
|
2944
|
+
*
|
|
2945
|
+
* Recovery links are single-use: once the password changes, the same link
|
|
2946
|
+
* is rejected.
|
|
2947
|
+
*/
|
|
2948
|
+
async resetPassword(params) {
|
|
2949
|
+
return asPostgrestResponse(async () => {
|
|
2950
|
+
const token = (params.tokenHash ?? params.token_hash ?? "").trim();
|
|
2951
|
+
if (!token) {
|
|
2952
|
+
throw new RagableError(
|
|
2953
|
+
"resetPassword requires the token_hash from the recovery email link.",
|
|
2954
|
+
400,
|
|
2955
|
+
{ code: "SDK_AUTH_MISSING_TOKEN_HASH" }
|
|
2956
|
+
);
|
|
2957
|
+
}
|
|
2958
|
+
const raw = await this.fetchAuth("/reset-password", "POST", {
|
|
2959
|
+
token,
|
|
2960
|
+
password: params.newPassword
|
|
2961
|
+
});
|
|
2962
|
+
const session = this.rawToSession(raw);
|
|
2963
|
+
await this.setSessionInternal(session, "SIGNED_IN");
|
|
2964
|
+
return { user: session.user, session };
|
|
2965
|
+
});
|
|
2966
|
+
}
|
|
2046
2967
|
async refreshSession(refreshToken) {
|
|
2047
2968
|
return asPostgrestResponse(async () => {
|
|
2048
2969
|
const token = refreshToken ?? this.currentSession?.refresh_token;
|
|
@@ -3254,6 +4175,22 @@ var RagableBrowserAuthClient = class {
|
|
|
3254
4175
|
async signOut(_options) {
|
|
3255
4176
|
return this.auth.signOut(_options);
|
|
3256
4177
|
}
|
|
4178
|
+
/** Email a password-recovery link — see {@link RagableAuth.resetPasswordForEmail}. */
|
|
4179
|
+
async resetPasswordForEmail(email, options) {
|
|
4180
|
+
return this.auth.resetPasswordForEmail(email, options);
|
|
4181
|
+
}
|
|
4182
|
+
/** Email a magic sign-in link — see {@link RagableAuth.signInWithOtp}. */
|
|
4183
|
+
async signInWithOtp(params) {
|
|
4184
|
+
return this.auth.signInWithOtp(params);
|
|
4185
|
+
}
|
|
4186
|
+
/** Exchange an emailed token_hash for a session — see {@link RagableAuth.verifyOtp}. */
|
|
4187
|
+
async verifyOtp(params) {
|
|
4188
|
+
return this.auth.verifyOtp(params);
|
|
4189
|
+
}
|
|
4190
|
+
/** Verify a recovery token and set a new password in one call — see {@link RagableAuth.resetPassword}. */
|
|
4191
|
+
async resetPassword(params) {
|
|
4192
|
+
return this.auth.resetPassword(params);
|
|
4193
|
+
}
|
|
3257
4194
|
async register(body) {
|
|
3258
4195
|
return this.auth.register(body);
|
|
3259
4196
|
}
|
|
@@ -3305,6 +4242,45 @@ var BrowserCollectionApi = class {
|
|
|
3305
4242
|
this.database = database;
|
|
3306
4243
|
this.name = name;
|
|
3307
4244
|
this.databaseInstanceId = databaseInstanceId;
|
|
4245
|
+
/** Transport for the chainable builders, bound to this collection. */
|
|
4246
|
+
__publicField(this, "chainRequest", (method, path, body, signal) => this.database._requestCollection(
|
|
4247
|
+
method,
|
|
4248
|
+
`/${encodeURIComponent(this.name)}${path}`,
|
|
4249
|
+
body,
|
|
4250
|
+
this.databaseInstanceId,
|
|
4251
|
+
signal
|
|
4252
|
+
));
|
|
4253
|
+
/**
|
|
4254
|
+
* Supabase-style chainable query. Rows come back flat (`id`, `createdAt`,
|
|
4255
|
+
* `updatedAt` merged into the document fields).
|
|
4256
|
+
*
|
|
4257
|
+
* ```ts
|
|
4258
|
+
* const { data, error, count } = await collections.todos
|
|
4259
|
+
* .select("*", { count: "exact" })
|
|
4260
|
+
* .eq("done", false)
|
|
4261
|
+
* .or("priority.gte.3,starred.eq.true")
|
|
4262
|
+
* .order("createdAt", { ascending: false })
|
|
4263
|
+
* .range(0, 19);
|
|
4264
|
+
* ```
|
|
4265
|
+
*
|
|
4266
|
+
* Relation embeds (one level): `select("*, author:users!author_id(name), comments(*)")`
|
|
4267
|
+
* — to-one embeds become an object (or null), to-many an array. Aggregations:
|
|
4268
|
+
* `select("category, count(), total:amount.sum()")` groups by the plain
|
|
4269
|
+
* columns; pass a type argument for the result shape:
|
|
4270
|
+
* `select<{ category: string; count: number }>("category, count()")`.
|
|
4271
|
+
*/
|
|
4272
|
+
__publicField(this, "select", (columns, options) => new CollectionSelectBuilder(this.chainRequest, columns, options));
|
|
4273
|
+
/**
|
|
4274
|
+
* Insert-or-update matched on `onConflict` (`"id"` by default, or any data
|
|
4275
|
+
* field, e.g. `{ onConflict: "slug" }`). Chain `.select()` for the affected
|
|
4276
|
+
* rows. Match-based (no unique indexes): concurrent upserts of the same new
|
|
4277
|
+
* value can both insert.
|
|
4278
|
+
*/
|
|
4279
|
+
__publicField(this, "upsert", (values, options) => new CollectionUpsertBuilder(
|
|
4280
|
+
this.chainRequest,
|
|
4281
|
+
Array.isArray(values) ? values : [values],
|
|
4282
|
+
options ?? {}
|
|
4283
|
+
));
|
|
3308
4284
|
__publicField(this, "requestFind", (body) => asPostgrestResponse(
|
|
3309
4285
|
() => this.database._requestCollection(
|
|
3310
4286
|
"POST",
|
|
@@ -3348,14 +4324,20 @@ var BrowserCollectionApi = class {
|
|
|
3348
4324
|
__publicField(this, "findUnique", async (args) => {
|
|
3349
4325
|
return this.findFirst({ where: args.where });
|
|
3350
4326
|
});
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
)
|
|
3358
|
-
|
|
4327
|
+
/**
|
|
4328
|
+
* Insert one row or an array of rows. Chain `.select()` for the inserted
|
|
4329
|
+
* rows in flat shape, or await directly for the record envelope(s)
|
|
4330
|
+
* (back-compat extension; Supabase resolves to null without `.select()`).
|
|
4331
|
+
*/
|
|
4332
|
+
__publicField(this, "insert", ((data) => {
|
|
4333
|
+
const isArray = Array.isArray(data);
|
|
4334
|
+
const rows = isArray ? data : [data];
|
|
4335
|
+
return new CollectionInsertChain(
|
|
4336
|
+
this.chainRequest,
|
|
4337
|
+
rows,
|
|
4338
|
+
!isArray
|
|
4339
|
+
);
|
|
4340
|
+
}));
|
|
3359
4341
|
/**
|
|
3360
4342
|
* Insert multiple rows in one request (server multi-value `INSERT`, single transaction).
|
|
3361
4343
|
* Empty **`items`** resolves to an empty array. Max batch size is enforced on the server (500).
|
|
@@ -3369,16 +4351,28 @@ var BrowserCollectionApi = class {
|
|
|
3369
4351
|
)
|
|
3370
4352
|
));
|
|
3371
4353
|
/**
|
|
3372
|
-
*
|
|
4354
|
+
* Two forms:
|
|
4355
|
+
* - `update(patch)` — Supabase-style chain: add filters, then await.
|
|
4356
|
+
* `update({ done: true }).eq("id", id)` (optionally `.select()`).
|
|
4357
|
+
* - `update(where, patch)` — legacy direct call, returns updated records.
|
|
3373
4358
|
*/
|
|
3374
|
-
__publicField(this, "update", (
|
|
3375
|
-
()
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
4359
|
+
__publicField(this, "update", ((...args) => {
|
|
4360
|
+
if (args.length >= 2) {
|
|
4361
|
+
const [where, patch, options] = args;
|
|
4362
|
+
return asPostgrestResponse(
|
|
4363
|
+
() => this.database._requestCollection(
|
|
4364
|
+
"PATCH",
|
|
4365
|
+
`/${encodeURIComponent(this.name)}/records`,
|
|
4366
|
+
{ where, patch, ...options?.limit ? { limit: options.limit } : {} },
|
|
4367
|
+
this.databaseInstanceId
|
|
4368
|
+
)
|
|
4369
|
+
);
|
|
4370
|
+
}
|
|
4371
|
+
return new CollectionUpdateBuilder(
|
|
4372
|
+
this.chainRequest,
|
|
4373
|
+
args[0]
|
|
4374
|
+
);
|
|
4375
|
+
}));
|
|
3382
4376
|
/**
|
|
3383
4377
|
* Like {@link BrowserCollectionApi.update} but the success payload includes
|
|
3384
4378
|
* `meta.count` (number of rows returned from the update, bounded by `limit`).
|
|
@@ -3388,14 +4382,26 @@ var BrowserCollectionApi = class {
|
|
|
3388
4382
|
if (r.error) return r;
|
|
3389
4383
|
return { data: { records: r.data, meta: { count: r.data.length } }, error: null };
|
|
3390
4384
|
});
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
4385
|
+
/**
|
|
4386
|
+
* Two forms:
|
|
4387
|
+
* - `delete()` — Supabase-style chain: add filters, then await.
|
|
4388
|
+
* `delete().eq("id", id)` (optionally `.select()`).
|
|
4389
|
+
* - `delete(where)` — legacy direct call, returns `{ deleted, records }`.
|
|
4390
|
+
*/
|
|
4391
|
+
__publicField(this, "delete", ((...args) => {
|
|
4392
|
+
if (args.length === 0) {
|
|
4393
|
+
return new CollectionDeleteBuilder(this.chainRequest);
|
|
4394
|
+
}
|
|
4395
|
+
const [where, options] = args;
|
|
4396
|
+
return asPostgrestResponse(
|
|
4397
|
+
() => this.database._requestCollection(
|
|
4398
|
+
"DELETE",
|
|
4399
|
+
`/${encodeURIComponent(this.name)}/records`,
|
|
4400
|
+
{ where, ...options?.limit ? { limit: options.limit } : {} },
|
|
4401
|
+
this.databaseInstanceId
|
|
4402
|
+
)
|
|
4403
|
+
);
|
|
4404
|
+
}));
|
|
3399
4405
|
/**
|
|
3400
4406
|
* Like {@link BrowserCollectionApi.delete} but the success payload includes **`meta.count`**
|
|
3401
4407
|
* (number of deleted rows), matching {@link BrowserCollectionApi.updateMany}.
|
|
@@ -3597,7 +4603,7 @@ var RagableBrowserDatabaseClient = class {
|
|
|
3597
4603
|
toUrl(path) {
|
|
3598
4604
|
return `${normalizeBrowserApiBase()}${path.startsWith("/") ? path : `/${path}`}`;
|
|
3599
4605
|
}
|
|
3600
|
-
async _requestCollection(method, path, body, databaseInstanceId) {
|
|
4606
|
+
async _requestCollection(method, path, body, databaseInstanceId, signal) {
|
|
3601
4607
|
const gid = requireAuthGroupId(this.options);
|
|
3602
4608
|
const token = await resolveDatabaseAuthBearer(this.options, this.ragableAuth);
|
|
3603
4609
|
const id = databaseInstanceId?.trim() || this.options.databaseInstanceId?.trim();
|
|
@@ -3617,7 +4623,8 @@ var RagableBrowserDatabaseClient = class {
|
|
|
3617
4623
|
{
|
|
3618
4624
|
method,
|
|
3619
4625
|
headers,
|
|
3620
|
-
body: body !== void 0 ? JSON.stringify(body) : void 0
|
|
4626
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0,
|
|
4627
|
+
...signal ? { signal } : {}
|
|
3621
4628
|
}
|
|
3622
4629
|
);
|
|
3623
4630
|
const payload = await parseMaybeJsonBody(response);
|
|
@@ -4648,6 +5655,12 @@ function createClient(options) {
|
|
|
4648
5655
|
export {
|
|
4649
5656
|
AuthBroadcastChannel,
|
|
4650
5657
|
BrowserStorageBucketClient,
|
|
5658
|
+
CollectionDeleteBuilder,
|
|
5659
|
+
CollectionInsertChain,
|
|
5660
|
+
CollectionMutationReturning,
|
|
5661
|
+
CollectionSelectBuilder,
|
|
5662
|
+
CollectionUpdateBuilder,
|
|
5663
|
+
CollectionUpsertBuilder,
|
|
4651
5664
|
CookieStorageAdapter,
|
|
4652
5665
|
DEFAULT_RAGABLE_API_BASE,
|
|
4653
5666
|
LocalStorageAdapter,
|
|
@@ -4708,6 +5721,8 @@ export {
|
|
|
4708
5721
|
normalizeBrowserApiBase,
|
|
4709
5722
|
parseAgentStreamAgentInfo,
|
|
4710
5723
|
parseAgentStreamDone,
|
|
5724
|
+
parseOrString,
|
|
5725
|
+
parseSelectExpression,
|
|
4711
5726
|
parseSseDataLine,
|
|
4712
5727
|
parseTransportResponse,
|
|
4713
5728
|
readSseStream,
|
|
@@ -4720,4 +5735,3 @@ export {
|
|
|
4720
5735
|
unwrapPostgrest,
|
|
4721
5736
|
wrapStreamTextAsObject
|
|
4722
5737
|
};
|
|
4723
|
-
//# sourceMappingURL=index.mjs.map
|