char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
78f4748
main
1import { 2openKv , 3type KvAccess , 4type KvAtomic , 5type KvAtomicResult , 6type KvDatabase , 7type KvEntry , 8type KvKey , 9type KvKeyspace , 10type KvListOptions , 11type KvOpenOptions , 12type KvPathUrl , 13type KvValue , 14type KvVersionstamp , 15} from "avril:kv" ; 16 17const access :KvAccess = "exclusive" ; 18const options :KvOpenOptions = { access, create :true }; 19const url :KvPathUrl = { href :"file:///tmp/kv.redb" }; 20 21const db :KvDatabase = await openKv ( "/tmp/kv.redb" , options ); 22const same :KvDatabase = await openKv ( url ); 23void same ; 24 25const space :KvKeyspace = db . keyspace ( "events" ); 26const spaceName :string = space . name ; 27void spaceName ; 28 29const key :KvKey = "users:42" ; 30const bytesKey :KvKey = new Uint8Array ([ 1 , 2 , 3 ]); 31void bytesKey ; 32 33const raw :Uint8Array | null = await space . get ( key ); 34void raw ; 35const text :string | null = await space . getText ( key ); 36void text ; 37 38const batch :( Uint8Array | null )[] = await space . getMany ([ key , "other" ]); 39void batch ; 40const textBatch :( string | null )[] = await space . getTextMany ([ key , "other" ]); 41void textBatch ; 42 43const entry :KvEntry | null = await space . entry ( key ); 44if ( entry !== null ) { 45const stamp :bigint = entry . versionstamp ; 46const owner :string = entry . keyspace ; 47const back :Uint8Array = entry . key ; 48const stored :Uint8Array = entry . value ; 49void stamp ; 50void owner ; 51void back ; 52void stored ; 53} 54 55const entries :( KvEntry | null )[] = await space . entries ([ key , "other" ]); 56void entries ; 57 58const written :{ readonly versionstamp :KvVersionstamp } = await space . set ( key , "v1" ); 59void written ; 60const writtenBytes :{ readonly versionstamp :KvVersionstamp } = await space . set ( 61new Uint8Array ([ 9 ]), 62new Uint8Array ([ 8 ]), 63); 64void writtenBytes ; 65await space . delete ( key ); 66await space . clear (); 67 68const listOptions :KvListOptions = { prefix :"user:" , limit :10 , reverse :true }; 69for await ( const listed of space . list ( listOptions )) { 70const again :KvEntry = listed ; 71void again ; 72} 73for await ( const viaBytes of space . list ({ prefix :new Uint8Array ([ 117 ]) })) { 74const again :KvEntry = viaBytes ; 75void again ; 76} 77 78const names :string [] = await db . keyspaces (); 79void names ; 80 81const result :KvAtomicResult = await db . atomic (( tx :KvAtomic ) => { 82tx . check ( space , key , null ); 83tx . set ( space , key , "1" ); 84tx . delete ( space , "gone" ); 85}); 86if ( entry !== null ) { 87await db . atomic (( tx ) => tx . check ( space , key , entry . versionstamp )); 88} 89if ( result . ok ) { 90const stamp :KvVersionstamp | null = result . versionstamp ; 91void stamp ; 92} 93 94const value :KvValue = "text" ; 95void value ; 96 97const genericDefault :KvEntry = entry as KvEntry ; 98void genericDefault ; 99 100// @ts-expect-error get returns bytes, not an entry object. 101const asEntry :{ value :unknown } = await space . get ( key ); 102void asEntry ; 103// @ts-expect-error getText returns a string, not an entry object. 104const asTextEntry :{ value :Uint8Array } = await space . getText ( key ); 105void asTextEntry ; 106// @ts-expect-error openKv requires a path. 107await openKv ( 42 ); 108// @ts-expect-error keyspace names must be strings. 109db . keyspace ( 1 ); 110// @ts-expect-error set rejects numbers. 111await space . set ( "k" , 42 ); 112// @ts-expect-error set rejects objects. 113await space . set ( "k" , { nested :true }); 114// @ts-expect-error get rejects numbers. 115await space . get ( 42 ); 116// @ts-expect-error versionstamp is a bigint, not a number. 117const numberStamp :KvVersionstamp = 42 ; 118void numberStamp ; 119// @ts-expect-error versionstamp is a bigint, not a string. 120const stringStamp :KvVersionstamp = "1" ; 121void stringStamp ; 122if ( entry !== null ) { 123// @ts-expect-error versionstamps do not narrow to strings. 124const asString :string = entry . versionstamp ; 125// @ts-expect-error versionstamps do not narrow to numbers. 126const asNumber :number = entry . versionstamp ; 127void asString ; 128void asNumber ; 129} 130await db . atomic (( tx ) => { 131// @ts-expect-error check expects a bigint stamp or null. 132tx . check ( space , key , "1" ); 133}); 134// @ts-expect-error list limit must be a number. 135space . list ({ limit :"many" }); 136// @ts-expect-error list options must be an object. 137space . list ( 42 ); 138// @ts-expect-error access must be a valid mode. 139await openKv ( "/tmp/kv.redb" , { access :"sometimes" });