@danielhritcu/zenstack-orm 3.5.5 → 3.5.7

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.cjs CHANGED
@@ -2874,9 +2874,18 @@ function extractSearch(where) {
2874
2874
  let found;
2875
2875
  const cleaned = {};
2876
2876
  for (const [key, value] of Object.entries(obj)) {
2877
- if (key === "search" && isSearchPayload(value)) {
2878
- found = value;
2879
- continue;
2877
+ if (key === "search") {
2878
+ if (typeof value === "string") {
2879
+ found = {
2880
+ q: value,
2881
+ profile: "default"
2882
+ };
2883
+ continue;
2884
+ }
2885
+ if (isSearchPayload(value)) {
2886
+ found = value;
2887
+ continue;
2888
+ }
2880
2889
  }
2881
2890
  if (key === "AND" || key === "OR") {
2882
2891
  const result = extractSearch(value);
@@ -3008,9 +3017,9 @@ function expandSearch(model, where, searchDefaults, schemaModels, virtualRelatio
3008
3017
  };
3009
3018
  const profile = search2.profile ? searchDefaults?.[model]?.[search2.profile] : void 0;
3010
3019
  const fields = search2.fields ?? profile?.fields;
3011
- if (!fields) return {
3012
- where: remaining
3013
- };
3020
+ if (!fields) {
3021
+ throw new Error(`Search on "${model}" requires a "default" search profile or explicit fields.`);
3022
+ }
3014
3023
  const mode = search2.mode ?? profile?.mode ?? "contains";
3015
3024
  const strategy = search2.strategy ?? profile?.strategy ?? "all";
3016
3025
  const tokens = unique(search2.q.trim().split(/\s+/).map((t) => t.toLowerCase()).filter(Boolean));
@@ -5839,7 +5848,28 @@ var ZodSchemaFactory = class {
5839
5848
  }).optional();
5840
5849
  const searchDefaults = this.schema.plugins?.searchDefaults?.[model];
5841
5850
  if (searchDefaults) {
5842
- fields["search"] = import_zod2.z.any().optional();
5851
+ const searchFieldMapSchema = import_zod2.z.lazy(() => import_zod2.z.record(import_zod2.z.string(), import_zod2.z.union([
5852
+ import_zod2.z.literal(true),
5853
+ searchFieldMapSchema
5854
+ ])));
5855
+ const searchPayloadSchema = zLooseObject({
5856
+ q: import_zod2.z.string(),
5857
+ fields: searchFieldMapSchema.optional(),
5858
+ profile: import_zod2.z.string().optional(),
5859
+ mode: import_zod2.z.enum([
5860
+ "contains",
5861
+ "startsWith",
5862
+ "equals"
5863
+ ]).optional(),
5864
+ strategy: import_zod2.z.enum([
5865
+ "all",
5866
+ "any"
5867
+ ]).optional()
5868
+ });
5869
+ fields["search"] = import_zod2.z.union([
5870
+ import_zod2.z.string(),
5871
+ searchPayloadSchema
5872
+ ]).optional();
5843
5873
  }
5844
5874
  fields["AND"] = this.orArray(import_zod2.z.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields, false, options)), true).optional();
5845
5875
  fields["OR"] = import_zod2.z.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields, false, options)).array().optional();
@@ -10175,7 +10205,87 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
10175
10205
  }, "groupBy"),
10176
10206
  exists: /* @__PURE__ */ __name((args) => {
10177
10207
  return createPromise("exists", "exists", args, new ExistsOperationHandler(client, model, inputValidator), false);
10178
- }, "exists")
10208
+ }, "exists"),
10209
+ paginate: /* @__PURE__ */ __name((args) => {
10210
+ return createZenStackPromise(async () => {
10211
+ const rawArgs = args ?? {};
10212
+ const pageSize = rawArgs["pageSize"] ?? client.$options?.default?.pageSize ?? 20;
10213
+ const page = rawArgs["page"];
10214
+ const cursorId = rawArgs["cursor"];
10215
+ const findBaseArgs = {};
10216
+ for (const [k, v] of Object.entries(rawArgs)) {
10217
+ if (k !== "page" && k !== "cursor" && k !== "pageSize") {
10218
+ findBaseArgs[k] = v;
10219
+ }
10220
+ }
10221
+ const countResult = await operations.count({
10222
+ where: findBaseArgs["where"]
10223
+ });
10224
+ let items;
10225
+ if (page !== void 0) {
10226
+ items = await operations.findMany({
10227
+ ...findBaseArgs,
10228
+ skip: (page - 1) * pageSize,
10229
+ take: pageSize
10230
+ });
10231
+ } else {
10232
+ items = await operations.findMany({
10233
+ ...findBaseArgs,
10234
+ ...cursorId ? {
10235
+ cursor: {
10236
+ id: cursorId
10237
+ },
10238
+ skip: 1
10239
+ } : {},
10240
+ take: pageSize + 1
10241
+ });
10242
+ }
10243
+ const count = countResult;
10244
+ if (page !== void 0) {
10245
+ const total = Math.ceil(count / pageSize);
10246
+ const hasNext = page < total;
10247
+ const hasPrev = page > 1;
10248
+ return {
10249
+ items,
10250
+ pagination: {
10251
+ count,
10252
+ page: {
10253
+ size: pageSize,
10254
+ current: page,
10255
+ next: hasNext ? page + 1 : null,
10256
+ prev: hasPrev ? page - 1 : null,
10257
+ total
10258
+ },
10259
+ has: {
10260
+ next: hasNext,
10261
+ prev: hasPrev
10262
+ }
10263
+ }
10264
+ };
10265
+ } else {
10266
+ const hasNext = items.length > pageSize;
10267
+ if (hasNext) items = items.slice(0, pageSize);
10268
+ const hasPrev = cursorId != null;
10269
+ return {
10270
+ items,
10271
+ pagination: {
10272
+ count,
10273
+ page: {
10274
+ size: pageSize
10275
+ },
10276
+ has: {
10277
+ next: hasNext,
10278
+ prev: hasPrev
10279
+ },
10280
+ cursor: {
10281
+ next: hasNext && items.length > 0 ? items[items.length - 1].id : null,
10282
+ prev: hasPrev && items.length > 0 ? items[0].id : null
10283
+ }
10284
+ }
10285
+ };
10286
+ }
10287
+ });
10288
+ }, "paginate")
10179
10289
  };
10180
10290
  const slicing = client.$options.slicing;
10181
10291
  if (slicing?.models) {