unconventional 2.1.0 → 2.3.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.
@@ -42,7 +42,7 @@ class BaseModel extends Document {
42
42
  action: db_types_1.SqlAction.Insert,
43
43
  type: unconventional_pg_queries_1.OneOrMany.One,
44
44
  table: this.collection,
45
- conflict: (0, db_utils_1.getConflict)(this, upsertConfig?.action || unconventional_pg_queries_1.ConflictResolution.doUpdate, upsertConfig?.constraint),
45
+ conflict: (0, db_utils_1.getConflict)(this, upsertConfig?.action || unconventional_pg_queries_1.ConflictResolution.doUpdate, upsertConfig?.constraint, upsertConfig?.where),
46
46
  data: this.prepareData({ ...data, ...timestamp })
47
47
  });
48
48
  if (!response)
@@ -59,7 +59,7 @@ class BaseModel extends Document {
59
59
  action: db_types_1.SqlAction.Insert,
60
60
  type: unconventional_pg_queries_1.OneOrMany.Many,
61
61
  table: this.collection,
62
- conflict: (0, db_utils_1.getConflict)(this, upsertConfig?.action || unconventional_pg_queries_1.ConflictResolution.doUpdate, upsertConfig?.constraint),
62
+ conflict: (0, db_utils_1.getConflict)(this, upsertConfig?.action || unconventional_pg_queries_1.ConflictResolution.doUpdate, upsertConfig?.constraint, upsertConfig?.where),
63
63
  data: data.map(item => this.prepareData({ ...item, ...timestamp }))
64
64
  });
65
65
  if (!response)
@@ -18,6 +18,7 @@ interface DownloadResponse {
18
18
  export interface UpsertConfig<M extends typeof BaseModel> {
19
19
  constraint?: Array<keyof InstanceType<M>>;
20
20
  action?: ConflictResolution;
21
+ where?: string;
21
22
  }
22
23
  export interface ServiceConfig<M extends typeof BaseModel> {
23
24
  cache?: KVNamespace;
@@ -7,6 +7,7 @@ export type IndexDefinition<T> = {
7
7
  /** Options to pass to MongoDB when creating an Index */
8
8
  export interface IndexOptions {
9
9
  unique?: boolean;
10
+ where?: string;
10
11
  }
11
12
  /**
12
13
  * Type for the Values stored in the Reflection for Indexes
@@ -9,7 +9,7 @@ export declare function getOrder<T extends typeof BaseModel>(model: T, sort?: st
9
9
  export declare function getPage<T extends typeof BaseModel>(model: T, cursor?: string | number): SqlPaginate | undefined;
10
10
  /** Retrieves all non-relational fields from a model for the 'returning' clause of a SQL call */
11
11
  export declare function getReturn<T extends typeof BaseModel>(model: T): string[];
12
- export declare function getConflict<T extends typeof BaseModel>(model: T, action: ConflictResolution, constraint?: Array<keyof InstanceType<T>>): SqlConflict | undefined;
12
+ export declare function getConflict<T extends typeof BaseModel>(model: T, action: ConflictResolution, constraint?: Array<keyof InstanceType<T>>, where?: string): SqlConflict | undefined;
13
13
  /** A type-safe way to build filter strings */
14
14
  export declare function buildFilter<T>(field: keyof T, operator: SqlWhereOperator, value?: any): string;
15
15
  /** Creates expansions for relational filters */
@@ -213,27 +213,32 @@ function getReturn(model) {
213
213
  return fields;
214
214
  }
215
215
  exports.getReturn = getReturn;
216
- function getConflict(model, action, constraint) {
216
+ function getConflict(model, action, constraint, where) {
217
217
  if (!constraint)
218
218
  return undefined;
219
219
  const indexes = model.schema.indexes;
220
220
  for (const index of indexes) {
221
221
  const keys = Object.keys(index.definition);
222
- if ((0, array_utils_1.equalArrays)(keys, constraint))
223
- return { action, constraint: keys };
222
+ if ((0, array_utils_1.equalArrays)(keys, constraint)) {
223
+ return { action, constraint: keys, where: where ?? index.options?.where };
224
+ }
224
225
  }
225
226
  return undefined;
226
227
  }
227
228
  exports.getConflict = getConflict;
228
229
  /** A type-safe way to build filter strings */
229
230
  function buildFilter(field, operator, value) {
231
+ const quote = (item) => `'${item.replace(/'/g, "''")}'`;
230
232
  let formatted = `${value}`;
231
- if (typeof value === 'string')
232
- formatted = `'${value}'`;
233
- else if (Array.isArray(value))
234
- formatted = (typeof value[0] === 'string') ? `('${value.join("','")}')` : `(${value.join()})`;
235
- else if (value == null || db_types_1.NullSqlOperators.includes(operator))
233
+ if (value == null || db_types_1.NullSqlOperators.includes(operator)) {
236
234
  formatted = '';
235
+ }
236
+ else if (typeof value === 'string') {
237
+ formatted = quote(value);
238
+ }
239
+ else if (Array.isArray(value)) {
240
+ formatted = `(${value.map(item => typeof item === 'string' ? quote(item) : `${item}`).join(',')})`;
241
+ }
237
242
  return `${field} ${operator} ${formatted}`;
238
243
  }
239
244
  exports.buildFilter = buildFilter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unconventional",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "A complete server built on Cloudflare Workers/Pages",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -46,15 +46,16 @@
46
46
  },
47
47
  "peerDependencies": {
48
48
  "hono": "^4",
49
- "unconventional-pg-queries": "^1.7.0"
49
+ "unconventional-pg-queries": "^2.0.0"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@cloudflare/workers-types": "^4.20231121.0",
53
53
  "@typescript-eslint/eslint-plugin": "^5.62.0",
54
54
  "@typescript-eslint/parser": "^5.62.0",
55
+ "generate-changelog": "^1.8.0",
55
56
  "hono": "^4",
56
57
  "tsx": "^4.20.6",
57
58
  "typescript": "^5.3.3",
58
- "unconventional-pg-queries": "^1.7.0"
59
+ "unconventional-pg-queries": "^2.0.0"
59
60
  }
60
61
  }