BaseCMS DB
The BaseCMS database driver. Requires direct MongoDB access.
Installation
yarn add @parameter1/base-cms-db
Usage
const { BaseDB, MongoDB } = require('@parameter1/base-cms-db');
const url = 'mongodb://localhost:1234/platform';
const client = new MongoDB.Client(url, {
useNewUrlParser: true,
});
const base = new BaseDB({
tenant: 'cygnus_ofcr',
client,
});
const run = async () => {
const content1 = await base.findById('platform.Content', 12345678);
const content2 = await base.strictFindById('platform.Content', 12345678);
const count = await base.count('platform.Content');
};
run();
API
Instance Methods
All methods return a Promise
and, as such, can be used within async
functions. 🤘
base.findById(modelName, id[, options])
Finds a single document for the provided model name and ID.
const go = async () => {
const content = await base.findById('platform.Content', 12345678);
const content2 = await base.findById('platform.Content', 12345678, {
projection: { name: 1, published: 1 },
});
};
go();
base.strictFindById(modelName, id[, options])
Finds a single document for the provided model name and ID. Will throw an error if the document is not found.
const go = async () => {
const content = await base.strictFindById('platform.Content', 12345678);
};
go().catch(err => console.log(err));
base.findOne(modelName[, query][, options])
Finds a single document for the provided model name and (optional) query criteria.
const go = async () => {
const content = await base.findOne('platform.Content', {
_id: 12345678,
status: 1,
});
const content2 = await base.findOne('platform.Content', {
_id: 12345678,
status: 1,
}, {
projection: { name: 1, published: 1 },
});
};
go();
base.strictFindOne(modelName[, query][, options])
Finds a single document for the provided model name and (optional) query criteria. Will throw an error if the document is not found.
const go = async () => {
const content = await base.strictFindOne('platform.Content', {
_id: 12345678,
status: 1,
});
};
go();
base.find(modelName[, query][, options])
Finds a multiple documents for the provided model name and (optional) query criteria. Will return a MongoDB cursor object.
const go = async () => {
const cursor = await base.find('platform.Content', {
published: { $lte: new Date() },
status: 1,
});
const cursor2 = await base.find('platform.Content', {
published: { $lte: new Date() },
status: 1,
}, {
sort: [['name', 1]],
});
cursor2.sort([['name', -1]]);
};
go();
base.count(modelName[, query][, options])
Counts the number of documents for the provided model name and (optional) query criteria.
const go = async () => {
const num = await base.count('platform.Content', { status: 1 });
};
go();
base.distinct(modelName, field[, query][, options])
Returns distinct values for the provided model name, key (field name) and (optional) query criteria.
const go = async () => {
const dates = await base.distinct('platform.Content', 'published', { status: 1 });
};
go();
base.db(namespace[, options])
Returns a MongoDB Db
instance for the provided namespace.
const go = async () => {
const db = base.db('platform');
};
go();
base.collection(namespace, resource[, options])
Returns a MongoDB Collection
instance for the provided namespace and resource
const go = async () => {
const collection = base.collection('platform', 'Content');
};
go();
base.tenant(key)
Set/change the active tenant. Note: this method does not return a Promise
.
base.tenant('some_tenant');
Static Methods
Utility/helper methods.
coerceID(id)
Coerces a string ID to either a MongoDB ObjectID or an integer. If the id
value is not a string, or does not match the requirements for the above, the id
value will be returned as-is.
const BaseDB = require('@parameter1/base-cms-db');
const { ObjectID } = require('mongodb');
const id1 = BaseDB.coerceID('1234');
const id2 = BaseDB.coerceID('5b0d4edb74265bb4c8edc863');
const id3 = BaseDB.coerceID(ObjectID('5bbb7920adff35d154b1c099'));
const id4 = BaseDB.coerceID('some-value');
Extracts a mutation value from a document for the provided type and field.
const BaseDB = require('@parameter1/base-cms-db');
const doc1 = {
mutations: {
Website: {
name: 'Foo',
},
},
};
const websiteName = BaseDB.extractMutationValue(doc, 'Website', 'name');
const magazibneName = BaseDB.extractMutationValue(doc, 'Magazine', 'name');