mongodb-repository-wmf.
This package is now substituted bysee):
Documentation for version 2 and version 1 that are now deprecated by version 3 (Description
mongodb-repository-wmf
or mongodb-entities
is a simple but powerfull MongoDB repository inspired to spring mongodb reactive repository
and based on mongoose
.
It provides a simple way to perfom all CRUD operations and an encryptyon utility to encrypt and decrypt data from and to DB. mongodb-repository-wmf
let you escape from all mongo-db connections troubles (like new connection opening
or connection closing) or mongoose schema-loading steps, without override nothing of mongoose
. You can use all mongoose
power but you don't have to concerne about opening and closing connection and model loading.
You only have to specify the connection string and the schema name in a new class, and without any other line of code you will perform all the CRUD operations in a mongoose way
, and all the CRUD operation will be available
for you.
You can change your schema and your db connection string in any time.
Version 1
mongodb-repository-wmf provides two base repositories (with callback logic):
- BaseMongoRepository
- SecretMongoRepository
Version 2
The Promise logic is added in version 2, with two new promise based repositories:
- BaseMongoPromiseRepository
- SecretMongoPromiseRepository
What's new in version 2
In version 2 Promise based repositories can be used instead the old one (Callback based).
To do that use the Promise version of the repository ( in example getBaseMongoPromiseRepository instead BaseMongoRepository)
var GetBaseMongoPromiseRepository = MongoRepository; ......
And write code in the Promise sintax to execute the operation:
personRepository;
Instead of the old one:
personRepository;
Simple Usage (No encryption) ==> BaseMongoRepository
First step you have to create the new repository (a class) that rappresent your schema and set two parameters, nothing more.
var BaseMongoRepository = MongoRepository; { var data = {} datadbName = 'mongodb://localhost/test'; // use your connection string dataschemaName = 'Person'; // schema Name superdata; }
Note: you can send the dbName and the SchemaName in the constructor
Define your model and initialize mongodb-repository-wmf:
var repository = MongoRepository;var model = Person: firstName : String secondName: String otherInfo : {} /* other schemas*/repository; // mongoose require the model loading
Insert a new Person in mongodb://localhost/test
var person = firstName : "Marcus" secondName : "Fenix" var insertData = query: personvar personRepository = ;personRepository;
Update a Person
var person = firstName : "Marcus" secondName : "Fenix"var updateData = {}updateDataquery = person;updateDataupdate = firstName: "Adam" var personRepository = ;personRepository
Delete a Person
var person = firstName : "Marcus" secondName : "Fenix" var removeData = {}removeDataquery = person; var personRepository = ;personRepository
Find a Person
var person = firstName : "Marcus" secondName : "Fenix" var findData = {}findDataquery = person; var personRepository = ;personRepository
Find all Persons
var findAllData = query: {} ;var personRepository = ;personRepository
Please remind that err and ret variables are returned by mongoose.
Encrypted Usage ==> SecretMongoRepository
First step you have to create the new repository (a class) that rappresent your schema and set two parameters, nothing more.
var SecretMongoRepository = MongoRepository; { var data = {} datadbName = 'mongodb://localhost/test'; // use your connection string dataschemaName = 'SecretPerson'; // schema Name dataparameters = "firstSecretInfo" "secondSecretInfo"; // list of secret fields datapassword = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" //your crypto key // if not present will be used 3zTvzr3p67VC61jmV54rIYu1545x4TlY superdata; }
Define your model and initialize mongodb-repository-wmf:
var repository = MongoRepository;var model = SecretPerson: firstName : String secondName: String firstSecretInfo : String secondSecretInfo : String otherInfo : {} /* other schemas*/repository; // mongoose require the model loading
You can perform all CRUD operation same as the previous example (BaseMongoRepository), but now in the DB you will have firstSecretInfo and secondSecretInfo encrypted.
Please note that the encryption is valid only for first level fields and must be String type, no field in otherInfo can be encrypted!
Examples
Promise Based Repository
var repository = MongoRepository; var GetBaseMongoPromiseRepository = repository; { var data = {} datadbName = 'mongodb://localhost/test'; // use your connection string dataschemaName = 'Person'; // schema Name superdata; } var model = Person: firstName : String secondName: String otherInfo : {} repository; var person = firstName : "Marcus" secondName : "Fenix" var insertData = query: person var personRepository = ;personRepository;
CallBack Based Repository
var repository = MongoRepository; var BaseMongoRepository = repository; { var data = {} datadbName = 'mongodb://localhost/test'; // use your connection string dataschemaName = 'Person'; // schema Name superdata; } var model = Person: firstName : String secondName: String otherInfo : {} repository; var person = firstName : "Marcus" secondName : "Fenix" var insertData = query: person var personRepository = ;personRepository;