@powersync/common 0.0.0-dev-20240903063630 → 0.0.0-dev-20240920093037
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/bundle.mjs +18 -0
- package/dist/bundle.mjs.map +1 -0
- package/lib/client/AbstractPowerSyncDatabase.d.ts +22 -15
- package/lib/client/AbstractPowerSyncDatabase.js +51 -36
- package/lib/client/AbstractPowerSyncOpenFactory.d.ts +5 -5
- package/lib/client/AbstractPowerSyncOpenFactory.js +0 -1
- package/lib/client/SQLOpenFactory.d.ts +1 -1
- package/lib/client/SQLOpenFactory.js +0 -1
- package/lib/client/connection/PowerSyncBackendConnector.d.ts +2 -2
- package/lib/client/connection/PowerSyncBackendConnector.js +0 -1
- package/lib/client/connection/PowerSyncCredentials.js +0 -1
- package/lib/client/constants.js +0 -1
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +8 -4
- package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -1
- package/lib/client/sync/bucket/CrudBatch.d.ts +1 -1
- package/lib/client/sync/bucket/CrudBatch.js +0 -1
- package/lib/client/sync/bucket/CrudEntry.js +0 -1
- package/lib/client/sync/bucket/CrudTransaction.d.ts +2 -2
- package/lib/client/sync/bucket/CrudTransaction.js +1 -2
- package/lib/client/sync/bucket/OpType.js +0 -1
- package/lib/client/sync/bucket/OplogEntry.d.ts +2 -2
- package/lib/client/sync/bucket/OplogEntry.js +1 -2
- package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +9 -6
- package/lib/client/sync/bucket/SqliteBucketStorage.js +28 -13
- package/lib/client/sync/bucket/SyncDataBatch.d.ts +1 -1
- package/lib/client/sync/bucket/SyncDataBatch.js +1 -2
- package/lib/client/sync/bucket/SyncDataBucket.d.ts +2 -2
- package/lib/client/sync/bucket/SyncDataBucket.js +1 -2
- package/lib/client/sync/stream/AbstractRemote.d.ts +8 -6
- package/lib/client/sync/stream/AbstractRemote.js +17 -7
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +8 -8
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +14 -11
- package/lib/client/sync/stream/streaming-sync-types.d.ts +4 -3
- package/lib/client/sync/stream/streaming-sync-types.js +0 -1
- package/lib/db/DBAdapter.d.ts +1 -1
- package/lib/db/DBAdapter.js +0 -1
- package/lib/db/crud/SyncStatus.js +0 -1
- package/lib/db/crud/UploadQueueStatus.js +0 -1
- package/lib/db/schema/Column.d.ts +30 -0
- package/lib/db/{Column.js → schema/Column.js} +17 -1
- package/lib/db/schema/Index.d.ts +3 -3
- package/lib/db/schema/Index.js +1 -2
- package/lib/db/schema/IndexedColumn.d.ts +2 -2
- package/lib/db/schema/IndexedColumn.js +1 -2
- package/lib/db/schema/Schema.d.ts +6 -7
- package/lib/db/schema/Schema.js +11 -3
- package/lib/db/schema/Table.d.ts +90 -10
- package/lib/db/schema/Table.js +61 -11
- package/lib/db/schema/TableV2.d.ts +8 -30
- package/lib/db/schema/TableV2.js +5 -62
- package/lib/index.d.ts +34 -35
- package/lib/index.js +34 -36
- package/lib/types/types.js +0 -1
- package/lib/utils/AbortOperation.js +0 -1
- package/lib/utils/BaseObserver.js +0 -1
- package/lib/utils/ControlledExecutor.js +0 -1
- package/lib/utils/DataStream.d.ts +1 -1
- package/lib/utils/DataStream.js +1 -2
- package/lib/utils/mutex.js +0 -1
- package/lib/utils/parseQuery.d.ts +1 -1
- package/lib/utils/parseQuery.js +0 -1
- package/lib/utils/throttle.d.ts +14 -0
- package/lib/utils/throttle.js +45 -0
- package/package.json +13 -5
- package/dist/index.js +0 -16
- package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
- package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
- package/lib/client/SQLOpenFactory.js.map +0 -1
- package/lib/client/connection/PowerSyncBackendConnector.js.map +0 -1
- package/lib/client/connection/PowerSyncCredentials.js.map +0 -1
- package/lib/client/constants.js.map +0 -1
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
- package/lib/client/sync/bucket/CrudBatch.js.map +0 -1
- package/lib/client/sync/bucket/CrudEntry.js.map +0 -1
- package/lib/client/sync/bucket/CrudTransaction.js.map +0 -1
- package/lib/client/sync/bucket/OpType.js.map +0 -1
- package/lib/client/sync/bucket/OplogEntry.js.map +0 -1
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
- package/lib/client/sync/bucket/SyncDataBatch.js.map +0 -1
- package/lib/client/sync/bucket/SyncDataBucket.js.map +0 -1
- package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
- package/lib/client/sync/stream/streaming-sync-types.js.map +0 -1
- package/lib/db/Column.d.ts +0 -19
- package/lib/db/Column.js.map +0 -1
- package/lib/db/DBAdapter.js.map +0 -1
- package/lib/db/crud/SyncStatus.js.map +0 -1
- package/lib/db/crud/UploadQueueStatus.js.map +0 -1
- package/lib/db/schema/Index.js.map +0 -1
- package/lib/db/schema/IndexedColumn.js.map +0 -1
- package/lib/db/schema/Schema.js.map +0 -1
- package/lib/db/schema/Table.js.map +0 -1
- package/lib/db/schema/TableV2.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/types/types.js.map +0 -1
- package/lib/utils/AbortOperation.js.map +0 -1
- package/lib/utils/BaseObserver.js.map +0 -1
- package/lib/utils/ControlledExecutor.js.map +0 -1
- package/lib/utils/DataStream.js.map +0 -1
- package/lib/utils/mutex.js.map +0 -1
- package/lib/utils/parseQuery.js.map +0 -1
- package/lib/utils/strings.d.ts +0 -3
- package/lib/utils/strings.js +0 -10
- package/lib/utils/strings.js.map +0 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { ILogger } from 'js-logger';
|
|
2
|
-
import { SyncStatus, SyncStatusOptions } from '../../../db/crud/SyncStatus';
|
|
3
|
-
import { BaseListener, BaseObserver, Disposable } from '../../../utils/BaseObserver';
|
|
4
|
-
import { BucketStorageAdapter } from '../bucket/BucketStorageAdapter';
|
|
5
|
-
import { AbstractRemote } from './AbstractRemote';
|
|
6
|
-
import { StreamingSyncRequestParameterType } from './streaming-sync-types';
|
|
1
|
+
import Logger, { ILogger } from 'js-logger';
|
|
2
|
+
import { SyncStatus, SyncStatusOptions } from '../../../db/crud/SyncStatus.js';
|
|
3
|
+
import { BaseListener, BaseObserver, Disposable } from '../../../utils/BaseObserver.js';
|
|
4
|
+
import { BucketStorageAdapter } from '../bucket/BucketStorageAdapter.js';
|
|
5
|
+
import { AbstractRemote } from './AbstractRemote.js';
|
|
6
|
+
import { StreamingSyncRequestParameterType } from './streaming-sync-types.js';
|
|
7
7
|
export declare enum LockType {
|
|
8
8
|
CRUD = "crud",
|
|
9
9
|
SYNC = "sync"
|
|
@@ -82,7 +82,7 @@ export interface StreamingSyncImplementation extends BaseObserver<StreamingSyncI
|
|
|
82
82
|
export declare const DEFAULT_CRUD_UPLOAD_THROTTLE_MS = 1000;
|
|
83
83
|
export declare const DEFAULT_STREAMING_SYNC_OPTIONS: {
|
|
84
84
|
retryDelayMs: number;
|
|
85
|
-
logger: ILogger;
|
|
85
|
+
logger: Logger.ILogger;
|
|
86
86
|
crudUploadThrottleMs: number;
|
|
87
87
|
};
|
|
88
88
|
export declare const DEFAULT_STREAM_CONNECTION_OPTIONS: Required<PowerSyncConnectionOptions>;
|
|
@@ -99,7 +99,7 @@ export declare abstract class AbstractStreamingSyncImplementation extends BaseOb
|
|
|
99
99
|
waitForStatus(status: SyncStatusOptions): Promise<void>;
|
|
100
100
|
get lastSyncedAt(): Date | undefined;
|
|
101
101
|
get isConnected(): boolean;
|
|
102
|
-
protected get logger(): ILogger;
|
|
102
|
+
protected get logger(): Logger.ILogger;
|
|
103
103
|
dispose(): Promise<void>;
|
|
104
104
|
abstract obtainLock<T>(lockOptions: LockOptions<T>): Promise<T>;
|
|
105
105
|
hasCompletedSync(): Promise<boolean>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import throttle from 'lodash/throttle';
|
|
2
1
|
import Logger from 'js-logger';
|
|
3
|
-
import { SyncStatus } from '../../../db/crud/SyncStatus';
|
|
4
|
-
import { AbortOperation } from '../../../utils/AbortOperation';
|
|
5
|
-
import { BaseObserver } from '../../../utils/BaseObserver';
|
|
6
|
-
import { SyncDataBucket } from '../bucket/SyncDataBucket';
|
|
7
|
-
import { isStreamingKeepalive, isStreamingSyncCheckpoint, isStreamingSyncCheckpointComplete, isStreamingSyncCheckpointDiff, isStreamingSyncData } from './streaming-sync-types';
|
|
2
|
+
import { SyncStatus } from '../../../db/crud/SyncStatus.js';
|
|
3
|
+
import { AbortOperation } from '../../../utils/AbortOperation.js';
|
|
4
|
+
import { BaseObserver } from '../../../utils/BaseObserver.js';
|
|
5
|
+
import { SyncDataBucket } from '../bucket/SyncDataBucket.js';
|
|
6
|
+
import { isStreamingKeepalive, isStreamingSyncCheckpoint, isStreamingSyncCheckpointComplete, isStreamingSyncCheckpointDiff, isStreamingSyncData } from './streaming-sync-types.js';
|
|
7
|
+
import { throttleLeadingTrailing } from '../../../utils/throttle.js';
|
|
8
8
|
export var LockType;
|
|
9
9
|
(function (LockType) {
|
|
10
10
|
LockType["CRUD"] = "crud";
|
|
@@ -45,12 +45,12 @@ export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
47
|
this.abortController = null;
|
|
48
|
-
this.triggerCrudUpload =
|
|
48
|
+
this.triggerCrudUpload = throttleLeadingTrailing(() => {
|
|
49
49
|
if (!this.syncStatus.connected || this.syncStatus.dataFlowStatus.uploading) {
|
|
50
50
|
return;
|
|
51
51
|
}
|
|
52
52
|
this._uploadAllCrud();
|
|
53
|
-
}, this.options.crudUploadThrottleMs
|
|
53
|
+
}, this.options.crudUploadThrottleMs);
|
|
54
54
|
}
|
|
55
55
|
async waitForReady() { }
|
|
56
56
|
waitForStatus(status) {
|
|
@@ -96,7 +96,9 @@ export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
96
96
|
return this.options.adapter.hasCompletedSync();
|
|
97
97
|
}
|
|
98
98
|
async getWriteCheckpoint() {
|
|
99
|
-
const
|
|
99
|
+
const clientId = await this.options.adapter.getClientId();
|
|
100
|
+
let path = `/write-checkpoint2.json?client_id=${clientId}`;
|
|
101
|
+
const response = await this.options.remote.get(path);
|
|
100
102
|
return response['data']['write_checkpoint'];
|
|
101
103
|
}
|
|
102
104
|
async _uploadAllCrud() {
|
|
@@ -319,6 +321,7 @@ The next upload iteration will be delayed.`);
|
|
|
319
321
|
let validatedCheckpoint = null;
|
|
320
322
|
let appliedCheckpoint = null;
|
|
321
323
|
let bucketSet = new Set(initialBuckets.keys());
|
|
324
|
+
const clientId = await this.options.adapter.getClientId();
|
|
322
325
|
this.logger.debug('Requesting stream from server');
|
|
323
326
|
const syncOptions = {
|
|
324
327
|
path: '/sync/stream',
|
|
@@ -327,7 +330,8 @@ The next upload iteration will be delayed.`);
|
|
|
327
330
|
buckets: req,
|
|
328
331
|
include_checksum: true,
|
|
329
332
|
raw_data: true,
|
|
330
|
-
parameters: resolvedOptions.params
|
|
333
|
+
parameters: resolvedOptions.params,
|
|
334
|
+
client_id: clientId
|
|
331
335
|
}
|
|
332
336
|
};
|
|
333
337
|
const stream = resolvedOptions?.connectionMethod == SyncStreamConnectionMethod.HTTP
|
|
@@ -503,4 +507,3 @@ The next upload iteration will be delayed.`);
|
|
|
503
507
|
return new Promise((resolve) => setTimeout(resolve, this.options.retryDelayMs));
|
|
504
508
|
}
|
|
505
509
|
}
|
|
506
|
-
//# sourceMappingURL=AbstractStreamingSyncImplementation.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BucketChecksum, Checkpoint } from '../bucket/BucketStorageAdapter';
|
|
2
|
-
import { CrudEntry, OpId } from '../bucket/CrudEntry';
|
|
3
|
-
import { SyncDataBucketJSON } from '../bucket/SyncDataBucket';
|
|
1
|
+
import { BucketChecksum, Checkpoint } from '../bucket/BucketStorageAdapter.js';
|
|
2
|
+
import { CrudEntry, OpId } from '../bucket/CrudEntry.js';
|
|
3
|
+
import { SyncDataBucketJSON } from '../bucket/SyncDataBucket.js';
|
|
4
4
|
/**
|
|
5
5
|
* For sync2.json
|
|
6
6
|
*/
|
|
@@ -69,6 +69,7 @@ export interface StreamingSyncRequest {
|
|
|
69
69
|
* Client parameters to be passed to the sync rules.
|
|
70
70
|
*/
|
|
71
71
|
parameters?: Record<string, StreamingSyncRequestParameterType>;
|
|
72
|
+
client_id?: string;
|
|
72
73
|
}
|
|
73
74
|
export interface StreamingSyncCheckpoint {
|
|
74
75
|
checkpoint: Checkpoint;
|
package/lib/db/DBAdapter.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Set of generic interfaces to allow PowerSync compatibility with
|
|
3
3
|
* different SQLite DB implementations.
|
|
4
4
|
*/
|
|
5
|
-
import { BaseListener, BaseObserverInterface } from '../utils/BaseObserver';
|
|
5
|
+
import { BaseListener, BaseObserverInterface } from '../utils/BaseObserver.js';
|
|
6
6
|
/**
|
|
7
7
|
* TODO most of these types could be exported to a common `types` package
|
|
8
8
|
* which is used by the DB adapter libraries as well.
|
package/lib/db/DBAdapter.js
CHANGED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export declare enum ColumnType {
|
|
2
|
+
TEXT = "TEXT",
|
|
3
|
+
INTEGER = "INTEGER",
|
|
4
|
+
REAL = "REAL"
|
|
5
|
+
}
|
|
6
|
+
export interface ColumnOptions {
|
|
7
|
+
name: string;
|
|
8
|
+
type?: ColumnType;
|
|
9
|
+
}
|
|
10
|
+
export type BaseColumnType<T extends number | string | null> = {
|
|
11
|
+
type: ColumnType;
|
|
12
|
+
};
|
|
13
|
+
export type ColumnsType = Record<string, BaseColumnType<any>>;
|
|
14
|
+
export type ExtractColumnValueType<T extends BaseColumnType<any>> = T extends BaseColumnType<infer R> ? R : unknown;
|
|
15
|
+
export declare const MAX_AMOUNT_OF_COLUMNS = 63;
|
|
16
|
+
export declare const column: {
|
|
17
|
+
text: BaseColumnType<string | null>;
|
|
18
|
+
integer: BaseColumnType<number | null>;
|
|
19
|
+
real: BaseColumnType<number | null>;
|
|
20
|
+
};
|
|
21
|
+
export declare class Column {
|
|
22
|
+
protected options: ColumnOptions;
|
|
23
|
+
constructor(options: ColumnOptions);
|
|
24
|
+
get name(): string;
|
|
25
|
+
get type(): ColumnType | undefined;
|
|
26
|
+
toJSON(): {
|
|
27
|
+
name: string;
|
|
28
|
+
type: ColumnType | undefined;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -5,6 +5,23 @@ export var ColumnType;
|
|
|
5
5
|
ColumnType["INTEGER"] = "INTEGER";
|
|
6
6
|
ColumnType["REAL"] = "REAL";
|
|
7
7
|
})(ColumnType || (ColumnType = {}));
|
|
8
|
+
const text = {
|
|
9
|
+
type: ColumnType.TEXT
|
|
10
|
+
};
|
|
11
|
+
const integer = {
|
|
12
|
+
type: ColumnType.INTEGER
|
|
13
|
+
};
|
|
14
|
+
const real = {
|
|
15
|
+
type: ColumnType.REAL
|
|
16
|
+
};
|
|
17
|
+
// There is maximum of 127 arguments for any function in SQLite. Currently we use json_object which uses 1 arg per key (column name)
|
|
18
|
+
// and one per value, which limits it to 63 arguments.
|
|
19
|
+
export const MAX_AMOUNT_OF_COLUMNS = 63;
|
|
20
|
+
export const column = {
|
|
21
|
+
text,
|
|
22
|
+
integer,
|
|
23
|
+
real
|
|
24
|
+
};
|
|
8
25
|
export class Column {
|
|
9
26
|
options;
|
|
10
27
|
constructor(options) {
|
|
@@ -23,4 +40,3 @@ export class Column {
|
|
|
23
40
|
};
|
|
24
41
|
}
|
|
25
42
|
}
|
|
26
|
-
//# sourceMappingURL=Column.js.map
|
package/lib/db/schema/Index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IndexedColumn } from './IndexedColumn';
|
|
2
|
-
import { Table } from './Table';
|
|
1
|
+
import { IndexedColumn } from './IndexedColumn.js';
|
|
2
|
+
import { Table } from './Table.js';
|
|
3
3
|
export interface IndexOptions {
|
|
4
4
|
name: string;
|
|
5
5
|
columns?: IndexedColumn[];
|
|
@@ -16,7 +16,7 @@ export declare class Index {
|
|
|
16
16
|
columns: {
|
|
17
17
|
name: string;
|
|
18
18
|
ascending: boolean | undefined;
|
|
19
|
-
type: import("
|
|
19
|
+
type: import("./Column.js").ColumnType;
|
|
20
20
|
}[];
|
|
21
21
|
};
|
|
22
22
|
}
|
package/lib/db/schema/Index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ColumnType } from '
|
|
1
|
+
import { ColumnType } from './Column.js';
|
|
2
2
|
export const DEFAULT_INDEX_COLUMN_OPTIONS = {
|
|
3
3
|
ascending: true
|
|
4
4
|
};
|
|
@@ -27,4 +27,3 @@ export class IndexedColumn {
|
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
//# sourceMappingURL=IndexedColumn.js.map
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Table
|
|
2
|
-
|
|
3
|
-
type SchemaType = Record<string, TableV2<any>>;
|
|
1
|
+
import { RowType, Table } from './Table.js';
|
|
2
|
+
type SchemaType = Record<string, Table<any>>;
|
|
4
3
|
type SchemaTableType<S extends SchemaType> = {
|
|
5
4
|
[K in keyof S]: RowType<S[K]>;
|
|
6
5
|
};
|
|
@@ -10,8 +9,8 @@ type SchemaTableType<S extends SchemaType> = {
|
|
|
10
9
|
export declare class Schema<S extends SchemaType = SchemaType> {
|
|
11
10
|
readonly types: SchemaTableType<S>;
|
|
12
11
|
readonly props: S;
|
|
13
|
-
readonly tables:
|
|
14
|
-
constructor(tables:
|
|
12
|
+
readonly tables: Table[];
|
|
13
|
+
constructor(tables: Table[] | S);
|
|
15
14
|
validate(): void;
|
|
16
15
|
toJSON(): {
|
|
17
16
|
tables: {
|
|
@@ -21,14 +20,14 @@ export declare class Schema<S extends SchemaType = SchemaType> {
|
|
|
21
20
|
insert_only: boolean;
|
|
22
21
|
columns: {
|
|
23
22
|
name: string;
|
|
24
|
-
type: import("
|
|
23
|
+
type: import("./Column.js").ColumnType | undefined;
|
|
25
24
|
}[];
|
|
26
25
|
indexes: {
|
|
27
26
|
name: string;
|
|
28
27
|
columns: {
|
|
29
28
|
name: string;
|
|
30
29
|
ascending: boolean | undefined;
|
|
31
|
-
type: import("
|
|
30
|
+
type: import("./Column.js").ColumnType;
|
|
32
31
|
}[];
|
|
33
32
|
}[];
|
|
34
33
|
}[];
|
package/lib/db/schema/Schema.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Table
|
|
1
|
+
import { Table } from './Table.js';
|
|
2
2
|
/**
|
|
3
3
|
* A schema is a collection of tables. It is used to define the structure of a database.
|
|
4
4
|
*/
|
|
@@ -25,13 +25,21 @@ export class Schema {
|
|
|
25
25
|
}
|
|
26
26
|
toJSON() {
|
|
27
27
|
return {
|
|
28
|
+
// This is required because "name" field is not present in TableV2
|
|
28
29
|
tables: this.tables.map((t) => t.toJSON())
|
|
29
30
|
};
|
|
30
31
|
}
|
|
31
32
|
convertToClassicTables(props) {
|
|
32
33
|
return Object.entries(props).map(([name, table]) => {
|
|
33
|
-
|
|
34
|
+
const convertedTable = new Table({
|
|
35
|
+
name,
|
|
36
|
+
columns: table.columns,
|
|
37
|
+
indexes: table.indexes,
|
|
38
|
+
localOnly: table.localOnly,
|
|
39
|
+
insertOnly: table.insertOnly,
|
|
40
|
+
viewName: table.viewNameOverride || name
|
|
41
|
+
});
|
|
42
|
+
return convertedTable;
|
|
34
43
|
});
|
|
35
44
|
}
|
|
36
45
|
}
|
|
37
|
-
//# sourceMappingURL=Schema.js.map
|
package/lib/db/schema/Table.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Column } from '
|
|
2
|
-
import
|
|
3
|
-
import { TableV2 } from './TableV2';
|
|
1
|
+
import { Column, ColumnsType, ColumnType, ExtractColumnValueType } from './Column.js';
|
|
2
|
+
import { Index } from './Index.js';
|
|
3
|
+
import { TableV2 } from './TableV2.js';
|
|
4
4
|
export interface TableOptions {
|
|
5
5
|
/**
|
|
6
6
|
* The synced table name, matching sync rules
|
|
@@ -12,18 +12,98 @@ export interface TableOptions {
|
|
|
12
12
|
insertOnly?: boolean;
|
|
13
13
|
viewName?: string;
|
|
14
14
|
}
|
|
15
|
-
export
|
|
15
|
+
export type RowType<T extends TableV2<any>> = {
|
|
16
|
+
[K in keyof T['columnMap']]: ExtractColumnValueType<T['columnMap'][K]>;
|
|
17
|
+
} & {
|
|
18
|
+
id: string;
|
|
19
|
+
};
|
|
20
|
+
export type IndexShorthand = Record<string, string[]>;
|
|
21
|
+
export interface TableV2Options {
|
|
22
|
+
indexes?: IndexShorthand;
|
|
23
|
+
localOnly?: boolean;
|
|
24
|
+
insertOnly?: boolean;
|
|
25
|
+
viewName?: string;
|
|
26
|
+
}
|
|
27
|
+
export declare const DEFAULT_TABLE_OPTIONS: {
|
|
28
|
+
indexes: never[];
|
|
29
|
+
insertOnly: boolean;
|
|
30
|
+
localOnly: boolean;
|
|
31
|
+
};
|
|
16
32
|
export declare const InvalidSQLCharacters: RegExp;
|
|
17
|
-
export declare class Table {
|
|
33
|
+
export declare class Table<Columns extends ColumnsType = ColumnsType> {
|
|
18
34
|
protected options: TableOptions;
|
|
19
|
-
|
|
20
|
-
static
|
|
21
|
-
static
|
|
35
|
+
protected _mappedColumns: Columns;
|
|
36
|
+
static createLocalOnly(options: TableOptions): Table<ColumnsType>;
|
|
37
|
+
static createInsertOnly(options: TableOptions): Table<ColumnsType>;
|
|
38
|
+
/**
|
|
39
|
+
* Create a table.
|
|
40
|
+
* @deprecated This was only only included for TableV2 and is no longer necessary.
|
|
41
|
+
* Prefer to use new Table() directly.
|
|
42
|
+
*
|
|
43
|
+
* TODO remove in the next major release.
|
|
44
|
+
*/
|
|
45
|
+
static createTable(name: string, table: Table): Table<ColumnsType>;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new Table instance.
|
|
48
|
+
*
|
|
49
|
+
* This constructor supports two different versions:
|
|
50
|
+
* 1. New constructor: Using a Columns object and an optional TableV2Options object
|
|
51
|
+
* 2. Deprecated constructor: Using a TableOptions object (will be removed in the next major release)
|
|
52
|
+
*
|
|
53
|
+
* @constructor
|
|
54
|
+
* @param {Columns | TableOptions} optionsOrColumns - Either a Columns object (for V2 syntax) or a TableOptions object (for V1 syntax)
|
|
55
|
+
* @param {TableV2Options} [v2Options] - Optional configuration options for V2 syntax
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```javascript
|
|
59
|
+
* // New Constructor
|
|
60
|
+
* const table = new Table(
|
|
61
|
+
* {
|
|
62
|
+
* name: column.text,
|
|
63
|
+
* age: column.integer
|
|
64
|
+
* },
|
|
65
|
+
* { indexes: { nameIndex: ['name'] } }
|
|
66
|
+
* );
|
|
67
|
+
*```
|
|
68
|
+
*
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```javascript
|
|
72
|
+
* // Deprecated Constructor
|
|
73
|
+
* const table = new Table({
|
|
74
|
+
* name: 'users',
|
|
75
|
+
* columns: [
|
|
76
|
+
* new Column({ name: 'name', type: ColumnType.TEXT }),
|
|
77
|
+
* new Column({ name: 'age', type: ColumnType.INTEGER })
|
|
78
|
+
* ]
|
|
79
|
+
* });
|
|
80
|
+
*```
|
|
81
|
+
*/
|
|
82
|
+
constructor(columns: Columns, options?: TableV2Options);
|
|
83
|
+
/**
|
|
84
|
+
* @deprecated This constructor will be removed in the next major release.
|
|
85
|
+
* Use the new constructor shown below instead as this does not show types.
|
|
86
|
+
* @example
|
|
87
|
+
* <caption>Use this instead</caption>
|
|
88
|
+
* ```javascript
|
|
89
|
+
* const table = new Table(
|
|
90
|
+
* {
|
|
91
|
+
* name: column.text,
|
|
92
|
+
* age: column.integer
|
|
93
|
+
* },
|
|
94
|
+
* { indexes: { nameIndex: ['name'] } }
|
|
95
|
+
* );
|
|
96
|
+
*```
|
|
97
|
+
*/
|
|
22
98
|
constructor(options: TableOptions);
|
|
99
|
+
private isTableV1;
|
|
100
|
+
private initTableV1;
|
|
101
|
+
private initTableV2;
|
|
23
102
|
get name(): string;
|
|
24
103
|
get viewNameOverride(): string | undefined;
|
|
25
104
|
get viewName(): string;
|
|
26
105
|
get columns(): Column[];
|
|
106
|
+
get columnMap(): Columns;
|
|
27
107
|
get indexes(): Index[];
|
|
28
108
|
get localOnly(): boolean;
|
|
29
109
|
get insertOnly(): boolean;
|
|
@@ -37,14 +117,14 @@ export declare class Table {
|
|
|
37
117
|
insert_only: boolean;
|
|
38
118
|
columns: {
|
|
39
119
|
name: string;
|
|
40
|
-
type:
|
|
120
|
+
type: ColumnType | undefined;
|
|
41
121
|
}[];
|
|
42
122
|
indexes: {
|
|
43
123
|
name: string;
|
|
44
124
|
columns: {
|
|
45
125
|
name: string;
|
|
46
126
|
ascending: boolean | undefined;
|
|
47
|
-
type:
|
|
127
|
+
type: ColumnType;
|
|
48
128
|
}[];
|
|
49
129
|
}[];
|
|
50
130
|
};
|
package/lib/db/schema/Table.js
CHANGED
|
@@ -1,31 +1,75 @@
|
|
|
1
|
-
import { Column } from '
|
|
1
|
+
import { Column, ColumnType, MAX_AMOUNT_OF_COLUMNS } from './Column.js';
|
|
2
|
+
import { Index } from './Index.js';
|
|
3
|
+
import { IndexedColumn } from './IndexedColumn.js';
|
|
2
4
|
export const DEFAULT_TABLE_OPTIONS = {
|
|
3
5
|
indexes: [],
|
|
4
6
|
insertOnly: false,
|
|
5
7
|
localOnly: false
|
|
6
8
|
};
|
|
7
|
-
const MAX_AMOUNT_OF_COLUMNS = 63;
|
|
8
9
|
export const InvalidSQLCharacters = /["'%,.#\s[\]]/;
|
|
9
10
|
export class Table {
|
|
10
11
|
options;
|
|
12
|
+
_mappedColumns;
|
|
11
13
|
static createLocalOnly(options) {
|
|
12
14
|
return new Table({ ...options, localOnly: true, insertOnly: false });
|
|
13
15
|
}
|
|
14
16
|
static createInsertOnly(options) {
|
|
15
17
|
return new Table({ ...options, localOnly: false, insertOnly: true });
|
|
16
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Create a table.
|
|
21
|
+
* @deprecated This was only only included for TableV2 and is no longer necessary.
|
|
22
|
+
* Prefer to use new Table() directly.
|
|
23
|
+
*
|
|
24
|
+
* TODO remove in the next major release.
|
|
25
|
+
*/
|
|
17
26
|
static createTable(name, table) {
|
|
18
27
|
return new Table({
|
|
19
28
|
name,
|
|
20
|
-
columns:
|
|
29
|
+
columns: table.columns,
|
|
21
30
|
indexes: table.indexes,
|
|
22
31
|
localOnly: table.options.localOnly,
|
|
23
32
|
insertOnly: table.options.insertOnly,
|
|
24
33
|
viewName: table.options.viewName
|
|
25
34
|
});
|
|
26
35
|
}
|
|
27
|
-
constructor(
|
|
28
|
-
this.
|
|
36
|
+
constructor(optionsOrColumns, v2Options) {
|
|
37
|
+
if (this.isTableV1(optionsOrColumns)) {
|
|
38
|
+
this.initTableV1(optionsOrColumns);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.initTableV2(optionsOrColumns, v2Options);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
isTableV1(arg) {
|
|
45
|
+
return 'columns' in arg && Array.isArray(arg.columns);
|
|
46
|
+
}
|
|
47
|
+
initTableV1(options) {
|
|
48
|
+
this.options = {
|
|
49
|
+
...options,
|
|
50
|
+
indexes: options.indexes || [],
|
|
51
|
+
insertOnly: options.insertOnly ?? DEFAULT_TABLE_OPTIONS.insertOnly,
|
|
52
|
+
localOnly: options.localOnly ?? DEFAULT_TABLE_OPTIONS.localOnly
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
initTableV2(columns, options) {
|
|
56
|
+
const convertedColumns = Object.entries(columns).map(([name, columnInfo]) => new Column({ name, type: columnInfo.type }));
|
|
57
|
+
const convertedIndexes = Object.entries(options?.indexes ?? {}).map(([name, columnNames]) => new Index({
|
|
58
|
+
name,
|
|
59
|
+
columns: columnNames.map((name) => new IndexedColumn({
|
|
60
|
+
name: name.replace(/^-/, ''),
|
|
61
|
+
ascending: !name.startsWith('-')
|
|
62
|
+
}))
|
|
63
|
+
}));
|
|
64
|
+
this.options = {
|
|
65
|
+
name: '',
|
|
66
|
+
columns: convertedColumns,
|
|
67
|
+
indexes: convertedIndexes,
|
|
68
|
+
insertOnly: options?.insertOnly ?? DEFAULT_TABLE_OPTIONS.insertOnly,
|
|
69
|
+
localOnly: options?.localOnly ?? DEFAULT_TABLE_OPTIONS.localOnly,
|
|
70
|
+
viewName: options?.viewName
|
|
71
|
+
};
|
|
72
|
+
this._mappedColumns = columns;
|
|
29
73
|
}
|
|
30
74
|
get name() {
|
|
31
75
|
return this.options.name;
|
|
@@ -39,6 +83,13 @@ export class Table {
|
|
|
39
83
|
get columns() {
|
|
40
84
|
return this.options.columns;
|
|
41
85
|
}
|
|
86
|
+
get columnMap() {
|
|
87
|
+
return (this._mappedColumns ??
|
|
88
|
+
this.columns.reduce((hash, column) => {
|
|
89
|
+
hash[column.name] = { type: column.type ?? ColumnType.TEXT };
|
|
90
|
+
return hash;
|
|
91
|
+
}, {}));
|
|
92
|
+
}
|
|
42
93
|
get indexes() {
|
|
43
94
|
return this.options.indexes ?? [];
|
|
44
95
|
}
|
|
@@ -71,30 +122,30 @@ export class Table {
|
|
|
71
122
|
throw new Error(`Invalid characters in view name: ${this.viewNameOverride}`);
|
|
72
123
|
}
|
|
73
124
|
if (this.columns.length > MAX_AMOUNT_OF_COLUMNS) {
|
|
74
|
-
throw new Error(`Table
|
|
125
|
+
throw new Error(`Table has too many columns. The maximum number of columns is ${MAX_AMOUNT_OF_COLUMNS}.`);
|
|
75
126
|
}
|
|
76
127
|
const columnNames = new Set();
|
|
77
128
|
columnNames.add('id');
|
|
78
129
|
for (const column of this.columns) {
|
|
79
130
|
const { name: columnName } = column;
|
|
80
131
|
if (column.name === 'id') {
|
|
81
|
-
throw new Error(
|
|
132
|
+
throw new Error(`An id column is automatically added, custom id columns are not supported`);
|
|
82
133
|
}
|
|
83
134
|
if (columnNames.has(columnName)) {
|
|
84
135
|
throw new Error(`Duplicate column ${columnName}`);
|
|
85
136
|
}
|
|
86
137
|
if (InvalidSQLCharacters.test(columnName)) {
|
|
87
|
-
throw new Error(`Invalid characters in column name: $
|
|
138
|
+
throw new Error(`Invalid characters in column name: ${column.name}`);
|
|
88
139
|
}
|
|
89
140
|
columnNames.add(columnName);
|
|
90
141
|
}
|
|
91
142
|
const indexNames = new Set();
|
|
92
143
|
for (const index of this.indexes) {
|
|
93
144
|
if (indexNames.has(index.name)) {
|
|
94
|
-
throw new Error(`Duplicate index $
|
|
145
|
+
throw new Error(`Duplicate index ${index.name}`);
|
|
95
146
|
}
|
|
96
147
|
if (InvalidSQLCharacters.test(index.name)) {
|
|
97
|
-
throw new Error(`Invalid characters in index name: $
|
|
148
|
+
throw new Error(`Invalid characters in index name: ${index.name}`);
|
|
98
149
|
}
|
|
99
150
|
for (const column of index.columns) {
|
|
100
151
|
if (!columnNames.has(column.name)) {
|
|
@@ -115,4 +166,3 @@ export class Table {
|
|
|
115
166
|
};
|
|
116
167
|
}
|
|
117
168
|
}
|
|
118
|
-
//# sourceMappingURL=Table.js.map
|
|
@@ -1,31 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
real: BaseColumnType<number | null>;
|
|
10
|
-
};
|
|
11
|
-
export type ColumnsType = Record<string, BaseColumnType<any>>;
|
|
12
|
-
export type ExtractColumnValueType<T extends BaseColumnType<any>> = T extends BaseColumnType<infer R> ? R : unknown;
|
|
13
|
-
export type RowType<T extends TableV2<any>> = {
|
|
14
|
-
[K in keyof T['columns']]: ExtractColumnValueType<T['columns'][K]>;
|
|
15
|
-
} & {
|
|
16
|
-
id: string;
|
|
17
|
-
};
|
|
18
|
-
export type IndexShorthand = Record<string, string[]>;
|
|
19
|
-
export interface TableV2Options {
|
|
20
|
-
indexes?: IndexShorthand;
|
|
21
|
-
localOnly?: boolean;
|
|
22
|
-
insertOnly?: boolean;
|
|
23
|
-
viewName?: string;
|
|
24
|
-
}
|
|
25
|
-
export declare class TableV2<Columns extends ColumnsType = ColumnsType> {
|
|
26
|
-
columns: Columns;
|
|
27
|
-
options: TableV2Options;
|
|
28
|
-
indexes: Index[];
|
|
29
|
-
constructor(columns: Columns, options?: TableV2Options);
|
|
30
|
-
private validateTable;
|
|
1
|
+
import { ColumnsType } from './Column.js';
|
|
2
|
+
import { Table } from './Table.js';
|
|
3
|
+
/**
|
|
4
|
+
Generate a new table from the columns and indexes
|
|
5
|
+
@deprecated You should use {@link Table} instead as it now allows TableV2 syntax.
|
|
6
|
+
This will be removed in the next major release.
|
|
7
|
+
*/
|
|
8
|
+
export declare class TableV2<Columns extends ColumnsType = ColumnsType> extends Table<Columns> {
|
|
31
9
|
}
|