Skip to content

Commit

Permalink
fix(database): schema extensions (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkopanidis committed Nov 16, 2022
1 parent 9cce293 commit 97340a5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
18 changes: 12 additions & 6 deletions modules/database/src/adapters/mongoose-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import pluralize from '../../utils/pluralize';
import { mongoSchemaConverter } from '../../introspection/mongoose/utils';
import { status } from '@grpc/grpc-js';
import { checkIfMongoOptions } from './utils';
import { ConduitDatabaseSchema } from '../../interfaces';

const parseSchema = require('mongodb-schema');
let deepPopulate = require('mongoose-deep-populate');
Expand Down Expand Up @@ -207,14 +208,19 @@ export class MongooseAdapter extends DatabaseAdapter<MongooseSchema> {
protected async _createSchemaFromAdapter(
schema: ConduitSchema,
): Promise<MongooseSchema> {
if (this.registeredSchemas.has(schema.name)) {
if (schema.name !== 'Config') {
schema = validateSchema(this.registeredSchemas.get(schema.name)!, schema);
let compiledSchema = JSON.parse(JSON.stringify(schema));
(compiledSchema as any).fields = (schema as ConduitDatabaseSchema).compiledFields;
if (this.registeredSchemas.has(compiledSchema.name)) {
if (compiledSchema.name !== 'Config') {
compiledSchema = validateSchema(
this.registeredSchemas.get(compiledSchema.name)!,
compiledSchema,
);
}
this.mongoose.connection.deleteModel(schema.name);
this.mongoose.connection.deleteModel(compiledSchema.name);
}

const newSchema = schemaConverter(schema);
const newSchema = schemaConverter(compiledSchema);
const indexes = newSchema.modelOptions.indexes;
delete newSchema.modelOptions.indexes;
this.registeredSchemas.set(
Expand All @@ -224,7 +230,7 @@ export class MongooseAdapter extends DatabaseAdapter<MongooseSchema> {
this.models[schema.name] = new MongooseSchema(
this.mongoose,
newSchema,
schema,
compiledSchema,
deepPopulate,
this,
);
Expand Down
18 changes: 12 additions & 6 deletions modules/database/src/adapters/sequelize-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { status } from '@grpc/grpc-js';
import { SequelizeAuto } from 'sequelize-auto';
import { isNil } from 'lodash';
import { checkIfPostgresOptions } from './utils';
import { ConduitDatabaseSchema } from '../../interfaces';

const sqlSchemaName = process.env.SQL_SCHEMA ?? 'public';

Expand Down Expand Up @@ -205,22 +206,27 @@ export class SequelizeAdapter extends DatabaseAdapter<SequelizeSchema> {
protected async _createSchemaFromAdapter(
schema: ConduitSchema,
): Promise<SequelizeSchema> {
if (this.registeredSchemas.has(schema.name)) {
if (schema.name !== 'Config') {
schema = validateSchema(this.registeredSchemas.get(schema.name)!, schema);
let compiledSchema = JSON.parse(JSON.stringify(schema));
(compiledSchema as any).fields = (schema as ConduitDatabaseSchema).compiledFields;
if (this.registeredSchemas.has(compiledSchema.name)) {
if (compiledSchema.name !== 'Config') {
compiledSchema = validateSchema(
this.registeredSchemas.get(compiledSchema.name)!,
compiledSchema,
);
}
delete this.sequelize.models[schema.collectionName];
delete this.sequelize.models[compiledSchema.collectionName];
}

const newSchema = schemaConverter(schema);
const newSchema = schemaConverter(compiledSchema);
this.registeredSchemas.set(
schema.name,
Object.freeze(JSON.parse(JSON.stringify(schema))),
);
this.models[schema.name] = new SequelizeSchema(
this.sequelize,
newSchema,
schema,
compiledSchema,
this,
);

Expand Down
40 changes: 13 additions & 27 deletions modules/database/src/admin/documents.admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ export class DocumentsAdmin {

async getDocument(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
const { schemaName, id, populate } = call.request.params;
const schema = await this.database
.getSchemaModel('_DeclaredSchema')
.model.findOne({ name: schemaName });
const schema = this.database.getSchemaModel(schemaName)?.model.originalSchema;
if (isNil(schema)) {
throw new GrpcError(status.NOT_FOUND, 'Schema does not exist');
throw new GrpcError(status.NOT_FOUND, 'Schema does not exist ');
}
let populates;
if (!isNil(populate)) {
Expand All @@ -43,9 +41,7 @@ export class DocumentsAdmin {
const { schemaName } = call.request.params;
const { skip } = call.request.params ?? 0;
const { limit } = call.request.params ?? 25;
const schema = await this.database.getSchemaModel('_DeclaredSchema').model.findOne({
name: schemaName,
});
const schema = this.database.getSchemaModel(schemaName)?.model.originalSchema;
if (isNil(schema)) {
throw new GrpcError(status.NOT_FOUND, 'Schema does not exist');
}
Expand All @@ -67,10 +63,8 @@ export class DocumentsAdmin {

async createDocument(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
const { schemaName, inputDocument } = call.request.params;
const schema = await this.database.getSchemaModel('_DeclaredSchema').model.findOne({
$and: [{ name: { $nin: this.database.systemSchemas } }, { name: schemaName }],
});
if (isNil(schema)) {
const schema = this.database.getSchemaModel(schemaName)?.model.originalSchema;
if (isNil(schema) || this.database.systemSchemas.includes(schemaName)) {
throw new GrpcError(
status.NOT_FOUND,
'Schema does not exist or disallows doc modifications',
Expand All @@ -81,10 +75,8 @@ export class DocumentsAdmin {

async createDocuments(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
const { schemaName, inputDocuments } = call.request.params;
const schema = await this.database.getSchemaModel('_DeclaredSchema').model.findOne({
$and: [{ name: { $nin: this.database.systemSchemas } }, { name: schemaName }],
});
if (isNil(schema)) {
const schema = this.database.getSchemaModel(schemaName)?.model.originalSchema;
if (isNil(schema) || this.database.systemSchemas.includes(schemaName)) {
throw new GrpcError(
status.NOT_FOUND,
'Schema does not exist or disallows doc modifications',
Expand All @@ -98,10 +90,8 @@ export class DocumentsAdmin {

async updateDocument(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
const { schemaName, id, changedDocument } = call.request.params;
const schema = await this.database.getSchemaModel('_DeclaredSchema').model.findOne({
$and: [{ name: { $nin: this.database.systemSchemas } }, { name: schemaName }],
});
if (isNil(schema)) {
const schema = this.database.getSchemaModel(schemaName)?.model.originalSchema;
if (isNil(schema) || this.database.systemSchemas.includes(schemaName)) {
throw new GrpcError(
status.NOT_FOUND,
'Schema does not exist or disallows doc modifications',
Expand All @@ -124,10 +114,8 @@ export class DocumentsAdmin {

async updateDocuments(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
const { schemaName, changedDocuments } = call.request.params;
const schema = await this.database.getSchemaModel('_DeclaredSchema').model.findOne({
$and: [{ name: { $nin: this.database.systemSchemas } }, { name: schemaName }],
});
if (isNil(schema)) {
const schema = this.database.getSchemaModel(schemaName)?.model.originalSchema;
if (isNil(schema) || this.database.systemSchemas.includes(schemaName)) {
throw new GrpcError(
status.NOT_FOUND,
'Schema does not exist or disallows doc modifications',
Expand All @@ -149,10 +137,8 @@ export class DocumentsAdmin {

async deleteDocument(call: ParsedRouterRequest): Promise<UnparsedRouterResponse> {
const { schemaName, id } = call.request.params;
const schema = await this.database.getSchemaModel('_DeclaredSchema').model.findOne({
$and: [{ name: { $nin: this.database.systemSchemas } }, { name: schemaName }],
});
if (isNil(schema)) {
const schema = this.database.getSchemaModel(schemaName)?.model.originalSchema;
if (isNil(schema) || this.database.systemSchemas.includes(schemaName)) {
throw new GrpcError(
status.NOT_FOUND,
'Schema does not exist or disallows doc modifications',
Expand Down

0 comments on commit 97340a5

Please sign in to comment.