Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(database): schema extensions #423

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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