Skip to content

Commit 068604c

Browse files
committed
refactor(uc)!: make UCDataStore compliant with Initializable
1 parent 7687c4d commit 068604c

13 files changed

Lines changed: 52 additions & 40 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Introduce `Initializable.initSync()` : useful for contexts where async is not available (e.g. Cloudflare workers initialization) => To address, simply add the method in the concerned implementations. You can leave it empty, re-use the same logic as your existing `init()` if its core is not async, or copy it and adapt it to make it sync. If you want to forbid its usage, use `throw new NotCallableError<this>('initSync', 'init', 'async-only');`
88
- Introduce `ServerManager.mountSync()` : useful for contexts where async is not available (e.g. Cloudflare workers initialization) => To address, simply add the method in the concerned implementations. You can leave it empty, re-use the same logic as your existing `mount()` if its core is not async, or copy it and adapt it to make it sync. If you want to forbid its usage, use `throw new NotCallableError<this>('mountSync', 'mount', 'async-only');`
99
- Change `uc_data_store_ucs_dataset_name` default value from `use-cases` to `uc_executions` : if you relied on the default value and want to keep it, explicitly set `uc_data_store_ucs_dataset_name` in your settings. If you want the new name, rename your existing collection to the new name
10+
- Make `UCDataStore` compliant with `Initializable` : rename `install` to `init`, add `initSync` and rename `initTx` to `startTx`
1011

1112
**Added**
1213

