From fcd92c1658dbf2c8242c2c856059b40a82ed7a98 Mon Sep 17 00:00:00 2001 From: Ardalan Amini Date: Mon, 3 Dec 2018 23:20:16 +0330 Subject: [PATCH] WIP --- CHANGELOG.md | 4 +++ package-lock.json | 2 +- package.json | 2 +- src/Collection.ts | 69 ++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 2 ++ test/Collection.ts | 44 +++++++++++++++++++++++++++++ 6 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 src/Collection.ts create mode 100644 test/Collection.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index eacf377..9837a1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ --- +## [v0.5.4](https://github.com/foxifyjs/odin/releases/tag/v0.5.4) - *(2018-12-03)* + +- :zap: Added `Collection` to create collection and indexes + ## [v0.5.0](https://github.com/foxifyjs/odin/releases/tag/v0.5.0) - *(2018-12-01)* - :zap: Added deep relation loading ability diff --git a/package-lock.json b/package-lock.json index 4b55ced..d26baf7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@foxify/odin", - "version": "0.5.3", + "version": "0.5.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 6ace9cc..7ee0a1e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@foxify/odin", - "version": "0.5.3", + "version": "0.5.4", "description": "Active Record Model", "author": "Ardalan Amini [https://github.com/ardalanamini]", "contributors": [ diff --git a/src/Collection.ts b/src/Collection.ts new file mode 100644 index 0000000..b6747f8 --- /dev/null +++ b/src/Collection.ts @@ -0,0 +1,69 @@ +import * as async from "async"; +import * as mongodb from "mongodb"; +import { connection as getConnection } from "./Connect"; + +namespace Collection { + export interface Spec { + [field: string]: 1 | -1; + } + + export interface Index { + spec: Spec; + options: mongodb.IndexOptions; + } +} + +class Collection { + protected _connection = "default"; + + protected _indexes: Collection.Index[] = []; + + constructor(public collection: string, public options: mongodb.CollectionCreateOptions = {}) { } + + public connection(connection: string) { + this._connection = connection; + + return this; + } + + public index(spec: Collection.Spec, options: mongodb.IndexOptions = {}) { + this._indexes.push({ + spec, + options, + }); + + return this; + } + + public exec(callback?: (err?: mongodb.MongoError) => void) { + const DB = getConnection(this._connection); + + if (callback) return DB.createCollection(this.collection, this.options, (err, COLLECTION) => { + if (err) return callback(err); + + async.forEach( + this._indexes, + ({ spec, options }, cb) => COLLECTION.createIndex(spec, options, cb), + err => callback(err as any) + ); + }); + + return new Promise((resolve, reject) => { + DB.createCollection(this.collection, this.options, (err, COLLECTION) => { + if (err) return reject(err); + + async.forEach( + this._indexes, + ({ spec, options }, cb) => COLLECTION.createIndex(spec, options, cb), + (err) => { + if (err) return reject(err); + + resolve(); + } + ); + }); + }); + } +} + +export default Collection; diff --git a/src/index.ts b/src/index.ts index 87e36fe..296e358 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import Relational from "./base/Relational"; +import Collection from "./Collection"; import Connect from "./Connect"; import * as DB from "./DB"; import events from "./events"; @@ -31,6 +32,7 @@ module Odin { } class Odin extends Relational { + public static Collection = Collection; public static Connect = Connect; public static DB = DB; public static GraphQL = GraphQL; diff --git a/test/Collection.ts b/test/Collection.ts new file mode 100644 index 0000000..64daa4e --- /dev/null +++ b/test/Collection.ts @@ -0,0 +1,44 @@ +import { Collection, Connect } from "../src"; + +declare global { + namespace NodeJS { + interface Global { + __MONGO_DB_NAME__: string; + __MONGO_CONNECTION__: any; + } + } +} + +Connect({ + default: { + database: global.__MONGO_DB_NAME__, + connection: global.__MONGO_CONNECTION__, + }, +}); + +it("Should create collection with the given indexes (async/await)", async () => { + expect.assertions(0); + + const collection = new Collection("tests"); + + collection + .index({ field_1: 1, field_2: -1 }, { name: "field_1_field_2", background: true, unique: true }) + .index({ field_3: 1 }, { name: "field_3", background: true }); + + await collection.exec(); +}); + +it("Should create collection with the given indexes (callback)", (done) => { + expect.assertions(1); + + const collection = new Collection("tests"); + + collection + .index({ field_1: 1, field_2: -1 }, { name: "field_1_field_2", background: true, unique: true }) + .index({ field_3: 1 }, { name: "field_3", background: true }); + + collection.exec((err) => { + expect(err).toBe(null); + done(); + }); +});