mirror of
https://github.com/HeyPuter/puter.git
synced 2025-12-30 17:50:00 -06:00
fix: backward compatibility for puter kv incr/decr (#1697)
This commit is contained in:
8
.github/workflows/test.yml
vendored
8
.github/workflows/test.yml
vendored
@@ -25,6 +25,7 @@ jobs:
|
||||
- name: Build
|
||||
run: |
|
||||
rm package-lock.json
|
||||
npm install -g npm@latest
|
||||
npm install
|
||||
npm run test
|
||||
|
||||
@@ -42,6 +43,13 @@ jobs:
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
rm package-lock.json
|
||||
npm install -g npm@latest
|
||||
npm install
|
||||
npm run test
|
||||
|
||||
- name: API Test
|
||||
run: |
|
||||
|
||||
2
src/puter-js/index.d.ts
vendored
2
src/puter-js/index.d.ts
vendored
@@ -298,7 +298,9 @@ interface KV {
|
||||
get(key: string): Promise<any>;
|
||||
del(key: string): Promise<boolean>;
|
||||
incr(key: string, pathAndAmount: { [key: string]: number }): Promise<number>;
|
||||
incr(key: string, amount?: number): Promise<number>;
|
||||
decr(key: string, pathAndAmount: { [key: string]: number }): Promise<number>;
|
||||
decr(key: string, amount?: number): Promise<number>;
|
||||
list(pattern?: string, returnValues?: boolean): Promise<string[] | KVPair[]>;
|
||||
list(returnValues?: boolean): Promise<string[] | KVPair[]>;
|
||||
flush(): Promise<boolean>;
|
||||
|
||||
@@ -173,7 +173,7 @@ class KV{
|
||||
}
|
||||
|
||||
options.key = args[0];
|
||||
options.pathAndAmountMap = args[1] ?? { '': 1 };
|
||||
options.pathAndAmountMap = !args[1] ? { '': 1 } : typeof args[1] === 'number' ? { '': args[1] } : args[1];
|
||||
|
||||
// key size cannot be larger than MAX_KEY_SIZE
|
||||
if ( options.key.length > this.MAX_KEY_SIZE ){
|
||||
@@ -192,7 +192,7 @@ class KV{
|
||||
}
|
||||
|
||||
options.key = args[0];
|
||||
options.pathAndAmountMap = args[1] ?? { '': 1 };
|
||||
options.pathAndAmountMap = !args[1] ? { '': 1 } : typeof args[1] === 'number' ? { '': args[1] } : args[1];
|
||||
|
||||
// key size cannot be larger than MAX_KEY_SIZE
|
||||
if ( options.key.length > this.MAX_KEY_SIZE ){
|
||||
|
||||
@@ -18,24 +18,83 @@ describe('Puter KV Module', () => {
|
||||
});
|
||||
|
||||
it('should increment a key success', async () => {
|
||||
const getRes = await puter.kv.get(TEST_KEY);
|
||||
expect(getRes).toBe(0);
|
||||
await puter.kv.set(TEST_KEY, 0);
|
||||
const incrRes = await puter.kv.incr(TEST_KEY, { '': 5 });
|
||||
console.log('incrRes', incrRes);
|
||||
expect(incrRes).toBe(5);
|
||||
const finalGet = await puter.kv.get(TEST_KEY);
|
||||
expect(finalGet).toBe(5);
|
||||
});
|
||||
|
||||
it('should decrement a key success', async () => {
|
||||
const getRes = await puter.kv.get(TEST_KEY);
|
||||
expect(getRes).toBe(5);
|
||||
const getRes = await puter.kv.set(TEST_KEY, 0);
|
||||
const decrRes = await puter.kv.decr(TEST_KEY, { '': 3 });
|
||||
console.log('decrRes', decrRes);
|
||||
expect(decrRes).toBe(2);
|
||||
expect(decrRes).toBe(-3);
|
||||
const finalGet = await puter.kv.get(TEST_KEY);
|
||||
expect(finalGet).toBe(-3);
|
||||
});
|
||||
|
||||
it('should increment a key with second argument', async () => {
|
||||
await puter.kv.set(TEST_KEY, 0);
|
||||
const incrRes = await puter.kv.incr(TEST_KEY);
|
||||
expect(incrRes).toBe(1);
|
||||
const finalGet = await puter.kv.get(TEST_KEY);
|
||||
expect(finalGet).toBe(1);
|
||||
})
|
||||
|
||||
it('should decrement a key with second argument', async () => {
|
||||
await puter.kv.set(TEST_KEY, 0);
|
||||
const incrRes = await puter.kv.decr(TEST_KEY);
|
||||
expect(incrRes).toBe(-1);
|
||||
const finalGet = await puter.kv.get(TEST_KEY);
|
||||
expect(finalGet).toBe(-1);
|
||||
})
|
||||
|
||||
it('should increment a key with second argument', async () => {
|
||||
await puter.kv.set(TEST_KEY, 0);
|
||||
const incrRes = await puter.kv.incr(TEST_KEY, 2);
|
||||
expect(incrRes).toBe(2);
|
||||
const finalGet = await puter.kv.get(TEST_KEY);
|
||||
expect(finalGet).toBe(2);
|
||||
});
|
||||
})
|
||||
|
||||
it('should decrement a key with second argument', async () => {
|
||||
await puter.kv.set(TEST_KEY, 0);
|
||||
const incrRes = await puter.kv.decr(TEST_KEY, 3);
|
||||
expect(incrRes).toBe(-3);
|
||||
const finalGet = await puter.kv.get(TEST_KEY);
|
||||
expect(finalGet).toBe(-3);
|
||||
})
|
||||
|
||||
it('should increment a key with nested path', async () => {
|
||||
await puter.kv.set(TEST_KEY, { a: { b: 0 } });
|
||||
const incrRes = await puter.kv.incr(TEST_KEY, { 'a.b': 1 });
|
||||
expect(incrRes).toEqual({ a: { b: 1 } });
|
||||
const finalGet = await puter.kv.get(TEST_KEY);
|
||||
expect(finalGet).toEqual({ a: { b: 1 } });
|
||||
})
|
||||
|
||||
it('should decrement a key with nested path', async () => {
|
||||
await puter.kv.set(TEST_KEY, { a: { b: 0 } });
|
||||
const incrRes = await puter.kv.decr(TEST_KEY, { 'a.b': 1 });
|
||||
expect(incrRes).toEqual({ a: { b: -1 } });
|
||||
const finalGet = await puter.kv.get(TEST_KEY);
|
||||
expect(finalGet).toEqual({ a: { b: -1 } });
|
||||
})
|
||||
|
||||
it('should increment a nonexistent key with nested path', async () => {
|
||||
const incrRes = await puter.kv.incr(TEST_KEY + 1, { 'a.b': 1 });
|
||||
expect(incrRes).toEqual({ a: { b: 1 } });
|
||||
const finalGet = await puter.kv.get(TEST_KEY + 1);
|
||||
expect(finalGet).toEqual({ a: { b: 1 } });
|
||||
})
|
||||
|
||||
it('should decrement a nonexistent key with nested path', async () => {
|
||||
const incrRes = await puter.kv.decr(TEST_KEY + 2, { 'a.b': 1 });
|
||||
expect(incrRes).toEqual({ a: { b: -1 } });
|
||||
const finalGet = await puter.kv.get(TEST_KEY + 2);
|
||||
expect(finalGet).toEqual({ a: { b: -1 } });
|
||||
})
|
||||
|
||||
it('should list keys', async () => {
|
||||
const listRes = await puter.kv.list();
|
||||
expect(Array.isArray(listRes)).toBe(true);
|
||||
|
||||
Reference in New Issue
Block a user