Skip to content

Commit ca6048e

Browse files
committed
fix: Remove v1+v2, defaulting to v1, and expand table interface
1 parent 4fe8511 commit ca6048e

File tree

15 files changed

+112
-349
lines changed

15 files changed

+112
-349
lines changed

config-root.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@
288288
"type": "boolean",
289289
"description": "Attempt fail-over to different node when unreachable. Default: true"
290290
},
291-
"shard": { "type": "integer", "description": "Shard id for this instance." },
291+
"shard": { "type": ["integer", "null"], "description": "Shard id for this instance." },
292292
"logging": { "$ref": "#/definitions/loggerConfig" }
293293
}
294294
},

index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
export { Resource } from './resources/Resource.ts';
22
import { Resource as ResourceImport } from './resources/Resource.ts';
33
export type {
4-
Query,
54
Context,
5+
Query,
6+
RequestTargetOrId,
7+
Session,
68
SourceContext,
79
SubscriptionRequest,
8-
RequestTargetOrId,
910
} from './resources/ResourceInterface.ts';
1011
export { ResourceInterface } from './resources/ResourceInterface.ts';
1112
export type { User } from './security/user.ts';

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"bin": {
2323
"harper": "./dist/bin/harper.js"
2424
},
25+
"exports": {
26+
".": "./index.js"
27+
},
2528
"files": [
2629
"dist",
2730
"static",
@@ -94,9 +97,6 @@
9497
"onFail": "error"
9598
}
9699
},
97-
"exports": {
98-
".": "./dist/index.js"
99-
},
100100
"devDependencies": {
101101
"@harperdb/code-guidelines": "^0.0.6",
102102
"@types/gunzip-maybe": "^1.4.3",

resources/ResourceInterface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface ResourceInterface<Record extends object = any>
3030
allowUpdate(user: User, record: Promise<Record & RecordObject>, context: Context): boolean | Promise<boolean>;
3131
put?(
3232
record: Record & RecordObject,
33-
target: RequestTargetOrId
33+
target?: RequestTargetOrId
3434
): void | (Record & Partial<RecordObject>) | Promise<void | (Record & Partial<RecordObject>)>;
3535
patch?(
3636
record: Partial<Record & RecordObject>,

resources/ResourceInterfaceV2.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

resources/ResourceV2.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

resources/Table.ts

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
*/
66

77
import { CONFIG_PARAMS, OPERATIONS_ENUM, SYSTEM_TABLE_NAMES, SYSTEM_SCHEMA_NAME } from '../utility/hdbTerms.ts';
8-
import { type Database } from 'lmdb';
98
import { getIndexedValues } from '../utility/lmdb/commonUtility.js';
109
import lodash from 'lodash';
1110
import { ExtendedIterable, SKIP } from '@harperfast/extended-iterable';
1211
import type {
13-
ResourceInterface,
1412
SubscriptionRequest,
1513
Id,
1614
Context,
@@ -19,6 +17,7 @@ import type {
1917
SubSelect,
2018
RequestTargetOrId,
2119
} from './ResourceInterface.ts';
20+
import { Table as TableType, TableInterface, TableStaticInterface, Attribute } from './TableInterface.ts';
2221
import type { User } from '../security/user.ts';
2322
import lmdbProcessRows from '../dataLayer/harperBridge/lmdbBridge/lmdbUtility/lmdbProcessRows.js';
2423
import { Resource, transformForSelect } from './Resource.ts';
@@ -69,15 +68,6 @@ import { contentTypes } from '../server/serverHelpers/contentTypes';
6968
const { sortBy } = lodash;
7069
const { validateAttribute } = lmdbProcessRows;
7170

72-
export type Attribute = {
73-
name: string;
74-
type: string;
75-
assignCreatedTime?: boolean;
76-
assignUpdatedTime?: boolean;
77-
expiresAt?: boolean;
78-
isPrimaryKey?: boolean;
79-
};
80-
8171
type MaybePromise<T> = T | Promise<T>;
8272

8373
const NULL_WITH_TIMESTAMP = new Uint8Array(9);
@@ -99,31 +89,15 @@ const FULL_PERMISSIONS = {
9989
delete: true,
10090
isSuperUser: true,
10191
};
102-
export interface Table {
103-
primaryStore: Database;
104-
auditStore: Database;
105-
indices: {};
106-
databasePath: string;
107-
tableName: string;
108-
databaseName: string;
109-
attributes: any[];
110-
primaryKey: string;
111-
splitSegments?: boolean;
112-
replicate?: boolean;
113-
subscriptions: Map<any, Function[]>;
114-
expirationMS: number;
115-
indexingOperations?: Promise<void>;
116-
source?: new () => ResourceInterface;
117-
Transaction: ReturnType<typeof makeTable>;
118-
}
92+
export { TableType, TableInterface, TableStaticInterface, Attribute };
11993
type ResidencyDefinition = number | string[] | void;
12094

12195
/**
12296
* This returns a Table class for the given table settings (determined from the metadata table)
12397
* Instances of the returned class are Resource instances, intended to provide a consistent view or transaction of the table
12498
* @param options
12599
*/
126-
export function makeTable(options) {
100+
export function makeTable(options): TableStaticInterface {
127101
const {
128102
primaryKey,
129103
indices,
@@ -206,7 +180,7 @@ export function makeTable(options) {
206180
return this.addTo(property, -value);
207181
}
208182
}
209-
class TableResource<Record extends object = any> extends Resource<Record> {
183+
class TableResource<Record extends object = any> extends Resource<Record> implements TableInterface<Record> {
210184
#record: any; // the stored/frozen record from the database and stored in the cache (should not be modified directly)
211185
#changes: any; // the changes to the record that have been made (should not be modified directly)
212186
#version?: number; // version of the record

resources/TableInterface.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { Database } from 'lmdb';
2+
import { ResourceInterface, Id, Context } from './ResourceInterface.ts';
3+
4+
export interface Attribute {
5+
name: string;
6+
type: string;
7+
assignCreatedTime?: boolean;
8+
assignUpdatedTime?: boolean;
9+
expiresAt?: boolean;
10+
isPrimaryKey?: boolean;
11+
}
12+
13+
export interface Table<Record extends object = any> extends TableInterface<Record> {
14+
primaryStore: Database;
15+
auditStore: Database;
16+
indices: {};
17+
databasePath: string;
18+
tableName: string;
19+
databaseName: string;
20+
attributes: Attribute[];
21+
primaryKey: string;
22+
splitSegments?: boolean;
23+
replicate?: boolean;
24+
subscriptions: Map<any, Function[]>;
25+
expirationMS: number;
26+
indexingOperations?: Promise<void>;
27+
source?: new () => ResourceInterface;
28+
Transaction: Table;
29+
origin?: string;
30+
schemaVersion?: number;
31+
}
32+
33+
export interface TableInterface<Record extends object = any> extends ResourceInterface<Record> {
34+
getUpdatedTime(): number;
35+
getExpiresAt(): number;
36+
}
37+
38+
export interface TableStaticInterface {
39+
new <Record extends object = any>(identifier?: Id, source?: any): TableInterface<Record>;
40+
41+
primaryStore: Database;
42+
auditStore: Database;
43+
primaryKey: string;
44+
tableName: string;
45+
tableId: number;
46+
indices: any;
47+
audit: any;
48+
databasePath: string;
49+
databaseName: string;
50+
attributes: Attribute[];
51+
replicate: boolean;
52+
sealed: boolean;
53+
splitSegments: boolean;
54+
createdTimeProperty: Attribute;
55+
updatedTimeProperty: Attribute;
56+
propertyResolvers: any;
57+
userResolvers: any;
58+
source?: any;
59+
sourceOptions: any;
60+
intermediateSource: boolean;
61+
getResidencyById: (id: Id) => number | void;
62+
expirationMS: number;
63+
dbisDB: any;
64+
schemaDefined: boolean;
65+
indexingOperation?: Promise<void>;
66+
origin?: string;
67+
schemaVersion?: number;
68+
69+
addAttributes(attributesToAdd: Attribute[]): Promise<void>;
70+
removeAttributes(names: string[]): Promise<void>;
71+
getSize(): number;
72+
getAuditSize(): number;
73+
getStorageStats(): { available: number; free: number; size: number };
74+
getRecordCount(options?: {
75+
exactCount?: boolean;
76+
}): Promise<number | { recordCount: number; estimatedRange: number[] }>;
77+
setTTLExpiration(expiration: any): void;
78+
sourcedFrom(source: any, options: any): void;
79+
getResource(target: any, request: Context, resourceOptions: any): Promise<TableInterface>;
80+
}

resources/dataLoader.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getWorkerIndex } from '../server/threads/manageThreads';
66
import { HTTP_STATUS_CODES } from '../utility/errors/commonErrors.js';
77
import { ClientError } from '../utility/errors/hdbError.js';
88
import harperLogger from '../utility/logging/harper_logger.js';
9-
import { Attribute } from './Table.ts';
9+
import { type Attribute, type Table } from './TableInterface.ts';
1010
import { FileEntry } from '../components/EntryHandler.ts';
1111

1212
const dataLoaderLogger = harperLogger.forComponent('dataLoader');
@@ -15,7 +15,7 @@ const dataLoaderLogger = harperLogger.forComponent('dataLoader');
1515
const DATA_LOADER_HASH_TABLE = 'hdb_dataloader_hash';
1616

1717
/** Lazy-initialized cache for the hash tracking table */
18-
let _hashTrackingTable: ReturnType<typeof table>;
18+
let _hashTrackingTable: Table;
1919

2020
/**
2121
* Computes a deterministic hash of a record's content for tracking data file changes.
@@ -42,7 +42,7 @@ export function computeRecordHash(record: Record<string, any>): string {
4242
* Gets or creates the hash tracking table in the system database.
4343
* Lazy-initializes the table on first access.
4444
*/
45-
function getHashTrackingTable(databasesRef: Databases) {
45+
function getHashTrackingTable(databasesRef: Databases): Table {
4646
// Always check databasesRef first (important for testing with mocks)
4747
if (databasesRef.system && databasesRef.system[DATA_LOADER_HASH_TABLE]) {
4848
return databasesRef.system[DATA_LOADER_HASH_TABLE];

0 commit comments

Comments
 (0)