@powersync/common 0.0.0-dev-20240506092851

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 (67) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +6 -0
  3. package/lib/client/AbstractPowerSyncDatabase.d.ts +367 -0
  4. package/lib/client/AbstractPowerSyncDatabase.js +644 -0
  5. package/lib/client/AbstractPowerSyncOpenFactory.d.ts +29 -0
  6. package/lib/client/AbstractPowerSyncOpenFactory.js +24 -0
  7. package/lib/client/connection/PowerSyncBackendConnector.d.ts +23 -0
  8. package/lib/client/connection/PowerSyncBackendConnector.js +1 -0
  9. package/lib/client/connection/PowerSyncCredentials.d.ts +5 -0
  10. package/lib/client/connection/PowerSyncCredentials.js +1 -0
  11. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +71 -0
  12. package/lib/client/sync/bucket/BucketStorageAdapter.js +7 -0
  13. package/lib/client/sync/bucket/CrudBatch.d.ts +31 -0
  14. package/lib/client/sync/bucket/CrudBatch.js +25 -0
  15. package/lib/client/sync/bucket/CrudEntry.d.ts +86 -0
  16. package/lib/client/sync/bucket/CrudEntry.js +84 -0
  17. package/lib/client/sync/bucket/CrudTransaction.d.ts +29 -0
  18. package/lib/client/sync/bucket/CrudTransaction.js +24 -0
  19. package/lib/client/sync/bucket/OpType.d.ts +16 -0
  20. package/lib/client/sync/bucket/OpType.js +22 -0
  21. package/lib/client/sync/bucket/OplogEntry.d.ts +23 -0
  22. package/lib/client/sync/bucket/OplogEntry.js +33 -0
  23. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +66 -0
  24. package/lib/client/sync/bucket/SqliteBucketStorage.js +298 -0
  25. package/lib/client/sync/bucket/SyncDataBatch.d.ts +6 -0
  26. package/lib/client/sync/bucket/SyncDataBatch.js +11 -0
  27. package/lib/client/sync/bucket/SyncDataBucket.d.ts +40 -0
  28. package/lib/client/sync/bucket/SyncDataBucket.js +40 -0
  29. package/lib/client/sync/stream/AbstractRemote.d.ts +25 -0
  30. package/lib/client/sync/stream/AbstractRemote.js +42 -0
  31. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +101 -0
  32. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +477 -0
  33. package/lib/client/sync/stream/streaming-sync-types.d.ts +116 -0
  34. package/lib/client/sync/stream/streaming-sync-types.js +22 -0
  35. package/lib/db/Column.d.ts +19 -0
  36. package/lib/db/Column.js +25 -0
  37. package/lib/db/DBAdapter.d.ts +95 -0
  38. package/lib/db/DBAdapter.js +19 -0
  39. package/lib/db/crud/SyncStatus.d.ts +38 -0
  40. package/lib/db/crud/SyncStatus.js +57 -0
  41. package/lib/db/crud/UploadQueueStatus.d.ts +20 -0
  42. package/lib/db/crud/UploadQueueStatus.js +24 -0
  43. package/lib/db/schema/Index.d.ts +22 -0
  44. package/lib/db/schema/Index.js +29 -0
  45. package/lib/db/schema/IndexedColumn.d.ts +19 -0
  46. package/lib/db/schema/IndexedColumn.js +29 -0
  47. package/lib/db/schema/Schema.d.ts +38 -0
  48. package/lib/db/schema/Schema.js +37 -0
  49. package/lib/db/schema/Table.d.ts +51 -0
  50. package/lib/db/schema/Table.js +113 -0
  51. package/lib/db/schema/TableV2.d.ts +30 -0
  52. package/lib/db/schema/TableV2.js +43 -0
  53. package/lib/index.d.ts +32 -0
  54. package/lib/index.js +32 -0
  55. package/lib/types/types.d.ts +8 -0
  56. package/lib/types/types.js +1 -0
  57. package/lib/utils/AbortOperation.d.ts +9 -0
  58. package/lib/utils/AbortOperation.js +18 -0
  59. package/lib/utils/BaseObserver.d.ts +20 -0
  60. package/lib/utils/BaseObserver.js +22 -0
  61. package/lib/utils/mutex.d.ts +7 -0
  62. package/lib/utils/mutex.js +28 -0
  63. package/lib/utils/parseQuery.d.ts +6 -0
  64. package/lib/utils/parseQuery.js +16 -0
  65. package/lib/utils/strings.d.ts +3 -0
  66. package/lib/utils/strings.js +9 -0
  67. package/package.json +44 -0
