Skip to content

Commit

Permalink
Added min, max, sum, and avg methods to VingKind.
Browse files Browse the repository at this point in the history
  • Loading branch information
rizen committed Apr 11, 2024
1 parent eabeb29 commit 81c3935
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ outline: deep
* Fixed: drizzle kit is now interactive #93
* Fixed: cannot dispose useVingRecord on edit page #102
* Added UserAvatar component.
* Added min, max, sum, and avg methods to VingKind.

## 2024-04-10
* Implemented: filterQualifier: true should be in the examples for all relation ids #96
Expand Down
2 changes: 1 addition & 1 deletion ving/drizzle/orm.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { drizzle } from 'drizzle-orm/mysql2';
import { customType } from 'drizzle-orm/mysql-core';
export { boolean, mysqlEnum, mysqlTable, timestamp, uniqueIndex, varchar, text, datetime, int, json, foreignKey } from 'drizzle-orm/mysql-core';
export { like, eq, asc, desc, and, or, ne, gt, gte, lt, lte, inArray, getTableName } from 'drizzle-orm';
export { like, eq, asc, desc, and, or, ne, gt, gte, lt, lte, inArray, getTableName, sum, min, max, avg, count } from 'drizzle-orm';
export { sql } from 'drizzle-orm/sql';

/**
Expand Down
58 changes: 55 additions & 3 deletions ving/record/VingRecord.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { findVingSchema } from '#ving/schema/map.mjs';
import ving from '#ving/index.mjs';
import _ from 'lodash';
import { eq, asc, desc, and, ne, sql, getTableName } from '#ving/drizzle/orm.mjs';
import { eq, asc, desc, and, ne, sql, getTableName, count, sum, avg, min, max } from '#ving/drizzle/orm.mjs';
import { stringDefault, booleanDefault, numberDefault, dateDefault } from '#ving/schema/helpers.mjs';

/**
Expand Down Expand Up @@ -652,10 +652,62 @@ export class VingKind {
* Usage: `const numberOfUsers = await Users.count()`
*
* @param {Object} where A drizzle where clause
* @returns A count of the records
* @returns {number} A count of the records
*/
async count(where) {
return (await this.db.select({ count: sql`count(*)`.as('count') }).from(this.table).where(this.calcWhere(where)))[0].count;
return (await this.db.select({ value: count() }).from(this.table).where(this.calcWhere(where)))[0].value;
}

/**
* Sum the values of a field of this kind
*
* Usage: `await S3Files.sum('sizeInBytes')`
*
* @param {string} field The name of the column to sum
* @param {Object} where A drizzle where clause
* @returns {number} A count of the records
*/
async sum(where) {
return (await this.db.select({ value: sum(this.table[field]) }).from(this.table).where(this.calcWhere(where)))[0].value;
}

/**
* Average the values of a field of this kind
*
* Usage: `await S3Files.avg('sizeInBytes')`
*
* @param {string} field The name of the column to average
* @param {Object} where A drizzle where clause
* @returns {number} A count of the records
*/
async avg(where) {
return (await this.db.select({ value: avg(this.table[field]) }).from(this.table).where(this.calcWhere(where)))[0].value;
}

/**
* Get the minimum value for a field of this kind
*
* Usage: `await S3Files.min('sizeInBytes')`
*
* @param {string} field The name of the column to get the minimum
* @param {Object} where A drizzle where clause
* @returns {number} A count of the records
*/
async min(where) {
return (await this.db.select({ value: min(this.table[field]) }).from(this.table).where(this.calcWhere(where)))[0].value;
}

/**
* Get the maximum value for a field of this kind.
*
* Usage: `await S3Files.max('sizeInBytes')`
*
* @param {string} field The name of the column to get the maximum
* @param {Object} where A drizzle where clause
* @returns {number} A count of the records
*/
async max(where) {
return (await this.db.select({ value: max(this.table[field]) }).from(this.table).where(this.calcWhere(where)))[0].value;
}

/**
Expand Down

0 comments on commit 81c3935

Please sign in to comment.