@deterministic-code/datasources 0.1.16 → 0.1.18
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/Datasource.d.ts +2 -0
- package/dist/Datasource.js +6 -0
- package/dist/IDatasource.d.ts +2 -0
- package/dist/converters/DefaultValueConverter.js +3 -16
- package/dist/converters/DeterministicTypeConverter.js +5 -11
- package/dist/converters/RowTypeConverter.js +1 -1
- package/dist/mocks/mock-datasource.d.ts +2 -0
- package/dist/mocks/mock-datasource.js +2 -0
- package/dist/stubs/StubDatasource.d.ts +2 -0
- package/dist/stubs/StubDatasource.js +6 -0
- package/package.json +1 -1
package/dist/Datasource.d.ts
CHANGED
|
@@ -17,6 +17,8 @@ export declare class Datasource implements IDatasource {
|
|
|
17
17
|
readonly repositories: RepositoryFactory;
|
|
18
18
|
constructor(provider: string, driver: IDriver, options?: DatasourceOptions | undefined);
|
|
19
19
|
private defaultConnection;
|
|
20
|
+
close: () => Promise<void>;
|
|
21
|
+
[Symbol.asyncDispose]: () => Promise<void>;
|
|
20
22
|
createConnection: () => Promise<IConnection>;
|
|
21
23
|
createCommand: (connection?: IConnection) => Promise<ICommand>;
|
|
22
24
|
private applyCrud;
|
package/dist/Datasource.js
CHANGED
|
@@ -29,6 +29,12 @@ export class Datasource {
|
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
31
|
defaultConnection = () => (this.connection ??= this.driver.connection(this.options));
|
|
32
|
+
close = async () => {
|
|
33
|
+
const connection = this.connection;
|
|
34
|
+
this.connection = undefined;
|
|
35
|
+
await connection?.close();
|
|
36
|
+
};
|
|
37
|
+
[Symbol.asyncDispose] = () => this.close();
|
|
32
38
|
createConnection = () => Promise.resolve(this.defaultConnection());
|
|
33
39
|
createCommand = async (connection) => {
|
|
34
40
|
if (!connection) {
|
package/dist/IDatasource.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ import type { RepositoryFactory } from "./RepositoryFactory.js";
|
|
|
9
9
|
export interface IDatasource {
|
|
10
10
|
readonly provider: string;
|
|
11
11
|
readonly repositories: RepositoryFactory;
|
|
12
|
+
close(): Promise<void>;
|
|
13
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
12
14
|
createConnection(): Promise<IConnection>;
|
|
13
15
|
createCommand(connection?: IConnection): Promise<ICommand>;
|
|
14
16
|
createRepository<T>(type: Constructable<T>, connection?: IConnection, options?: CRUDRepositoryOptions<T>): ICRUDRepository<T>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export const EMPTY_UUID = "00000000-0000-0000-0000-000000000000";
|
|
2
|
-
const
|
|
3
|
-
"number",
|
|
2
|
+
const NUMERIC_TYPES = [
|
|
4
3
|
"integer",
|
|
5
4
|
"unsignedinteger",
|
|
6
5
|
"smallinteger",
|
|
@@ -9,8 +8,8 @@ const NUMERIC_LITERAL_TYPES = new Set([
|
|
|
9
8
|
"unsignedbiginteger",
|
|
10
9
|
"float",
|
|
11
10
|
"decimal",
|
|
12
|
-
|
|
13
|
-
]);
|
|
11
|
+
];
|
|
12
|
+
const NUMERIC_LITERAL_TYPES = new Set([...NUMERIC_TYPES, "reference"]);
|
|
14
13
|
const CALL_FORM = /^([A-Za-z]+)\('(.*)'\)$/;
|
|
15
14
|
const quotedReverse = /^'(.*)'$/;
|
|
16
15
|
const identity = (arg) => arg;
|
|
@@ -27,22 +26,10 @@ const token = (specToken, native, reverse) => ({
|
|
|
27
26
|
native,
|
|
28
27
|
reverse,
|
|
29
28
|
});
|
|
30
|
-
const NUMERIC_TYPES = [
|
|
31
|
-
"number",
|
|
32
|
-
"integer",
|
|
33
|
-
"unsignedinteger",
|
|
34
|
-
"smallinteger",
|
|
35
|
-
"unsignedsmallinteger",
|
|
36
|
-
"biginteger",
|
|
37
|
-
"unsignedbiginteger",
|
|
38
|
-
"float",
|
|
39
|
-
"decimal",
|
|
40
|
-
];
|
|
41
29
|
export class DefaultValueConverter {
|
|
42
30
|
static spec = {
|
|
43
31
|
string: { String: { spec: /^.*$/, specText: identity } },
|
|
44
32
|
character: { String: { spec: /^.+$/, specText: identity } },
|
|
45
|
-
number: { Numeric: intLit },
|
|
46
33
|
integer: { Numeric: intLit },
|
|
47
34
|
unsignedinteger: { Numeric: uintLit },
|
|
48
35
|
smallinteger: { Numeric: intLit },
|
|
@@ -23,11 +23,8 @@ export class DeterministicTypeConverter {
|
|
|
23
23
|
this.#rules = rules;
|
|
24
24
|
this.#reverse = new Map();
|
|
25
25
|
for (const [specType, native] of Object.entries(dictionary)) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
this.#reverse.set(native, [specType]);
|
|
29
|
-
else
|
|
30
|
-
existing.push(specType);
|
|
26
|
+
if (!this.#reverse.has(native))
|
|
27
|
+
this.#reverse.set(native, specType);
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
30
|
convert(from) {
|
|
@@ -63,12 +60,9 @@ export class DeterministicTypeConverter {
|
|
|
63
60
|
return this.convert(field.type);
|
|
64
61
|
}
|
|
65
62
|
reverseConvert = (native) => {
|
|
66
|
-
const
|
|
67
|
-
if (
|
|
63
|
+
const spec = this.#reverse.get(native);
|
|
64
|
+
if (spec === undefined)
|
|
68
65
|
throw new Error(`Unknown native field type: ${native}`);
|
|
69
|
-
|
|
70
|
-
throw new Error(`Native field type ${native} maps to multiple spec types: ${specTypes.join(", ")}`);
|
|
71
|
-
}
|
|
72
|
-
return specTypes[0];
|
|
66
|
+
return spec;
|
|
73
67
|
};
|
|
74
68
|
}
|
|
@@ -8,7 +8,7 @@ export class RowTypeConverter extends TypeConverter {
|
|
|
8
8
|
static from(converters, columns) {
|
|
9
9
|
const fields = {};
|
|
10
10
|
for (const [name, type] of Object.entries(columns)) {
|
|
11
|
-
const converter = converters[type];
|
|
11
|
+
const converter = converters[type] ?? (type.endsWith("integer") ? converters.integer : undefined);
|
|
12
12
|
if (converter)
|
|
13
13
|
fields[name] = converter;
|
|
14
14
|
}
|
|
@@ -15,6 +15,8 @@ export declare class MockDatasource implements IDatasource {
|
|
|
15
15
|
repository?: ICRUDRepository;
|
|
16
16
|
readonly repositories: RepositoryFactory;
|
|
17
17
|
constructor(provider?: string);
|
|
18
|
+
close: () => Promise<void>;
|
|
19
|
+
[Symbol.asyncDispose]: () => Promise<void>;
|
|
18
20
|
createConnection(): Promise<IConnection>;
|
|
19
21
|
createCommand(connection?: IConnection): Promise<ICommand>;
|
|
20
22
|
createRepository<T>(type: Constructable<T>, connection?: IConnection): ICRUDRepository<T>;
|
|
@@ -16,6 +16,8 @@ export class MockDatasource {
|
|
|
16
16
|
this.provider = provider;
|
|
17
17
|
this.connection = new MockConnection();
|
|
18
18
|
}
|
|
19
|
+
close = () => this.connection.close();
|
|
20
|
+
[Symbol.asyncDispose] = () => this.close();
|
|
19
21
|
createConnection() {
|
|
20
22
|
this.createConnectionCalls += 1;
|
|
21
23
|
return Promise.resolve(this.connection);
|
|
@@ -11,6 +11,8 @@ export declare class StubDatasource implements IDatasource {
|
|
|
11
11
|
readonly repositories: RepositoryFactory;
|
|
12
12
|
constructor(provider: string);
|
|
13
13
|
private defaultConnection;
|
|
14
|
+
close: () => Promise<void>;
|
|
15
|
+
[Symbol.asyncDispose]: () => Promise<void>;
|
|
14
16
|
createConnection: () => Promise<IConnection>;
|
|
15
17
|
createCommand: (connection?: IConnection) => Promise<ICommand>;
|
|
16
18
|
createRepository<T>(type: Constructable<T>, connection?: IConnection): ICRUDRepository<T>;
|
|
@@ -11,6 +11,12 @@ export class StubDatasource {
|
|
|
11
11
|
this.provider = provider;
|
|
12
12
|
}
|
|
13
13
|
defaultConnection = () => (this.connection ??= new StubConnection(this.provider));
|
|
14
|
+
close = async () => {
|
|
15
|
+
const connection = this.connection;
|
|
16
|
+
this.connection = undefined;
|
|
17
|
+
await connection?.close();
|
|
18
|
+
};
|
|
19
|
+
[Symbol.asyncDispose] = () => this.close();
|
|
14
20
|
createConnection = () => Promise.resolve(this.defaultConnection());
|
|
15
21
|
createCommand = async (connection) => {
|
|
16
22
|
if (!connection) {
|