remult 0.27.8-next.3 → 0.27.8-next.4
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.
- package/esm/remult-knex/index.js +3 -3
- package/esm/remult-mongo.js +6 -6
- package/esm/src/data-api.js +13 -17
- package/esm/src/data-providers/array-entity-data-provider.js +11 -11
- package/esm/src/data-providers/json-data-provider.js +2 -2
- package/esm/src/data-providers/rest-data-provider.js +5 -5
- package/esm/src/data-providers/sql-database.js +8 -8
- package/esm/src/remult-proxy.js +5 -0
- package/esm/src/remult3/RepositoryImplementation.js +10 -9
- package/esm/src/remult3/remult3.js +4 -3
- package/index.d.ts +2 -2
- package/package.json +1 -1
- package/remult-knex/index.js +2 -2
- package/remult-mongo.js +8 -8
- package/src/data-api.d.ts +1 -1
- package/src/data-api.js +15 -19
- package/src/data-interfaces.d.ts +5 -5
- package/src/data-providers/array-entity-data-provider.d.ts +2 -2
- package/src/data-providers/array-entity-data-provider.js +13 -13
- package/src/data-providers/json-data-provider.js +2 -2
- package/src/data-providers/rest-data-provider.d.ts +2 -2
- package/src/data-providers/rest-data-provider.js +5 -5
- package/src/data-providers/sql-database.d.ts +2 -2
- package/src/data-providers/sql-database.js +13 -13
- package/src/remult-proxy.js +11 -0
- package/src/remult3/RepositoryImplementation.d.ts +3 -2
- package/src/remult3/RepositoryImplementation.js +18 -10
- package/src/remult3/remult3.d.ts +28 -14
- package/src/remult3/remult3.js +5 -4
package/esm/remult-knex/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { Sort } from '../src/sort.js';
|
|
|
7
7
|
import { ValueConverters } from '../src/valueConverters.js';
|
|
8
8
|
import { getRepositoryInternals } from '../src/remult3/repository-internals.js';
|
|
9
9
|
import { remultStatic } from '../src/remult-static.js';
|
|
10
|
-
import {
|
|
10
|
+
import { groupByImpl, getRowAfterUpdate, } from '../src/data-providers/sql-database.js';
|
|
11
11
|
export class KnexDataProvider {
|
|
12
12
|
knex;
|
|
13
13
|
constructor(knex) {
|
|
@@ -117,8 +117,8 @@ class KnexEntityDataProvider {
|
|
|
117
117
|
this.rawSqlWrapIdentifier = rawSqlWrapIdentifier;
|
|
118
118
|
this.dp = dp;
|
|
119
119
|
}
|
|
120
|
-
async
|
|
121
|
-
return
|
|
120
|
+
async groupBy(options) {
|
|
121
|
+
return groupByImpl(options, await this.init(), this.dp.createCommand(), false, (limit, offset) => ' limit ' + limit + ' offset ' + offset);
|
|
122
122
|
}
|
|
123
123
|
async count(where) {
|
|
124
124
|
const e = await this.init();
|
package/esm/remult-mongo.js
CHANGED
|
@@ -5,7 +5,7 @@ import { remult as remultContext } from './src/remult-proxy.js';
|
|
|
5
5
|
import { getRepository } from './src/remult3/RepositoryImplementation.js';
|
|
6
6
|
import { getRepositoryInternals } from './src/remult3/repository-internals.js';
|
|
7
7
|
import { getRowAfterUpdate } from './src/data-providers/sql-database.js';
|
|
8
|
-
import {
|
|
8
|
+
import { GroupByCountMember, GroupByOperators } from './src/remult3/remult3.js';
|
|
9
9
|
export class MongoDataProvider {
|
|
10
10
|
db;
|
|
11
11
|
client;
|
|
@@ -98,7 +98,7 @@ class MongoEntityDataProvider {
|
|
|
98
98
|
let w = await x.resolveWhere();
|
|
99
99
|
return await collection.countDocuments(w, { session: this.session });
|
|
100
100
|
}
|
|
101
|
-
async
|
|
101
|
+
async groupBy(options) {
|
|
102
102
|
let { collection, e } = await this.collection();
|
|
103
103
|
let x = new FilterConsumerBridgeToMongo(e);
|
|
104
104
|
const pipeLine = [];
|
|
@@ -112,14 +112,14 @@ class MongoEntityDataProvider {
|
|
|
112
112
|
__count: { $sum: 1 },
|
|
113
113
|
};
|
|
114
114
|
processResultRow.push((mongoRow, resultRow) => {
|
|
115
|
-
resultRow[
|
|
115
|
+
resultRow[GroupByCountMember] = mongoRow.__count;
|
|
116
116
|
});
|
|
117
117
|
pipeLine.push({ $group });
|
|
118
118
|
const $addFields = {};
|
|
119
119
|
pipeLine.push({ $addFields });
|
|
120
|
-
if (options?.
|
|
120
|
+
if (options?.group) {
|
|
121
121
|
$group._id = {};
|
|
122
|
-
for (const element of options.
|
|
122
|
+
for (const element of options.group) {
|
|
123
123
|
const name = e.$dbNameOf(element);
|
|
124
124
|
$group._id[name] = `$${name}`;
|
|
125
125
|
processResultRow.push((mongoRow, resultRow) => {
|
|
@@ -130,7 +130,7 @@ class MongoEntityDataProvider {
|
|
|
130
130
|
else {
|
|
131
131
|
$group._id = null;
|
|
132
132
|
}
|
|
133
|
-
for (const operator of
|
|
133
|
+
for (const operator of GroupByOperators) {
|
|
134
134
|
if (options?.[operator]) {
|
|
135
135
|
for (const element of options[operator]) {
|
|
136
136
|
const name = e.$dbNameOf(element) + '_' + operator;
|
package/esm/src/data-api.js
CHANGED
|
@@ -2,7 +2,7 @@ import { doTransaction } from './context.js';
|
|
|
2
2
|
import { findOptionsToJson, liveQueryAction, } from './data-providers/rest-data-provider.js';
|
|
3
3
|
import { Filter, buildFilterFromRequestParameters, customUrlToken, } from './filter/filter-interfaces.js';
|
|
4
4
|
import { getRelationFieldInfo } from './remult3/relationInfoMember.js';
|
|
5
|
-
import {
|
|
5
|
+
import { GroupByCountMember, GroupByForApiKey, } from './remult3/remult3.js';
|
|
6
6
|
import { ForbiddenError } from './server-action.js';
|
|
7
7
|
export class DataApi {
|
|
8
8
|
repository;
|
|
@@ -19,8 +19,8 @@ export class DataApi {
|
|
|
19
19
|
case 'get':
|
|
20
20
|
case 'count':
|
|
21
21
|
return this.count(res, req, undefined);
|
|
22
|
-
case '
|
|
23
|
-
return this.
|
|
22
|
+
case 'groupBy':
|
|
23
|
+
return this.groupBy(res, req, undefined);
|
|
24
24
|
}
|
|
25
25
|
return this.getArray(res, req, undefined);
|
|
26
26
|
}
|
|
@@ -45,8 +45,8 @@ export class DataApi {
|
|
|
45
45
|
case 'count':
|
|
46
46
|
validateWhereInBody();
|
|
47
47
|
return this.count(res, req, body);
|
|
48
|
-
case '
|
|
49
|
-
return this.
|
|
48
|
+
case 'groupBy':
|
|
49
|
+
return this.groupBy(res, req, body);
|
|
50
50
|
case 'deleteMany':
|
|
51
51
|
validateWhereInBody();
|
|
52
52
|
return this.deleteMany(res, req, body);
|
|
@@ -103,7 +103,7 @@ export class DataApi {
|
|
|
103
103
|
response.error(err, this.repository.metadata);
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
|
-
async
|
|
106
|
+
async groupBy(response, request, body) {
|
|
107
107
|
let findOptions = await this.findOptionsFromRequest(request, body);
|
|
108
108
|
let orderBy = {};
|
|
109
109
|
if (body?.orderBy) {
|
|
@@ -114,7 +114,7 @@ export class DataApi {
|
|
|
114
114
|
orderBy[element.field] = direction;
|
|
115
115
|
break;
|
|
116
116
|
case 'count':
|
|
117
|
-
orderBy[
|
|
117
|
+
orderBy[GroupByCountMember] = direction;
|
|
118
118
|
break;
|
|
119
119
|
default:
|
|
120
120
|
orderBy[element.field] = {
|
|
@@ -125,14 +125,14 @@ export class DataApi {
|
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
-
const
|
|
129
|
-
let result = await this.repository.
|
|
128
|
+
const group = body?.groupBy?.filter((x) => this.repository.fields.find(x).includedInApi());
|
|
129
|
+
let result = await this.repository.groupBy({
|
|
130
130
|
where: findOptions.where,
|
|
131
131
|
limit: findOptions.limit,
|
|
132
132
|
page: findOptions.page,
|
|
133
133
|
//@ts-expect-error internal key
|
|
134
|
-
[
|
|
135
|
-
|
|
134
|
+
[GroupByForApiKey]: true,
|
|
135
|
+
group,
|
|
136
136
|
sum: body?.sum?.filter((x) => this.repository.fields.find(x).includedInApi()),
|
|
137
137
|
avg: body?.avg?.filter((x) => this.repository.fields.find(x).includedInApi()),
|
|
138
138
|
min: body?.min?.filter((x) => this.repository.fields.find(x).includedInApi()),
|
|
@@ -140,16 +140,12 @@ export class DataApi {
|
|
|
140
140
|
distinctCount: body?.distinctCount?.filter((x) => this.repository.fields.find(x).includedInApi()),
|
|
141
141
|
orderBy: orderBy,
|
|
142
142
|
});
|
|
143
|
-
if (
|
|
144
|
-
result = [result];
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
143
|
+
if (group)
|
|
147
144
|
result.forEach((x) => {
|
|
148
|
-
for (const f of
|
|
145
|
+
for (const f of group) {
|
|
149
146
|
x[f] = this.repository.fields.find(f).valueConverter.toJson(x[f]);
|
|
150
147
|
}
|
|
151
148
|
});
|
|
152
|
-
}
|
|
153
149
|
response.success(result);
|
|
154
150
|
}
|
|
155
151
|
async getArrayImpl(response, request, body) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CompoundIdField } from '../CompoundIdField.js';
|
|
2
2
|
import { dbNamesOf, isDbReadonly, } from '../filter/filter-consumer-bridge-to-sql-request.js';
|
|
3
3
|
import { Filter, customDatabaseFilterToken, } from '../filter/filter-interfaces.js';
|
|
4
|
-
import {
|
|
4
|
+
import { GroupByCountMember, GroupByOperators, } from '../remult3/remult3.js';
|
|
5
5
|
import { compareForSort, Sort } from '../sort.js';
|
|
6
6
|
export class ArrayEntityDataProvider {
|
|
7
7
|
entity;
|
|
@@ -17,10 +17,10 @@ export class ArrayEntityDataProvider {
|
|
|
17
17
|
this.entity = entity;
|
|
18
18
|
this.rows = rows;
|
|
19
19
|
}
|
|
20
|
-
async
|
|
20
|
+
async groupBy(options) {
|
|
21
21
|
const sort = new Sort();
|
|
22
|
-
if (options?.
|
|
23
|
-
for (const field of options?.
|
|
22
|
+
if (options?.group)
|
|
23
|
+
for (const field of options?.group) {
|
|
24
24
|
sort.Segments.push({ field: field });
|
|
25
25
|
}
|
|
26
26
|
const rows = await this.find({ orderBy: sort, where: options?.where });
|
|
@@ -112,7 +112,7 @@ export class ArrayEntityDataProvider {
|
|
|
112
112
|
};
|
|
113
113
|
},
|
|
114
114
|
};
|
|
115
|
-
for (let operator of
|
|
115
|
+
for (let operator of GroupByOperators) {
|
|
116
116
|
if (options?.[operator]) {
|
|
117
117
|
for (const element of options[operator]) {
|
|
118
118
|
aggregates.push(operatorImpl[operator](element.key));
|
|
@@ -124,15 +124,15 @@ export class ArrayEntityDataProvider {
|
|
|
124
124
|
for (const a of aggregates) {
|
|
125
125
|
a.finishGroup(r);
|
|
126
126
|
}
|
|
127
|
-
r[
|
|
127
|
+
r[GroupByCountMember] = count;
|
|
128
128
|
result.push(r);
|
|
129
129
|
first = true;
|
|
130
130
|
count = 0;
|
|
131
131
|
}
|
|
132
132
|
for (const row of rows) {
|
|
133
|
-
if (options?.
|
|
133
|
+
if (options?.group) {
|
|
134
134
|
if (!first) {
|
|
135
|
-
for (const field of options?.
|
|
135
|
+
for (const field of options?.group) {
|
|
136
136
|
if (group[field.key] != row[field.key]) {
|
|
137
137
|
finishGroup();
|
|
138
138
|
break;
|
|
@@ -140,7 +140,7 @@ export class ArrayEntityDataProvider {
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
if (first) {
|
|
143
|
-
for (const field of options?.
|
|
143
|
+
for (const field of options?.group) {
|
|
144
144
|
group[field.key] = row[field.key];
|
|
145
145
|
}
|
|
146
146
|
}
|
|
@@ -157,12 +157,12 @@ export class ArrayEntityDataProvider {
|
|
|
157
157
|
for (const x of options.orderBy) {
|
|
158
158
|
const getValue = (row) => {
|
|
159
159
|
if (!x.field && x.operation == 'count') {
|
|
160
|
-
return row[
|
|
160
|
+
return row[GroupByCountMember];
|
|
161
161
|
}
|
|
162
162
|
else {
|
|
163
163
|
switch (x.operation) {
|
|
164
164
|
case 'count':
|
|
165
|
-
return row[
|
|
165
|
+
return row[GroupByCountMember];
|
|
166
166
|
case undefined:
|
|
167
167
|
return row[x.field.key];
|
|
168
168
|
default:
|
|
@@ -22,8 +22,8 @@ class JsonEntityDataProvider {
|
|
|
22
22
|
this.helper = helper;
|
|
23
23
|
this.formatted = formatted;
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
return (this.p = this.p.then(() => this.loadEntityData((dp, save) => dp.
|
|
25
|
+
groupBy(options) {
|
|
26
|
+
return (this.p = this.p.then(() => this.loadEntityData((dp, save) => dp.groupBy(options))));
|
|
27
27
|
}
|
|
28
28
|
async loadEntityData(what) {
|
|
29
29
|
let data = [];
|
|
@@ -101,14 +101,14 @@ export class RestEntityDataProvider {
|
|
|
101
101
|
this.http = http;
|
|
102
102
|
this.entity = entity;
|
|
103
103
|
}
|
|
104
|
-
async
|
|
104
|
+
async groupBy(options) {
|
|
105
105
|
const { run } = this.buildFindRequest({
|
|
106
106
|
where: options?.where,
|
|
107
107
|
limit: options?.limit,
|
|
108
108
|
page: options?.page,
|
|
109
109
|
});
|
|
110
110
|
const body = {
|
|
111
|
-
groupBy: options?.
|
|
111
|
+
groupBy: options?.group?.map((x) => x.key),
|
|
112
112
|
sum: options?.sum?.map((x) => x.key),
|
|
113
113
|
avg: options?.avg?.map((x) => x.key),
|
|
114
114
|
min: options?.min?.map((x) => x.key),
|
|
@@ -116,10 +116,10 @@ export class RestEntityDataProvider {
|
|
|
116
116
|
distinctCount: options?.distinctCount?.map((x) => x.key),
|
|
117
117
|
orderBy: options?.orderBy?.map((x) => ({ ...x, field: x.field?.key })),
|
|
118
118
|
};
|
|
119
|
-
const result = await run('
|
|
120
|
-
if (options?.
|
|
119
|
+
const result = await run('groupBy', Object.keys(body).length > 0 ? body : undefined);
|
|
120
|
+
if (options?.group)
|
|
121
121
|
result.forEach((row) => {
|
|
122
|
-
for (const g of options.
|
|
122
|
+
for (const g of options.group) {
|
|
123
123
|
row[g.key] = g.valueConverter.fromJson(row[g.key]);
|
|
124
124
|
}
|
|
125
125
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { FilterConsumerBridgeToSqlRequest, dbNamesOfWithForceSqlExpression, isDbReadonly, } from '../filter/filter-consumer-bridge-to-sql-request.js';
|
|
2
2
|
import { Filter, customDatabaseFilterToken, } from '../filter/filter-interfaces.js';
|
|
3
3
|
import { remult as defaultRemult } from '../remult-proxy.js';
|
|
4
|
-
import {
|
|
4
|
+
import { GroupByCountMember, GroupByOperators, } from '../remult3/remult3.js';
|
|
5
5
|
import { getRepository, isAutoIncrement, } from '../remult3/RepositoryImplementation.js';
|
|
6
6
|
import { Sort } from '../sort.js';
|
|
7
7
|
import { ValueConverters } from '../valueConverters.js';
|
|
@@ -287,8 +287,8 @@ class ActualSQLEntityDataProvider {
|
|
|
287
287
|
return Number(r.rows[0].count);
|
|
288
288
|
});
|
|
289
289
|
}
|
|
290
|
-
async
|
|
291
|
-
return await
|
|
290
|
+
async groupBy(options) {
|
|
291
|
+
return await groupByImpl(options, await this.init(), this.sql.createCommand(), this.sql._getSourceSql().orderByNullsFirst, this.sql._getSourceSql().getLimitSqlSyntax);
|
|
292
292
|
}
|
|
293
293
|
async find(options) {
|
|
294
294
|
let e = await this.init();
|
|
@@ -541,15 +541,15 @@ export function getRowAfterUpdate(meta, dataProvider, data, id, operation) {
|
|
|
541
541
|
return r[0];
|
|
542
542
|
});
|
|
543
543
|
}
|
|
544
|
-
export async function
|
|
544
|
+
export async function groupByImpl(options, e, r, orderByNullFirst, limitSyntax) {
|
|
545
545
|
let select = 'select count(*) as count';
|
|
546
546
|
let groupBy = '';
|
|
547
547
|
const processResultRow = [];
|
|
548
548
|
processResultRow.push((sqlVal, theResult) => {
|
|
549
|
-
theResult[
|
|
549
|
+
theResult[GroupByCountMember] = Number(sqlVal);
|
|
550
550
|
});
|
|
551
|
-
if (options?.
|
|
552
|
-
for (const x of options?.
|
|
551
|
+
if (options?.group)
|
|
552
|
+
for (const x of options?.group) {
|
|
553
553
|
if (x.isServerExpression) {
|
|
554
554
|
}
|
|
555
555
|
else {
|
|
@@ -566,7 +566,7 @@ export async function aggregateImpl(options, e, r, orderByNullFirst, limitSyntax
|
|
|
566
566
|
theResult[x.key] = x.valueConverter.fromDb(sqlResult);
|
|
567
567
|
});
|
|
568
568
|
}
|
|
569
|
-
for (const operator of
|
|
569
|
+
for (const operator of GroupByOperators) {
|
|
570
570
|
const fields = options?.[operator];
|
|
571
571
|
if (fields)
|
|
572
572
|
for (const x of fields) {
|
package/esm/src/remult-proxy.js
CHANGED
|
@@ -112,6 +112,11 @@ export class RemultProxy {
|
|
|
112
112
|
.remultFactory()
|
|
113
113
|
.repo(...args)
|
|
114
114
|
.find(...args2),
|
|
115
|
+
groupBy: (...args2) => self
|
|
116
|
+
.remultFactory()
|
|
117
|
+
.repo(...args)
|
|
118
|
+
//@ts-ignore
|
|
119
|
+
.groupBy(...args2),
|
|
115
120
|
aggregate: (...args2) => self
|
|
116
121
|
.remultFactory()
|
|
117
122
|
.repo(...args)
|
|
@@ -3,7 +3,7 @@ import { CompoundIdField } from '../CompoundIdField.js';
|
|
|
3
3
|
import { Remult, isBackend, queryConfig } from '../context.js';
|
|
4
4
|
import { Filter } from '../filter/filter-interfaces.js';
|
|
5
5
|
import { Sort } from '../sort.js';
|
|
6
|
-
import {
|
|
6
|
+
import { GroupByForApiKey, } from './remult3.js';
|
|
7
7
|
import { assign } from '../../assign.js';
|
|
8
8
|
import { ValueConverters } from '../valueConverters.js';
|
|
9
9
|
import { findOptionsToJson } from '../data-providers/rest-data-provider.js';
|
|
@@ -129,6 +129,9 @@ export class RepositoryImplementation {
|
|
|
129
129
|
this._defaultFindOptions = _defaultFindOptions;
|
|
130
130
|
}
|
|
131
131
|
async aggregate(options) {
|
|
132
|
+
return (await this.groupBy(options))[0];
|
|
133
|
+
}
|
|
134
|
+
async groupBy(options) {
|
|
132
135
|
let findOptions = await this._buildEntityDataProviderFindOptions({
|
|
133
136
|
...options,
|
|
134
137
|
});
|
|
@@ -139,7 +142,7 @@ export class RepositoryImplementation {
|
|
|
139
142
|
return r;
|
|
140
143
|
};
|
|
141
144
|
const getFieldNotInGroupBy = (key) => {
|
|
142
|
-
if (options?.
|
|
145
|
+
if (options?.group?.includes(key))
|
|
143
146
|
throw Error(`field "${key}" cannot be used both in an aggregate and in group by`);
|
|
144
147
|
return getField(key);
|
|
145
148
|
};
|
|
@@ -147,7 +150,7 @@ export class RepositoryImplementation {
|
|
|
147
150
|
where: findOptions.where,
|
|
148
151
|
limit: findOptions.limit,
|
|
149
152
|
page: findOptions.page,
|
|
150
|
-
|
|
153
|
+
group: options?.group?.map(getField),
|
|
151
154
|
sum: options?.sum?.map(getFieldNotInGroupBy),
|
|
152
155
|
avg: options?.avg?.map(getFieldNotInGroupBy),
|
|
153
156
|
min: options?.min?.map(getFieldNotInGroupBy),
|
|
@@ -182,19 +185,17 @@ export class RepositoryImplementation {
|
|
|
182
185
|
}
|
|
183
186
|
}
|
|
184
187
|
}
|
|
185
|
-
const result = await this._edp.
|
|
186
|
-
if (!options?.groupBy)
|
|
187
|
-
return result[0];
|
|
188
|
+
const result = await this._edp.groupBy(dpOptions);
|
|
188
189
|
//@ts-expect-error an internal option
|
|
189
|
-
if (!options?.[
|
|
190
|
+
if (!options?.[GroupByForApiKey] && options.group) {
|
|
190
191
|
const loaderOptions = {
|
|
191
192
|
include: {},
|
|
192
193
|
};
|
|
193
|
-
for (const key of options.
|
|
194
|
+
for (const key of options.group) {
|
|
194
195
|
loaderOptions.include[key] = true;
|
|
195
196
|
}
|
|
196
197
|
const loader = new RelationLoader();
|
|
197
|
-
await this.populateRelationsForFields(dpOptions.
|
|
198
|
+
await this.populateRelationsForFields(dpOptions.group, loaderOptions, result, loader);
|
|
198
199
|
await loader.resolveAll();
|
|
199
200
|
}
|
|
200
201
|
return result;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export const
|
|
2
|
-
export const
|
|
3
|
-
export const
|
|
1
|
+
export const GroupByCountMember = '$count';
|
|
2
|
+
export const GroupByForApiKey = Symbol.for('GroupByForApiKey');
|
|
3
|
+
export const GroupByOperators = [
|
|
4
4
|
'sum',
|
|
5
5
|
'avg',
|
|
6
6
|
'min',
|
|
@@ -16,6 +16,7 @@ export const flags = {
|
|
|
16
16
|
/*p1 - min, max, avg, sum, countDistinct - noam come up with an api
|
|
17
17
|
p1 - handle relations
|
|
18
18
|
*/
|
|
19
|
+
//y1 - find and count / aggregate with a single call
|
|
19
20
|
/*p1 - add id and use uuid by default, but allow changes with Fields.id.defaultIdProvider NO but defaultProvider yes???
|
|
20
21
|
//p1 - replace uuid with crypto.randomUUID and allow custom fallback NO
|
|
21
22
|
//p1 - Add example for luid
|
package/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { ClassType } from './classType.js';
|
|
|
2
2
|
import type { DataProvider } from './src/data-interfaces.js';
|
|
3
3
|
export { ArrayEntityDataProvider } from './src/data-providers/array-entity-data-provider.js';
|
|
4
4
|
export type { ClassType } from './classType.js';
|
|
5
|
-
export { ValidateFieldEvent, MembersOnly, FieldsMetadata, FieldRef, IdFieldRef, FieldsRef, EntityMetadata, EntityOrderBy, EntityFilter, FindOptions, FindOptionsBase, QueryResult, QueryOptions, Repository,
|
|
5
|
+
export { ValidateFieldEvent, MembersOnly, FieldsMetadata, FieldRef, IdFieldRef, FieldsRef, EntityMetadata, EntityOrderBy, EntityFilter, FindOptions, FindOptionsBase, QueryResult, QueryOptions, Repository, GroupByOptions, FindFirstOptions, ComparisonValueFilter, ValueFilter, IdFilter, ContainsStringValueFilter, EntityRef, SortSegments, Paginator, LiveQuery, LiveQueryChangeInfo, Subscribable, RefSubscriber, RefSubscriberBase, RelationOptions, ObjectMembersOnly, MembersToInclude, RepositoryRelations, EntityIdFields, ClassFieldDecorator, ClassFieldDecoratorContextStub, //n1 consider removing in ts5
|
|
6
6
|
LifecycleEvent, FieldsRefBase, FieldsRefForEntityBase, RepositoryRelationsForEntityBase, ControllerRefForControllerBase, ControllerRefBase, ControllerRef, EntityRefForEntityBase, IdMetadata, FindFirstOptionsBase, } from './src/remult3/remult3.js';
|
|
7
7
|
export { EntityBase, ControllerBase, FieldType, getFields, ValueListFieldType, getValueList, ValueListFieldOptions, ValueListInfo, CaptionTransformer, } from './src/remult3/RepositoryImplementation.js';
|
|
8
8
|
export { Entity } from './src/remult3/entity.js';
|
|
@@ -11,7 +11,7 @@ export { Field, Fields, StringFieldOptions, Relations, } from './src/remult3/Fie
|
|
|
11
11
|
export { IdEntity } from './src/remult3/IdEntity.js';
|
|
12
12
|
export { describeClass, describeBackendMethods, describeEntity, } from './src/remult3/classDescribers.js';
|
|
13
13
|
export { EntityOptions, PreprocessFilterEvent } from './src/entity.js';
|
|
14
|
-
export { DataProvider, EntityDataProvider, EntityDataProviderFindOptions, ErrorInfo, RestDataProviderHttpProvider, } from './src/data-interfaces.js';
|
|
14
|
+
export { DataProvider, EntityDataProvider, EntityDataProviderGroupByOptions, EntityDataProviderFindOptions, ErrorInfo, RestDataProviderHttpProvider, } from './src/data-interfaces.js';
|
|
15
15
|
export { SqlCommand, SqlCommandWithParameters, SqlImplementation, SqlResult, } from './src/sql-command.js';
|
|
16
16
|
export { FieldMetadata, FieldOptions, FieldValidator, ValueConverter, ValueListItem, // reconsider, maybe it should go to remult angular as the abstraction ?
|
|
17
17
|
ValueOrExpression, } from './src/column-interfaces.js';
|
package/package.json
CHANGED
package/remult-knex/index.js
CHANGED
|
@@ -202,13 +202,13 @@ var KnexEntityDataProvider = /** @class */ (function () {
|
|
|
202
202
|
this.rawSqlWrapIdentifier = rawSqlWrapIdentifier;
|
|
203
203
|
this.dp = dp;
|
|
204
204
|
}
|
|
205
|
-
KnexEntityDataProvider.prototype.
|
|
205
|
+
KnexEntityDataProvider.prototype.groupBy = function (options) {
|
|
206
206
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
207
207
|
var _a, _b;
|
|
208
208
|
return tslib_1.__generator(this, function (_c) {
|
|
209
209
|
switch (_c.label) {
|
|
210
210
|
case 0:
|
|
211
|
-
_a = sql_database_js_1.
|
|
211
|
+
_a = sql_database_js_1.groupByImpl;
|
|
212
212
|
_b = [options];
|
|
213
213
|
return [4 /*yield*/, this.init()];
|
|
214
214
|
case 1: return [2 /*return*/, _a.apply(void 0, _b.concat([_c.sent(), this.dp.createCommand(),
|
package/remult-mongo.js
CHANGED
|
@@ -169,9 +169,9 @@ var MongoEntityDataProvider = /** @class */ (function () {
|
|
|
169
169
|
});
|
|
170
170
|
});
|
|
171
171
|
};
|
|
172
|
-
MongoEntityDataProvider.prototype.
|
|
172
|
+
MongoEntityDataProvider.prototype.groupBy = function (options) {
|
|
173
173
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
174
|
-
var _a, collection, e, x, pipeLine, where, processResultRow, $group, $addFields, _loop_1, _b, _c, element, _loop_2,
|
|
174
|
+
var _a, collection, e, x, pipeLine, where, processResultRow, $group, $addFields, _loop_1, _b, _c, element, _loop_2, GroupByOperators_1, GroupByOperators_1_1, operator, sort, _d, _e, x_1, direction, r, r2;
|
|
175
175
|
var e_3, _f, e_4, _g, e_5, _h;
|
|
176
176
|
return tslib_1.__generator(this, function (_j) {
|
|
177
177
|
switch (_j.label) {
|
|
@@ -193,12 +193,12 @@ var MongoEntityDataProvider = /** @class */ (function () {
|
|
|
193
193
|
__count: { $sum: 1 },
|
|
194
194
|
};
|
|
195
195
|
processResultRow.push(function (mongoRow, resultRow) {
|
|
196
|
-
resultRow[remult3_js_1.
|
|
196
|
+
resultRow[remult3_js_1.GroupByCountMember] = mongoRow.__count;
|
|
197
197
|
});
|
|
198
198
|
pipeLine.push({ $group: $group });
|
|
199
199
|
$addFields = {};
|
|
200
200
|
pipeLine.push({ $addFields: $addFields });
|
|
201
|
-
if (options === null || options === void 0 ? void 0 : options.
|
|
201
|
+
if (options === null || options === void 0 ? void 0 : options.group) {
|
|
202
202
|
$group._id = {};
|
|
203
203
|
_loop_1 = function (element) {
|
|
204
204
|
var name_1 = e.$dbNameOf(element);
|
|
@@ -208,7 +208,7 @@ var MongoEntityDataProvider = /** @class */ (function () {
|
|
|
208
208
|
});
|
|
209
209
|
};
|
|
210
210
|
try {
|
|
211
|
-
for (_b = tslib_1.__values(options.
|
|
211
|
+
for (_b = tslib_1.__values(options.group), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
212
212
|
element = _c.value;
|
|
213
213
|
_loop_1(element);
|
|
214
214
|
}
|
|
@@ -257,15 +257,15 @@ var MongoEntityDataProvider = /** @class */ (function () {
|
|
|
257
257
|
}
|
|
258
258
|
};
|
|
259
259
|
try {
|
|
260
|
-
for (
|
|
261
|
-
operator =
|
|
260
|
+
for (GroupByOperators_1 = tslib_1.__values(remult3_js_1.GroupByOperators), GroupByOperators_1_1 = GroupByOperators_1.next(); !GroupByOperators_1_1.done; GroupByOperators_1_1 = GroupByOperators_1.next()) {
|
|
261
|
+
operator = GroupByOperators_1_1.value;
|
|
262
262
|
_loop_2(operator);
|
|
263
263
|
}
|
|
264
264
|
}
|
|
265
265
|
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
266
266
|
finally {
|
|
267
267
|
try {
|
|
268
|
-
if (
|
|
268
|
+
if (GroupByOperators_1_1 && !GroupByOperators_1_1.done && (_g = GroupByOperators_1.return)) _g.call(GroupByOperators_1);
|
|
269
269
|
}
|
|
270
270
|
finally { if (e_4) throw e_4.error; }
|
|
271
271
|
}
|
package/src/data-api.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export declare class DataApi<T = unknown> {
|
|
|
12
12
|
get(response: DataApiResponse, id: any): Promise<void>;
|
|
13
13
|
count(response: DataApiResponse, request: DataApiRequest, body?: any): Promise<void>;
|
|
14
14
|
deleteMany(response: DataApiResponse, request: DataApiRequest, body?: any): Promise<void>;
|
|
15
|
-
|
|
15
|
+
groupBy(response: DataApiResponse, request: DataApiRequest, body: any): Promise<void>;
|
|
16
16
|
getArrayImpl(response: DataApiResponse, request: DataApiRequest, body: any): Promise<{
|
|
17
17
|
r: any[];
|
|
18
18
|
findOptions: FindOptions<T>;
|
package/src/data-api.js
CHANGED
|
@@ -23,8 +23,8 @@ var DataApi = /** @class */ (function () {
|
|
|
23
23
|
case 'get':
|
|
24
24
|
case 'count':
|
|
25
25
|
return this.count(res, req, undefined);
|
|
26
|
-
case '
|
|
27
|
-
return this.
|
|
26
|
+
case 'groupBy':
|
|
27
|
+
return this.groupBy(res, req, undefined);
|
|
28
28
|
}
|
|
29
29
|
return this.getArray(res, req, undefined);
|
|
30
30
|
};
|
|
@@ -51,7 +51,7 @@ var DataApi = /** @class */ (function () {
|
|
|
51
51
|
switch (_a) {
|
|
52
52
|
case 'get': return [3 /*break*/, 1];
|
|
53
53
|
case 'count': return [3 /*break*/, 2];
|
|
54
|
-
case '
|
|
54
|
+
case 'groupBy': return [3 /*break*/, 3];
|
|
55
55
|
case 'deleteMany': return [3 /*break*/, 4];
|
|
56
56
|
case 'updateMany': return [3 /*break*/, 5];
|
|
57
57
|
case 'endLiveQuery': return [3 /*break*/, 6];
|
|
@@ -63,7 +63,7 @@ var DataApi = /** @class */ (function () {
|
|
|
63
63
|
case 2:
|
|
64
64
|
validateWhereInBody();
|
|
65
65
|
return [2 /*return*/, this.count(res, req, body)];
|
|
66
|
-
case 3: return [2 /*return*/, this.
|
|
66
|
+
case 3: return [2 /*return*/, this.groupBy(res, req, body)];
|
|
67
67
|
case 4:
|
|
68
68
|
validateWhereInBody();
|
|
69
69
|
return [2 /*return*/, this.deleteMany(res, req, body)];
|
|
@@ -204,9 +204,9 @@ var DataApi = /** @class */ (function () {
|
|
|
204
204
|
});
|
|
205
205
|
});
|
|
206
206
|
};
|
|
207
|
-
DataApi.prototype.
|
|
207
|
+
DataApi.prototype.groupBy = function (response, request, body) {
|
|
208
208
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
209
|
-
var findOptions, orderBy, _a, _b, element, direction,
|
|
209
|
+
var findOptions, orderBy, _a, _b, element, direction, group, result;
|
|
210
210
|
var e_2, _c, _d, _e;
|
|
211
211
|
var _this = this;
|
|
212
212
|
var _f, _g, _h, _j, _k, _l;
|
|
@@ -226,7 +226,7 @@ var DataApi = /** @class */ (function () {
|
|
|
226
226
|
orderBy[element.field] = direction;
|
|
227
227
|
break;
|
|
228
228
|
case 'count':
|
|
229
|
-
orderBy[remult3_js_1.
|
|
229
|
+
orderBy[remult3_js_1.GroupByCountMember] = direction;
|
|
230
230
|
break;
|
|
231
231
|
default:
|
|
232
232
|
orderBy[element.field] = tslib_1.__assign(tslib_1.__assign({}, orderBy[element.field]), (_d = {}, _d[element.operation] = direction, _d));
|
|
@@ -242,17 +242,17 @@ var DataApi = /** @class */ (function () {
|
|
|
242
242
|
finally { if (e_2) throw e_2.error; }
|
|
243
243
|
}
|
|
244
244
|
}
|
|
245
|
-
|
|
245
|
+
group = (_f = body === null || body === void 0 ? void 0 : body.groupBy) === null || _f === void 0 ? void 0 : _f.filter(function (x) {
|
|
246
246
|
return _this.repository.fields.find(x).includedInApi();
|
|
247
247
|
});
|
|
248
|
-
return [4 /*yield*/, this.repository.
|
|
248
|
+
return [4 /*yield*/, this.repository.groupBy((_e = {
|
|
249
249
|
where: findOptions.where,
|
|
250
250
|
limit: findOptions.limit,
|
|
251
251
|
page: findOptions.page
|
|
252
252
|
},
|
|
253
253
|
//@ts-expect-error internal key
|
|
254
|
-
_e[remult3_js_1.
|
|
255
|
-
_e.
|
|
254
|
+
_e[remult3_js_1.GroupByForApiKey] = true,
|
|
255
|
+
_e.group = group,
|
|
256
256
|
_e.sum = (_g = body === null || body === void 0 ? void 0 : body.sum) === null || _g === void 0 ? void 0 : _g.filter(function (x) {
|
|
257
257
|
return _this.repository.fields.find(x).includedInApi();
|
|
258
258
|
}),
|
|
@@ -272,27 +272,23 @@ var DataApi = /** @class */ (function () {
|
|
|
272
272
|
_e))];
|
|
273
273
|
case 2:
|
|
274
274
|
result = _m.sent();
|
|
275
|
-
if (
|
|
276
|
-
result = [result];
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
275
|
+
if (group)
|
|
279
276
|
result.forEach(function (x) {
|
|
280
277
|
var e_3, _a;
|
|
281
278
|
try {
|
|
282
|
-
for (var
|
|
283
|
-
var f =
|
|
279
|
+
for (var group_1 = tslib_1.__values(group), group_1_1 = group_1.next(); !group_1_1.done; group_1_1 = group_1.next()) {
|
|
280
|
+
var f = group_1_1.value;
|
|
284
281
|
x[f] = _this.repository.fields.find(f).valueConverter.toJson(x[f]);
|
|
285
282
|
}
|
|
286
283
|
}
|
|
287
284
|
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
288
285
|
finally {
|
|
289
286
|
try {
|
|
290
|
-
if (
|
|
287
|
+
if (group_1_1 && !group_1_1.done && (_a = group_1.return)) _a.call(group_1);
|
|
291
288
|
}
|
|
292
289
|
finally { if (e_3) throw e_3.error; }
|
|
293
290
|
}
|
|
294
291
|
});
|
|
295
|
-
}
|
|
296
292
|
response.success(result);
|
|
297
293
|
return [2 /*return*/];
|
|
298
294
|
}
|
package/src/data-interfaces.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { FieldMetadata } from '../index.js';
|
|
2
2
|
import type { Filter } from './filter/filter-interfaces.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { GroupByOperators, EntityMetadata, MembersOnly } from './remult3/remult3.js';
|
|
4
4
|
import { Sort } from './sort.js';
|
|
5
5
|
export interface DataProvider {
|
|
6
6
|
getEntityDataProvider(entity: EntityMetadata): EntityDataProvider;
|
|
@@ -11,8 +11,8 @@ export interface DataProvider {
|
|
|
11
11
|
export interface Storage {
|
|
12
12
|
ensureSchema(): Promise<void>;
|
|
13
13
|
}
|
|
14
|
-
export interface
|
|
15
|
-
|
|
14
|
+
export interface EntityDataProviderGroupByOptions extends Pick<EntityDataProviderFindOptions, 'where' | 'limit' | 'page'> {
|
|
15
|
+
group?: FieldMetadata[];
|
|
16
16
|
sum?: FieldMetadata[];
|
|
17
17
|
avg?: FieldMetadata[];
|
|
18
18
|
min?: FieldMetadata[];
|
|
@@ -21,13 +21,13 @@ export interface EntityDataProviderAggregateOptions extends Pick<EntityDataProvi
|
|
|
21
21
|
orderBy?: {
|
|
22
22
|
field?: FieldMetadata;
|
|
23
23
|
isDescending?: boolean;
|
|
24
|
-
operation?: (typeof
|
|
24
|
+
operation?: (typeof GroupByOperators)[number] | 'count';
|
|
25
25
|
}[];
|
|
26
26
|
}
|
|
27
27
|
export interface EntityDataProvider {
|
|
28
28
|
count(where: Filter): Promise<number>;
|
|
29
29
|
find(options?: EntityDataProviderFindOptions): Promise<Array<any>>;
|
|
30
|
-
|
|
30
|
+
groupBy(options?: EntityDataProviderGroupByOptions): Promise<any[]>;
|
|
31
31
|
update(id: any, data: any): Promise<any>;
|
|
32
32
|
delete(id: any): Promise<void>;
|
|
33
33
|
insert(data: any): Promise<any>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EntityDataProvider,
|
|
1
|
+
import type { EntityDataProvider, EntityDataProviderGroupByOptions, EntityDataProviderFindOptions } from '../data-interfaces.js';
|
|
2
2
|
import { Filter } from '../filter/filter-interfaces.js';
|
|
3
3
|
import { type EntityFilter, type EntityMetadata } from '../remult3/remult3.js';
|
|
4
4
|
export declare class ArrayEntityDataProvider implements EntityDataProvider {
|
|
@@ -6,7 +6,7 @@ export declare class ArrayEntityDataProvider implements EntityDataProvider {
|
|
|
6
6
|
private rows;
|
|
7
7
|
static rawFilter(filter: CustomArrayFilter): EntityFilter<any>;
|
|
8
8
|
constructor(entity: EntityMetadata, rows: () => any[]);
|
|
9
|
-
|
|
9
|
+
groupBy(options?: EntityDataProviderGroupByOptions): Promise<any[]>;
|
|
10
10
|
count(where?: Filter): Promise<number>;
|
|
11
11
|
find(options?: EntityDataProviderFindOptions): Promise<any[]>;
|
|
12
12
|
update(id: any, data: any): Promise<any>;
|
|
@@ -20,7 +20,7 @@ var ArrayEntityDataProvider = /** @class */ (function () {
|
|
|
20
20
|
},
|
|
21
21
|
_a;
|
|
22
22
|
};
|
|
23
|
-
ArrayEntityDataProvider.prototype.
|
|
23
|
+
ArrayEntityDataProvider.prototype.groupBy = function (options) {
|
|
24
24
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
25
25
|
function finishGroup() {
|
|
26
26
|
var e_8, _a;
|
|
@@ -38,20 +38,20 @@ var ArrayEntityDataProvider = /** @class */ (function () {
|
|
|
38
38
|
}
|
|
39
39
|
finally { if (e_8) throw e_8.error; }
|
|
40
40
|
}
|
|
41
|
-
r[remult3_js_1.
|
|
41
|
+
r[remult3_js_1.GroupByCountMember] = count;
|
|
42
42
|
result.push(r);
|
|
43
43
|
first = true;
|
|
44
44
|
count = 0;
|
|
45
45
|
}
|
|
46
|
-
var sort, _a, _b, field, rows, result, group, first, count, aggregates, operatorImpl,
|
|
46
|
+
var sort, _a, _b, field, rows, result, group, first, count, aggregates, operatorImpl, GroupByOperators_1, GroupByOperators_1_1, operator, _c, _d, element, rows_1, rows_1_1, row, _e, _f, field, _g, _h, field, aggregates_1, aggregates_1_1, a;
|
|
47
47
|
var e_1, _j, e_2, _k, e_3, _l, e_4, _m, e_5, _o, e_6, _p, e_7, _q;
|
|
48
48
|
return tslib_1.__generator(this, function (_r) {
|
|
49
49
|
switch (_r.label) {
|
|
50
50
|
case 0:
|
|
51
51
|
sort = new sort_js_1.Sort();
|
|
52
|
-
if (options === null || options === void 0 ? void 0 : options.
|
|
52
|
+
if (options === null || options === void 0 ? void 0 : options.group)
|
|
53
53
|
try {
|
|
54
|
-
for (_a = tslib_1.__values(options === null || options === void 0 ? void 0 : options.
|
|
54
|
+
for (_a = tslib_1.__values(options === null || options === void 0 ? void 0 : options.group), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
55
55
|
field = _b.value;
|
|
56
56
|
sort.Segments.push({ field: field });
|
|
57
57
|
}
|
|
@@ -152,8 +152,8 @@ var ArrayEntityDataProvider = /** @class */ (function () {
|
|
|
152
152
|
},
|
|
153
153
|
};
|
|
154
154
|
try {
|
|
155
|
-
for (
|
|
156
|
-
operator =
|
|
155
|
+
for (GroupByOperators_1 = tslib_1.__values(remult3_js_1.GroupByOperators), GroupByOperators_1_1 = GroupByOperators_1.next(); !GroupByOperators_1_1.done; GroupByOperators_1_1 = GroupByOperators_1.next()) {
|
|
156
|
+
operator = GroupByOperators_1_1.value;
|
|
157
157
|
if (options === null || options === void 0 ? void 0 : options[operator]) {
|
|
158
158
|
try {
|
|
159
159
|
for (_c = (e_3 = void 0, tslib_1.__values(options[operator])), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
@@ -174,17 +174,17 @@ var ArrayEntityDataProvider = /** @class */ (function () {
|
|
|
174
174
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
175
175
|
finally {
|
|
176
176
|
try {
|
|
177
|
-
if (
|
|
177
|
+
if (GroupByOperators_1_1 && !GroupByOperators_1_1.done && (_k = GroupByOperators_1.return)) _k.call(GroupByOperators_1);
|
|
178
178
|
}
|
|
179
179
|
finally { if (e_2) throw e_2.error; }
|
|
180
180
|
}
|
|
181
181
|
try {
|
|
182
182
|
for (rows_1 = tslib_1.__values(rows), rows_1_1 = rows_1.next(); !rows_1_1.done; rows_1_1 = rows_1.next()) {
|
|
183
183
|
row = rows_1_1.value;
|
|
184
|
-
if (options === null || options === void 0 ? void 0 : options.
|
|
184
|
+
if (options === null || options === void 0 ? void 0 : options.group) {
|
|
185
185
|
if (!first) {
|
|
186
186
|
try {
|
|
187
|
-
for (_e = (e_5 = void 0, tslib_1.__values(options === null || options === void 0 ? void 0 : options.
|
|
187
|
+
for (_e = (e_5 = void 0, tslib_1.__values(options === null || options === void 0 ? void 0 : options.group)), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
188
188
|
field = _f.value;
|
|
189
189
|
if (group[field.key] != row[field.key]) {
|
|
190
190
|
finishGroup();
|
|
@@ -202,7 +202,7 @@ var ArrayEntityDataProvider = /** @class */ (function () {
|
|
|
202
202
|
}
|
|
203
203
|
if (first) {
|
|
204
204
|
try {
|
|
205
|
-
for (_g = (e_6 = void 0, tslib_1.__values(options === null || options === void 0 ? void 0 : options.
|
|
205
|
+
for (_g = (e_6 = void 0, tslib_1.__values(options === null || options === void 0 ? void 0 : options.group)), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
206
206
|
field = _h.value;
|
|
207
207
|
group[field.key] = row[field.key];
|
|
208
208
|
}
|
|
@@ -247,12 +247,12 @@ var ArrayEntityDataProvider = /** @class */ (function () {
|
|
|
247
247
|
var _loop_1 = function (x) {
|
|
248
248
|
var getValue_1 = function (row) {
|
|
249
249
|
if (!x.field && x.operation == 'count') {
|
|
250
|
-
return row[remult3_js_1.
|
|
250
|
+
return row[remult3_js_1.GroupByCountMember];
|
|
251
251
|
}
|
|
252
252
|
else {
|
|
253
253
|
switch (x.operation) {
|
|
254
254
|
case 'count':
|
|
255
|
-
return row[remult3_js_1.
|
|
255
|
+
return row[remult3_js_1.GroupByCountMember];
|
|
256
256
|
case undefined:
|
|
257
257
|
return row[x.field.key];
|
|
258
258
|
default:
|
|
@@ -34,10 +34,10 @@ var JsonEntityDataProvider = /** @class */ (function () {
|
|
|
34
34
|
this.formatted = formatted;
|
|
35
35
|
this.p = Promise.resolve();
|
|
36
36
|
}
|
|
37
|
-
JsonEntityDataProvider.prototype.
|
|
37
|
+
JsonEntityDataProvider.prototype.groupBy = function (options) {
|
|
38
38
|
var _this = this;
|
|
39
39
|
return (this.p = this.p.then(function () {
|
|
40
|
-
return _this.loadEntityData(function (dp, save) { return dp.
|
|
40
|
+
return _this.loadEntityData(function (dp, save) { return dp.groupBy(options); });
|
|
41
41
|
}));
|
|
42
42
|
};
|
|
43
43
|
JsonEntityDataProvider.prototype.loadEntityData = function (what) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DataProvider, EntityDataProvider,
|
|
1
|
+
import type { DataProvider, EntityDataProvider, EntityDataProviderGroupByOptions, EntityDataProviderFindOptions, ProxyEntityDataProvider, RestDataProviderHttpProvider } from '../data-interfaces.js';
|
|
2
2
|
import { UrlBuilder } from '../../urlBuilder.js';
|
|
3
3
|
import type { ApiClient } from '../context.js';
|
|
4
4
|
import { Filter } from '../filter/filter-interfaces.js';
|
|
@@ -15,7 +15,7 @@ export declare class RestEntityDataProvider implements EntityDataProvider, Proxy
|
|
|
15
15
|
private http;
|
|
16
16
|
private entity;
|
|
17
17
|
constructor(url: () => string, http: () => RestDataProviderHttpProvider, entity: EntityMetadata);
|
|
18
|
-
|
|
18
|
+
groupBy(options?: EntityDataProviderGroupByOptions): Promise<any[]>;
|
|
19
19
|
translateFromJson(row: any): any;
|
|
20
20
|
translateToJson(row: any): any;
|
|
21
21
|
count(where: Filter): Promise<number>;
|
|
@@ -117,7 +117,7 @@ var RestEntityDataProvider = /** @class */ (function () {
|
|
|
117
117
|
this.http = http;
|
|
118
118
|
this.entity = entity;
|
|
119
119
|
}
|
|
120
|
-
RestEntityDataProvider.prototype.
|
|
120
|
+
RestEntityDataProvider.prototype.groupBy = function (options) {
|
|
121
121
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
122
122
|
var run, body, result;
|
|
123
123
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
@@ -130,7 +130,7 @@ var RestEntityDataProvider = /** @class */ (function () {
|
|
|
130
130
|
page: options === null || options === void 0 ? void 0 : options.page,
|
|
131
131
|
}).run;
|
|
132
132
|
body = {
|
|
133
|
-
groupBy: (_a = options === null || options === void 0 ? void 0 : options.
|
|
133
|
+
groupBy: (_a = options === null || options === void 0 ? void 0 : options.group) === null || _a === void 0 ? void 0 : _a.map(function (x) { return x.key; }),
|
|
134
134
|
sum: (_b = options === null || options === void 0 ? void 0 : options.sum) === null || _b === void 0 ? void 0 : _b.map(function (x) { return x.key; }),
|
|
135
135
|
avg: (_c = options === null || options === void 0 ? void 0 : options.avg) === null || _c === void 0 ? void 0 : _c.map(function (x) { return x.key; }),
|
|
136
136
|
min: (_d = options === null || options === void 0 ? void 0 : options.min) === null || _d === void 0 ? void 0 : _d.map(function (x) { return x.key; }),
|
|
@@ -138,14 +138,14 @@ var RestEntityDataProvider = /** @class */ (function () {
|
|
|
138
138
|
distinctCount: (_f = options === null || options === void 0 ? void 0 : options.distinctCount) === null || _f === void 0 ? void 0 : _f.map(function (x) { return x.key; }),
|
|
139
139
|
orderBy: (_g = options === null || options === void 0 ? void 0 : options.orderBy) === null || _g === void 0 ? void 0 : _g.map(function (x) { var _a; return (tslib_1.__assign(tslib_1.__assign({}, x), { field: (_a = x.field) === null || _a === void 0 ? void 0 : _a.key })); }),
|
|
140
140
|
};
|
|
141
|
-
return [4 /*yield*/, run('
|
|
141
|
+
return [4 /*yield*/, run('groupBy', Object.keys(body).length > 0 ? body : undefined)];
|
|
142
142
|
case 1:
|
|
143
143
|
result = _h.sent();
|
|
144
|
-
if (options === null || options === void 0 ? void 0 : options.
|
|
144
|
+
if (options === null || options === void 0 ? void 0 : options.group)
|
|
145
145
|
result.forEach(function (row) {
|
|
146
146
|
var e_2, _a;
|
|
147
147
|
try {
|
|
148
|
-
for (var _b = tslib_1.__values(options.
|
|
148
|
+
for (var _b = tslib_1.__values(options.group), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
149
149
|
var g = _c.value;
|
|
150
150
|
row[g.key] = g.valueConverter.fromJson(row[g.key]);
|
|
151
151
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DataProvider, EntityDataProvider,
|
|
1
|
+
import type { DataProvider, EntityDataProvider, EntityDataProviderGroupByOptions } from '../data-interfaces.js';
|
|
2
2
|
import type { HasWrapIdentifier, SqlCommand, SqlCommandFactory, SqlCommandWithParameters, SqlImplementation, SqlResult } from '../sql-command.js';
|
|
3
3
|
import type { CustomSqlFilterBuilderFunction, EntityDbNamesBase } from '../filter/filter-consumer-bridge-to-sql-request.js';
|
|
4
4
|
import { type EntityFilter, type EntityMetadata } from '../remult3/remult3.js';
|
|
@@ -95,4 +95,4 @@ export declare class SqlDatabase implements DataProvider, HasWrapIdentifier, Can
|
|
|
95
95
|
end: () => Promise<void>;
|
|
96
96
|
}
|
|
97
97
|
export declare function getRowAfterUpdate<entityType>(meta: EntityMetadata<entityType>, dataProvider: EntityDataProvider, data: any, id: any, operation: string): any;
|
|
98
|
-
export declare function
|
|
98
|
+
export declare function groupByImpl(options: EntityDataProviderGroupByOptions | undefined, e: EntityDbNamesBase, r: SqlCommand, orderByNullFirst: boolean | undefined, limitSyntax: (limit: number, offset: number) => string): Promise<any[]>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SqlDatabase = void 0;
|
|
4
4
|
exports.getRowAfterUpdate = getRowAfterUpdate;
|
|
5
|
-
exports.
|
|
5
|
+
exports.groupByImpl = groupByImpl;
|
|
6
6
|
var tslib_1 = require("tslib");
|
|
7
7
|
var filter_consumer_bridge_to_sql_request_js_1 = require("../filter/filter-consumer-bridge-to-sql-request.js");
|
|
8
8
|
var filter_interfaces_js_1 = require("../filter/filter-interfaces.js");
|
|
@@ -394,13 +394,13 @@ var ActualSQLEntityDataProvider = /** @class */ (function () {
|
|
|
394
394
|
});
|
|
395
395
|
});
|
|
396
396
|
};
|
|
397
|
-
ActualSQLEntityDataProvider.prototype.
|
|
397
|
+
ActualSQLEntityDataProvider.prototype.groupBy = function (options) {
|
|
398
398
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
399
399
|
var _a, _b;
|
|
400
400
|
return tslib_1.__generator(this, function (_c) {
|
|
401
401
|
switch (_c.label) {
|
|
402
402
|
case 0:
|
|
403
|
-
_a =
|
|
403
|
+
_a = groupByImpl;
|
|
404
404
|
_b = [options];
|
|
405
405
|
return [4 /*yield*/, this.init()];
|
|
406
406
|
case 1: return [4 /*yield*/, _a.apply(void 0, _b.concat([_c.sent(), this.sql.createCommand(),
|
|
@@ -825,14 +825,14 @@ function getRowAfterUpdate(meta, dataProvider, data, id, operation) {
|
|
|
825
825
|
return r[0];
|
|
826
826
|
});
|
|
827
827
|
}
|
|
828
|
-
function
|
|
828
|
+
function groupByImpl(options, e, r, orderByNullFirst, limitSyntax) {
|
|
829
829
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
830
830
|
function aggregateSqlSyntax(operator, dbName) {
|
|
831
831
|
return operator === 'distinctCount'
|
|
832
832
|
? "count (distinct ".concat(dbName, ")")
|
|
833
833
|
: "".concat(operator, "( ").concat(dbName, " )");
|
|
834
834
|
}
|
|
835
|
-
var select, groupBy, processResultRow, _loop_2, _a, _b, x, _loop_3,
|
|
835
|
+
var select, groupBy, processResultRow, _loop_2, _a, _b, x, _loop_3, GroupByOperators_1, GroupByOperators_1_1, operator, e_8_1, where, _c, orderBy, _d, _e, x, field, _f, e_9_1, page, result;
|
|
836
836
|
var e_10, _g, e_8, _h, e_9, _j;
|
|
837
837
|
return tslib_1.__generator(this, function (_k) {
|
|
838
838
|
switch (_k.label) {
|
|
@@ -841,9 +841,9 @@ function aggregateImpl(options, e, r, orderByNullFirst, limitSyntax) {
|
|
|
841
841
|
groupBy = '';
|
|
842
842
|
processResultRow = [];
|
|
843
843
|
processResultRow.push(function (sqlVal, theResult) {
|
|
844
|
-
theResult[remult3_js_1.
|
|
844
|
+
theResult[remult3_js_1.GroupByCountMember] = Number(sqlVal);
|
|
845
845
|
});
|
|
846
|
-
if (options === null || options === void 0 ? void 0 : options.
|
|
846
|
+
if (options === null || options === void 0 ? void 0 : options.group) {
|
|
847
847
|
_loop_2 = function (x) {
|
|
848
848
|
if (x.isServerExpression) {
|
|
849
849
|
}
|
|
@@ -862,7 +862,7 @@ function aggregateImpl(options, e, r, orderByNullFirst, limitSyntax) {
|
|
|
862
862
|
});
|
|
863
863
|
};
|
|
864
864
|
try {
|
|
865
|
-
for (_a = tslib_1.__values(options === null || options === void 0 ? void 0 : options.
|
|
865
|
+
for (_a = tslib_1.__values(options === null || options === void 0 ? void 0 : options.group), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
866
866
|
x = _b.value;
|
|
867
867
|
_loop_2(x);
|
|
868
868
|
}
|
|
@@ -937,17 +937,17 @@ function aggregateImpl(options, e, r, orderByNullFirst, limitSyntax) {
|
|
|
937
937
|
_k.label = 1;
|
|
938
938
|
case 1:
|
|
939
939
|
_k.trys.push([1, 6, 7, 8]);
|
|
940
|
-
|
|
940
|
+
GroupByOperators_1 = tslib_1.__values(remult3_js_1.GroupByOperators), GroupByOperators_1_1 = GroupByOperators_1.next();
|
|
941
941
|
_k.label = 2;
|
|
942
942
|
case 2:
|
|
943
|
-
if (!!
|
|
944
|
-
operator =
|
|
943
|
+
if (!!GroupByOperators_1_1.done) return [3 /*break*/, 5];
|
|
944
|
+
operator = GroupByOperators_1_1.value;
|
|
945
945
|
return [5 /*yield**/, _loop_3(operator)];
|
|
946
946
|
case 3:
|
|
947
947
|
_k.sent();
|
|
948
948
|
_k.label = 4;
|
|
949
949
|
case 4:
|
|
950
|
-
|
|
950
|
+
GroupByOperators_1_1 = GroupByOperators_1.next();
|
|
951
951
|
return [3 /*break*/, 2];
|
|
952
952
|
case 5: return [3 /*break*/, 8];
|
|
953
953
|
case 6:
|
|
@@ -956,7 +956,7 @@ function aggregateImpl(options, e, r, orderByNullFirst, limitSyntax) {
|
|
|
956
956
|
return [3 /*break*/, 8];
|
|
957
957
|
case 7:
|
|
958
958
|
try {
|
|
959
|
-
if (
|
|
959
|
+
if (GroupByOperators_1_1 && !GroupByOperators_1_1.done && (_h = GroupByOperators_1.return)) _h.call(GroupByOperators_1);
|
|
960
960
|
}
|
|
961
961
|
finally { if (e_8) throw e_8.error; }
|
|
962
962
|
return [7 /*endfinally*/];
|
package/src/remult-proxy.js
CHANGED
|
@@ -121,6 +121,17 @@ var RemultProxy = /** @class */ (function () {
|
|
|
121
121
|
.repo.apply(_b, tslib_1.__spreadArray([], tslib_1.__read(args), false)))
|
|
122
122
|
.find.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(args2), false));
|
|
123
123
|
},
|
|
124
|
+
_a.groupBy = function () {
|
|
125
|
+
var _a, _b;
|
|
126
|
+
var args2 = [];
|
|
127
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
128
|
+
args2[_i] = arguments[_i];
|
|
129
|
+
}
|
|
130
|
+
return (_a = (_b = self
|
|
131
|
+
.remultFactory())
|
|
132
|
+
.repo.apply(_b, tslib_1.__spreadArray([], tslib_1.__read(args), false)))
|
|
133
|
+
.groupBy.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(args2), false));
|
|
134
|
+
},
|
|
124
135
|
_a.aggregate = function () {
|
|
125
136
|
var _a, _b;
|
|
126
137
|
var args2 = [];
|
|
@@ -4,7 +4,7 @@ import type { FieldMetadata, FieldOptions, ValueConverter, ValueListItem } from
|
|
|
4
4
|
import { Remult } from '../context.js';
|
|
5
5
|
import type { EntityOptions } from '../entity.js';
|
|
6
6
|
import { Filter } from '../filter/filter-interfaces.js';
|
|
7
|
-
import { type
|
|
7
|
+
import { type GroupByOptions, type GroupByResult, type ControllerRef, type ControllerRefForControllerBase, type EntityFilter, type EntityMetadata, type EntityOrderBy, type EntityRef, type EntityRefForEntityBase, type FieldRef, type FieldsMetadata, type FieldsRef, type FieldsRefForEntityBase, type FindFirstOptions, type FindFirstOptionsBase, type FindOptions, type IdMetadata, type LiveQuery, type LoadOptions, type MembersOnly, type NumericKeys, type QueryOptions, type QueryResult, type Repository, type RepositoryRelations, type Subscribable, type idType } from './remult3.js';
|
|
8
8
|
import type { RefSubscriber } from './remult3.js';
|
|
9
9
|
import type { entityEventListener } from '../__EntityValueProvider.js';
|
|
10
10
|
import type { DataProvider, EntityDataProvider, EntityDataProviderFindOptions, ErrorInfo } from '../data-interfaces.js';
|
|
@@ -32,7 +32,8 @@ export declare class RepositoryImplementation<entityType> implements Repository<
|
|
|
32
32
|
private __edp?;
|
|
33
33
|
private get _edp();
|
|
34
34
|
constructor(_entity: ClassType<entityType>, _remult: Remult, _dataProvider: DataProvider, _info: EntityFullInfo<entityType>, _defaultFindOptions?: FindOptions<entityType> | undefined);
|
|
35
|
-
aggregate<
|
|
35
|
+
aggregate<sumFields extends NumericKeys<entityType>[] | undefined = undefined, averageFields extends NumericKeys<entityType>[] | undefined = undefined, minFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined, maxFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined, distinctCountFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined>(options: Omit<GroupByOptions<entityType, never, sumFields extends undefined ? never : sumFields, averageFields extends undefined ? never : averageFields, minFields extends undefined ? never : minFields, maxFields extends undefined ? never : maxFields, distinctCountFields extends undefined ? never : distinctCountFields>, 'limit' | 'page' | 'orderBy'>): Promise<GroupByResult<entityType, never, sumFields extends undefined ? never : sumFields, averageFields extends undefined ? never : averageFields, minFields extends undefined ? never : minFields, maxFields extends undefined ? never : maxFields, distinctCountFields extends undefined ? never : distinctCountFields>>;
|
|
36
|
+
groupBy<groupByFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined, sumFields extends NumericKeys<entityType>[] | undefined = undefined, averageFields extends NumericKeys<entityType>[] | undefined = undefined, minFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined, maxFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined, distinctCountFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined>(options: GroupByOptions<entityType, groupByFields extends undefined ? never : groupByFields, sumFields extends undefined ? never : sumFields, averageFields extends undefined ? never : averageFields, minFields extends undefined ? never : minFields, maxFields extends undefined ? never : maxFields, distinctCountFields extends undefined ? never : distinctCountFields>): Promise<GroupByResult<entityType, groupByFields extends undefined ? never : groupByFields, sumFields extends undefined ? never : sumFields, averageFields extends undefined ? never : averageFields, minFields extends undefined ? never : minFields, maxFields extends undefined ? never : maxFields, distinctCountFields extends undefined ? never : distinctCountFields>[]>;
|
|
36
37
|
_idCache: Map<any, any>;
|
|
37
38
|
_getCachedById(id: any, doNotLoadIfNotFound: boolean): entityType | undefined;
|
|
38
39
|
_getCachedByIdAsync(id: any, doNotLoadIfNotFound: boolean): Promise<entityType | undefined>;
|
|
@@ -182,6 +182,16 @@ var RepositoryImplementation = /** @class */ (function () {
|
|
|
182
182
|
configurable: true
|
|
183
183
|
});
|
|
184
184
|
RepositoryImplementation.prototype.aggregate = function (options) {
|
|
185
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
186
|
+
return tslib_1.__generator(this, function (_a) {
|
|
187
|
+
switch (_a.label) {
|
|
188
|
+
case 0: return [4 /*yield*/, this.groupBy(options)];
|
|
189
|
+
case 1: return [2 /*return*/, (_a.sent())[0]];
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
RepositoryImplementation.prototype.groupBy = function (options) {
|
|
185
195
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
186
196
|
var findOptions, getField, getFieldNotInGroupBy, dpOptions, key, element, operation, direction, result, loaderOptions, _a, _b, key, loader;
|
|
187
197
|
var e_4, _c;
|
|
@@ -200,7 +210,7 @@ var RepositoryImplementation = /** @class */ (function () {
|
|
|
200
210
|
};
|
|
201
211
|
getFieldNotInGroupBy = function (key) {
|
|
202
212
|
var _a;
|
|
203
|
-
if ((_a = options === null || options === void 0 ? void 0 : options.
|
|
213
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.group) === null || _a === void 0 ? void 0 : _a.includes(key))
|
|
204
214
|
throw Error("field \"".concat(key, "\" cannot be used both in an aggregate and in group by"));
|
|
205
215
|
return getField(key);
|
|
206
216
|
};
|
|
@@ -208,7 +218,7 @@ var RepositoryImplementation = /** @class */ (function () {
|
|
|
208
218
|
where: findOptions.where,
|
|
209
219
|
limit: findOptions.limit,
|
|
210
220
|
page: findOptions.page,
|
|
211
|
-
|
|
221
|
+
group: (_d = options === null || options === void 0 ? void 0 : options.group) === null || _d === void 0 ? void 0 : _d.map(getField),
|
|
212
222
|
sum: (_e = options === null || options === void 0 ? void 0 : options.sum) === null || _e === void 0 ? void 0 : _e.map(getFieldNotInGroupBy),
|
|
213
223
|
avg: (_f = options === null || options === void 0 ? void 0 : options.avg) === null || _f === void 0 ? void 0 : _f.map(getFieldNotInGroupBy),
|
|
214
224
|
min: (_g = options === null || options === void 0 ? void 0 : options.min) === null || _g === void 0 ? void 0 : _g.map(getFieldNotInGroupBy),
|
|
@@ -243,19 +253,17 @@ var RepositoryImplementation = /** @class */ (function () {
|
|
|
243
253
|
}
|
|
244
254
|
}
|
|
245
255
|
}
|
|
246
|
-
return [4 /*yield*/, this._edp.
|
|
256
|
+
return [4 /*yield*/, this._edp.groupBy(dpOptions)
|
|
257
|
+
//@ts-expect-error an internal option
|
|
258
|
+
];
|
|
247
259
|
case 2:
|
|
248
260
|
result = _k.sent();
|
|
249
|
-
if (!(options === null || options === void 0 ? void 0 : options.
|
|
250
|
-
return [2 /*return*/, result[0]
|
|
251
|
-
//@ts-expect-error an internal option
|
|
252
|
-
];
|
|
253
|
-
if (!!(options === null || options === void 0 ? void 0 : options[remult3_js_1.AggregateForApiKey])) return [3 /*break*/, 5];
|
|
261
|
+
if (!(!(options === null || options === void 0 ? void 0 : options[remult3_js_1.GroupByForApiKey]) && options.group)) return [3 /*break*/, 5];
|
|
254
262
|
loaderOptions = {
|
|
255
263
|
include: {},
|
|
256
264
|
};
|
|
257
265
|
try {
|
|
258
|
-
for (_a = tslib_1.__values(options.
|
|
266
|
+
for (_a = tslib_1.__values(options.group), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
259
267
|
key = _b.value;
|
|
260
268
|
loaderOptions.include[key] = true;
|
|
261
269
|
}
|
|
@@ -268,7 +276,7 @@ var RepositoryImplementation = /** @class */ (function () {
|
|
|
268
276
|
finally { if (e_4) throw e_4.error; }
|
|
269
277
|
}
|
|
270
278
|
loader = new relation_loader_js_1.RelationLoader();
|
|
271
|
-
return [4 /*yield*/, this.populateRelationsForFields(dpOptions.
|
|
279
|
+
return [4 /*yield*/, this.populateRelationsForFields(dpOptions.group, loaderOptions, result, loader)];
|
|
272
280
|
case 3:
|
|
273
281
|
_k.sent();
|
|
274
282
|
return [4 /*yield*/, loader.resolveAll()];
|
package/src/remult3/remult3.d.ts
CHANGED
|
@@ -272,8 +272,8 @@ export type NumericKeys<T> = {
|
|
|
272
272
|
*
|
|
273
273
|
* @example
|
|
274
274
|
* // Grouping by country and city, summing the salary field, and ordering by country and sum of salary:
|
|
275
|
-
* const results = await repo.
|
|
276
|
-
*
|
|
275
|
+
* const results = await repo.groupBy({
|
|
276
|
+
* group: ['country', 'city'],
|
|
277
277
|
* sum: ['salary'],
|
|
278
278
|
* where: {
|
|
279
279
|
* salary: { $ne: 1000 },
|
|
@@ -299,11 +299,11 @@ export type NumericKeys<T> = {
|
|
|
299
299
|
* });
|
|
300
300
|
* console.log(totalSalary.salary.sum); // Outputs the total sum of salaries
|
|
301
301
|
*/
|
|
302
|
-
export type
|
|
302
|
+
export type GroupByOptions<entityType, groupByFields extends (keyof MembersOnly<entityType>)[], sumFields extends NumericKeys<entityType>[], averageFields extends NumericKeys<entityType>[], minFields extends (keyof MembersOnly<entityType>)[], maxFields extends (keyof MembersOnly<entityType>)[], distinctCountFields extends (keyof MembersOnly<entityType>)[]> = {
|
|
303
303
|
/**
|
|
304
304
|
* Fields to group by. The result will include one entry per unique combination of these fields.
|
|
305
305
|
*/
|
|
306
|
-
|
|
306
|
+
group?: groupByFields;
|
|
307
307
|
/**
|
|
308
308
|
* Fields to sum. The result will include the sum of these fields for each group.
|
|
309
309
|
*/
|
|
@@ -359,10 +359,10 @@ export type AggregateOptions<entityType, groupByFields extends (keyof MembersOnl
|
|
|
359
359
|
$count?: 'asc' | 'desc';
|
|
360
360
|
};
|
|
361
361
|
} & Pick<FindOptions<entityType>, 'limit' | 'page'>;
|
|
362
|
-
export declare const
|
|
363
|
-
export declare const
|
|
364
|
-
export declare const
|
|
365
|
-
export type
|
|
362
|
+
export declare const GroupByCountMember: "$count";
|
|
363
|
+
export declare const GroupByForApiKey: unique symbol;
|
|
364
|
+
export declare const GroupByOperators: readonly ["sum", "avg", "min", "max", "distinctCount"];
|
|
365
|
+
export type GroupByResult<entityType, groupByFields extends (keyof entityType)[], sumFields extends (keyof entityType)[], averageFields extends NumericKeys<entityType>[], minFields extends NumericKeys<entityType>[], maxFields extends NumericKeys<entityType>[], distinctCountFields extends (keyof entityType)[]> = {
|
|
366
366
|
[K in groupByFields[number] | sumFields[number]]: K extends groupByFields[number] ? entityType[K] : K extends sumFields[number] ? {
|
|
367
367
|
sum: number;
|
|
368
368
|
} : never;
|
|
@@ -415,13 +415,13 @@ export interface Repository<entityType> {
|
|
|
415
415
|
* @template sumFields The fields to sum, provided as an array of numeric keys from the entity type.
|
|
416
416
|
* @template averageFields The fields to average, provided as an array of numeric keys from the entity type.
|
|
417
417
|
*
|
|
418
|
-
* @param {
|
|
419
|
-
* @returns {Promise<
|
|
418
|
+
* @param {GroupByOptions<entityType, groupByFields, sumFields, averageFields>} options - The options for the aggregation.
|
|
419
|
+
* @returns {Promise<GroupByResult<entityType, groupByFields, sumFields, averageFields>[]> } The result of the aggregation.
|
|
420
420
|
*
|
|
421
421
|
* @example
|
|
422
422
|
* // Grouping by country and city, summing the salary field, and ordering by country and sum of salary:
|
|
423
|
-
* const results = await repo.
|
|
424
|
-
*
|
|
423
|
+
* const results = await repo.groupBy({
|
|
424
|
+
* group: ['country', 'city'],
|
|
425
425
|
* sum: ['salary'],
|
|
426
426
|
* where: {
|
|
427
427
|
* salary: { $ne: 1000 },
|
|
@@ -440,14 +440,28 @@ export interface Repository<entityType> {
|
|
|
440
440
|
* console.log(results[0].$count); // count for London, UK
|
|
441
441
|
* console.log(results[0].salary.sum); // Sum of salaries for London, UK
|
|
442
442
|
*
|
|
443
|
+
|
|
444
|
+
*/
|
|
445
|
+
groupBy<groupByFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined, sumFields extends NumericKeys<entityType>[] | undefined = undefined, averageFields extends NumericKeys<entityType>[] | undefined = undefined, minFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined, maxFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined, distinctCountFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined>(options: GroupByOptions<entityType, groupByFields extends undefined ? never : groupByFields, sumFields extends undefined ? never : sumFields, averageFields extends undefined ? never : averageFields, minFields extends undefined ? never : minFields, maxFields extends undefined ? never : maxFields, distinctCountFields extends undefined ? never : distinctCountFields>): Promise<GroupByResult<entityType, groupByFields extends undefined ? never : groupByFields, sumFields extends undefined ? never : sumFields, averageFields extends undefined ? never : averageFields, minFields extends undefined ? never : minFields, maxFields extends undefined ? never : maxFields, distinctCountFields extends undefined ? never : distinctCountFields>[]>;
|
|
446
|
+
/**
|
|
447
|
+
* Performs an aggregation on the repository's entity type based on the specified options.
|
|
448
|
+
* @template entityType The type of the entity being aggregated.
|
|
449
|
+
* @template groupByFields The fields to group by, provided as an array of keys from the entity type.
|
|
450
|
+
* @template sumFields The fields to sum, provided as an array of numeric keys from the entity type.
|
|
451
|
+
* @template averageFields The fields to average, provided as an array of numeric keys from the entity type.
|
|
452
|
+
*
|
|
453
|
+
* @param {GroupByOptions<entityType, groupByFields, sumFields, averageFields>} options - The options for the aggregation.
|
|
454
|
+
* @returns {Promise<GroupByResult<entityType, groupByFields, sumFields, averageFields>[]> } The result of the aggregation.
|
|
455
|
+
*
|
|
443
456
|
* @example
|
|
444
|
-
* // Aggregating
|
|
457
|
+
* // Aggregating (summing the salary field across all items):
|
|
445
458
|
* const totalSalary = await repo.aggregate({
|
|
446
459
|
* sum: ['salary'],
|
|
447
460
|
* });
|
|
448
461
|
* console.log(totalSalary.salary.sum); // Outputs the total sum of salaries
|
|
462
|
+
|
|
449
463
|
*/
|
|
450
|
-
aggregate<
|
|
464
|
+
aggregate<sumFields extends NumericKeys<entityType>[] | undefined = undefined, averageFields extends NumericKeys<entityType>[] | undefined = undefined, minFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined, maxFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined, distinctCountFields extends (keyof MembersOnly<entityType>)[] | undefined = undefined>(options: Omit<GroupByOptions<entityType, never, sumFields extends undefined ? never : sumFields, averageFields extends undefined ? never : averageFields, minFields extends undefined ? never : minFields, maxFields extends undefined ? never : maxFields, distinctCountFields extends undefined ? never : distinctCountFields>, 'orderBy' | 'limit' | 'page'>): Promise<GroupByResult<entityType, never, sumFields extends undefined ? never : sumFields, averageFields extends undefined ? never : averageFields, minFields extends undefined ? never : minFields, maxFields extends undefined ? never : maxFields, distinctCountFields extends undefined ? never : distinctCountFields>>;
|
|
451
465
|
/** An alternative form of fetching data from the API server, which is intended for operating on large numbers of entity objects.
|
|
452
466
|
*
|
|
453
467
|
* It also has it's own paging mechanism that can be used n paging scenarios.
|
package/src/remult3/remult3.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.flags = exports.
|
|
4
|
-
exports.
|
|
5
|
-
exports.
|
|
6
|
-
exports.
|
|
3
|
+
exports.flags = exports.GroupByOperators = exports.GroupByForApiKey = exports.GroupByCountMember = void 0;
|
|
4
|
+
exports.GroupByCountMember = '$count';
|
|
5
|
+
exports.GroupByForApiKey = Symbol.for('GroupByForApiKey');
|
|
6
|
+
exports.GroupByOperators = [
|
|
7
7
|
'sum',
|
|
8
8
|
'avg',
|
|
9
9
|
'min',
|
|
@@ -19,6 +19,7 @@ exports.flags = {
|
|
|
19
19
|
/*p1 - min, max, avg, sum, countDistinct - noam come up with an api
|
|
20
20
|
p1 - handle relations
|
|
21
21
|
*/
|
|
22
|
+
//y1 - find and count / aggregate with a single call
|
|
22
23
|
/*p1 - add id and use uuid by default, but allow changes with Fields.id.defaultIdProvider NO but defaultProvider yes???
|
|
23
24
|
//p1 - replace uuid with crypto.randomUUID and allow custom fallback NO
|
|
24
25
|
//p1 - Add example for luid
|