@rebasepro/common 0.2.5 → 0.4.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.
@@ -1,4 +1,7 @@
1
- import { FindResponse, CollectionAccessor, QueryBuilderInterface, FilterOperator } from "@rebasepro/types";
1
+ import { FindResponse, CollectionAccessor, QueryBuilderInterface, FilterOperator, LogicalCondition, WhereValue, FilterCondition } from "@rebasepro/types";
2
+ export declare function or(...conditions: (FilterCondition | LogicalCondition)[]): LogicalCondition;
3
+ export declare function and(...conditions: (FilterCondition | LogicalCondition)[]): LogicalCondition;
4
+ export declare function cond(column: string, operator: FilterOperator, value: unknown): FilterCondition;
2
5
  export declare class QueryBuilder<M extends Record<string, unknown> = Record<string, unknown>> implements QueryBuilderInterface<M> {
3
6
  private collection;
4
7
  private params;
@@ -8,7 +11,8 @@ export declare class QueryBuilder<M extends Record<string, unknown> = Record<str
8
11
  * @example
9
12
  * client.collection('users').where('age', '>=', 18).find()
10
13
  */
11
- where(column: keyof M & string, operator: FilterOperator, value: unknown): this;
14
+ where<K extends keyof M & string>(column: K, operator: FilterOperator, value: WhereValue<M[K]>): this;
15
+ where(logicalCondition: LogicalCondition): this;
12
16
  /**
13
17
  * Order the results by a specific column.
14
18
  * @example
package/dist/index.es.js CHANGED
@@ -1973,6 +1973,13 @@ const defaultUsersCollection = {
1973
1973
  group: "Settings",
1974
1974
  openEntityMode: "dialog",
1975
1975
  disableDefaultActions: ["copy"],
1976
+ securityRules: [{
1977
+ operation: "select",
1978
+ roles: ["admin"]
1979
+ }, {
1980
+ operations: ["insert", "update", "delete"],
1981
+ roles: ["admin"]
1982
+ }],
1976
1983
  sort: ["createdAt", "desc"],
1977
1984
  properties: {
1978
1985
  id: {
@@ -2079,6 +2086,7 @@ const defaultUsersCollection = {
2079
2086
  name: "Created At",
2080
2087
  type: "date",
2081
2088
  columnName: "created_at",
2089
+ autoValue: "on_create",
2082
2090
  ui: {
2083
2091
  readOnly: true
2084
2092
  }
@@ -2099,29 +2107,24 @@ const defaultUsersCollection = {
2099
2107
  listProperties: ["displayName", "email", "roles", "createdAt"],
2100
2108
  propertiesOrder: ["id", "email", "displayName", "roles", "createdAt"]
2101
2109
  };
2102
- function mapOperator(op) {
2103
- switch (op) {
2104
- case "==":
2105
- return "eq";
2106
- case "!=":
2107
- return "neq";
2108
- case ">":
2109
- return "gt";
2110
- case ">=":
2111
- return "gte";
2112
- case "<":
2113
- return "lt";
2114
- case "<=":
2115
- return "lte";
2116
- case "array-contains":
2117
- return "cs";
2118
- case "array-contains-any":
2119
- return "csa";
2120
- case "not-in":
2121
- return "nin";
2122
- default:
2123
- return op;
2124
- }
2110
+ function or(...conditions) {
2111
+ return {
2112
+ type: "or",
2113
+ conditions
2114
+ };
2115
+ }
2116
+ function and(...conditions) {
2117
+ return {
2118
+ type: "and",
2119
+ conditions
2120
+ };
2121
+ }
2122
+ function cond(column, operator, value) {
2123
+ return {
2124
+ column,
2125
+ operator,
2126
+ value
2127
+ };
2125
2128
  }
2126
2129
  class QueryBuilder {
2127
2130
  constructor(collection) {
@@ -2130,23 +2133,30 @@ class QueryBuilder {
2130
2133
  params = {
2131
2134
  where: {}
2132
2135
  };
2133
- /**
2134
- * Add a filter condition to your query.
2135
- * @example
2136
- * client.collection('users').where('age', '>=', 18).find()
2137
- */
2138
- where(column, operator, value) {
2136
+ where(columnOrCondition, operator, value) {
2137
+ if (typeof columnOrCondition === "object" && columnOrCondition !== null && "type" in columnOrCondition) {
2138
+ this.params.logical = columnOrCondition;
2139
+ return this;
2140
+ }
2139
2141
  if (!this.params.where) {
2140
2142
  this.params.where = {};
2141
2143
  }
2142
- const mappedOp = mapOperator(operator);
2143
- let formattedValue = value;
2144
- if (Array.isArray(value) && ["in", "nin", "cs", "csa"].includes(mappedOp)) {
2145
- formattedValue = `(${value.join(",")})`;
2146
- } else if (value === null) {
2147
- formattedValue = "null";
2144
+ const column = columnOrCondition;
2145
+ const condition = [operator, value];
2146
+ const existing = this.params.where[column];
2147
+ if (existing === void 0) {
2148
+ this.params.where[column] = condition;
2149
+ } else if (Array.isArray(existing) && existing.length > 0 && Array.isArray(existing[0])) {
2150
+ this.params.where[column].push(condition);
2151
+ } else {
2152
+ let firstCondition;
2153
+ if (Array.isArray(existing) && existing.length === 2 && typeof existing[0] === "string") {
2154
+ firstCondition = existing;
2155
+ } else {
2156
+ firstCondition = ["==", existing];
2157
+ }
2158
+ this.params.where[column] = [firstCondition, condition];
2148
2159
  }
2149
- this.params.where[column] = mappedOp === "eq" ? String(formattedValue) : `${mappedOp}.${formattedValue}`;
2150
2160
  return this;
2151
2161
  }
2152
2162
  /**
@@ -2248,10 +2258,13 @@ function convertWhereToFilter(where) {
2248
2258
  filter[field] = ["==", rawValue];
2249
2259
  continue;
2250
2260
  }
2251
- if (Array.isArray(rawValue) && rawValue.length === 2) {
2252
- const [rawOp, val] = rawValue;
2253
- const mappedOp = operatorMap[rawOp] ?? "==";
2254
- filter[field] = [mappedOp, val];
2261
+ if (Array.isArray(rawValue)) {
2262
+ const conditions = Array.isArray(rawValue[0]) ? rawValue : [rawValue];
2263
+ const mappedConditions = conditions.map(([rawOp, val]) => {
2264
+ const mappedOp = operatorMap[rawOp] ?? "==";
2265
+ return [mappedOp, val];
2266
+ });
2267
+ filter[field] = Array.isArray(rawValue[0]) ? mappedConditions : mappedConditions[0];
2255
2268
  continue;
2256
2269
  }
2257
2270
  if (typeof rawValue === "string") {
@@ -2389,8 +2402,12 @@ function createDriverAccessor(driver, slug) {
2389
2402
  });
2390
2403
  } : void 0,
2391
2404
  // Fluent Query Builder
2392
- where(column, operator, value) {
2393
- return new QueryBuilder(accessor).where(column, operator, value);
2405
+ where(columnOrCondition, operator, value) {
2406
+ const builder = new QueryBuilder(accessor);
2407
+ if (typeof columnOrCondition === "object") {
2408
+ return builder.where(columnOrCondition);
2409
+ }
2410
+ return builder.where(columnOrCondition, operator, value);
2394
2411
  },
2395
2412
  orderBy(column, ascending) {
2396
2413
  return new QueryBuilder(accessor).orderBy(column, ascending);
@@ -2440,6 +2457,7 @@ export {
2440
2457
  DEFAULT_ONE_OF_VALUE,
2441
2458
  QueryBuilder,
2442
2459
  addInitialSlash,
2460
+ and,
2443
2461
  applyPropertyConditions,
2444
2462
  buildAdditionalFieldDelegate,
2445
2463
  buildCollection,
@@ -2457,6 +2475,7 @@ export {
2457
2475
  canEditEntity,
2458
2476
  canReadCollection,
2459
2477
  checkOperation,
2478
+ cond,
2460
2479
  createRelationRef,
2461
2480
  createRelationRefWithData,
2462
2481
  defaultUsersCollection,
@@ -2488,6 +2507,7 @@ export {
2488
2507
  isPropertyBuilder,
2489
2508
  isReadOnly,
2490
2509
  normalizeToEntityRelation,
2510
+ or,
2491
2511
  registerConditionOperations,
2492
2512
  removeInitialAndTrailingSlashes,
2493
2513
  removeInitialSlash,