@powersync/lib-service-postgres 0.0.0-dev-20250116115804

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.
Files changed (53) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/LICENSE +67 -0
  3. package/README.md +3 -0
  4. package/dist/db/connection/AbstractPostgresConnection.d.ts +29 -0
  5. package/dist/db/connection/AbstractPostgresConnection.js +82 -0
  6. package/dist/db/connection/AbstractPostgresConnection.js.map +1 -0
  7. package/dist/db/connection/ConnectionSlot.d.ts +41 -0
  8. package/dist/db/connection/ConnectionSlot.js +122 -0
  9. package/dist/db/connection/ConnectionSlot.js.map +1 -0
  10. package/dist/db/connection/DatabaseClient.d.ts +62 -0
  11. package/dist/db/connection/DatabaseClient.js +209 -0
  12. package/dist/db/connection/DatabaseClient.js.map +1 -0
  13. package/dist/db/connection/WrappedConnection.d.ts +9 -0
  14. package/dist/db/connection/WrappedConnection.js +11 -0
  15. package/dist/db/connection/WrappedConnection.js.map +1 -0
  16. package/dist/db/db-index.d.ts +4 -0
  17. package/dist/db/db-index.js +5 -0
  18. package/dist/db/db-index.js.map +1 -0
  19. package/dist/index.d.ts +8 -0
  20. package/dist/index.js +9 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/locks/PostgresLockManager.d.ts +17 -0
  23. package/dist/locks/PostgresLockManager.js +112 -0
  24. package/dist/locks/PostgresLockManager.js.map +1 -0
  25. package/dist/locks/locks-index.d.ts +1 -0
  26. package/dist/locks/locks-index.js +2 -0
  27. package/dist/locks/locks-index.js.map +1 -0
  28. package/dist/types/types.d.ts +70 -0
  29. package/dist/types/types.js +119 -0
  30. package/dist/types/types.js.map +1 -0
  31. package/dist/utils/pgwire_utils.d.ts +5 -0
  32. package/dist/utils/pgwire_utils.js +47 -0
  33. package/dist/utils/pgwire_utils.js.map +1 -0
  34. package/dist/utils/utils-index.d.ts +1 -0
  35. package/dist/utils/utils-index.js +2 -0
  36. package/dist/utils/utils-index.js.map +1 -0
  37. package/package.json +42 -0
  38. package/src/db/connection/AbstractPostgresConnection.ts +109 -0
  39. package/src/db/connection/ConnectionSlot.ts +165 -0
  40. package/src/db/connection/DatabaseClient.ts +261 -0
  41. package/src/db/connection/WrappedConnection.ts +11 -0
  42. package/src/db/db-index.ts +4 -0
  43. package/src/index.ts +11 -0
  44. package/src/locks/PostgresLockManager.ts +128 -0
  45. package/src/locks/locks-index.ts +1 -0
  46. package/src/types/types.ts +148 -0
  47. package/src/utils/pgwire_utils.ts +48 -0
  48. package/src/utils/utils-index.ts +1 -0
  49. package/test/src/config.test.ts +12 -0
  50. package/test/tsconfig.json +18 -0
  51. package/tsconfig.json +12 -0
  52. package/tsconfig.tsbuildinfo +1 -0
  53. package/vitest.config.ts +3 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ # @powersync/lib-service-postgres
