Skip to content

Commit

Permalink
refactor: move internals to dedicated object (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-oloughlin authored Feb 7, 2025
1 parent f3d9757 commit 48b9972
Show file tree
Hide file tree
Showing 19 changed files with 272 additions and 180 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@olli/kvdex",
"version": "3.1.0",
"version": "3.1.1",
"exports": {
".": "./mod.ts",
"./zod": "./src/ext/zod/mod.ts",
Expand Down
51 changes: 30 additions & 21 deletions src/atomic_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class AtomicBuilder<
operations?: Operations,
) {
// Check for large collection
if (collection._encoder) {
if (collection.一internal.encoder) {
throw new InvalidCollectionError(
"Atomic operations are not supported for serialized collections",
);
Expand Down Expand Up @@ -187,15 +187,17 @@ export class AtomicBuilder<
delete(id: ParseId<TOptions>): this {
// Create id key from id and collection id key
const collection = this.collection;
const idKey = extendKey(collection._keys.id, id);
const idKey = extendKey(collection.一internal.keys.id, id);

// Add delete operation
this.operations.atomic.delete(idKey);

// If collection is indexable, handle indexing
if (this.collection._isIndexable) {
if (this.collection.一internal.isIndexable) {
// Add collection key for collision detection
this.operations.indexDeleteCollectionKeys.push(collection._keys.base);
this.operations.indexDeleteCollectionKeys.push(
collection.一internal.keys.base,
);

// Add delete preperation function to prepeare delete functions list
this.operations.prepareDeleteFns.push(async (kv) => {
Expand All @@ -209,8 +211,12 @@ export class AtomicBuilder<
}

// Set history entry if keeps history
if (this.collection._keepsHistory) {
const historyKey = extendKey(this.collection._keys.history, id, ulid());
if (this.collection.一internal.keepsHistory) {
const historyKey = extendKey(
this.collection.一internal.keys.history,
id,
ulid(),
);

const historyEntry: HistoryEntry<TOutput> = {
type: "delete",
Expand Down Expand Up @@ -244,7 +250,7 @@ export class AtomicBuilder<
// Create Denoatomic checks from atomci checks input list
const checks: DenoAtomicCheck[] = atomicChecks.map(
({ id, versionstamp }) => {
const key = extendKey(this.collection._keys.id, id);
const key = extendKey(this.collection.一internal.keys.id, id);
return {
key,
versionstamp,
Expand Down Expand Up @@ -278,7 +284,7 @@ export class AtomicBuilder<
id: ParseId<TOptions>,
value: TOutput extends DenoKvU64 ? bigint : never,
): this {
const idKey = extendKey(this.collection._keys.id, id);
const idKey = extendKey(this.collection.一internal.keys.id, id);
this.operations.atomic.sum(idKey, value);
return this;
}
Expand All @@ -303,7 +309,7 @@ export class AtomicBuilder<
id: ParseId<TOptions>,
value: TOutput extends DenoKvU64 ? bigint : never,
): this {
const idKey = extendKey(this.collection._keys.id, id);
const idKey = extendKey(this.collection.一internal.keys.id, id);
this.operations.atomic.min(idKey, value);
return this;
}
Expand All @@ -328,7 +334,7 @@ export class AtomicBuilder<
id: ParseId<TOptions>,
value: TOutput extends DenoKvU64 ? bigint : never,
): this {
const idKey = extendKey(this.collection._keys.id, id);
const idKey = extendKey(this.collection.一internal.keys.id, id);
this.operations.atomic.max(idKey, value);
return this;
}
Expand Down Expand Up @@ -418,8 +424,8 @@ export class AtomicBuilder<
enqueue(data: KvValue, options?: EnqueueOptions): this {
// Prepare and add enqueue operation
const prep = prepareEnqueue(
this.collection._keys.base,
this.collection._keys.undelivered,
this.collection.一internal.keys.base,
this.collection.一internal.keys.undelivered,
data,
options,
);
Expand Down Expand Up @@ -517,7 +523,7 @@ export class AtomicBuilder<
const overwrite = !!(options as AtomicSetOptions<EmptyObject> | undefined)
?.overwrite;

if (this.collection._isIndexable && overwrite) {
if (this.collection.一internal.isIndexable && overwrite) {
throw new InvalidCollectionError(
"The overwrite property is not supported for indexable collections",
);
Expand All @@ -527,21 +533,24 @@ export class AtomicBuilder<
// Create id key from collection id key and id
const collection = this.collection;

const parsed = collection._model._transform?.(value as TInput) ??
collection._model.parse(value);
const parsed =
collection.一internal.model._transform?.(value as TInput) ??
collection.一internal.model.parse(value);

const docId = id ?? await collection._idGenerator(parsed);
const idKey = extendKey(collection._keys.id, docId);
const docId = id ?? await collection.一internal.idGenerator(parsed);
const idKey = extendKey(collection.一internal.keys.id, docId);

// Add set operation
this.operations.atomic.set(idKey, parsed, options);
if (!overwrite) {
this.operations.atomic.check({ key: idKey, versionstamp: null });
}

if (collection._isIndexable) {
if (collection.一internal.isIndexable) {
// Add collection id key for collision detection
this.operations.indexAddCollectionKeys.push(collection._keys.base);
this.operations.indexAddCollectionKeys.push(
collection.一internal.keys.base,
);

// Add indexing operations
await setIndices(
Expand All @@ -555,9 +564,9 @@ export class AtomicBuilder<
}

// Set history entry if keeps history
if (this.collection._keepsHistory) {
if (this.collection.一internal.keepsHistory) {
const historyKey = extendKey(
this.collection._keys.history,
this.collection.一internal.keys.history,
docId,
ulid(),
);
Expand Down
Loading

0 comments on commit 48b9972

Please sign in to comment.