0.0.1 • Published 5 years ago

mongofox v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

MongoFox

MongoDB Wrapper | Supports as much as possible Native features

Description

High perfomance and less code.

What is it?

const { Client, Db, Model } = require('./build/src/index');

// Client is MongoClient Wrapper.     //
// Db is MongoDB's Db Wrapper.        //
// Model is Collection Wrapper.       //
// They are not inherit from Natives. //


// How connect to database by MongoFox? //
// ------------------------------------ //

const client = new Client('URL' && 'mongodb://localhost/test');
const db1 = await client.connect(/* dbname */);

// NOTE: wrap code in `async` function to run it.
// The connection URL defaults to 'mongodb://localhost/test';
// Now `db1` is default db which is specified in URL.
// I.e. db1's name is 'test' here.

const client2 = await Client.connect('URL' && 'mongodb://localhost/test');

// or you can lose client instance:
const db2 = await Db.connect('URL' && 'mongodb://localhost/test');

const db = db1 || db2;


// What is Model in MongoFox? //
// -------------------------- //

// Model is just wrapper for Collection which has more convenient method.
// For start lets create Model instance.

const userModel = db.createModel('foxtest');
// 'foxtest' here is the name of collection which relates to the model.

// Create indexes:
userModel.setIndexes({ // NOTE: `createIndexes` works as native.
  name: ['unique'], // auto ascending
  surname: [-1, 'sparse'], // descending + sparse
  singupTime: [{ // condition + TTL
    expireAfterSeconds: 1000 * 60 * 60 * 24,
    partialFilterExpression: {
      isVerified: false
    }
  }]
});

// Inserting returns inserted id(s).
// Removing returns number of removed.
// Updateing returns number of updated.

// Read operation example:
await userModel.findNow( // `findNow` returns promise, `find` returns cursor
  { age: { $gt: 18 } },    // <-- query criteria
  { name: 1, address: 1 }, // <-- projection
  { sort: { name: 1 } },   // <-- options
);


// MongoFox has types for typescript? //
// ---------------------------------- //

// Yes. If you'he installed MongoFox you already installed MongoFox's types.
// NOTE: This can be changed in future.


// If MongoFox's developers died you can get Native Classes' instances:
const nativeClient = client.getMongoClient();
const nativeDb = db.getMongoDb();
const nativeCollection = userModel.getMongoCollection();

Testing

Those tests is not test all, only not native features. 1. Start mongod (>=3.4) on default port. 2. Drop test db. 3. npm i && npm run test 4. Optional. Drop test db again.