dist/esm/std/impl/UCDataStoreExternalResourceManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let UCDataStoreExternalResourceManager = class UCDataStoreExternalResourceManage
2020
this.ucDataStore = ucDataStore;
2121
}
2222
async create() {
23-
return this.ucDataStore.install();
23+
return this.ucDataStore.init();
2424
}
2525
async delete() {
2626
return this.ucDataStore.destroy();

dist/esm/testing/UCDataStoreTester.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { APP_NAME_PLACEHOLDER } from '../convention.js';
1616
import { TAmount, TCompanyName, } from '../dt/index.js';
1717
import { UCExecMode, } from '../uc/index.js';
1818
const ERR_SHOULD_NOT_EXIST_AFTER_DESTROY = 'It should not exist after destroy';
19-
const ERR_SHOULD_EXIST_AFTER_INSTALL = 'It should exist after install';
19+
const ERR_SHOULD_EXIST_AFTER_INIT = 'It should exist after init';
2020
const ERR_SHOULD_RETURN_X_RECORDS = (n) => `It should return ${n} record(s)`;
2121
/**
2222
* Test that a {@link UCDataStore} conforms to the spec
@@ -52,10 +52,10 @@ let UCDataStoreTester = class UCDataStoreTester {
5252
if (this.exists) {
5353
throw new Error(ERR_SHOULD_NOT_EXIST_AFTER_DESTROY);
5454
}
55-
await ucDataStore.install();
55+
await ucDataStore.init();
5656
this.exists = await ucDataStore.exists();
5757
if (!this.exists) {
58-
throw new Error(ERR_SHOULD_EXIST_AFTER_INSTALL);
58+
throw new Error(ERR_SHOULD_EXIST_AFTER_INIT);
5959
}
6060
this.readRes = await ucDataStore.read();
6161
this.expectXRecords(0);

dist/esm/uc/data-store.d.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AppName } from '../app/index.js';
22
import type { UIntQuantity, UUID } from '../dt/index.js';
3-
import type { Clearable, StringKeys } from '../utils/index.js';
3+
import type { Clearable, Initializable, StringKeys } from '../utils/index.js';
44
import type { UCData } from './data.js';
55
import type { UCExecMode } from './exec.js';
66
import type { UCInput } from './input.js';
@@ -55,13 +55,12 @@ export interface UCDataStoreRecord<I extends UCInput | undefined = undefined, D
5555
organizationId: UUID | null;
5656
userId: UUID | null;
5757
}
58-
export interface UCDataStore extends Clearable {
58+
export interface UCDataStore extends Clearable, Initializable {
5959
destroy(): Promise<void>;
6060
exists(): Promise<boolean>;
61-
initTx(): Promise<UCDataStoreTx['ref']>;
62-
install(): Promise<void>;
6361
read<I extends UCInput | undefined = undefined, D extends UCData | null = null>(opts?: UCDataStoreReadOpts<I, D>): Promise<UCDataStoreReadResponse<I, D>>;
6462
readProjection<T extends object>(name: string, opts?: UCDataStoreReadProjectionOpts<T>): Promise<T[]>;
63+
startTx(): Promise<UCDataStoreTx['ref']>;
6564
supportedSpecificBindings(): UCDataStoreWriteProjectionSpecificBinding[];
6665
testKey(encryptionKey: Uint8Array): Promise<void>;
6766
write<I extends UCInput | undefined = undefined, D extends UCData | null = null>(record: UCDataStoreRecord<I, D>, opts?: UCDataStoreWriteOpts): Promise<void>;

dist/esm/uc/impl/CloudflareD1UCDataStore.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ export declare class CloudflareD1UCDataStore implements Configurable<S>, UCDataS
1717
clear(): Promise<void>;
1818
destroy(): Promise<void>;
1919
exists(): Promise<boolean>;
20-
initTx(): Promise<UCDataStoreTx['ref']>;
21-
install(): Promise<void>;
20+
init(): Promise<void>;
21+
initSync(): void;
2222
read<I extends UCInput | undefined = undefined, D extends UCData | null = null>(opts?: UCDataStoreReadOpts<I, D>): Promise<UCDataStoreReadResponse<I, D>>;
2323
readProjection<T extends object>(_name: string, _opts?: UCDataStoreReadProjectionOpts<T>): Promise<T[]>;
24+
startTx(): Promise<UCDataStoreTx['ref']>;
2425
supportedSpecificBindings(): UCDataStoreWriteProjectionSpecificBinding[];
2526
testKey(_encryptionKey: Uint8Array): Promise<void>;
2627
write<I extends UCInput | undefined = undefined, D extends UCData | null = null>(record: UCDataStoreRecord<I, D>, _opts?: UCDataStoreWriteOpts): Promise<void>;

dist/esm/uc/impl/CloudflareD1UCDataStore.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ let CloudflareD1UCDataStore = class CloudflareD1UCDataStore {
5050
async exists() {
5151
throw new NotImplementedError('exists');
5252
}
53-
async initTx() {
54-
throw new NotImplementedError('initTx');
55-
}
56-
async install() {
57-
throw new NotImplementedError('install');
53+
async init() {
54+
throw new NotAvailableError('initSync');
5855
// Since workers do not accept top-level await, we cannot invoke this method when initiating it.
5956
// Plus, calling it in every request handler would be overkill and would add too much overhead.
6057
// In the meantime, process as follows :
@@ -90,6 +87,9 @@ CREATE INDEX IF NOT EXISTS uc_executions_user_id_index ON uc_executions (userId)
9087
pnpm wrangler d1 list
9188
*/
9289
}
90+
initSync() {
91+
throw new NotAvailableError('initSync');
92+
}
9393
async read(opts) {
9494
this.assertClient();
9595
// TODO : Consider using a query builder (or Google's pipe operator ?) when it gets too complicated or dangerous
@@ -133,6 +133,9 @@ CREATE INDEX IF NOT EXISTS uc_executions_user_id_index ON uc_executions (userId)
133133
async readProjection(_name, _opts) {
134134
throw new NotImplementedError('readProjection');
135135
}
136+
async startTx() {
137+
throw new NotImplementedError('startTx');
138+
}
136139
supportedSpecificBindings() {
137140
throw new NotImplementedError('supportedSpecificBindings');
138141
}

dist/esm/uc/impl/InMemoryUCDataStore.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ export declare class InMemoryUCDataStore implements UCDataStore {
99
clear(): Promise<void>;
1010
destroy(): Promise<void>;
1111
exists(): Promise<boolean>;
12-
initTx(): Promise<UCDataStoreTx['ref']>;
13-
install(): Promise<void>;
12+
init(): Promise<void>;
13+
initSync(): void;
1414
read<I extends UCInput | undefined = undefined, D extends UCData | null = null>(opts?: UCDataStoreReadOpts<I, D>): Promise<UCDataStoreReadResponse<I, D>>;
1515
readProjection<T extends object>(): Promise<T[]>;
16+
startTx(): Promise<UCDataStoreTx['ref']>;
1617
supportedSpecificBindings(): UCDataStoreWriteProjectionSpecificBinding[];
1718
testKey(): Promise<void>;
1819
write<I extends UCInput | undefined = undefined, D extends UCData | null = null>(record: UCDataStoreRecord<I, D>): Promise<void>;

dist/esm/uc/impl/InMemoryUCDataStore.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,10 @@ let InMemoryUCDataStore = class InMemoryUCDataStore {
3434
async exists() {
3535
return this.entries.length > 0;
3636
}
37-
async initTx() {
38-
this.tx = 'pending';
39-
return {
40-
commit: async () => {
41-
this.tx = 'committed';
42-
},
43-
rollback: async () => {
44-
this.tx = 'rollbacked';
45-
this.entries = [];
46-
},
47-
};
37+
async init() {
38+
// Nothing to do
4839
}
49-
async install() {
40+
initSync() {
5041
// Nothing to do
5142
}
5243
async read(opts) {
@@ -81,6 +72,18 @@ let InMemoryUCDataStore = class InMemoryUCDataStore {
8172
async readProjection() {
8273
return [];
8374
}
75+
async startTx() {
76+
this.tx = 'pending';
77+
return {
78+
commit: async () => {
79+
this.tx = 'committed';
80+
},
81+
rollback: async () => {
82+
this.tx = 'rollbacked';
83+
this.entries = [];
84+
},
85+
};
86+
}
8487
supportedSpecificBindings() {
8588
return [];
8689
}

dist/esm/uc/impl/KnexUCDataStore.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ export declare class KnexUCDataStore implements Configurable<S>, UCDataStore {
2626
clear(): Promise<void>;
2727
destroy(): Promise<void>;
2828
exists(): Promise<boolean>;
29-
initTx(): Promise<UCDataStoreTx['ref']>;
30-
install(): Promise<void>;
29+
init(): Promise<void>;
30+
initSync(): void;
3131
read<I extends UCInput | undefined = undefined, D extends UCData | null = null>(opts?: UCDataStoreReadOpts<I, D>): Promise<UCDataStoreReadResponse<I, D>>;
3232
readProjection<T extends object>(name: string, opts?: UCDataStoreReadProjectionOpts<T>): Promise<T[]>;
33+
startTx(): Promise<UCDataStoreTx['ref']>;
3334
supportedSpecificBindings(): UCDataStoreWriteProjectionSpecificBinding[];
3435
testKey(_encryptionKey: Uint8Array): Promise<void>;
3536
write<I extends UCInput | undefined = undefined, D extends UCData | null = null>(record: UCDataStoreRecord<I, D>, opts?: UCDataStoreWriteOpts): Promise<void>;

dist/esm/uc/impl/KnexUCDataStore.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
1212
};
1313
import { inject, injectable } from 'inversify';
1414
import knex, {} from 'knex';
15-
import { NotAvailableError } from '../../error/index.js';
15+
import { NotAvailableError, NotCallableError } from '../../error/index.js';
1616
const FILTERS_MAPPING = new Map([
1717
['aggregateId', 'aggregate_id'],
1818
['appName', 'app_name'],
@@ -51,12 +51,12 @@ let KnexUCDataStore = class KnexUCDataStore {
5151
async exists() {
5252
return this.client.schema.hasTable(this.s().uc_data_store_ucs_dataset_name);
5353
}
54-
async initTx() {
55-
return this.client.transaction();
56-
}
57-
async install() {
54+
async init() {
5855
await this.migration001CreateMainTable();
5956
}
57+
initSync() {
58+
throw new NotCallableError('initSync', 'init', 'async-only');
59+
}
6060
async read(opts) {
6161
const query = this.client(this.s().uc_data_store_ucs_dataset_name);
6262
// Filter
@@ -101,6 +101,9 @@ let KnexUCDataStore = class KnexUCDataStore {
101101
const records = await query;
102102
return records;
103103
}
104+
async startTx() {
105+
return this.client.transaction();
106+
}
104107
supportedSpecificBindings() {
105108
const type = this.s().knex_uc_data_store_type;
106109
switch (type) {

0 commit comments

Comments
 (0)