Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Dec 3, 2018
1 parent 823a33c commit fcd92c1
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foxify/odin",
"version": "0.5.3",
"version": "0.5.4",
"description": "Active Record Model",
"author": "Ardalan Amini <[email protected]> [https://github.com/ardalanamini]",
"contributors": [
Expand Down
69 changes: 69 additions & 0 deletions src/Collection.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -31,6 +32,7 @@ module Odin {
}

class Odin<T extends object = {}> extends Relational<T> {
public static Collection = Collection;
public static Connect = Connect;
public static DB = DB;
public static GraphQL = GraphQL;
Expand Down
44 changes: 44 additions & 0 deletions test/Collection.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit fcd92c1

Please sign in to comment.