diff --git a/README.md b/README.md index 3101788..42091a9 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,7 @@ const kv = await Deno.openKv() const db = kvdex(kv, { numbers: collection(model()), serializedStrings: collection(model(), { - serialize: true + serialize: "auto" }), users: collection(UserSchema, { idGenerator: () => crypto.randomUUID(), @@ -665,11 +665,11 @@ db.users.listenQueue(async (data) => { Serialized collections can store much larger sized data by serializaing, compresing and splitting the data across multiple KV entries. There is a tradeoff between speed and storage efficiency. Custom serialize and compress -functions can be set through the collection options. The default serializer -picks between the Deno core serializer and custom json serializer depending on -being run in the standard Deno runtime or on Deploy (uses the slower json -serializer on Deploy as Deno core is not exposed). Alternatively, the serializer -can be specified to always use core or json. +functions can be set through the collection options. Setting the serialize +option to "auto" picks between the Deno core serializer and custom json +serializer depending on being run in the standard Deno runtime or on Deploy +(uses the slower json serializer on Deploy as Deno core is not exposed). +Alternatively, the serialize option can be set to specify between core or json. ```ts import { collection, kvdex, model } from "https://deno.land/x/kvdex/mod.ts" @@ -683,8 +683,8 @@ type LargeData = { const kv = await Deno.openKv() const db = kvdex(kv, { users: collection(model(), { - // Use default serialize/compress - serialize: true, + // Picks default serialize/compress + serialize: "auto", // Use Deno core serializer serialize: "core", diff --git a/src/collection.ts b/src/collection.ts index de52dab..7c8b277 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -208,7 +208,7 @@ export class Collection< const defaultSerialize = isDeploy ? jsonSerialize : denoCoreSerialize const defaultDeserialize = isDeploy ? jsonDeserialize : denoCoreDeserialize - if (opts?.serialize === true) { + if (opts?.serialize === "auto") { this._serializer = { serialize: defaultSerialize, deserialize: defaultDeserialize, diff --git a/src/kvdex.ts b/src/kvdex.ts index ae71a6b..2a86ad0 100644 --- a/src/kvdex.ts +++ b/src/kvdex.ts @@ -59,7 +59,7 @@ import { AtomicWrapper } from "./atomic_wrapper.ts" * const db = kvdex(kv, { * numbers: collection(model()), * u64s: collection(model()), - * serializedStrings: collection(model(), { serialize: true }), + * serializedStrings: collection(model(), { serialize: "auto" }), * users: collection(model(), { * indices: { * username: "primary", diff --git a/src/types.ts b/src/types.ts index b01b697..f1141e7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -308,7 +308,7 @@ export type SerializedEntry = { ids: KvId[] } -export type SerializeOptions = true | "core" | "json" | Partial +export type SerializeOptions = "auto" | "core" | "json" | Partial /***************************/ /* */ diff --git a/tests/ext/zod.test.ts b/tests/ext/zod.test.ts index ac2281b..eb124e4 100644 --- a/tests/ext/zod.test.ts +++ b/tests/ext/zod.test.ts @@ -80,7 +80,7 @@ Deno.test("ext - zod", async (t) => { age: "secondary", }, }), - s_users: collection(zodModel(UserSchema), { serialize: true }), + s_users: collection(zodModel(UserSchema), { serialize: "auto" }), }) const cr1 = await db.users.add({ diff --git a/tests/serialized_collection/enqueue.test.ts b/tests/serialized_collection/enqueue.test.ts index 6f07d50..802d0a9 100644 --- a/tests/serialized_collection/enqueue.test.ts +++ b/tests/serialized_collection/enqueue.test.ts @@ -17,7 +17,7 @@ Deno.test("serialized_collection - enqueue", async (t) => { const undeliveredId = "undelivered" const db = kvdex(kv, { - s_users: collection(model(), { serialize: true }), + s_users: collection(model(), { serialize: "auto" }), }) const sleeper = createResolver() diff --git a/tests/serialized_collection/properties.test.ts b/tests/serialized_collection/properties.test.ts index 1f1ac59..4d1e7a2 100644 --- a/tests/serialized_collection/properties.test.ts +++ b/tests/serialized_collection/properties.test.ts @@ -28,9 +28,11 @@ Deno.test("serialized_collection - properties", async (t) => { await useKv((kv) => { const db = kvdex(kv, { users1: collection(model(), { + serialize: "auto", idGenerator: () => Math.random(), }), users2: collection(model(), { + serialize: "auto", idGenerator: (data) => data.username, }), }) diff --git a/tests/serialized_collection/types.test.ts b/tests/serialized_collection/types.test.ts index b9fd99c..06ff6b7 100644 --- a/tests/serialized_collection/types.test.ts +++ b/tests/serialized_collection/types.test.ts @@ -8,12 +8,12 @@ Deno.test("serialized_collection - types", async (t) => { async () => { await useKv(async (kv) => { const db = kvdex(kv, { - nulls: collection(model(), { serialize: true }), - undefineds: collection(model(), { serialize: true }), - strings: collection(model(), { serialize: true }), - numbers: collection(model(), { serialize: true }), - bigints: collection(model(), { serialize: true }), - u64s: collection(model(), { serialize: true }), + nulls: collection(model(), { serialize: "auto" }), + undefineds: collection(model(), { serialize: "auto" }), + strings: collection(model(), { serialize: "auto" }), + numbers: collection(model(), { serialize: "auto" }), + bigints: collection(model(), { serialize: "auto" }), + u64s: collection(model(), { serialize: "auto" }), }) const cr1 = await db.nulls.add(null) @@ -49,12 +49,12 @@ Deno.test("serialized_collection - types", async (t) => { async () => { await useKv(async (kv) => { const db = kvdex(kv, { - dates: collection(model(), { serialize: true }), - sets: collection(model>(), { serialize: true }), - maps: collection(model>(), { serialize: true }), - regExps: collection(model(), { serialize: true }), - dataVeiws: collection(model(), { serialize: true }), - errors: collection(model(), { serialize: true }), + dates: collection(model(), { serialize: "auto" }), + sets: collection(model>(), { serialize: "auto" }), + maps: collection(model>(), { serialize: "auto" }), + regExps: collection(model(), { serialize: "auto" }), + dataVeiws: collection(model(), { serialize: "auto" }), + errors: collection(model(), { serialize: "auto" }), }) const cr1 = await db.dates.add(new Date()) @@ -103,19 +103,21 @@ Deno.test("serialized_collection - types", async (t) => { async () => { await useKv(async (kv) => { const db = kvdex(kv, { - arrs: collection(model>(), { serialize: true }), - i8arrs: collection(model(), { serialize: true }), - i16arrs: collection(model(), { serialize: true }), - i32arrs: collection(model(), { serialize: true }), - i64arrs: collection(model(), { serialize: true }), - u8arrs: collection(model(), { serialize: true }), - u16arrs: collection(model(), { serialize: true }), - u32arrs: collection(model(), { serialize: true }), - u64arrs: collection(model(), { serialize: true }), - u8carrs: collection(model(), { serialize: true }), - f32arrs: collection(model(), { serialize: true }), - f64arrs: collection(model(), { serialize: true }), - buffers: collection(model(), { serialize: true }), + arrs: collection(model>(), { serialize: "auto" }), + i8arrs: collection(model(), { serialize: "auto" }), + i16arrs: collection(model(), { serialize: "auto" }), + i32arrs: collection(model(), { serialize: "auto" }), + i64arrs: collection(model(), { serialize: "auto" }), + u8arrs: collection(model(), { serialize: "auto" }), + u16arrs: collection(model(), { serialize: "auto" }), + u32arrs: collection(model(), { serialize: "auto" }), + u64arrs: collection(model(), { serialize: "auto" }), + u8carrs: collection(model(), { + serialize: "auto", + }), + f32arrs: collection(model(), { serialize: "auto" }), + f64arrs: collection(model(), { serialize: "auto" }), + buffers: collection(model(), { serialize: "auto" }), }) const cr1 = await db.arrs.add(["str1", "str2", "str3"]) diff --git a/tests/serialized_indexable_collection/listenQueue.test.ts b/tests/serialized_indexable_collection/listenQueue.test.ts index 25979fe..4e85c46 100644 --- a/tests/serialized_indexable_collection/listenQueue.test.ts +++ b/tests/serialized_indexable_collection/listenQueue.test.ts @@ -22,7 +22,7 @@ Deno.test("serialized_indexable_collection - listenQueue", async (t) => { const sleeper = createResolver() const db = kvdex(kv, { - is_users: collection(model(), { indices: {}, serialize: true }), + is_users: collection(model(), { indices: {}, serialize: "auto" }), }) const handlerId = createHandlerId(db.is_users._keys.base, undefined) @@ -61,7 +61,7 @@ Deno.test("serialized_indexable_collection - listenQueue", async (t) => { await t.step("Should not receive db queue message", async () => { await useKv(async (kv) => { const db = kvdex(kv, { - is_users: collection(model(), { indices: {}, serialize: true }), + is_users: collection(model(), { indices: {}, serialize: "auto" }), }) let assertion = true diff --git a/tests/serialized_indexable_collection/properties.test.ts b/tests/serialized_indexable_collection/properties.test.ts index 3222ae4..e12dc9f 100644 --- a/tests/serialized_indexable_collection/properties.test.ts +++ b/tests/serialized_indexable_collection/properties.test.ts @@ -38,12 +38,12 @@ Deno.test("serialized_indexable_collection - properties", async (t) => { users1: collection(model(), { idGenerator: () => Math.random(), indices: {}, - serialize: true, + serialize: "auto", }), users2: collection(model(), { idGenerator: (data) => data.username, indices: {}, - serialize: true, + serialize: "auto", }), }) @@ -221,7 +221,7 @@ Deno.test("serialized_indexable_collection - properties", async (t) => { optPrimary: "primary", optSecondary: "secondary", }, - serialize: true, + serialize: "auto", }, ), }) @@ -419,182 +419,182 @@ Deno.test("serialized_indexable_collection - properties", async (t) => { const db = kvdex(kv, { val1: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val2: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val3: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val4: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val5: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val6: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val7: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val8: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val9: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val10: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val11: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val12: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val13: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val14: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val15: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val16: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val17: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val18: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val19: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val20: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val21: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val22: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val23: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val24: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val25: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", }, }), val26: collection(model(), { - serialize: true, + serialize: "auto", indices: { p: "primary", s: "secondary", diff --git a/tests/utils.ts b/tests/utils.ts index 8758a57..5a50f6d 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -7,7 +7,7 @@ export function createDb(kv: Deno.Kv) { return kvdex(kv, { u64s: collection(model()), s_u64s: collection(model(), { - serialize: true, + serialize: "auto", }), users: collection(model()), i_users: collection(model(), { @@ -17,14 +17,14 @@ export function createDb(kv: Deno.Kv) { }, }), s_users: collection(model(), { - serialize: true, + serialize: "auto", }), is_users: collection(model(), { indices: { username: "primary", age: "secondary", }, - serialize: true, + serialize: "auto", }), z_users: collection(UserSchema), zi_users: collection(UserSchema, { @@ -34,14 +34,14 @@ export function createDb(kv: Deno.Kv) { }, }), zs_users: collection(UserSchema, { - serialize: true, + serialize: "auto", }), zis_users: collection(UserSchema, { indices: { username: "primary", age: "secondary", }, - serialize: true, + serialize: "auto", }), a_users: collection(TransformUserModel), ai_users: collection(TransformUserModel, { @@ -51,14 +51,14 @@ export function createDb(kv: Deno.Kv) { }, }), as_users: collection(TransformUserModel, { - serialize: true, + serialize: "auto", }), ais_users: collection(TransformUserModel, { indices: { name: "primary", decadeAge: "secondary", }, - serialize: true, + serialize: "auto", }), }) }