next-data-kit 9.9.2 → 11.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 +75 -0
- package/README.md +289 -698
- package/dist/chunk-5D747NEB.js +12 -0
- package/dist/chunk-5D747NEB.js.map +1 -0
- package/dist/index.d.ts +109 -110
- package/dist/index.js +109 -205
- 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 +273 -88
- package/dist/server.js +269 -282
- package/dist/server.js.map +1 -1
- package/dist/source--l-z9HHB.d.ts +60 -0
- package/package.json +75 -107
- package/registry/r/data-kit.json +74 -0
- package/registry/r/registry.json +72 -0
- package/dist/action-DCmAhOQa.d.cts +0 -196
- package/dist/action-DCmAhOQa.d.ts +0 -196
- package/dist/chunk-5TIEIY7M.cjs +0 -393
- package/dist/chunk-5TIEIY7M.cjs.map +0 -1
- package/dist/chunk-MTJATM72.cjs +0 -2423
- package/dist/chunk-MTJATM72.cjs.map +0 -1
- package/dist/chunk-O377UO7C.js +0 -2379
- package/dist/chunk-O377UO7C.js.map +0 -1
- package/dist/chunk-VFSNUXOW.js +0 -382
- package/dist/chunk-VFSNUXOW.js.map +0 -1
- package/dist/client/components/data-kit-infinity.d.ts +0 -23
- package/dist/client/components/data-kit-infinity.d.ts.map +0 -1
- package/dist/client/components/data-kit-table.d.ts +0 -34
- package/dist/client/components/data-kit-table.d.ts.map +0 -1
- package/dist/client/components/data-kit.d.ts +0 -25
- package/dist/client/components/data-kit.d.ts.map +0 -1
- package/dist/client/components/index.d.ts +0 -4
- package/dist/client/components/index.d.ts.map +0 -1
- package/dist/client/components/ui/button.d.ts +0 -14
- package/dist/client/components/ui/button.d.ts.map +0 -1
- package/dist/client/components/ui/checkbox.d.ts +0 -5
- package/dist/client/components/ui/checkbox.d.ts.map +0 -1
- package/dist/client/components/ui/dropdown-menu.d.ts +0 -28
- package/dist/client/components/ui/dropdown-menu.d.ts.map +0 -1
- package/dist/client/components/ui/index.d.ts +0 -8
- package/dist/client/components/ui/index.d.ts.map +0 -1
- package/dist/client/components/ui/pagination.d.ts +0 -14
- package/dist/client/components/ui/pagination.d.ts.map +0 -1
- package/dist/client/components/ui/popover.d.ts +0 -10
- package/dist/client/components/ui/popover.d.ts.map +0 -1
- package/dist/client/components/ui/select.d.ts +0 -18
- package/dist/client/components/ui/select.d.ts.map +0 -1
- package/dist/client/components/ui/table.d.ts +0 -11
- package/dist/client/components/ui/table.d.ts.map +0 -1
- package/dist/client/context/index.d.ts +0 -22
- package/dist/client/context/index.d.ts.map +0 -1
- package/dist/client/hooks/index.d.ts +0 -7
- package/dist/client/hooks/index.d.ts.map +0 -1
- package/dist/client/hooks/useDataKit.d.ts +0 -9
- package/dist/client/hooks/useDataKit.d.ts.map +0 -1
- package/dist/client/hooks/usePagination.d.ts +0 -18
- package/dist/client/hooks/usePagination.d.ts.map +0 -1
- package/dist/client/hooks/useSelection.d.ts +0 -13
- package/dist/client/hooks/useSelection.d.ts.map +0 -1
- package/dist/client/index.d.ts +0 -11
- package/dist/client/index.d.ts.map +0 -1
- package/dist/client/utils/cn.d.ts +0 -10
- package/dist/client/utils/cn.d.ts.map +0 -1
- package/dist/client/utils/index.d.ts +0 -60
- package/dist/client/utils/index.d.ts.map +0 -1
- package/dist/client.cjs +0 -85
- package/dist/client.cjs.map +0 -1
- package/dist/client.d.cts +0 -62
- package/dist/client.d.ts +0 -7
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -4
- package/dist/client.js.map +0 -1
- package/dist/index-DUVfK8q6.d.ts +0 -284
- package/dist/index-qXbjvUTB.d.cts +0 -284
- package/dist/index.cjs +0 -300
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -117
- package/dist/internal/hooks.cjs +0 -12
- package/dist/internal/hooks.cjs.map +0 -1
- package/dist/internal/hooks.d.cts +0 -1
- package/dist/internal/hooks.d.ts +0 -1
- package/dist/internal/hooks.js +0 -3
- package/dist/internal/hooks.js.map +0 -1
- package/dist/server/utils.d.ts +0 -52
- package/dist/server/utils.d.ts.map +0 -1
- package/dist/server.cjs +0 -315
- package/dist/server.cjs.map +0 -1
- package/dist/server.d.cts +0 -99
- package/dist/styles.css +0 -2
- package/dist/types/client/component.d.ts +0 -129
- package/dist/types/client/component.d.ts.map +0 -1
- package/dist/types/client/hook.d.ts +0 -116
- package/dist/types/client/hook.d.ts.map +0 -1
- package/dist/types/client/selectable.d.ts +0 -43
- package/dist/types/client/selectable.d.ts.map +0 -1
- package/dist/types/index.cjs +0 -4
- package/dist/types/index.cjs.map +0 -1
- package/dist/types/index.d.cts +0 -291
- package/dist/types/index.d.ts +0 -12
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -3
- package/dist/types/index.js.map +0 -1
- package/dist/types/next-data-kit.d.ts +0 -95
- package/dist/types/next-data-kit.d.ts.map +0 -1
- package/dist/types/server/action.d.ts +0 -44
- package/dist/types/server/action.d.ts.map +0 -1
- package/dist/types/server/database/mongo.d.ts +0 -56
- package/dist/types/server/database/mongo.d.ts.map +0 -1
- package/dist/useDataKit-XFp3_mUc.d.cts +0 -276
- package/dist/useDataKit-XFp3_mUc.d.ts +0 -276
package/dist/server.js
CHANGED
|
@@ -1,305 +1,292 @@
|
|
|
1
|
+
import { sourceDefine } from './chunk-5D747NEB.js';
|
|
2
|
+
export { sourceDefine } from './chunk-5D747NEB.js';
|
|
1
3
|
import { z } from 'zod';
|
|
2
4
|
|
|
3
|
-
// src/
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var matchesExact = (rowValue, needle) => {
|
|
19
|
-
if (Array.isArray(needle)) return needle.includes(rowValue);
|
|
20
|
-
return rowValue === needle;
|
|
21
|
-
};
|
|
22
|
-
var matchesRegexLike = (rowValue, needle) => {
|
|
23
|
-
if (!isProvided(needle)) return true;
|
|
24
|
-
if (rowValue === void 0 || rowValue === null) return false;
|
|
25
|
-
const hay = String(rowValue);
|
|
26
|
-
if (needle instanceof RegExp) return needle.test(hay);
|
|
27
|
-
const n = String(needle);
|
|
28
|
-
return hay.toLowerCase().includes(n.toLowerCase());
|
|
5
|
+
// src/url/index.ts
|
|
6
|
+
var reserved = ["page", "size", "sort", "search"];
|
|
7
|
+
var urlEncode = (props) => {
|
|
8
|
+
const { input, key, search, remove = [] } = props;
|
|
9
|
+
const prefix = key ? `${key}.` : "";
|
|
10
|
+
const params = new URLSearchParams(search);
|
|
11
|
+
[...params.keys()].filter((name) => key ? name.startsWith(prefix) : reserved.includes(name) || name in input.filter || remove.includes(name)).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.column}.${sort.direction}`).join(","));
|
|
15
|
+
if (input.search) params.set(`${prefix}search`, input.search);
|
|
16
|
+
Object.entries(input.filter).forEach(([name, value]) => {
|
|
17
|
+
if (value) params.set(`${prefix}${name}`, value);
|
|
18
|
+
});
|
|
19
|
+
return params.toString();
|
|
29
20
|
};
|
|
30
|
-
var
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var isSafeKey = (key) => {
|
|
42
|
-
const unsafeKeys = ["__proto__", "constructor", "prototype"];
|
|
43
|
-
return !unsafeKeys.includes(key);
|
|
21
|
+
var urlDecode = (props) => {
|
|
22
|
+
const { searchParams, key } = props;
|
|
23
|
+
const prefix = key ? `${key}.` : "";
|
|
24
|
+
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]);
|
|
25
|
+
const values = Object.fromEntries(entries);
|
|
26
|
+
return {
|
|
27
|
+
page: { number: values.page, size: values.size },
|
|
28
|
+
sort: (values.sort ?? "").split(",").filter(Boolean).map((sort) => ({ column: sort.slice(0, sort.lastIndexOf(".")), direction: sort.slice(sort.lastIndexOf(".") + 1) })),
|
|
29
|
+
search: values.search,
|
|
30
|
+
filter: Object.fromEntries(entries.filter(([name]) => !reserved.includes(name)))
|
|
31
|
+
};
|
|
44
32
|
};
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
|
|
34
|
+
// src/server/data-kit.ts
|
|
35
|
+
var filterCustom = (props) => ({ custom: props });
|
|
36
|
+
var filterValue = (props) => {
|
|
37
|
+
const { variant, value, preset } = props;
|
|
38
|
+
if (value.length > 4e3) return void 0;
|
|
39
|
+
if (variant === "text" || variant === "select") return value.trim() && value.trim().length <= 200 ? value.trim() : void 0;
|
|
40
|
+
if (variant === "number") return value !== "" && Number.isFinite(Number(value)) ? Number(value) : void 0;
|
|
41
|
+
if (variant === "boolean") return value === "true" ? true : value === "false" ? false : void 0;
|
|
42
|
+
if (variant === "multiSelect") {
|
|
43
|
+
const list = value.split(",").filter(Boolean);
|
|
44
|
+
return list.length && list.length <= 100 && list.every((item) => item.length <= 200) ? list : void 0;
|
|
45
|
+
}
|
|
46
|
+
const range = /^(\d+(?:\.\d+)?)?-(\d+(?:\.\d+)?)?$/.exec(value);
|
|
47
|
+
if (variant === "range") return range && (range[1] || range[2]) ? [range[1] ? Number(range[1]) : void 0, range[2] ? Number(range[2]) : void 0] : void 0;
|
|
48
|
+
if (preset?.includes(value)) {
|
|
49
|
+
const [, amount, unit] = /^(\d+)([hd])$/.exec(value) ?? [];
|
|
50
|
+
return amount ? [new Date(Date.now() - Number(amount) * (unit === "h" ? 36e5 : 864e5)), void 0] : void 0;
|
|
51
|
+
}
|
|
52
|
+
return range?.[1] && range[2] ? [new Date(Number(range[1])), new Date(Number(range[2]))] : void 0;
|
|
47
53
|
};
|
|
48
|
-
var
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
54
|
+
var dataKit = (props) => {
|
|
55
|
+
const { source, column, search = [], scope, page = {}, row } = props;
|
|
56
|
+
const reserved2 = Object.keys(column).filter((key) => ["page", "size", "sort", "search"].includes(key));
|
|
57
|
+
if (reserved2.length) throw new Error(`[next-data-kit] "${reserved2.join('", "')}" can't be a column key: page, size, sort and search are reserved.`);
|
|
58
|
+
const size = typeof page.size === "object" ? page.size : { default: page.size ?? 20, max: Math.max(page.size ?? 20, 100) };
|
|
59
|
+
const field = (key) => column[key]?.field ?? key;
|
|
60
|
+
const sortDefault = [props.sort ?? []].flat().map((sort) => ({ column: sort.slice(0, sort.lastIndexOf(".")), direction: sort.endsWith(".asc") ? "asc" : "desc" })).filter((sort) => column[sort.column]?.sort);
|
|
61
|
+
const schema = z.object({
|
|
62
|
+
page: z.object({ number: z.coerce.number().int().min(1).catch(1), size: z.coerce.number().int().min(1).max(size.max).catch(size.default) }).catch({ number: 1, size: size.default }),
|
|
63
|
+
sort: z.array(z.object({ column: z.string(), direction: z.enum(["asc", "desc"]) })).catch([]).transform((sorts) => sorts.filter((sort) => column[sort.column]?.sort)).transform((sorts) => sorts.length ? sorts : sortDefault),
|
|
64
|
+
search: z.string().trim().max(200).optional().catch(void 0).transform((value) => search.length && value ? value : void 0),
|
|
65
|
+
// ** Undeclared or invalid filters are dropped before anything reaches the source
|
|
66
|
+
filter: z.record(z.string(), z.string()).catch({}).transform(
|
|
67
|
+
(filter) => Object.fromEntries(
|
|
68
|
+
Object.entries(filter).filter(([key, value]) => {
|
|
69
|
+
const declared = column[key]?.filter;
|
|
70
|
+
if (!declared) return false;
|
|
71
|
+
if (typeof declared === "object") return declared.custom.schema.safeParse(value).success;
|
|
72
|
+
return filterValue({ variant: declared, value, preset: column[key]?.preset }) !== void 0;
|
|
73
|
+
})
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
schema,
|
|
79
|
+
// ** searchParams from a server component, or an input from a server action / export (validated either way)
|
|
80
|
+
query: async (props2) => {
|
|
81
|
+
const { context, page: override } = props2;
|
|
82
|
+
const input = schema.parse("input" in props2 ? props2.input : urlDecode({ searchParams: props2.searchParams, key: props2.key }));
|
|
83
|
+
const option = Object.fromEntries(
|
|
84
|
+
await Promise.all(
|
|
85
|
+
Object.entries(column).filter(([, declared]) => declared.option).map(async ([key, declared]) => [key, typeof declared.option === "function" ? await declared.option({ context }) : [...declared.option]])
|
|
86
|
+
)
|
|
87
|
+
);
|
|
88
|
+
const filter = Object.fromEntries(
|
|
89
|
+
Object.entries(input.filter).filter(([key, value]) => !option[key] || value.split(column[key]?.filter === "multiSelect" ? "," : "\0").every((item) => option[key].includes(item)))
|
|
90
|
+
);
|
|
91
|
+
const scoped = scope?.({ context });
|
|
92
|
+
const conditions = [
|
|
93
|
+
...Array.isArray(scoped) ? scoped : Object.entries(scoped ?? {}).map(([key, value]) => ({ field: key, operator: "eq", value })),
|
|
94
|
+
...input.search ? [{ or: search.map((key) => ({ field: field(key), operator: "contains", value: input.search })) }] : [],
|
|
95
|
+
...(await Promise.all(
|
|
96
|
+
Object.entries(filter).map(async ([key, raw]) => {
|
|
97
|
+
const declared = column[key].filter;
|
|
98
|
+
if (typeof declared === "object") return [{ native: await declared.custom.query({ value: declared.custom.schema.parse(raw), context }) }];
|
|
99
|
+
const value = filterValue({ variant: declared, value: raw, preset: column[key]?.preset });
|
|
100
|
+
if (column[key].query) return [{ native: await column[key].query({ value, context }) }];
|
|
101
|
+
if (declared === "multiSelect") return [{ field: field(key), operator: "in", value }];
|
|
102
|
+
if (!Array.isArray(value)) return [{ field: field(key), operator: declared === "text" ? "contains" : "eq", value }];
|
|
103
|
+
const [min, max] = value;
|
|
104
|
+
if (min !== void 0 && max !== void 0) return [{ field: field(key), operator: "between", value: [min, max] }];
|
|
105
|
+
return min !== void 0 ? [{ field: field(key), operator: "gte", value: min }] : [{ field: field(key), operator: "lte", value: max }];
|
|
106
|
+
})
|
|
107
|
+
)).flat()
|
|
108
|
+
];
|
|
109
|
+
const limit = override?.size ?? input.page.size;
|
|
110
|
+
const count = page.count !== "none";
|
|
111
|
+
const sort = input.sort.map((sort2) => ({ field: field(sort2.column), direction: sort2.direction }));
|
|
112
|
+
let number = input.page.number;
|
|
113
|
+
let result = await source.list({ filter: conditions, sort, page: { offset: (number - 1) * limit, limit, count } });
|
|
114
|
+
const last = result.total === null ? number : Math.max(1, Math.ceil(result.total / limit));
|
|
115
|
+
if (number > last) {
|
|
116
|
+
number = last;
|
|
117
|
+
result = await source.list({ filter: conditions, sort, page: { offset: (number - 1) * limit, limit, count } });
|
|
118
|
+
}
|
|
119
|
+
const rows = await Promise.all(result.rows.map((raw) => row({ row: raw })));
|
|
120
|
+
if (process.env.NODE_ENV !== "production" && rows[0] && (typeof rows[0].id !== "string" || Object.values(rows[0]).some((value) => typeof value === "object" && value !== null && "_bsontype" in value))) {
|
|
121
|
+
console.warn("[next-data-kit] row() must return plain JSON with a string `id` (convert ObjectId with .toString()).");
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
rows,
|
|
125
|
+
total: result.total,
|
|
126
|
+
page: { number, size: limit },
|
|
127
|
+
sort: input.sort,
|
|
128
|
+
filter,
|
|
129
|
+
...input.search && { search: input.search },
|
|
130
|
+
meta: {
|
|
131
|
+
search: search.length > 0,
|
|
132
|
+
sort: sortDefault,
|
|
133
|
+
column: Object.fromEntries(
|
|
134
|
+
Object.entries(column).map(([key, declared]) => [
|
|
135
|
+
key,
|
|
136
|
+
{
|
|
137
|
+
...declared.filter && { filter: typeof declared.filter === "object" ? "custom" : declared.filter },
|
|
138
|
+
...option[key] && { option: option[key] },
|
|
139
|
+
...declared.preset && { preset: [...declared.preset] },
|
|
140
|
+
sort: declared.sort ?? false,
|
|
141
|
+
hidden: declared.hidden ?? false
|
|
142
|
+
}
|
|
143
|
+
])
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
};
|
|
52
147
|
}
|
|
53
|
-
const escapedValue = escapeRegex(value);
|
|
54
|
-
return {
|
|
55
|
-
$or: fields.map((field) => ({
|
|
56
|
-
[field]: { $regex: escapedValue, $options: "i" }
|
|
57
|
-
}))
|
|
58
|
-
};
|
|
59
148
|
};
|
|
60
149
|
};
|
|
61
|
-
var calculatePagination = (page, limit, total) => ({
|
|
62
|
-
currentPage: page,
|
|
63
|
-
totalPages: Math.ceil(total / limit),
|
|
64
|
-
totalItems: total,
|
|
65
|
-
itemsPerPage: limit,
|
|
66
|
-
hasNextPage: page * limit < total,
|
|
67
|
-
hasPrevPage: page > 1
|
|
68
|
-
});
|
|
69
150
|
|
|
70
|
-
// src/server/
|
|
71
|
-
var
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (input.filterConfig) {
|
|
101
|
-
Object.entries(filter).forEach(([key, value]) => {
|
|
102
|
-
if (filterCustom && filterCustom[key]) return;
|
|
103
|
-
if (isProvided(value) && isSafeKey(key) && input.filterConfig?.[key]) {
|
|
104
|
-
const config = input.filterConfig[key];
|
|
105
|
-
const fieldName = config?.field ?? key;
|
|
106
|
-
if (config?.type === "REGEX") {
|
|
107
|
-
filterQuery[fieldName] = {
|
|
108
|
-
$regex: escapeRegex(String(value)),
|
|
109
|
-
$options: "i"
|
|
110
|
-
};
|
|
111
|
-
} else if (config?.type === "EXACT") {
|
|
112
|
-
filterQuery[fieldName] = value;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
} else {
|
|
117
|
-
Object.entries(filter).forEach(([key, value]) => {
|
|
118
|
-
if (filterCustom && filterCustom[key]) return;
|
|
119
|
-
if (isProvided(value) && isSafeKey(key)) {
|
|
120
|
-
if (typeof value === "string") {
|
|
121
|
-
filterQuery[key] = {
|
|
122
|
-
$regex: escapeRegex(value),
|
|
123
|
-
$options: "i"
|
|
124
|
-
};
|
|
125
|
-
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
126
|
-
filterQuery[key] = value;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
151
|
+
// src/server/source/mongoose.ts
|
|
152
|
+
var sourceMongooseQuery = {
|
|
153
|
+
operator: {
|
|
154
|
+
eq: (props) => ({ [props.field]: props.value }),
|
|
155
|
+
ne: (props) => ({ [props.field]: { $ne: props.value } }),
|
|
156
|
+
lt: (props) => ({ [props.field]: { $lt: props.value } }),
|
|
157
|
+
lte: (props) => ({ [props.field]: { $lte: props.value } }),
|
|
158
|
+
gt: (props) => ({ [props.field]: { $gt: props.value } }),
|
|
159
|
+
gte: (props) => ({ [props.field]: { $gte: props.value } }),
|
|
160
|
+
in: (props) => ({ [props.field]: { $in: props.value } }),
|
|
161
|
+
nin: (props) => ({ [props.field]: { $nin: props.value } }),
|
|
162
|
+
// ** Escaped, so the text is literal, never a regex
|
|
163
|
+
contains: (props) => ({ [props.field]: { $regex: String(props.value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), $options: "i" } }),
|
|
164
|
+
between: (props) => ({ [props.field]: { $gte: props.value[0], $lte: props.value[1] } }),
|
|
165
|
+
null: (props) => ({ [props.field]: props.value ? null : { $ne: null } })
|
|
166
|
+
},
|
|
167
|
+
and: (list) => list.length === 1 ? list[0] : { $and: list },
|
|
168
|
+
or: (list) => ({ $or: list })
|
|
169
|
+
};
|
|
170
|
+
var sourceMongoose = (props) => {
|
|
171
|
+
const { model, hydrate, select } = props;
|
|
172
|
+
return sourceDefine({
|
|
173
|
+
...sourceMongooseQuery,
|
|
174
|
+
list: async (props2) => {
|
|
175
|
+
const { where = {}, sort, page } = props2;
|
|
176
|
+
const order = Object.fromEntries([...sort.map((entry) => [entry.field, entry.direction === "asc" ? 1 : -1]), ...sort.some((entry) => entry.field === "_id") ? [] : [["_id", 1]]]);
|
|
177
|
+
const find = model.find(where).sort(order).skip(page.offset).limit(page.limit);
|
|
178
|
+
if (select) find.select(select);
|
|
179
|
+
const [rows, total] = await Promise.all([hydrate ? find.exec() : find.lean().exec(), page.count ? model.countDocuments(where).exec() : null]);
|
|
180
|
+
return { rows, total };
|
|
131
181
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// src/server/source/memory.ts
|
|
186
|
+
var sourceMemory = (props) => {
|
|
187
|
+
const { rows } = props;
|
|
188
|
+
const read = (row, field) => field.split(".").reduce((value, part) => value?.[part], row);
|
|
189
|
+
const compare = (a, b) => (a instanceof Date ? a.getTime() : a) - (b instanceof Date ? b.getTime() : b);
|
|
190
|
+
return sourceDefine({
|
|
191
|
+
operator: {
|
|
192
|
+
eq: (props2) => (row) => read(row, props2.field) === props2.value,
|
|
193
|
+
ne: (props2) => (row) => read(row, props2.field) !== props2.value,
|
|
194
|
+
lt: (props2) => (row) => compare(read(row, props2.field), props2.value) < 0,
|
|
195
|
+
lte: (props2) => (row) => compare(read(row, props2.field), props2.value) <= 0,
|
|
196
|
+
gt: (props2) => (row) => compare(read(row, props2.field), props2.value) > 0,
|
|
197
|
+
gte: (props2) => (row) => compare(read(row, props2.field), props2.value) >= 0,
|
|
198
|
+
in: (props2) => (row) => props2.value.includes(read(row, props2.field)),
|
|
199
|
+
nin: (props2) => (row) => !props2.value.includes(read(row, props2.field)),
|
|
200
|
+
contains: (props2) => (row) => String(read(row, props2.field) ?? "").toLowerCase().includes(String(props2.value).toLowerCase()),
|
|
201
|
+
between: (props2) => (row) => compare(read(row, props2.field), props2.value[0]) >= 0 && compare(read(row, props2.field), props2.value[1]) <= 0,
|
|
202
|
+
null: (props2) => (row) => read(row, props2.field) == null === props2.value
|
|
203
|
+
},
|
|
204
|
+
and: (list) => (row) => list.every((match) => match(row)),
|
|
205
|
+
or: (list) => (row) => list.some((match) => match(row)),
|
|
206
|
+
list: async (props2) => {
|
|
207
|
+
const { where, sort, page } = props2;
|
|
208
|
+
const matched = where ? rows.filter(where) : rows;
|
|
209
|
+
const sorted = [...matched].sort((a, b) => {
|
|
210
|
+
for (const entry of sort) {
|
|
211
|
+
const [x, y] = [read(a, entry.field), read(b, entry.field)];
|
|
212
|
+
if (x !== y) return (x < y ? -1 : 1) * (entry.direction === "asc" ? 1 : -1);
|
|
137
213
|
}
|
|
214
|
+
return 0;
|
|
138
215
|
});
|
|
216
|
+
return { rows: sorted.slice(page.offset, page.offset + page.limit), total: page.count ? matched.length : null };
|
|
139
217
|
}
|
|
140
|
-
|
|
141
|
-
const items = await model.find(filterQuery).sort(sortOption).limit(limit).skip(skip);
|
|
142
|
-
return { items, total };
|
|
143
|
-
};
|
|
218
|
+
});
|
|
144
219
|
};
|
|
145
220
|
|
|
146
|
-
// src/server/
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (val !== null && typeof val === "object") {
|
|
172
|
-
throw new Error(`[Security] Filter value for '${key}' must be a primitive.`);
|
|
173
|
-
}
|
|
174
|
-
if (val !== void 0) {
|
|
175
|
-
safeFilter[key] = val;
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
input.filter = safeFilter;
|
|
179
|
-
}
|
|
180
|
-
if (input.sorts && sortAllowed) {
|
|
181
|
-
input.sorts.forEach((sort) => {
|
|
182
|
-
if (!sortAllowed.includes(sort.path)) {
|
|
183
|
-
throw new Error(`[Security] Sort field '${sort.path}' is not allowed.`);
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
switch (input.action ?? "FETCH") {
|
|
188
|
-
case "FETCH": {
|
|
189
|
-
if (!input.limit || !input.page) {
|
|
190
|
-
throw new Error("Invalid input: missing limit or page");
|
|
191
|
-
}
|
|
192
|
-
const limit = Math.min(input.limit, maxLimit);
|
|
193
|
-
const skip = limit * (input.page - 1);
|
|
194
|
-
const { items, total } = await adapter({
|
|
195
|
-
filter: input.filter ?? {},
|
|
196
|
-
sorts: input.sorts ?? [],
|
|
197
|
-
limit,
|
|
198
|
-
page: input.page,
|
|
199
|
-
skip,
|
|
200
|
-
input
|
|
201
|
-
});
|
|
202
|
-
const processedItems = await Promise.all(items.map((dataItem) => item(dataItem)));
|
|
203
|
-
return {
|
|
204
|
-
type: "ITEMS",
|
|
205
|
-
items: processedItems,
|
|
206
|
-
documentTotal: total
|
|
207
|
-
};
|
|
221
|
+
// src/server/source/prisma.ts
|
|
222
|
+
var sourcePrisma = (props) => {
|
|
223
|
+
const { model, select, id = "id", insensitive = true } = props;
|
|
224
|
+
const nest = (field, value) => field.split(".").reduceRight((inner, part) => ({ [part]: inner }), value);
|
|
225
|
+
return sourceDefine({
|
|
226
|
+
operator: {
|
|
227
|
+
eq: (props2) => nest(props2.field, { equals: props2.value }),
|
|
228
|
+
ne: (props2) => nest(props2.field, { not: props2.value }),
|
|
229
|
+
lt: (props2) => nest(props2.field, { lt: props2.value }),
|
|
230
|
+
lte: (props2) => nest(props2.field, { lte: props2.value }),
|
|
231
|
+
gt: (props2) => nest(props2.field, { gt: props2.value }),
|
|
232
|
+
gte: (props2) => nest(props2.field, { gte: props2.value }),
|
|
233
|
+
in: (props2) => nest(props2.field, { in: props2.value }),
|
|
234
|
+
nin: (props2) => nest(props2.field, { notIn: props2.value }),
|
|
235
|
+
contains: (props2) => nest(props2.field, { contains: props2.value, ...insensitive && { mode: "insensitive" } }),
|
|
236
|
+
between: (props2) => nest(props2.field, { gte: props2.value[0], lte: props2.value[1] }),
|
|
237
|
+
null: (props2) => nest(props2.field, props2.value ? { equals: null } : { not: null })
|
|
238
|
+
},
|
|
239
|
+
and: (list) => ({ AND: list }),
|
|
240
|
+
or: (list) => ({ OR: list }),
|
|
241
|
+
list: async (props2) => {
|
|
242
|
+
const { where, sort, page } = props2;
|
|
243
|
+
const orderBy = [...sort.map((entry) => nest(entry.field, entry.direction)), ...sort.some((entry) => entry.field === id) ? [] : [{ [id]: "asc" }]];
|
|
244
|
+
const [rows, total] = await Promise.all([model.findMany({ where, orderBy, skip: page.offset, take: page.limit, ...select && { select } }), page.count ? model.count({ where }) : null]);
|
|
245
|
+
return { rows, total };
|
|
208
246
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
async function dataKitServerAction(props) {
|
|
214
|
-
const { input, item, maxLimit = 100, queryAllowed, filterAllowed: explicitFilterAllowed, sortAllowed } = props;
|
|
215
|
-
const filterAllowed = explicitFilterAllowed;
|
|
216
|
-
let finalAdapter;
|
|
217
|
-
if ("adapter" in props && props.adapter) {
|
|
218
|
-
finalAdapter = props.adapter;
|
|
219
|
-
} else if ("model" in props && props.model) {
|
|
220
|
-
const model = props.model;
|
|
221
|
-
finalAdapter = mongooseAdapter(model, {
|
|
222
|
-
filter: props.filter,
|
|
223
|
-
filterCustom: props.filterCustom,
|
|
224
|
-
defaultSort: props.defaultSort
|
|
225
|
-
});
|
|
226
|
-
} else {
|
|
227
|
-
throw new Error("Either model or adapter must be provided");
|
|
228
|
-
}
|
|
229
|
-
return executeDataKit(input, finalAdapter, item, maxLimit, filterAllowed, queryAllowed, sortAllowed);
|
|
230
|
-
}
|
|
247
|
+
});
|
|
248
|
+
};
|
|
231
249
|
|
|
232
|
-
// src/server/
|
|
233
|
-
var
|
|
234
|
-
const {
|
|
235
|
-
return
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
250
|
+
// src/server/source/rest.ts
|
|
251
|
+
var sourceRest = (props) => {
|
|
252
|
+
const { url, request, response } = props;
|
|
253
|
+
return sourceDefine({
|
|
254
|
+
operator: {
|
|
255
|
+
eq: (props2) => [[props2.field, String(props2.value)]],
|
|
256
|
+
ne: (props2) => [[`${props2.field}_ne`, String(props2.value)]],
|
|
257
|
+
lt: (props2) => [[`${props2.field}_lt`, String(props2.value)]],
|
|
258
|
+
lte: (props2) => [[`${props2.field}_lte`, String(props2.value)]],
|
|
259
|
+
gt: (props2) => [[`${props2.field}_gt`, String(props2.value)]],
|
|
260
|
+
gte: (props2) => [[`${props2.field}_gte`, String(props2.value)]],
|
|
261
|
+
in: (props2) => props2.value.map((value) => [props2.field, String(value)]),
|
|
262
|
+
nin: (props2) => props2.value.map((value) => [`${props2.field}_ne`, String(value)]),
|
|
263
|
+
contains: (props2) => [[`${props2.field}_like`, String(props2.value)]],
|
|
264
|
+
between: (props2) => [
|
|
265
|
+
[`${props2.field}_gte`, String(props2.value[0] instanceof Date ? props2.value[0].toISOString() : props2.value[0])],
|
|
266
|
+
[`${props2.field}_lte`, String(props2.value[1] instanceof Date ? props2.value[1].toISOString() : props2.value[1])]
|
|
267
|
+
],
|
|
268
|
+
null: (props2) => [[props2.field, props2.value ? "null" : ""]]
|
|
269
|
+
},
|
|
270
|
+
and: (list) => list.flat(),
|
|
271
|
+
// ** The search box's or-group becomes json-server's full-text `q`
|
|
272
|
+
or: (list) => [["q", list.flat()[0]?.[1] ?? ""]],
|
|
273
|
+
list: async (props2) => {
|
|
274
|
+
const { where = [], sort, page } = props2;
|
|
275
|
+
const params = new URLSearchParams([...where, ["_start", String(page.offset)], ["_end", String(page.offset + page.limit)]]);
|
|
276
|
+
if (sort.length) {
|
|
277
|
+
params.set("_sort", sort.map((entry) => entry.field).join(","));
|
|
278
|
+
params.set("_order", sort.map((entry) => entry.direction).join(","));
|
|
243
279
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
const config = filterConfig?.[key];
|
|
251
|
-
const field = config?.field ?? key;
|
|
252
|
-
const rowValue = getValueByPath(row, field);
|
|
253
|
-
const type = config?.type ?? defaultFilterType;
|
|
254
|
-
if (type === "regex") {
|
|
255
|
-
if (!matchesRegexLike(rowValue, value)) return false;
|
|
256
|
-
} else {
|
|
257
|
-
if (!matchesExact(rowValue, value)) return false;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
return true;
|
|
261
|
-
});
|
|
262
|
-
const normalizedSorts = normalizeSorts(sorts);
|
|
263
|
-
if (normalizedSorts.length > 0) {
|
|
264
|
-
rows = [...rows].sort((ra, rb) => {
|
|
265
|
-
for (const s of normalizedSorts) {
|
|
266
|
-
const av = getValueByPath(ra, s.path);
|
|
267
|
-
const bv = getValueByPath(rb, s.path);
|
|
268
|
-
const cmp = compareValues(av, bv);
|
|
269
|
-
if (cmp !== 0) return s.value === 1 ? cmp : -cmp;
|
|
270
|
-
}
|
|
271
|
-
return 0;
|
|
272
|
-
});
|
|
280
|
+
const answer = await fetch(`${url}?${params}`, request);
|
|
281
|
+
if (!answer.ok) throw new Error(`[next-data-kit] ${url} answered ${answer.status}`);
|
|
282
|
+
const body = await answer.json();
|
|
283
|
+
if (response) return response({ body, header: answer.headers });
|
|
284
|
+
const total = answer.headers.get("x-total-count");
|
|
285
|
+
return { rows: body, total: total === null ? null : Number(total) };
|
|
273
286
|
}
|
|
274
|
-
|
|
275
|
-
const items = rows.slice(skip, skip + limit);
|
|
276
|
-
return { items, total };
|
|
277
|
-
};
|
|
287
|
+
});
|
|
278
288
|
};
|
|
279
|
-
var dataKitSchemaZod = z.object({
|
|
280
|
-
action: z.literal("FETCH").optional(),
|
|
281
|
-
page: z.number().int().positive().optional(),
|
|
282
|
-
limit: z.number().int().positive().optional(),
|
|
283
|
-
query: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()])).optional(),
|
|
284
|
-
filter: z.record(z.string(), z.union([z.string(), z.number(), z.boolean(), z.null()])).optional(),
|
|
285
|
-
filterConfig: z.record(
|
|
286
|
-
z.string(),
|
|
287
|
-
z.object({
|
|
288
|
-
type: z.enum(["REGEX", "EXACT"]),
|
|
289
|
-
field: z.string().optional()
|
|
290
|
-
})
|
|
291
|
-
).optional(),
|
|
292
|
-
sorts: z.array(
|
|
293
|
-
z.object({
|
|
294
|
-
path: z.string().max(100),
|
|
295
|
-
// Limit path length to prevent abuse
|
|
296
|
-
value: z.literal(-1).or(z.literal(1))
|
|
297
|
-
})
|
|
298
|
-
).max(5).optional(),
|
|
299
|
-
// Limit to 5 sort fields
|
|
300
|
-
sort: z.record(z.string(), z.literal(1).or(z.literal(-1))).optional()
|
|
301
|
-
});
|
|
302
289
|
|
|
303
|
-
export {
|
|
290
|
+
export { dataKit, filterCustom, sourceMemory, sourceMongoose, sourceMongooseQuery, sourcePrisma, sourceRest, urlDecode, urlEncode };
|
|
304
291
|
//# sourceMappingURL=server.js.map
|
|
305
292
|
//# sourceMappingURL=server.js.map
|