Skip to content

Commit

Permalink
feat(adapters): add adapter() function to inject configured adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Sep 8, 2024
1 parent d6f9fc7 commit 9a82771
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 130 deletions.
351 changes: 245 additions & 106 deletions packages/orm/adapters/src/decorators/injectAdapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,150 +3,289 @@ import {Injectable} from "@tsed/di";
import {Name, Property} from "@tsed/schema";
import {MemoryAdapter} from "../adapters/MemoryAdapter.js";
import {Adapter} from "../domain/Adapter.js";
import {InjectAdapter} from "./injectAdapter.js";
import {adapter, InjectAdapter} from "./injectAdapter.js";

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

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

const stub = vi.fn();
const stub = vi.fn();

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

$onInit() {
stub();
$onInit() {
stub();
}
}
}

const clients = await PlatformTest.invoke<Clients>(Clients);
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).toBeInstanceOf(MemoryAdapter);
expect(stub).toHaveBeenCalledWith();
expect(clients.adapter.collectionName).toBe("client");
expect(clients.adapter.model).toBe(Client);
const client = new Client();
client.clientId = "test";

const client = new Client();
client.clientId = "test";
await clients.adapter.create(client);

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;

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

@Name("client_id")
clientId: string;
}
const stub = vi.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";

const stub = vi.fn();
await clients.adapter.create(client);

@Injectable()
class Clients {
@InjectAdapter("client", Client, {useAlias: true})
adapter: Adapter<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 {}

$onInit() {
stub();
@Injectable()
class Clients {
@InjectAdapter(Client)
adapter: Adapter<Client>;
}
}

const clients = await PlatformTest.invoke<Clients>(Clients);
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);
expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(clients.adapter.collectionName).toBe("Clients");
expect(clients.adapter.model).toBe(Client);
});
it("should inject adapter (model only 2)", async () => {
class Entity {}

const client = new Client();
client.clientId = "test";
@Injectable()
class Clients {
@InjectAdapter(Entity)
adapter: Adapter<Entity>;
}

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

await clients.adapter.create(client);
expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(clients.adapter.collectionName).toBe("Entities");
expect(clients.adapter.model).toBe(Entity);
});
it("should inject adapter (object)", async () => {
class Client {}

const items = (clients.adapter as MemoryAdapter<Client>).collection.value();
expect(items).toEqual([
{
_id: expect.any(String),
client_id: "test"
@Injectable()
class Clients {
@InjectAdapter({model: Client, indexes: {}})
adapter: Adapter<Client>;
}
]);
});
it("should inject adapter (model only)", async () => {
class Client {}

@Injectable()
class Clients {
@InjectAdapter(Client)
adapter: Adapter<Client>;
}
const clients = await PlatformTest.invoke<Clients>(Clients);

const clients = await PlatformTest.invoke<Clients>(Clients);
expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(clients.adapter.collectionName).toBe("Clients");
expect(clients.adapter.model).toBe(Client);
});
it("should inject adapter (without $onInit)", async () => {
class Client {}

expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(clients.adapter.collectionName).toBe("Clients");
expect(clients.adapter.model).toBe(Client);
@Injectable()
class Clients {
@InjectAdapter("client", Client)
adapter: Adapter<Client>;
}

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

expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
});
});
it("should inject adapter (model only 2)", async () => {
class Entity {}
describe("using function adapter()", () => {
it("should inject adapter (model and collectionName)", async () => {
class Client {
@Property()
_id: string;

@Injectable()
class Clients {
@InjectAdapter(Entity)
adapter: Adapter<Entity>;
}
@Name("client_id")
clientId: string;
}

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

expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(clients.adapter.collectionName).toBe("Entities");
expect(clients.adapter.model).toBe(Entity);
});
it("should inject adapter (object)", async () => {
class Client {}
@Injectable()
class Clients {
adapter = adapter("client", Client);

@Injectable()
class Clients {
@InjectAdapter({model: Client, indexes: {}})
adapter: Adapter<Client>;
}
$onInit() {
stub();
}
}

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

expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(clients.adapter.collectionName).toBe("Clients");
expect(clients.adapter.model).toBe(Client);
});
it("should inject adapter (without $onInit)", async () => {
class Client {}
expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
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 = vi.fn();

@Injectable()
class Clients {
adapter = adapter("client", Client, {useAlias: true});

$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 {}

@Injectable()
class Clients {
adapter = adapter(Client);
}

@Injectable()
class Clients {
@InjectAdapter("client", Client)
adapter: Adapter<Client>;
}
const clients = await PlatformTest.invoke<Clients>(Clients);

expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(clients.adapter.collectionName).toBe("Clients");
expect(clients.adapter.model).toBe(Client);
});
it("should inject adapter (model only 2)", async () => {
class Entity {}

@Injectable()
class Clients {
adapter: Adapter<Entity> = adapter(Entity);
}

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

expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(clients.adapter.collectionName).toBe("Entities");
expect(clients.adapter.model).toBe(Entity);
});
it("should inject adapter (object)", async () => {
class Client {}

@Injectable()
class Clients {
adapter = adapter({model: Client, indexes: {}});
}

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

expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(clients.adapter.collectionName).toBe("Clients");
expect(clients.adapter.model).toBe(Client);
});
it("should inject adapter (without $onInit)", async () => {
class Client {}

@Injectable()
class Clients {
adapter = adapter("client", Client);
}

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

expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
expect(clients.adapter).toBeInstanceOf(MemoryAdapter);
});
});
});
Loading

0 comments on commit 9a82771

Please sign in to comment.