Skip to content

Commit

Permalink
Move internals to private props (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-oloughlin authored Feb 8, 2025
1 parent 4466b4a commit 349be3a
Show file tree
Hide file tree
Showing 33 changed files with 267 additions and 318 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ jobs:

- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Check Types
run: deno task check
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/test_minimum.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Test

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

permissions:
contents: read
id-token: write # The OIDC ID token is used for authentication with JSR.

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Setup repo
uses: actions/checkout@v3

- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.0.0

- name: Check Types
run: deno task check

- name: Check formatting
run: deno fmt --check

- name: Run linter
run: deno lint

- name: "Test publish"
run: deno publish --dry-run

- name: Run tests
run: deno task test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ native functionality as possible, like atomic operations, real-time data updates
and queue listeners. Also works with other runtimes such as Node.js and Bun, and
has compatibility options for the browser.

_Supported Deno verisons:_ **^1.43.0**
_Supported Deno verisons:_ **^2.0.0**

## Highlights

Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@olli/kvdex",
"version": "3.1.2",
"version": "3.1.3",
"exports": {
".": "./mod.ts",
"./zod": "./src/ext/zod/mod.ts",
Expand Down Expand Up @@ -34,7 +34,7 @@
"@deno/kv": "npm:@deno/kv@^0.8.4",
"@std/assert": "jsr:@std/assert@1.0.7",
"@std/bytes": "jsr:@std/bytes@^1.0.4",
"@std/cli": "jsr:@std/cli@^1.0.6",
"@std/cli": "jsr:@std/cli@1.0.12",
"@std/collections": "jsr:@std/collections@^1.0.9",
"@std/ulid": "jsr:@std/ulid@^1.0.0",
"zod": "npm:zod@^3.23.8"
Expand Down
8 changes: 4 additions & 4 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 21 additions & 30 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.一internal.encoder) {
if (collection["encoder"]) {
throw new InvalidCollectionError(
"Atomic operations are not supported for serialized collections",
);
Expand Down Expand Up @@ -187,17 +187,15 @@ 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.一internal.keys.id, id);
const idKey = extendKey(collection["keys"].id, id);

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

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

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

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

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

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

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

const docId = id ?? await collection.一internal.idGenerator(parsed);
const idKey = extendKey(collection.一internal.keys.id, docId);
const docId = id ?? await collection["idGenerator"](parsed);
const idKey = extendKey(collection["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.一internal.isIndexable) {
if (collection["isIndexable"]) {
// Add collection id key for collision detection
this.operations.indexAddCollectionKeys.push(
collection.一internal.keys.base,
);
this.operations.indexAddCollectionKeys.push(collection["keys"].base);

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

// Set history entry if keeps history
if (this.collection.一internal.keepsHistory) {
if (this.collection["keepsHistory"]) {
const historyKey = extendKey(
this.collection.一internal.keys.history,
this.collection["keys"].history,
docId,
ulid(),
);
Expand Down
14 changes: 7 additions & 7 deletions src/atomic_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ export class AtomicPool implements DenoAtomicOperation {
this.pool = [];
}

set(key: DenoKvStrictKey, value: unknown, options?: DenoKvSetOptions) {
set(key: DenoKvStrictKey, value: unknown, options?: DenoKvSetOptions): this {
this.pool.push((op) => op.set(key, value, options));
return this;
}

delete(key: DenoKvStrictKey) {
delete(key: DenoKvStrictKey): this {
this.pool.push((op) => op.delete(key));
return this;
}

check(...checks: DenoAtomicCheck[]) {
check(...checks: DenoAtomicCheck[]): this {
this.pool.push((op) => op.check(...checks));
return this;
}

sum(key: DenoKvStrictKey, n: bigint) {
sum(key: DenoKvStrictKey, n: bigint): this {
this.pool.push((op) => op.sum(key, n));
return this;
}

max(key: DenoKvStrictKey, n: bigint) {
max(key: DenoKvStrictKey, n: bigint): this {
this.pool.push((op) => op.max(key, n));
return this;
}
Expand All @@ -51,7 +51,7 @@ export class AtomicPool implements DenoAtomicOperation {
delay?: number | undefined;
keysIfUndelivered?: DenoKvStrictKey[];
},
) {
): this {
this.pool.push((op) => op.enqueue(value, options));
return this;
}
Expand All @@ -60,7 +60,7 @@ export class AtomicPool implements DenoAtomicOperation {
throw Error("Not Implemented");
}

bindTo(atomic: DenoAtomicOperation) {
bindTo(atomic: DenoAtomicOperation): void {
this.pool.forEach((mutation) => mutation(atomic));
}
}
14 changes: 7 additions & 7 deletions src/atomic_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,29 @@ export class AtomicWrapper implements DenoAtomicOperation {
this.currentKeySize = 0;
}

set(key: DenoKvStrictKey, value: unknown, options?: DenoKvSetOptions) {
set(key: DenoKvStrictKey, value: unknown, options?: DenoKvSetOptions): this {
this.addMutation((op) => op.set(key, value, options), 67, 2, false);
return this;
}

delete(key: DenoKvStrictKey) {
delete(key: DenoKvStrictKey): this {
this.addMutation((op) => op.delete(key), 3, 2, false);
return this;
}

check(...checks: DenoAtomicCheck[]) {
check(...checks: DenoAtomicCheck[]): this {
checks.forEach((check) =>
this.addMutation((op) => op.check(check), 3, 2, true)
);
return this;
}

sum(key: DenoKvStrictKey, n: bigint) {
sum(key: DenoKvStrictKey, n: bigint): this {
this.addMutation((op) => op.sum(key, n), 3, 2, false);
return this;
}

max(key: DenoKvStrictKey, n: bigint) {
max(key: DenoKvStrictKey, n: bigint): this {
this.addMutation((op) => op.max(key, n), 3, 2, false);
return this;
}
Expand All @@ -75,7 +75,7 @@ export class AtomicWrapper implements DenoAtomicOperation {
delay?: number | undefined;
keysIfUndelivered?: DenoKvStrictKey[] | undefined;
} | undefined,
) {
): this {
this.addMutation(
(op) => op.enqueue(value, options),
96,
Expand Down Expand Up @@ -128,7 +128,7 @@ export class AtomicWrapper implements DenoAtomicOperation {
size: number,
keySize: number,
isCheck: boolean,
) {
): void {
this.currentSize += size;
this.currentKeySize += keySize;
this.currentCount++;
Expand Down
Loading

0 comments on commit 349be3a

Please sign in to comment.