@powersync/service-module-mssql 0.0.1
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/LICENSE +67 -0
- package/README.md +3 -0
- package/ci/init-mssql.sql +50 -0
- package/dist/api/MSSQLRouteAPIAdapter.d.ts +21 -0
- package/dist/api/MSSQLRouteAPIAdapter.js +248 -0
- package/dist/api/MSSQLRouteAPIAdapter.js.map +1 -0
- package/dist/common/LSN.d.ts +37 -0
- package/dist/common/LSN.js +64 -0
- package/dist/common/LSN.js.map +1 -0
- package/dist/common/MSSQLSourceTable.d.ts +27 -0
- package/dist/common/MSSQLSourceTable.js +35 -0
- package/dist/common/MSSQLSourceTable.js.map +1 -0
- package/dist/common/MSSQLSourceTableCache.d.ts +14 -0
- package/dist/common/MSSQLSourceTableCache.js +28 -0
- package/dist/common/MSSQLSourceTableCache.js.map +1 -0
- package/dist/common/mssqls-to-sqlite.d.ts +18 -0
- package/dist/common/mssqls-to-sqlite.js +143 -0
- package/dist/common/mssqls-to-sqlite.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/module/MSSQLModule.d.ts +15 -0
- package/dist/module/MSSQLModule.js +68 -0
- package/dist/module/MSSQLModule.js.map +1 -0
- package/dist/replication/CDCPoller.d.ts +67 -0
- package/dist/replication/CDCPoller.js +183 -0
- package/dist/replication/CDCPoller.js.map +1 -0
- package/dist/replication/CDCReplicationJob.d.ts +17 -0
- package/dist/replication/CDCReplicationJob.js +76 -0
- package/dist/replication/CDCReplicationJob.js.map +1 -0
- package/dist/replication/CDCReplicator.d.ts +18 -0
- package/dist/replication/CDCReplicator.js +55 -0
- package/dist/replication/CDCReplicator.js.map +1 -0
- package/dist/replication/CDCStream.d.ts +106 -0
- package/dist/replication/CDCStream.js +536 -0
- package/dist/replication/CDCStream.js.map +1 -0
- package/dist/replication/MSSQLConnectionManager.d.ts +23 -0
- package/dist/replication/MSSQLConnectionManager.js +97 -0
- package/dist/replication/MSSQLConnectionManager.js.map +1 -0
- package/dist/replication/MSSQLConnectionManagerFactory.d.ts +10 -0
- package/dist/replication/MSSQLConnectionManagerFactory.js +28 -0
- package/dist/replication/MSSQLConnectionManagerFactory.js.map +1 -0
- package/dist/replication/MSSQLErrorRateLimiter.d.ts +10 -0
- package/dist/replication/MSSQLErrorRateLimiter.js +34 -0
- package/dist/replication/MSSQLErrorRateLimiter.js.map +1 -0
- package/dist/replication/MSSQLSnapshotQuery.d.ts +71 -0
- package/dist/replication/MSSQLSnapshotQuery.js +190 -0
- package/dist/replication/MSSQLSnapshotQuery.js.map +1 -0
- package/dist/types/mssql-data-types.d.ts +66 -0
- package/dist/types/mssql-data-types.js +62 -0
- package/dist/types/mssql-data-types.js.map +1 -0
- package/dist/types/types.d.ts +177 -0
- package/dist/types/types.js +141 -0
- package/dist/types/types.js.map +1 -0
- package/dist/utils/mssql.d.ts +80 -0
- package/dist/utils/mssql.js +329 -0
- package/dist/utils/mssql.js.map +1 -0
- package/dist/utils/schema.d.ts +21 -0
- package/dist/utils/schema.js +131 -0
- package/dist/utils/schema.js.map +1 -0
- package/package.json +51 -0
- package/src/api/MSSQLRouteAPIAdapter.ts +283 -0
- package/src/common/LSN.ts +77 -0
- package/src/common/MSSQLSourceTable.ts +54 -0
- package/src/common/MSSQLSourceTableCache.ts +36 -0
- package/src/common/mssqls-to-sqlite.ts +151 -0
- package/src/index.ts +1 -0
- package/src/module/MSSQLModule.ts +82 -0
- package/src/replication/CDCPoller.ts +241 -0
- package/src/replication/CDCReplicationJob.ts +87 -0
- package/src/replication/CDCReplicator.ts +70 -0
- package/src/replication/CDCStream.ts +688 -0
- package/src/replication/MSSQLConnectionManager.ts +113 -0
- package/src/replication/MSSQLConnectionManagerFactory.ts +33 -0
- package/src/replication/MSSQLErrorRateLimiter.ts +36 -0
- package/src/replication/MSSQLSnapshotQuery.ts +230 -0
- package/src/types/mssql-data-types.ts +79 -0
- package/src/types/types.ts +224 -0
- package/src/utils/mssql.ts +420 -0
- package/src/utils/schema.ts +172 -0
- package/test/src/CDCStream.test.ts +206 -0
- package/test/src/CDCStreamTestContext.ts +212 -0
- package/test/src/CDCStream_resumable_snapshot.test.ts +152 -0
- package/test/src/env.ts +11 -0
- package/test/src/mssql-to-sqlite.test.ts +474 -0
- package/test/src/setup.ts +12 -0
- package/test/src/util.ts +189 -0
- package/test/tsconfig.json +28 -0
- package/test/tsconfig.tsbuildinfo +1 -0
- package/tsconfig.json +26 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/vitest.config.ts +15 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { BaseObserver, logger } from '@powersync/lib-services-framework';
|
|
2
|
+
import sql from 'mssql';
|
|
3
|
+
import { POWERSYNC_VERSION } from '@powersync/service-core';
|
|
4
|
+
import { addParameters } from '../utils/mssql.js';
|
|
5
|
+
export const DEFAULT_SCHEMA = 'dbo';
|
|
6
|
+
export class MSSQLConnectionManager extends BaseObserver {
|
|
7
|
+
options;
|
|
8
|
+
pool;
|
|
9
|
+
constructor(options, poolOptions) {
|
|
10
|
+
super();
|
|
11
|
+
this.options = options;
|
|
12
|
+
// The pool is lazy - no connections are opened until a query is performed.
|
|
13
|
+
this.pool = new sql.ConnectionPool({
|
|
14
|
+
authentication: options.authentication,
|
|
15
|
+
user: options.username,
|
|
16
|
+
password: options.password,
|
|
17
|
+
server: options.hostname,
|
|
18
|
+
port: options.port,
|
|
19
|
+
database: options.database,
|
|
20
|
+
pool: poolOptions,
|
|
21
|
+
options: {
|
|
22
|
+
appName: `powersync/${POWERSYNC_VERSION}`,
|
|
23
|
+
encrypt: true, // Required for Azure
|
|
24
|
+
trustServerCertificate: options.trustServerCertificate
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
get connectionTag() {
|
|
29
|
+
return this.options.tag;
|
|
30
|
+
}
|
|
31
|
+
get connectionId() {
|
|
32
|
+
return this.options.id;
|
|
33
|
+
}
|
|
34
|
+
get databaseName() {
|
|
35
|
+
return this.options.database;
|
|
36
|
+
}
|
|
37
|
+
get schema() {
|
|
38
|
+
return this.options.schema ?? DEFAULT_SCHEMA;
|
|
39
|
+
}
|
|
40
|
+
async ensureConnected() {
|
|
41
|
+
await this.pool.connect();
|
|
42
|
+
}
|
|
43
|
+
async createTransaction() {
|
|
44
|
+
await this.ensureConnected();
|
|
45
|
+
return this.pool.transaction();
|
|
46
|
+
}
|
|
47
|
+
async createRequest() {
|
|
48
|
+
await this.ensureConnected();
|
|
49
|
+
return this.pool.request();
|
|
50
|
+
}
|
|
51
|
+
async query(query, parameters) {
|
|
52
|
+
await this.ensureConnected();
|
|
53
|
+
for (let tries = 2;; tries--) {
|
|
54
|
+
try {
|
|
55
|
+
logger.debug(`Executing query: ${query}`);
|
|
56
|
+
let request = this.pool.request();
|
|
57
|
+
if (parameters) {
|
|
58
|
+
request = addParameters(request, parameters);
|
|
59
|
+
}
|
|
60
|
+
return await request.query(query);
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
if (tries == 1) {
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
66
|
+
logger.warn('Query error, retrying..', e);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async execute(procedure, parameters) {
|
|
71
|
+
await this.ensureConnected();
|
|
72
|
+
let request = this.pool.request();
|
|
73
|
+
if (parameters) {
|
|
74
|
+
if (parameters) {
|
|
75
|
+
request = addParameters(request, parameters);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return request.execute(procedure);
|
|
79
|
+
}
|
|
80
|
+
async end() {
|
|
81
|
+
if (this.pool.connected) {
|
|
82
|
+
try {
|
|
83
|
+
await this.pool.close();
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
// We don't particularly care if any errors are thrown when shutting down the pool
|
|
87
|
+
logger.warn('Error shutting down MSSQL connection pool', error);
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
this.iterateListeners((listener) => {
|
|
91
|
+
listener.onEnded?.();
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=MSSQLConnectionManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MSSQLConnectionManager.js","sourceRoot":"","sources":["../../src/replication/MSSQLConnectionManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,GAAG,MAAM,OAAO,CAAC;AAExB,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,CAAC;AAMpC,MAAM,OAAO,sBAAuB,SAAQ,YAA4C;IAI7E;IAHQ,IAAI,CAAqB;IAE1C,YACS,OAAwC,EAC/C,WAAyC;QAEzC,KAAK,EAAE,CAAC;QAHD,YAAO,GAAP,OAAO,CAAiC;QAI/C,2EAA2E;QAC3E,IAAI,CAAC,IAAI,GAAG,IAAI,GAAG,CAAC,cAAc,CAAC;YACjC,cAAc,EAAE,OAAO,CAAC,cAAc;YACtC,IAAI,EAAE,OAAO,CAAC,QAAQ;YACtB,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,MAAM,EAAE,OAAO,CAAC,QAAQ;YACxB,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,IAAI,EAAE,WAAW;YACjB,OAAO,EAAE;gBACP,OAAO,EAAE,aAAa,iBAAiB,EAAE;gBACzC,OAAO,EAAE,IAAI,EAAE,qBAAqB;gBACpC,sBAAsB,EAAE,OAAO,CAAC,sBAAsB;aACvD;SACF,CAAC,CAAC;IACL,CAAC;IAED,IAAW,aAAa;Q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AAM,CAAC,CAAC;gBACV,CAAC;gBACD,MAAM,CAAC,IAAI,CAAC,yBAAyB,EAAE,CAAC,CAAC,CAAC;YAC5C,CAAC;QACH,CAAC;IACH,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,SAAiB,EAAE,UAA6B;QAC5D,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;QAC7B,IAAI,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;QAClC,IAAI,UAAU,EAAE,CAAC;YACf,IAAI,UAAU,EAAE,CAAC;gBACf,OAAO,GAAG,aAAa,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;YAC/C,CAAC;QACH,CAAC;QACD,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;IAED,KAAK,CAAC,GAAG;QACP,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACxB,IAAI,CAAC;gBACH,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;YAC1B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,kFAAkF;gBAClF,MAAM,CAAC,IAAI,CAAC,2CAA2C,EAAE,KAAK,CAAC,CAAC;YAClE,CAAC;oBAAS,CAAC;gBACT,IAAI,CAAC,gBAAgB,CAAC,CAAC,QAAQ,EAAE,EAAE;oBACjC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;gBACvB,CAAC,CAAC,CAAC;YACL,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ResolvedMSSQLConnectionConfig } from '../types/types.js';
|
|
2
|
+
import { MSSQLConnectionManager } from './MSSQLConnectionManager.js';
|
|
3
|
+
import sql from 'mssql';
|
|
4
|
+
export declare class MSSQLConnectionManagerFactory {
|
|
5
|
+
private readonly connectionManagers;
|
|
6
|
+
readonly connectionConfig: ResolvedMSSQLConnectionConfig;
|
|
7
|
+
constructor(connectionConfig: ResolvedMSSQLConnectionConfig);
|
|
8
|
+
create(poolOptions: sql.PoolOpts<sql.Connection>): MSSQLConnectionManager;
|
|
9
|
+
shutdown(): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { logger } from '@powersync/lib-services-framework';
|
|
2
|
+
import { MSSQLConnectionManager } from './MSSQLConnectionManager.js';
|
|
3
|
+
export class MSSQLConnectionManagerFactory {
|
|
4
|
+
connectionManagers;
|
|
5
|
+
connectionConfig;
|
|
6
|
+
constructor(connectionConfig) {
|
|
7
|
+
this.connectionConfig = connectionConfig;
|
|
8
|
+
this.connectionManagers = new Set();
|
|
9
|
+
}
|
|
10
|
+
create(poolOptions) {
|
|
11
|
+
const manager = new MSSQLConnectionManager(this.connectionConfig, poolOptions);
|
|
12
|
+
manager.registerListener({
|
|
13
|
+
onEnded: () => {
|
|
14
|
+
this.connectionManagers.delete(manager);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
this.connectionManagers.add(manager);
|
|
18
|
+
return manager;
|
|
19
|
+
}
|
|
20
|
+
async shutdown() {
|
|
21
|
+
logger.info('Shutting down MSSQL connection Managers...');
|
|
22
|
+
for (const manager of this.connectionManagers.values()) {
|
|
23
|
+
await manager.end();
|
|
24
|
+
}
|
|
25
|
+
logger.info('MSSQL connection Managers shutdown completed.');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=MSSQLConnectionManagerFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MSSQLConnectionManagerFactory.js","sourceRoot":"","sources":["../../src/replication/MSSQLConnectionManagerFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAE3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAGrE,MAAM,OAAO,6BAA6B;IACvB,kBAAkB,CAA8B;IACjD,gBAAgB,CAAgC;IAEhE,YAAY,gBAA+C;QACzD,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,kBAAkB,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC9D,CAAC;IAED,MAAM,CAAC,WAAyC;QAC9C,MAAM,OAAO,GAAG,IAAI,sBAAsB,CAAC,IAAI,CAAC,gBAAgB,EAAE,WAAW,CAAC,CAAC;QAC/E,OAAO,CAAC,gBAAgB,CAAC;YACvB,OAAO,EAAE,GAAG,EAAE;gBACZ,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAC1C,CAAC;SACF,CAAC,CAAC;QACH,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACrC,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,KAAK,CAAC,QAAQ;QACZ,MAAM,CAAC,IAAI,CAAC,4CAA4C,CAAC,CAAC;QAC1D,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,kBAAkB,CAAC,MAAM,EAAE,EAAE,CAAC;YACvD,MAAM,OAAO,CAAC,GAAG,EAAE,CAAC;QACtB,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,+CAA+C,CAAC,CAAC;IAC/D,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ErrorRateLimiter } from '@powersync/service-core';
|
|
2
|
+
export declare class MSSQLErrorRateLimiter implements ErrorRateLimiter {
|
|
3
|
+
nextAllowed: number;
|
|
4
|
+
waitUntilAllowed(options?: {
|
|
5
|
+
signal?: AbortSignal | undefined;
|
|
6
|
+
} | undefined): Promise<void>;
|
|
7
|
+
mayPing(): boolean;
|
|
8
|
+
reportError(e: any): void;
|
|
9
|
+
private setDelay;
|
|
10
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { setTimeout } from 'timers/promises';
|
|
2
|
+
export class MSSQLErrorRateLimiter {
|
|
3
|
+
nextAllowed = Date.now();
|
|
4
|
+
async waitUntilAllowed(options) {
|
|
5
|
+
const delay = Math.max(0, this.nextAllowed - Date.now());
|
|
6
|
+
// Minimum delay between connections, even without errors
|
|
7
|
+
this.setDelay(500);
|
|
8
|
+
await setTimeout(delay, undefined, { signal: options?.signal });
|
|
9
|
+
}
|
|
10
|
+
mayPing() {
|
|
11
|
+
return Date.now() >= this.nextAllowed;
|
|
12
|
+
}
|
|
13
|
+
reportError(e) {
|
|
14
|
+
const message = e.message ?? '';
|
|
15
|
+
if (message.includes('password authentication failed')) {
|
|
16
|
+
this.setDelay(900_000);
|
|
17
|
+
}
|
|
18
|
+
else if (message.includes('ENOTFOUND')) {
|
|
19
|
+
// DNS lookup issue - incorrect URI or deleted instance
|
|
20
|
+
this.setDelay(120_000);
|
|
21
|
+
}
|
|
22
|
+
else if (message.includes('ECONNREFUSED')) {
|
|
23
|
+
// Could be fail2ban or similar
|
|
24
|
+
this.setDelay(120_000);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this.setDelay(30_000);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
setDelay(delay) {
|
|
31
|
+
this.nextAllowed = Math.max(this.nextAllowed, Date.now() + delay);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=MSSQLErrorRateLimiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MSSQLErrorRateLimiter.js","sourceRoot":"","sources":["../../src/replication/MSSQLErrorRateLimiter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,MAAM,OAAO,qBAAqB;IAChC,WAAW,GAAW,IAAI,CAAC,GAAG,EAAE,CAAC;IAEjC,KAAK,CAAC,gBAAgB,CAAC,OAA0D;QAC/E,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;QACzD,yDAAyD;QACzD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACnB,MAAM,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;IAClE,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,WAAW,CAAC;IACxC,CAAC;IAED,WAAW,CAAC,CAAM;QAChB,MAAM,OAAO,GAAI,CAAC,CAAC,OAAkB,IAAI,EAAE,CAAC;QAC5C,IAAI,OAAO,CAAC,QAAQ,CAAC,gCAAgC,CAAC,EAAE,CAAC;YACvD,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QACzB,CAAC;aAAM,IAAI,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;YACzC,uDAAuD;YACvD,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QACzB,CAAC;aAAM,IAAI,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;YAC5C,+BAA+B;YAC/B,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QACzB,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IAEO,QAAQ,CAAC,KAAa;QAC5B,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC;IACpE,CAAC;CACF"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { SourceTable } from '@powersync/service-core';
|
|
2
|
+
import { SqliteValue } from '@powersync/service-sync-rules';
|
|
3
|
+
import { MSSQLBaseType } from '../types/mssql-data-types.js';
|
|
4
|
+
import sql from 'mssql';
|
|
5
|
+
import { MSSQLSourceTable } from '../common/MSSQLSourceTable.js';
|
|
6
|
+
export interface MSSQLSnapshotQuery {
|
|
7
|
+
initialize(): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Returns an async iterable iterator that yields the column metadata for the query followed by rows of data.
|
|
10
|
+
*/
|
|
11
|
+
next(): AsyncIterableIterator<sql.IColumnMetadata | sql.IRecordSet<any>>;
|
|
12
|
+
}
|
|
13
|
+
export type PrimaryKeyValue = Record<string, SqliteValue>;
|
|
14
|
+
/**
|
|
15
|
+
* Snapshot query using a plain SELECT * FROM table
|
|
16
|
+
*
|
|
17
|
+
* This supports all tables but does not efficiently resume the snapshot
|
|
18
|
+
* if the process is restarted.
|
|
19
|
+
*/
|
|
20
|
+
export declare class SimpleSnapshotQuery implements MSSQLSnapshotQuery {
|
|
21
|
+
private readonly transaction;
|
|
22
|
+
private readonly table;
|
|
23
|
+
constructor(transaction: sql.Transaction, table: MSSQLSourceTable);
|
|
24
|
+
initialize(): Promise<void>;
|
|
25
|
+
next(): AsyncIterableIterator<sql.IColumnMetadata | sql.IRecordSet<any>>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Performs a table snapshot query, batching by ranges of primary key data.
|
|
29
|
+
*
|
|
30
|
+
* This may miss some rows if they are modified during the snapshot query.
|
|
31
|
+
* In that case, replication will pick up those rows afterward,
|
|
32
|
+
* possibly resulting in an IdSnapshotQuery.
|
|
33
|
+
*
|
|
34
|
+
* Currently, this only supports a table with a single primary key column,
|
|
35
|
+
* of a select few types.
|
|
36
|
+
*/
|
|
37
|
+
export declare class BatchedSnapshotQuery implements MSSQLSnapshotQuery {
|
|
38
|
+
private readonly transaction;
|
|
39
|
+
private readonly table;
|
|
40
|
+
private readonly batchSize;
|
|
41
|
+
/**
|
|
42
|
+
* Primary key types that we support for batched snapshots.
|
|
43
|
+
*
|
|
44
|
+
* Can expand this over time as we add more tests,
|
|
45
|
+
* and ensure there are no issues with type conversion.
|
|
46
|
+
*/
|
|
47
|
+
static SUPPORTED_TYPES: MSSQLBaseType[];
|
|
48
|
+
static supports(table: SourceTable | MSSQLSourceTable): boolean;
|
|
49
|
+
private readonly key;
|
|
50
|
+
lastKey: string | bigint | null;
|
|
51
|
+
constructor(transaction: sql.Transaction, table: MSSQLSourceTable, batchSize: number | undefined, lastKeySerialized: Uint8Array | null);
|
|
52
|
+
initialize(): Promise<void>;
|
|
53
|
+
getLastKeySerialized(): Uint8Array;
|
|
54
|
+
next(): AsyncIterableIterator<sql.IColumnMetadata | sql.IRecordSet<any>>;
|
|
55
|
+
private deserializeKey;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* This performs a snapshot query using a list of primary keys.
|
|
59
|
+
*
|
|
60
|
+
* This is not used for general snapshots, but is used when we need to re-fetch specific rows
|
|
61
|
+
* during streaming replication.
|
|
62
|
+
*/
|
|
63
|
+
export declare class IdSnapshotQuery implements MSSQLSnapshotQuery {
|
|
64
|
+
private readonly transaction;
|
|
65
|
+
private readonly table;
|
|
66
|
+
private readonly keys;
|
|
67
|
+
static supports(table: SourceTable | MSSQLSourceTable): boolean;
|
|
68
|
+
constructor(transaction: sql.Transaction, table: MSSQLSourceTable, keys: PrimaryKeyValue[]);
|
|
69
|
+
initialize(): Promise<void>;
|
|
70
|
+
next(): AsyncIterableIterator<sql.IColumnMetadata | sql.IRecordSet<any>>;
|
|
71
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { bson } from '@powersync/service-core';
|
|
2
|
+
import { ServiceAssertionError } from '@powersync/lib-services-framework';
|
|
3
|
+
import { MSSQLBaseType } from '../types/mssql-data-types.js';
|
|
4
|
+
import { escapeIdentifier } from '../utils/mssql.js';
|
|
5
|
+
import { MSSQLSourceTable } from '../common/MSSQLSourceTable.js';
|
|
6
|
+
/**
|
|
7
|
+
* Snapshot query using a plain SELECT * FROM table
|
|
8
|
+
*
|
|
9
|
+
* This supports all tables but does not efficiently resume the snapshot
|
|
10
|
+
* if the process is restarted.
|
|
11
|
+
*/
|
|
12
|
+
export class SimpleSnapshotQuery {
|
|
13
|
+
transaction;
|
|
14
|
+
table;
|
|
15
|
+
constructor(transaction, table) {
|
|
16
|
+
this.transaction = transaction;
|
|
17
|
+
this.table = table;
|
|
18
|
+
}
|
|
19
|
+
async initialize() { }
|
|
20
|
+
async *next() {
|
|
21
|
+
const metadataRequest = this.transaction.request();
|
|
22
|
+
metadataRequest.stream = true;
|
|
23
|
+
const metadataPromise = new Promise((resolve, reject) => {
|
|
24
|
+
metadataRequest.on('recordset', resolve);
|
|
25
|
+
metadataRequest.on('error', reject);
|
|
26
|
+
});
|
|
27
|
+
metadataRequest.query(`SELECT TOP(0) * FROM ${this.table.toQualifiedName()}`);
|
|
28
|
+
const columnMetadata = await metadataPromise;
|
|
29
|
+
yield columnMetadata;
|
|
30
|
+
const request = this.transaction.request();
|
|
31
|
+
const stream = request.toReadableStream();
|
|
32
|
+
request.query(`SELECT * FROM ${this.table.toQualifiedName()}`);
|
|
33
|
+
// MSSQL only streams one row at a time
|
|
34
|
+
for await (const row of stream) {
|
|
35
|
+
yield row;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Performs a table snapshot query, batching by ranges of primary key data.
|
|
41
|
+
*
|
|
42
|
+
* This may miss some rows if they are modified during the snapshot query.
|
|
43
|
+
* In that case, replication will pick up those rows afterward,
|
|
44
|
+
* possibly resulting in an IdSnapshotQuery.
|
|
45
|
+
*
|
|
46
|
+
* Currently, this only supports a table with a single primary key column,
|
|
47
|
+
* of a select few types.
|
|
48
|
+
*/
|
|
49
|
+
export class BatchedSnapshotQuery {
|
|
50
|
+
transaction;
|
|
51
|
+
table;
|
|
52
|
+
batchSize;
|
|
53
|
+
/**
|
|
54
|
+
* Primary key types that we support for batched snapshots.
|
|
55
|
+
*
|
|
56
|
+
* Can expand this over time as we add more tests,
|
|
57
|
+
* and ensure there are no issues with type conversion.
|
|
58
|
+
*/
|
|
59
|
+
static SUPPORTED_TYPES = [
|
|
60
|
+
MSSQLBaseType.TEXT,
|
|
61
|
+
MSSQLBaseType.NTEXT,
|
|
62
|
+
MSSQLBaseType.VARCHAR,
|
|
63
|
+
MSSQLBaseType.NVARCHAR,
|
|
64
|
+
MSSQLBaseType.CHAR,
|
|
65
|
+
MSSQLBaseType.NCHAR,
|
|
66
|
+
MSSQLBaseType.UNIQUEIDENTIFIER,
|
|
67
|
+
MSSQLBaseType.TINYINT,
|
|
68
|
+
MSSQLBaseType.SMALLINT,
|
|
69
|
+
MSSQLBaseType.INT,
|
|
70
|
+
MSSQLBaseType.BIGINT
|
|
71
|
+
];
|
|
72
|
+
static supports(table) {
|
|
73
|
+
const sourceTable = table instanceof MSSQLSourceTable ? table.sourceTable : table;
|
|
74
|
+
if (sourceTable.replicaIdColumns.length != 1) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const primaryKey = sourceTable.replicaIdColumns[0];
|
|
78
|
+
return primaryKey.typeId != null && BatchedSnapshotQuery.SUPPORTED_TYPES.includes(Number(primaryKey.typeId));
|
|
79
|
+
}
|
|
80
|
+
key;
|
|
81
|
+
lastKey = null;
|
|
82
|
+
constructor(transaction, table, batchSize = 10_000, lastKeySerialized) {
|
|
83
|
+
this.transaction = transaction;
|
|
84
|
+
this.table = table;
|
|
85
|
+
this.batchSize = batchSize;
|
|
86
|
+
this.key = table.sourceTable.replicaIdColumns[0];
|
|
87
|
+
if (lastKeySerialized != null) {
|
|
88
|
+
this.lastKey = this.deserializeKey(lastKeySerialized);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async initialize() {
|
|
92
|
+
// No-op
|
|
93
|
+
}
|
|
94
|
+
getLastKeySerialized() {
|
|
95
|
+
return bson.serialize({ [this.key.name]: this.lastKey });
|
|
96
|
+
}
|
|
97
|
+
async *next() {
|
|
98
|
+
const escapedKeyName = escapeIdentifier(this.key.name);
|
|
99
|
+
const metadataRequest = this.transaction.request();
|
|
100
|
+
metadataRequest.stream = true;
|
|
101
|
+
const metadataPromise = new Promise((resolve, reject) => {
|
|
102
|
+
metadataRequest.on('recordset', resolve);
|
|
103
|
+
metadataRequest.on('error', reject);
|
|
104
|
+
});
|
|
105
|
+
metadataRequest.query(`SELECT TOP(0) * FROM ${this.table.toQualifiedName()}`);
|
|
106
|
+
const columnMetadata = await metadataPromise;
|
|
107
|
+
const foundPrimaryKey = columnMetadata[this.key.name];
|
|
108
|
+
if (!foundPrimaryKey) {
|
|
109
|
+
throw new Error(`Cannot find primary key column ${this.key.name} in results. Keys: ${Object.keys(columnMetadata.columns).join(', ')}`);
|
|
110
|
+
}
|
|
111
|
+
yield columnMetadata;
|
|
112
|
+
const request = this.transaction.request();
|
|
113
|
+
const stream = request.toReadableStream();
|
|
114
|
+
if (this.lastKey == null) {
|
|
115
|
+
request.query(`SELECT TOP(${this.batchSize}) * FROM ${this.table.toQualifiedName()} ORDER BY ${escapedKeyName}`);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
if (this.key.typeId == null) {
|
|
119
|
+
throw new Error(`typeId required for primary key ${this.key.name}`);
|
|
120
|
+
}
|
|
121
|
+
request
|
|
122
|
+
.input('lastKey', this.lastKey)
|
|
123
|
+
.query(`SELECT TOP(${this.batchSize}) * FROM ${this.table.toQualifiedName()} WHERE ${escapedKeyName} > @lastKey ORDER BY ${escapedKeyName}`);
|
|
124
|
+
}
|
|
125
|
+
// MSSQL only streams one row at a time
|
|
126
|
+
for await (const row of stream) {
|
|
127
|
+
this.lastKey = row[this.key.name];
|
|
128
|
+
yield row;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
deserializeKey(key) {
|
|
132
|
+
const decoded = bson.deserialize(key, { useBigInt64: true });
|
|
133
|
+
const keys = Object.keys(decoded);
|
|
134
|
+
if (keys.length != 1) {
|
|
135
|
+
throw new ServiceAssertionError(`Multiple keys found: ${keys.join(', ')}`);
|
|
136
|
+
}
|
|
137
|
+
if (keys[0] != this.key.name) {
|
|
138
|
+
throw new ServiceAssertionError(`Key name mismatch: expected ${this.key.name}, got ${keys[0]}`);
|
|
139
|
+
}
|
|
140
|
+
return decoded[this.key.name];
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* This performs a snapshot query using a list of primary keys.
|
|
145
|
+
*
|
|
146
|
+
* This is not used for general snapshots, but is used when we need to re-fetch specific rows
|
|
147
|
+
* during streaming replication.
|
|
148
|
+
*/
|
|
149
|
+
export class IdSnapshotQuery {
|
|
150
|
+
transaction;
|
|
151
|
+
table;
|
|
152
|
+
keys;
|
|
153
|
+
static supports(table) {
|
|
154
|
+
// We have the same requirements as BatchedSnapshotQuery.
|
|
155
|
+
// This is typically only used as a fallback when ChunkedSnapshotQuery
|
|
156
|
+
// skipped some rows.
|
|
157
|
+
return BatchedSnapshotQuery.supports(table);
|
|
158
|
+
}
|
|
159
|
+
constructor(transaction, table, keys) {
|
|
160
|
+
this.transaction = transaction;
|
|
161
|
+
this.table = table;
|
|
162
|
+
this.keys = keys;
|
|
163
|
+
}
|
|
164
|
+
async initialize() {
|
|
165
|
+
// No-op
|
|
166
|
+
}
|
|
167
|
+
async *next() {
|
|
168
|
+
const metadataRequest = this.transaction.request();
|
|
169
|
+
metadataRequest.stream = true;
|
|
170
|
+
const metadataPromise = new Promise((resolve, reject) => {
|
|
171
|
+
metadataRequest.on('recordset', resolve);
|
|
172
|
+
metadataRequest.on('error', reject);
|
|
173
|
+
});
|
|
174
|
+
metadataRequest.query(`SELECT TOP(0) * FROM ${this.table.toQualifiedName()}`);
|
|
175
|
+
const columnMetadata = await metadataPromise;
|
|
176
|
+
yield columnMetadata;
|
|
177
|
+
const keyDefinition = this.table.sourceTable.replicaIdColumns[0];
|
|
178
|
+
const ids = this.keys.map((record) => record[keyDefinition.name]);
|
|
179
|
+
const request = this.transaction.request();
|
|
180
|
+
const stream = request.toReadableStream();
|
|
181
|
+
request
|
|
182
|
+
.input('ids', ids)
|
|
183
|
+
.query(`SELECT * FROM ${this.table.toQualifiedName()} WHERE ${escapeIdentifier(keyDefinition.name)} = @ids`);
|
|
184
|
+
// MSSQL only streams one row at a time
|
|
185
|
+
for await (const row of stream) {
|
|
186
|
+
yield row;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
//# sourceMappingURL=MSSQLSnapshotQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MSSQLSnapshotQuery.js","sourceRoot":"","sources":["../../src/replication/MSSQLSnapshotQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAiC,MAAM,yBAAyB,CAAC;AAE9E,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAE7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAajE;;;;;GAKG;AACH,MAAM,OAAO,mBAAmB;IAEX;IACA;IAFnB,YACmB,WAA4B,EAC5B,KAAuB;QADvB,gBAAW,GAAX,WAAW,CAAiB;QAC5B,UAAK,GAAL,KAAK,CAAkB;IACvC,CAAC;IAEG,KAAK,CAAC,UAAU,KAAmB,CAAC;IAEpC,KAAK,CAAC,CAAC,IAAI;QAChB,MAAM,eAAe,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;QACnD,eAAe,CAAC,MAAM,GAAG,IAAI,CAAC;QAC9B,MAAM,eAAe,GAAG,IAAI,OAAO,CAAsB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3E,eAAe,CAAC,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;YACzC,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC,CAAC,CAAC;QAEH,eAAe,CAAC,KAAK,CAAC,wBAAwB,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,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AAe,CAAC,MAAM,GAAG,IAAI,CAAC;QAC9B,MAAM,eAAe,GAAG,IAAI,OAAO,CAAsB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3E,eAAe,CAAC,EAAE,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;YACzC,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACtC,CAAC,CAAC,CAAC;QACH,eAAe,CAAC,KAAK,CAAC,wBAAwB,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC;QAC9E,MAAM,cAAc,GAAwB,MAAM,eAAe,CAAC;QAClE,MAAM,cAAc,CAAC;QAErB,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC;QACjE,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC;QAElE,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;QAC3C,MAAM,MAAM,GAAG,OAAO,CAAC,gBAAgB,EAAE,CAAC;QAC1C,OAAO;aACJ,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC;aACjB,KAAK,CAAC,iBAAiB,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,UAAU,gBAAgB,CAAC,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAE/G,uCAAuC;QACvC,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;YAC/B,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ColumnDescriptor } from '@powersync/service-core';
|
|
2
|
+
import { ISqlType } from 'mssql';
|
|
3
|
+
export interface MSSQLColumnDescriptor extends ColumnDescriptor {
|
|
4
|
+
/** The underlying system type id. For base types system type id == user type id */
|
|
5
|
+
typeId: number;
|
|
6
|
+
/** The unique user type id that uniquely identifies the type */
|
|
7
|
+
userTypeId: number;
|
|
8
|
+
}
|
|
9
|
+
/** The shared system type id for all CLR_UDT types in SQL Server */
|
|
10
|
+
export declare const CLR_UDT_TYPE_ID = 240;
|
|
11
|
+
/**
|
|
12
|
+
* Enum mapping the base MSSQL data types to their system type IDs.
|
|
13
|
+
*/
|
|
14
|
+
export declare enum MSSQLBaseType {
|
|
15
|
+
IMAGE = 34,
|
|
16
|
+
TEXT = 35,
|
|
17
|
+
UNIQUEIDENTIFIER = 36,
|
|
18
|
+
DATE = 40,
|
|
19
|
+
TIME = 41,
|
|
20
|
+
DATETIME2 = 42,
|
|
21
|
+
DATETIMEOFFSET = 43,
|
|
22
|
+
TINYINT = 48,
|
|
23
|
+
SMALLINT = 52,
|
|
24
|
+
INT = 56,
|
|
25
|
+
SMALLDATETIME = 58,
|
|
26
|
+
REAL = 59,
|
|
27
|
+
MONEY = 60,
|
|
28
|
+
DATETIME = 61,
|
|
29
|
+
FLOAT = 62,
|
|
30
|
+
SQL_VARIANT = 98,
|
|
31
|
+
NTEXT = 99,
|
|
32
|
+
BIT = 104,
|
|
33
|
+
DECIMAL = 106,
|
|
34
|
+
NUMERIC = 108,
|
|
35
|
+
SMALLMONEY = 122,
|
|
36
|
+
BIGINT = 127,
|
|
37
|
+
VARBINARY = 165,
|
|
38
|
+
VARCHAR = 167,
|
|
39
|
+
BINARY = 173,
|
|
40
|
+
CHAR = 175,
|
|
41
|
+
TIMESTAMP = 189,
|
|
42
|
+
NVARCHAR = 231,
|
|
43
|
+
NCHAR = 239,
|
|
44
|
+
XML = 241,
|
|
45
|
+
JSON = 244
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Enum mapping some of the extended user-defined MSSQL data types to their user type IDs.
|
|
49
|
+
*/
|
|
50
|
+
export declare enum MSSQLExtendedUserType {
|
|
51
|
+
VECTOR = 255,
|
|
52
|
+
SYSNAME = 256,
|
|
53
|
+
HIERARCHYID = 128,
|
|
54
|
+
GEOMETRY = 129,
|
|
55
|
+
GEOGRAPHY = 130
|
|
56
|
+
}
|
|
57
|
+
export declare enum MSSQLUserDefinedType {
|
|
58
|
+
VECTOR = "vector",
|
|
59
|
+
SYSNAME = "sysname",
|
|
60
|
+
HIERARCHYID = "hierarchyid"
|
|
61
|
+
}
|
|
62
|
+
export interface MSSQLParameter {
|
|
63
|
+
name: string;
|
|
64
|
+
value: any;
|
|
65
|
+
type?: (() => ISqlType) | ISqlType;
|
|
66
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** The shared system type id for all CLR_UDT types in SQL Server */
|
|
2
|
+
export const CLR_UDT_TYPE_ID = 240;
|
|
3
|
+
/**
|
|
4
|
+
* Enum mapping the base MSSQL data types to their system type IDs.
|
|
5
|
+
*/
|
|
6
|
+
export var MSSQLBaseType;
|
|
7
|
+
(function (MSSQLBaseType) {
|
|
8
|
+
MSSQLBaseType[MSSQLBaseType["IMAGE"] = 34] = "IMAGE";
|
|
9
|
+
MSSQLBaseType[MSSQLBaseType["TEXT"] = 35] = "TEXT";
|
|
10
|
+
MSSQLBaseType[MSSQLBaseType["UNIQUEIDENTIFIER"] = 36] = "UNIQUEIDENTIFIER";
|
|
11
|
+
MSSQLBaseType[MSSQLBaseType["DATE"] = 40] = "DATE";
|
|
12
|
+
MSSQLBaseType[MSSQLBaseType["TIME"] = 41] = "TIME";
|
|
13
|
+
MSSQLBaseType[MSSQLBaseType["DATETIME2"] = 42] = "DATETIME2";
|
|
14
|
+
MSSQLBaseType[MSSQLBaseType["DATETIMEOFFSET"] = 43] = "DATETIMEOFFSET";
|
|
15
|
+
MSSQLBaseType[MSSQLBaseType["TINYINT"] = 48] = "TINYINT";
|
|
16
|
+
MSSQLBaseType[MSSQLBaseType["SMALLINT"] = 52] = "SMALLINT";
|
|
17
|
+
MSSQLBaseType[MSSQLBaseType["INT"] = 56] = "INT";
|
|
18
|
+
MSSQLBaseType[MSSQLBaseType["SMALLDATETIME"] = 58] = "SMALLDATETIME";
|
|
19
|
+
MSSQLBaseType[MSSQLBaseType["REAL"] = 59] = "REAL";
|
|
20
|
+
MSSQLBaseType[MSSQLBaseType["MONEY"] = 60] = "MONEY";
|
|
21
|
+
MSSQLBaseType[MSSQLBaseType["DATETIME"] = 61] = "DATETIME";
|
|
22
|
+
MSSQLBaseType[MSSQLBaseType["FLOAT"] = 62] = "FLOAT";
|
|
23
|
+
MSSQLBaseType[MSSQLBaseType["SQL_VARIANT"] = 98] = "SQL_VARIANT";
|
|
24
|
+
MSSQLBaseType[MSSQLBaseType["NTEXT"] = 99] = "NTEXT";
|
|
25
|
+
MSSQLBaseType[MSSQLBaseType["BIT"] = 104] = "BIT";
|
|
26
|
+
MSSQLBaseType[MSSQLBaseType["DECIMAL"] = 106] = "DECIMAL";
|
|
27
|
+
MSSQLBaseType[MSSQLBaseType["NUMERIC"] = 108] = "NUMERIC";
|
|
28
|
+
MSSQLBaseType[MSSQLBaseType["SMALLMONEY"] = 122] = "SMALLMONEY";
|
|
29
|
+
MSSQLBaseType[MSSQLBaseType["BIGINT"] = 127] = "BIGINT";
|
|
30
|
+
MSSQLBaseType[MSSQLBaseType["VARBINARY"] = 165] = "VARBINARY";
|
|
31
|
+
MSSQLBaseType[MSSQLBaseType["VARCHAR"] = 167] = "VARCHAR";
|
|
32
|
+
MSSQLBaseType[MSSQLBaseType["BINARY"] = 173] = "BINARY";
|
|
33
|
+
MSSQLBaseType[MSSQLBaseType["CHAR"] = 175] = "CHAR";
|
|
34
|
+
MSSQLBaseType[MSSQLBaseType["TIMESTAMP"] = 189] = "TIMESTAMP";
|
|
35
|
+
MSSQLBaseType[MSSQLBaseType["NVARCHAR"] = 231] = "NVARCHAR";
|
|
36
|
+
MSSQLBaseType[MSSQLBaseType["NCHAR"] = 239] = "NCHAR";
|
|
37
|
+
MSSQLBaseType[MSSQLBaseType["XML"] = 241] = "XML";
|
|
38
|
+
MSSQLBaseType[MSSQLBaseType["JSON"] = 244] = "JSON";
|
|
39
|
+
})(MSSQLBaseType || (MSSQLBaseType = {}));
|
|
40
|
+
/**
|
|
41
|
+
* Enum mapping some of the extended user-defined MSSQL data types to their user type IDs.
|
|
42
|
+
*/
|
|
43
|
+
export var MSSQLExtendedUserType;
|
|
44
|
+
(function (MSSQLExtendedUserType) {
|
|
45
|
+
// VARBINARY system type [155]
|
|
46
|
+
MSSQLExtendedUserType[MSSQLExtendedUserType["VECTOR"] = 255] = "VECTOR";
|
|
47
|
+
// NVARCHAR system type [231]
|
|
48
|
+
MSSQLExtendedUserType[MSSQLExtendedUserType["SYSNAME"] = 256] = "SYSNAME";
|
|
49
|
+
// CLR_UDT system type [240]
|
|
50
|
+
MSSQLExtendedUserType[MSSQLExtendedUserType["HIERARCHYID"] = 128] = "HIERARCHYID";
|
|
51
|
+
// CLR_UDT system type [240]
|
|
52
|
+
MSSQLExtendedUserType[MSSQLExtendedUserType["GEOMETRY"] = 129] = "GEOMETRY";
|
|
53
|
+
// CLR_UDT system type [240]
|
|
54
|
+
MSSQLExtendedUserType[MSSQLExtendedUserType["GEOGRAPHY"] = 130] = "GEOGRAPHY";
|
|
55
|
+
})(MSSQLExtendedUserType || (MSSQLExtendedUserType = {}));
|
|
56
|
+
export var MSSQLUserDefinedType;
|
|
57
|
+
(function (MSSQLUserDefinedType) {
|
|
58
|
+
MSSQLUserDefinedType["VECTOR"] = "vector";
|
|
59
|
+
MSSQLUserDefinedType["SYSNAME"] = "sysname";
|
|
60
|
+
MSSQLUserDefinedType["HIERARCHYID"] = "hierarchyid";
|
|
61
|
+
})(MSSQLUserDefinedType || (MSSQLUserDefinedType = {}));
|
|
62
|
+
//# sourceMappingURL=mssql-data-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mssql-data-types.js","sourceRoot":"","sources":["../../src/types/mssql-data-types.ts"],"names":[],"mappings":"AAYA,oEAAo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