Skip to content
Snippets Groups Projects
Unverified Commit 4dfb486c authored by Rodrigo Nascimento's avatar Rodrigo Nascimento
Browse files

Isolate db methods from ModelsBase

parent cd5b98b5
No related branches found
No related tags found
No related merge requests found
Showing
with 210 additions and 135 deletions
if UploadFS?
RocketChat.models.Uploads.model.allow
RocketChat.models.Uploads.allow
insert: (userId, doc) ->
return userId
......
RocketChat.models.Permissions = new class extends RocketChat.models._Base
constructor: ->
@_initModel 'permissions'
super('permissions')
# FIND
findByRole: (role, options) ->
......
RocketChat.models.Roles = new class extends RocketChat.models._Base
constructor: ->
@_initModel 'roles'
super('roles')
@tryEnsureIndex { 'name': 1 }
@tryEnsureIndex { 'scope': 1 }
......
class EmojiCustom extends RocketChat.models._Base {
constructor() {
super();
this._initModel('custom_emoji');
super('custom_emoji');
this.tryEnsureIndex({ 'name': 1 });
this.tryEnsureIndex({ 'aliases': 1 });
......
Importer.Imports = new class Importer.Imports extends RocketChat.models._Base
constructor: ->
@_initModel 'import'
super('import')
Importer.RawImports = new class Importer.RawImports extends RocketChat.models._Base
constructor: ->
@_initModel 'raw_imports'
super('raw_imports')
RocketChat.models.Integrations = new class extends RocketChat.models._Base
constructor: ->
@_initModel 'integrations'
super('integrations')
# FIND
......
RocketChat.models.Messages = new class extends RocketChat.models._Base
constructor: ->
@_initModel 'message'
super('message')
@tryEnsureIndex { 'rid': 1, 'ts': 1 }
@tryEnsureIndex { 'ts': 1 }
......
RocketChat.models.Reports = new class extends RocketChat.models._Base
constructor: ->
@_initModel 'reports'
super('reports')
# INSERT
......
RocketChat.models.Rooms = new class extends RocketChat.models._Base
constructor: ->
@_initModel 'room'
super('room')
@tryEnsureIndex { 'name': 1 }, { unique: 1, sparse: 1 }
@tryEnsureIndex { 'default': 1 }
......
RocketChat.models.Settings = new class extends RocketChat.models._Base
constructor: ->
@_initModel 'settings'
super('settings')
@tryEnsureIndex { 'blocked': 1 }, { sparse: 1 }
@tryEnsureIndex { 'hidden': 1 }, { sparse: 1 }
......
RocketChat.models.Subscriptions = new class extends RocketChat.models._Base
constructor: ->
@_initModel 'subscription'
super('subscription')
@tryEnsureIndex { 'rid': 1, 'u._id': 1 }, { unique: 1 }
@tryEnsureIndex { 'rid': 1, 'alert': 1, 'u._id': 1 }
......
RocketChat.models.Uploads = new class extends RocketChat.models._Base
constructor: ->
@_initModel 'uploads'
super('uploads')
@tryEnsureIndex { 'rid': 1 }
@tryEnsureIndex { 'uploadedAt': 1 }
......
RocketChat.models.Users = new class extends RocketChat.models._Base
constructor: ->
@model = Meteor.users
super(Meteor.users)
@tryEnsureIndex { 'roles': 1 }, { sparse: 1 }
@tryEnsureIndex { 'name': 1 }
......
const baseName = 'rocketchat_';
const trash = new Mongo.Collection(baseName + '_trash');
try {
trash._ensureIndex({ collection: 1 });
trash._ensureIndex({ _deletedAt: 1 }, { expireAfterSeconds: 60 * 60 * 24 * 30 });
} catch (e) {
console.log(e);
}
import ModelsBaseDb from './_BaseDb';
class ModelsBase {
_baseName() {
return baseName;
}
_initModel(name) {
check(name, String);
this.name = name;
this.model = new Mongo.Collection(this._baseName() + name);
this.tryEnsureIndex({ '_updatedAt': 1 });
constructor() {
this.db = new ModelsBaseDb(...arguments);
this.model = this.db.model;
this.collectionName = this.db.collectionName;
this.name = this.db.name;
}
setUpdatedAt(record = {}, checkQuery = false, query) {
if (checkQuery === true) {
if (!query || Object.keys(query).length === 0) {
throw new Meteor.Error('Models._Base: Empty query');
}
}
if (/(^|,)\$/.test(Object.keys(record).join(','))) {
record.$set = record.$set || {};
record.$set._updatedAt = new Date;
} else {
record._updatedAt = new Date;
}
return record;
setUpdatedAt(/*record, checkQuery, query*/) {
return this.db.setUpdatedAt(...arguments);
}
find() {
return this.model.find(...arguments);
return this.db.find(...arguments);
}
findOne() {
return this.model.findOne(...arguments);
return this.db.findOne(...arguments);
}
insert(record) {
this.setUpdatedAt(record);
const result = this.model.insert(...arguments);
record._id = result;
return result;
insert(/*record*/) {
return this.db.insert(...arguments);
}
update(query, update, options = {}) {
this.setUpdatedAt(update, true, query);
if (options.upsert) {
return this.upsert(query, update);
}
return this.model.update(query, update, options);
update(/*query, update, options*/) {
return this.db.update(...arguments);
}
upsert(query, update) {
this.setUpdatedAt(update, true, query);
return this.model.upsert(...arguments);
upsert(/*query, update*/) {
return this.db.upsert(...arguments);
}
remove(query) {
const records = this.model.find(query).fetch();
const ids = [];
for (const record of records) {
ids.push(record._id);
record._deletedAt = new Date;
record.__collection__ = this.name;
trash.upsert({_id: record._id}, _.omit(record, '_id'));
}
query = { _id: { $in: ids } };
return this.model.remove(query);
remove(/*query*/) {
return this.db.remove(...arguments);
}
insertOrUpsert(...args) {
if (args[0] && args[0]._id) {
const _id = args[0]._id;
delete args[0]._id;
args.unshift({
_id: _id
});
this.upsert(...args);
return _id;
} else {
return this.insert(...args);
}
insertOrUpsert() {
return this.db.insertOrUpsert(...arguments);
}
allow() {
return this.model.allow(...arguments);
return this.db.allow(...arguments);
}
deny() {
return this.model.deny(...arguments);
return this.db.deny(...arguments);
}
ensureIndex() {
return this.model._ensureIndex(...arguments);
return this.db.ensureIndex(...arguments);
}
dropIndex() {
return this.model._dropIndex(...arguments);
return this.db.dropIndex(...arguments);
}
tryEnsureIndex() {
try {
return this.ensureIndex(...arguments);
} catch (e) {
console.log(e);
}
return this.db.tryEnsureIndex(...arguments);
}
tryDropIndex() {
try {
return this.dropIndex(...arguments);
} catch (e) {
console.log(e);
}
return this.db.tryDropIndex(...arguments);
}
trashFind(query, options) {
query.__collection__ = this.name;
return trash.find(query, options);
trashFind(/*query, options*/) {
return this.db.trashFind(...arguments);
}
trashFindDeletedAfter(deletedAt, query = {}, options) {
query.__collection__ = this.name;
query._deletedAt = {
$gt: deletedAt
};
return trash.find(query, options);
trashFindDeletedAfter(/*deletedAt, query, options*/) {
return this.db.trashFindDeletedAfter(...arguments);
}
// dinamicTrashFindAfter(method, deletedAt, ...args) {
......
const baseName = 'rocketchat_';
const trash = new Mongo.Collection(baseName + '_trash');
try {
trash._ensureIndex({ collection: 1 });
trash._ensureIndex({ _deletedAt: 1 }, { expireAfterSeconds: 60 * 60 * 24 * 30 });
} catch (e) {
console.log(e);
}
class ModelsBaseDb {
constructor(model) {
if (Match.test(model, String)) {
this.name = model;
this.collectionName = this.baseName + this.name;
this.model = new Mongo.Collection(this.collectionName);
} else {
this.name = model._name;
this.collectionName = this.name;
this.model = model;
}
this.tryEnsureIndex({ '_updatedAt': 1 });
}
get baseName() {
return baseName;
}
setUpdatedAt(record = {}, checkQuery = false, query) {
if (checkQuery === true) {
if (!query || Object.keys(query).length === 0) {
throw new Meteor.Error('Models._Base: Empty query');
}
}
if (/(^|,)\$/.test(Object.keys(record).join(','))) {
record.$set = record.$set || {};
record.$set._updatedAt = new Date;
} else {
record._updatedAt = new Date;
}
return record;
}
find() {
return this.model.find(...arguments);
}
findOne() {
return this.model.findOne(...arguments);
}
insert(record) {
this.setUpdatedAt(record);
const result = this.model.insert(...arguments);
record._id = result;
return result;
}
update(query, update, options = {}) {
this.setUpdatedAt(update, true, query);
if (options.upsert) {
return this.upsert(query, update);
}
return this.model.update(query, update, options);
}
upsert(query, update) {
this.setUpdatedAt(update, true, query);
return this.model.upsert(...arguments);
}
remove(query) {
const records = this.model.find(query).fetch();
const ids = [];
for (const record of records) {
ids.push(record._id);
record._deletedAt = new Date;
record.__collection__ = this.name;
trash.upsert({_id: record._id}, _.omit(record, '_id'));
}
query = { _id: { $in: ids } };
return this.model.remove(query);
}
insertOrUpsert(...args) {
if (args[0] && args[0]._id) {
const _id = args[0]._id;
delete args[0]._id;
args.unshift({
_id: _id
});
this.upsert(...args);
return _id;
} else {
return this.insert(...args);
}
}
allow() {
return this.model.allow(...arguments);
}
deny() {
return this.model.deny(...arguments);
}
ensureIndex() {
return this.model._ensureIndex(...arguments);
}
dropIndex() {
return this.model._dropIndex(...arguments);
}
tryEnsureIndex() {
try {
return this.ensureIndex(...arguments);
} catch (e) {
console.log(e);
}
}
tryDropIndex() {
try {
return this.dropIndex(...arguments);
} catch (e) {
console.log(e);
}
}
trashFind(query, options) {
query.__collection__ = this.name;
return trash.find(query, options);
}
trashFindDeletedAfter(deletedAt, query = {}, options) {
query.__collection__ = this.name;
query._deletedAt = {
$gt: deletedAt
};
return trash.find(query, options);
}
}
export default ModelsBaseDb;
......@@ -2,9 +2,8 @@
/* eslint new-cap: 0 */
import loki from 'lokijs';
const {EventEmitter} = Npm.require('events');
const objectPath = Npm.require('object-path');
import {EventEmitter} from 'events';
import objectPath from 'object-path';
const ignore = [
'emit',
......@@ -409,7 +408,7 @@ RocketChat.cache._Base = (class CacheBase extends EventEmitter {
startOplog() {
const query = {
collection: this.model.model._name
collection: this.model.db.collectionName
};
MongoInternals.defaultRemoteCollectionDriver().mongo._oplogHandle.onOplogEntry(query, (record) => {
......
......@@ -3,8 +3,7 @@
*/
class LivechatCustomField extends RocketChat.models._Base {
constructor() {
super();
this._initModel('livechat_custom_field');
super('livechat_custom_field');
}
// FIND
......
......@@ -3,8 +3,7 @@
*/
class LivechatDepartment extends RocketChat.models._Base {
constructor() {
super();
this._initModel('livechat_department');
super('livechat_department');
}
// FIND
......
......@@ -3,8 +3,7 @@
*/
class LivechatDepartmentAgents extends RocketChat.models._Base {
constructor() {
super();
this._initModel('livechat_department_agents');
super('livechat_department_agents');
}
findByDepartmentId(departmentId) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment