@contember/database 2.0.8 → 2.0.10
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/dist/development/src/builders/LimitByGroupWrapper.cjs +1 -1
- package/dist/development/src/builders/LimitByGroupWrapper.cjs.map +1 -1
- package/dist/development/src/builders/LimitByGroupWrapper.js +1 -1
- package/dist/development/src/builders/LimitByGroupWrapper.js.map +1 -1
- package/dist/development/src/builders/WindowFunction.cjs +1 -1
- package/dist/development/src/builders/WindowFunction.cjs.map +1 -1
- package/dist/development/src/builders/WindowFunction.js +1 -1
- package/dist/development/src/builders/WindowFunction.js.map +1 -1
- package/dist/development/src/client/Connection.cjs +41 -5
- package/dist/development/src/client/Connection.cjs.map +1 -1
- package/dist/development/src/client/Connection.js +41 -5
- package/dist/development/src/client/Connection.js.map +1 -1
- package/dist/production/src/builders/LimitByGroupWrapper.cjs +1 -1
- package/dist/production/src/builders/LimitByGroupWrapper.cjs.map +1 -1
- package/dist/production/src/builders/LimitByGroupWrapper.js +1 -1
- package/dist/production/src/builders/LimitByGroupWrapper.js.map +1 -1
- package/dist/production/src/builders/WindowFunction.cjs +1 -1
- package/dist/production/src/builders/WindowFunction.cjs.map +1 -1
- package/dist/production/src/builders/WindowFunction.js +1 -1
- package/dist/production/src/builders/WindowFunction.js.map +1 -1
- package/dist/production/src/client/Connection.cjs +41 -5
- package/dist/production/src/client/Connection.cjs.map +1 -1
- package/dist/production/src/client/Connection.js +41 -5
- package/dist/production/src/client/Connection.js.map +1 -1
- package/dist/tests/cases/unit/createConnectionMockAlt.d.ts +7 -2
- package/dist/tests/cases/unit/createConnectionMockAlt.d.ts.map +1 -1
- package/dist/tests/cases/unit/staleConnectionRetry.test.d.ts +2 -0
- package/dist/tests/cases/unit/staleConnectionRetry.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/builders/WindowFunction.d.ts +2 -1
- package/dist/types/builders/WindowFunction.d.ts.map +1 -1
- package/dist/types/client/Connection.d.ts +11 -2
- package/dist/types/client/Connection.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/src/builders/LimitByGroupWrapper.ts +1 -1
- package/src/builders/WindowFunction.ts +3 -2
- package/src/client/Connection.ts +58 -3
- package/tests/cases/unit/createConnectionMockAlt.ts +18 -2
- package/tests/cases/unit/queryBuilder.test.ts +22 -1
- package/tests/cases/unit/staleConnectionRetry.test.ts +86 -0
|
@@ -26,7 +26,7 @@ class LimitByGroupWrapper {
|
|
|
26
26
|
if (limit !== void 0) {
|
|
27
27
|
wrapperQb = wrapperQb.where((expr) => expr.compare(["data", "rowNumber_"], ConditionBuilder.Operator.lte, start + limit));
|
|
28
28
|
}
|
|
29
|
-
return await wrapperQb.getResult(db);
|
|
29
|
+
return await wrapperQb.orderBy(["data", "rowNumber_"]).getResult(db);
|
|
30
30
|
} else if (this.orderByCallback) {
|
|
31
31
|
return await this.orderByCallback(null, qb)[1].getResult(db);
|
|
32
32
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LimitByGroupWrapper.cjs","sources":["../../../../../packages/database/src/builders/LimitByGroupWrapper.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { Operator } from './ConditionBuilder'\nimport { Client } from '../client'\nimport { WindowFunction } from './WindowFunction'\n\ntype OrderByCallback = <Orderable extends QueryBuilder.Orderable<any> | null>(\n\torderable: Orderable,\n\tqb: SelectBuilder<any>,\n) => [Orderable, SelectBuilder<any>]\n\nclass LimitByGroupWrapper {\n\tconstructor(\n\t\tprivate readonly groupBy: QueryBuilder.ColumnIdentifier,\n\t\tprivate readonly orderByCallback: OrderByCallback | undefined,\n\t\tprivate readonly skip: number | undefined,\n\t\tprivate readonly limit: number | undefined,\n\t) {}\n\n\tpublic async getResult<R>(qb: SelectBuilder<R>, db: Client): Promise<R[]> {\n\t\tif (this.limit !== undefined || this.skip !== undefined) {\n\t\t\tlet window = WindowFunction.createEmpty().rowNumber().partitionBy(this.groupBy)\n\t\t\tif (this.orderByCallback !== undefined) {\n\t\t\t\t[window, qb] = this.orderByCallback(window, qb)\n\t\t\t}\n\n\t\t\tqb = qb.select(window.compile(), 'rowNumber_')\n\n\t\t\tlet wrapperQb: SelectBuilder<R> = SelectBuilder.create<R>()\n\t\t\t\t.meta(qb.options.meta)\n\t\t\t\t.with('data', qb)\n\t\t\t\t.from('data')\n\t\t\t\t.select(['data', '*'])\n\n\t\t\tconst start = this.skip || 0\n\t\t\tif (start > 0) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.gt, start))\n\t\t\t}\n\n\t\t\tconst limit = this.limit\n\t\t\tif (limit !== undefined) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.lte, start + limit))\n\t\t\t}\n\n\t\t\treturn await wrapperQb.getResult(db)\n\t\t} else if (this.orderByCallback) {\n\t\t\treturn await this.orderByCallback(null, qb)[1].getResult(db)\n\t\t}\n\n\t\treturn await qb.getResult(db)\n\t}\n}\n\nexport { LimitByGroupWrapper }\n"],"names":["WindowFunction","SelectBuilder","Operator"],"mappings":";;;;;AAWA,MAAM,oBAAoB;AAAA,EACzB,YACkB,SACA,iBACA,MACA,OAChB;AAJgB,SAAA,UAAA;AACA,SAAA,kBAAA;AACA,SAAA,OAAA;AACA,SAAA,QAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,UAAa,IAAsB,IAA0B;AACzE,QAAI,KAAK,UAAU,UAAa,KAAK,SAAS,QAAW;AACpD,UAAA,SAASA,8BAAe,YAAY,EAAE,YAAY,YAAY,KAAK,OAAO;AAC1E,UAAA,KAAK,oBAAoB,QAAW;AACvC,SAAC,QAAQ,EAAE,IAAI,KAAK,gBAAgB,QAAQ,EAAE;AAAA,MAAA;AAG/C,WAAK,GAAG,OAAO,OAAO,QAAA,GAAW,YAAY;AAEzC,UAAA,YAA8BC,4BAAc,OAAU,EACxD,KAAK,GAAG,QAAQ,IAAI,EACpB,KAAK,QAAQ,EAAE,EACf,KAAK,MAAM,EACX,OAAO,CAAC,QAAQ,GAAG,CAAC;AAEhB,YAAA,QAAQ,KAAK,QAAQ;AAC3B,UAAI,QAAQ,GAAG;AACd,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAGC,iBAAA,SAAS,IAAI,KAAK,CAAC;AAAA,MAAA;AAG7F,YAAM,QAAQ,KAAK;AACnB,UAAI,UAAU,QAAW;AACxB,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAGA,iBAAA,SAAS,KAAK,QAAQ,KAAK,CAAC;AAAA,MAAA;AAG/F,aAAA,MAAM,UAAU,UAAU,EAAE;AAAA,IAAA,
|
|
1
|
+
{"version":3,"file":"LimitByGroupWrapper.cjs","sources":["../../../../../packages/database/src/builders/LimitByGroupWrapper.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { Operator } from './ConditionBuilder'\nimport { Client } from '../client'\nimport { WindowFunction } from './WindowFunction'\n\ntype OrderByCallback = <Orderable extends QueryBuilder.Orderable<any> | null>(\n\torderable: Orderable,\n\tqb: SelectBuilder<any>,\n) => [Orderable, SelectBuilder<any>]\n\nclass LimitByGroupWrapper {\n\tconstructor(\n\t\tprivate readonly groupBy: QueryBuilder.ColumnIdentifier,\n\t\tprivate readonly orderByCallback: OrderByCallback | undefined,\n\t\tprivate readonly skip: number | undefined,\n\t\tprivate readonly limit: number | undefined,\n\t) {}\n\n\tpublic async getResult<R>(qb: SelectBuilder<R>, db: Client): Promise<R[]> {\n\t\tif (this.limit !== undefined || this.skip !== undefined) {\n\t\t\tlet window = WindowFunction.createEmpty().rowNumber().partitionBy(this.groupBy)\n\t\t\tif (this.orderByCallback !== undefined) {\n\t\t\t\t[window, qb] = this.orderByCallback(window, qb)\n\t\t\t}\n\n\t\t\tqb = qb.select(window.compile(), 'rowNumber_')\n\n\t\t\tlet wrapperQb: SelectBuilder<R> = SelectBuilder.create<R>()\n\t\t\t\t.meta(qb.options.meta)\n\t\t\t\t.with('data', qb)\n\t\t\t\t.from('data')\n\t\t\t\t.select(['data', '*'])\n\n\t\t\tconst start = this.skip || 0\n\t\t\tif (start > 0) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.gt, start))\n\t\t\t}\n\n\t\t\tconst limit = this.limit\n\t\t\tif (limit !== undefined) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.lte, start + limit))\n\t\t\t}\n\n\t\t\treturn await wrapperQb.orderBy(['data', 'rowNumber_']).getResult(db)\n\t\t} else if (this.orderByCallback) {\n\t\t\treturn await this.orderByCallback(null, qb)[1].getResult(db)\n\t\t}\n\n\t\treturn await qb.getResult(db)\n\t}\n}\n\nexport { LimitByGroupWrapper }\n"],"names":["WindowFunction","SelectBuilder","Operator"],"mappings":";;;;;AAWA,MAAM,oBAAoB;AAAA,EACzB,YACkB,SACA,iBACA,MACA,OAChB;AAJgB,SAAA,UAAA;AACA,SAAA,kBAAA;AACA,SAAA,OAAA;AACA,SAAA,QAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,UAAa,IAAsB,IAA0B;AACzE,QAAI,KAAK,UAAU,UAAa,KAAK,SAAS,QAAW;AACpD,UAAA,SAASA,8BAAe,YAAY,EAAE,YAAY,YAAY,KAAK,OAAO;AAC1E,UAAA,KAAK,oBAAoB,QAAW;AACvC,SAAC,QAAQ,EAAE,IAAI,KAAK,gBAAgB,QAAQ,EAAE;AAAA,MAAA;AAG/C,WAAK,GAAG,OAAO,OAAO,QAAA,GAAW,YAAY;AAEzC,UAAA,YAA8BC,4BAAc,OAAU,EACxD,KAAK,GAAG,QAAQ,IAAI,EACpB,KAAK,QAAQ,EAAE,EACf,KAAK,MAAM,EACX,OAAO,CAAC,QAAQ,GAAG,CAAC;AAEhB,YAAA,QAAQ,KAAK,QAAQ;AAC3B,UAAI,QAAQ,GAAG;AACd,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAGC,iBAAA,SAAS,IAAI,KAAK,CAAC;AAAA,MAAA;AAG7F,YAAM,QAAQ,KAAK;AACnB,UAAI,UAAU,QAAW;AACxB,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAGA,iBAAA,SAAS,KAAK,QAAQ,KAAK,CAAC;AAAA,MAAA;AAG/F,aAAA,MAAM,UAAU,QAAQ,CAAC,QAAQ,YAAY,CAAC,EAAE,UAAU,EAAE;AAAA,IAAA,WACzD,KAAK,iBAAiB;AACzB,aAAA,MAAM,KAAK,gBAAgB,MAAM,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE;AAAA,IAAA;AAGrD,WAAA,MAAM,GAAG,UAAU,EAAE;AAAA,EAAA;AAE9B;;"}
|
|
@@ -24,7 +24,7 @@ class LimitByGroupWrapper {
|
|
|
24
24
|
if (limit !== void 0) {
|
|
25
25
|
wrapperQb = wrapperQb.where((expr) => expr.compare(["data", "rowNumber_"], Operator.lte, start + limit));
|
|
26
26
|
}
|
|
27
|
-
return await wrapperQb.getResult(db);
|
|
27
|
+
return await wrapperQb.orderBy(["data", "rowNumber_"]).getResult(db);
|
|
28
28
|
} else if (this.orderByCallback) {
|
|
29
29
|
return await this.orderByCallback(null, qb)[1].getResult(db);
|
|
30
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LimitByGroupWrapper.js","sources":["../../../../../packages/database/src/builders/LimitByGroupWrapper.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { Operator } from './ConditionBuilder'\nimport { Client } from '../client'\nimport { WindowFunction } from './WindowFunction'\n\ntype OrderByCallback = <Orderable extends QueryBuilder.Orderable<any> | null>(\n\torderable: Orderable,\n\tqb: SelectBuilder<any>,\n) => [Orderable, SelectBuilder<any>]\n\nclass LimitByGroupWrapper {\n\tconstructor(\n\t\tprivate readonly groupBy: QueryBuilder.ColumnIdentifier,\n\t\tprivate readonly orderByCallback: OrderByCallback | undefined,\n\t\tprivate readonly skip: number | undefined,\n\t\tprivate readonly limit: number | undefined,\n\t) {}\n\n\tpublic async getResult<R>(qb: SelectBuilder<R>, db: Client): Promise<R[]> {\n\t\tif (this.limit !== undefined || this.skip !== undefined) {\n\t\t\tlet window = WindowFunction.createEmpty().rowNumber().partitionBy(this.groupBy)\n\t\t\tif (this.orderByCallback !== undefined) {\n\t\t\t\t[window, qb] = this.orderByCallback(window, qb)\n\t\t\t}\n\n\t\t\tqb = qb.select(window.compile(), 'rowNumber_')\n\n\t\t\tlet wrapperQb: SelectBuilder<R> = SelectBuilder.create<R>()\n\t\t\t\t.meta(qb.options.meta)\n\t\t\t\t.with('data', qb)\n\t\t\t\t.from('data')\n\t\t\t\t.select(['data', '*'])\n\n\t\t\tconst start = this.skip || 0\n\t\t\tif (start > 0) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.gt, start))\n\t\t\t}\n\n\t\t\tconst limit = this.limit\n\t\t\tif (limit !== undefined) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.lte, start + limit))\n\t\t\t}\n\n\t\t\treturn await wrapperQb.getResult(db)\n\t\t} else if (this.orderByCallback) {\n\t\t\treturn await this.orderByCallback(null, qb)[1].getResult(db)\n\t\t}\n\n\t\treturn await qb.getResult(db)\n\t}\n}\n\nexport { LimitByGroupWrapper }\n"],"names":[],"mappings":";;;AAWA,MAAM,oBAAoB;AAAA,EACzB,YACkB,SACA,iBACA,MACA,OAChB;AAJgB,SAAA,UAAA;AACA,SAAA,kBAAA;AACA,SAAA,OAAA;AACA,SAAA,QAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,UAAa,IAAsB,IAA0B;AACzE,QAAI,KAAK,UAAU,UAAa,KAAK,SAAS,QAAW;AACpD,UAAA,SAAS,eAAe,YAAY,EAAE,YAAY,YAAY,KAAK,OAAO;AAC1E,UAAA,KAAK,oBAAoB,QAAW;AACvC,SAAC,QAAQ,EAAE,IAAI,KAAK,gBAAgB,QAAQ,EAAE;AAAA,MAAA;AAG/C,WAAK,GAAG,OAAO,OAAO,QAAA,GAAW,YAAY;AAEzC,UAAA,YAA8B,cAAc,OAAU,EACxD,KAAK,GAAG,QAAQ,IAAI,EACpB,KAAK,QAAQ,EAAE,EACf,KAAK,MAAM,EACX,OAAO,CAAC,QAAQ,GAAG,CAAC;AAEhB,YAAA,QAAQ,KAAK,QAAQ;AAC3B,UAAI,QAAQ,GAAG;AACd,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAG,SAAS,IAAI,KAAK,CAAC;AAAA,MAAA;AAG7F,YAAM,QAAQ,KAAK;AACnB,UAAI,UAAU,QAAW;AACxB,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAG,SAAS,KAAK,QAAQ,KAAK,CAAC;AAAA,MAAA;AAG/F,aAAA,MAAM,UAAU,UAAU,EAAE;AAAA,IAAA,
|
|
1
|
+
{"version":3,"file":"LimitByGroupWrapper.js","sources":["../../../../../packages/database/src/builders/LimitByGroupWrapper.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { Operator } from './ConditionBuilder'\nimport { Client } from '../client'\nimport { WindowFunction } from './WindowFunction'\n\ntype OrderByCallback = <Orderable extends QueryBuilder.Orderable<any> | null>(\n\torderable: Orderable,\n\tqb: SelectBuilder<any>,\n) => [Orderable, SelectBuilder<any>]\n\nclass LimitByGroupWrapper {\n\tconstructor(\n\t\tprivate readonly groupBy: QueryBuilder.ColumnIdentifier,\n\t\tprivate readonly orderByCallback: OrderByCallback | undefined,\n\t\tprivate readonly skip: number | undefined,\n\t\tprivate readonly limit: number | undefined,\n\t) {}\n\n\tpublic async getResult<R>(qb: SelectBuilder<R>, db: Client): Promise<R[]> {\n\t\tif (this.limit !== undefined || this.skip !== undefined) {\n\t\t\tlet window = WindowFunction.createEmpty().rowNumber().partitionBy(this.groupBy)\n\t\t\tif (this.orderByCallback !== undefined) {\n\t\t\t\t[window, qb] = this.orderByCallback(window, qb)\n\t\t\t}\n\n\t\t\tqb = qb.select(window.compile(), 'rowNumber_')\n\n\t\t\tlet wrapperQb: SelectBuilder<R> = SelectBuilder.create<R>()\n\t\t\t\t.meta(qb.options.meta)\n\t\t\t\t.with('data', qb)\n\t\t\t\t.from('data')\n\t\t\t\t.select(['data', '*'])\n\n\t\t\tconst start = this.skip || 0\n\t\t\tif (start > 0) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.gt, start))\n\t\t\t}\n\n\t\t\tconst limit = this.limit\n\t\t\tif (limit !== undefined) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.lte, start + limit))\n\t\t\t}\n\n\t\t\treturn await wrapperQb.orderBy(['data', 'rowNumber_']).getResult(db)\n\t\t} else if (this.orderByCallback) {\n\t\t\treturn await this.orderByCallback(null, qb)[1].getResult(db)\n\t\t}\n\n\t\treturn await qb.getResult(db)\n\t}\n}\n\nexport { LimitByGroupWrapper }\n"],"names":[],"mappings":";;;AAWA,MAAM,oBAAoB;AAAA,EACzB,YACkB,SACA,iBACA,MACA,OAChB;AAJgB,SAAA,UAAA;AACA,SAAA,kBAAA;AACA,SAAA,OAAA;AACA,SAAA,QAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,UAAa,IAAsB,IAA0B;AACzE,QAAI,KAAK,UAAU,UAAa,KAAK,SAAS,QAAW;AACpD,UAAA,SAAS,eAAe,YAAY,EAAE,YAAY,YAAY,KAAK,OAAO;AAC1E,UAAA,KAAK,oBAAoB,QAAW;AACvC,SAAC,QAAQ,EAAE,IAAI,KAAK,gBAAgB,QAAQ,EAAE;AAAA,MAAA;AAG/C,WAAK,GAAG,OAAO,OAAO,QAAA,GAAW,YAAY;AAEzC,UAAA,YAA8B,cAAc,OAAU,EACxD,KAAK,GAAG,QAAQ,IAAI,EACpB,KAAK,QAAQ,EAAE,EACf,KAAK,MAAM,EACX,OAAO,CAAC,QAAQ,GAAG,CAAC;AAEhB,YAAA,QAAQ,KAAK,QAAQ;AAC3B,UAAI,QAAQ,GAAG;AACd,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAG,SAAS,IAAI,KAAK,CAAC;AAAA,MAAA;AAG7F,YAAM,QAAQ,KAAK;AACnB,UAAI,UAAU,QAAW;AACxB,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAG,SAAS,KAAK,QAAQ,KAAK,CAAC;AAAA,MAAA;AAG/F,aAAA,MAAM,UAAU,QAAQ,CAAC,QAAQ,YAAY,CAAC,EAAE,UAAU,EAAE;AAAA,IAAA,WACzD,KAAK,iBAAiB;AACzB,aAAA,MAAM,KAAK,gBAAgB,MAAM,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE;AAAA,IAAA;AAGrD,WAAA,MAAM,GAAG,UAAU,EAAE;AAAA,EAAA;AAE9B;"}
|
|
@@ -26,7 +26,7 @@ class WindowFunction {
|
|
|
26
26
|
const raw = expression instanceof Literal.Literal ? expression : new Literal.Literal(formatUtils.toFqnWrap(expression));
|
|
27
27
|
return new WindowFunction(this.windowFunction, this.partitionByExpr, [
|
|
28
28
|
...this.orderByColumns,
|
|
29
|
-
raw.appendString(direction
|
|
29
|
+
raw.appendString(` ${direction}`)
|
|
30
30
|
]);
|
|
31
31
|
}
|
|
32
32
|
compile() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WindowFunction.cjs","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral, toFqnWrap } from './utils'\n\nclass WindowFunction<HasFunction extends boolean> implements QueryBuilder.Orderable<WindowFunction<HasFunction>> {\n\tprivate constructor(\n\t\tprivate readonly windowFunction: Literal | undefined,\n\t\tprivate readonly partitionByExpr: Literal | undefined,\n\t\tprivate readonly orderByColumns: Literal[],\n\t) {}\n\n\tpublic static createEmpty(): WindowFunction<false> {\n\t\treturn new WindowFunction<false>(undefined, undefined, [])\n\t}\n\n\tpublic rowNumber(): WindowFunction<true> {\n\t\treturn new WindowFunction(new Literal('row_number()'), this.partitionByExpr, this.orderByColumns)\n\t}\n\n\tpublic partitionBy(columnName: QueryBuilder.ColumnIdentifier): WindowFunction<HasFunction>\n\tpublic partitionBy(callback: QueryBuilder.ColumnExpression): WindowFunction<HasFunction>\n\tpublic partitionBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): WindowFunction<HasFunction> {\n\t\tconst raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn new WindowFunction(this.windowFunction, raw, this.orderByColumns)\n\t}\n\n\torderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection:
|
|
1
|
+
{"version":3,"file":"WindowFunction.cjs","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral, toFqnWrap } from './utils'\nimport type { SelectBuilder } from './SelectBuilder'\n\nclass WindowFunction<HasFunction extends boolean> implements QueryBuilder.Orderable<WindowFunction<HasFunction>> {\n\tprivate constructor(\n\t\tprivate readonly windowFunction: Literal | undefined,\n\t\tprivate readonly partitionByExpr: Literal | undefined,\n\t\tprivate readonly orderByColumns: Literal[],\n\t) {}\n\n\tpublic static createEmpty(): WindowFunction<false> {\n\t\treturn new WindowFunction<false>(undefined, undefined, [])\n\t}\n\n\tpublic rowNumber(): WindowFunction<true> {\n\t\treturn new WindowFunction(new Literal('row_number()'), this.partitionByExpr, this.orderByColumns)\n\t}\n\n\tpublic partitionBy(columnName: QueryBuilder.ColumnIdentifier): WindowFunction<HasFunction>\n\tpublic partitionBy(callback: QueryBuilder.ColumnExpression): WindowFunction<HasFunction>\n\tpublic partitionBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): WindowFunction<HasFunction> {\n\t\tconst raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn new WindowFunction(this.windowFunction, raw, this.orderByColumns)\n\t}\n\n\torderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection: SelectBuilder.OrderByDirection = 'asc',\n\t): WindowFunction<HasFunction> {\n\t\tconst raw = expression instanceof Literal ? expression : new Literal(toFqnWrap(expression))\n\t\treturn new WindowFunction(this.windowFunction, this.partitionByExpr, [\n\t\t\t...this.orderByColumns,\n\t\t\traw.appendString(` ${direction}`),\n\t\t])\n\t}\n\n\tcompile(): Literal {\n\t\tif (this.windowFunction === undefined) {\n\t\t\tthrow new Error()\n\t\t}\n\t\tlet windowDefinition = new Literal('')\n\t\tif (this.partitionByExpr !== undefined) {\n\t\t\twindowDefinition = windowDefinition.appendString('partition by ').append(this.partitionByExpr)\n\t\t}\n\t\tif (this.orderByColumns.length > 0) {\n\t\t\twindowDefinition = windowDefinition.appendString(' order by ').appendAll(this.orderByColumns, ', ')\n\t\t}\n\t\treturn new Literal(this.windowFunction.sql + ' over(' + windowDefinition.sql + ')', [\n\t\t\t...this.windowFunction.parameters,\n\t\t\t...windowDefinition.parameters,\n\t\t])\n\t}\n}\n\nexport { WindowFunction }\n"],"names":["Literal","columnExpressionToLiteral","toFqnWrap"],"mappings":";;;;;AAKA,MAAM,eAA2G;AAAA,EACxG,YACU,gBACA,iBACA,gBAChB;AAHgB,SAAA,iBAAA;AACA,SAAA,kBAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAGlB,OAAc,cAAqC;AAClD,WAAO,IAAI,eAAsB,QAAW,QAAW,CAAA,CAAE;AAAA,EAAA;AAAA,EAGnD,YAAkC;AACjC,WAAA,IAAI,eAAe,IAAIA,gBAAQ,cAAc,GAAG,KAAK,iBAAiB,KAAK,cAAc;AAAA,EAAA;AAAA,EAK1F,YAAY,MAAkG;AAC9G,UAAA,MAAMC,gCAA0B,IAAI;AAC1C,QAAI,QAAQ,QAAW;AACf,aAAA;AAAA,IAAA;AAER,WAAO,IAAI,eAAe,KAAK,gBAAgB,KAAK,KAAK,cAAc;AAAA,EAAA;AAAA,EAGxE,QACC,YACA,YAA4C,OACd;AACxB,UAAA,MAAM,sBAAsBD,kBAAU,aAAa,IAAIA,gBAAQE,YAAAA,UAAU,UAAU,CAAC;AAC1F,WAAO,IAAI,eAAe,KAAK,gBAAgB,KAAK,iBAAiB;AAAA,MACpE,GAAG,KAAK;AAAA,MACR,IAAI,aAAa,IAAI,SAAS,EAAE;AAAA,IAAA,CAChC;AAAA,EAAA;AAAA,EAGF,UAAmB;AACd,QAAA,KAAK,mBAAmB,QAAW;AACtC,YAAM,IAAI,MAAM;AAAA,IAAA;AAEb,QAAA,mBAAmB,IAAIF,QAAA,QAAQ,EAAE;AACjC,QAAA,KAAK,oBAAoB,QAAW;AACvC,yBAAmB,iBAAiB,aAAa,eAAe,EAAE,OAAO,KAAK,eAAe;AAAA,IAAA;AAE1F,QAAA,KAAK,eAAe,SAAS,GAAG;AACnC,yBAAmB,iBAAiB,aAAa,YAAY,EAAE,UAAU,KAAK,gBAAgB,IAAI;AAAA,IAAA;AAE5F,WAAA,IAAIA,QAAAA,QAAQ,KAAK,eAAe,MAAM,WAAW,iBAAiB,MAAM,KAAK;AAAA,MACnF,GAAG,KAAK,eAAe;AAAA,MACvB,GAAG,iBAAiB;AAAA,IAAA,CACpB;AAAA,EAAA;AAEH;;"}
|
|
@@ -24,7 +24,7 @@ class WindowFunction {
|
|
|
24
24
|
const raw = expression instanceof Literal ? expression : new Literal(toFqnWrap(expression));
|
|
25
25
|
return new WindowFunction(this.windowFunction, this.partitionByExpr, [
|
|
26
26
|
...this.orderByColumns,
|
|
27
|
-
raw.appendString(direction
|
|
27
|
+
raw.appendString(` ${direction}`)
|
|
28
28
|
]);
|
|
29
29
|
}
|
|
30
30
|
compile() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WindowFunction.js","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral, toFqnWrap } from './utils'\n\nclass WindowFunction<HasFunction extends boolean> implements QueryBuilder.Orderable<WindowFunction<HasFunction>> {\n\tprivate constructor(\n\t\tprivate readonly windowFunction: Literal | undefined,\n\t\tprivate readonly partitionByExpr: Literal | undefined,\n\t\tprivate readonly orderByColumns: Literal[],\n\t) {}\n\n\tpublic static createEmpty(): WindowFunction<false> {\n\t\treturn new WindowFunction<false>(undefined, undefined, [])\n\t}\n\n\tpublic rowNumber(): WindowFunction<true> {\n\t\treturn new WindowFunction(new Literal('row_number()'), this.partitionByExpr, this.orderByColumns)\n\t}\n\n\tpublic partitionBy(columnName: QueryBuilder.ColumnIdentifier): WindowFunction<HasFunction>\n\tpublic partitionBy(callback: QueryBuilder.ColumnExpression): WindowFunction<HasFunction>\n\tpublic partitionBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): WindowFunction<HasFunction> {\n\t\tconst raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn new WindowFunction(this.windowFunction, raw, this.orderByColumns)\n\t}\n\n\torderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection:
|
|
1
|
+
{"version":3,"file":"WindowFunction.js","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral, toFqnWrap } from './utils'\nimport type { SelectBuilder } from './SelectBuilder'\n\nclass WindowFunction<HasFunction extends boolean> implements QueryBuilder.Orderable<WindowFunction<HasFunction>> {\n\tprivate constructor(\n\t\tprivate readonly windowFunction: Literal | undefined,\n\t\tprivate readonly partitionByExpr: Literal | undefined,\n\t\tprivate readonly orderByColumns: Literal[],\n\t) {}\n\n\tpublic static createEmpty(): WindowFunction<false> {\n\t\treturn new WindowFunction<false>(undefined, undefined, [])\n\t}\n\n\tpublic rowNumber(): WindowFunction<true> {\n\t\treturn new WindowFunction(new Literal('row_number()'), this.partitionByExpr, this.orderByColumns)\n\t}\n\n\tpublic partitionBy(columnName: QueryBuilder.ColumnIdentifier): WindowFunction<HasFunction>\n\tpublic partitionBy(callback: QueryBuilder.ColumnExpression): WindowFunction<HasFunction>\n\tpublic partitionBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): WindowFunction<HasFunction> {\n\t\tconst raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn new WindowFunction(this.windowFunction, raw, this.orderByColumns)\n\t}\n\n\torderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection: SelectBuilder.OrderByDirection = 'asc',\n\t): WindowFunction<HasFunction> {\n\t\tconst raw = expression instanceof Literal ? expression : new Literal(toFqnWrap(expression))\n\t\treturn new WindowFunction(this.windowFunction, this.partitionByExpr, [\n\t\t\t...this.orderByColumns,\n\t\t\traw.appendString(` ${direction}`),\n\t\t])\n\t}\n\n\tcompile(): Literal {\n\t\tif (this.windowFunction === undefined) {\n\t\t\tthrow new Error()\n\t\t}\n\t\tlet windowDefinition = new Literal('')\n\t\tif (this.partitionByExpr !== undefined) {\n\t\t\twindowDefinition = windowDefinition.appendString('partition by ').append(this.partitionByExpr)\n\t\t}\n\t\tif (this.orderByColumns.length > 0) {\n\t\t\twindowDefinition = windowDefinition.appendString(' order by ').appendAll(this.orderByColumns, ', ')\n\t\t}\n\t\treturn new Literal(this.windowFunction.sql + ' over(' + windowDefinition.sql + ')', [\n\t\t\t...this.windowFunction.parameters,\n\t\t\t...windowDefinition.parameters,\n\t\t])\n\t}\n}\n\nexport { WindowFunction }\n"],"names":[],"mappings":";;;AAKA,MAAM,eAA2G;AAAA,EACxG,YACU,gBACA,iBACA,gBAChB;AAHgB,SAAA,iBAAA;AACA,SAAA,kBAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAGlB,OAAc,cAAqC;AAClD,WAAO,IAAI,eAAsB,QAAW,QAAW,CAAA,CAAE;AAAA,EAAA;AAAA,EAGnD,YAAkC;AACjC,WAAA,IAAI,eAAe,IAAI,QAAQ,cAAc,GAAG,KAAK,iBAAiB,KAAK,cAAc;AAAA,EAAA;AAAA,EAK1F,YAAY,MAAkG;AAC9G,UAAA,MAAM,0BAA0B,IAAI;AAC1C,QAAI,QAAQ,QAAW;AACf,aAAA;AAAA,IAAA;AAER,WAAO,IAAI,eAAe,KAAK,gBAAgB,KAAK,KAAK,cAAc;AAAA,EAAA;AAAA,EAGxE,QACC,YACA,YAA4C,OACd;AACxB,UAAA,MAAM,sBAAsB,UAAU,aAAa,IAAI,QAAQ,UAAU,UAAU,CAAC;AAC1F,WAAO,IAAI,eAAe,KAAK,gBAAgB,KAAK,iBAAiB;AAAA,MACpE,GAAG,KAAK;AAAA,MACR,IAAI,aAAa,IAAI,SAAS,EAAE;AAAA,IAAA,CAChC;AAAA,EAAA;AAAA,EAGF,UAAmB;AACd,QAAA,KAAK,mBAAmB,QAAW;AACtC,YAAM,IAAI,MAAM;AAAA,IAAA;AAEb,QAAA,mBAAmB,IAAI,QAAQ,EAAE;AACjC,QAAA,KAAK,oBAAoB,QAAW;AACvC,yBAAmB,iBAAiB,aAAa,eAAe,EAAE,OAAO,KAAK,eAAe;AAAA,IAAA;AAE1F,QAAA,KAAK,eAAe,SAAS,GAAG;AACnC,yBAAmB,iBAAiB,aAAa,YAAY,EAAE,UAAU,KAAK,gBAAgB,IAAI;AAAA,IAAA;AAE5F,WAAA,IAAI,QAAQ,KAAK,eAAe,MAAM,WAAW,iBAAiB,MAAM,KAAK;AAAA,MACnF,GAAG,KAAK,eAAe;AAAA,MACvB,GAAG,iBAAiB;AAAA,IAAA,CACpB;AAAA,EAAA;AAEH;"}
|
|
@@ -4,17 +4,20 @@ const EventManager = require("./EventManager.cjs");
|
|
|
4
4
|
const Client = require("./Client.cjs");
|
|
5
5
|
const Pool = require("./Pool.cjs");
|
|
6
6
|
require("../Literal.cjs");
|
|
7
|
+
const errorCodes = require("./errorCodes.cjs");
|
|
8
|
+
const errors = require("./errors.cjs");
|
|
7
9
|
require("node:timers");
|
|
8
10
|
require("../utils/Mutex.cjs");
|
|
9
11
|
const pgClientFactory = require("../utils/pgClientFactory.cjs");
|
|
10
12
|
const AcquiredConnection = require("./AcquiredConnection.cjs");
|
|
11
13
|
class Connection {
|
|
12
|
-
constructor(pool, eventManager = new EventManager.EventManager(null)) {
|
|
14
|
+
constructor(pool, eventManager = new EventManager.EventManager(null), options = {}) {
|
|
13
15
|
this.pool = pool;
|
|
14
16
|
this.eventManager = eventManager;
|
|
17
|
+
this.options = options;
|
|
15
18
|
}
|
|
16
|
-
static create({ pool = {}, ...config }, logError) {
|
|
17
|
-
return new Connection(new Pool.Pool(pgClientFactory.createPgClientFactory(config), { ...pool, logError }));
|
|
19
|
+
static create({ pool = {}, ...config }, logError, options = {}) {
|
|
20
|
+
return new Connection(new Pool.Pool(pgClientFactory.createPgClientFactory(config), { ...pool, logError }), new EventManager.EventManager(null), options);
|
|
18
21
|
}
|
|
19
22
|
static createSingle(config, logError) {
|
|
20
23
|
return new Connection(new Pool.Pool(pgClientFactory.createPgClientFactory(config), { maxConnections: 1, maxIdle: 0, logError }));
|
|
@@ -23,16 +26,41 @@ class Connection {
|
|
|
23
26
|
return new Client.Client(this, schema, queryMeta, new EventManager.EventManager(this.eventManager));
|
|
24
27
|
}
|
|
25
28
|
async scope(callback, options = {}) {
|
|
26
|
-
const acquired = await this.pool.acquire();
|
|
27
29
|
const eventManager = new EventManager.EventManager(options.eventManager ?? this.eventManager);
|
|
30
|
+
while (true) {
|
|
31
|
+
const attempt = await this.scopeOnce(callback, eventManager);
|
|
32
|
+
if (!attempt.stale) {
|
|
33
|
+
return attempt.result;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async scopeOnce(callback, eventManager) {
|
|
38
|
+
const acquired = await this.pool.acquire();
|
|
39
|
+
const reused = acquired.uses > 1;
|
|
40
|
+
let lost = false;
|
|
41
|
+
let answered = false;
|
|
42
|
+
const onError = () => {
|
|
43
|
+
lost = true;
|
|
44
|
+
};
|
|
45
|
+
const onAnswer = () => {
|
|
46
|
+
answered = true;
|
|
47
|
+
};
|
|
48
|
+
acquired.client.on("error", onError);
|
|
49
|
+
eventManager.on(EventManager.EventManager.Event.queryEnd, onAnswer);
|
|
28
50
|
try {
|
|
29
51
|
const connection = new AcquiredConnection.AcquiredConnection(acquired.client, eventManager);
|
|
30
52
|
const result = await callback(connection);
|
|
31
53
|
this.pool.release(acquired);
|
|
32
|
-
return result;
|
|
54
|
+
return { stale: false, result };
|
|
33
55
|
} catch (e) {
|
|
34
56
|
this.pool.dispose(acquired);
|
|
57
|
+
if (this.options.retryStaleConnection && reused && !answered && (lost || isConnectionLoss(e))) {
|
|
58
|
+
return { stale: true };
|
|
59
|
+
}
|
|
35
60
|
throw e;
|
|
61
|
+
} finally {
|
|
62
|
+
acquired.client.off("error", onError);
|
|
63
|
+
eventManager.removeListener(EventManager.EventManager.Event.queryEnd, onAnswer);
|
|
36
64
|
}
|
|
37
65
|
}
|
|
38
66
|
async transaction(callback, options = {}) {
|
|
@@ -55,6 +83,14 @@ class Connection {
|
|
|
55
83
|
return this.pool.getPoolStatus();
|
|
56
84
|
}
|
|
57
85
|
}
|
|
86
|
+
const connectionLossCodes = /* @__PURE__ */ new Set([
|
|
87
|
+
errorCodes.ClientErrorCodes.CONNECTION_EXCEPTION,
|
|
88
|
+
errorCodes.ClientErrorCodes.CONNECTION_DOES_NOT_EXIST,
|
|
89
|
+
errorCodes.ClientErrorCodes.CONNECTION_FAILURE,
|
|
90
|
+
errorCodes.ClientErrorCodes.ADMIN_SHUTDOWN,
|
|
91
|
+
errorCodes.ClientErrorCodes.CRASH_SHUTDOWN
|
|
92
|
+
]);
|
|
93
|
+
const isConnectionLoss = (error) => error instanceof errors.DatabaseError && error.code !== void 0 && connectionLossCodes.has(error.code);
|
|
58
94
|
((Connection2) => {
|
|
59
95
|
Connection2.REPEATABLE_READ = "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ";
|
|
60
96
|
})(Connection || (Connection = {}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Connection.cjs","sources":["../../../../../packages/database/src/client/Connection.ts"],"sourcesContent":["import { EventManager } from './EventManager'\nimport { Client } from './Client'\nimport { Pool, PoolConfig, PoolStatus } from './Pool'\nimport { createPgClientFactory } from '../utils'\nimport { DatabaseConfig } from '../types'\nimport { AcquiredConnection } from './AcquiredConnection'\nimport { Notification } from 'pg'\n\nclass Connection implements Connection.ConnectionLike, Connection.ClientFactory, Connection.PoolStatusProvider {\n\tconstructor(\n\t\tprivate readonly pool: Pool,\n\t\tpublic readonly eventManager: EventManager = new EventManager(null),\n\t) {\n\t}\n\n\tpublic static create(\n\t\t{ pool = {}, ...config }: DatabaseConfig & { pool?: Omit<PoolConfig, 'logError'> },\n\t\tlogError: (error: Error) => void,\n\t): Connection {\n\t\treturn new Connection(new Pool(createPgClientFactory(config), { ...pool, logError }))\n\t}\n\n\tpublic static createSingle(\n\t\tconfig: DatabaseConfig,\n\t\tlogError: (error: Error) => void,\n\t): Connection {\n\t\treturn new Connection(new Pool(createPgClientFactory(config), { maxConnections: 1, maxIdle: 0, logError }))\n\t}\n\n\tpublic createClient(schema: string, queryMeta: Record<string, any>): Client {\n\t\treturn new Client(this, schema, queryMeta, new EventManager(this.eventManager))\n\t}\n\n\tasync scope<Result>(\n\t\tcallback: (connection: Connection.AcquiredConnectionLike) => Promise<Result> | Result,\n\t\toptions: { eventManager?: EventManager } = {},\n\t): Promise<Result> {\n\t\tconst acquired = await this.pool.acquire()\n\t\tconst
|
|
1
|
+
{"version":3,"file":"Connection.cjs","sources":["../../../../../packages/database/src/client/Connection.ts"],"sourcesContent":["import { EventManager } from './EventManager'\nimport { Client } from './Client'\nimport { Pool, PoolConfig, PoolStatus } from './Pool'\nimport { createPgClientFactory } from '../utils'\nimport { DatabaseConfig } from '../types'\nimport { AcquiredConnection } from './AcquiredConnection'\nimport { Notification } from 'pg'\nimport { DatabaseError } from './errors'\nimport { ClientErrorCodes } from './errorCodes'\n\nclass Connection implements Connection.ConnectionLike, Connection.ClientFactory, Connection.PoolStatusProvider {\n\tconstructor(\n\t\tprivate readonly pool: Pool,\n\t\tpublic readonly eventManager: EventManager = new EventManager(null),\n\t\tprivate readonly options: Connection.Options = {},\n\t) {\n\t}\n\n\tpublic static create(\n\t\t{ pool = {}, ...config }: DatabaseConfig & { pool?: Omit<PoolConfig, 'logError'> },\n\t\tlogError: (error: Error) => void,\n\t\toptions: Connection.Options = {},\n\t): Connection {\n\t\treturn new Connection(new Pool(createPgClientFactory(config), { ...pool, logError }), new EventManager(null), options)\n\t}\n\n\tpublic static createSingle(\n\t\tconfig: DatabaseConfig,\n\t\tlogError: (error: Error) => void,\n\t): Connection {\n\t\treturn new Connection(new Pool(createPgClientFactory(config), { maxConnections: 1, maxIdle: 0, logError }))\n\t}\n\n\tpublic createClient(schema: string, queryMeta: Record<string, any>): Client {\n\t\treturn new Client(this, schema, queryMeta, new EventManager(this.eventManager))\n\t}\n\n\tasync scope<Result>(\n\t\tcallback: (connection: Connection.AcquiredConnectionLike) => Promise<Result> | Result,\n\t\toptions: { eventManager?: EventManager } = {},\n\t): Promise<Result> {\n\t\tconst eventManager = new EventManager(options.eventManager ?? this.eventManager)\n\t\t// Each retry disposes the stale connection it got, so the loop ends once the idle connections are used up.\n\t\twhile (true) {\n\t\t\tconst attempt = await this.scopeOnce(callback, eventManager)\n\t\t\tif (!attempt.stale) {\n\t\t\t\treturn attempt.result\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async scopeOnce<Result>(\n\t\tcallback: (connection: Connection.AcquiredConnectionLike) => Promise<Result> | Result,\n\t\teventManager: EventManager,\n\t): Promise<{ stale: false; result: Result } | { stale: true }> {\n\t\tconst acquired = await this.pool.acquire()\n\t\tconst reused = acquired.uses > 1\n\t\t// pg emits `error` only for a connection lost unexpectedly, not for its own end().\n\t\tlet lost = false\n\t\tlet answered = false\n\t\tconst onError = () => {\n\t\t\tlost = true\n\t\t}\n\t\tconst onAnswer = () => {\n\t\t\tanswered = true\n\t\t}\n\t\tacquired.client.on('error', onError)\n\t\teventManager.on(EventManager.Event.queryEnd, onAnswer)\n\t\ttry {\n\t\t\tconst connection = new AcquiredConnection(acquired.client, eventManager)\n\t\t\tconst result = await callback(connection)\n\t\t\tthis.pool.release(acquired)\n\n\t\t\treturn { stale: false, result }\n\t\t} catch (e) {\n\t\t\tthis.pool.dispose(acquired)\n\t\t\t// The server closed a pooled connection before answering anything, e.g. a pooler or replica restart.\n\t\t\tif (this.options.retryStaleConnection && reused && !answered && (lost || isConnectionLoss(e))) {\n\t\t\t\treturn { stale: true }\n\t\t\t}\n\t\t\tthrow e\n\t\t} finally {\n\t\t\tacquired.client.off('error', onError)\n\t\t\teventManager.removeListener(EventManager.Event.queryEnd, onAnswer)\n\t\t}\n\t}\n\n\tasync transaction<Result>(\n\t\tcallback: (connection: Connection.TransactionLike) => Promise<Result> | Result,\n\t\toptions: { eventManager?: EventManager } = {},\n\t): Promise<Result> {\n\t\treturn await this.scope(async connection => {\n\t\t\treturn await connection.transaction(callback)\n\t\t}, options)\n\t}\n\n\tasync query<Row extends Record<string, any>>(\n\t\tsql: string,\n\t\tparameters: any[] = [],\n\t\tmeta: Record<string, any> = {},\n\t): Promise<Connection.Result<Row>> {\n\t\treturn await this.scope(async connection => {\n\t\t\treturn await connection.query(sql, parameters, meta)\n\t\t})\n\t}\n\n\tasync end(): Promise<void> {\n\t\tawait this.pool.end()\n\t}\n\n\tpublic async clearPool(): Promise<void> {\n\t\tawait this.pool.closeIdle()\n\t}\n\n\tgetPoolStatus(): PoolStatus {\n\t\treturn this.pool.getPoolStatus()\n\t}\n}\n\nconst connectionLossCodes = new Set<string>([\n\tClientErrorCodes.CONNECTION_EXCEPTION,\n\tClientErrorCodes.CONNECTION_DOES_NOT_EXIST,\n\tClientErrorCodes.CONNECTION_FAILURE,\n\tClientErrorCodes.ADMIN_SHUTDOWN,\n\tClientErrorCodes.CRASH_SHUTDOWN,\n])\n\nconst isConnectionLoss = (error: unknown): boolean =>\n\terror instanceof DatabaseError && error.code !== undefined && connectionLossCodes.has(error.code)\n\nnamespace Connection {\n\texport interface Options {\n\t\t/**\n\t\t * Runs a scope again on another connection when a connection taken from the pool turns out to be closed by\n\t\t * the server before it answered anything. Only for connections whose scopes may run twice, such as a read replica.\n\t\t */\n\t\tretryStaleConnection?: boolean\n\t}\n\n\texport interface Queryable {\n\t\treadonly eventManager: EventManager\n\n\t\tquery<Row extends Record<string, any>>(\n\t\t\tsql: string,\n\t\t\tparameters?: readonly any[],\n\t\t\tmeta?: Record<string, any>,\n\t\t): Promise<Connection.Result<Row>>\n\t}\n\n\texport interface Transactional {\n\t\ttransaction<Result>(\n\t\t\ttrx: (connection: TransactionLike) => Promise<Result> | Result,\n\t\t\toptions?: { eventManager?: EventManager },\n\t\t): Promise<Result>\n\t}\n\n\texport type ConnectionType =\n\t\t& Connection.ConnectionLike\n\t\t& Connection.ClientFactory\n\t\t& Connection.PoolStatusProvider\n\n\texport interface ConnectionLike<T = {}> extends Transactional, Queryable {\n\t\tscope<Result>(\n\t\t\tcallback: (connection: T & AcquiredConnectionLike) => Promise<Result> | Result,\n\t\t\toptions?: { eventManager?: EventManager },\n\t\t): Promise<Result>\n\t}\n\n\texport interface AcquiredConnectionLike<T = {}> extends ConnectionLike<T> {\n\t\ton(event: 'notification', listener: (message: Notification) => void): () => void\n\t\ton(event: 'end', listener: () => void): () => void\n\t\ton(event: 'error', listener: (error: any) => void): () => void\n\t}\n\n\texport interface ClientFactory {\n\t\tcreateClient(schema: string, queryMeta: Record<string, any>): Client\n\t}\n\n\texport interface PoolStatusProvider {\n\t\tgetPoolStatus(): PoolStatus | undefined\n\t}\n\n\texport interface TransactionLike extends AcquiredConnectionLike<TransactionLike> {\n\t\treadonly isClosed: boolean\n\n\t\trollback(): Promise<void>\n\n\t\tcommit(): Promise<void>\n\t}\n\n\texport interface Query {\n\t\treadonly sql: string\n\t\treadonly parameters: any[]\n\t\treadonly meta: Record<string, any>\n\t}\n\n\texport interface Result<Row extends Record<string, any> = Record<string, any>> {\n\t\treadonly command?: string\n\t\treadonly rowCount: number | null\n\t\treadonly rows: Row[]\n\t\treadonly timing?: {\n\t\t\tselfDuration: number\n\t\t\t/** @deprecated both selfDuration and totalDuration now contains same number */\n\t\t\ttotalDuration: number\n\t\t}\n\t}\n\n\texport const REPEATABLE_READ = 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ'\n\n}\n\nexport { Connection }\n"],"names":["EventManager","Pool","createPgClientFactory","Client","AcquiredConnection","ClientErrorCodes","DatabaseError","Connection"],"mappings":"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"}
|
|
@@ -2,17 +2,20 @@ import { EventManager } from "./EventManager.js";
|
|
|
2
2
|
import { Client } from "./Client.js";
|
|
3
3
|
import { Pool } from "./Pool.js";
|
|
4
4
|
import "../Literal.js";
|
|
5
|
+
import { ClientErrorCodes } from "./errorCodes.js";
|
|
6
|
+
import { DatabaseError } from "./errors.js";
|
|
5
7
|
import "node:timers";
|
|
6
8
|
import "../utils/Mutex.js";
|
|
7
9
|
import { createPgClientFactory } from "../utils/pgClientFactory.js";
|
|
8
10
|
import { AcquiredConnection } from "./AcquiredConnection.js";
|
|
9
11
|
class Connection {
|
|
10
|
-
constructor(pool, eventManager = new EventManager(null)) {
|
|
12
|
+
constructor(pool, eventManager = new EventManager(null), options = {}) {
|
|
11
13
|
this.pool = pool;
|
|
12
14
|
this.eventManager = eventManager;
|
|
15
|
+
this.options = options;
|
|
13
16
|
}
|
|
14
|
-
static create({ pool = {}, ...config }, logError) {
|
|
15
|
-
return new Connection(new Pool(createPgClientFactory(config), { ...pool, logError }));
|
|
17
|
+
static create({ pool = {}, ...config }, logError, options = {}) {
|
|
18
|
+
return new Connection(new Pool(createPgClientFactory(config), { ...pool, logError }), new EventManager(null), options);
|
|
16
19
|
}
|
|
17
20
|
static createSingle(config, logError) {
|
|
18
21
|
return new Connection(new Pool(createPgClientFactory(config), { maxConnections: 1, maxIdle: 0, logError }));
|
|
@@ -21,16 +24,41 @@ class Connection {
|
|
|
21
24
|
return new Client(this, schema, queryMeta, new EventManager(this.eventManager));
|
|
22
25
|
}
|
|
23
26
|
async scope(callback, options = {}) {
|
|
24
|
-
const acquired = await this.pool.acquire();
|
|
25
27
|
const eventManager = new EventManager(options.eventManager ?? this.eventManager);
|
|
28
|
+
while (true) {
|
|
29
|
+
const attempt = await this.scopeOnce(callback, eventManager);
|
|
30
|
+
if (!attempt.stale) {
|
|
31
|
+
return attempt.result;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async scopeOnce(callback, eventManager) {
|
|
36
|
+
const acquired = await this.pool.acquire();
|
|
37
|
+
const reused = acquired.uses > 1;
|
|
38
|
+
let lost = false;
|
|
39
|
+
let answered = false;
|
|
40
|
+
const onError = () => {
|
|
41
|
+
lost = true;
|
|
42
|
+
};
|
|
43
|
+
const onAnswer = () => {
|
|
44
|
+
answered = true;
|
|
45
|
+
};
|
|
46
|
+
acquired.client.on("error", onError);
|
|
47
|
+
eventManager.on(EventManager.Event.queryEnd, onAnswer);
|
|
26
48
|
try {
|
|
27
49
|
const connection = new AcquiredConnection(acquired.client, eventManager);
|
|
28
50
|
const result = await callback(connection);
|
|
29
51
|
this.pool.release(acquired);
|
|
30
|
-
return result;
|
|
52
|
+
return { stale: false, result };
|
|
31
53
|
} catch (e) {
|
|
32
54
|
this.pool.dispose(acquired);
|
|
55
|
+
if (this.options.retryStaleConnection && reused && !answered && (lost || isConnectionLoss(e))) {
|
|
56
|
+
return { stale: true };
|
|
57
|
+
}
|
|
33
58
|
throw e;
|
|
59
|
+
} finally {
|
|
60
|
+
acquired.client.off("error", onError);
|
|
61
|
+
eventManager.removeListener(EventManager.Event.queryEnd, onAnswer);
|
|
34
62
|
}
|
|
35
63
|
}
|
|
36
64
|
async transaction(callback, options = {}) {
|
|
@@ -53,6 +81,14 @@ class Connection {
|
|
|
53
81
|
return this.pool.getPoolStatus();
|
|
54
82
|
}
|
|
55
83
|
}
|
|
84
|
+
const connectionLossCodes = /* @__PURE__ */ new Set([
|
|
85
|
+
ClientErrorCodes.CONNECTION_EXCEPTION,
|
|
86
|
+
ClientErrorCodes.CONNECTION_DOES_NOT_EXIST,
|
|
87
|
+
ClientErrorCodes.CONNECTION_FAILURE,
|
|
88
|
+
ClientErrorCodes.ADMIN_SHUTDOWN,
|
|
89
|
+
ClientErrorCodes.CRASH_SHUTDOWN
|
|
90
|
+
]);
|
|
91
|
+
const isConnectionLoss = (error) => error instanceof DatabaseError && error.code !== void 0 && connectionLossCodes.has(error.code);
|
|
56
92
|
((Connection2) => {
|
|
57
93
|
Connection2.REPEATABLE_READ = "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ";
|
|
58
94
|
})(Connection || (Connection = {}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Connection.js","sources":["../../../../../packages/database/src/client/Connection.ts"],"sourcesContent":["import { EventManager } from './EventManager'\nimport { Client } from './Client'\nimport { Pool, PoolConfig, PoolStatus } from './Pool'\nimport { createPgClientFactory } from '../utils'\nimport { DatabaseConfig } from '../types'\nimport { AcquiredConnection } from './AcquiredConnection'\nimport { Notification } from 'pg'\n\nclass Connection implements Connection.ConnectionLike, Connection.ClientFactory, Connection.PoolStatusProvider {\n\tconstructor(\n\t\tprivate readonly pool: Pool,\n\t\tpublic readonly eventManager: EventManager = new EventManager(null),\n\t) {\n\t}\n\n\tpublic static create(\n\t\t{ pool = {}, ...config }: DatabaseConfig & { pool?: Omit<PoolConfig, 'logError'> },\n\t\tlogError: (error: Error) => void,\n\t): Connection {\n\t\treturn new Connection(new Pool(createPgClientFactory(config), { ...pool, logError }))\n\t}\n\n\tpublic static createSingle(\n\t\tconfig: DatabaseConfig,\n\t\tlogError: (error: Error) => void,\n\t): Connection {\n\t\treturn new Connection(new Pool(createPgClientFactory(config), { maxConnections: 1, maxIdle: 0, logError }))\n\t}\n\n\tpublic createClient(schema: string, queryMeta: Record<string, any>): Client {\n\t\treturn new Client(this, schema, queryMeta, new EventManager(this.eventManager))\n\t}\n\n\tasync scope<Result>(\n\t\tcallback: (connection: Connection.AcquiredConnectionLike) => Promise<Result> | Result,\n\t\toptions: { eventManager?: EventManager } = {},\n\t): Promise<Result> {\n\t\tconst acquired = await this.pool.acquire()\n\t\tconst
|
|
1
|
+
{"version":3,"file":"Connection.js","sources":["../../../../../packages/database/src/client/Connection.ts"],"sourcesContent":["import { EventManager } from './EventManager'\nimport { Client } from './Client'\nimport { Pool, PoolConfig, PoolStatus } from './Pool'\nimport { createPgClientFactory } from '../utils'\nimport { DatabaseConfig } from '../types'\nimport { AcquiredConnection } from './AcquiredConnection'\nimport { Notification } from 'pg'\nimport { DatabaseError } from './errors'\nimport { ClientErrorCodes } from './errorCodes'\n\nclass Connection implements Connection.ConnectionLike, Connection.ClientFactory, Connection.PoolStatusProvider {\n\tconstructor(\n\t\tprivate readonly pool: Pool,\n\t\tpublic readonly eventManager: EventManager = new EventManager(null),\n\t\tprivate readonly options: Connection.Options = {},\n\t) {\n\t}\n\n\tpublic static create(\n\t\t{ pool = {}, ...config }: DatabaseConfig & { pool?: Omit<PoolConfig, 'logError'> },\n\t\tlogError: (error: Error) => void,\n\t\toptions: Connection.Options = {},\n\t): Connection {\n\t\treturn new Connection(new Pool(createPgClientFactory(config), { ...pool, logError }), new EventManager(null), options)\n\t}\n\n\tpublic static createSingle(\n\t\tconfig: DatabaseConfig,\n\t\tlogError: (error: Error) => void,\n\t): Connection {\n\t\treturn new Connection(new Pool(createPgClientFactory(config), { maxConnections: 1, maxIdle: 0, logError }))\n\t}\n\n\tpublic createClient(schema: string, queryMeta: Record<string, any>): Client {\n\t\treturn new Client(this, schema, queryMeta, new EventManager(this.eventManager))\n\t}\n\n\tasync scope<Result>(\n\t\tcallback: (connection: Connection.AcquiredConnectionLike) => Promise<Result> | Result,\n\t\toptions: { eventManager?: EventManager } = {},\n\t): Promise<Result> {\n\t\tconst eventManager = new EventManager(options.eventManager ?? this.eventManager)\n\t\t// Each retry disposes the stale connection it got, so the loop ends once the idle connections are used up.\n\t\twhile (true) {\n\t\t\tconst attempt = await this.scopeOnce(callback, eventManager)\n\t\t\tif (!attempt.stale) {\n\t\t\t\treturn attempt.result\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async scopeOnce<Result>(\n\t\tcallback: (connection: Connection.AcquiredConnectionLike) => Promise<Result> | Result,\n\t\teventManager: EventManager,\n\t): Promise<{ stale: false; result: Result } | { stale: true }> {\n\t\tconst acquired = await this.pool.acquire()\n\t\tconst reused = acquired.uses > 1\n\t\t// pg emits `error` only for a connection lost unexpectedly, not for its own end().\n\t\tlet lost = false\n\t\tlet answered = false\n\t\tconst onError = () => {\n\t\t\tlost = true\n\t\t}\n\t\tconst onAnswer = () => {\n\t\t\tanswered = true\n\t\t}\n\t\tacquired.client.on('error', onError)\n\t\teventManager.on(EventManager.Event.queryEnd, onAnswer)\n\t\ttry {\n\t\t\tconst connection = new AcquiredConnection(acquired.client, eventManager)\n\t\t\tconst result = await callback(connection)\n\t\t\tthis.pool.release(acquired)\n\n\t\t\treturn { stale: false, result }\n\t\t} catch (e) {\n\t\t\tthis.pool.dispose(acquired)\n\t\t\t// The server closed a pooled connection before answering anything, e.g. a pooler or replica restart.\n\t\t\tif (this.options.retryStaleConnection && reused && !answered && (lost || isConnectionLoss(e))) {\n\t\t\t\treturn { stale: true }\n\t\t\t}\n\t\t\tthrow e\n\t\t} finally {\n\t\t\tacquired.client.off('error', onError)\n\t\t\teventManager.removeListener(EventManager.Event.queryEnd, onAnswer)\n\t\t}\n\t}\n\n\tasync transaction<Result>(\n\t\tcallback: (connection: Connection.TransactionLike) => Promise<Result> | Result,\n\t\toptions: { eventManager?: EventManager } = {},\n\t): Promise<Result> {\n\t\treturn await this.scope(async connection => {\n\t\t\treturn await connection.transaction(callback)\n\t\t}, options)\n\t}\n\n\tasync query<Row extends Record<string, any>>(\n\t\tsql: string,\n\t\tparameters: any[] = [],\n\t\tmeta: Record<string, any> = {},\n\t): Promise<Connection.Result<Row>> {\n\t\treturn await this.scope(async connection => {\n\t\t\treturn await connection.query(sql, parameters, meta)\n\t\t})\n\t}\n\n\tasync end(): Promise<void> {\n\t\tawait this.pool.end()\n\t}\n\n\tpublic async clearPool(): Promise<void> {\n\t\tawait this.pool.closeIdle()\n\t}\n\n\tgetPoolStatus(): PoolStatus {\n\t\treturn this.pool.getPoolStatus()\n\t}\n}\n\nconst connectionLossCodes = new Set<string>([\n\tClientErrorCodes.CONNECTION_EXCEPTION,\n\tClientErrorCodes.CONNECTION_DOES_NOT_EXIST,\n\tClientErrorCodes.CONNECTION_FAILURE,\n\tClientErrorCodes.ADMIN_SHUTDOWN,\n\tClientErrorCodes.CRASH_SHUTDOWN,\n])\n\nconst isConnectionLoss = (error: unknown): boolean =>\n\terror instanceof DatabaseError && error.code !== undefined && connectionLossCodes.has(error.code)\n\nnamespace Connection {\n\texport interface Options {\n\t\t/**\n\t\t * Runs a scope again on another connection when a connection taken from the pool turns out to be closed by\n\t\t * the server before it answered anything. Only for connections whose scopes may run twice, such as a read replica.\n\t\t */\n\t\tretryStaleConnection?: boolean\n\t}\n\n\texport interface Queryable {\n\t\treadonly eventManager: EventManager\n\n\t\tquery<Row extends Record<string, any>>(\n\t\t\tsql: string,\n\t\t\tparameters?: readonly any[],\n\t\t\tmeta?: Record<string, any>,\n\t\t): Promise<Connection.Result<Row>>\n\t}\n\n\texport interface Transactional {\n\t\ttransaction<Result>(\n\t\t\ttrx: (connection: TransactionLike) => Promise<Result> | Result,\n\t\t\toptions?: { eventManager?: EventManager },\n\t\t): Promise<Result>\n\t}\n\n\texport type ConnectionType =\n\t\t& Connection.ConnectionLike\n\t\t& Connection.ClientFactory\n\t\t& Connection.PoolStatusProvider\n\n\texport interface ConnectionLike<T = {}> extends Transactional, Queryable {\n\t\tscope<Result>(\n\t\t\tcallback: (connection: T & AcquiredConnectionLike) => Promise<Result> | Result,\n\t\t\toptions?: { eventManager?: EventManager },\n\t\t): Promise<Result>\n\t}\n\n\texport interface AcquiredConnectionLike<T = {}> extends ConnectionLike<T> {\n\t\ton(event: 'notification', listener: (message: Notification) => void): () => void\n\t\ton(event: 'end', listener: () => void): () => void\n\t\ton(event: 'error', listener: (error: any) => void): () => void\n\t}\n\n\texport interface ClientFactory {\n\t\tcreateClient(schema: string, queryMeta: Record<string, any>): Client\n\t}\n\n\texport interface PoolStatusProvider {\n\t\tgetPoolStatus(): PoolStatus | undefined\n\t}\n\n\texport interface TransactionLike extends AcquiredConnectionLike<TransactionLike> {\n\t\treadonly isClosed: boolean\n\n\t\trollback(): Promise<void>\n\n\t\tcommit(): Promise<void>\n\t}\n\n\texport interface Query {\n\t\treadonly sql: string\n\t\treadonly parameters: any[]\n\t\treadonly meta: Record<string, any>\n\t}\n\n\texport interface Result<Row extends Record<string, any> = Record<string, any>> {\n\t\treadonly command?: string\n\t\treadonly rowCount: number | null\n\t\treadonly rows: Row[]\n\t\treadonly timing?: {\n\t\t\tselfDuration: number\n\t\t\t/** @deprecated both selfDuration and totalDuration now contains same number */\n\t\t\ttotalDuration: number\n\t\t}\n\t}\n\n\texport const REPEATABLE_READ = 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ'\n\n}\n\nexport { Connection }\n"],"names":["Connection"],"mappings":"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"}
|
|
@@ -26,7 +26,7 @@ class LimitByGroupWrapper {
|
|
|
26
26
|
if (limit !== void 0) {
|
|
27
27
|
wrapperQb = wrapperQb.where((expr) => expr.compare(["data", "rowNumber_"], ConditionBuilder.Operator.lte, start + limit));
|
|
28
28
|
}
|
|
29
|
-
return await wrapperQb.getResult(db);
|
|
29
|
+
return await wrapperQb.orderBy(["data", "rowNumber_"]).getResult(db);
|
|
30
30
|
} else if (this.orderByCallback) {
|
|
31
31
|
return await this.orderByCallback(null, qb)[1].getResult(db);
|
|
32
32
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LimitByGroupWrapper.cjs","sources":["../../../../../packages/database/src/builders/LimitByGroupWrapper.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { Operator } from './ConditionBuilder'\nimport { Client } from '../client'\nimport { WindowFunction } from './WindowFunction'\n\ntype OrderByCallback = <Orderable extends QueryBuilder.Orderable<any> | null>(\n\torderable: Orderable,\n\tqb: SelectBuilder<any>,\n) => [Orderable, SelectBuilder<any>]\n\nclass LimitByGroupWrapper {\n\tconstructor(\n\t\tprivate readonly groupBy: QueryBuilder.ColumnIdentifier,\n\t\tprivate readonly orderByCallback: OrderByCallback | undefined,\n\t\tprivate readonly skip: number | undefined,\n\t\tprivate readonly limit: number | undefined,\n\t) {}\n\n\tpublic async getResult<R>(qb: SelectBuilder<R>, db: Client): Promise<R[]> {\n\t\tif (this.limit !== undefined || this.skip !== undefined) {\n\t\t\tlet window = WindowFunction.createEmpty().rowNumber().partitionBy(this.groupBy)\n\t\t\tif (this.orderByCallback !== undefined) {\n\t\t\t\t[window, qb] = this.orderByCallback(window, qb)\n\t\t\t}\n\n\t\t\tqb = qb.select(window.compile(), 'rowNumber_')\n\n\t\t\tlet wrapperQb: SelectBuilder<R> = SelectBuilder.create<R>()\n\t\t\t\t.meta(qb.options.meta)\n\t\t\t\t.with('data', qb)\n\t\t\t\t.from('data')\n\t\t\t\t.select(['data', '*'])\n\n\t\t\tconst start = this.skip || 0\n\t\t\tif (start > 0) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.gt, start))\n\t\t\t}\n\n\t\t\tconst limit = this.limit\n\t\t\tif (limit !== undefined) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.lte, start + limit))\n\t\t\t}\n\n\t\t\treturn await wrapperQb.getResult(db)\n\t\t} else if (this.orderByCallback) {\n\t\t\treturn await this.orderByCallback(null, qb)[1].getResult(db)\n\t\t}\n\n\t\treturn await qb.getResult(db)\n\t}\n}\n\nexport { LimitByGroupWrapper }\n"],"names":["WindowFunction","SelectBuilder","Operator"],"mappings":";;;;;AAWA,MAAM,oBAAoB;AAAA,EACzB,YACkB,SACA,iBACA,MACA,OAChB;AAJgB,SAAA,UAAA;AACA,SAAA,kBAAA;AACA,SAAA,OAAA;AACA,SAAA,QAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,UAAa,IAAsB,IAA0B;AACzE,QAAI,KAAK,UAAU,UAAa,KAAK,SAAS,QAAW;AACpD,UAAA,SAASA,8BAAe,YAAY,EAAE,YAAY,YAAY,KAAK,OAAO;AAC1E,UAAA,KAAK,oBAAoB,QAAW;AACvC,SAAC,QAAQ,EAAE,IAAI,KAAK,gBAAgB,QAAQ,EAAE;AAAA,MAAA;AAG/C,WAAK,GAAG,OAAO,OAAO,QAAA,GAAW,YAAY;AAEzC,UAAA,YAA8BC,4BAAc,OAAU,EACxD,KAAK,GAAG,QAAQ,IAAI,EACpB,KAAK,QAAQ,EAAE,EACf,KAAK,MAAM,EACX,OAAO,CAAC,QAAQ,GAAG,CAAC;AAEhB,YAAA,QAAQ,KAAK,QAAQ;AAC3B,UAAI,QAAQ,GAAG;AACd,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAGC,iBAAA,SAAS,IAAI,KAAK,CAAC;AAAA,MAAA;AAG7F,YAAM,QAAQ,KAAK;AACnB,UAAI,UAAU,QAAW;AACxB,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAGA,iBAAA,SAAS,KAAK,QAAQ,KAAK,CAAC;AAAA,MAAA;AAG/F,aAAA,MAAM,UAAU,UAAU,EAAE;AAAA,IAAA,
|
|
1
|
+
{"version":3,"file":"LimitByGroupWrapper.cjs","sources":["../../../../../packages/database/src/builders/LimitByGroupWrapper.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { Operator } from './ConditionBuilder'\nimport { Client } from '../client'\nimport { WindowFunction } from './WindowFunction'\n\ntype OrderByCallback = <Orderable extends QueryBuilder.Orderable<any> | null>(\n\torderable: Orderable,\n\tqb: SelectBuilder<any>,\n) => [Orderable, SelectBuilder<any>]\n\nclass LimitByGroupWrapper {\n\tconstructor(\n\t\tprivate readonly groupBy: QueryBuilder.ColumnIdentifier,\n\t\tprivate readonly orderByCallback: OrderByCallback | undefined,\n\t\tprivate readonly skip: number | undefined,\n\t\tprivate readonly limit: number | undefined,\n\t) {}\n\n\tpublic async getResult<R>(qb: SelectBuilder<R>, db: Client): Promise<R[]> {\n\t\tif (this.limit !== undefined || this.skip !== undefined) {\n\t\t\tlet window = WindowFunction.createEmpty().rowNumber().partitionBy(this.groupBy)\n\t\t\tif (this.orderByCallback !== undefined) {\n\t\t\t\t[window, qb] = this.orderByCallback(window, qb)\n\t\t\t}\n\n\t\t\tqb = qb.select(window.compile(), 'rowNumber_')\n\n\t\t\tlet wrapperQb: SelectBuilder<R> = SelectBuilder.create<R>()\n\t\t\t\t.meta(qb.options.meta)\n\t\t\t\t.with('data', qb)\n\t\t\t\t.from('data')\n\t\t\t\t.select(['data', '*'])\n\n\t\t\tconst start = this.skip || 0\n\t\t\tif (start > 0) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.gt, start))\n\t\t\t}\n\n\t\t\tconst limit = this.limit\n\t\t\tif (limit !== undefined) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.lte, start + limit))\n\t\t\t}\n\n\t\t\treturn await wrapperQb.orderBy(['data', 'rowNumber_']).getResult(db)\n\t\t} else if (this.orderByCallback) {\n\t\t\treturn await this.orderByCallback(null, qb)[1].getResult(db)\n\t\t}\n\n\t\treturn await qb.getResult(db)\n\t}\n}\n\nexport { LimitByGroupWrapper }\n"],"names":["WindowFunction","SelectBuilder","Operator"],"mappings":";;;;;AAWA,MAAM,oBAAoB;AAAA,EACzB,YACkB,SACA,iBACA,MACA,OAChB;AAJgB,SAAA,UAAA;AACA,SAAA,kBAAA;AACA,SAAA,OAAA;AACA,SAAA,QAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,UAAa,IAAsB,IAA0B;AACzE,QAAI,KAAK,UAAU,UAAa,KAAK,SAAS,QAAW;AACpD,UAAA,SAASA,8BAAe,YAAY,EAAE,YAAY,YAAY,KAAK,OAAO;AAC1E,UAAA,KAAK,oBAAoB,QAAW;AACvC,SAAC,QAAQ,EAAE,IAAI,KAAK,gBAAgB,QAAQ,EAAE;AAAA,MAAA;AAG/C,WAAK,GAAG,OAAO,OAAO,QAAA,GAAW,YAAY;AAEzC,UAAA,YAA8BC,4BAAc,OAAU,EACxD,KAAK,GAAG,QAAQ,IAAI,EACpB,KAAK,QAAQ,EAAE,EACf,KAAK,MAAM,EACX,OAAO,CAAC,QAAQ,GAAG,CAAC;AAEhB,YAAA,QAAQ,KAAK,QAAQ;AAC3B,UAAI,QAAQ,GAAG;AACd,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAGC,iBAAA,SAAS,IAAI,KAAK,CAAC;AAAA,MAAA;AAG7F,YAAM,QAAQ,KAAK;AACnB,UAAI,UAAU,QAAW;AACxB,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAGA,iBAAA,SAAS,KAAK,QAAQ,KAAK,CAAC;AAAA,MAAA;AAG/F,aAAA,MAAM,UAAU,QAAQ,CAAC,QAAQ,YAAY,CAAC,EAAE,UAAU,EAAE;AAAA,IAAA,WACzD,KAAK,iBAAiB;AACzB,aAAA,MAAM,KAAK,gBAAgB,MAAM,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE;AAAA,IAAA;AAGrD,WAAA,MAAM,GAAG,UAAU,EAAE;AAAA,EAAA;AAE9B;;"}
|
|
@@ -24,7 +24,7 @@ class LimitByGroupWrapper {
|
|
|
24
24
|
if (limit !== void 0) {
|
|
25
25
|
wrapperQb = wrapperQb.where((expr) => expr.compare(["data", "rowNumber_"], Operator.lte, start + limit));
|
|
26
26
|
}
|
|
27
|
-
return await wrapperQb.getResult(db);
|
|
27
|
+
return await wrapperQb.orderBy(["data", "rowNumber_"]).getResult(db);
|
|
28
28
|
} else if (this.orderByCallback) {
|
|
29
29
|
return await this.orderByCallback(null, qb)[1].getResult(db);
|
|
30
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LimitByGroupWrapper.js","sources":["../../../../../packages/database/src/builders/LimitByGroupWrapper.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { Operator } from './ConditionBuilder'\nimport { Client } from '../client'\nimport { WindowFunction } from './WindowFunction'\n\ntype OrderByCallback = <Orderable extends QueryBuilder.Orderable<any> | null>(\n\torderable: Orderable,\n\tqb: SelectBuilder<any>,\n) => [Orderable, SelectBuilder<any>]\n\nclass LimitByGroupWrapper {\n\tconstructor(\n\t\tprivate readonly groupBy: QueryBuilder.ColumnIdentifier,\n\t\tprivate readonly orderByCallback: OrderByCallback | undefined,\n\t\tprivate readonly skip: number | undefined,\n\t\tprivate readonly limit: number | undefined,\n\t) {}\n\n\tpublic async getResult<R>(qb: SelectBuilder<R>, db: Client): Promise<R[]> {\n\t\tif (this.limit !== undefined || this.skip !== undefined) {\n\t\t\tlet window = WindowFunction.createEmpty().rowNumber().partitionBy(this.groupBy)\n\t\t\tif (this.orderByCallback !== undefined) {\n\t\t\t\t[window, qb] = this.orderByCallback(window, qb)\n\t\t\t}\n\n\t\t\tqb = qb.select(window.compile(), 'rowNumber_')\n\n\t\t\tlet wrapperQb: SelectBuilder<R> = SelectBuilder.create<R>()\n\t\t\t\t.meta(qb.options.meta)\n\t\t\t\t.with('data', qb)\n\t\t\t\t.from('data')\n\t\t\t\t.select(['data', '*'])\n\n\t\t\tconst start = this.skip || 0\n\t\t\tif (start > 0) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.gt, start))\n\t\t\t}\n\n\t\t\tconst limit = this.limit\n\t\t\tif (limit !== undefined) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.lte, start + limit))\n\t\t\t}\n\n\t\t\treturn await wrapperQb.getResult(db)\n\t\t} else if (this.orderByCallback) {\n\t\t\treturn await this.orderByCallback(null, qb)[1].getResult(db)\n\t\t}\n\n\t\treturn await qb.getResult(db)\n\t}\n}\n\nexport { LimitByGroupWrapper }\n"],"names":[],"mappings":";;;AAWA,MAAM,oBAAoB;AAAA,EACzB,YACkB,SACA,iBACA,MACA,OAChB;AAJgB,SAAA,UAAA;AACA,SAAA,kBAAA;AACA,SAAA,OAAA;AACA,SAAA,QAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,UAAa,IAAsB,IAA0B;AACzE,QAAI,KAAK,UAAU,UAAa,KAAK,SAAS,QAAW;AACpD,UAAA,SAAS,eAAe,YAAY,EAAE,YAAY,YAAY,KAAK,OAAO;AAC1E,UAAA,KAAK,oBAAoB,QAAW;AACvC,SAAC,QAAQ,EAAE,IAAI,KAAK,gBAAgB,QAAQ,EAAE;AAAA,MAAA;AAG/C,WAAK,GAAG,OAAO,OAAO,QAAA,GAAW,YAAY;AAEzC,UAAA,YAA8B,cAAc,OAAU,EACxD,KAAK,GAAG,QAAQ,IAAI,EACpB,KAAK,QAAQ,EAAE,EACf,KAAK,MAAM,EACX,OAAO,CAAC,QAAQ,GAAG,CAAC;AAEhB,YAAA,QAAQ,KAAK,QAAQ;AAC3B,UAAI,QAAQ,GAAG;AACd,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAG,SAAS,IAAI,KAAK,CAAC;AAAA,MAAA;AAG7F,YAAM,QAAQ,KAAK;AACnB,UAAI,UAAU,QAAW;AACxB,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAG,SAAS,KAAK,QAAQ,KAAK,CAAC;AAAA,MAAA;AAG/F,aAAA,MAAM,UAAU,UAAU,EAAE;AAAA,IAAA,
|
|
1
|
+
{"version":3,"file":"LimitByGroupWrapper.js","sources":["../../../../../packages/database/src/builders/LimitByGroupWrapper.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { Operator } from './ConditionBuilder'\nimport { Client } from '../client'\nimport { WindowFunction } from './WindowFunction'\n\ntype OrderByCallback = <Orderable extends QueryBuilder.Orderable<any> | null>(\n\torderable: Orderable,\n\tqb: SelectBuilder<any>,\n) => [Orderable, SelectBuilder<any>]\n\nclass LimitByGroupWrapper {\n\tconstructor(\n\t\tprivate readonly groupBy: QueryBuilder.ColumnIdentifier,\n\t\tprivate readonly orderByCallback: OrderByCallback | undefined,\n\t\tprivate readonly skip: number | undefined,\n\t\tprivate readonly limit: number | undefined,\n\t) {}\n\n\tpublic async getResult<R>(qb: SelectBuilder<R>, db: Client): Promise<R[]> {\n\t\tif (this.limit !== undefined || this.skip !== undefined) {\n\t\t\tlet window = WindowFunction.createEmpty().rowNumber().partitionBy(this.groupBy)\n\t\t\tif (this.orderByCallback !== undefined) {\n\t\t\t\t[window, qb] = this.orderByCallback(window, qb)\n\t\t\t}\n\n\t\t\tqb = qb.select(window.compile(), 'rowNumber_')\n\n\t\t\tlet wrapperQb: SelectBuilder<R> = SelectBuilder.create<R>()\n\t\t\t\t.meta(qb.options.meta)\n\t\t\t\t.with('data', qb)\n\t\t\t\t.from('data')\n\t\t\t\t.select(['data', '*'])\n\n\t\t\tconst start = this.skip || 0\n\t\t\tif (start > 0) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.gt, start))\n\t\t\t}\n\n\t\t\tconst limit = this.limit\n\t\t\tif (limit !== undefined) {\n\t\t\t\twrapperQb = wrapperQb.where(expr => expr.compare(['data', 'rowNumber_'], Operator.lte, start + limit))\n\t\t\t}\n\n\t\t\treturn await wrapperQb.orderBy(['data', 'rowNumber_']).getResult(db)\n\t\t} else if (this.orderByCallback) {\n\t\t\treturn await this.orderByCallback(null, qb)[1].getResult(db)\n\t\t}\n\n\t\treturn await qb.getResult(db)\n\t}\n}\n\nexport { LimitByGroupWrapper }\n"],"names":[],"mappings":";;;AAWA,MAAM,oBAAoB;AAAA,EACzB,YACkB,SACA,iBACA,MACA,OAChB;AAJgB,SAAA,UAAA;AACA,SAAA,kBAAA;AACA,SAAA,OAAA;AACA,SAAA,QAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,UAAa,IAAsB,IAA0B;AACzE,QAAI,KAAK,UAAU,UAAa,KAAK,SAAS,QAAW;AACpD,UAAA,SAAS,eAAe,YAAY,EAAE,YAAY,YAAY,KAAK,OAAO;AAC1E,UAAA,KAAK,oBAAoB,QAAW;AACvC,SAAC,QAAQ,EAAE,IAAI,KAAK,gBAAgB,QAAQ,EAAE;AAAA,MAAA;AAG/C,WAAK,GAAG,OAAO,OAAO,QAAA,GAAW,YAAY;AAEzC,UAAA,YAA8B,cAAc,OAAU,EACxD,KAAK,GAAG,QAAQ,IAAI,EACpB,KAAK,QAAQ,EAAE,EACf,KAAK,MAAM,EACX,OAAO,CAAC,QAAQ,GAAG,CAAC;AAEhB,YAAA,QAAQ,KAAK,QAAQ;AAC3B,UAAI,QAAQ,GAAG;AACd,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAG,SAAS,IAAI,KAAK,CAAC;AAAA,MAAA;AAG7F,YAAM,QAAQ,KAAK;AACnB,UAAI,UAAU,QAAW;AACxB,oBAAY,UAAU,MAAM,CAAQ,SAAA,KAAK,QAAQ,CAAC,QAAQ,YAAY,GAAG,SAAS,KAAK,QAAQ,KAAK,CAAC;AAAA,MAAA;AAG/F,aAAA,MAAM,UAAU,QAAQ,CAAC,QAAQ,YAAY,CAAC,EAAE,UAAU,EAAE;AAAA,IAAA,WACzD,KAAK,iBAAiB;AACzB,aAAA,MAAM,KAAK,gBAAgB,MAAM,EAAE,EAAE,CAAC,EAAE,UAAU,EAAE;AAAA,IAAA;AAGrD,WAAA,MAAM,GAAG,UAAU,EAAE;AAAA,EAAA;AAE9B;"}
|
|
@@ -26,7 +26,7 @@ class WindowFunction {
|
|
|
26
26
|
const raw = expression instanceof Literal.Literal ? expression : new Literal.Literal(formatUtils.toFqnWrap(expression));
|
|
27
27
|
return new WindowFunction(this.windowFunction, this.partitionByExpr, [
|
|
28
28
|
...this.orderByColumns,
|
|
29
|
-
raw.appendString(direction
|
|
29
|
+
raw.appendString(` ${direction}`)
|
|
30
30
|
]);
|
|
31
31
|
}
|
|
32
32
|
compile() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WindowFunction.cjs","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral, toFqnWrap } from './utils'\n\nclass WindowFunction<HasFunction extends boolean> implements QueryBuilder.Orderable<WindowFunction<HasFunction>> {\n\tprivate constructor(\n\t\tprivate readonly windowFunction: Literal | undefined,\n\t\tprivate readonly partitionByExpr: Literal | undefined,\n\t\tprivate readonly orderByColumns: Literal[],\n\t) {}\n\n\tpublic static createEmpty(): WindowFunction<false> {\n\t\treturn new WindowFunction<false>(undefined, undefined, [])\n\t}\n\n\tpublic rowNumber(): WindowFunction<true> {\n\t\treturn new WindowFunction(new Literal('row_number()'), this.partitionByExpr, this.orderByColumns)\n\t}\n\n\tpublic partitionBy(columnName: QueryBuilder.ColumnIdentifier): WindowFunction<HasFunction>\n\tpublic partitionBy(callback: QueryBuilder.ColumnExpression): WindowFunction<HasFunction>\n\tpublic partitionBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): WindowFunction<HasFunction> {\n\t\tconst raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn new WindowFunction(this.windowFunction, raw, this.orderByColumns)\n\t}\n\n\torderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection:
|
|
1
|
+
{"version":3,"file":"WindowFunction.cjs","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral, toFqnWrap } from './utils'\nimport type { SelectBuilder } from './SelectBuilder'\n\nclass WindowFunction<HasFunction extends boolean> implements QueryBuilder.Orderable<WindowFunction<HasFunction>> {\n\tprivate constructor(\n\t\tprivate readonly windowFunction: Literal | undefined,\n\t\tprivate readonly partitionByExpr: Literal | undefined,\n\t\tprivate readonly orderByColumns: Literal[],\n\t) {}\n\n\tpublic static createEmpty(): WindowFunction<false> {\n\t\treturn new WindowFunction<false>(undefined, undefined, [])\n\t}\n\n\tpublic rowNumber(): WindowFunction<true> {\n\t\treturn new WindowFunction(new Literal('row_number()'), this.partitionByExpr, this.orderByColumns)\n\t}\n\n\tpublic partitionBy(columnName: QueryBuilder.ColumnIdentifier): WindowFunction<HasFunction>\n\tpublic partitionBy(callback: QueryBuilder.ColumnExpression): WindowFunction<HasFunction>\n\tpublic partitionBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): WindowFunction<HasFunction> {\n\t\tconst raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn new WindowFunction(this.windowFunction, raw, this.orderByColumns)\n\t}\n\n\torderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection: SelectBuilder.OrderByDirection = 'asc',\n\t): WindowFunction<HasFunction> {\n\t\tconst raw = expression instanceof Literal ? expression : new Literal(toFqnWrap(expression))\n\t\treturn new WindowFunction(this.windowFunction, this.partitionByExpr, [\n\t\t\t...this.orderByColumns,\n\t\t\traw.appendString(` ${direction}`),\n\t\t])\n\t}\n\n\tcompile(): Literal {\n\t\tif (this.windowFunction === undefined) {\n\t\t\tthrow new Error()\n\t\t}\n\t\tlet windowDefinition = new Literal('')\n\t\tif (this.partitionByExpr !== undefined) {\n\t\t\twindowDefinition = windowDefinition.appendString('partition by ').append(this.partitionByExpr)\n\t\t}\n\t\tif (this.orderByColumns.length > 0) {\n\t\t\twindowDefinition = windowDefinition.appendString(' order by ').appendAll(this.orderByColumns, ', ')\n\t\t}\n\t\treturn new Literal(this.windowFunction.sql + ' over(' + windowDefinition.sql + ')', [\n\t\t\t...this.windowFunction.parameters,\n\t\t\t...windowDefinition.parameters,\n\t\t])\n\t}\n}\n\nexport { WindowFunction }\n"],"names":["Literal","columnExpressionToLiteral","toFqnWrap"],"mappings":";;;;;AAKA,MAAM,eAA2G;AAAA,EACxG,YACU,gBACA,iBACA,gBAChB;AAHgB,SAAA,iBAAA;AACA,SAAA,kBAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAGlB,OAAc,cAAqC;AAClD,WAAO,IAAI,eAAsB,QAAW,QAAW,CAAA,CAAE;AAAA,EAAA;AAAA,EAGnD,YAAkC;AACjC,WAAA,IAAI,eAAe,IAAIA,gBAAQ,cAAc,GAAG,KAAK,iBAAiB,KAAK,cAAc;AAAA,EAAA;AAAA,EAK1F,YAAY,MAAkG;AAC9G,UAAA,MAAMC,gCAA0B,IAAI;AAC1C,QAAI,QAAQ,QAAW;AACf,aAAA;AAAA,IAAA;AAER,WAAO,IAAI,eAAe,KAAK,gBAAgB,KAAK,KAAK,cAAc;AAAA,EAAA;AAAA,EAGxE,QACC,YACA,YAA4C,OACd;AACxB,UAAA,MAAM,sBAAsBD,kBAAU,aAAa,IAAIA,gBAAQE,YAAAA,UAAU,UAAU,CAAC;AAC1F,WAAO,IAAI,eAAe,KAAK,gBAAgB,KAAK,iBAAiB;AAAA,MACpE,GAAG,KAAK;AAAA,MACR,IAAI,aAAa,IAAI,SAAS,EAAE;AAAA,IAAA,CAChC;AAAA,EAAA;AAAA,EAGF,UAAmB;AACd,QAAA,KAAK,mBAAmB,QAAW;AACtC,YAAM,IAAI,MAAM;AAAA,IAAA;AAEb,QAAA,mBAAmB,IAAIF,QAAA,QAAQ,EAAE;AACjC,QAAA,KAAK,oBAAoB,QAAW;AACvC,yBAAmB,iBAAiB,aAAa,eAAe,EAAE,OAAO,KAAK,eAAe;AAAA,IAAA;AAE1F,QAAA,KAAK,eAAe,SAAS,GAAG;AACnC,yBAAmB,iBAAiB,aAAa,YAAY,EAAE,UAAU,KAAK,gBAAgB,IAAI;AAAA,IAAA;AAE5F,WAAA,IAAIA,QAAAA,QAAQ,KAAK,eAAe,MAAM,WAAW,iBAAiB,MAAM,KAAK;AAAA,MACnF,GAAG,KAAK,eAAe;AAAA,MACvB,GAAG,iBAAiB;AAAA,IAAA,CACpB;AAAA,EAAA;AAEH;;"}
|
|
@@ -24,7 +24,7 @@ class WindowFunction {
|
|
|
24
24
|
const raw = expression instanceof Literal ? expression : new Literal(toFqnWrap(expression));
|
|
25
25
|
return new WindowFunction(this.windowFunction, this.partitionByExpr, [
|
|
26
26
|
...this.orderByColumns,
|
|
27
|
-
raw.appendString(direction
|
|
27
|
+
raw.appendString(` ${direction}`)
|
|
28
28
|
]);
|
|
29
29
|
}
|
|
30
30
|
compile() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WindowFunction.js","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral, toFqnWrap } from './utils'\n\nclass WindowFunction<HasFunction extends boolean> implements QueryBuilder.Orderable<WindowFunction<HasFunction>> {\n\tprivate constructor(\n\t\tprivate readonly windowFunction: Literal | undefined,\n\t\tprivate readonly partitionByExpr: Literal | undefined,\n\t\tprivate readonly orderByColumns: Literal[],\n\t) {}\n\n\tpublic static createEmpty(): WindowFunction<false> {\n\t\treturn new WindowFunction<false>(undefined, undefined, [])\n\t}\n\n\tpublic rowNumber(): WindowFunction<true> {\n\t\treturn new WindowFunction(new Literal('row_number()'), this.partitionByExpr, this.orderByColumns)\n\t}\n\n\tpublic partitionBy(columnName: QueryBuilder.ColumnIdentifier): WindowFunction<HasFunction>\n\tpublic partitionBy(callback: QueryBuilder.ColumnExpression): WindowFunction<HasFunction>\n\tpublic partitionBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): WindowFunction<HasFunction> {\n\t\tconst raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn new WindowFunction(this.windowFunction, raw, this.orderByColumns)\n\t}\n\n\torderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection:
|
|
1
|
+
{"version":3,"file":"WindowFunction.js","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral, toFqnWrap } from './utils'\nimport type { SelectBuilder } from './SelectBuilder'\n\nclass WindowFunction<HasFunction extends boolean> implements QueryBuilder.Orderable<WindowFunction<HasFunction>> {\n\tprivate constructor(\n\t\tprivate readonly windowFunction: Literal | undefined,\n\t\tprivate readonly partitionByExpr: Literal | undefined,\n\t\tprivate readonly orderByColumns: Literal[],\n\t) {}\n\n\tpublic static createEmpty(): WindowFunction<false> {\n\t\treturn new WindowFunction<false>(undefined, undefined, [])\n\t}\n\n\tpublic rowNumber(): WindowFunction<true> {\n\t\treturn new WindowFunction(new Literal('row_number()'), this.partitionByExpr, this.orderByColumns)\n\t}\n\n\tpublic partitionBy(columnName: QueryBuilder.ColumnIdentifier): WindowFunction<HasFunction>\n\tpublic partitionBy(callback: QueryBuilder.ColumnExpression): WindowFunction<HasFunction>\n\tpublic partitionBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): WindowFunction<HasFunction> {\n\t\tconst raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn new WindowFunction(this.windowFunction, raw, this.orderByColumns)\n\t}\n\n\torderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection: SelectBuilder.OrderByDirection = 'asc',\n\t): WindowFunction<HasFunction> {\n\t\tconst raw = expression instanceof Literal ? expression : new Literal(toFqnWrap(expression))\n\t\treturn new WindowFunction(this.windowFunction, this.partitionByExpr, [\n\t\t\t...this.orderByColumns,\n\t\t\traw.appendString(` ${direction}`),\n\t\t])\n\t}\n\n\tcompile(): Literal {\n\t\tif (this.windowFunction === undefined) {\n\t\t\tthrow new Error()\n\t\t}\n\t\tlet windowDefinition = new Literal('')\n\t\tif (this.partitionByExpr !== undefined) {\n\t\t\twindowDefinition = windowDefinition.appendString('partition by ').append(this.partitionByExpr)\n\t\t}\n\t\tif (this.orderByColumns.length > 0) {\n\t\t\twindowDefinition = windowDefinition.appendString(' order by ').appendAll(this.orderByColumns, ', ')\n\t\t}\n\t\treturn new Literal(this.windowFunction.sql + ' over(' + windowDefinition.sql + ')', [\n\t\t\t...this.windowFunction.parameters,\n\t\t\t...windowDefinition.parameters,\n\t\t])\n\t}\n}\n\nexport { WindowFunction }\n"],"names":[],"mappings":";;;AAKA,MAAM,eAA2G;AAAA,EACxG,YACU,gBACA,iBACA,gBAChB;AAHgB,SAAA,iBAAA;AACA,SAAA,kBAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAGlB,OAAc,cAAqC;AAClD,WAAO,IAAI,eAAsB,QAAW,QAAW,CAAA,CAAE;AAAA,EAAA;AAAA,EAGnD,YAAkC;AACjC,WAAA,IAAI,eAAe,IAAI,QAAQ,cAAc,GAAG,KAAK,iBAAiB,KAAK,cAAc;AAAA,EAAA;AAAA,EAK1F,YAAY,MAAkG;AAC9G,UAAA,MAAM,0BAA0B,IAAI;AAC1C,QAAI,QAAQ,QAAW;AACf,aAAA;AAAA,IAAA;AAER,WAAO,IAAI,eAAe,KAAK,gBAAgB,KAAK,KAAK,cAAc;AAAA,EAAA;AAAA,EAGxE,QACC,YACA,YAA4C,OACd;AACxB,UAAA,MAAM,sBAAsB,UAAU,aAAa,IAAI,QAAQ,UAAU,UAAU,CAAC;AAC1F,WAAO,IAAI,eAAe,KAAK,gBAAgB,KAAK,iBAAiB;AAAA,MACpE,GAAG,KAAK;AAAA,MACR,IAAI,aAAa,IAAI,SAAS,EAAE;AAAA,IAAA,CAChC;AAAA,EAAA;AAAA,EAGF,UAAmB;AACd,QAAA,KAAK,mBAAmB,QAAW;AACtC,YAAM,IAAI,MAAM;AAAA,IAAA;AAEb,QAAA,mBAAmB,IAAI,QAAQ,EAAE;AACjC,QAAA,KAAK,oBAAoB,QAAW;AACvC,yBAAmB,iBAAiB,aAAa,eAAe,EAAE,OAAO,KAAK,eAAe;AAAA,IAAA;AAE1F,QAAA,KAAK,eAAe,SAAS,GAAG;AACnC,yBAAmB,iBAAiB,aAAa,YAAY,EAAE,UAAU,KAAK,gBAAgB,IAAI;AAAA,IAAA;AAE5F,WAAA,IAAI,QAAQ,KAAK,eAAe,MAAM,WAAW,iBAAiB,MAAM,KAAK;AAAA,MACnF,GAAG,KAAK,eAAe;AAAA,MACvB,GAAG,iBAAiB;AAAA,IAAA,CACpB;AAAA,EAAA;AAEH;"}
|
|
@@ -4,17 +4,20 @@ const EventManager = require("./EventManager.cjs");
|
|
|
4
4
|
const Client = require("./Client.cjs");
|
|
5
5
|
const Pool = require("./Pool.cjs");
|
|
6
6
|
require("../Literal.cjs");
|
|
7
|
+
const errorCodes = require("./errorCodes.cjs");
|
|
8
|
+
const errors = require("./errors.cjs");
|
|
7
9
|
require("node:timers");
|
|
8
10
|
require("../utils/Mutex.cjs");
|
|
9
11
|
const pgClientFactory = require("../utils/pgClientFactory.cjs");
|
|
10
12
|
const AcquiredConnection = require("./AcquiredConnection.cjs");
|
|
11
13
|
class Connection {
|
|
12
|
-
constructor(pool, eventManager = new EventManager.EventManager(null)) {
|
|
14
|
+
constructor(pool, eventManager = new EventManager.EventManager(null), options = {}) {
|
|
13
15
|
this.pool = pool;
|
|
14
16
|
this.eventManager = eventManager;
|
|
17
|
+
this.options = options;
|
|
15
18
|
}
|
|
16
|
-
static create({ pool = {}, ...config }, logError) {
|
|
17
|
-
return new Connection(new Pool.Pool(pgClientFactory.createPgClientFactory(config), { ...pool, logError }));
|
|
19
|
+
static create({ pool = {}, ...config }, logError, options = {}) {
|
|
20
|
+
return new Connection(new Pool.Pool(pgClientFactory.createPgClientFactory(config), { ...pool, logError }), new EventManager.EventManager(null), options);
|
|
18
21
|
}
|
|
19
22
|
static createSingle(config, logError) {
|
|
20
23
|
return new Connection(new Pool.Pool(pgClientFactory.createPgClientFactory(config), { maxConnections: 1, maxIdle: 0, logError }));
|
|
@@ -23,16 +26,41 @@ class Connection {
|
|
|
23
26
|
return new Client.Client(this, schema, queryMeta, new EventManager.EventManager(this.eventManager));
|
|
24
27
|
}
|
|
25
28
|
async scope(callback, options = {}) {
|
|
26
|
-
const acquired = await this.pool.acquire();
|
|
27
29
|
const eventManager = new EventManager.EventManager(options.eventManager ?? this.eventManager);
|
|
30
|
+
while (true) {
|
|
31
|
+
const attempt = await this.scopeOnce(callback, eventManager);
|
|
32
|
+
if (!attempt.stale) {
|
|
33
|
+
return attempt.result;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async scopeOnce(callback, eventManager) {
|
|
38
|
+
const acquired = await this.pool.acquire();
|
|
39
|
+
const reused = acquired.uses > 1;
|
|
40
|
+
let lost = false;
|
|
41
|
+
let answered = false;
|
|
42
|
+
const onError = () => {
|
|
43
|
+
lost = true;
|
|
44
|
+
};
|
|
45
|
+
const onAnswer = () => {
|
|
46
|
+
answered = true;
|
|
47
|
+
};
|
|
48
|
+
acquired.client.on("error", onError);
|
|
49
|
+
eventManager.on(EventManager.EventManager.Event.queryEnd, onAnswer);
|
|
28
50
|
try {
|
|
29
51
|
const connection = new AcquiredConnection.AcquiredConnection(acquired.client, eventManager);
|
|
30
52
|
const result = await callback(connection);
|
|
31
53
|
this.pool.release(acquired);
|
|
32
|
-
return result;
|
|
54
|
+
return { stale: false, result };
|
|
33
55
|
} catch (e) {
|
|
34
56
|
this.pool.dispose(acquired);
|
|
57
|
+
if (this.options.retryStaleConnection && reused && !answered && (lost || isConnectionLoss(e))) {
|
|
58
|
+
return { stale: true };
|
|
59
|
+
}
|
|
35
60
|
throw e;
|
|
61
|
+
} finally {
|
|
62
|
+
acquired.client.off("error", onError);
|
|
63
|
+
eventManager.removeListener(EventManager.EventManager.Event.queryEnd, onAnswer);
|
|
36
64
|
}
|
|
37
65
|
}
|
|
38
66
|
async transaction(callback, options = {}) {
|
|
@@ -55,6 +83,14 @@ class Connection {
|
|
|
55
83
|
return this.pool.getPoolStatus();
|
|
56
84
|
}
|
|
57
85
|
}
|
|
86
|
+
const connectionLossCodes = /* @__PURE__ */ new Set([
|
|
87
|
+
errorCodes.ClientErrorCodes.CONNECTION_EXCEPTION,
|
|
88
|
+
errorCodes.ClientErrorCodes.CONNECTION_DOES_NOT_EXIST,
|
|
89
|
+
errorCodes.ClientErrorCodes.CONNECTION_FAILURE,
|
|
90
|
+
errorCodes.ClientErrorCodes.ADMIN_SHUTDOWN,
|
|
91
|
+
errorCodes.ClientErrorCodes.CRASH_SHUTDOWN
|
|
92
|
+
]);
|
|
93
|
+
const isConnectionLoss = (error) => error instanceof errors.DatabaseError && error.code !== void 0 && connectionLossCodes.has(error.code);
|
|
58
94
|
((Connection2) => {
|
|
59
95
|
Connection2.REPEATABLE_READ = "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ";
|
|
60
96
|
})(Connection || (Connection = {}));
|