mirror of
https://github.com/wassname/talk.git
synced 2026-08-14 12:50:17 +08:00
fix: type adjustments
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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";
|
||||
|
||||
|
||||
@@ -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<Asset> {
|
||||
return db.collection<Asset>("assets");
|
||||
function collection(db: Db) {
|
||||
return db.collection<Readonly<Asset>>("assets");
|
||||
}
|
||||
|
||||
export interface Asset extends TenantResource {
|
||||
@@ -33,7 +33,7 @@ export async function createAsset(
|
||||
db: Db,
|
||||
tenantID: string,
|
||||
input: CreateAssetInput
|
||||
): Promise<Readonly<Asset> | 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<Asset>("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<Asset | null> {
|
||||
return await db
|
||||
.collection<Asset>("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<Array<Asset | null>> {
|
||||
const cursor = await db
|
||||
.collection<Asset>("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<Readonly<Asset> | null> {
|
||||
const result = await db.collection<Asset>("assets").findOneAndUpdate(
|
||||
) {
|
||||
const result = await collection(db).findOneAndUpdate(
|
||||
{ id, tenant_id: tenantID },
|
||||
// Only update fields that have been updated.
|
||||
{ $set: dotize(update) },
|
||||
|
||||
@@ -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<Comment> {
|
||||
return db.collection<Comment>("comments");
|
||||
function collection(db: Db) {
|
||||
return db.collection<Readonly<Comment>>("comments");
|
||||
}
|
||||
|
||||
export interface BodyHistoryItem {
|
||||
@@ -62,7 +62,7 @@ export async function create(
|
||||
db: Db,
|
||||
tenantID: string,
|
||||
input: CreateCommentInput
|
||||
): Promise<Readonly<Comment>> {
|
||||
) {
|
||||
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<Comment> = 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<Readonly<Comment> | 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<Array<Readonly<Comment> | 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<Edge<Comment>> {
|
||||
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<Readonly<Connection<Comment>>> {
|
||||
) {
|
||||
// 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<Readonly<Connection<Comment>>> {
|
||||
) {
|
||||
// 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<Comment>
|
||||
): Promise<Readonly<Connection<Comment>>> {
|
||||
) {
|
||||
// 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<Connection<Readonly<Comment>>> = {
|
||||
edges,
|
||||
pageInfo: {
|
||||
hasNextPage,
|
||||
},
|
||||
};
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
@@ -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<Tenant> {
|
||||
return db.collection<Tenant>("tenants");
|
||||
function collection(db: Db) {
|
||||
return db.collection<Readonly<Tenant>>("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<Readonly<Tenant>> {
|
||||
export async function createTenant(db: Db, input: CreateTenantInput) {
|
||||
const defaults: Sub<Tenant, CreateTenantInput> = {
|
||||
// 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<Tenant> = 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<Readonly<Tenant> | null> {
|
||||
export async function retrieveTenantByDomain(db: Db, domain: string) {
|
||||
return collection(db).findOne({ domain });
|
||||
}
|
||||
|
||||
export async function retrieve(
|
||||
db: Db,
|
||||
id: string
|
||||
): Promise<Readonly<Tenant> | null> {
|
||||
export async function retrieve(db: Db, id: string) {
|
||||
return collection(db).findOne({ id });
|
||||
}
|
||||
|
||||
export async function retrieveManyTenants(
|
||||
db: Db,
|
||||
ids: string[]
|
||||
): Promise<Array<Readonly<Tenant> | 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<Array<Readonly<Tenant> | 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<Array<Readonly<Tenant>>> {
|
||||
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<CreateTenantInput>
|
||||
): Promise<Readonly<Tenant> | null> {
|
||||
) {
|
||||
// Get the tenant from the database.
|
||||
const result = await collection(db).findOneAndUpdate(
|
||||
{ id },
|
||||
|
||||
@@ -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<User> {
|
||||
return db.collection<User>("users");
|
||||
function collection(db: Db) {
|
||||
return db.collection<Readonly<User>>("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<Readonly<User>> {
|
||||
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<User> = 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<Readonly<User> | 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<Array<Readonly<User> | 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<Readonly<User> | null> {
|
||||
) {
|
||||
const result = await collection(db).findOneAndUpdate(
|
||||
{ id, tenant_id: tenantID },
|
||||
{ $set: { role } },
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Vendored
-9
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user