unconventional 2.3.0 → 3.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.
@@ -31,6 +31,10 @@ export declare class BaseModel extends Document {
31
31
  static findMany<T extends typeof BaseModel>(this: T, config?: PageConfig & FilterConfig): Promise<Array<InstanceType<T>>>;
32
32
  static update<T extends typeof BaseModel>(this: T, id: string | number, data: Partial<InstanceType<T>>): Promise<InstanceType<T> | null>;
33
33
  static updateMany<T extends typeof BaseModel>(this: T, data: Partial<InstanceType<T>>[]): Promise<InstanceType<T>[] | null>;
34
+ /** Applies one patch to every record matching a required filter. */
35
+ static updateWhere<T extends typeof BaseModel>(this: T, data: Partial<InstanceType<T>>, config: FilterConfig & {
36
+ filter: string;
37
+ }): Promise<InstanceType<T>[] | null>;
34
38
  static delete<T extends typeof BaseModel>(this: T, id: string | number): Promise<InstanceType<T> | null>;
35
39
  static deleteMany<T extends typeof BaseModel>(this: T, config?: FilterConfig): Promise<InstanceType<T>[] | null>;
36
40
  static count<T extends typeof BaseModel>(this: T, config?: FilterConfig): Promise<number | null>;
@@ -133,6 +133,26 @@ class BaseModel extends Document {
133
133
  return null;
134
134
  return response.map(item => this.fromDatabaseJson(item));
135
135
  }
136
+ /** Applies one patch to every record matching a required filter. */
137
+ static async updateWhere(data, config) {
138
+ if (!config?.filter)
139
+ throw new Error("No filter provided");
140
+ const timestamp = this.schema.timestamp ? { [this.schema.timestamp.updatedField]: new Date() } : {};
141
+ const where = (0, db_utils_1.getWhere)(this, config.filter);
142
+ const expand = (0, db_utils_1.expandRelationalFilters)(this, where, (0, db_utils_1.getExpand)(this, config.expand));
143
+ const response = await this.db.fetch({
144
+ action: db_types_1.SqlAction.Update,
145
+ type: unconventional_pg_queries_1.OneOrMany.Many,
146
+ table: this.collection,
147
+ expand,
148
+ where,
149
+ data: this.prepareData({ ...data, ...timestamp }, false)
150
+ });
151
+ if (!response)
152
+ return null;
153
+ const results = Array.isArray(response) ? response : [response];
154
+ return results.map(item => this.fromDatabaseJson(item));
155
+ }
136
156
  static async delete(id) {
137
157
  const targetField = this.getFieldName(id);
138
158
  if (!targetField)
@@ -47,6 +47,10 @@ export declare class BaseService<M extends typeof BaseModel> {
47
47
  * @returns An array of updated documents.
48
48
  */
49
49
  updateMany(data: Partial<InstanceType<M>>[], config?: ServiceConfig<M>): Promise<InstanceType<M>[]>;
50
+ /** Applies one patch to every document matching a required filter. */
51
+ updateWhere(data: Partial<InstanceType<M>>, config: FilterConfig & {
52
+ filter: string;
53
+ } & ServiceConfig<M>): Promise<InstanceType<M>[]>;
50
54
  /**
51
55
  * Deletes the document with the given identifier.
52
56
  * @param identifier - The id or key of a document. If type is number, lookup will be done with the model's idColumn. Otherwise, keyColumn is used.
@@ -138,6 +138,22 @@ class BaseService {
138
138
  }
139
139
  return response;
140
140
  }
141
+ /** Applies one patch to every document matching a required filter. */
142
+ async updateWhere(data, config) {
143
+ if (!config?.filter)
144
+ throw api_error_1.APIError.errBadRequest("No filter provided");
145
+ const response = await this.model.updateWhere(data, { filter: config.filter, expand: config.expand });
146
+ if (!response) {
147
+ throw api_error_1.APIError.errResourceUpdateFailed(`Failed to update ${this.model.name}s`);
148
+ }
149
+ else if (config.cache) {
150
+ const cache = config.cache;
151
+ await base_cache_1.BaseCache.clearPage(cache, this.model);
152
+ await Promise.all(response.map(item => base_cache_1.BaseCache.clearModel(cache, item)));
153
+ await Promise.all(response.map(item => base_cache_1.BaseCache.setModel(cache, item, undefined, config.cacheTTL)));
154
+ }
155
+ return response;
156
+ }
141
157
  /**
142
158
  * Deletes the document with the given identifier.
143
159
  * @param identifier - The id or key of a document. If type is number, lookup will be done with the model's idColumn. Otherwise, keyColumn is used.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unconventional",
3
- "version": "2.3.0",
3
+ "version": "3.0.0",
4
4
  "description": "A complete server built on Cloudflare Workers/Pages",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "peerDependencies": {
48
48
  "hono": "^4",
49
- "unconventional-pg-queries": "^2.0.0"
49
+ "unconventional-pg-queries": "^3.0.0"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@cloudflare/workers-types": "^4.20231121.0",
@@ -56,6 +56,6 @@
56
56
  "hono": "^4",
57
57
  "tsx": "^4.20.6",
58
58
  "typescript": "^5.3.3",
59
- "unconventional-pg-queries": "^2.0.0"
59
+ "unconventional-pg-queries": "^3.0.0"
60
60
  }
61
61
  }