Add new MongoSchemaCollection class that manages schemas for all collections.
This commit is contained in:
58
src/Adapters/Storage/Mongo/MongoSchemaCollection.js
Normal file
58
src/Adapters/Storage/Mongo/MongoSchemaCollection.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
|
||||||
|
import MongoCollection from './MongoCollection';
|
||||||
|
|
||||||
|
function _mongoSchemaQueryFromNameQuery(name: string, query) {
|
||||||
|
return _mongoSchemaObjectFromNameFields(name, query);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _mongoSchemaObjectFromNameFields(name: string, fields) {
|
||||||
|
let object = { _id: name };
|
||||||
|
if (fields) {
|
||||||
|
Object.keys(fields).forEach(key => {
|
||||||
|
object[key] = fields[key];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class MongoSchemaCollection {
|
||||||
|
_collection: MongoCollection;
|
||||||
|
|
||||||
|
constructor(collection: MongoCollection) {
|
||||||
|
this._collection = collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
getAllSchemas() {
|
||||||
|
return this._collection._rawFind({});
|
||||||
|
}
|
||||||
|
|
||||||
|
findSchema(name: string) {
|
||||||
|
return this._collection._rawFind(_mongoSchemaQueryFromNameQuery(name), { limit: 1 }).then(results => {
|
||||||
|
return results[0];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Atomically find and delete an object based on query.
|
||||||
|
// The result is the promise with an object that was in the database before deleting.
|
||||||
|
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.
|
||||||
|
findAndDeleteSchema(name: string) {
|
||||||
|
// arguments: query, sort
|
||||||
|
return this._collection._mongoCollection.findAndRemove(_mongoSchemaQueryFromNameQuery(name), []).then(document => {
|
||||||
|
// Value is the object where mongo returns multiple fields.
|
||||||
|
return document.value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
addSchema(name: string, fields) {
|
||||||
|
let mongoObject = _mongoSchemaObjectFromNameFields(name, fields);
|
||||||
|
return this._collection.insertOne(mongoObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
updateSchema(name: string, update) {
|
||||||
|
return this._collection.updateOne(_mongoSchemaQueryFromNameQuery(name), update);
|
||||||
|
}
|
||||||
|
|
||||||
|
upsertSchema(name: string, query: string, update) {
|
||||||
|
return this._collection.upsertOne(_mongoSchemaQueryFromNameQuery(name, query), update);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
|
|
||||||
import MongoCollection from './MongoCollection';
|
import MongoCollection from './MongoCollection';
|
||||||
|
import MongoSchemaCollection from './MongoSchemaCollection';
|
||||||
|
|
||||||
let mongodb = require('mongodb');
|
let mongodb = require('mongodb');
|
||||||
let MongoClient = mongodb.MongoClient;
|
let MongoClient = mongodb.MongoClient;
|
||||||
|
|
||||||
|
const MongoSchemaCollectionName = '_SCHEMA';
|
||||||
|
|
||||||
export class MongoStorageAdapter {
|
export class MongoStorageAdapter {
|
||||||
// Private
|
// Private
|
||||||
_uri: string;
|
_uri: string;
|
||||||
@@ -38,6 +41,12 @@ export class MongoStorageAdapter {
|
|||||||
.then(rawCollection => new MongoCollection(rawCollection));
|
.then(rawCollection => new MongoCollection(rawCollection));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
schemaCollection(collectionPrefix: string) {
|
||||||
|
return this.connect()
|
||||||
|
.then(() => this.adaptiveCollection(collectionPrefix + MongoSchemaCollectionName))
|
||||||
|
.then(collection => new MongoSchemaCollection(collection));
|
||||||
|
}
|
||||||
|
|
||||||
collectionExists(name: string) {
|
collectionExists(name: string) {
|
||||||
return this.connect().then(() => {
|
return this.connect().then(() => {
|
||||||
return this.database.listCollections({ name: name }).toArray();
|
return this.database.listCollections({ name: name }).toArray();
|
||||||
|
|||||||
@@ -33,6 +33,10 @@ DatabaseController.prototype.adaptiveCollection = function(className) {
|
|||||||
return this.adapter.adaptiveCollection(this.collectionPrefix + className);
|
return this.adapter.adaptiveCollection(this.collectionPrefix + className);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
DatabaseController.prototype.schemaCollection = function() {
|
||||||
|
return this.adapter.schemaCollection(this.collectionPrefix);
|
||||||
|
};
|
||||||
|
|
||||||
DatabaseController.prototype.collectionExists = function(className) {
|
DatabaseController.prototype.collectionExists = function(className) {
|
||||||
return this.adapter.collectionExists(this.collectionPrefix + className);
|
return this.adapter.collectionExists(this.collectionPrefix + className);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user