Skip to content

Commit

Permalink
tweak: Update the way changes and conditions are defined to allow arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Aug 1, 2016
1 parent 74f9477 commit 388f784
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 34 deletions.
23 changes: 22 additions & 1 deletion lib/BSON.ts
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
export { ObjectID, Binary } from "mongodb";
export { ObjectID, Binary } from "mongodb";
import { ObjectID } from "mongodb";

export type BSONType =
undefined
|null
|number
|string|symbol
|BSONObject
|BSONArray
|ObjectID
|boolean
|Date
|RegExp;

export interface BSONObject {
[property: string]: BSONType;
}

export interface BSONArray {
[property: number]: BSONType;
}
25 changes: 12 additions & 13 deletions lib/Changes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {Conditions, UnderlyingBSONType} from "./Conditions";
import {Conditions} from "./Conditions";
import {BSONType, BSONArray, BSONObject} from "./BSON";

export interface Changes {
$set?: {
[property: string]: UnderlyingBSONType;
};
$set?: BSONObject;

$unset?: {
[property: string]: boolean;
Expand All @@ -19,29 +18,29 @@ export interface Changes {

$addToSet?: {
[property: string]: {
$each: UnderlyingBSONType[];
}|any;
$each: BSONArray;
};
}

$push?: {
[property: string]: {
$each: UnderlyingBSONType[];
$slice?: number;
$position?: number;
}|{
$each: UnderlyingBSONType[];
$each: BSONArray;
$slice: number;
$sort: { [property: string]: number; };
$position?: number;
}|UnderlyingBSONType;
}|{
$each: BSONArray;
$slice?: number;
$position?: number;
}|BSONObject|BSONType;
};

$pull?: {
[property: string]: Conditions;
};

$pullAll?: {
[property: string]: UnderlyingBSONType[];
[property: string]: BSONArray;
};

$rename?: {
Expand Down
30 changes: 10 additions & 20 deletions lib/Conditions.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import {ObjectID} from "mongodb";

export type UnderlyingBSONType = null
|number
|string|symbol
|{[property: string]: UnderlyingBSONType;}
//|UnderlyingBSONType[]
|ObjectID
|boolean
|Date
|RegExp;
import {BSONType, BSONArray, ObjectID} from "./BSON";

export interface Conditions {
[property: string]: {
$eq?: UnderlyingBSONType;
$ne?: UnderlyingBSONType;
$in?: UnderlyingBSONType[];
$nin?: UnderlyingBSONType[];
$eq?: BSONType;
$ne?: BSONType;
$in?: BSONArray;
$nin?: BSONArray;
$elemMatch?: Conditions;
$gt?: UnderlyingBSONType;
$gte?: UnderlyingBSONType;
$lt?: UnderlyingBSONType;
$lte?: UnderlyingBSONType;
}|UnderlyingBSONType;
$gt?: BSONType;
$gte?: BSONType;
$lt?: BSONType;
$lte?: BSONType;
}|BSONType;
}
47 changes: 47 additions & 0 deletions test/Changes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as Iridium from "../iridium";
import * as chai from "chai";

interface Document {
_id?: string;
path: {
type: "LineString";
coordinates: number[][];
};
}

@Iridium.Collection("test")
class Instance extends Iridium.Instance<Document, Instance> {
@Iridium.ObjectID
_id: string;

@Iridium.Property({
type: /^LineString$/,
coordinates: [[Number, 2, 2]]
})
path: {
type: "LineString";
coordinates: number[][];
};
}

class Core extends Iridium.Core {
Model = new Iridium.Model<Document, Instance>(this, Instance);
}

describe("Changes", () => {
let core = new Core("mongodb://localhost/iridium_test");

describe("should allow you to specify valid operations", () => {
it("should allow you to use the $push operator", () => {
return core.Model.update({}, {
$push: {
"path.coordinates": {
$each: [[1, 2]],
$position: 1,
$sort: { a: 1 }
}
}
});
});
});
});

0 comments on commit 388f784

Please sign in to comment.