From bcf5658c36d94857275aab4503a27295cb0c58f8 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 27 Jun 2018 11:39:26 -0600 Subject: [PATCH] fix: type adjustments --- src/core/server/app/middleware/tenant.ts | 4 +- src/core/server/graph/tenant/middleware.ts | 2 +- src/core/server/models/asset.ts | 47 +++++++++------------- src/core/server/models/comment.ts | 37 +++++++---------- src/core/server/models/tenant.ts | 39 +++++------------- src/core/server/models/user.ts | 28 ++++--------- src/core/server/types/express.ts | 9 +++++ src/types/express.d.ts | 9 ----- 8 files changed, 66 insertions(+), 109 deletions(-) create mode 100644 src/core/server/types/express.ts delete mode 100644 src/types/express.d.ts diff --git a/src/core/server/app/middleware/tenant.ts b/src/core/server/app/middleware/tenant.ts index 26c13e73a..9338b85c9 100644 --- a/src/core/server/app/middleware/tenant.ts +++ b/src/core/server/app/middleware/tenant.ts @@ -1,6 +1,8 @@ -import { NextFunction, Request, Response } from "express"; +import { NextFunction, Response } from "express"; import { Db } from "mongodb"; + import { retrieveTenantByDomain } from "talk-server/models/tenant"; +import { Request } from "talk-server/types/express"; export interface MiddlewareOptions { db: Db; diff --git a/src/core/server/graph/tenant/middleware.ts b/src/core/server/graph/tenant/middleware.ts index 776b8eca7..f583c6d7a 100644 --- a/src/core/server/graph/tenant/middleware.ts +++ b/src/core/server/graph/tenant/middleware.ts @@ -1,9 +1,9 @@ -import { Request } from "express"; import { GraphQLSchema } from "graphql"; import { Db } from "mongodb"; import { Config } from "talk-server/config"; import { graphqlMiddleware } from "talk-server/graph/common/middleware"; +import { Request } from "talk-server/types/express"; import TenantContext from "./context"; diff --git a/src/core/server/models/asset.ts b/src/core/server/models/asset.ts index 6fc5c55c9..e00dd2a56 100644 --- a/src/core/server/models/asset.ts +++ b/src/core/server/models/asset.ts @@ -1,13 +1,13 @@ import dotize from "dotize"; import { defaults } from "lodash"; -import { Collection, Db } from "mongodb"; +import { Db } from "mongodb"; import { Omit } from "talk-common/types"; import { TenantResource } from "talk-server/models/tenant"; import uuid from "uuid"; import Query from "./query"; -function collection(db: Db): Collection { - return db.collection("assets"); +function collection(db: Db) { + return db.collection>("assets"); } export interface Asset extends TenantResource { @@ -33,7 +33,7 @@ export async function createAsset( db: Db, tenantID: string, input: CreateAssetInput -): Promise | null> { +) { const now = new Date(); // Construct the filter. @@ -56,37 +56,30 @@ export async function createAsset( }; // Perform the upsert operation. - const result = await db - .collection("assets") - .findOneAndUpdate(query.filter, update, { - // Create the object if it doesn't already exist. - upsert: true, - // False to return the updated document instead of the original - // document. - returnOriginal: false, - }); + const result = await collection(db).findOneAndUpdate(query.filter, update, { + // Create the object if it doesn't already exist. + upsert: true, + // False to return the updated document instead of the original + // document. + returnOriginal: false, + }); return result.value || null; } -export async function retrieveAsset( - db: Db, - tenantID: string, - id: string -): Promise { - return await db - .collection("assets") - .findOne({ id, tenant_id: tenantID }); +export async function retrieveAsset(db: Db, tenantID: string, id: string) { + return await collection(db).findOne({ id, tenant_id: tenantID }); } export async function retrieveManyAssets( db: Db, tenantID: string, ids: string[] -): Promise> { - const cursor = await db - .collection("assets") - .find({ id: { $in: ids }, tenant_id: tenantID }); +) { + const cursor = await collection(db).find({ + id: { $in: ids }, + tenant_id: tenantID, + }); const assets = await cursor.toArray(); @@ -103,8 +96,8 @@ export async function updateAsset( tenantID: string, id: string, update: UpdateAssetInput -): Promise | null> { - const result = await db.collection("assets").findOneAndUpdate( +) { + const result = await collection(db).findOneAndUpdate( { id, tenant_id: tenantID }, // Only update fields that have been updated. { $set: dotize(update) }, diff --git a/src/core/server/models/comment.ts b/src/core/server/models/comment.ts index 058f3799a..6f11c2559 100644 --- a/src/core/server/models/comment.ts +++ b/src/core/server/models/comment.ts @@ -1,5 +1,5 @@ import { merge } from "lodash"; -import { Collection, Db } from "mongodb"; +import { Db } from "mongodb"; import { Omit, Sub } from "talk-common/types"; import { ActionCounts } from "talk-server/models/actions"; import { Connection, Cursor, Edge } from "talk-server/models/connection"; @@ -7,8 +7,8 @@ import Query from "talk-server/models/query"; import { TenantResource } from "talk-server/models/tenant"; import uuid from "uuid"; -function collection(db: Db): Collection { - return db.collection("comments"); +function collection(db: Db) { + return db.collection>("comments"); } export interface BodyHistoryItem { @@ -62,7 +62,7 @@ export async function create( db: Db, tenantID: string, input: CreateCommentInput -): Promise> { +) { const now = new Date(); // Pull out some useful properties from the input. @@ -90,7 +90,7 @@ export async function create( }; // Merge the defaults and the input together. - const comment: Comment = merge({}, defaults, input); + const comment: Readonly = merge({}, defaults, input); // TODO: Check for existence of the parent ID before we create the comment. @@ -104,19 +104,11 @@ export async function create( return comment; } -export async function retrieve( - db: Db, - tenantID: string, - id: string -): Promise | null> { +export async function retrieve(db: Db, tenantID: string, id: string) { return collection(db).findOne({ id, tenant_id: tenantID }); } -export async function retrieveMany( - db: Db, - tenantID: string, - ids: string[] -): Promise | null>> { +export async function retrieveMany(db: Db, tenantID: string, ids: string[]) { const cursor = await collection(db).find({ id: { $in: ids, @@ -149,10 +141,7 @@ export interface ConnectionInput { * @param input connection configuration * @param nodes nodes returned from the query */ -function nodesToEdge( - input: ConnectionInput, - nodes: Comment[] -): Array> { +function nodesToEdge(input: ConnectionInput, nodes: Comment[]) { let getCursor: (comment: Comment, index: number) => Cursor; switch (input.orderBy) { case CommentSort.CREATED_AT_DESC: @@ -186,7 +175,7 @@ export async function retrieveRepliesConnection( assetID: string, parentID: string, input: ConnectionInput -): Promise>> { +) { // Create the query. const query = new Query(collection(db)).where({ tenant_id: tenantID, @@ -211,7 +200,7 @@ export async function retrieveAssetConnection( tenantID: string, assetID: string, input: ConnectionInput -): Promise>> { +) { // Create the query. const query = new Query(collection(db)).where({ tenant_id: tenantID, @@ -234,7 +223,7 @@ export async function retrieveAssetConnection( async function retrieveConnection( input: ConnectionInput, query: Query -): Promise>> { +) { // Apply some sorting options. switch (input.orderBy) { case CommentSort.CREATED_AT_DESC: @@ -287,10 +276,12 @@ async function retrieveConnection( const edges = nodesToEdge(input, nodes); // Return the connection. - return { + const connection: Readonly>> = { edges, pageInfo: { hasNextPage, }, }; + + return connection; } diff --git a/src/core/server/models/tenant.ts b/src/core/server/models/tenant.ts index 44a7d316b..8e41a1bf2 100644 --- a/src/core/server/models/tenant.ts +++ b/src/core/server/models/tenant.ts @@ -1,11 +1,11 @@ import dotize from "dotize"; import { merge } from "lodash"; -import { Collection, Db } from "mongodb"; +import { Db } from "mongodb"; import { Sub } from "talk-common/types"; import uuid from "uuid"; -function collection(db: Db): Collection { - return db.collection("tenants"); +function collection(db: Db) { + return db.collection>("tenants"); } export interface TenantResource { @@ -75,10 +75,7 @@ export type CreateTenantInput = Pick< * @param db the MongoDB connection used to create the tenant. * @param input the customizable parts of the Tenant available during creation */ -export async function createTenant( - db: Db, - input: CreateTenantInput -): Promise> { +export async function createTenant(db: Db, input: CreateTenantInput) { const defaults: Sub = { // Create a new ID. id: uuid.v4(), @@ -105,7 +102,7 @@ export async function createTenant( }; // Create the new Tenant by merging it together with the defaults. - const tenant = merge({}, input, defaults); + const tenant: Readonly = merge({}, input, defaults); // Insert the Tenant into the database. await collection(db).insert(tenant); @@ -113,24 +110,15 @@ export async function createTenant( return tenant; } -export async function retrieveTenantByDomain( - db: Db, - domain: string -): Promise | null> { +export async function retrieveTenantByDomain(db: Db, domain: string) { return collection(db).findOne({ domain }); } -export async function retrieve( - db: Db, - id: string -): Promise | null> { +export async function retrieve(db: Db, id: string) { return collection(db).findOne({ id }); } -export async function retrieveManyTenants( - db: Db, - ids: string[] -): Promise | null>> { +export async function retrieveManyTenants(db: Db, ids: string[]) { const cursor = await collection(db).find({ id: { $in: ids, @@ -142,10 +130,7 @@ export async function retrieveManyTenants( return ids.map(id => tenants.find(tenant => tenant.id === id) || null); } -export async function retrieveManyTenantsByDomain( - db: Db, - domains: string[] -): Promise | null>> { +export async function retrieveManyTenantsByDomain(db: Db, domains: string[]) { const cursor = await collection(db).find({ domain: { $in: domains, @@ -159,9 +144,7 @@ export async function retrieveManyTenantsByDomain( ); } -export async function retrieveAllTenants( - db: Db -): Promise>> { +export async function retrieveAllTenants(db: Db) { return collection(db) .find({}) .toArray(); @@ -171,7 +154,7 @@ export async function updateTenant( db: Db, id: string, update: Partial -): Promise | null> { +) { // Get the tenant from the database. const result = await collection(db).findOneAndUpdate( { id }, diff --git a/src/core/server/models/user.ts b/src/core/server/models/user.ts index 14182c666..6812fefce 100644 --- a/src/core/server/models/user.ts +++ b/src/core/server/models/user.ts @@ -1,12 +1,12 @@ import { merge } from "lodash"; -import { Collection, Db } from "mongodb"; +import { Db } from "mongodb"; import { Omit, Sub } from "talk-common/types"; import { ActionCounts } from "talk-server/models/actions"; import { TenantResource } from "talk-server/models/tenant"; import uuid from "uuid"; -function collection(db: Db): Collection { - return db.collection("users"); +function collection(db: Db) { + return db.collection>("users"); } export interface Profile { @@ -95,11 +95,7 @@ export type CreateUserInput = Omit< | "created_at" >; -export async function create( - db: Db, - tenantID: string, - input: CreateUserInput -): Promise> { +export async function create(db: Db, tenantID: string, input: CreateUserInput) { const now = new Date(); // // Pull out some useful properties from the input. @@ -132,7 +128,7 @@ export async function create( }; // Merge the defaults and the input together. - const user: User = merge({}, defaults, input); + const user: Readonly = merge({}, defaults, input); // Insert it into the database. await collection(db).insertOne(user); @@ -140,19 +136,11 @@ export async function create( return user; } -export async function retrieve( - db: Db, - tenantID: string, - id: string -): Promise | null> { +export async function retrieve(db: Db, tenantID: string, id: string) { return collection(db).findOne({ id, tenant_id: tenantID }); } -export async function retrieveMany( - db: Db, - tenantID: string, - ids: string[] -): Promise | null>> { +export async function retrieveMany(db: Db, tenantID: string, ids: string[]) { const cursor = await collection(db).find({ id: { $in: ids, @@ -170,7 +158,7 @@ export async function updateRole( tenantID: string, id: string, role: UserRole -): Promise | null> { +) { const result = await collection(db).findOneAndUpdate( { id, tenant_id: tenantID }, { $set: { role } }, diff --git a/src/core/server/types/express.ts b/src/core/server/types/express.ts new file mode 100644 index 000000000..d18c0fdfb --- /dev/null +++ b/src/core/server/types/express.ts @@ -0,0 +1,9 @@ +import { Request } from "express"; + +import { Tenant } from "talk-server/models/tenant"; +import { User } from "talk-server/models/user"; + +export interface Request extends Request { + user?: User; + tenant?: Tenant; +} diff --git a/src/types/express.d.ts b/src/types/express.d.ts deleted file mode 100644 index b870dde73..000000000 --- a/src/types/express.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Tenant } from "talk-server/models/tenant"; -import { User } from "talk-server/models/user"; - -declare module "express" { - interface Request { - user?: User; - tenant?: Tenant; - } -}