Commit 8851a991 authored by nanahira's avatar nanahira

overload of set function

parent fe695d6d
......@@ -8,6 +8,7 @@ import { encode, decode } from 'encoded-buffer';
import { makeArray, MayBeArray } from './utility/utility';
import _ from 'lodash';
import { PartialDeep } from './utility/partial-deep';
export class Aragami {
readonly driver: BaseDriver;
......@@ -70,17 +71,39 @@ export class Aragami {
async set<T>(
o: T,
options: { ttl?: number; key?: string; prototype?: ClassType<T> } = {},
) {
options?: { ttl?: number; key?: string; prototype?: ClassType<T> },
): Promise<T>;
async set<T>(
prototype: ClassType<T>,
o: PartialDeep<T>,
options?: { ttl?: number; key?: string },
): Promise<T>;
async set<T>(...args: any[]) {
let prototype: ClassType<T>;
let o: T;
let options: { ttl?: number; key?: string; prototype?: ClassType<T> };
const firstArg = args[0];
if (typeof firstArg === 'function') {
prototype = firstArg;
o = args[1];
options = args[2] || {};
} else {
o = firstArg;
options = args[1] || {};
prototype = options.prototype;
}
if (!o) {
return o;
}
if (prototype) {
o = plainToInstance(prototype, o);
}
const buf = this.encode(o);
await this.driver.set(
this.getBaseKey(options.prototype || o),
options.key || (await this.getKey(o, options.prototype)),
this.getBaseKey(o),
options.key || (await this.getKey(o)),
buf,
options.ttl ?? this.getTTL(options.prototype || o),
options.ttl ?? this.getTTL(o),
);
return o;
}
......
export type PartialDeep<T> = T extends
| string
| number
| bigint
| boolean
| null
| undefined
| symbol
| Date
// eslint-disable-next-line @typescript-eslint/ban-types
| Function
? T | undefined
: // Arrays, Sets and Maps and their readonly counterparts have their items made
// deeply partial, but their own instances are left untouched
T extends Array<infer ArrayType>
? Array<PartialDeep<ArrayType>>
: T extends ReadonlyArray<infer ArrayType>
? ReadonlyArray<ArrayType>
: T extends Set<infer SetType>
? Set<PartialDeep<SetType>>
: T extends ReadonlySet<infer SetType>
? ReadonlySet<SetType>
: T extends Map<infer KeyType, infer ValueType>
? Map<PartialDeep<KeyType>, PartialDeep<ValueType>>
: T extends ReadonlyMap<infer KeyType, infer ValueType>
? ReadonlyMap<PartialDeep<KeyType>, PartialDeep<ValueType>>
: // ...and finally, all other objects.
{
[K in keyof T]?: PartialDeep<T[K]>;
};
......@@ -33,6 +33,10 @@ describe('Aragami.', () => {
await expect(aragami.has(user)).resolves.toBeTruthy();
await expect(aragami.has(User, 'n.John')).resolves.toBeTruthy();
await expect(aragami.has('user', 'n.John')).resolves.toBeTruthy();
const tmpUser = await aragami.set(User, { name: 'Nanahira', age: 17 });
expect(tmpUser).toEqual({ name: 'Nanahira', age: 17 });
expect(tmpUser).toBeInstanceOf(User);
await expect(aragami.del(tmpUser)).resolves.toBeTruthy();
const userGet = await aragami.get(User, 'n.John');
expect(userGet).toEqual({ name: 'John', age: 30 });
expect(userGet).toBeInstanceOf(User);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment