-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
407 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as mongo from "https://deno.land/x/[email protected]/mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./mongo.ts"; | ||
export * from "./repository.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { mongo } from "./deps.ts"; | ||
import { type Repository } from "./repository.ts"; | ||
|
||
class MongoRepository<T = mongo.Bson.Document> implements Repository<T, "_id"> { | ||
collection: mongo.Collection<T>; | ||
|
||
constructor(collection: mongo.Collection<T>) { | ||
this.collection = collection; | ||
} | ||
|
||
get(id: string): Promise<T | undefined> { | ||
return this.collection.findOne({ | ||
_id: new mongo.ObjectId(id), | ||
}); | ||
} | ||
|
||
getAll(): Promise<T[]> { | ||
return this.collection.find().toArray(); | ||
} | ||
|
||
async add(data: Omit<T, "_id">): Promise<string> { | ||
const id = await this.collection.insertOne(data); | ||
|
||
return String(id); | ||
} | ||
|
||
async update(id: string, data: Partial<T>): Promise<void> { | ||
await this.collection.updateOne( | ||
{ _id: new mongo.ObjectId(id) }, | ||
// @ts-ignore a bug in type definition | ||
{ $set: data }, | ||
); | ||
} | ||
|
||
async replace(id: string, data: Omit<T, "_id">): Promise<void> { | ||
await this.collection.replaceOne( | ||
{ _id: new mongo.ObjectId(id) }, | ||
// @ts-ignore a bug in type definition | ||
data, | ||
); | ||
} | ||
|
||
async remove(id: string): Promise<void> { | ||
await this.collection.deleteOne( | ||
{ _id: new mongo.ObjectId(id) }, | ||
); | ||
} | ||
} | ||
|
||
export { MongoRepository }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// deno-lint-ignore no-explicit-any | ||
interface Repository<T = unknown, K extends keyof any = "id"> { | ||
get(id: string): Promise<T | undefined>; | ||
getAll(): Promise<T[]>; | ||
|
||
add(data: Omit<T, K>): Promise<string>; | ||
update(id: string, data: Partial<T>): Promise<void>; | ||
replace(id: string, data: Omit<T, K>): Promise<void>; | ||
remove(id: string): Promise<void>; | ||
|
||
// TODO getCursor, bulkInsert, upsert, count, aggregate, etc. | ||
} | ||
|
||
export { type Repository }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * as asserts from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
export * as djwt from "https://deno.land/x/[email protected]/mod.ts"; | ||
export * as log from "https://deno.land/[email protected]/log/mod.ts"; | ||
export * as logLevels from "https://deno.land/[email protected]/log/levels.ts"; | ||
export * as oak from "https://deno.land/x/[email protected]/mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { oak } from "./deps.ts"; | ||
|
||
type Application = oak.Application; | ||
type Middleware = oak.Middleware; | ||
type Router = oak.Router; | ||
type State = oak.State; | ||
type Context = oak.Context & { | ||
params: Record<string, string>; | ||
}; | ||
type RouteParams<Route extends string> = oak.RouteParams<Route>; | ||
type Route< | ||
R extends string, | ||
P extends RouteParams<R> = RouteParams<R>, | ||
// deno-lint-ignore no-explicit-any | ||
S extends State = Record<string, any>, | ||
> = oak.Route<R, P, S>; | ||
type RouterMiddleware< | ||
R extends string, | ||
P extends RouteParams<R> = RouteParams<R>, | ||
// deno-lint-ignore no-explicit-any | ||
S extends State = Record<string, any>, | ||
> = oak.RouterMiddleware<R, P, S>; | ||
type RouterContext< | ||
R extends string, | ||
P extends RouteParams<R> = RouteParams<R>, | ||
// deno-lint-ignore no-explicit-any | ||
S extends State = Record<string, any>, | ||
> = oak.RouterContext<R, P, S>; | ||
|
||
type HttpMethods = | ||
| "all" | ||
| "get" | ||
| "post" | ||
| "patch" | ||
| "put" | ||
| "delete" | ||
| "head" | ||
| "options"; | ||
|
||
export { | ||
type Application, | ||
type Context, | ||
type HttpMethods, | ||
type Middleware, | ||
type Route, | ||
type RouteParams, | ||
type Router, | ||
type RouterContext, | ||
type RouterMiddleware, | ||
type State, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { log } from "../deps.ts"; | ||
|
||
// deno-lint-ignore no-explicit-any | ||
const timerMiddleware = async (ctx: any, next: any) => { | ||
const start = Date.now(); | ||
|
||
await next(); | ||
|
||
const ms = Date.now() - start; | ||
|
||
// ctx.response.headers.set("X-Response-Time", `${ms}ms`); | ||
log.info(`${ctx.request.method} ${ctx.request.url} - ${ms}ms`); | ||
}; | ||
|
||
export { timerMiddleware, timerMiddleware as default }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./http-types.ts"; | ||
export * from "./options.ts"; | ||
export * from "./service.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { logLevels } from "./deps.ts"; | ||
import * as options from "../options/mod.ts"; | ||
|
||
// interface definitions | ||
interface ServiceOptionsValues { | ||
port: number; | ||
logs: logLevels.LevelName; | ||
} | ||
|
||
type ServiceOptions = options.Options<ServiceOptionsValues>; | ||
|
||
// public functions | ||
const loadServiceOptions = async (): Promise<ServiceOptions> => { | ||
const serviceOptions = await options.loadOptions<ServiceOptionsValues>( | ||
(env, opts) => { | ||
opts.port = env.readInt("PORT", 3000); | ||
opts.logs = env.readEnum<logLevels.LevelName>("LOGS", [ | ||
"DEBUG", | ||
"INFO", | ||
"WARNING", | ||
"ERROR", | ||
"CRITICAL", | ||
], "INFO"); | ||
}, | ||
); | ||
|
||
return serviceOptions; | ||
}; | ||
|
||
export { loadServiceOptions, type ServiceOptions }; |
Oops, something went wrong.