@wuguishifu/mongodb-schema-applier
v1.0.0
Published
An app to deploy schemas to a MongoDB database
Downloads
4
Readme
@wuguishifu/mongodb-schema-applier
This package makes it easy to apply index specification schemas to existing mongodb applications. One major use-case is using a schema file to ensure that indexes exist on a mongodb slice on startup of a backend server.
Installation
using npm:
npm install @wuguishifu/mongodb-schema-applier
Usage
Applying Schemas
- Create an
Applier
object. - Use the
apply()
function to asynchronously apply the schema.
import Applier, { Schema } from '@wuguishifu/mongodb-schema-applier';
const schema: Schema = { ... };
const applier = new Applier('localhost:27017', 'my-db', { w: 'majority', retryWrites: true });
applier.apply(schema);
Clearing Schemas
- Create an
Applier
object. - Use the
clear()
function to asynchronously drop indexes
import Applier, { Schema } from '@wuguishifu/mongodb-schema-applier';
const schema: Schema = { ... };
const applier = new Applier('localhost:27017', 'my-db', { w: 'majority', retryWrites: true });
applier.clear(schema);
Usage with schema file
schema.json
see full example in schema-example.json
.
{
"collections": {
"my-collection": {
"indexes": [
{
"field": "myUniqueIndex",
"type": "default",
"indexSpec": {
"pattern": 1
}
}
]
}
}
}
index.ts
import Applier, { Schema } from '@wuguishifu/mongodb-schema-applier';
import _schema from './schema.json';
const schema = _schema as Schema;
const applier = new Applier('localhost:27017', 'my-db', { w: 'majority', retryWrites: true });
// add indexes
try {
await applier.apply(schema);
} catch (error) {
console.error(error);
process.exit(1);
}
// delete indexes
try {
await applier.clear(schema);
} catch (error) {
console.error(error);
process.exit(1);
}
Credit
Built by Bo Bramer. Copyright 2023 Bo Bramer ([email protected])