You need to sign in or sign up before continuing.
@macss/modular-api-sqlserver 0.4.7
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/README.md +42 -0
- package/dist/dbClient.d.ts +249 -0
- package/dist/dbClient.js +417 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @macss/modular-api-sqlserver
|
|
2
|
+
|
|
3
|
+
Official MACSS SQL Server integration package for TypeScript.
|
|
4
|
+
|
|
5
|
+
## Quick start
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { DbClient, DbCommand, DbCommandKind, DbConnectionSettings } from '@macss/modular-api-sqlserver';
|
|
9
|
+
|
|
10
|
+
const settings = DbConnectionSettings.fromEnvironment();
|
|
11
|
+
|
|
12
|
+
const client = new DbClient({
|
|
13
|
+
settings,
|
|
14
|
+
sessionProvider: mySessionProvider,
|
|
15
|
+
commandExecutor: myCommandExecutor,
|
|
16
|
+
transactionRunner: myTransactionRunner,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const result = await client.scalar<number>(
|
|
20
|
+
new DbCommand({
|
|
21
|
+
kind: DbCommandKind.scalar,
|
|
22
|
+
text: 'select count(*) from users',
|
|
23
|
+
label: 'users.count',
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
if (result.isSuccess) {
|
|
28
|
+
console.log(result.value.value);
|
|
29
|
+
} else {
|
|
30
|
+
console.error(result.failure.message);
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
See [example/example.ts](example/example.ts) for a complete in-memory wiring sample.
|
|
35
|
+
|
|
36
|
+
## Current slice
|
|
37
|
+
|
|
38
|
+
- normalized SQL Server connection defaults and redacted summaries
|
|
39
|
+
- engine-agnostic `DbClient`, `DbRepository`, and transaction contracts
|
|
40
|
+
- explicit lease ownership semantics for package-owned and application-owned sessions
|
|
41
|
+
- health contributor and GraphQL support bundle for higher-level integrations
|
|
42
|
+
- real driver bindings intentionally remain outside this first slice
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
export declare enum DbCommandKind {
|
|
2
|
+
query = "query",
|
|
3
|
+
execute = "execute",
|
|
4
|
+
batch = "batch",
|
|
5
|
+
scalar = "scalar"
|
|
6
|
+
}
|
|
7
|
+
export declare enum DbFailureKind {
|
|
8
|
+
connectivity = "connectivity",
|
|
9
|
+
timeout = "timeout",
|
|
10
|
+
authentication = "authentication",
|
|
11
|
+
authorization = "authorization",
|
|
12
|
+
constraint = "constraint",
|
|
13
|
+
conflict = "conflict",
|
|
14
|
+
notFound = "not_found",
|
|
15
|
+
serialization = "serialization",
|
|
16
|
+
cancelled = "cancelled",
|
|
17
|
+
unknown = "unknown"
|
|
18
|
+
}
|
|
19
|
+
export declare enum DbHealthStatus {
|
|
20
|
+
healthy = "healthy",
|
|
21
|
+
unhealthy = "unhealthy"
|
|
22
|
+
}
|
|
23
|
+
export declare class DbConnectionSettings {
|
|
24
|
+
readonly host: string;
|
|
25
|
+
readonly port: number;
|
|
26
|
+
readonly database: string;
|
|
27
|
+
readonly username: string;
|
|
28
|
+
readonly password: string;
|
|
29
|
+
readonly driver: string;
|
|
30
|
+
readonly options: Record<string, unknown>;
|
|
31
|
+
constructor(options: {
|
|
32
|
+
host: string;
|
|
33
|
+
port: number;
|
|
34
|
+
database: string;
|
|
35
|
+
username: string;
|
|
36
|
+
password: string;
|
|
37
|
+
driver: string;
|
|
38
|
+
options?: Record<string, unknown>;
|
|
39
|
+
});
|
|
40
|
+
static fromEnvironment(environment?: Record<string, string | undefined>): DbConnectionSettings;
|
|
41
|
+
get engineId(): string;
|
|
42
|
+
get redactedSummary(): string;
|
|
43
|
+
}
|
|
44
|
+
export declare class DbCommand {
|
|
45
|
+
readonly kind: DbCommandKind;
|
|
46
|
+
readonly text: string;
|
|
47
|
+
readonly parameters: unknown[];
|
|
48
|
+
readonly label?: string;
|
|
49
|
+
constructor(options: {
|
|
50
|
+
kind: DbCommandKind;
|
|
51
|
+
text: string;
|
|
52
|
+
parameters?: unknown[];
|
|
53
|
+
label?: string;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
export declare class DbExecutionMetadata {
|
|
57
|
+
readonly duration: number;
|
|
58
|
+
readonly commandLabel?: string;
|
|
59
|
+
readonly rowCount?: number;
|
|
60
|
+
readonly affectedCount?: number;
|
|
61
|
+
constructor(options: {
|
|
62
|
+
duration: number;
|
|
63
|
+
commandLabel?: string;
|
|
64
|
+
rowCount?: number;
|
|
65
|
+
affectedCount?: number;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
export declare class DbRowSet {
|
|
69
|
+
readonly rows: Array<Record<string, unknown>>;
|
|
70
|
+
readonly metadata: DbExecutionMetadata;
|
|
71
|
+
constructor(options: {
|
|
72
|
+
rows: Array<Record<string, unknown>>;
|
|
73
|
+
metadata: DbExecutionMetadata;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
export declare class DbExecutionSummary {
|
|
77
|
+
readonly affectedCount: number;
|
|
78
|
+
readonly metadata: DbExecutionMetadata;
|
|
79
|
+
constructor(options: {
|
|
80
|
+
affectedCount: number;
|
|
81
|
+
metadata: DbExecutionMetadata;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
export declare class DbScalar<T> {
|
|
85
|
+
readonly value: T;
|
|
86
|
+
readonly metadata: DbExecutionMetadata;
|
|
87
|
+
constructor(options: {
|
|
88
|
+
value: T;
|
|
89
|
+
metadata: DbExecutionMetadata;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
export declare class DbFailure {
|
|
93
|
+
readonly kind: DbFailureKind;
|
|
94
|
+
readonly code: string;
|
|
95
|
+
readonly message: string;
|
|
96
|
+
readonly retryable: boolean;
|
|
97
|
+
readonly transient: boolean;
|
|
98
|
+
readonly details?: unknown;
|
|
99
|
+
readonly causeSummary?: string;
|
|
100
|
+
constructor(options: {
|
|
101
|
+
kind: DbFailureKind;
|
|
102
|
+
code: string;
|
|
103
|
+
message: string;
|
|
104
|
+
retryable: boolean;
|
|
105
|
+
transient: boolean;
|
|
106
|
+
details?: unknown;
|
|
107
|
+
causeSummary?: string;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
export declare class DbResult<T> {
|
|
111
|
+
private readonly innerValue?;
|
|
112
|
+
private readonly innerFailure?;
|
|
113
|
+
private constructor();
|
|
114
|
+
static success<T>(value: T): DbResult<T>;
|
|
115
|
+
static failure<T>(failure: DbFailure): DbResult<T>;
|
|
116
|
+
get isSuccess(): boolean;
|
|
117
|
+
get isFailure(): boolean;
|
|
118
|
+
get value(): T;
|
|
119
|
+
get failure(): DbFailure;
|
|
120
|
+
map<R>(transform: (value: T) => R): DbResult<R>;
|
|
121
|
+
flatMap<R>(transform: (value: T) => DbResult<R>): DbResult<R>;
|
|
122
|
+
mapFailure(transform: (failure: DbFailure) => DbFailure): DbResult<T>;
|
|
123
|
+
getOrThrow(message?: string): T;
|
|
124
|
+
}
|
|
125
|
+
export declare class DbProviderDescription {
|
|
126
|
+
readonly engineId: string;
|
|
127
|
+
readonly database: string;
|
|
128
|
+
readonly redactedSummary: string;
|
|
129
|
+
readonly ownsResources: boolean;
|
|
130
|
+
constructor(options: {
|
|
131
|
+
engineId: string;
|
|
132
|
+
database: string;
|
|
133
|
+
redactedSummary: string;
|
|
134
|
+
ownsResources: boolean;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
export declare class DbSessionLease<S> {
|
|
138
|
+
readonly session: S;
|
|
139
|
+
readonly ownedByPackage: boolean;
|
|
140
|
+
private readonly releaser;
|
|
141
|
+
constructor(options: {
|
|
142
|
+
session: S;
|
|
143
|
+
ownedByPackage: boolean;
|
|
144
|
+
releaser: () => Promise<DbResult<void>>;
|
|
145
|
+
});
|
|
146
|
+
release(): Promise<DbResult<void>>;
|
|
147
|
+
}
|
|
148
|
+
export interface DbSessionProvider<S> {
|
|
149
|
+
acquire(): Promise<DbResult<DbSessionLease<S>>>;
|
|
150
|
+
close(): Promise<DbResult<void>>;
|
|
151
|
+
describe(): DbProviderDescription;
|
|
152
|
+
}
|
|
153
|
+
export interface DbCommandExecutor<S> {
|
|
154
|
+
query(session: S, command: DbCommand): Promise<DbResult<DbRowSet>>;
|
|
155
|
+
execute(session: S, command: DbCommand): Promise<DbResult<DbExecutionSummary>>;
|
|
156
|
+
scalar<T>(session: S, command: DbCommand): Promise<DbResult<DbScalar<T>>>;
|
|
157
|
+
}
|
|
158
|
+
export declare class DbTransactionContext<S> {
|
|
159
|
+
readonly settings: DbConnectionSettings;
|
|
160
|
+
readonly session: S;
|
|
161
|
+
readonly commandExecutor: DbCommandExecutor<S>;
|
|
162
|
+
constructor(options: {
|
|
163
|
+
settings: DbConnectionSettings;
|
|
164
|
+
session: S;
|
|
165
|
+
commandExecutor: DbCommandExecutor<S>;
|
|
166
|
+
});
|
|
167
|
+
query(command: DbCommand): Promise<DbResult<DbRowSet>>;
|
|
168
|
+
execute(command: DbCommand): Promise<DbResult<DbExecutionSummary>>;
|
|
169
|
+
scalar<T>(command: DbCommand): Promise<DbResult<DbScalar<T>>>;
|
|
170
|
+
}
|
|
171
|
+
export interface DbTransactionRunner<S> {
|
|
172
|
+
run<T>(context: DbTransactionContext<S>, body: (context: DbTransactionContext<S>) => Promise<DbResult<T>>): Promise<DbResult<T>>;
|
|
173
|
+
}
|
|
174
|
+
export declare class DbRepositoryContext<S> {
|
|
175
|
+
readonly settings: DbConnectionSettings;
|
|
176
|
+
readonly sessionProvider: DbSessionProvider<S>;
|
|
177
|
+
readonly commandExecutor: DbCommandExecutor<S>;
|
|
178
|
+
readonly transactionRunner: DbTransactionRunner<S>;
|
|
179
|
+
constructor(options: {
|
|
180
|
+
settings: DbConnectionSettings;
|
|
181
|
+
sessionProvider: DbSessionProvider<S>;
|
|
182
|
+
commandExecutor: DbCommandExecutor<S>;
|
|
183
|
+
transactionRunner: DbTransactionRunner<S>;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
export declare abstract class DbRepository<S> {
|
|
187
|
+
readonly context: DbRepositoryContext<S>;
|
|
188
|
+
constructor(context: DbRepositoryContext<S>);
|
|
189
|
+
query(command: DbCommand): Promise<DbResult<DbRowSet>>;
|
|
190
|
+
execute(command: DbCommand): Promise<DbResult<DbExecutionSummary>>;
|
|
191
|
+
scalar<T>(command: DbCommand): Promise<DbResult<DbScalar<T>>>;
|
|
192
|
+
transaction<T>(body: (context: DbTransactionContext<S>) => Promise<DbResult<T>>): Promise<DbResult<T>>;
|
|
193
|
+
}
|
|
194
|
+
export declare class DbClient<S> {
|
|
195
|
+
readonly settings: DbConnectionSettings;
|
|
196
|
+
readonly sessionProvider: DbSessionProvider<S>;
|
|
197
|
+
readonly commandExecutor: DbCommandExecutor<S>;
|
|
198
|
+
readonly transactionRunner: DbTransactionRunner<S>;
|
|
199
|
+
constructor(options: {
|
|
200
|
+
settings: DbConnectionSettings;
|
|
201
|
+
sessionProvider: DbSessionProvider<S>;
|
|
202
|
+
commandExecutor: DbCommandExecutor<S>;
|
|
203
|
+
transactionRunner: DbTransactionRunner<S>;
|
|
204
|
+
});
|
|
205
|
+
query(command: DbCommand): Promise<DbResult<DbRowSet>>;
|
|
206
|
+
execute(command: DbCommand): Promise<DbResult<DbExecutionSummary>>;
|
|
207
|
+
scalar<T>(command: DbCommand): Promise<DbResult<DbScalar<T>>>;
|
|
208
|
+
transaction<T>(body: (context: DbTransactionContext<S>) => Promise<DbResult<T>>): Promise<DbResult<T>>;
|
|
209
|
+
repositoryContext(): DbRepositoryContext<S>;
|
|
210
|
+
close(): Promise<DbResult<void>>;
|
|
211
|
+
describe(): DbProviderDescription;
|
|
212
|
+
}
|
|
213
|
+
export declare class DbHealthReport {
|
|
214
|
+
readonly status: DbHealthStatus;
|
|
215
|
+
readonly responseTime: number;
|
|
216
|
+
readonly redactedSummary: string;
|
|
217
|
+
readonly details?: string;
|
|
218
|
+
constructor(options: {
|
|
219
|
+
status: DbHealthStatus;
|
|
220
|
+
responseTime: number;
|
|
221
|
+
redactedSummary: string;
|
|
222
|
+
details?: string;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
export declare class DbHealthContributor<S> {
|
|
226
|
+
readonly client: DbClient<S>;
|
|
227
|
+
readonly probeCommand: DbCommand;
|
|
228
|
+
constructor(options: {
|
|
229
|
+
client: DbClient<S>;
|
|
230
|
+
probeCommand?: DbCommand;
|
|
231
|
+
});
|
|
232
|
+
probe(): Promise<DbHealthReport>;
|
|
233
|
+
}
|
|
234
|
+
export declare class DbGraphqlSupport<S> {
|
|
235
|
+
readonly catalogProvider: unknown;
|
|
236
|
+
readonly readExecutor: unknown;
|
|
237
|
+
readonly healthContributor: DbHealthContributor<S>;
|
|
238
|
+
readonly sourceDigestFactory?: unknown;
|
|
239
|
+
readonly artifactLoader?: unknown;
|
|
240
|
+
readonly capabilityRegistration?: unknown;
|
|
241
|
+
constructor(options: {
|
|
242
|
+
catalogProvider: unknown;
|
|
243
|
+
readExecutor: unknown;
|
|
244
|
+
healthContributor: DbHealthContributor<S>;
|
|
245
|
+
sourceDigestFactory?: unknown;
|
|
246
|
+
artifactLoader?: unknown;
|
|
247
|
+
capabilityRegistration?: unknown;
|
|
248
|
+
});
|
|
249
|
+
}
|
package/dist/dbClient.js
ADDED
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DbGraphqlSupport = exports.DbHealthContributor = exports.DbHealthReport = exports.DbClient = exports.DbRepository = exports.DbRepositoryContext = exports.DbTransactionContext = exports.DbSessionLease = exports.DbProviderDescription = exports.DbResult = exports.DbFailure = exports.DbScalar = exports.DbExecutionSummary = exports.DbRowSet = exports.DbExecutionMetadata = exports.DbCommand = exports.DbConnectionSettings = exports.DbHealthStatus = exports.DbFailureKind = exports.DbCommandKind = void 0;
|
|
4
|
+
var DbCommandKind;
|
|
5
|
+
(function (DbCommandKind) {
|
|
6
|
+
DbCommandKind["query"] = "query";
|
|
7
|
+
DbCommandKind["execute"] = "execute";
|
|
8
|
+
DbCommandKind["batch"] = "batch";
|
|
9
|
+
DbCommandKind["scalar"] = "scalar";
|
|
10
|
+
})(DbCommandKind || (exports.DbCommandKind = DbCommandKind = {}));
|
|
11
|
+
var DbFailureKind;
|
|
12
|
+
(function (DbFailureKind) {
|
|
13
|
+
DbFailureKind["connectivity"] = "connectivity";
|
|
14
|
+
DbFailureKind["timeout"] = "timeout";
|
|
15
|
+
DbFailureKind["authentication"] = "authentication";
|
|
16
|
+
DbFailureKind["authorization"] = "authorization";
|
|
17
|
+
DbFailureKind["constraint"] = "constraint";
|
|
18
|
+
DbFailureKind["conflict"] = "conflict";
|
|
19
|
+
DbFailureKind["notFound"] = "not_found";
|
|
20
|
+
DbFailureKind["serialization"] = "serialization";
|
|
21
|
+
DbFailureKind["cancelled"] = "cancelled";
|
|
22
|
+
DbFailureKind["unknown"] = "unknown";
|
|
23
|
+
})(DbFailureKind || (exports.DbFailureKind = DbFailureKind = {}));
|
|
24
|
+
var DbHealthStatus;
|
|
25
|
+
(function (DbHealthStatus) {
|
|
26
|
+
DbHealthStatus["healthy"] = "healthy";
|
|
27
|
+
DbHealthStatus["unhealthy"] = "unhealthy";
|
|
28
|
+
})(DbHealthStatus || (exports.DbHealthStatus = DbHealthStatus = {}));
|
|
29
|
+
class DbConnectionSettings {
|
|
30
|
+
host;
|
|
31
|
+
port;
|
|
32
|
+
database;
|
|
33
|
+
username;
|
|
34
|
+
password;
|
|
35
|
+
driver;
|
|
36
|
+
options;
|
|
37
|
+
constructor(options) {
|
|
38
|
+
this.host = options.host;
|
|
39
|
+
this.port = options.port;
|
|
40
|
+
this.database = options.database;
|
|
41
|
+
this.username = options.username;
|
|
42
|
+
this.password = options.password;
|
|
43
|
+
this.driver = options.driver;
|
|
44
|
+
this.options = { ...(options.options ?? {}) };
|
|
45
|
+
}
|
|
46
|
+
static fromEnvironment(environment = process.env) {
|
|
47
|
+
const parsedPort = Number.parseInt(environment.MODULAR_API_SQLSERVER_PORT ?? '', 10);
|
|
48
|
+
return new DbConnectionSettings({
|
|
49
|
+
host: environment.MODULAR_API_SQLSERVER_HOST ?? '127.0.0.1',
|
|
50
|
+
port: Number.isNaN(parsedPort) ? 14333 : parsedPort,
|
|
51
|
+
database: environment.MODULAR_API_SQLSERVER_DATABASE ?? 'modular_api_graphql_v1',
|
|
52
|
+
username: environment.MODULAR_API_SQLSERVER_USERNAME ?? 'sa',
|
|
53
|
+
password: environment.MODULAR_API_SQLSERVER_PASSWORD ?? 'ModularApi_dev_StrongPass1',
|
|
54
|
+
driver: environment.MODULAR_API_SQLSERVER_DRIVER ?? 'ODBC Driver 17 for SQL Server',
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
get engineId() {
|
|
58
|
+
return 'sqlserver';
|
|
59
|
+
}
|
|
60
|
+
get redactedSummary() {
|
|
61
|
+
return `${this.engineId}://${this.username}@${this.host}:${this.port}/${this.database}?driver=${this.driver}`;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.DbConnectionSettings = DbConnectionSettings;
|
|
65
|
+
class DbCommand {
|
|
66
|
+
kind;
|
|
67
|
+
text;
|
|
68
|
+
parameters;
|
|
69
|
+
label;
|
|
70
|
+
constructor(options) {
|
|
71
|
+
this.kind = options.kind;
|
|
72
|
+
this.text = options.text;
|
|
73
|
+
this.parameters = [...(options.parameters ?? [])];
|
|
74
|
+
this.label = options.label;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.DbCommand = DbCommand;
|
|
78
|
+
class DbExecutionMetadata {
|
|
79
|
+
duration;
|
|
80
|
+
commandLabel;
|
|
81
|
+
rowCount;
|
|
82
|
+
affectedCount;
|
|
83
|
+
constructor(options) {
|
|
84
|
+
this.duration = options.duration;
|
|
85
|
+
this.commandLabel = options.commandLabel;
|
|
86
|
+
this.rowCount = options.rowCount;
|
|
87
|
+
this.affectedCount = options.affectedCount;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.DbExecutionMetadata = DbExecutionMetadata;
|
|
91
|
+
class DbRowSet {
|
|
92
|
+
rows;
|
|
93
|
+
metadata;
|
|
94
|
+
constructor(options) {
|
|
95
|
+
this.rows = [...options.rows];
|
|
96
|
+
this.metadata = options.metadata;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.DbRowSet = DbRowSet;
|
|
100
|
+
class DbExecutionSummary {
|
|
101
|
+
affectedCount;
|
|
102
|
+
metadata;
|
|
103
|
+
constructor(options) {
|
|
104
|
+
this.affectedCount = options.affectedCount;
|
|
105
|
+
this.metadata = options.metadata;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.DbExecutionSummary = DbExecutionSummary;
|
|
109
|
+
class DbScalar {
|
|
110
|
+
value;
|
|
111
|
+
metadata;
|
|
112
|
+
constructor(options) {
|
|
113
|
+
this.value = options.value;
|
|
114
|
+
this.metadata = options.metadata;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.DbScalar = DbScalar;
|
|
118
|
+
class DbFailure {
|
|
119
|
+
kind;
|
|
120
|
+
code;
|
|
121
|
+
message;
|
|
122
|
+
retryable;
|
|
123
|
+
transient;
|
|
124
|
+
details;
|
|
125
|
+
causeSummary;
|
|
126
|
+
constructor(options) {
|
|
127
|
+
this.kind = options.kind;
|
|
128
|
+
this.code = options.code;
|
|
129
|
+
this.message = options.message;
|
|
130
|
+
this.retryable = options.retryable;
|
|
131
|
+
this.transient = options.transient;
|
|
132
|
+
this.details = options.details;
|
|
133
|
+
this.causeSummary = options.causeSummary;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.DbFailure = DbFailure;
|
|
137
|
+
class DbResult {
|
|
138
|
+
innerValue;
|
|
139
|
+
innerFailure;
|
|
140
|
+
constructor(value, failure) {
|
|
141
|
+
this.innerValue = value;
|
|
142
|
+
this.innerFailure = failure;
|
|
143
|
+
}
|
|
144
|
+
static success(value) {
|
|
145
|
+
return new DbResult(value, undefined);
|
|
146
|
+
}
|
|
147
|
+
static failure(failure) {
|
|
148
|
+
return new DbResult(undefined, failure);
|
|
149
|
+
}
|
|
150
|
+
get isSuccess() {
|
|
151
|
+
return this.innerFailure === undefined;
|
|
152
|
+
}
|
|
153
|
+
get isFailure() {
|
|
154
|
+
return this.innerFailure !== undefined;
|
|
155
|
+
}
|
|
156
|
+
get value() {
|
|
157
|
+
if (this.innerFailure !== undefined) {
|
|
158
|
+
throw new Error('DbResult does not contain a success value.');
|
|
159
|
+
}
|
|
160
|
+
return this.innerValue;
|
|
161
|
+
}
|
|
162
|
+
get failure() {
|
|
163
|
+
if (this.innerFailure === undefined) {
|
|
164
|
+
throw new Error('DbResult does not contain a failure value.');
|
|
165
|
+
}
|
|
166
|
+
return this.innerFailure;
|
|
167
|
+
}
|
|
168
|
+
map(transform) {
|
|
169
|
+
if (this.isFailure) {
|
|
170
|
+
return DbResult.failure(this.failure);
|
|
171
|
+
}
|
|
172
|
+
return DbResult.success(transform(this.value));
|
|
173
|
+
}
|
|
174
|
+
flatMap(transform) {
|
|
175
|
+
if (this.isFailure) {
|
|
176
|
+
return DbResult.failure(this.failure);
|
|
177
|
+
}
|
|
178
|
+
return transform(this.value);
|
|
179
|
+
}
|
|
180
|
+
mapFailure(transform) {
|
|
181
|
+
if (this.isSuccess) {
|
|
182
|
+
return DbResult.success(this.value);
|
|
183
|
+
}
|
|
184
|
+
return DbResult.failure(transform(this.failure));
|
|
185
|
+
}
|
|
186
|
+
getOrThrow(message) {
|
|
187
|
+
if (this.isFailure) {
|
|
188
|
+
throw new Error(message ?? this.failure.message);
|
|
189
|
+
}
|
|
190
|
+
return this.value;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.DbResult = DbResult;
|
|
194
|
+
class DbProviderDescription {
|
|
195
|
+
engineId;
|
|
196
|
+
database;
|
|
197
|
+
redactedSummary;
|
|
198
|
+
ownsResources;
|
|
199
|
+
constructor(options) {
|
|
200
|
+
this.engineId = options.engineId;
|
|
201
|
+
this.database = options.database;
|
|
202
|
+
this.redactedSummary = options.redactedSummary;
|
|
203
|
+
this.ownsResources = options.ownsResources;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
exports.DbProviderDescription = DbProviderDescription;
|
|
207
|
+
class DbSessionLease {
|
|
208
|
+
session;
|
|
209
|
+
ownedByPackage;
|
|
210
|
+
releaser;
|
|
211
|
+
constructor(options) {
|
|
212
|
+
this.session = options.session;
|
|
213
|
+
this.ownedByPackage = options.ownedByPackage;
|
|
214
|
+
this.releaser = options.releaser;
|
|
215
|
+
}
|
|
216
|
+
async release() {
|
|
217
|
+
if (!this.ownedByPackage) {
|
|
218
|
+
return DbResult.success(undefined);
|
|
219
|
+
}
|
|
220
|
+
return this.releaser();
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
exports.DbSessionLease = DbSessionLease;
|
|
224
|
+
class DbTransactionContext {
|
|
225
|
+
settings;
|
|
226
|
+
session;
|
|
227
|
+
commandExecutor;
|
|
228
|
+
constructor(options) {
|
|
229
|
+
this.settings = options.settings;
|
|
230
|
+
this.session = options.session;
|
|
231
|
+
this.commandExecutor = options.commandExecutor;
|
|
232
|
+
}
|
|
233
|
+
query(command) {
|
|
234
|
+
return this.commandExecutor.query(this.session, command);
|
|
235
|
+
}
|
|
236
|
+
execute(command) {
|
|
237
|
+
return this.commandExecutor.execute(this.session, command);
|
|
238
|
+
}
|
|
239
|
+
scalar(command) {
|
|
240
|
+
return this.commandExecutor.scalar(this.session, command);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
exports.DbTransactionContext = DbTransactionContext;
|
|
244
|
+
class DbRepositoryContext {
|
|
245
|
+
settings;
|
|
246
|
+
sessionProvider;
|
|
247
|
+
commandExecutor;
|
|
248
|
+
transactionRunner;
|
|
249
|
+
constructor(options) {
|
|
250
|
+
this.settings = options.settings;
|
|
251
|
+
this.sessionProvider = options.sessionProvider;
|
|
252
|
+
this.commandExecutor = options.commandExecutor;
|
|
253
|
+
this.transactionRunner = options.transactionRunner;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
exports.DbRepositoryContext = DbRepositoryContext;
|
|
257
|
+
class DbRepository {
|
|
258
|
+
context;
|
|
259
|
+
constructor(context) {
|
|
260
|
+
this.context = context;
|
|
261
|
+
}
|
|
262
|
+
query(command) {
|
|
263
|
+
return withLease(this.context.sessionProvider, (lease) => this.context.commandExecutor.query(lease.session, command));
|
|
264
|
+
}
|
|
265
|
+
execute(command) {
|
|
266
|
+
return withLease(this.context.sessionProvider, (lease) => this.context.commandExecutor.execute(lease.session, command));
|
|
267
|
+
}
|
|
268
|
+
scalar(command) {
|
|
269
|
+
return withLease(this.context.sessionProvider, (lease) => this.context.commandExecutor.scalar(lease.session, command));
|
|
270
|
+
}
|
|
271
|
+
transaction(body) {
|
|
272
|
+
const client = new DbClient({
|
|
273
|
+
settings: this.context.settings,
|
|
274
|
+
sessionProvider: this.context.sessionProvider,
|
|
275
|
+
commandExecutor: this.context.commandExecutor,
|
|
276
|
+
transactionRunner: this.context.transactionRunner,
|
|
277
|
+
});
|
|
278
|
+
return client.transaction(body);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
exports.DbRepository = DbRepository;
|
|
282
|
+
class DbClient {
|
|
283
|
+
settings;
|
|
284
|
+
sessionProvider;
|
|
285
|
+
commandExecutor;
|
|
286
|
+
transactionRunner;
|
|
287
|
+
constructor(options) {
|
|
288
|
+
this.settings = options.settings;
|
|
289
|
+
this.sessionProvider = options.sessionProvider;
|
|
290
|
+
this.commandExecutor = options.commandExecutor;
|
|
291
|
+
this.transactionRunner = options.transactionRunner;
|
|
292
|
+
}
|
|
293
|
+
query(command) {
|
|
294
|
+
return withLease(this.sessionProvider, (lease) => this.commandExecutor.query(lease.session, command));
|
|
295
|
+
}
|
|
296
|
+
execute(command) {
|
|
297
|
+
return withLease(this.sessionProvider, (lease) => this.commandExecutor.execute(lease.session, command));
|
|
298
|
+
}
|
|
299
|
+
scalar(command) {
|
|
300
|
+
return withLease(this.sessionProvider, (lease) => this.commandExecutor.scalar(lease.session, command));
|
|
301
|
+
}
|
|
302
|
+
async transaction(body) {
|
|
303
|
+
const leaseResult = await this.sessionProvider.acquire();
|
|
304
|
+
if (leaseResult.isFailure) {
|
|
305
|
+
return DbResult.failure(leaseResult.failure);
|
|
306
|
+
}
|
|
307
|
+
const lease = leaseResult.value;
|
|
308
|
+
const context = new DbTransactionContext({
|
|
309
|
+
settings: this.settings,
|
|
310
|
+
session: lease.session,
|
|
311
|
+
commandExecutor: this.commandExecutor,
|
|
312
|
+
});
|
|
313
|
+
const result = await this.transactionRunner.run(context, body);
|
|
314
|
+
const releaseResult = await lease.release();
|
|
315
|
+
if (result.isFailure) {
|
|
316
|
+
return DbResult.failure(result.failure);
|
|
317
|
+
}
|
|
318
|
+
if (releaseResult.isFailure) {
|
|
319
|
+
return DbResult.failure(releaseResult.failure);
|
|
320
|
+
}
|
|
321
|
+
return result;
|
|
322
|
+
}
|
|
323
|
+
repositoryContext() {
|
|
324
|
+
return new DbRepositoryContext({
|
|
325
|
+
settings: this.settings,
|
|
326
|
+
sessionProvider: this.sessionProvider,
|
|
327
|
+
commandExecutor: this.commandExecutor,
|
|
328
|
+
transactionRunner: this.transactionRunner,
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
close() {
|
|
332
|
+
return this.sessionProvider.close();
|
|
333
|
+
}
|
|
334
|
+
describe() {
|
|
335
|
+
return this.sessionProvider.describe();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
exports.DbClient = DbClient;
|
|
339
|
+
class DbHealthReport {
|
|
340
|
+
status;
|
|
341
|
+
responseTime;
|
|
342
|
+
redactedSummary;
|
|
343
|
+
details;
|
|
344
|
+
constructor(options) {
|
|
345
|
+
this.status = options.status;
|
|
346
|
+
this.responseTime = options.responseTime;
|
|
347
|
+
this.redactedSummary = options.redactedSummary;
|
|
348
|
+
this.details = options.details;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
exports.DbHealthReport = DbHealthReport;
|
|
352
|
+
class DbHealthContributor {
|
|
353
|
+
client;
|
|
354
|
+
probeCommand;
|
|
355
|
+
constructor(options) {
|
|
356
|
+
this.client = options.client;
|
|
357
|
+
this.probeCommand =
|
|
358
|
+
options.probeCommand ??
|
|
359
|
+
new DbCommand({
|
|
360
|
+
kind: DbCommandKind.scalar,
|
|
361
|
+
text: 'SELECT 1',
|
|
362
|
+
label: 'db.health',
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
async probe() {
|
|
366
|
+
const startedAt = Date.now();
|
|
367
|
+
const result = await this.client.scalar(this.probeCommand);
|
|
368
|
+
const responseTime = Date.now() - startedAt;
|
|
369
|
+
if (result.isSuccess) {
|
|
370
|
+
return new DbHealthReport({
|
|
371
|
+
status: DbHealthStatus.healthy,
|
|
372
|
+
responseTime,
|
|
373
|
+
redactedSummary: this.client.describe().redactedSummary,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
return new DbHealthReport({
|
|
377
|
+
status: DbHealthStatus.unhealthy,
|
|
378
|
+
responseTime,
|
|
379
|
+
redactedSummary: this.client.describe().redactedSummary,
|
|
380
|
+
details: result.failure.code,
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
exports.DbHealthContributor = DbHealthContributor;
|
|
385
|
+
class DbGraphqlSupport {
|
|
386
|
+
catalogProvider;
|
|
387
|
+
readExecutor;
|
|
388
|
+
healthContributor;
|
|
389
|
+
sourceDigestFactory;
|
|
390
|
+
artifactLoader;
|
|
391
|
+
capabilityRegistration;
|
|
392
|
+
constructor(options) {
|
|
393
|
+
this.catalogProvider = options.catalogProvider;
|
|
394
|
+
this.readExecutor = options.readExecutor;
|
|
395
|
+
this.healthContributor = options.healthContributor;
|
|
396
|
+
this.sourceDigestFactory = options.sourceDigestFactory;
|
|
397
|
+
this.artifactLoader = options.artifactLoader;
|
|
398
|
+
this.capabilityRegistration = options.capabilityRegistration;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
exports.DbGraphqlSupport = DbGraphqlSupport;
|
|
402
|
+
async function withLease(sessionProvider, operation) {
|
|
403
|
+
const leaseResult = await sessionProvider.acquire();
|
|
404
|
+
if (leaseResult.isFailure) {
|
|
405
|
+
return DbResult.failure(leaseResult.failure);
|
|
406
|
+
}
|
|
407
|
+
const lease = leaseResult.value;
|
|
408
|
+
const operationResult = await operation(lease);
|
|
409
|
+
const releaseResult = await lease.release();
|
|
410
|
+
if (operationResult.isFailure) {
|
|
411
|
+
return DbResult.failure(operationResult.failure);
|
|
412
|
+
}
|
|
413
|
+
if (releaseResult.isFailure) {
|
|
414
|
+
return DbResult.failure(releaseResult.failure);
|
|
415
|
+
}
|
|
416
|
+
return operationResult;
|
|
417
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dbClient';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./dbClient"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@macss/modular-api-sqlserver",
|
|
3
|
+
"version": "0.4.7",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Official MACSS SQL Server integration package for TypeScript.",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc --project tsconfig.build.json",
|
|
14
|
+
"prepublishOnly": "npm run build",
|
|
15
|
+
"test": "vitest run"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"macss",
|
|
19
|
+
"sqlserver",
|
|
20
|
+
"database"
|
|
21
|
+
],
|
|
22
|
+
"author": "ccisne.dev",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/ccisnedev/modular_api.git",
|
|
27
|
+
"directory": "code/ts/modular_api_sqlserver"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/ccisnedev/modular_api/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/ccisnedev/modular_api/tree/main/code/ts/modular_api_sqlserver#readme",
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^22.19.11",
|
|
35
|
+
"typescript": "^5.9.3",
|
|
36
|
+
"vitest": "^4.0.18"
|
|
37
|
+
}
|
|
38
|
+
}
|