Update mongodb

This commit is contained in:
syuilo 2018-10-16 11:38:09 +09:00
parent d32b2a8ce5
commit 9427a756c9
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
31 changed files with 83 additions and 52 deletions

View File

@ -160,7 +160,7 @@
"mkdirp": "0.5.1",
"mocha": "5.2.0",
"moji": "0.5.1",
"mongodb": "3.1.1",
"mongodb": "3.1.8",
"monk": "6.0.6",
"ms": "2.1.1",
"nan": "2.11.1",

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import { Context } from 'cafy';
import isObjectId from './is-objectid';
export const isAnId = (x: any) => mongo.ObjectID.isValid(x);
export const isNotAnId = (x: any) => !isAnId(x);
@ -12,7 +13,7 @@ export default class ID extends Context<mongo.ObjectID> {
super();
this.transform = v => {
if (isAnId(v) && !mongo.ObjectID.prototype.isPrototypeOf(v)) {
if (isAnId(v) && !isObjectId(v)) {
return new mongo.ObjectID(v);
} else {
return v;
@ -20,7 +21,7 @@ export default class ID extends Context<mongo.ObjectID> {
};
this.push(v => {
if (!mongo.ObjectID.prototype.isPrototypeOf(v) && isNotAnId(v)) {
if (!isObjectId(v) && isNotAnId(v)) {
return new Error('must-be-an-id');
}
return true;

3
src/misc/is-objectid.ts Normal file
View File

@ -0,0 +1,3 @@
export default function(x: any): boolean {
return x.hasOwnProperty('toHexString') || x.hasOwnProperty('_bsontype');
}

View File

@ -1,7 +1,8 @@
import * as mongo from 'mongodb';
import isObjectId from './is-objectid';
function toString(id: any) {
return mongo.ObjectID.prototype.isPrototypeOf(id) ? (id as mongo.ObjectID).toHexString() : id;
return isObjectId(id) ? (id as mongo.ObjectID).toHexString() : id;
}
export default function(note: any, mutedUserIds: string[]): boolean {

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const AccessToken = db.get<IAccessToken>('accessTokens');
AccessToken.createIndex('token');
@ -22,7 +23,7 @@ export async function deleteAccessToken(accessToken: string | mongo.ObjectID | I
let a: IAccessToken;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(accessToken)) {
if (isObjectId(accessToken)) {
a = await AccessToken.findOne({
_id: accessToken
});

View File

@ -2,6 +2,7 @@ import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import AccessToken from './access-token';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import config from '../config';
const App = db.get<IApp>('apps');
@ -43,7 +44,7 @@ export const pack = (
let _app: any;
// Populate the app if 'app' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(app)) {
if (isObjectId(app)) {
_app = await App.findOne({
_id: app
});
@ -56,7 +57,7 @@ export const pack = (
}
// Me
if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) {
if (me && !isObjectId(me)) {
if (typeof me === 'string') {
me = new mongo.ObjectID(me);
} else {

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { pack as packApp } from './app';
const AuthSession = db.get<IAuthSession>('authSessions');
@ -31,7 +32,7 @@ export const pack = (
_session = deepcopy(session);
// Me
if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) {
if (me && !isObjectId(me)) {
if (typeof me === 'string') {
me = new mongo.ObjectID(me);
} else {

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import monkDb, { nativeDbConn } from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const DriveFileThumbnail = monkDb.get<IDriveFileThumbnail>('driveFileThumbnails.files');
DriveFileThumbnail.createIndex('metadata.originalId', { sparse: true, unique: true });
@ -35,7 +36,7 @@ export async function deleteDriveFileThumbnail(driveFile: string | mongo.ObjectI
let d: IDriveFileThumbnail;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(driveFile)) {
if (isObjectId(driveFile)) {
d = await DriveFileThumbnail.findOne({
_id: driveFile
});

View File

@ -3,6 +3,7 @@ const deepcopy = require('deepcopy');
import { pack as packFolder } from './drive-folder';
import config from '../config';
import monkDb, { nativeDbConn } from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import Note, { deleteNote } from './note';
import MessagingMessage, { deleteMessagingMessage } from './messaging-message';
import User from './user';
@ -78,7 +79,7 @@ export async function deleteDriveFile(driveFile: string | mongo.ObjectID | IDriv
let d: IDriveFile;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(driveFile)) {
if (isObjectId(driveFile)) {
d = await DriveFile.findOne({
_id: driveFile
});
@ -154,7 +155,7 @@ export const pack = (
let _file: any;
// Populate the file if 'file' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(file)) {
if (isObjectId(file)) {
_file = await DriveFile.findOne({
_id: file
});

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import DriveFile from './drive-file';
const DriveFolder = db.get<IDriveFolder>('driveFolders');
@ -29,7 +30,7 @@ export async function deleteDriveFolder(driveFolder: string | mongo.ObjectID | I
let d: IDriveFolder;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(driveFolder)) {
if (isObjectId(driveFolder)) {
d = await DriveFolder.findOne({
_id: driveFolder
});
@ -83,7 +84,7 @@ export const pack = (
let _folder: any;
// Populate the folder if 'folder' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(folder)) {
if (isObjectId(folder)) {
_folder = await DriveFolder.findOne({ _id: folder });
} else if (typeof folder === 'string') {
_folder = await DriveFolder.findOne({ _id: new mongo.ObjectID(folder) });

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { pack as packNote } from './note';
const Favorite = db.get<IFavorite>('favorites');
@ -21,7 +22,7 @@ export async function deleteFavorite(favorite: string | mongo.ObjectID | IFavori
let f: IFavorite;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(favorite)) {
if (isObjectId(favorite)) {
f = await Favorite.findOne({
_id: favorite
});
@ -58,7 +59,7 @@ export const pack = (
let _favorite: any;
// Populate the favorite if 'favorite' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(favorite)) {
if (isObjectId(favorite)) {
_favorite = await Favorite.findOne({
_id: favorite
});

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { pack as packUser } from './user';
const FollowRequest = db.get<IFollowRequest>('followRequests');
@ -34,7 +35,7 @@ export async function deleteFollowRequest(followRequest: string | mongo.ObjectID
let f: IFollowRequest;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(followRequest)) {
if (isObjectId(followRequest)) {
f = await FollowRequest.findOne({
_id: followRequest
});
@ -64,7 +65,7 @@ export const pack = (
let _request: any;
// Populate the request if 'request' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(request)) {
if (isObjectId(request)) {
_request = await FollowRequest.findOne({
_id: request
});

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const FollowedLog = db.get<IFollowedLog>('followedLogs');
export default FollowedLog;
@ -18,7 +19,7 @@ export async function deleteFollowedLog(followedLog: string | mongo.ObjectID | I
let f: IFollowedLog;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(followedLog)) {
if (isObjectId(followedLog)) {
f = await FollowedLog.findOne({
_id: followedLog
});

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const FollowingLog = db.get<IFollowingLog>('followingLogs');
export default FollowingLog;
@ -18,7 +19,7 @@ export async function deleteFollowingLog(followingLog: string | mongo.ObjectID |
let f: IFollowingLog;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(followingLog)) {
if (isObjectId(followingLog)) {
f = await FollowingLog.findOne({
_id: followingLog
});

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const Following = db.get<IFollowing>('following');
Following.createIndex(['followerId', 'followeeId'], { unique: true });
@ -32,7 +33,7 @@ export async function deleteFollowing(following: string | mongo.ObjectID | IFoll
let f: IFollowing;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(following)) {
if (isObjectId(following)) {
f = await Following.findOne({
_id: following
});

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import db from '../../../db/mongodb';
import isObjectId from '../../../misc/is-objectid';
import { IUser, pack as packUser } from '../../user';
const ReversiGame = db.get<IReversiGame>('reversiGames');
@ -62,7 +63,7 @@ export const pack = (
let _game: any;
// Populate the game if 'game' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(game)) {
if (isObjectId(game)) {
_game = await ReversiGame.findOne({
_id: game
});
@ -76,7 +77,7 @@ export const pack = (
// Me
const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me)
? isObjectId(me)
? me as mongo.ObjectID
: typeof me === 'string'
? new mongo.ObjectID(me)

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import db from '../../../db/mongodb';
import isObjectId from '../../../misc/is-objectid';
import { IUser, pack as packUser } from '../../user';
const Matching = db.get<IMatching>('reversiMatchings');
@ -23,7 +24,7 @@ export const pack = (
// Me
const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me)
? isObjectId(me)
? me as mongo.ObjectID
: typeof me === 'string'
? new mongo.ObjectID(me)

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const MessagingHistory = db.get<IMessagingHistory>('messagingHistories');
export default MessagingHistory;
@ -19,7 +20,7 @@ export async function deleteMessagingHistory(messagingHistory: string | mongo.Ob
let m: IMessagingHistory;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(messagingHistory)) {
if (isObjectId(messagingHistory)) {
m = await MessagingHistory.findOne({
_id: messagingHistory
});

View File

@ -3,6 +3,7 @@ const deepcopy = require('deepcopy');
import { pack as packUser } from './user';
import { pack as packFile } from './drive-file';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import MessagingHistory, { deleteMessagingHistory } from './messaging-history';
import { length } from 'stringz';
@ -30,7 +31,7 @@ export async function deleteMessagingMessage(messagingMessage: string | mongo.Ob
let m: IMessagingMessage;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(messagingMessage)) {
if (isObjectId(messagingMessage)) {
m = await MessagingMessage.findOne({
_id: messagingMessage
});
@ -72,7 +73,7 @@ export const pack = (
let _message: any;
// Populate the message if 'message' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(message)) {
if (isObjectId(message)) {
_message = await MessagingMessage.findOne({
_id: message
});

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const Mute = db.get<IMute>('mute');
Mute.createIndex(['muterId', 'muteeId'], { unique: true });
@ -19,7 +20,7 @@ export async function deleteMute(mute: string | mongo.ObjectID | IMute) {
let m: IMute;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(mute)) {
if (isObjectId(mute)) {
m = await Mute.findOne({
_id: mute
});

View File

@ -2,6 +2,7 @@ import * as mongo from 'mongodb';
import $ from 'cafy';
const deepcopy = require('deepcopy');
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import Reaction from './note-reaction';
import { pack as packUser } from './user';
@ -37,7 +38,7 @@ export async function deleteNoteReaction(noteReaction: string | mongo.ObjectID |
let n: INoteReaction;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(noteReaction)) {
if (isObjectId(noteReaction)) {
n = await NoteReaction.findOne({
_id: noteReaction
});
@ -67,7 +68,7 @@ export const pack = (
let _reaction: any;
// Populate the reaction if 'reaction' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(reaction)) {
if (isObjectId(reaction)) {
_reaction = await Reaction.findOne({
_id: reaction
});

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const NoteWatching = db.get<INoteWatching>('noteWatching');
NoteWatching.createIndex(['userId', 'noteId'], { unique: true });
@ -19,7 +20,7 @@ export async function deleteNoteWatching(noteWatching: string | mongo.ObjectID |
let n: INoteWatching;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(noteWatching)) {
if (isObjectId(noteWatching)) {
n = await NoteWatching.findOne({
_id: noteWatching
});

View File

@ -2,6 +2,7 @@ import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import rap from '@prezzemolo/rap';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { length } from 'stringz';
import { IUser, pack as packUser } from './user';
import { pack as packApp } from './app';
@ -107,7 +108,7 @@ export async function deleteNote(note: string | mongo.ObjectID | INote) {
let n: INote;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(note)) {
if (isObjectId(note)) {
n = await Note.findOne({
_id: note
});
@ -259,7 +260,7 @@ export const pack = async (
// Me
const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me)
? isObjectId(me)
? me as mongo.ObjectID
: typeof me === 'string'
? new mongo.ObjectID(me)
@ -269,7 +270,7 @@ export const pack = async (
let _note: any;
// Populate the note if 'note' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(note)) {
if (isObjectId(note)) {
_note = await Note.findOne({
_id: note
});

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { IUser, pack as packUser } from './user';
import { pack as packNote } from './note';
@ -57,7 +58,7 @@ export async function deleteNotification(notification: string | mongo.ObjectID |
let n: INotification;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(notification)) {
if (isObjectId(notification)) {
n = await Notification.findOne({
_id: notification
});
@ -90,7 +91,7 @@ export const pack = (notification: any) => new Promise<any>(async (resolve, reje
let _notification: any;
// Populate the notification if 'notification' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(notification)) {
if (isObjectId(notification)) {
_notification = await Notification.findOne({
_id: notification
});

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const PollVote = db.get<IPollVote>('pollVotes');
export default PollVote;
@ -19,7 +20,7 @@ export async function deletePollVote(pollVote: string | mongo.ObjectID | IPollVo
let p: IPollVote;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(pollVote)) {
if (isObjectId(pollVote)) {
p = await PollVote.findOne({
_id: pollVote
});

View File

@ -1,5 +1,6 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const SwSubscription = db.get<ISwSubscription>('swSubscriptions');
export default SwSubscription;
@ -19,7 +20,7 @@ export async function deleteSwSubscription(swSubscription: string | mongo.Object
let s: ISwSubscription;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(swSubscription)) {
if (isObjectId(swSubscription)) {
s = await SwSubscription.findOne({
_id: swSubscription
});

View File

@ -1,6 +1,7 @@
import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
const UserList = db.get<IUserList>('userList');
export default UserList;
@ -20,7 +21,7 @@ export async function deleteUserList(userList: string | mongo.ObjectID | IUserLi
let u: IUserList;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(userList)) {
if (isObjectId(userList)) {
u = await UserList.findOne({
_id: userList
});
@ -45,7 +46,7 @@ export const pack = (
) => new Promise<any>(async (resolve, reject) => {
let _userList: any;
if (mongo.ObjectID.prototype.isPrototypeOf(userList)) {
if (isObjectId(userList)) {
_userList = await UserList.findOne({
_id: userList
});

View File

@ -3,6 +3,7 @@ const deepcopy = require('deepcopy');
const sequential = require('promise-sequential');
import rap from '@prezzemolo/rap';
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import Note, { packMany as packNoteMany, deleteNote } from './note';
import Following, { deleteFollowing } from './following';
import Mute, { deleteMute } from './mute';
@ -175,7 +176,7 @@ export async function deleteUser(user: string | mongo.ObjectID | IUser) {
let u: IUser;
// Populate
if (mongo.ObjectID.prototype.isPrototypeOf(user)) {
if (isObjectId(user)) {
u = await User.findOne({
_id: user
});
@ -340,7 +341,6 @@ export const pack = (
includeHasUnreadNotes?: boolean
}
) => new Promise<any>(async (resolve, reject) => {
const opts = Object.assign({
detail: false,
includeSecrets: false
@ -358,7 +358,7 @@ export const pack = (
};
// Populate the user if 'user' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(user)) {
if (isObjectId(user)) {
_user = await User.findOne({
_id: user
}, { fields });
@ -378,7 +378,7 @@ export const pack = (
// Me
const meId: mongo.ObjectID = me
? mongo.ObjectID.prototype.isPrototypeOf(me)
? isObjectId(me)
? me as mongo.ObjectID
: typeof me === 'string'
? new mongo.ObjectID(me)

View File

@ -1,4 +1,5 @@
import * as mongo from 'mongodb';
import isObjectId from '../../../misc/is-objectid';
import Message from '../../../models/messaging-message';
import { IMessagingMessage as IMessage } from '../../../models/messaging-message';
import { publishMainStream } from '../../../stream';
@ -15,21 +16,21 @@ export default (
message: string | string[] | IMessage | IMessage[] | mongo.ObjectID | mongo.ObjectID[]
) => new Promise<any>(async (resolve, reject) => {
const userId = mongo.ObjectID.prototype.isPrototypeOf(user)
const userId = isObjectId(user)
? user
: new mongo.ObjectID(user);
const otherpartyId = mongo.ObjectID.prototype.isPrototypeOf(otherparty)
const otherpartyId = isObjectId(otherparty)
? otherparty
: new mongo.ObjectID(otherparty);
const ids: mongo.ObjectID[] = Array.isArray(message)
? mongo.ObjectID.prototype.isPrototypeOf(message[0])
? isObjectId(message[0])
? (message as mongo.ObjectID[])
: typeof message[0] === 'string'
? (message as string[]).map(m => new mongo.ObjectID(m))
: (message as IMessage[]).map(m => m._id)
: mongo.ObjectID.prototype.isPrototypeOf(message)
: isObjectId(message)
? [(message as mongo.ObjectID)]
: typeof message === 'string'
? [new mongo.ObjectID(message)]

View File

@ -1,4 +1,5 @@
import * as mongo from 'mongodb';
import isObjectId from '../../../misc/is-objectid';
import { default as Notification, INotification } from '../../../models/notification';
import { publishMainStream } from '../../../stream';
import Mute from '../../../models/mute';
@ -12,17 +13,17 @@ export default (
message: string | string[] | INotification | INotification[] | mongo.ObjectID | mongo.ObjectID[]
) => new Promise<any>(async (resolve, reject) => {
const userId = mongo.ObjectID.prototype.isPrototypeOf(user)
const userId = isObjectId(user)
? user
: new mongo.ObjectID(user);
const ids: mongo.ObjectID[] = Array.isArray(message)
? mongo.ObjectID.prototype.isPrototypeOf(message[0])
? isObjectId(message[0])
? (message as mongo.ObjectID[])
: typeof message[0] === 'string'
? (message as string[]).map(m => new mongo.ObjectID(m))
: (message as INotification[]).map(m => m._id)
: mongo.ObjectID.prototype.isPrototypeOf(message)
: isObjectId(message)
? [(message as mongo.ObjectID)]
: typeof message === 'string'
? [new mongo.ObjectID(message)]

View File

@ -1,4 +1,5 @@
import * as mongo from 'mongodb';
import isObjectId from '../../misc/is-objectid';
import { publishMainStream } from '../../stream';
import User from '../../models/user';
import NoteUnread from '../../models/note-unread';
@ -11,11 +12,11 @@ export default (
note: string | mongo.ObjectID
) => new Promise<any>(async (resolve, reject) => {
const userId: mongo.ObjectID = mongo.ObjectID.prototype.isPrototypeOf(user)
const userId: mongo.ObjectID = isObjectId(user)
? user as mongo.ObjectID
: new mongo.ObjectID(user);
const noteId: mongo.ObjectID = mongo.ObjectID.prototype.isPrototypeOf(note)
const noteId: mongo.ObjectID = isObjectId(note)
? note as mongo.ObjectID
: new mongo.ObjectID(note);