Skip to content

Commit

Permalink
Merge pull request #517 from Quintessential-SFT/revert-515-database_i…
Browse files Browse the repository at this point in the history
…nternal_communication

Revert "fix(database): database talking to itself through GRPC #1xgx2et"
  • Loading branch information
kkopanidis authored Jan 10, 2022
2 parents 29a7771 + 91cd741 commit ec4f86e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 65 deletions.
48 changes: 23 additions & 25 deletions modules/database/src/DatabaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class DatabaseProvider extends ConduitServiceModule {
private _admin: AdminHandlers;

constructor(protected readonly grpcSdk: ConduitGrpcSdk) {
super(grpcSdk);
super(grpcSdk)
const dbType = process.env.databaseType ?? 'mongodb';
const databaseUrl = process.env.databaseURL ?? 'mongodb://localhost:27017';

Expand Down Expand Up @@ -78,12 +78,11 @@ export class DatabaseProvider extends ConduitServiceModule {
deleteOne: this.deleteOne.bind(this),
deleteMany: this.deleteMany.bind(this),
countDocuments: this.countDocuments.bind(this),
},
}
);
await this.grpcServer.start();
}


async activate() {
const self = this;
await this.grpcSdk.initializeEventBus();
Expand All @@ -101,13 +100,12 @@ export class DatabaseProvider extends ConduitServiceModule {
receivedSchema.name,
receivedSchema.modelSchema,
receivedSchema.modelOptions,
receivedSchema.collectionName,
receivedSchema.collectionName
);
schema.ownerModule = receivedSchema.ownerModule;
self._activeAdapter
.createSchemaFromAdapter(schema)
.then(() => {
})
.then(() => {})
.catch(() => {
console.log('Failed to create/update schema');
});
Expand All @@ -117,11 +115,11 @@ export class DatabaseProvider extends ConduitServiceModule {
}
});
await this._activeAdapter.createSchemaFromAdapter(
models._DeclaredSchema.getInstance((this.grpcSdk.databaseProvider!)),
models._DeclaredSchema.getInstance((this.grpcSdk.databaseProvider!))
);
await migrateModelOptions();
await this._activeAdapter.recoverSchemasFromDatabase();
this._admin = new AdminHandlers(this.grpcServer, this.grpcSdk, this._activeAdapter);
this._admin = new AdminHandlers(this.grpcServer, this.grpcSdk);
}

