Skip to content

Commit

Permalink
fix(adapters): add useAlias to store correctly the model in database
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Aug 27, 2022
1 parent 0feb374 commit 964508c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/orm/adapters/src/adapters/LowDbAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class LowDbAdapter<T extends AdapterModel> extends Adapter<T> {

await this.validate(payload as T);

await this.collection.push(payload as T).write();
await this.collection.push(this.serialize(payload) as T).write();

return this.deserialize(payload);
}
Expand All @@ -44,7 +44,7 @@ export class LowDbAdapter<T extends AdapterModel> extends Adapter<T> {
payload = {...payload, _id: id || uuid(), expires_at: expiresAt};

await this.validate(payload as T);
await this.collection.push(payload as T).write();
await this.collection.push(this.serialize(payload) as T).write();

return this.deserialize(payload);
}
Expand Down
64 changes: 63 additions & 1 deletion packages/orm/adapters/src/decorators/injectAdapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import {Adapter} from "@tsed/adapters";
import {PlatformTest} from "@tsed/common";
import {Injectable} from "@tsed/di";
import {Name, Property} from "@tsed/schema";
import {MemoryAdapter} from "../adapters/MemoryAdapter";
import {InjectAdapter} from "./injectAdapter";

describe("InjectAdapter", () => {
beforeEach(() => PlatformTest.create());
afterEach(() => PlatformTest.create());
it("should inject adapter (model and collectionName)", async () => {
class Client {}
class Client {
@Property()
_id: string;

@Name("client_id")
clientId: string;
}

const stub = jest.fn();

Expand All @@ -28,6 +35,61 @@ describe("InjectAdapter", () => {
expect(stub).toHaveBeenCalledWith();
expect(clients.adapter.collectionName).toBe("client");
expect(clients.adapter.model).toBe(Client);

const client = new Client();
client.clientId = "test";

await clients.adapter.create(client);

const items = (clients.adapter as MemoryAdapter<Client>).collection.value();
expect(items).toEqual([
{
_id: expect.any(String),
clientId: "test"
}
]);
});
it("should inject adapter (model, collectionName and useAlias true)", async () => {
class Client {
@Property()
_id: string;

@Name("client_id")
clientId: string;
}

const stub = jest.fn();

@Injectable()
class Clients {
@InjectAdapter("client", Client, {useAlias: true})
adapter: Adapter<Client>;

$onInit() {
stub();
}
}

const clients = await PlatformTest.invoke<Clients>(Clients);

expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(stub).toHaveBeenCalledWith();
expect(clients.adapter.collectionName).toBe("client");
expect(clients.adapter.model).toBe(Client);
expect(clients.adapter.useAlias).toEqual(true);

const client = new Client();
client.clientId = "test";

await clients.adapter.create(client);

const items = (clients.adapter as MemoryAdapter<Client>).collection.value();
expect(items).toEqual([
{
_id: expect.any(String),
client_id: "test"
}
]);
});
it("should inject adapter (model only)", async () => {
class Client {}
Expand Down
8 changes: 6 additions & 2 deletions packages/orm/adapters/src/decorators/injectAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ function mapOptions(args: any[]): AdapterInvokeOptions {
}

export function InjectAdapter(options: AdapterInvokeOptions): PropertyDecorator;
export function InjectAdapter(model: Type<any>, options?: Partial<AdapterInvokeOptions>): PropertyDecorator;
export function InjectAdapter(collectionName: string, model: Type<any>, options?: Partial<AdapterInvokeOptions>): PropertyDecorator;
export function InjectAdapter(model: Type<any>, options?: Partial<Omit<AdapterInvokeOptions, "client">>): PropertyDecorator;
export function InjectAdapter(
collectionName: string,
model: Type<any>,
options?: Partial<Omit<AdapterInvokeOptions, "collectionName" | "client">>
): PropertyDecorator;
export function InjectAdapter(...args: any[]): PropertyDecorator {
const options: AdapterInvokeOptions = mapOptions(args);

Expand Down
15 changes: 12 additions & 3 deletions packages/orm/adapters/src/domain/Adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export abstract class Adapter<Model = any> {
readonly model: Type<Model> | Object;
readonly collectionName: string;
readonly indexes: {propertyKey: string; options: any}[];
readonly useAlias: boolean = false;

@Inject()
protected ajvService: AjvService;
Expand All @@ -56,12 +57,13 @@ export abstract class Adapter<Model = any> {
}

this.indexes = indexes;
this.useAlias = !!options.useAlias;
}

async validate(value: Model): Promise<void> {
if (this.getModel()) {
await this.ajvService.validate(this.serialize(value), {
useAlias: false,
useAlias: this.useAlias,
type: this.model
});
}
Expand Down Expand Up @@ -104,15 +106,22 @@ export abstract class Adapter<Model = any> {

protected deserialize(obj: any, opts?: JsonDeserializerOptions) {
return deserialize(obj, {
useAlias: false,
useAlias: this.useAlias,
...opts,
type: this.getModel()
});
}

protected serialize(obj: any, opts?: JsonSerializerOptions) {
console.log(
serialize(obj, {
useAlias: this.useAlias,
...opts,
type: this.getModel()
})
);
return serialize(obj, {
useAlias: false,
useAlias: this.useAlias,
...opts,
type: this.getModel()
});
Expand Down
2 changes: 0 additions & 2 deletions packages/orm/adapters/src/domain/AdaptersSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export interface AdaptersSettings {

declare global {
namespace TsED {
interface AdaptersOptions extends AdaptersSettings {}

interface Configuration {
adapters: AdaptersSettings;
}
Expand Down

0 comments on commit 964508c

Please sign in to comment.