next-data-kit 10.0.0 → 12.0.0
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/MIGRATION.md +93 -3
- package/README.md +463 -96
- package/dist/chunk-A4ILIJJR.js +12 -0
- package/dist/chunk-A4ILIJJR.js.map +1 -0
- package/dist/chunk-BERW2A2B.js +123 -0
- package/dist/chunk-BERW2A2B.js.map +1 -0
- package/dist/index-j8_iNoy6.d.ts +120 -0
- package/dist/index.d.ts +13 -80
- package/dist/index.js +5 -105
- package/dist/index.js.map +1 -1
- package/dist/server/drizzle.d.ts +11 -0
- package/dist/server/drizzle.js +39 -0
- package/dist/server/drizzle.js.map +1 -0
- package/dist/server.d.ts +250 -78
- package/dist/server.js +308 -79
- package/dist/server.js.map +1 -1
- package/dist/source-BPO-9lNK.d.ts +70 -0
- package/dist/style.css +1 -0
- package/dist/ui.d.ts +401 -0
- package/dist/ui.js +696 -0
- package/dist/ui.js.map +1 -0
- package/package.json +62 -13
- package/registry/r/data-kit.json +71 -19
- package/registry/r/registry.json +88 -9
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/server/source/define.ts
|
|
2
|
+
var dataKitSourceDefine = (props) => {
|
|
3
|
+
const { operator, and, or, list } = props;
|
|
4
|
+
const translate = (condition) => "or" in condition ? or(condition.or.map(translate)) : "native" in condition ? condition.native : operator[condition.operator]({ field: condition.field, value: condition.value });
|
|
5
|
+
return {
|
|
6
|
+
list: (props2) => list({ where: props2.filter.length ? and(props2.filter.map(translate)) : void 0, sort: props2.sort, page: props2.page })
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export { dataKitSourceDefine };
|
|
11
|
+
//# sourceMappingURL=chunk-A4ILIJJR.js.map
|
|
12
|
+
//# sourceMappingURL=chunk-A4ILIJJR.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/server/source/define.ts"],"names":["props"],"mappings":";AAKO,IAAM,mBAAA,GAAsB,CAClC,KAAA,KAM0B;AAE1B,EAAA,MAAM,EAAE,QAAA,EAAU,GAAA,EAAK,EAAA,EAAI,MAAK,GAAI,KAAA;AAGpC,EAAA,MAAM,SAAA,GAAY,CAAC,SAAA,KAClB,IAAA,IAAQ,SAAA,GAAY,EAAA,CAAG,SAAA,CAAU,EAAA,CAAG,GAAA,CAAI,SAAS,CAAC,CAAA,GAAI,QAAA,IAAY,SAAA,GAAa,SAAA,CAAU,MAAA,GAAqB,QAAA,CAAS,SAAA,CAAU,QAAQ,CAAA,CAAE,EAAE,KAAA,EAAO,SAAA,CAAU,KAAA,EAAO,KAAA,EAAO,SAAA,CAAU,KAAA,EAAgB,CAAA;AAEvM,EAAA,OAAO;AAAA,IACN,IAAA,EAAM,CAACA,MAAAA,KAAU,IAAA,CAAK,EAAE,KAAA,EAAOA,MAAAA,CAAM,MAAA,CAAO,MAAA,GAAS,GAAA,CAAIA,MAAAA,CAAM,OAAO,GAAA,CAAI,SAAS,CAAC,CAAA,GAAI,MAAA,EAAW,IAAA,EAAMA,OAAM,IAAA,EAAM,IAAA,EAAMA,MAAAA,CAAM,IAAA,EAAM;AAAA,GACxI;AACD","file":"chunk-A4ILIJJR.js","sourcesContent":["import type { TDataKitCondition, TDataKitOperator, TDataKitSort } from '../../types';\nimport type { TDataKitSource } from '../../types/server/source';\n\n// ** Build a source for any database: map the 11 operators to your query language once, then run the query in `list`\n// ** `between` always gets both ends; `in` / `nin` get an array; `contains` gets raw text (escape it); `null` gets true (is null) or false\nexport const dataKitSourceDefine = <TRow, TNative>(\n\tprops: Readonly<{\n\t\toperator: { [TKey in TDataKitOperator]: (props: Readonly<{ field: string; value: TKey extends 'in' | 'nin' ? unknown[] : TKey extends 'between' ? [unknown, unknown] : TKey extends 'null' ? boolean : unknown }>) => TNative };\n\t\tand: (list: TNative[]) => TNative;\n\t\tor: (list: TNative[]) => TNative;\n\t\tlist: (props: Readonly<{ where: TNative | undefined; sort: TDataKitSort[]; page: { offset: number; limit: number; count: boolean } }>) => Promise<{ rows: TRow[]; total: number | null }>;\n\t}>,\n): TDataKitSource<TRow> => {\n\t// ** Deconstruct Props\n\tconst { operator, and, or, list } = props;\n\n\t// ** A custom filter's `native` query passes through untouched\n\tconst translate = (condition: TDataKitCondition): TNative =>\n\t\t'or' in condition ? or(condition.or.map(translate)) : 'native' in condition ? (condition.native as TNative) : operator[condition.operator]({ field: condition.field, value: condition.value as never });\n\n\treturn {\n\t\tlist: (props) => list({ where: props.filter.length ? and(props.filter.map(translate)) : undefined, sort: props.sort, page: props.page }),\n\t};\n};\n"]}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// src/url/index.ts
|
|
4
|
+
var reserved = ["page", "size", "sort", "search"];
|
|
5
|
+
var dataKitUrlEncode = (props) => {
|
|
6
|
+
const { input, prefix, search, remove = [] } = props;
|
|
7
|
+
const start = prefix ? `${prefix}.` : "";
|
|
8
|
+
const params = new URLSearchParams(search);
|
|
9
|
+
[...params.keys()].filter((name) => prefix ? name.startsWith(start) : reserved.includes(name) || name in input.filter || remove.includes(name)).forEach((name) => params.delete(name));
|
|
10
|
+
if (input.page.number > 1) params.set(`${start}page`, String(input.page.number));
|
|
11
|
+
params.set(`${start}size`, String(input.page.size));
|
|
12
|
+
if (input.sort.length) params.set(`${start}sort`, input.sort.map((sort) => `${sort.column}.${sort.direction}`).join(","));
|
|
13
|
+
if (input.search) params.set(`${start}search`, input.search);
|
|
14
|
+
Object.entries(input.filter).forEach(([name, value]) => {
|
|
15
|
+
if (value) params.set(`${start}${name}`, value);
|
|
16
|
+
});
|
|
17
|
+
return params.toString();
|
|
18
|
+
};
|
|
19
|
+
var dataKitUrlDecode = (props) => {
|
|
20
|
+
const { searchParams, prefix } = props;
|
|
21
|
+
const start = prefix ? `${prefix}.` : "";
|
|
22
|
+
const entries = (searchParams instanceof URLSearchParams ? [...searchParams.entries()] : Object.entries(searchParams).map(([name, value]) => [name, Array.isArray(value) ? value[0] : value])).filter((entry) => typeof entry[1] === "string" && entry[0].startsWith(start)).map(([name, value]) => [name.slice(start.length), value]);
|
|
23
|
+
const values = Object.fromEntries(entries);
|
|
24
|
+
return {
|
|
25
|
+
page: { number: values.page, size: values.size },
|
|
26
|
+
sort: (values.sort ?? "").split(",").filter(Boolean).map((sort) => ({ column: sort.slice(0, sort.lastIndexOf(".")), direction: sort.slice(sort.lastIndexOf(".") + 1) })),
|
|
27
|
+
search: values.search,
|
|
28
|
+
filter: Object.fromEntries(entries.filter(([name]) => !reserved.includes(name)))
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// src/hook/index.ts
|
|
33
|
+
import { useEffect, useRef, useState, useTransition } from "react";
|
|
34
|
+
var useDataKit = (props) => {
|
|
35
|
+
const { data, source, append, refetch } = props;
|
|
36
|
+
const [isPending, startTransition] = useTransition();
|
|
37
|
+
const [result, setResult] = useState(data);
|
|
38
|
+
const [previous, setPrevious] = useState(data);
|
|
39
|
+
const [error, setError] = useState(null);
|
|
40
|
+
const [selected, setSelected] = useState([]);
|
|
41
|
+
const sequence = useRef(0);
|
|
42
|
+
if (data !== previous) {
|
|
43
|
+
setPrevious(data);
|
|
44
|
+
setResult(data);
|
|
45
|
+
setSelected((ids) => ids.filter((id) => data.rows.some((row) => row.id === id)));
|
|
46
|
+
}
|
|
47
|
+
const prefix = data.prefix;
|
|
48
|
+
const current = { page: result.page, sort: result.sort, filter: result.filter, ...result.search && { search: result.search } };
|
|
49
|
+
const url = (input) => `${window.location.pathname}?${dataKitUrlEncode({ input, prefix, search: window.location.search, remove: Object.keys(current.filter) })}`;
|
|
50
|
+
const go = (input, more) => {
|
|
51
|
+
setError(null);
|
|
52
|
+
if ("navigate" in source) {
|
|
53
|
+
startTransition(() => source.navigate(url(input)));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const request = ++sequence.current;
|
|
57
|
+
startTransition(async () => {
|
|
58
|
+
try {
|
|
59
|
+
const next2 = await source.fetch(input);
|
|
60
|
+
if (request !== sequence.current) return;
|
|
61
|
+
if (source.url && !more) window.history.pushState(null, "", url(next2));
|
|
62
|
+
startTransition(() => setResult((value) => more ? { ...next2, rows: [...value.rows, ...next2.rows.filter((row) => !value.rows.some((item) => item.id === row.id))] } : next2));
|
|
63
|
+
} catch (caught) {
|
|
64
|
+
if (request === sequence.current) setError(caught instanceof Error ? caught : new Error(String(caught)));
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
const refresh = () => "navigate" in source ? source.refresh?.() : go(current);
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if ("navigate" in source || !source.url) return;
|
|
71
|
+
const listener = () => go(dataKitUrlDecode({ searchParams: new URLSearchParams(window.location.search), prefix }));
|
|
72
|
+
window.addEventListener("popstate", listener);
|
|
73
|
+
return () => window.removeEventListener("popstate", listener);
|
|
74
|
+
});
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (!refetch) return;
|
|
77
|
+
const interval = setInterval(refresh, refetch.intervalMs);
|
|
78
|
+
return () => clearInterval(interval);
|
|
79
|
+
});
|
|
80
|
+
const count = result.total === null ? null : Math.max(1, Math.ceil(result.total / result.page.size));
|
|
81
|
+
const next = result.total === null ? result.rows.length >= result.page.size : result.page.number < (count ?? 1);
|
|
82
|
+
return {
|
|
83
|
+
prefix,
|
|
84
|
+
rows: result.rows,
|
|
85
|
+
total: result.total,
|
|
86
|
+
meta: result.meta,
|
|
87
|
+
page: { number: result.page.number, size: result.page.size, count, next },
|
|
88
|
+
sort: result.sort,
|
|
89
|
+
filter: result.filter,
|
|
90
|
+
search: result.search ?? "",
|
|
91
|
+
state: { isPending, error },
|
|
92
|
+
input: current,
|
|
93
|
+
url,
|
|
94
|
+
set: {
|
|
95
|
+
page: {
|
|
96
|
+
number: (number) => go({ ...current, page: { ...current.page, number } }, append && number === current.page.number + 1),
|
|
97
|
+
size: (size) => go({ ...current, page: { number: 1, size } })
|
|
98
|
+
},
|
|
99
|
+
sort: (sort) => go({ ...current, page: { ...current.page, number: 1 }, sort }),
|
|
100
|
+
// ** A changed filter or search always starts again from page 1; undefined or "" clears it
|
|
101
|
+
filter: (filter) => go({ ...current, page: { ...current.page, number: 1 }, filter: Object.fromEntries(Object.entries({ ...current.filter, ...filter }).filter((entry) => Boolean(entry[1]))) }),
|
|
102
|
+
search: (search) => go({ ...current, page: { ...current.page, number: 1 }, ...search.trim() ? { search: search.trim() } : { search: void 0 } })
|
|
103
|
+
},
|
|
104
|
+
refresh,
|
|
105
|
+
reset: () => go({ page: { number: 1, size: result.page.size }, sort: [], filter: {} }),
|
|
106
|
+
row: {
|
|
107
|
+
push: (props2) => setResult((value) => ({ ...value, total: value.total === null ? null : value.total + 1, rows: props2.position === "end" ? [...value.rows, props2.row] : [props2.row, ...value.rows] })),
|
|
108
|
+
update: (props2) => setResult((value) => ({ ...value, rows: value.rows.map((row) => row.id === props2.id ? { ...row, ...props2.data } : row) })),
|
|
109
|
+
remove: (props2) => setResult((value) => ({ ...value, total: value.total === null ? null : value.total - 1, rows: value.rows.filter((row) => row.id !== props2.id) }))
|
|
110
|
+
},
|
|
111
|
+
selection: {
|
|
112
|
+
ids: selected,
|
|
113
|
+
set: setSelected,
|
|
114
|
+
clear: () => setSelected([])
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export {
|
|
120
|
+
dataKitUrlEncode,
|
|
121
|
+
useDataKit
|
|
122
|
+
};
|
|
123
|
+
//# sourceMappingURL=chunk-BERW2A2B.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/url/index.ts","../src/hook/index.ts"],"sourcesContent":["import type { TDataKitInput } from '../types';\n\n// ** One format for both sides: the hook writes it, tableX.query reads it\n// ** ?page=2&size=20&sort=name.asc&search=ada&role=admin,user&age=18-30&createdAt=7d — with prefix \"user\": ?user.page=2…\n// ** Filter values are already their URL strings (the toolbar builds them), so this only moves strings around\n\nconst reserved = ['page', 'size', 'sort', 'search'];\n\n// ** `remove`: filter names the previous state had, so a filter that was cleared leaves the URL too\nexport const dataKitUrlEncode = (props: Readonly<{ input: TDataKitInput; prefix?: string; search?: string; remove?: string[] }>) => {\n\t// ** Deconstruct Props\n\tconst { input, prefix, search, remove = [] } = props;\n\n\tconst start = prefix ? `${prefix}.` : '';\n\tconst params = new URLSearchParams(search);\n\n\t// ** Drop this table's old params, keep everything else on the page (other tables, returnUrl, …)\n\t[...params.keys()].filter((name) => (prefix ? name.startsWith(start) : reserved.includes(name) || name in input.filter || remove.includes(name))).forEach((name) => params.delete(name));\n\n\tif (input.page.number > 1) params.set(`${start}page`, String(input.page.number));\n\tparams.set(`${start}size`, String(input.page.size));\n\tif (input.sort.length) params.set(`${start}sort`, input.sort.map((sort) => `${sort.column}.${sort.direction}`).join(','));\n\tif (input.search) params.set(`${start}search`, input.search);\n\tObject.entries(input.filter).forEach(([name, value]) => {\n\t\tif (value) params.set(`${start}${name}`, value);\n\t});\n\n\treturn params.toString();\n};\n\n// ** Raw strings, unvalidated: the table's schema decides what survives\nexport const dataKitUrlDecode = (props: Readonly<{ searchParams: URLSearchParams | Record<string, string | string[] | undefined>; prefix?: string }>) => {\n\t// ** Deconstruct Props\n\tconst { searchParams, prefix } = props;\n\n\tconst start = prefix ? `${prefix}.` : '';\n\tconst entries = (searchParams instanceof URLSearchParams ? [...searchParams.entries()] : Object.entries(searchParams).map(([name, value]) => [name, Array.isArray(value) ? value[0] : value] as [string, string | undefined]))\n\t\t.filter((entry): entry is [string, string] => typeof entry[1] === 'string' && entry[0].startsWith(start))\n\t\t.map(([name, value]) => [name.slice(start.length), value] as const);\n\tconst values = Object.fromEntries(entries);\n\n\treturn {\n\t\tpage: { number: values.page, size: values.size },\n\t\tsort: (values.sort ?? '')\n\t\t\t.split(',')\n\t\t\t.filter(Boolean)\n\t\t\t.map((sort) => ({ column: sort.slice(0, sort.lastIndexOf('.')), direction: sort.slice(sort.lastIndexOf('.') + 1) })),\n\t\tsearch: values.search,\n\t\tfilter: Object.fromEntries(entries.filter(([name]) => !reserved.includes(name))),\n\t};\n};\n","import { useEffect, useRef, useState, useTransition } from 'react';\nimport type { TDataKitData, TDataKitInput } from '../types';\nimport { dataKitUrlDecode, dataKitUrlEncode } from '../url';\n\n// ** One hook for a table's state: page, sort, filters, search, selection and local row edits\n// ** source.navigate: state lives in the URL and the server page renders the next `data` (Next: router.push)\n// ** source.fetch: your own authed action returns the next data (infinite scroll, apps without server rendering)\n// ** The URL prefix comes from `data.prefix` (set once in query({ prefix })), so several tables on a page never collide\nexport const useDataKit = <TRow extends { id: string }>(\n\tprops: Readonly<{\n\t\tdata: TDataKitData<TRow>;\n\t\tsource: { navigate: (url: string) => void; refresh?: () => void } | { fetch: (input: unknown) => Promise<TDataKitData<TRow>>; url?: boolean };\n\t\t// ** fetch only: add the next page under the current rows instead of replacing them\n\t\tappend?: boolean;\n\t\trefetch?: { intervalMs: number };\n\t}>,\n) => {\n\t// ** Deconstruct Props\n\tconst { data, source, append, refetch } = props;\n\n\tconst [isPending, startTransition] = useTransition();\n\tconst [result, setResult] = useState(data);\n\tconst [previous, setPrevious] = useState(data);\n\tconst [error, setError] = useState<Error | null>(null);\n\tconst [selected, setSelected] = useState<string[]>([]);\n\t// ** Only the latest request may apply its answer (fast typing, slow network)\n\tconst sequence = useRef(0);\n\n\t// ** New server-rendered `data` (navigation, router.refresh) replaces local state\n\tif (data !== previous) {\n\t\tsetPrevious(data);\n\t\tsetResult(data);\n\t\t// ** Rows that are gone (deleted, filtered out) drop out of the selection\n\t\tsetSelected((ids) => ids.filter((id) => data.rows.some((row) => row.id === id)));\n\t}\n\n\tconst prefix = data.prefix;\n\tconst current: TDataKitInput = { page: result.page, sort: result.sort, filter: result.filter, ...(result.search && { search: result.search }) };\n\tconst url = (input: TDataKitInput) => `${window.location.pathname}?${dataKitUrlEncode({ input, prefix, search: window.location.search, remove: Object.keys(current.filter) })}`;\n\n\tconst go = (input: TDataKitInput, more?: boolean) => {\n\t\tsetError(null);\n\t\tif ('navigate' in source) {\n\t\t\tstartTransition(() => source.navigate(url(input)));\n\t\t\treturn;\n\t\t}\n\t\tconst request = ++sequence.current;\n\t\tstartTransition(async () => {\n\t\t\ttry {\n\t\t\t\tconst next = await source.fetch(input);\n\t\t\t\tif (request !== sequence.current) return;\n\t\t\t\tif (source.url && !more) window.history.pushState(null, '', url(next));\n\t\t\t\tstartTransition(() => setResult((value) => (more ? { ...next, rows: [...value.rows, ...next.rows.filter((row) => !value.rows.some((item) => item.id === row.id))] } : next)));\n\t\t\t} catch (caught) {\n\t\t\t\tif (request === sequence.current) setError(caught instanceof Error ? caught : new Error(String(caught)));\n\t\t\t}\n\t\t});\n\t};\n\n\tconst refresh = () => ('navigate' in source ? source.refresh?.() : go(current));\n\n\t// ** fetch + url: back / forward re-reads the URL\n\tuseEffect(() => {\n\t\tif ('navigate' in source || !source.url) return;\n\t\tconst listener = () => go(dataKitUrlDecode({ searchParams: new URLSearchParams(window.location.search), prefix }) as unknown as TDataKitInput);\n\t\twindow.addEventListener('popstate', listener);\n\t\treturn () => window.removeEventListener('popstate', listener);\n\t});\n\n\tuseEffect(() => {\n\t\tif (!refetch) return;\n\t\tconst interval = setInterval(refresh, refetch.intervalMs);\n\t\treturn () => clearInterval(interval);\n\t});\n\n\t// ** Without a count, there's a next page whenever this one is full\n\tconst count = result.total === null ? null : Math.max(1, Math.ceil(result.total / result.page.size));\n\tconst next = result.total === null ? result.rows.length >= result.page.size : result.page.number < (count ?? 1);\n\n\treturn {\n\t\tprefix,\n\t\trows: result.rows,\n\t\ttotal: result.total,\n\t\tmeta: result.meta,\n\t\tpage: { number: result.page.number, size: result.page.size, count, next },\n\t\tsort: result.sort,\n\t\tfilter: result.filter,\n\t\tsearch: result.search ?? '',\n\t\tstate: { isPending, error },\n\t\tinput: current,\n\t\turl,\n\t\tset: {\n\t\t\tpage: {\n\t\t\t\tnumber: (number: number) => go({ ...current, page: { ...current.page, number } }, append && number === current.page.number + 1),\n\t\t\t\tsize: (size: number) => go({ ...current, page: { number: 1, size } }),\n\t\t\t},\n\t\t\tsort: (sort: TDataKitInput['sort']) => go({ ...current, page: { ...current.page, number: 1 }, sort }),\n\t\t\t// ** A changed filter or search always starts again from page 1; undefined or \"\" clears it\n\t\t\tfilter: (filter: Record<string, string | undefined>) =>\n\t\t\t\tgo({ ...current, page: { ...current.page, number: 1 }, filter: Object.fromEntries(Object.entries({ ...current.filter, ...filter }).filter((entry): entry is [string, string] => Boolean(entry[1]))) }),\n\t\t\tsearch: (search: string) => go({ ...current, page: { ...current.page, number: 1 }, ...(search.trim() ? { search: search.trim() } : { search: undefined }) }),\n\t\t},\n\t\trefresh,\n\t\treset: () => go({ page: { number: 1, size: result.page.size }, sort: [], filter: {} }),\n\t\trow: {\n\t\t\tpush: (props: Readonly<{ row: TRow; position?: 'start' | 'end' }>) => setResult((value) => ({ ...value, total: value.total === null ? null : value.total + 1, rows: props.position === 'end' ? [...value.rows, props.row] : [props.row, ...value.rows] })),\n\t\t\tupdate: (props: Readonly<{ id: string; data: Partial<TRow> }>) => setResult((value) => ({ ...value, rows: value.rows.map((row) => (row.id === props.id ? { ...row, ...props.data } : row)) })),\n\t\t\tremove: (props: Readonly<{ id: string }>) => setResult((value) => ({ ...value, total: value.total === null ? null : value.total - 1, rows: value.rows.filter((row) => row.id !== props.id) })),\n\t\t},\n\t\tselection: {\n\t\t\tids: selected,\n\t\t\tset: setSelected,\n\t\t\tclear: () => setSelected([]),\n\t\t},\n\t};\n};\n"],"mappings":";;;AAMA,IAAM,WAAW,CAAC,QAAQ,QAAQ,QAAQ,QAAQ;AAG3C,IAAM,mBAAmB,CAAC,UAAmG;AAEnI,QAAM,EAAE,OAAO,QAAQ,QAAQ,SAAS,CAAC,EAAE,IAAI;AAE/C,QAAM,QAAQ,SAAS,GAAG,MAAM,MAAM;AACtC,QAAM,SAAS,IAAI,gBAAgB,MAAM;AAGzC,GAAC,GAAG,O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,MAAM,YAAY,CAAC,CAAC;AAAA,IAC5B;AAAA,EACD;AACD;","names":["next","props"]}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type TDataKitSort = {
|
|
4
|
+
field: string;
|
|
5
|
+
direction: 'asc' | 'desc';
|
|
6
|
+
};
|
|
7
|
+
type TDataKitVariant = 'text' | 'number' | 'numberCompare' | 'range' | 'slider' | 'select' | 'multiSelect' | 'tags' | 'boolean' | 'exists' | 'date' | 'dateRange' | 'dateTimeRange';
|
|
8
|
+
type TDataKitCompare = 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte';
|
|
9
|
+
type TDataKitInput = {
|
|
10
|
+
page: {
|
|
11
|
+
number: number;
|
|
12
|
+
size: number;
|
|
13
|
+
};
|
|
14
|
+
sort: {
|
|
15
|
+
column: string;
|
|
16
|
+
direction: 'asc' | 'desc';
|
|
17
|
+
}[];
|
|
18
|
+
filter: Record<string, string>;
|
|
19
|
+
search?: string;
|
|
20
|
+
};
|
|
21
|
+
type TDataKitColumnMeta = {
|
|
22
|
+
sort: boolean;
|
|
23
|
+
filter?: {
|
|
24
|
+
variant: TDataKitVariant | 'custom';
|
|
25
|
+
option?: string[];
|
|
26
|
+
preset?: string[];
|
|
27
|
+
bound?: {
|
|
28
|
+
min: number;
|
|
29
|
+
max: number;
|
|
30
|
+
step: number;
|
|
31
|
+
};
|
|
32
|
+
compare?: TDataKitCompare[];
|
|
33
|
+
hidden: boolean;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
type TDataKitData<TRow> = TDataKitInput & {
|
|
37
|
+
rows: TRow[];
|
|
38
|
+
total: number | null;
|
|
39
|
+
prefix?: string;
|
|
40
|
+
meta: {
|
|
41
|
+
column: Record<string, TDataKitColumnMeta>;
|
|
42
|
+
search: boolean;
|
|
43
|
+
sort: TDataKitInput['sort'];
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
declare const useDataKit: <TRow extends {
|
|
48
|
+
id: string;
|
|
49
|
+
}>(props: Readonly<{
|
|
50
|
+
data: TDataKitData<TRow>;
|
|
51
|
+
source: {
|
|
52
|
+
navigate: (url: string) => void;
|
|
53
|
+
refresh?: () => void;
|
|
54
|
+
} | {
|
|
55
|
+
fetch: (input: unknown) => Promise<TDataKitData<TRow>>;
|
|
56
|
+
url?: boolean;
|
|
57
|
+
};
|
|
58
|
+
append?: boolean;
|
|
59
|
+
refetch?: {
|
|
60
|
+
intervalMs: number;
|
|
61
|
+
};
|
|
62
|
+
}>) => {
|
|
63
|
+
prefix: string | undefined;
|
|
64
|
+
rows: TRow[];
|
|
65
|
+
total: number | null;
|
|
66
|
+
meta: {
|
|
67
|
+
column: Record<string, TDataKitColumnMeta>;
|
|
68
|
+
search: boolean;
|
|
69
|
+
sort: TDataKitInput["sort"];
|
|
70
|
+
};
|
|
71
|
+
page: {
|
|
72
|
+
number: number;
|
|
73
|
+
size: number;
|
|
74
|
+
count: number | null;
|
|
75
|
+
next: boolean;
|
|
76
|
+
};
|
|
77
|
+
sort: {
|
|
78
|
+
column: string;
|
|
79
|
+
direction: "asc" | "desc";
|
|
80
|
+
}[];
|
|
81
|
+
filter: Record<string, string>;
|
|
82
|
+
search: string;
|
|
83
|
+
state: {
|
|
84
|
+
isPending: boolean;
|
|
85
|
+
error: Error | null;
|
|
86
|
+
};
|
|
87
|
+
input: TDataKitInput;
|
|
88
|
+
url: (input: TDataKitInput) => string;
|
|
89
|
+
set: {
|
|
90
|
+
page: {
|
|
91
|
+
number: (number: number) => void;
|
|
92
|
+
size: (size: number) => void;
|
|
93
|
+
};
|
|
94
|
+
sort: (sort: TDataKitInput["sort"]) => void;
|
|
95
|
+
filter: (filter: Record<string, string | undefined>) => void;
|
|
96
|
+
search: (search: string) => void;
|
|
97
|
+
};
|
|
98
|
+
refresh: () => void;
|
|
99
|
+
reset: () => void;
|
|
100
|
+
row: {
|
|
101
|
+
push: (props: Readonly<{
|
|
102
|
+
row: TRow;
|
|
103
|
+
position?: "start" | "end";
|
|
104
|
+
}>) => void;
|
|
105
|
+
update: (props: Readonly<{
|
|
106
|
+
id: string;
|
|
107
|
+
data: Partial<TRow>;
|
|
108
|
+
}>) => void;
|
|
109
|
+
remove: (props: Readonly<{
|
|
110
|
+
id: string;
|
|
111
|
+
}>) => void;
|
|
112
|
+
};
|
|
113
|
+
selection: {
|
|
114
|
+
ids: string[];
|
|
115
|
+
set: react.Dispatch<react.SetStateAction<string[]>>;
|
|
116
|
+
clear: () => void;
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export { type TDataKitInput as T, type TDataKitData as a, type TDataKitColumnMeta as b, type TDataKitCompare as c, type TDataKitSort as d, type TDataKitVariant as e, useDataKit as u };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,85 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
type TDataKitInput = {
|
|
6
|
-
page: {
|
|
7
|
-
number: number;
|
|
8
|
-
size: number;
|
|
9
|
-
};
|
|
10
|
-
sort: TDataKitSort[];
|
|
11
|
-
filter: Record<string, unknown>;
|
|
12
|
-
};
|
|
13
|
-
type TDataKitResult<TItem> = TDataKitInput & {
|
|
14
|
-
items: TItem[];
|
|
15
|
-
total: number;
|
|
16
|
-
};
|
|
1
|
+
import { T as TDataKitInput, a as TDataKitData } from './index-j8_iNoy6.js';
|
|
2
|
+
export { b as TDataKitColumnMeta, c as TDataKitCompare, d as TDataKitSort, e as TDataKitVariant, u as useDataKit } from './index-j8_iNoy6.js';
|
|
3
|
+
import 'react';
|
|
17
4
|
|
|
18
|
-
declare const
|
|
19
|
-
id: string;
|
|
20
|
-
}>(props: Readonly<{
|
|
21
|
-
initial: TDataKitResult<TItem>;
|
|
22
|
-
source: {
|
|
23
|
-
navigate: (url: string) => void;
|
|
24
|
-
refresh?: () => void;
|
|
25
|
-
} | {
|
|
26
|
-
fetch: (input: unknown) => Promise<TDataKitResult<TItem>>;
|
|
27
|
-
url?: boolean;
|
|
28
|
-
};
|
|
29
|
-
key?: string;
|
|
30
|
-
append?: boolean;
|
|
31
|
-
refetch?: {
|
|
32
|
-
intervalMs: number;
|
|
33
|
-
};
|
|
34
|
-
}>) => {
|
|
35
|
-
key: string | undefined;
|
|
36
|
-
items: TItem[];
|
|
37
|
-
total: number;
|
|
38
|
-
page: {
|
|
39
|
-
number: number;
|
|
40
|
-
size: number;
|
|
41
|
-
count: number;
|
|
42
|
-
};
|
|
43
|
-
sort: TDataKitSort[];
|
|
44
|
-
filter: Record<string, unknown>;
|
|
45
|
-
state: {
|
|
46
|
-
isPending: boolean;
|
|
47
|
-
error: Error | null;
|
|
48
|
-
};
|
|
49
|
-
set: {
|
|
50
|
-
page: (number: number) => void;
|
|
51
|
-
size: (size: number) => void;
|
|
52
|
-
sort: (sort: TDataKitSort[]) => void;
|
|
53
|
-
filter: (filter: Record<string, unknown>) => void;
|
|
54
|
-
};
|
|
55
|
-
refresh: () => void;
|
|
56
|
-
reset: () => void;
|
|
57
|
-
item: {
|
|
58
|
-
push: (props: Readonly<{
|
|
59
|
-
item: TItem;
|
|
60
|
-
position?: "start" | "end";
|
|
61
|
-
}>) => void;
|
|
62
|
-
update: (props: Readonly<{
|
|
63
|
-
id: string;
|
|
64
|
-
data: Partial<TItem>;
|
|
65
|
-
}>) => void;
|
|
66
|
-
remove: (props: Readonly<{
|
|
67
|
-
id: string;
|
|
68
|
-
}>) => void;
|
|
69
|
-
};
|
|
70
|
-
selection: {
|
|
71
|
-
ids: string[];
|
|
72
|
-
has: (id: string) => boolean;
|
|
73
|
-
toggle: (id: string) => void;
|
|
74
|
-
all: () => void;
|
|
75
|
-
clear: () => void;
|
|
76
|
-
};
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
declare const urlEncode: (props: Readonly<{
|
|
5
|
+
declare const dataKitUrlEncode: (props: Readonly<{
|
|
80
6
|
input: TDataKitInput;
|
|
81
|
-
|
|
7
|
+
prefix?: string;
|
|
82
8
|
search?: string;
|
|
9
|
+
remove?: string[];
|
|
83
10
|
}>) => string;
|
|
84
11
|
|
|
85
|
-
|
|
12
|
+
type TDataKitTableAny = {
|
|
13
|
+
query: (...args: any[]) => Promise<TDataKitData<any>>;
|
|
14
|
+
};
|
|
15
|
+
type TDataKitTableData<TTable extends TDataKitTableAny> = Awaited<ReturnType<TTable['query']>>;
|
|
16
|
+
type TDataKitTableRow<TTable extends TDataKitTableAny> = TDataKitTableData<TTable>['rows'][number];
|
|
17
|
+
|
|
18
|
+
export { TDataKitData, TDataKitInput, type TDataKitTableData, type TDataKitTableRow, dataKitUrlEncode };
|
package/dist/index.js
CHANGED
|
@@ -1,110 +1,10 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// src/url/index.ts
|
|
7
|
-
var urlEncode = (props) => {
|
|
8
|
-
const { input, key, search } = props;
|
|
9
|
-
const prefix = key ? `${key}.` : "";
|
|
10
|
-
const params = new URLSearchParams(search);
|
|
11
|
-
[...params.keys()].filter((name) => key ? name.startsWith(prefix) : ["page", "size", "sort"].includes(name) || name in input.filter).forEach((name) => params.delete(name));
|
|
12
|
-
if (input.page.number > 1) params.set(`${prefix}page`, String(input.page.number));
|
|
13
|
-
params.set(`${prefix}size`, String(input.page.size));
|
|
14
|
-
if (input.sort.length) params.set(`${prefix}sort`, input.sort.map((sort) => `${sort.field}.${sort.direction}`).join(","));
|
|
15
|
-
Object.entries(input.filter).forEach(([name, value]) => {
|
|
16
|
-
if (value !== void 0 && value !== null && value !== "") params.set(`${prefix}${name}`, String(value));
|
|
17
|
-
});
|
|
18
|
-
return params.toString();
|
|
19
|
-
};
|
|
20
|
-
var urlDecode = (props) => {
|
|
21
|
-
const { searchParams, key } = props;
|
|
22
|
-
const prefix = key ? `${key}.` : "";
|
|
23
|
-
const entries = (searchParams instanceof URLSearchParams ? [...searchParams.entries()] : Object.entries(searchParams).map(([name, value]) => [name, Array.isArray(value) ? value[0] : value])).filter((entry) => typeof entry[1] === "string" && entry[0].startsWith(prefix)).map(([name, value]) => [name.slice(prefix.length), value]);
|
|
24
|
-
const values = Object.fromEntries(entries);
|
|
25
|
-
return {
|
|
26
|
-
page: { number: values.page, size: values.size },
|
|
27
|
-
sort: (values.sort ?? "").split(",").filter(Boolean).map((sort) => ({ field: sort.slice(0, sort.lastIndexOf(".")), direction: sort.slice(sort.lastIndexOf(".") + 1) })),
|
|
28
|
-
filter: Object.fromEntries(entries.filter(([name]) => !["page", "size", "sort"].includes(name)))
|
|
29
|
-
};
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
// src/hook/index.ts
|
|
33
|
-
var useDataKit = (props) => {
|
|
34
|
-
const { initial, source, key, append, refetch } = props;
|
|
35
|
-
const [isPending, startTransition] = useTransition();
|
|
36
|
-
const [result, setResult] = useState(initial);
|
|
37
|
-
const [previous, setPrevious] = useState(initial);
|
|
38
|
-
const [error, setError] = useState(null);
|
|
39
|
-
const [selected, setSelected] = useState([]);
|
|
40
|
-
if (initial !== previous) {
|
|
41
|
-
setPrevious(initial);
|
|
42
|
-
setResult(initial);
|
|
43
|
-
}
|
|
44
|
-
const go = (input, more) => {
|
|
45
|
-
setError(null);
|
|
46
|
-
if ("navigate" in source) {
|
|
47
|
-
startTransition(() => source.navigate(`${window.location.pathname}?${urlEncode({ input, key, search: window.location.search })}`));
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
startTransition(async () => {
|
|
51
|
-
try {
|
|
52
|
-
const next = await source.fetch(input);
|
|
53
|
-
if (source.url && !more) window.history.pushState(null, "", `${window.location.pathname}?${urlEncode({ input: next, key, search: window.location.search })}`);
|
|
54
|
-
startTransition(() => setResult((current2) => more ? { ...next, items: [...current2.items, ...next.items] } : next));
|
|
55
|
-
} catch (caught) {
|
|
56
|
-
setError(caught instanceof Error ? caught : new Error(String(caught)));
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
};
|
|
60
|
-
const current = { page: result.page, sort: result.sort, filter: result.filter };
|
|
61
|
-
const refresh = () => "navigate" in source ? source.refresh?.() : go(current);
|
|
62
|
-
useEffect(() => {
|
|
63
|
-
if ("navigate" in source || !source.url) return;
|
|
64
|
-
const listener = () => go(urlDecode({ searchParams: new URLSearchParams(window.location.search), key }));
|
|
65
|
-
window.addEventListener("popstate", listener);
|
|
66
|
-
return () => window.removeEventListener("popstate", listener);
|
|
67
|
-
});
|
|
68
|
-
useEffect(() => {
|
|
69
|
-
if (!refetch) return;
|
|
70
|
-
const interval = setInterval(refresh, refetch.intervalMs);
|
|
71
|
-
return () => clearInterval(interval);
|
|
72
|
-
});
|
|
73
|
-
const count = Math.max(1, Math.ceil(result.total / result.page.size));
|
|
74
|
-
return {
|
|
75
|
-
key,
|
|
76
|
-
items: result.items,
|
|
77
|
-
total: result.total,
|
|
78
|
-
page: { number: result.page.number, size: result.page.size, count },
|
|
79
|
-
sort: result.sort,
|
|
80
|
-
filter: result.filter,
|
|
81
|
-
state: { isPending, error },
|
|
82
|
-
set: {
|
|
83
|
-
page: (number) => go({ ...current, page: { ...current.page, number } }, append && number === current.page.number + 1),
|
|
84
|
-
size: (size) => go({ ...current, page: { number: 1, size } }),
|
|
85
|
-
sort: (sort) => go({ ...current, page: { ...current.page, number: 1 }, sort }),
|
|
86
|
-
// ** A changed filter always starts again from page 1
|
|
87
|
-
filter: (filter) => go({ ...current, page: { ...current.page, number: 1 }, filter: { ...current.filter, ...filter } })
|
|
88
|
-
},
|
|
89
|
-
refresh,
|
|
90
|
-
reset: () => go({ page: { number: 1, size: result.page.size }, sort: [], filter: Object.fromEntries(Object.keys(current.filter).map((name) => [name, void 0])) }),
|
|
91
|
-
item: {
|
|
92
|
-
push: (props2) => setResult((value) => ({ ...value, total: value.total + 1, items: props2.position === "end" ? [...value.items, props2.item] : [props2.item, ...value.items] })),
|
|
93
|
-
update: (props2) => setResult((value) => ({ ...value, items: value.items.map((item) => item.id === props2.id ? { ...item, ...props2.data } : item) })),
|
|
94
|
-
remove: (props2) => setResult((value) => ({ ...value, total: value.total - 1, items: value.items.filter((item) => item.id !== props2.id) }))
|
|
95
|
-
},
|
|
96
|
-
selection: {
|
|
97
|
-
ids: selected,
|
|
98
|
-
has: (id) => selected.includes(id),
|
|
99
|
-
toggle: (id) => setSelected((ids) => ids.includes(id) ? ids.filter((value) => value !== id) : [...ids, id]),
|
|
100
|
-
// ** Selects every row on this page, or clears them when they're all selected already
|
|
101
|
-
all: () => setSelected((ids) => result.items.every((item) => ids.includes(item.id)) ? [] : result.items.map((item) => item.id)),
|
|
102
|
-
clear: () => setSelected([])
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
};
|
|
2
|
+
import {
|
|
3
|
+
dataKitUrlEncode,
|
|
4
|
+
useDataKit
|
|
5
|
+
} from "./chunk-BERW2A2B.js";
|
|
106
6
|
export {
|
|
107
|
-
|
|
7
|
+
dataKitUrlEncode,
|
|
108
8
|
useDataKit
|
|
109
9
|
};
|
|
110
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hook/index.ts","../src/url/index.ts"],"sourcesContent":["import { useEffect, useState, useTransition } from 'react';\nimport type { TDataKitInput, TDataKitResult, TDataKitSort } from '../types';\nimport { urlDecode, urlEncode } from '../url';\n\n// ** One hook for a table's state: page, sort, filter, selection and local item edits\n// ** source.navigate: state lives in the URL and the server page renders the next `initial` (Next: router.push)\n// ** source.fetch: the host's own authed action returns the next result (infinite scroll, apps without server rendering)\nexport const useDataKit = <TItem extends { id: string }>(\n\tprops: Readonly<{\n\t\tinitial: TDataKitResult<TItem>;\n\t\tsource: { navigate: (url: string) => void; refresh?: () => void } | { fetch: (input: unknown) => Promise<TDataKitResult<TItem>>; url?: boolean };\n\t\tkey?: string;\n\t\t// ** fetch only: add the next page under the current items instead of replacing them\n\t\tappend?: boolean;\n\t\trefetch?: { intervalMs: number };\n\t}>,\n) => {\n\t// ** Deconstruct Props\n\tconst { initial, source, key, append, refetch } = props;\n\n\tconst [isPending, startTransition] = useTransition();\n\tconst [result, setResult] = useState(initial);\n\tconst [previous, setPrevious] = useState(initial);\n\tconst [error, setError] = useState<Error | null>(null);\n\tconst [selected, setSelected] = useState<string[]>([]);\n\n\t// ** A new server-rendered `initial` (navigation, router.refresh) replaces local state\n\tif (initial !== previous) {\n\t\tsetPrevious(initial);\n\t\tsetResult(initial);\n\t}\n\n\tconst go = (input: TDataKitInput | unknown, more?: boolean) => {\n\t\tsetError(null);\n\t\tif ('navigate' in source) {\n\t\t\tstartTransition(() => source.navigate(`${window.location.pathname}?${urlEncode({ input: input as TDataKitInput, key, search: window.location.search })}`));\n\t\t\treturn;\n\t\t}\n\t\tstartTransition(async () => {\n\t\t\ttry {\n\t\t\t\tconst next = await source.fetch(input);\n\t\t\t\tif (source.url && !more) window.history.pushState(null, '', `${window.location.pathname}?${urlEncode({ input: next, key, search: window.location.search })}`);\n\t\t\t\tstartTransition(() => setResult((current) => (more ? { ...next, items: [...current.items, ...next.items] } : next)));\n\t\t\t} catch (caught) {\n\t\t\t\tsetError(caught instanceof Error ? caught : new Error(String(caught)));\n\t\t\t}\n\t\t});\n\t};\n\n\tconst current: TDataKitInput = { page: result.page, sort: result.sort, filter: result.filter };\n\tconst refresh = () => ('navigate' in source ? source.refresh?.() : go(current));\n\n\t// ** fetch + url: back/forward re-reads the URL\n\tuseEffect(() => {\n\t\tif ('navigate' in source || !source.url) return;\n\t\tconst listener = () => go(urlDecode({ searchParams: new URLSearchParams(window.location.search), key }));\n\t\twindow.addEventListener('popstate', listener);\n\t\treturn () => window.removeEventListener('popstate', listener);\n\t});\n\n\tuseEffect(() => {\n\t\tif (!refetch) return;\n\t\tconst interval = setInterval(refresh, refetch.intervalMs);\n\t\treturn () => clearInterval(interval);\n\t});\n\n\tconst count = Math.max(1, Math.ceil(result.total / result.page.size));\n\n\treturn {\n\t\tkey,\n\t\titems: result.items,\n\t\ttotal: result.total,\n\t\tpage: { number: result.page.number, size: result.page.size, count },\n\t\tsort: result.sort,\n\t\tfilter: result.filter,\n\t\tstate: { isPending, error },\n\t\tset: {\n\t\t\tpage: (number: number) => go({ ...current, page: { ...current.page, number } }, append && number === current.page.number + 1),\n\t\t\tsize: (size: number) => go({ ...current, page: { number: 1, size } }),\n\t\t\tsort: (sort: TDataKitSort[]) => go({ ...current, page: { ...current.page, number: 1 }, sort }),\n\t\t\t// ** A changed filter always starts again from page 1\n\t\t\tfilter: (filter: Record<string, unknown>) => go({ ...current, page: { ...current.page, number: 1 }, filter: { ...current.filter, ...filter } }),\n\t\t},\n\t\trefresh,\n\t\treset: () => go({ page: { number: 1, size: result.page.size }, sort: [], filter: Object.fromEntries(Object.keys(current.filter).map((name) => [name, undefined])) }),\n\t\titem: {\n\t\t\tpush: (props: Readonly<{ item: TItem; position?: 'start' | 'end' }>) => setResult((value) => ({ ...value, total: value.total + 1, items: props.position === 'end' ? [...value.items, props.item] : [props.item, ...value.items] })),\n\t\t\tupdate: (props: Readonly<{ id: string; data: Partial<TItem> }>) => setResult((value) => ({ ...value, items: value.items.map((item) => (item.id === props.id ? { ...item, ...props.data } : item)) })),\n\t\t\tremove: (props: Readonly<{ id: string }>) => setResult((value) => ({ ...value, total: value.total - 1, items: value.items.filter((item) => item.id !== props.id) })),\n\t\t},\n\t\tselection: {\n\t\t\tids: selected,\n\t\t\thas: (id: string) => selected.includes(id),\n\t\t\ttoggle: (id: string) => setSelected((ids) => (ids.includes(id) ? ids.filter((value) => value !== id) : [...ids, id])),\n\t\t\t// ** Selects every row on this page, or clears them when they're all selected already\n\t\t\tall: () => setSelected((ids) => (result.items.every((item) => ids.includes(item.id)) ? [] : result.items.map((item) => item.id))),\n\t\t\tclear: () => setSelected([]),\n\t\t},\n\t};\n};\n","import type { TDataKitInput } from '../types';\n\n// ** One format for both sides: the hook writes it, kit.parse reads it\n// ** ?page=2&size=20&sort=name.asc,createdAt.desc&search=x — with key \"user\": ?user.page=2&user.sort=…\n\nexport const urlEncode = (props: Readonly<{ input: TDataKitInput; key?: string; search?: string }>) => {\n\t// ** Deconstruct Props\n\tconst { input, key, search } = props;\n\n\tconst prefix = key ? `${key}.` : '';\n\tconst params = new URLSearchParams(search);\n\n\t// ** Drop this kit's old params, keep everything else on the page (other kits, returnUrl, …)\n\t[...params.keys()].filter((name) => (key ? name.startsWith(prefix) : ['page', 'size', 'sort'].includes(name) || name in input.filter)).forEach((name) => params.delete(name));\n\n\tif (input.page.number > 1) params.set(`${prefix}page`, String(input.page.number));\n\tparams.set(`${prefix}size`, String(input.page.size));\n\tif (input.sort.length) params.set(`${prefix}sort`, input.sort.map((sort) => `${sort.field}.${sort.direction}`).join(','));\n\tObject.entries(input.filter).forEach(([name, value]) => {\n\t\tif (value !== undefined && value !== null && value !== '') params.set(`${prefix}${name}`, String(value));\n\t});\n\n\treturn params.toString();\n};\n\n// ** Raw, unvalidated values: kit.schema decides what survives\nexport const urlDecode = (props: Readonly<{ searchParams: URLSearchParams | Record<string, string | string[] | undefined>; key?: string }>) => {\n\t// ** Deconstruct Props\n\tconst { searchParams, key } = props;\n\n\tconst prefix = key ? `${key}.` : '';\n\tconst entries = (searchParams instanceof URLSearchParams ? [...searchParams.entries()] : Object.entries(searchParams).map(([name, value]) => [name, Array.isArray(value) ? value[0] : value] as [string, string | undefined]))\n\t\t.filter((entry): entry is [string, string] => typeof entry[1] === 'string' && entry[0].startsWith(prefix))\n\t\t.map(([name, value]) => [name.slice(prefix.length), value] as const);\n\tconst values = Object.fromEntries(entries);\n\n\treturn {\n\t\tpage: { number: values.page, size: values.size },\n\t\tsort: (values.sort ?? '')\n\t\t\t.split(',')\n\t\t\t.filter(Boolean)\n\t\t\t.map((sort) => ({ field: sort.slice(0, sort.lastIndexOf('.')), direction: sort.slice(sort.lastIndexOf('.') + 1) })),\n\t\tfilter: Object.fromEntries(entries.filter(([name]) => !['page', 'size', 'sort'].includes(name))),\n\t};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A,OAAM,IAAI,IAAI,CAACA,OAAM,MAAM,GAAG,MAAM,KAAK,EAAE,EAAE;AAAA,MAClO,QAAQ,CAACA,WAA0D,UAAU,CAAC,WAAW,EAAE,GAAG,OAAO,OAAO,MAAM,MAAM,IAAI,CAAC,SAAU,KAAK,OAAOA,OAAM,KAAK,EAAE,GAAG,MAAM,GAAGA,OAAM,KAAK,IAAI,IAAK,EAAE,EAAE;AAAA,MACpM,QAAQ,CAACA,WAAoC,UAAU,CAAC,WAAW,EAAE,GAAG,OAAO,OAAO,MAAM,QAAQ,GAAG,OAAO,MAAM,MAAM,OAAO,CAAC,SAAS,KAAK,OAAOA,OAAM,EAAE,EAAE,EAAE;AAAA,IACpK;AAAA,IACA,WAAW;AAAA,MACV,KAAK;AAAA,MACL,KAAK,CAAC,OAAe,SAAS,SAAS,EAAE;AAAA,MACzC,QAAQ,CAAC,OAAe,YAAY,CAAC,QAAS,IAAI,SAAS,EAAE,IAAI,IAAI,OAAO,CAAC,UAAU,UAAU,EAAE,IAAI,CAAC,GAAG,KAAK,EAAE,CAAE;AAAA;AAAA,MAEpH,KAAK,MAAM,YAAY,CAAC,QAAS,OAAO,MAAM,MAAM,CAAC,SAAS,IAAI,SAAS,KAAK,EAAE,CAAC,IAAI,CAAC,IAAI,OAAO,MAAM,IAAI,CAAC,SAAS,KAAK,EAAE,CAAE;AAAA,MAChI,OAAO,MAAM,YAAY,CAAC,CAAC;AAAA,IAC5B;AAAA,EACD;AACD;","names":["current","props"]}
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { b as TDataKitSource } from '../source-BPO-9lNK.js';
|
|
2
|
+
import { Table } from 'drizzle-orm';
|
|
3
|
+
|
|
4
|
+
declare const dataKitSourceDrizzle: <TTable extends Table>(props: Readonly<{
|
|
5
|
+
db: any;
|
|
6
|
+
table: TTable;
|
|
7
|
+
id?: string;
|
|
8
|
+
dialect?: "pg" | "mysql" | "sqlite";
|
|
9
|
+
}>) => TDataKitSource<TTable["$inferSelect"]>;
|
|
10
|
+
|
|
11
|
+
export { dataKitSourceDrizzle };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { dataKitSourceDefine } from '../chunk-A4ILIJJR.js';
|
|
2
|
+
import { getTableColumns, or, and, isNull, isNotNull, notInArray, inArray, gte, gt, lte, lt, ne, eq, asc, desc, sql } from 'drizzle-orm';
|
|
3
|
+
|
|
4
|
+
var dataKitSourceDrizzle = (props) => {
|
|
5
|
+
const { db, table, id = "id", dialect = "pg" } = props;
|
|
6
|
+
const columns = getTableColumns(table);
|
|
7
|
+
const column = (field) => {
|
|
8
|
+
if (!columns[field]) throw new Error(`[next-data-kit] "${field}" isn't a column of this table.`);
|
|
9
|
+
return columns[field];
|
|
10
|
+
};
|
|
11
|
+
return dataKitSourceDefine({
|
|
12
|
+
operator: {
|
|
13
|
+
eq: (props2) => eq(column(props2.field), props2.value),
|
|
14
|
+
ne: (props2) => ne(column(props2.field), props2.value),
|
|
15
|
+
lt: (props2) => lt(column(props2.field), props2.value),
|
|
16
|
+
lte: (props2) => lte(column(props2.field), props2.value),
|
|
17
|
+
gt: (props2) => gt(column(props2.field), props2.value),
|
|
18
|
+
gte: (props2) => gte(column(props2.field), props2.value),
|
|
19
|
+
in: (props2) => inArray(column(props2.field), props2.value),
|
|
20
|
+
nin: (props2) => notInArray(column(props2.field), props2.value),
|
|
21
|
+
// ** % and _ are escaped, so the text is literal; the value always goes as a parameter
|
|
22
|
+
contains: (props2) => sql`${column(props2.field)} ${dialect === "pg" ? sql`ilike` : sql`like`} ${`%${String(props2.value).replace(/[\\%_]/g, "\\$&")}%`} escape '\\'`,
|
|
23
|
+
between: (props2) => and(gte(column(props2.field), props2.value[0]), lte(column(props2.field), props2.value[1])),
|
|
24
|
+
null: (props2) => props2.value ? isNull(column(props2.field)) : isNotNull(column(props2.field))
|
|
25
|
+
},
|
|
26
|
+
and: (list) => and(...list),
|
|
27
|
+
or: (list) => or(...list),
|
|
28
|
+
list: async (props2) => {
|
|
29
|
+
const { where, sort, page } = props2;
|
|
30
|
+
const order = [...sort.map((entry) => (entry.direction === "asc" ? asc : desc)(column(entry.field))), ...sort.some((entry) => entry.field === id) ? [] : [asc(column(id))]];
|
|
31
|
+
const [rows, total] = await Promise.all([db.select().from(table).where(where).orderBy(...order).limit(page.limit).offset(page.offset), page.count ? db.$count(table, where) : null]);
|
|
32
|
+
return { rows, total: total === null ? null : Number(total) };
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export { dataKitSourceDrizzle };
|
|
38
|
+
//# sourceMappingURL=drizzle.js.map
|
|
39
|
+
//# sourceMappingURL=drizzle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/server/source/drizzle.ts"],"names":["props"],"mappings":";;;AAMO,IAAM,oBAAA,GAAuB,CAAuB,KAAA,KAAkG;AAE5J,EAAA,MAAM,EAAE,EAAA,EAAI,KAAA,EAAO,KAAK,IAAA,EAAM,OAAA,GAAU,MAAK,GAAI,KAAA;AAEjD,EAAA,MAAM,OAAA,GAAU,gBAAgB,KAAK,CAAA;AACrC,EAAA,MAAM,MAAA,GAAS,CAAC,KAAA,KAAkB;AACjC,IAAA,IAAI,CAAC,QAAQ,KAAK,CAAA,QAAS,IAAI,KAAA,CAAM,CAAA,iBAAA,EAAoB,KAAK,CAAA,+BAAA,CAAiC,CAAA;AAC/F,IAAA,OAAO,QAAQ,KAAK,CAAA;AAAA,EACrB,CAAA;AAEA,EAAA,OAAO,mBAAA,CAAiD;AAAA,IACvD,QAAA,EAAU;AAAA,MACT,EAAA,EAAI,CAACA,MAAAA,KAAU,EAAA,CAAG,OAAOA,MAAAA,CAAM,KAAK,CAAA,EAAGA,MAAAA,CAAM,KAAK,CAAA;AAAA,MAClD,EAAA,EAAI,CAACA,MAAAA,KAAU,EAAA,CAAG,OAAOA,MAAAA,CAAM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MAAA,CAAO,OAAO,KAAK,CAAA,GAAI,IAAI,CAAC,CAAA;AACnL,MAAA,OAAO,EAAE,MAAM,KAAA,EAAO,KAAA,KAAU,OAAO,IAAA,GAAO,MAAA,CAAO,KAAK,CAAA,EAAE;AAAA,IAC7D;AAAA,GACA,CAAA;AACF","file":"drizzle.js","sourcesContent":["import { and, asc, desc, eq, getTableColumns, gt, gte, inArray, isNotNull, isNull, lt, lte, ne, notInArray, or, sql, type SQL, type Table } from 'drizzle-orm';\nimport { dataKitSourceDefine } from './define';\n\n// ** A Drizzle table on any dialect: fields resolve to the table's own columns, so a name never becomes raw SQL\n// ** `dialect: 'pg'` searches with ilike; MySQL and SQLite use like (case-insensitive by collation)\n/* eslint-disable @typescript-eslint/no-explicit-any */\nexport const dataKitSourceDrizzle = <TTable extends Table>(props: Readonly<{ db: any; table: TTable; id?: string; dialect?: 'pg' | 'mysql' | 'sqlite' }>) => {\n\t// ** Deconstruct Props\n\tconst { db, table, id = 'id', dialect = 'pg' } = props;\n\n\tconst columns = getTableColumns(table) as Record<string, any>;\n\tconst column = (field: string) => {\n\t\tif (!columns[field]) throw new Error(`[next-data-kit] \"${field}\" isn't a column of this table.`);\n\t\treturn columns[field];\n\t};\n\n\treturn dataKitSourceDefine<TTable['$inferSelect'], SQL>({\n\t\toperator: {\n\t\t\teq: (props) => eq(column(props.field), props.value),\n\t\t\tne: (props) => ne(column(props.field), props.value),\n\t\t\tlt: (props) => lt(column(props.field), props.value),\n\t\t\tlte: (props) => lte(column(props.field), props.value),\n\t\t\tgt: (props) => gt(column(props.field), props.value),\n\t\t\tgte: (props) => gte(column(props.field), props.value),\n\t\t\tin: (props) => inArray(column(props.field), props.value),\n\t\t\tnin: (props) => notInArray(column(props.field), props.value),\n\t\t\t// ** % and _ are escaped, so the text is literal; the value always goes as a parameter\n\t\t\tcontains: (props) => sql`${column(props.field)} ${dialect === 'pg' ? sql`ilike` : sql`like`} ${`%${String(props.value).replace(/[\\\\%_]/g, '\\\\$&')}%`} escape '\\\\'`,\n\t\t\tbetween: (props) => and(gte(column(props.field), props.value[0]), lte(column(props.field), props.value[1]))!,\n\t\t\tnull: (props) => (props.value ? isNull(column(props.field)) : isNotNull(column(props.field))),\n\t\t},\n\t\tand: (list) => and(...list)!,\n\t\tor: (list) => or(...list)!,\n\t\tlist: async (props) => {\n\t\t\t// ** Deconstruct Props\n\t\t\tconst { where, sort, page } = props;\n\n\t\t\t// ** The id last keeps the order stable when sorted values tie\n\t\t\tconst order = [...sort.map((entry) => (entry.direction === 'asc' ? asc : desc)(column(entry.field))), ...(sort.some((entry) => entry.field === id) ? [] : [asc(column(id))])];\n\t\t\tconst [rows, total] = await Promise.all([db.select().from(table).where(where).orderBy(...order).limit(page.limit).offset(page.offset), page.count ? db.$count(table, where) : null]);\n\t\t\treturn { rows, total: total === null ? null : Number(total) };\n\t\t},\n\t});\n};\n"]}
|