From 0cc5ca598a6ca5b3ac105419e905cb89df854698 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 28 Mar 2018 09:10:05 +0900 Subject: [PATCH] wip --- src/api/models/access-token.ts | 18 +++++-- src/api/models/app.ts | 20 ++++--- src/api/models/appdata.ts | 3 -- src/api/models/auth-session.ts | 9 ++-- src/api/models/channel-watching.ts | 13 ++++- src/api/models/channel.ts | 11 ++-- src/api/models/drive-file.ts | 14 ++--- src/api/models/drive-folder.ts | 14 ++--- src/api/models/drive-tag.ts | 3 -- src/api/models/favorite.ts | 11 +++- src/api/models/following.ts | 12 ++++- tools/migration/shell.camel-case.js | 81 +++++++++++++++++++++++++++++ 12 files changed, 166 insertions(+), 43 deletions(-) delete mode 100644 src/api/models/appdata.ts delete mode 100644 src/api/models/drive-tag.ts create mode 100644 tools/migration/shell.camel-case.js diff --git a/src/api/models/access-token.ts b/src/api/models/access-token.ts index 9985be501..9e1cb6474 100644 --- a/src/api/models/access-token.ts +++ b/src/api/models/access-token.ts @@ -1,8 +1,16 @@ +import * as mongo from 'mongodb'; import db from '../../db/mongodb'; -const collection = db.get('access_tokens'); +const AccessToken = db.get('accessTokens'); +AccessToken.createIndex('token'); +AccessToken.createIndex('hash'); +export default AccessToken; -(collection as any).createIndex('token'); // fuck type definition -(collection as any).createIndex('hash'); // fuck type definition - -export default collection as any; // fuck type definition +export type IAccessTokens = { + _id: mongo.ObjectID; + createdAt: Date; + appId: mongo.ObjectID; + userId: mongo.ObjectID; + token: string; + hash: string; +}; diff --git a/src/api/models/app.ts b/src/api/models/app.ts index 34e9867db..20af049b2 100644 --- a/src/api/models/app.ts +++ b/src/api/models/app.ts @@ -5,16 +5,22 @@ import db from '../../db/mongodb'; import config from '../../conf'; const App = db.get('apps'); -App.createIndex('name_id'); -App.createIndex('name_id_lower'); +App.createIndex('nameId'); +App.createIndex('nameIdLower'); App.createIndex('secret'); export default App; export type IApp = { _id: mongo.ObjectID; - created_at: Date; - user_id: mongo.ObjectID; + createdAt: Date; + userId: mongo.ObjectID; secret: string; + name: string; + nameId: string; + nameIdLower: string; + description: string; + permission: string; + callbackUrl: string; }; export function isValidNameId(nameId: string): boolean { @@ -70,7 +76,7 @@ export const pack = ( _app.id = _app._id; delete _app._id; - delete _app.name_id_lower; + delete _app.nameIdLower; // Visible by only owner if (!opts.includeSecret) { @@ -84,8 +90,8 @@ export const pack = ( if (me) { // 既に連携しているか const exist = await AccessToken.count({ - app_id: _app.id, - user_id: me, + appId: _app.id, + userId: me, }, { limit: 1 }); diff --git a/src/api/models/appdata.ts b/src/api/models/appdata.ts deleted file mode 100644 index 3e68354fa..000000000 --- a/src/api/models/appdata.ts +++ /dev/null @@ -1,3 +0,0 @@ -import db from '../../db/mongodb'; - -export default db.get('appdata') as any; // fuck type definition diff --git a/src/api/models/auth-session.ts b/src/api/models/auth-session.ts index 997ec61c2..e6b8b2318 100644 --- a/src/api/models/auth-session.ts +++ b/src/api/models/auth-session.ts @@ -3,11 +3,15 @@ import deepcopy = require('deepcopy'); import db from '../../db/mongodb'; import { pack as packApp } from './app'; -const AuthSession = db.get('auth_sessions'); +const AuthSession = db.get('authSessions'); export default AuthSession; export interface IAuthSession { _id: mongo.ObjectID; + createdAt: Date; + appId: mongo.ObjectID; + userId: mongo.ObjectID; + token: string; } /** @@ -24,7 +28,6 @@ export const pack = ( let _session: any; // TODO: Populate session if it ID - _session = deepcopy(session); // Me @@ -39,7 +42,7 @@ export const pack = ( delete _session._id; // Populate app - _session.app = await packApp(_session.app_id, me); + _session.app = await packApp(_session.appId, me); resolve(_session); }); diff --git a/src/api/models/channel-watching.ts b/src/api/models/channel-watching.ts index 6184ae408..23886d0c7 100644 --- a/src/api/models/channel-watching.ts +++ b/src/api/models/channel-watching.ts @@ -1,3 +1,14 @@ +import * as mongo from 'mongodb'; + import db from '../../db/mongodb'; -export default db.get('channel_watching') as any; // fuck type definition +const ChannelWatching = db.get('channelWatching'); +export default ChannelWatching; + +export interface IChannelWatching { + _id: mongo.ObjectID; + createdAt: Date; + deletedAt: Date; + channel_id: mongo.ObjectID; + userId: mongo.ObjectID; +} diff --git a/src/api/models/channel.ts b/src/api/models/channel.ts index 815d53593..a753a4eba 100644 --- a/src/api/models/channel.ts +++ b/src/api/models/channel.ts @@ -9,10 +9,11 @@ export default Channel; export type IChannel = { _id: mongo.ObjectID; - created_at: Date; + createdAt: Date; title: string; - user_id: mongo.ObjectID; + userId: mongo.ObjectID; index: number; + watchingCount: number; }; /** @@ -47,7 +48,7 @@ export const pack = ( delete _channel._id; // Remove needless properties - delete _channel.user_id; + delete _channel.userId; // Me const meId: mongo.ObjectID = me @@ -61,9 +62,9 @@ export const pack = ( if (me) { //#region Watchしているかどうか const watch = await Watching.findOne({ - user_id: meId, + userId: meId, channel_id: _channel.id, - deleted_at: { $exists: false } + deletedAt: { $exists: false } }); _channel.is_watching = watch !== null; diff --git a/src/api/models/drive-file.ts b/src/api/models/drive-file.ts index 2a46d8dc4..b0e4d1db0 100644 --- a/src/api/models/drive-file.ts +++ b/src/api/models/drive-file.ts @@ -4,14 +4,14 @@ import { pack as packFolder } from './drive-folder'; import config from '../../conf'; import monkDb, { nativeDbConn } from '../../db/mongodb'; -const DriveFile = monkDb.get('drive_files.files'); +const DriveFile = monkDb.get('driveFiles.files'); export default DriveFile; const getGridFSBucket = async (): Promise => { const db = await nativeDbConn(); const bucket = new mongodb.GridFSBucket(db, { - bucketName: 'drive_files' + bucketName: 'driveFiles' }); return bucket; }; @@ -26,8 +26,8 @@ export type IDriveFile = { contentType: string; metadata: { properties: any; - user_id: mongodb.ObjectID; - folder_id: mongodb.ObjectID; + userId: mongodb.ObjectID; + folderId: mongodb.ObjectID; } }; @@ -79,7 +79,7 @@ export const pack = ( let _target: any = {}; _target.id = _file._id; - _target.created_at = _file.uploadDate; + _target.createdAt = _file.uploadDate; _target.name = _file.filename; _target.type = _file.contentType; _target.datasize = _file.length; @@ -92,9 +92,9 @@ export const pack = ( if (_target.properties == null) _target.properties = {}; if (opts.detail) { - if (_target.folder_id) { + if (_target.folderId) { // Populate folder - _target.folder = await packFolder(_target.folder_id, { + _target.folder = await packFolder(_target.folderId, { detail: true }); } diff --git a/src/api/models/drive-folder.ts b/src/api/models/drive-folder.ts index 54b45049b..52f784e06 100644 --- a/src/api/models/drive-folder.ts +++ b/src/api/models/drive-folder.ts @@ -8,10 +8,10 @@ export default DriveFolder; export type IDriveFolder = { _id: mongo.ObjectID; - created_at: Date; + createdAt: Date; name: string; - user_id: mongo.ObjectID; - parent_id: mongo.ObjectID; + userId: mongo.ObjectID; + parentId: mongo.ObjectID; }; export function isValidFolderName(name: string): boolean { @@ -55,20 +55,20 @@ export const pack = ( if (opts.detail) { const childFoldersCount = await DriveFolder.count({ - parent_id: _folder.id + parentId: _folder.id }); const childFilesCount = await DriveFile.count({ - 'metadata.folder_id': _folder.id + 'metadata.folderId': _folder.id }); _folder.folders_count = childFoldersCount; _folder.files_count = childFilesCount; } - if (opts.detail && _folder.parent_id) { + if (opts.detail && _folder.parentId) { // Populate parent folder - _folder.parent = await pack(_folder.parent_id, { + _folder.parent = await pack(_folder.parentId, { detail: true }); } diff --git a/src/api/models/drive-tag.ts b/src/api/models/drive-tag.ts deleted file mode 100644 index 991c935e8..000000000 --- a/src/api/models/drive-tag.ts +++ /dev/null @@ -1,3 +0,0 @@ -import db from '../../db/mongodb'; - -export default db.get('drive_tags') as any; // fuck type definition diff --git a/src/api/models/favorite.ts b/src/api/models/favorite.ts index e01d9e343..5ba55c4c9 100644 --- a/src/api/models/favorite.ts +++ b/src/api/models/favorite.ts @@ -1,3 +1,12 @@ +import * as mongo from 'mongodb'; import db from '../../db/mongodb'; -export default db.get('favorites') as any; // fuck type definition +const Favorites = db.get('favorites'); +export default Favorites; + +export type IFavorites = { + _id: mongo.ObjectID; + createdAt: Date; + userId: mongo.ObjectID; + postId: mongo.ObjectID; +}; diff --git a/src/api/models/following.ts b/src/api/models/following.ts index cb3db9b53..1163bf6b3 100644 --- a/src/api/models/following.ts +++ b/src/api/models/following.ts @@ -1,3 +1,13 @@ +import * as mongo from 'mongodb'; import db from '../../db/mongodb'; -export default db.get('following') as any; // fuck type definition +const Following = db.get('following'); +export default Following; + +export type IFollowing = { + _id: mongo.ObjectID; + createdAt: Date; + deletedAt: Date; + followeeId: mongo.ObjectID; + followerId: mongo.ObjectID; +}; diff --git a/tools/migration/shell.camel-case.js b/tools/migration/shell.camel-case.js new file mode 100644 index 000000000..6d6b01a07 --- /dev/null +++ b/tools/migration/shell.camel-case.js @@ -0,0 +1,81 @@ +db.access_tokens.renameCollection('accessTokens'); +db.accessTokens.update({}, { + $rename: { + created_at: 'createdAt', + app_id: 'appId', + user_id: 'userId', + } +}, false, true); + +db.apps.update({}, { + $rename: { + created_at: 'createdAt', + user_id: 'userId', + name_id: 'nameId', + name_id_lower: 'nameIdLower', + callback_url: 'callbackUrl', + } +}, false, true); + +db.auth_sessions.renameCollection('authSessions'); +db.authSessions.update({}, { + $rename: { + created_at: 'createdAt', + app_id: 'appId', + user_id: 'userId', + } +}, false, true); + +db.channel_watching.renameCollection('channelWatching'); +db.channelWatching.update({}, { + $rename: { + created_at: 'createdAt', + deleted_at: 'deletedAt', + channel_id: 'channelId', + user_id: 'userId', + } +}, false, true); + +db.channels.update({}, { + $rename: { + created_at: 'createdAt', + user_id: 'userId', + watching_count: 'watchingCount' + } +}, false, true); + +db.drive_files.files.renameCollection('driveFiles.files'); +db.drive_files.chunks.renameCollection('driveFiles.chunks'); +db.driveFiles.files.update({}, { + $rename: { + 'metadata.user_id': 'metadata.userId', + 'metadata.folder_id': 'metadata.folderId', + 'metadata.properties.average_color': 'metadata.properties.avgColor' + } +}, false, true); + +db.drive_folders.renameCollection('driveFolders'); +db.driveFolders.update({}, { + $rename: { + created_at: 'createdAt', + user_id: 'userId', + parent_id: 'parentId', + } +}, false, true); + +db.favorites.update({}, { + $rename: { + created_at: 'createdAt', + user_id: 'userId', + post_id: 'postId', + } +}, false, true); + +db.following.update({}, { + $rename: { + created_at: 'createdAt', + deleted_at: 'deletedAt', + followee_id: 'followeeId', + follower_id: 'followerId', + } +}, false, true);