package/lib/index.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ export * from './client/AbstractPowerSyncDatabase';
2
+ export * from './client/AbstractPowerSyncOpenFactory';
3
+ export * from './client/connection/PowerSyncBackendConnector';
4
+ export * from './client/connection/PowerSyncCredentials';
5
+ export * from './client/sync/bucket/BucketStorageAdapter';
6
+ export * from './client/sync/bucket/CrudEntry';
7
+ export * from './client/sync/bucket/SqliteBucketStorage';
8
+ export * from './client/sync/bucket/CrudBatch';
9
+ export * from './client/sync/bucket/CrudTransaction';
10
+ export * from './client/sync/bucket/SyncDataBatch';
11
+ export * from './client/sync/bucket/SyncDataBucket';
12
+ export * from './client/sync/bucket/OpType';
13
+ export * from './client/sync/bucket/OplogEntry';
14
+ export * from './client/sync/stream/AbstractRemote';
15
+ export * from './client/sync/stream/AbstractStreamingSyncImplementation';
16
+ export * from './client/sync/stream/streaming-sync-types';
17
+ export * from './db/crud/SyncStatus';
18
+ export * from './db/crud/UploadQueueStatus';
19
+ export * from './db/schema/Schema';
20
+ export * from './db/schema/Table';
21
+ export * from './db/schema/Index';
22
+ export * from './db/schema/IndexedColumn';
23
+ export * from './db/crud/SyncStatus';
24
+ export * from './db/crud/UploadQueueStatus';
25
+ export * from './db/DBAdapter';
26
+ export * from './db/Column';
27
+ export * from './db/schema/TableV2';
28
+ export * from './utils/AbortOperation';
29
+ export * from './utils/BaseObserver';
30
+ export * from './utils/strings';
31
+ export * from './utils/parseQuery';
32
+ export * from './types/types';
package/lib/index.js ADDED
@@ -0,0 +1,32 @@
1
+ export * from './client/AbstractPowerSyncDatabase';
2
+ export * from './client/AbstractPowerSyncOpenFactory';
3
+ export * from './client/connection/PowerSyncBackendConnector';
4
+ export * from './client/connection/PowerSyncCredentials';
5
+ export * from './client/sync/bucket/BucketStorageAdapter';
6
+ export * from './client/sync/bucket/CrudEntry';
7
+ export * from './client/sync/bucket/SqliteBucketStorage';
8
+ export * from './client/sync/bucket/CrudBatch';
9
+ export * from './client/sync/bucket/CrudTransaction';
10
+ export * from './client/sync/bucket/SyncDataBatch';
11
+ export * from './client/sync/bucket/SyncDataBucket';
12
+ export * from './client/sync/bucket/OpType';
13
+ export * from './client/sync/bucket/OplogEntry';
14
+ export * from './client/sync/stream/AbstractRemote';
15
+ export * from './client/sync/stream/AbstractStreamingSyncImplementation';
16
+ export * from './client/sync/stream/streaming-sync-types';
17
+ export * from './db/crud/SyncStatus';
18
+ export * from './db/crud/UploadQueueStatus';
19
+ export * from './db/schema/Schema';
20
+ export * from './db/schema/Table';
21
+ export * from './db/schema/Index';
22
+ export * from './db/schema/IndexedColumn';
23
+ export * from './db/crud/SyncStatus';
24
+ export * from './db/crud/UploadQueueStatus';
25
+ export * from './db/DBAdapter';
26
+ export * from './db/Column';
27
+ export * from './db/schema/TableV2';
28
+ export * from './utils/AbortOperation';
29
+ export * from './utils/BaseObserver';
30
+ export * from './utils/strings';
31
+ export * from './utils/parseQuery';
32
+ export * from './types/types';
@@ -0,0 +1,8 @@
1
+ export interface CompilableQuery<T> {
2
+ execute(): Promise<T[]>;
3
+ compile(): CompiledQuery;
4
+ }
5
+ export interface CompiledQuery {
6
+ readonly sql: string;
7
+ readonly parameters: ReadonlyArray<unknown>;
8
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Calls to Abortcontroller.abort(reason: any) will result in the
3
+ * `reason` being thrown. This is not necessarily an error,
4
+ * but extends error for better logging purposes.
5
+ */
6
+ export declare class AbortOperation extends Error {
7
+ protected reason: string;
8
+ constructor(reason: string);
9
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Calls to Abortcontroller.abort(reason: any) will result in the
3
+ * `reason` being thrown. This is not necessarily an error,
4
+ * but extends error for better logging purposes.
5
+ */
6
+ export class AbortOperation extends Error {
7
+ reason;
8
+ constructor(reason) {
9
+ super(reason);
10
+ this.reason = reason;
11
+ // Set the prototype explicitly
12
+ Object.setPrototypeOf(this, AbortOperation.prototype);
13
+ // Capture stack trace
14
+ if (Error.captureStackTrace) {
15
+ Error.captureStackTrace(this, AbortOperation);
16
+ }
17
+ }
18
+ }
@@ -0,0 +1,20 @@
1
+ export interface Disposable {
2
+ dispose: () => Promise<void>;
3
+ }
4
+ export interface BaseObserverInterface<T extends BaseListener> {
5
+ registerListener(listener: Partial<T>): () => void;
6
+ }
7
+ export type BaseListener = {
8
+ [key: string]: ((...event: any) => any) | undefined;
9
+ };
10
+ export declare class BaseObserver<T extends BaseListener = BaseListener> implements BaseObserverInterface<T> {
11
+ protected listeners: {
12
+ [id: string]: Partial<T>;
13
+ };
14
+ constructor();
15
+ /**
16
+ * Register a listener for updates to the PowerSync client.
17
+ */
18
+ registerListener(listener: Partial<T>): () => void;
19
+ iterateListeners(cb: (listener: Partial<T>) => any): void;
20
+ }
@@ -0,0 +1,22 @@
1
+ import { v4 } from 'uuid';
2
+ export class BaseObserver {
3
+ listeners;
4
+ constructor() {
5
+ this.listeners = {};
6
+ }
7
+ /**
8
+ * Register a listener for updates to the PowerSync client.
9
+ */
10
+ registerListener(listener) {
11
+ const id = v4();
12
+ this.listeners[id] = listener;
13
+ return () => {
14
+ delete this.listeners[id];
15
+ };
16
+ }
17
+ iterateListeners(cb) {
18
+ for (const i in this.listeners) {
19
+ cb(this.listeners[i]);
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,7 @@
1
+ import { Mutex } from 'async-mutex';
2
+ /**
3
+ * Wrapper for async-mutex runExclusive, which allows for a timeout on each exclusive lock.
4
+ */
5
+ export declare function mutexRunExclusive<T>(mutex: Mutex, callback: () => Promise<T>, options?: {
6
+ timeoutMs: number;
7
+ }): Promise<T>;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Wrapper for async-mutex runExclusive, which allows for a timeout on each exclusive lock.
3
+ */
4
+ export async function mutexRunExclusive(mutex, callback, options) {
5
+ return new Promise((resolve, reject) => {
6
+ const timeout = options?.timeoutMs;
7
+ let timedOut = false;
8
+ const timeoutId = timeout
9
+ ? setTimeout(() => {
10
+ timedOut = true;
11
+ reject(new Error('Timeout waiting for lock'));
12
+ }, timeout)
13
+ : undefined;
14
+ mutex.runExclusive(async () => {
15
+ if (timeoutId) {
16
+ clearTimeout(timeoutId);
17
+ }
18
+ if (timedOut)
19
+ return;
20
+ try {
21
+ resolve(await callback());
22
+ }
23
+ catch (ex) {
24
+ reject(ex);
25
+ }
26
+ });
27
+ });
28
+ }
@@ -0,0 +1,6 @@
1
+ import type { CompilableQuery } from '../types/types';
2
+ export interface ParsedQuery {
3
+ sqlStatement: string;
4
+ parameters: any[];
5
+ }
6
+ export declare const parseQuery: <T>(query: string | CompilableQuery<T>, parameters: any[]) => ParsedQuery;
@@ -0,0 +1,16 @@
1
+ export const parseQuery = (query, parameters) => {
2
+ let sqlStatement;
3
+ if (typeof query == 'string') {
4
+ sqlStatement = query;
5
+ }
6
+ else {
7
+ const hasAdditionalParameters = parameters.length > 0;
8
+ if (hasAdditionalParameters) {
9
+ throw new Error('You cannot pass parameters to a compiled query.');
10
+ }
11
+ const compiled = query.compile();
12
+ sqlStatement = compiled.sql;
13
+ parameters = compiled.parameters;
14
+ }
15
+ return { sqlStatement, parameters: parameters };
16
+ };
@@ -0,0 +1,3 @@
1
+ export declare function quoteString(s: string): string;
2
+ export declare function quoteJsonPath(path: string): string;
3
+ export declare function quoteIdentifier(s: string): string;
@@ -0,0 +1,9 @@
1
+ export function quoteString(s) {
2
+ return `'${s.replaceAll("'", "''")}'`;
3
+ }
4
+ export function quoteJsonPath(path) {
5
+ return quoteString(`$.${path}`);
6
+ }
7
+ export function quoteIdentifier(s) {
8
+ return `"${s.replaceAll('"', '""')}"`;
9
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@powersync/common",
3
+ "version": "0.0.0-dev-20240506092851",
4
+ "publishConfig": {
5
+ "registry": "https://registry.npmjs.org/",
6
+ "access": "public"
7
+ },
8
+ "description": "API definitions for JourneyApps PowerSync",
9
+ "main": "lib/index.js",
10
+ "types": "lib/index.d.ts",
11
+ "author": "JOURNEYAPPS",
12
+ "license": "Apache-2.0",
13
+ "files": [
14
+ "lib"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/powersync-ja/powersync-js.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/powersync-ja/powersync-js/issues"
22
+ },
23
+ "homepage": "https://docs.powersync.com/resources/api-reference",
24
+ "dependencies": {
25
+ "async-mutex": "^0.4.0",
26
+ "can-ndjson-stream": "^1.0.2",
27
+ "event-iterator": "^2.0.0",
28
+ "js-logger": "^1.6.1",
29
+ "lodash": "^4.17.21",
30
+ "uuid": "^9.0.1"
31
+ },
32
+ "devDependencies": {
33
+ "@types/lodash": "^4.14.197",
34
+ "@types/node": "^20.5.9",
35
+ "@types/uuid": "^9.0.1",
36
+ "typescript": "^5.1.3",
37
+ "vitest": "^1.5.2"
38
+ },
39
+ "scripts": {
40
+ "build": "tsc -b",
41
+ "clean": "rm -rf lib tsconfig.tsbuildinfo",
42
+ "test": "vitest"
43
+ }
44
+ }