unconventional 1.5.0 → 1.6.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.
@@ -26,6 +26,10 @@ export declare class BaseModel extends Document {
26
26
  static delete<T extends typeof BaseModel>(this: T, id: string | number): Promise<InstanceType<T> | null>;
27
27
  static deleteMany<T extends typeof BaseModel>(this: T, config?: FilterConfig): Promise<InstanceType<T>[] | null>;
28
28
  static count<T extends typeof BaseModel>(this: T): Promise<number | null>;
29
+ static increment<T extends typeof BaseModel>(this: T, id: string | number, data: {
30
+ [key in keyof InstanceType<T>]: number;
31
+ }): Promise<InstanceType<T> | null>;
32
+ static truncate<T extends typeof BaseModel>(this: T): Promise<void>;
29
33
  static fromDatabaseJson<T extends typeof BaseModel>(this: T, json: object): InstanceType<T>;
30
34
  private static addDefaults;
31
35
  /** Gets the field name to use for the provided identifier */
@@ -154,12 +154,36 @@ class BaseModel extends Document {
154
154
  action: db_types_1.SqlAction.Select,
155
155
  type: unconventional_pg_queries_1.OneOrMany.Many,
156
156
  table: this.collection,
157
- count: true
157
+ subAction: unconventional_pg_queries_1.SubAction.Count
158
158
  });
159
159
  if (!response)
160
160
  return null;
161
161
  return response.count;
162
162
  }
163
+ static async increment(id, data) {
164
+ const targetField = this.getFieldName(id);
165
+ if (!targetField)
166
+ return null;
167
+ const response = await this.db.fetch({
168
+ action: db_types_1.SqlAction.Update,
169
+ type: unconventional_pg_queries_1.OneOrMany.One,
170
+ table: this.collection,
171
+ where: [{ field: targetField, operator: unconventional_pg_queries_1.SqlWhereOperator.Eq, value: id }],
172
+ subAction: unconventional_pg_queries_1.SubAction.Increment,
173
+ data
174
+ });
175
+ if (!response)
176
+ return null;
177
+ const result = Array.isArray(response) ? response[0] : response;
178
+ return this.fromDatabaseJson(result);
179
+ }
180
+ static async truncate() {
181
+ await this.db.fetch({
182
+ action: db_types_1.SqlAction.Truncate,
183
+ type: unconventional_pg_queries_1.OneOrMany.Many,
184
+ table: this.collection
185
+ });
186
+ }
163
187
  static fromDatabaseJson(json) {
164
188
  const model = new this();
165
189
  Object.assign(model, json);
@@ -9,7 +9,8 @@ export declare enum SqlAction {
9
9
  Select = "SELECT",
10
10
  Insert = "INSERT",
11
11
  Update = "UPDATE",
12
- Delete = "DELETE"
12
+ Delete = "DELETE",
13
+ Truncate = "TRUNCATE"
13
14
  }
14
15
  export interface DBCall extends QueryDefinition {
15
16
  action: SqlAction;
@@ -12,4 +12,5 @@ var SqlAction;
12
12
  SqlAction["Insert"] = "INSERT";
13
13
  SqlAction["Update"] = "UPDATE";
14
14
  SqlAction["Delete"] = "DELETE";
15
+ SqlAction["Truncate"] = "TRUNCATE";
15
16
  })(SqlAction || (exports.SqlAction = SqlAction = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unconventional",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "A complete server built on Cloudflare Workers/Pages",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "peerDependencies": {
47
47
  "hono": "^4",
48
- "unconventional-pg-queries": "^0.0.8"
48
+ "unconventional-pg-queries": "^1.0.0"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@cloudflare/workers-types": "^4.20231121.0",
@@ -53,6 +53,6 @@
53
53
  "@typescript-eslint/parser": "^5.62.0",
54
54
  "hono": "^4",
55
55
  "typescript": "^5.3.3",
56
- "unconventional-pg-queries": "^0.0.8"
56
+ "unconventional-pg-queries": "^1.0.0"
57
57
  }
58
58
  }