Skip to content

Commit

Permalink
🔧
Browse files Browse the repository at this point in the history
  • Loading branch information
divmgl committed Oct 29, 2023
1 parent 39eff48 commit c3b8bd3
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 107 deletions.
3 changes: 2 additions & 1 deletion packages/example-fastify/src/AppContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export type AppContext = {
export async function createContext() {
const database = await createDatabase()

const context = Container.register("db", () => database)
const context = Container.new()
.register("db", () => database)
.instance("tasksCreator", TasksCreator)
.instance("tasks", SQLiteTaskStore)
.context<AppContext>()
Expand Down
8 changes: 6 additions & 2 deletions packages/example-fastify/src/SQLiteTaskStore.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { TaskStore } from "./TaskStore"
import { Task } from "./Task"
import { Service } from "./Service"
import { Service } from "nwire"
import { AppContext } from "./AppContext"

export class SQLiteTaskStore extends Service implements TaskStore {
export class SQLiteTaskStore
extends Service<AppContext>()
implements TaskStore
{
async get(id: number): Promise<Task | null> {
return (await this.db.get(`SELECT * FROM tasks WHERE id = ?`, [id])) ?? null
}
Expand Down
12 changes: 0 additions & 12 deletions packages/example-fastify/src/Service.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/example-fastify/src/TasksCreator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Service } from "nwire"
import { AppContext } from "./AppContext"
import { Service } from "./Service"

export class TasksCreator extends Service {
export class TasksCreator extends Service<AppContext>() {
async createBasicTasks() {
await this.tasks.save("My first test")
await this.tasks.save("My second test")
Expand Down
3 changes: 1 addition & 2 deletions packages/example-fastify/src/createServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ describe("server", function () {
beforeEach(async (context) => {
const database = await createDatabase()

const container = Container
//
const container = Container.new()
.register("db", () => database)
.instance("tasks", SQLiteTaskStore)
.instance("tasksCreator", TasksCreator)
Expand Down
12 changes: 7 additions & 5 deletions packages/example-hello-world/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Container, Injected } from "nwire"
import { Singleton } from "nwire"
import { Container, Service } from "nwire"

type MyTypedContext = {
type MyContext = {
banner: string
my: MyService
}

export class MyService extends Injected<MyTypedContext> {
export class MyService extends Service<MyContext>(Singleton) {
helloWorld() {
return this._context.banner
return this.banner
}
}

const context = Container.register("banner", () => "Hello world!")
const context = Container.new()
.register("banner", () => "Hello world!")
.instance("my", MyService)
.context()

Expand Down
6 changes: 3 additions & 3 deletions packages/nwire/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type MyTypedContext = {
my: MyService
}

export class MyService extends Injected<MyTypedContext> {
export class MyService extends WithContextProperties(Singleton) {
helloWorld() {
return this._context.banner
return this.banner
}
}

Expand Down Expand Up @@ -395,7 +395,7 @@ const user = await context.users.find("123")
the `UsersService` calls `users.findOne`, `nwire` will **lazily** return an instance of
`UserRepository`.

> ⚠️ `nwire` contexts are not normal objects. `nwire` use a proxy under the hood to evaluate your dependencies as needed. This is an intentional design decision to avoid having to instantiate the entire `Container` for tests.
> ⚠️ `nwire` contexts are not normal objects. `nwire` uses a proxy under the hood to evaluate your dependencies as needed. This is an intentional design decision to avoid having to instantiate the entire `Container` for tests.
## License

Expand Down
1 change: 1 addition & 0 deletions packages/nwire/dist/Container.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export declare class Container<TContext extends Context = {}> {
private _cache;
private _transient;
private _base;
private _context;
private _rootContainer;
private _parentContainer;
constructor(rootContainer?: Container, _parentContainer?: Container);
Expand Down
2 changes: 1 addition & 1 deletion packages/nwire/dist/Singleton.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Context } from "./Container";
type PopulatedSingleton<T> = T & {
[key in keyof T]: T[key];
};
export declare function WithContextProperties<T extends Context>(Base: any): new (context: T) => PopulatedSingleton<T>;
export declare function Service<T extends Context>(Base?: any): new (context: T) => PopulatedSingleton<T>;
export declare class Singleton<TContext extends Context> {
protected _context: TContext;
constructor(context: TContext);
Expand Down
45 changes: 25 additions & 20 deletions packages/nwire/dist/cjs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/nwire/dist/cjs/index.js.map

Large diffs are not rendered by default.

41 changes: 23 additions & 18 deletions packages/nwire/dist/esm/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c3b8bd3

Please sign in to comment.