2
+
3
+ ## 0.0.0-dev-20250116115804
4
+
5
+ ### Minor Changes
6
+
7
+ - b07189d: Allow limiting IP ranges of outgoing connections
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [b07189d]
12
+ - Updated dependencies [b07189d]
13
+ - Updated dependencies [b07189d]
14
+ - @powersync/service-types@0.0.0-dev-20250116115804
15
+ - @powersync/lib-services-framework@0.0.0-dev-20250116115804
16
+ - @powersync/service-jpgwire@0.0.0-dev-20250116115804
17
+
18
+ ## 0.1.0
19
+
20
+ ### Minor Changes
21
+
22
+ - 9d9ff08: Initial release of Postgres bucket storage.
23
+
24
+ ### Patch Changes
25
+
26
+ - Updated dependencies [9d9ff08]
27
+ - @powersync/lib-services-framework@0.4.0
package/LICENSE ADDED
@@ -0,0 +1,67 @@
1
+ # Functional Source License, Version 1.1, Apache 2.0 Future License
2
+
3
+ ## Abbreviation
4
+
5
+ FSL-1.1-Apache-2.0
6
+
7
+ ## Notice
8
+
9
+ Copyright 2023-2024 Journey Mobile, Inc.
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under these Terms and Conditions, as indicated by our inclusion of these Terms and Conditions with the Software.
20
+
21
+ ### License Grant
22
+
23
+ Subject to your compliance with this License Grant and the Patents, Redistribution and Trademark clauses below, we hereby grant you the right to use, copy, modify, create derivative works, publicly perform, publicly display and redistribute the Software for any Permitted Purpose identified below.
24
+
25
+ ### Permitted Purpose
26
+
27
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use means making the Software available to others in a commercial product or service that:
28
+
29
+ 1. substitutes for the Software;
30
+ 2. substitutes for any other product or service we offer using the Software that exists as of the date we make the Software available; or
31
+ 3. offers the same or substantially similar functionality as the Software.
32
+
33
+ Permitted Purposes specifically include using the Software:
34
+
35
+ 1. for your internal use and access;
36
+ 2. for non-commercial education;
37
+ 3. for non-commercial research; and
38
+ 4. in connection with professional services that you provide to a licensee using the Software in accordance with these Terms and Conditions.
39
+
40
+ ### Patents
41
+
42
+ To the extent your use for a Permitted Purpose would necessarily infringe our patents, the license grant above includes a license under our patents. If you make a claim against any party that the Software infringes or contributes to the infringement of any patent, then your patent license to the Software ends immediately.
43
+
44
+ ### Redistribution
45
+
46
+ The Terms and Conditions apply to all copies, modifications and derivatives of the Software.
47
+ If you redistribute any copies, modifications or derivatives of the Software, you must include a copy of or a link to these Terms and Conditions and not remove any copyright notices provided in or with the Software.
48
+
49
+ ### Disclaimer
50
+
51
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
52
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
53
+
54
+ ### Trademarks
55
+
56
+ Except for displaying the License Details and identifying us as the origin of the Software, you have no right under these Terms and Conditions to use our trademarks, trade names, service marks or product names.
57
+
58
+ ## Grant of Future License
59
+
60
+ We hereby irrevocably grant you an additional license to use the Software under the Apache License, Version 2.0 that is effective on the second anniversary of the date we make the Software available. On or after that date, you may use the Software under the Apache License, Version 2.0, in which case the following will apply:
61
+
62
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
63
+ You may obtain a copy of the License at
64
+
65
+ http://www.apache.org/licenses/LICENSE-2.0
66
+
67
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # PowerSync Service Postgres
2
+
3
+ Library for common Postgres logic used in the PowerSync service.
@@ -0,0 +1,29 @@
1
+ import * as framework from '@powersync/lib-services-framework';
2
+ import * as pgwire from '@powersync/service-jpgwire';
3
+ import * as t from 'ts-codec';
4
+ export type DecodedSQLQueryExecutor<T extends t.Codec<any, any>> = {
5
+ first: () => Promise<t.Decoded<T> | null>;
6
+ rows: () => Promise<t.Decoded<T>[]>;
7
+ };
8
+ export declare abstract class AbstractPostgresConnection<Listener extends framework.DisposableListener = framework.DisposableListener> extends framework.DisposableObserver<Listener> {
9
+ protected abstract baseConnection: pgwire.PgClient;
10
+ stream(...args: pgwire.Statement[]): AsyncIterableIterator<pgwire.PgChunk>;
11
+ query(script: string, options?: pgwire.PgSimpleQueryOptions): Promise<pgwire.PgResult>;
12
+ query(...args: pgwire.Statement[]): Promise<pgwire.PgResult>;
13
+ /**
14
+ * Template string helper which can be used to execute template SQL strings.
15
+ */
16
+ sql(strings: TemplateStringsArray, ...params: pgwire.StatementParam[]): {
17
+ execute: () => Promise<pgwire.PgResult>;
18
+ rows: <T>() => Promise<T[]>;
19
+ first: <T>() => Promise<T | null>;
20
+ decoded: <T extends t.Codec<any, any>>(codec: T) => DecodedSQLQueryExecutor<T>;
21
+ };
22
+ queryRows<T>(script: string, options?: pgwire.PgSimpleQueryOptions): Promise<T[]>;
23
+ queryRows<T>(...args: pgwire.Statement[] | [...pgwire.Statement[], pgwire.PgExtendedQueryOptions]): Promise<T[]>;
24
+ streamRows<T>(...args: pgwire.Statement[]): AsyncIterableIterator<T[]>;
25
+ }
26
+ /**
27
+ * Template string helper function which generates PGWire statements.
28
+ */
29
+ export declare const sql: (strings: TemplateStringsArray, ...params: pgwire.StatementParam[]) => pgwire.Statement;
@@ -0,0 +1,82 @@
1
+ import * as framework from '@powersync/lib-services-framework';
2
+ import * as pgwire from '@powersync/service-jpgwire';
3
+ export class AbstractPostgresConnection extends framework.DisposableObserver {
4
+ stream(...args) {
5
+ return this.baseConnection.stream(...args);
6
+ }
7
+ query(...args) {
8
+ return this.baseConnection.query(...args);
9
+ }
10
+ /**
11
+ * Template string helper which can be used to execute template SQL strings.
12
+ */
13
+ sql(strings, ...params) {
14
+ const { statement, params: queryParams } = sql(strings, ...params);
15
+ const rows = () => this.queryRows({
16
+ statement,
17
+ params: queryParams
18
+ });
19
+ const first = async () => {
20
+ const [f] = await rows();
21
+ return f;
22
+ };
23
+ return {
24
+ execute: () => this.query({
25
+ statement,
26
+ params
27
+ }),
28
+ rows,
29
+ first,
30
+ decoded: (codec) => {
31
+ return {
32
+ first: async () => {
33
+ const result = await first();
34
+ return result && codec.decode(result);
35
+ },
36
+ rows: async () => {
37
+ const results = await rows();
38
+ return results.map((r) => {
39
+ return codec.decode(r);
40
+ });
41
+ }
42
+ };
43
+ }
44
+ };
45
+ }
46
+ async queryRows(...args) {
47
+ return pgwire.pgwireRows(await this.query(...args));
48
+ }
49
+ async *streamRows(...args) {
50
+ let columns = [];
51
+ for await (const chunk of this.stream(...args)) {
52
+ if (chunk.tag == 'RowDescription') {
53
+ columns = chunk.payload.map((c, index) => {
54
+ return c.name;
55
+ });
56
+ continue;
57
+ }
58
+ if (!chunk.rows.length) {
59
+ continue;
60
+ }
61
+ yield chunk.rows.map((row) => {
62
+ let q = {};
63
+ for (const [index, c] of columns.entries()) {
64
+ q[c] = row[index];
65
+ }
66
+ return q;
67
+ });
68
+ }
69
+ }
70
+ }
71
+ /**
72
+ * Template string helper function which generates PGWire statements.
73
+ */
74
+ export const sql = (strings, ...params) => {
75
+ const paramPlaceholders = new Array(params.length).fill('').map((value, index) => `$${index + 1}`);
76
+ const joinedQueryStatement = strings.map((query, index) => `${query} ${paramPlaceholders[index] ?? ''}`).join(' ');
77
+ return {
78
+ statement: joinedQueryStatement,
79
+ params
80
+ };
81
+ };
82
+ //# sourceMappingURL=AbstractPostgresConnection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AbstractPostgresConnection.js","sourceRoot":"","sources":["../../../src/db/connection/AbstractPostgresConnection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,mCAAmC,CAAC;AAC/D,OAAO,KAAK,MAAM,MAAM,4BAA4B,CAAC;AAQrD,MAAM,OAAgB,0BAEpB,SAAQ,SAAS,CAAC,kBAA4B;IAG9C,MAAM,CAAC,GAAG,IAAwB;QAChC,OAAO,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC;IAC7C,CAAC;IAID,KAAK,CAAC,GAAG,IAAW;QAClB,OAAO,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;IAC5C,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,OAA6B,EAAE,GAAG,MAA+B;QACnE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,GAAG,CAAC,OAAO,EAAE,GAAG,MAAM,CAAC,CAAC;QAEnE,MAAM,IAAI,GAAG,GAAoB,EAAE,CACjC,IAAI,CAAC,SAAS,CAAC;YACb,SAAS;YACT,MAAM,EAAE,WAAW;SACp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wire';
3
+ export interface NotificationListener extends framework.DisposableListener {
4
+ notification?: (payload: pgwire.PgNotification) => void;
5
+ }
6
+ export interface ConnectionSlotListener extends NotificationListener {
7
+ connectionAvailable?: () => void;
8
+ connectionError?: (exception: any) => void;
9
+ connectionCreated?: (connection: pgwire.PgConnection) => Promise<void>;
10
+ }
11
+ export type ConnectionLease = {
12
+ connection: pgwire.PgConnection;
13
+ release: () => void;
14
+ };
15
+ export type ConnectionSlotOptions = {
16
+ config: pgwire.NormalizedConnectionConfig;
17
+ notificationChannels?: string[];
18
+ };
19
+ export declare const MAX_CONNECTION_ATTEMPTS = 5;
20
+ export declare class ConnectionSlot extends framework.DisposableObserver<ConnectionSlotListener> {
21
+ protected options: ConnectionSlotOptions;
22
+ isAvailable: boolean;
23
+ isPoking: boolean;
24
+ closed: boolean;
25
+ protected connection: pgwire.PgConnection | null;
26
+ protected connectingPromise: Promise<pgwire.PgConnection> | null;
27
+ constructor(options: ConnectionSlotOptions);
28
+ get isConnected(): boolean;
29
+ protected connect(): Promise<pgwire.PgConnection>;
30
+ [Symbol.asyncDispose](): Promise<void>;
31
+ protected configureConnectionNotifications(connection: pgwire.PgConnection): Promise<void>;
32
+ registerListener(listener: Partial<ConnectionSlotListener>): () => void;
33
+ protected handleNotification: (payload: pgwire.PgNotification) => void;
34
+ protected hasNotificationListener(): boolean;
35
+ /**
36
+ * Test the connection if it can be reached.
37
+ */
38
+ poke(): Promise<void>;
39
+ protected setAvailable(): void;
40
+ lock(): ConnectionLease | null;
41
+ }
@@ -0,0 +1,122 @@
1
+ import * as framework from '@powersync/lib-services-framework';
2
+ import * as pgwire from '@powersync/service-jpgwire';
3
+ export const MAX_CONNECTION_ATTEMPTS = 5;
4
+ export class ConnectionSlot extends framework.DisposableObserver {
5
+ constructor(options) {
6
+ super();
7
+ this.options = options;
8
+ this.handleNotification = (payload) => {
9
+ if (!this.options.notificationChannels?.includes(payload.channel)) {
10
+ return;
11
+ }
12
+ this.iterateListeners((l) => l.notification?.(payload));
13
+ };
14
+ this.isAvailable = false;
15
+ this.connection = null;
16
+ this.isPoking = false;
17
+ this.connectingPromise = null;
18
+ this.closed = false;
19
+ }
20
+ get isConnected() {
21
+ return !!this.connection;
22
+ }
23
+ async connect() {
24
+ this.connectingPromise = pgwire.connectPgWire(this.options.config, { type: 'standard' });
25
+ const connection = await this.connectingPromise;
26
+ this.connectingPromise = null;
27
+ await this.iterateAsyncListeners(async (l) => l.connectionCreated?.(connection));
28
+ if (this.hasNotificationListener()) {
29
+ await this.configureConnectionNotifications(connection);
30
+ }
31
+ return connection;
32
+ }
33
+ async [Symbol.asyncDispose]() {
34
+ this.closed = true;
35
+ const connection = this.connection ?? (await this.connectingPromise);
36
+ await connection?.end();
37
+ return super[Symbol.dispose]();
38
+ }
39
+ async configureConnectionNotifications(connection) {
40
+ if (connection.onnotification == this.handleNotification || this.closed == true) {
41
+ // Already configured
42
+ return;
43
+ }
44
+ connection.onnotification = this.handleNotification;
45
+ for (const channelName of this.options.notificationChannels ?? []) {
46
+ await connection.query({
47
+ statement: `LISTEN ${channelName}`
48
+ });
49
+ }
50
+ }
51
+ registerListener(listener) {
52
+ const dispose = super.registerListener(listener);
53
+ if (this.connection && this.hasNotificationListener()) {
54
+ this.configureConnectionNotifications(this.connection);
55
+ }
56
+ return () => {
57
+ dispose();
58
+ if (this.connection && !this.hasNotificationListener()) {
59
+ this.connection.onnotification = () => { };
60
+ }
61
+ };
62
+ }
63
+ hasNotificationListener() {
64
+ return !!Object.values(this.listeners).find((l) => !!l.notification);
65
+ }
66
+ /**
67
+ * Test the connection if it can be reached.
68
+ */
69
+ async poke() {
70
+ if (this.isPoking || (this.isConnected && this.isAvailable == false) || this.closed) {
71
+ return;
72
+ }
73
+ this.isPoking = true;
74
+ for (let retryCounter = 0; retryCounter <= MAX_CONNECTION_ATTEMPTS; retryCounter++) {
75
+ try {
76
+ const connection = this.connection ?? (await this.connect());
77
+ await connection.query({
78
+ statement: 'SELECT 1'
79
+ });
80
+ if (!this.connection) {
81
+ this.connection = connection;
82
+ this.setAvailable();
83
+ }
84
+ else if (this.isAvailable) {
85
+ this.iterateListeners((cb) => cb.connectionAvailable?.());
86
+ }
87
+ // Connection is alive and healthy
88
+ break;
89
+ }
90
+ catch (ex) {
91
+ // Should be valid for all cases
92
+ this.isAvailable = false;
93
+ if (this.connection) {
94
+ this.connection.onnotification = () => { };
95
+ this.connection.destroy();
96
+ this.connection = null;
97
+ }
98
+ if (retryCounter >= MAX_CONNECTION_ATTEMPTS) {
99
+ this.iterateListeners((cb) => cb.connectionError?.(ex));
100
+ }
101
+ }
102
+ }
103
+ this.isPoking = false;
104
+ }
105
+ setAvailable() {
106
+ this.isAvailable = true;
107
+ this.iterateListeners((l) => l.connectionAvailable?.());
108
+ }
109
+ lock() {
110
+ if (!this.isAvailable || !this.connection || this.closed) {
111
+ return null;
112
+ }
113
+ this.isAvailable = false;
114
+ return {
115
+ connection: this.connection,
116
+ release: () => {
117
+ this.setAvailable();
118
+ }
119
+ };
120
+ }
121
+ }
122
+ //# sourceMappingURL=ConnectionSlot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ConnectionSlot.js","sourceRoot":"","sources":["../../../src/db/connection/ConnectionSlot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,mCAAmC,CAAC;AAC/D,OAAO,KAAK,MAAM,MAAM,4BAA4B,CAAC;AAsBrD,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,CAAC;AAEzC,MAAM,OAAO,cAAe,SAAQ,SAAS,CAAC,kBAA0C;IAStF,YAAsB,OAA8B;QAClD,KAAK,EAAE,CAAC;QADY,YAAO,GAAP,OAAO,CAAuB;QA2D1C,uBAAkB,GAAG,CAAC,OAA8B,EAAE,EAAE;YAChE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,oBAAoB,EAAE,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;gBAClE,OAAO;YACT,CAAC;YACD,IAAI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AC,eAAe,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;gBAC1D,CAAC;YACH,CAAC;QACH,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;IACxB,CAAC;IAES,YAAY;QACpB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;QACxB,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,mBAAmB,EAAE,EAAE,CAAC,CAAC;IAC1D,CAAC;IAED,IAAI;QACF,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YACzD,OAAO,IAAI,CAAC;QACd,CAAC;QAED,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;QAEzB,OAAO;YACL,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,OAAO,EAAE,GAAG,EAAE;gBACZ,IAAI,CAAC,YAAY,EAAE,CAAC;YACtB,CAAC;SACF,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,62 @@
1
+ import * as lib_postgres from '@powersync/lib-service-postgres';
2
+ import * as pgwire from '@powersync/service-jpgwire';
3
+ import { DeferredPromise } from 'p-defer';
4
+ import { AbstractPostgresConnection } from './AbstractPostgresConnection.js';
5
+ import { ConnectionLease, ConnectionSlot, NotificationListener } from './ConnectionSlot.js';
6
+ import { WrappedConnection } from './WrappedConnection.js';
7
+ export type DatabaseClientOptions = {
8
+ config: lib_postgres.NormalizedBasePostgresConnectionConfig;
9
+ /**
10
+ * Optional schema which will be used as the default search path
11
+ */
12
+ schema?: string;
13
+ /**
14
+ * Notification channels to listen to.
15
+ */
16
+ notificationChannels?: string[];
17
+ };
18
+ export type DatabaseClientListener = NotificationListener & {
19
+ connectionCreated?: (connection: pgwire.PgConnection) => Promise<void>;
20
+ };
21
+ export declare const TRANSACTION_CONNECTION_COUNT = 5;
22
+ /**
23
+ * This provides access to Postgres via the PGWire library.
24
+ * A connection pool is used for individual query executions while
25
+ * a custom pool of connections is available for transactions or other operations
26
+ * which require being executed on the same connection.
27
+ */
28
+ export declare class DatabaseClient extends AbstractPostgresConnection<DatabaseClientListener> {
29
+ protected options: DatabaseClientOptions;
30
+ closed: boolean;
31
+ protected pool: pgwire.PgClient;
32
+ protected connections: ConnectionSlot[];
33
+ protected initialized: Promise<void>;
34
+ protected queue: DeferredPromise<ConnectionLease>[];
35
+ constructor(options: DatabaseClientOptions);
36
+ protected get baseConnection(): pgwire.PgClient;
37
+ protected get schemaStatement(): {
38
+ statement: string;
39
+ } | undefined;
40
+ registerListener(listener: Partial<DatabaseClientListener>): () => void;
41
+ query(script: string, options?: pgwire.PgSimpleQueryOptions): Promise<pgwire.PgResult>;
42
+ query(...args: pgwire.Statement[]): Promise<pgwire.PgResult>;
43
+ stream(...args: pgwire.Statement[]): AsyncIterableIterator<pgwire.PgChunk>;
44
+ lockConnection<T>(callback: (db: WrappedConnection) => Promise<T>): Promise<T>;
45
+ transaction<T>(tx: (db: WrappedConnection) => Promise<T>): Promise<T>;
46
+ /**
47
+ * Use the `powersync` schema as the default when resolving table names
48
+ */
49
+ protected setSchema(client: pgwire.PgClient): Promise<void>;
50
+ protected initialize(): Promise<void>;
51
+ protected requestConnection(): Promise<ConnectionLease>;
52
+ protected pokeSlots(): void;
53
+ protected leaseConnectionSlot(): ConnectionLease | null;
54
+ protected processConnectionQueue(): void;
55
+ /**
56
+ * Reports connection errors which might occur from bad configuration or
57
+ * a server which is no longer available.
58
+ * This fails all pending requests.
59
+ */
60
+ protected handleConnectionError(exception: any): void;
61
+ [Symbol.asyncDispose](): Promise<void>;
62
+ }
@@ -0,0 +1,209 @@
1
+ import * as lib_postgres from '@powersync/lib-service-postgres';
2
+ import * as pgwire from '@powersync/service-jpgwire';
3
+ import pDefer from 'p-defer';
4
+ import { AbstractPostgresConnection, sql } from './AbstractPostgresConnection.js';
5
+ import { ConnectionSlot } from './ConnectionSlot.js';
6
+ import { WrappedConnection } from './WrappedConnection.js';
7
+ export const TRANSACTION_CONNECTION_COUNT = 5;
8
+ /**
9
+ * This provides access to Postgres via the PGWire library.
10
+ * A connection pool is used for individual query executions while
11
+ * a custom pool of connections is available for transactions or other operations
12
+ * which require being executed on the same connection.
13
+ */
14
+ export class DatabaseClient extends AbstractPostgresConnection {
15
+ constructor(options) {
16
+ super();
17
+ this.options = options;
18
+ this.closed = false;
19
+ this.pool = pgwire.connectPgWirePool(options.config);
20
+ this.connections = Array.from({ length: TRANSACTION_CONNECTION_COUNT }, () => {
21
+ const slot = new ConnectionSlot({ config: options.config, notificationChannels: options.notificationChannels });
22
+ slot.registerListener({
23
+ connectionAvailable: () => this.processConnectionQueue(),
24
+ connectionError: (ex) => this.handleConnectionError(ex),
25
+ connectionCreated: (connection) => this.iterateAsyncListeners(async (l) => l.connectionCreated?.(connection))
26
+ });
27
+ return slot;
28
+ });
29
+ this.queue = [];
30
+ this.initialized = this.initialize();
31
+ }
32
+ get baseConnection() {
33
+ return this.pool;
34
+ }
35
+ get schemaStatement() {
36
+ const { schema } = this.options;
37
+ if (!schema) {
38
+ return;
39
+ }
40
+ return {
41
+ statement: `SET search_path TO ${schema};`
42
+ };
43
+ }
44
+ registerListener(listener) {
45
+ let disposeNotification = null;
46
+ if ('notification' in listener) {
47
+ // Pass this on to the first connection slot
48
+ // It will only actively listen on the connection once a listener has been registered
49
+ disposeNotification = this.connections[0].registerListener({
50
+ notification: listener.notification
51
+ });
52
+ this.pokeSlots();
53
+ delete listener['notification'];
54
+ }
55
+ const superDispose = super.registerListener(listener);
56
+ return () => {
57
+ disposeNotification?.();
58
+ superDispose();
59
+ };
60
+ }
61
+ async query(...args) {
62
+ await this.initialized;
63
+ /**
64
+ * There is no direct way to set the default schema with pgwire.
65
+ * This hack uses multiple statements in order to always ensure the
66
+ * appropriate connection (in the pool) uses the correct schema.
67
+ */
68
+ const { schemaStatement } = this;
69
+ if (typeof args[0] == 'object' && schemaStatement) {
70
+ args.unshift(schemaStatement);
71
+ }
72
+ else if (typeof args[0] == 'string' && schemaStatement) {
73
+ args[0] = `${schemaStatement.statement}; ${args[0]}`;
74
+ }
75
+ // Retry pool queries. Note that we can't retry queries in a transaction
76
+ // since a failed query will end the transaction.
77
+ return lib_postgres.retriedQuery(this.pool, ...args);
78
+ }
79
+ async *stream(...args) {
80
+ await this.initialized;
81
+ const { schemaStatement } = this;
82
+ if (schemaStatement) {
83
+ args.unshift(schemaStatement);
84
+ }
85
+ yield* super.stream(...args);
86
+ }
87
+ async lockConnection(callback) {
88
+ const { connection, release } = await this.requestConnection();
89
+ await this.setSchema(connection);
90
+ try {
91
+ return await callback(new WrappedConnection(connection));
92
+ }
93
+ finally {
94
+ release();
95
+ }
96
+ }
97
+ async transaction(tx) {
98
+ return this.lockConnection(async (db) => {
99
+ try {
100
+ await db.query(sql `BEGIN`);
101
+ const result = await tx(db);
102
+ await db.query(sql `COMMIT`);
103
+ return result;
104
+ }
105
+ catch (ex) {
106
+ await db.query(sql `ROLLBACK`);
107
+ throw ex;
108
+ }
109
+ });
110
+ }
111
+ /**
112
+ * Use the `powersync` schema as the default when resolving table names
113
+ */
114
+ async setSchema(client) {
115
+ const { schemaStatement } = this;
116
+ if (!schemaStatement) {
117
+ return;
118
+ }
119
+ await client.query(schemaStatement);
120
+ }
121
+ async initialize() {
122
+ const { schema } = this.options;
123
+ if (schema) {
124
+ // First check if it exists
125
+ const exists = await this.pool.query(sql `
126
+ SELECT
127
+ schema_name
128
+ FROM
129
+ information_schema.schemata
130
+ WHERE
131
+ schema_name = ${{ type: 'varchar', value: schema }};
132
+ `);
133
+ if (exists.rows.length) {
134
+ return;
135
+ }
136
+ // Create the schema if it doesn't exist
137
+ await this.pool.query({ statement: `CREATE SCHEMA IF NOT EXISTS ${this.options.schema}` });
138
+ }
139
+ }
140
+ async requestConnection() {
141
+ if (this.closed) {
142
+ throw new Error('Database client is closed');
143
+ }
144
+ await this.initialized;
145
+ // Queue the operation
146
+ const deferred = pDefer();
147
+ this.queue.push(deferred);
148
+ this.pokeSlots();
149
+ return deferred.promise;
150
+ }
151
+ pokeSlots() {
152
+ // Poke the slots to check if they are alive
153
+ for (const slot of this.connections) {
154
+ // No need to await this. Errors are reported asynchronously
155
+ slot.poke();
156
+ }
157
+ }
158
+ leaseConnectionSlot() {
159
+ const availableSlots = this.connections.filter((s) => s.isAvailable);
160
+ for (const slot of availableSlots) {
161
+ const lease = slot.lock();
162
+ if (lease) {
163
+ return lease;
164
+ }
165
+ // Possibly some contention detected, keep trying
166
+ }
167
+ return null;
168
+ }
169
+ processConnectionQueue() {
170
+ if (this.closed && this.queue.length) {
171
+ for (const q of this.queue) {
172
+ q.reject(new Error('Database has closed while waiting for a connection'));
173
+ }
174
+ this.queue = [];
175
+ }
176
+ if (this.queue.length) {
177
+ const lease = this.leaseConnectionSlot();
178
+ if (lease) {
179
+ const deferred = this.queue.shift();
180
+ deferred.resolve(lease);
181
+ }
182
+ }
183
+ }
184
+ /**
185
+ * Reports connection errors which might occur from bad configuration or
186
+ * a server which is no longer available.
187
+ * This fails all pending requests.
188
+ */
189
+ handleConnectionError(exception) {
190
+ for (const q of this.queue) {
191
+ q.reject(exception);
192
+ }
193
+ this.queue = [];
194
+ }
195
+ async [Symbol.asyncDispose]() {
196
+ await this.initialized;
197
+ this.closed = true;
198
+ for (const c of this.connections) {
199
+ await c[Symbol.asyncDispose]();
200
+ }
201
+ await this.pool.end();
202
+ // Reject all remaining items
203
+ for (const q of this.queue) {
204
+ q.reject(new Error(`Database is disposed`));
205
+ }
206
+ this.queue = [];
207
+ }
208
+ }
209
+ //# sourceMappingURL=DatabaseClient.js.map