/**
Expand All @@ -134,7 +132,7 @@ export class DatabaseProvider extends ConduitServiceModule {
call.request.schema.name,
JSON.parse(call.request.schema.modelSchema),
JSON.parse(call.request.schema.modelOptions),
call.request.schema.collectionName,
call.request.schema.collectionName
);
if (schema.name.indexOf('-') >= 0 || schema.name.indexOf(' ') >= 0) {
return callback({
Expand Down Expand Up @@ -217,7 +215,7 @@ export class DatabaseProvider extends ConduitServiceModule {

async deleteSchema(call: DropCollectionRequest, callback: DropCollectionResponse) {
try {
const schemas = await this._activeAdapter.deleteSchema(
const schemas = await this._activeAdapter.deleteSchema(
call.request.schemaName,
call.request.deleteData,
(call as any).metadata.get('module-name')[0],
Expand All @@ -238,7 +236,7 @@ export class DatabaseProvider extends ConduitServiceModule {
call.request.query,
call.request.select,
call.request.populate,
schemaAdapter.relations,
schemaAdapter.relations
);
callback(null, { result: JSON.stringify(doc) });
} catch (err) {
Expand Down Expand Up @@ -266,7 +264,7 @@ export class DatabaseProvider extends ConduitServiceModule {
select,
sort,
populate,
schemaAdapter.relations,
schemaAdapter.relations
);
callback(null, { result: JSON.stringify(docs) });
} catch (err) {
Expand All @@ -285,7 +283,7 @@ export class DatabaseProvider extends ConduitServiceModule {
if (!await canCreate(moduleName, schemaAdapter.model)) {
return callback({
code: status.PERMISSION_DENIED,
message: `Module ${moduleName} is not authorized to create ${schemaName} entries!`,
message: `Module ${moduleName} is not authorized to create ${schemaName} entries!`
});
}

Expand All @@ -294,7 +292,7 @@ export class DatabaseProvider extends ConduitServiceModule {

this.grpcSdk.bus?.publish(
`${MODULE_NAME}:create:${schemaName}`,
docString,
docString
);

callback(null, { result: docString });
Expand All @@ -314,7 +312,7 @@ export class DatabaseProvider extends ConduitServiceModule {
if (!await canCreate(moduleName, schemaAdapter.model)) {
return callback({
code: status.PERMISSION_DENIED,
message: `Module ${moduleName} is not authorized to create ${schemaName} entries!`,
message: `Module ${moduleName} is not authorized to create ${schemaName} entries!`
});
}

Expand All @@ -323,7 +321,7 @@ export class DatabaseProvider extends ConduitServiceModule {

this.grpcSdk.bus?.publish(
`${MODULE_NAME}:createMany:${schemaName}`,
docsString,
docsString
);

callback(null, { result: docsString });
Expand All @@ -343,7 +341,7 @@ export class DatabaseProvider extends ConduitServiceModule {
if (!await canModify(moduleName, schemaAdapter.model)) {
return callback({
code: status.PERMISSION_DENIED,
message: `Module ${moduleName} is not authorized to modify ${schemaName} entries!`,
message: `Module ${moduleName} is not authorized to modify ${schemaName} entries!`
});
}

Expand All @@ -358,7 +356,7 @@ export class DatabaseProvider extends ConduitServiceModule {

this.grpcSdk.bus?.publish(
`${MODULE_NAME}:update:${schemaName}`,
resultString,
resultString
);

callback(null, { result: resultString });
Expand All @@ -378,20 +376,20 @@ export class DatabaseProvider extends ConduitServiceModule {
if (!await canModify(moduleName, schemaAdapter.model)) {
return callback({
code: status.PERMISSION_DENIED,
message: `Module ${moduleName} is not authorized to modify ${schemaName} entries!`,
message: `Module ${moduleName} is not authorized to modify ${schemaName} entries!`
});
}

const result = await schemaAdapter.model.updateMany(
call.request.filterQuery,
call.request.query,
call.request.updateProvidedOnly,
call.request.updateProvidedOnly
);
const resultString = JSON.stringify(result);

this.grpcSdk.bus?.publish(
`${MODULE_NAME}:updateMany:${schemaName}`,
resultString,
resultString
);

callback(null, { result: resultString });
Expand All @@ -411,7 +409,7 @@ export class DatabaseProvider extends ConduitServiceModule {
if (!await canDelete(moduleName, schemaAdapter.model)) {
return callback({
code: status.PERMISSION_DENIED,
message: `Module ${moduleName} is not authorized to delete ${schemaName} entries!`,
message: `Module ${moduleName} is not authorized to delete ${schemaName} entries!`
});
}

Expand All @@ -420,7 +418,7 @@ export class DatabaseProvider extends ConduitServiceModule {

this.grpcSdk.bus?.publish(
`${MODULE_NAME}:delete:${schemaName}`,
resultString,
resultString
);

callback(null, { result: resultString });
Expand All @@ -440,7 +438,7 @@ export class DatabaseProvider extends ConduitServiceModule {
if (!await canDelete(moduleName, schemaAdapter.model)) {
return callback({
code: status.PERMISSION_DENIED,
message: `Module ${moduleName} is not authorized to delete ${schemaName} entries!`,
message: `Module ${moduleName} is not authorized to delete ${schemaName} entries!`
});
}

Expand All @@ -449,7 +447,7 @@ export class DatabaseProvider extends ConduitServiceModule {

this.grpcSdk.bus?.publish(
`${MODULE_NAME}:delete:${schemaName}`,
resultString,
resultString
);

callback(null, { result: resultString });
Expand Down
17 changes: 8 additions & 9 deletions modules/database/src/adapters/DatabaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ export abstract class DatabaseAdapter<T extends SchemaAdapter<any>> {
abstract async deleteSchema(schemaName: string, deleteData: boolean, callerModule: string): Promise<string>;

abstract getSchemaModel(
schemaName: string,
schemaName: string
): { model: SchemaAdapter<any>; relations: any };

async checkModelOwnership(schema: ConduitSchema) {
if (schema.name === '_DeclaredSchema') return true;

const model = await this.models!['_DeclaredSchema'].model.findOne({ name: schema.name });
const model = await _DeclaredSchema.getInstance().findOne({ name: schema.name });
if (model && ((model.ownerModule === schema.ownerModule) || (model.ownerModule === 'unknown'))) {
return true;
} else if (model) {
Expand All @@ -41,20 +40,20 @@ export abstract class DatabaseAdapter<T extends SchemaAdapter<any>> {
protected async saveSchemaToDatabase(schema: ConduitSchema) {
if (schema.name === '_DeclaredSchema') return;

const model = await this.models!['_DeclaredSchema'].findMany('{}');
const model = await _DeclaredSchema.getInstance().findOne({ name: schema.name });
if (model) {
await this.models!['_DeclaredSchema'].model
await _DeclaredSchema.getInstance()
.findByIdAndUpdate(
model._id,
{
name: schema.name,
fields: schema.fields,
modelOptions: schema.schemaOptions,
ownerModule: schema.ownerModule,
},
}
);
} else {
await this.models!['_DeclaredSchema'].model
await _DeclaredSchema.getInstance()
.create({
name: schema.name,
fields: schema.fields,
Expand All @@ -65,13 +64,13 @@ export abstract class DatabaseAdapter<T extends SchemaAdapter<any>> {
}

async recoverSchemasFromDatabase(): Promise<any> {
let models: any = await this.models!['_DeclaredSchema'].findMany('{}');
let models: any = await _DeclaredSchema.getInstance().findMany({});
models = models
.map((model: any) => {
let schema = new ConduitSchema(
model.name,
model.fields,
model.modelOptions,
model.modelOptions
);
schema.ownerModule = model.ownerModule;
return schema;
Expand Down
22 changes: 8 additions & 14 deletions modules/database/src/adapters/mongoose-adapter/MongooseSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { SchemaAdapter } from '../../interfaces';
import { MongooseAdapter } from './index';
import { ConduitSchema } from '@quintessential-sft/conduit-grpc-sdk';
import { createWithPopulations } from './utils';

const EJSON = require('mongodb-extended-json');

export class MongooseSchema implements SchemaAdapter<Model<any>> {
Expand All @@ -15,7 +14,7 @@ export class MongooseSchema implements SchemaAdapter<Model<any>> {
schema: ConduitSchema,
originalSchema: any,
deepPopulate: any,
private readonly adapter: MongooseAdapter,
private readonly adapter: MongooseAdapter
) {
this.originalSchema = originalSchema;
let mongooseSchema = new Schema(schema.modelSchema as any, schema.schemaOptions);
Expand Down Expand Up @@ -47,7 +46,7 @@ export class MongooseSchema implements SchemaAdapter<Model<any>> {
id: string,
query: string,
updateProvidedOnly: boolean = false,
populate?: string[],
populate?: string[]
): Promise<any> {
let parsedQuery: any = EJSON.parse(query);
parsedQuery['updatedAt'] = new Date();
Expand All @@ -57,7 +56,7 @@ export class MongooseSchema implements SchemaAdapter<Model<any>> {
$set: parsedQuery,
};
}
let finalQuery = this.model.findByIdAndUpdate(id, parsedQuery, { new: true });
let finalQuery = this.model.findByIdAndUpdate(id, parsedQuery, { new: true })
if (populate !== undefined && populate !== null) {
finalQuery = this.calculatePopulates(finalQuery, populate);
}
Expand All @@ -67,7 +66,7 @@ export class MongooseSchema implements SchemaAdapter<Model<any>> {
async updateMany(
filterQuery: string,
query: string,
updateProvidedOnly: boolean = false,
updateProvidedOnly: boolean = false
): Promise<any> {
let parsedFilter: any = EJSON.parse(filterQuery);
let parsedQuery: any = EJSON.parse(query);
Expand Down Expand Up @@ -100,7 +99,7 @@ export class MongooseSchema implements SchemaAdapter<Model<any>> {
if (this.originalSchema.modelSchema[r[0]]) {
controlBool = false;
} else if (r[0] === undefined || r[0].length === 0 || r[0] === '') {
throw new Error('Failed populating \'' + final + '\'');
throw new Error("Failed populating '" + final + "'");
} else {
r.splice(0, 1);
}
Expand All @@ -115,19 +114,14 @@ export class MongooseSchema implements SchemaAdapter<Model<any>> {
}

findMany(
query: string | any,
query: string,
skip?: number,
limit?: number,
select?: string,
sort?: string,
populate?: string[],
populate?: string[]
): Promise<any> {
let parsedQuery;
if (typeof query === 'string'){
parsedQuery = EJSON.parse(query);
} else {
parsedQuery = query;
}
let parsedQuery: any = EJSON.parse(query);
let finalQuery = this.model.find(this.parseQuery(parsedQuery), select);
if (skip !== null) {
finalQuery = finalQuery.skip(skip!);
Expand Down
4 changes: 2 additions & 2 deletions modules/database/src/adapters/mongoose-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ export class MongooseAdapter extends DatabaseAdapter<MongooseSchema> {
.drop()
.catch((e: Error) => { throw new GrpcError(status.INTERNAL, e.message); });
}
this.models!['_DeclaredSchema'].model
_DeclaredSchema.getInstance()
.findOne({ name: schemaName })
.then( model => {
if (model) {
this.models!['_DeclaredSchema'].model
_DeclaredSchema.getInstance()
.deleteOne({name: schemaName})
.catch((e: Error) => { throw new GrpcError(status.INTERNAL, e.message); })
}
Expand Down
Loading

0 comments on commit ec4f86e

Please sign in to comment.