Skip to content

Commit 2730398

Browse files
committed
Add new MongoSchemaCollection class that manages schemas for all collections.
1 parent 72362bc commit 2730398

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
import MongoCollection from './MongoCollection';
3+
4+
function _mongoSchemaQueryFromNameQuery(name: string, query) {
5+
return _mongoSchemaObjectFromNameFields(name, query);
6+
}
7+
8+
function _mongoSchemaObjectFromNameFields(name: string, fields) {
9+
let object = { _id: name };
10+
if (fields) {
11+
Object.keys(fields).forEach(key => {
12+
object[key] = fields[key];
13+
});
14+
}
15+
return object;
16+
}
17+
18+
export default class MongoSchemaCollection {
19+
_collection: MongoCollection;
20+
21+
constructor(collection: MongoCollection) {
22+
this._collection = collection;
23+
}
24+
25+
getAllSchemas() {
26+
return this._collection._rawFind({});
27+
}
28+
29+
findSchema(name: string) {
30+
return this._collection._rawFind(_mongoSchemaQueryFromNameQuery(name), { limit: 1 }).then(results => {
31+
return results[0];
32+
});
33+
}
34+
35+
// Atomically find and delete an object based on query.
36+
// The result is the promise with an object that was in the database before deleting.
37+
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.
38+
findAndDeleteSchema(name: string) {
39+
// arguments: query, sort
40+
return this._collection._mongoCollection.findAndRemove(_mongoSchemaQueryFromNameQuery(name), []).then(document => {
41+
// Value is the object where mongo returns multiple fields.
42+
return document.value;
43+
});
44+
}
45+
46+
addSchema(name: string, fields) {
47+
let mongoObject = _mongoSchemaObjectFromNameFields(name, fields);
48+
return this._collection.insertOne(mongoObject);
49+
}
50+
51+
updateSchema(name: string, update) {
52+
return this._collection.updateOne(_mongoSchemaQueryFromNameQuery(name), update);
53+
}
54+
55+
upsertSchema(name: string, query: string, update) {
56+
return this._collection.upsertOne(_mongoSchemaQueryFromNameQuery(name, query), update);
57+
}
58+
}

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11

22
import MongoCollection from './MongoCollection';
3+
import MongoSchemaCollection from './MongoSchemaCollection';
34

45
let mongodb = require('mongodb');
56
let MongoClient = mongodb.MongoClient;
67

8+
const MongoSchemaCollectionName = '_SCHEMA';
9+
710
export class MongoStorageAdapter {
811
// Private
912
_uri: string;
@@ -38,6 +41,12 @@ export class MongoStorageAdapter {
3841
.then(rawCollection => new MongoCollection(rawCollection));
3942
}
4043

44+
schemaCollection(collectionPrefix: string) {
45+
return this.connect()
46+
.then(() => this.adaptiveCollection(collectionPrefix + MongoSchemaCollectionName))
47+
.then(collection => new MongoSchemaCollection(collection));
48+
}
49+
4150
collectionExists(name: string) {
4251
return this.connect().then(() => {
4352
return this.database.listCollections({ name: name }).toArray();

src/Controllers/DatabaseController.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ DatabaseController.prototype.adaptiveCollection = function(className) {
3333
return this.adapter.adaptiveCollection(this.collectionPrefix + className);
3434
};
3535

36+
DatabaseController.prototype.schemaCollection = function() {
37+
return this.adapter.schemaCollection(this.collectionPrefix);
38+
};
39+
3640
DatabaseController.prototype.collectionExists = function(className) {
3741
return this.adapter.collectionExists(this.collectionPrefix + className);
3842
};

0 commit comments

Comments
 (0)