Skip to content

Commit

Permalink
Merge 9f2b809 into cccc1e9
Browse files Browse the repository at this point in the history
  • Loading branch information
gcandal committed Apr 21, 2021
2 parents cccc1e9 + 9f2b809 commit 7aafc99
Show file tree
Hide file tree
Showing 7 changed files with 949 additions and 31 deletions.
6 changes: 6 additions & 0 deletions js/base/error.js
Expand Up @@ -38,4 +38,10 @@ export class ValidationError extends OperationalError {
}
}

export class AttributeError extends YoniusError {
constructor(message = "Attribute not found") {
super(message);
}
}

export default YoniusError;
21 changes: 20 additions & 1 deletion js/data/collection.js
@@ -1,5 +1,16 @@
import { NotImplementedError, request } from "../base";

import { Reference, References } from "./typesf";

/**
* A mapping from yonius types to the schema types to
* be used by the underlying Mongo collection.
*/
const MONGO_TYPES = [
[Reference, Number],
[References, Array]
];

/**
* Abstract class definition that defines the interface
* expected to be implemented by data driven collections
Expand Down Expand Up @@ -59,7 +70,15 @@ export class MongoCollection extends Collection {

// creates the internal "mongoose" reference to the
// model by encapsulating its name and schema
this._models[name] = mongoose.model(name, new mongoose.Schema(schema));
const filteredSchema = { ...schema };
Object.entries(filteredSchema).forEach(([name, value]) => {
const found = MONGO_TYPES.find(
([type, mongoType]) => value.type.prototype instanceof type
);
if (!found) return;
filteredSchema[name].type = found[1];
});
this._models[name] = mongoose.model(name, new mongoose.Schema(filteredSchema));

// returns the newly constructor mongoose model to
// the caller methods
Expand Down
1 change: 1 addition & 0 deletions js/data/index.js
@@ -1,2 +1,3 @@
export * from "./collection";
export * from "./model";
export * from "./typesf";

0 comments on commit 7aafc99

Please sign in to comment.