mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-07 00:07:07 +00:00
[mongorito] Typing for version 3.0
This commit is contained in:
Vendored
+297
@@ -0,0 +1,297 @@
|
||||
// Type definitions for mongorito v3.0.4
|
||||
// Project: https://github.com/vadimdemedes/mongorito
|
||||
// Definitions by: Pinguet62 <https://github.com/pinguet62>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.4
|
||||
|
||||
import {Collection, CommonOptions, Db, IndexOptions, Long, MongoClientOptions, ReadPreference} from 'mongodb';
|
||||
|
||||
declare namespace mongorito {
|
||||
}
|
||||
|
||||
export {Timestamp, ObjectId, MinKey, MaxKey, DBRef, Long} from 'mongodb';
|
||||
|
||||
type Class<T> = new (...args: any[]) => T;
|
||||
export type ModelClass = Class<Model>;
|
||||
|
||||
declare class MQuery {
|
||||
}
|
||||
|
||||
// "extends MQuery": not actually inheritance, but more easy to implement
|
||||
export class Query extends MQuery {
|
||||
static find<T extends Model>(this: Class<T>, query?: object): Promise<T[]>;
|
||||
|
||||
static findOne<T extends Model>(this: Class<T>, query?: object): Promise<T | null>;
|
||||
|
||||
static findById<T extends Model>(this: Class<T>, id: object | string): Promise<T | null>;
|
||||
|
||||
static count(query?: object): Promise<number>;
|
||||
|
||||
static sort(field: string | string[], value?: string): Query;
|
||||
static sort(query: object): Query; // TODO best parameter type
|
||||
|
||||
static include(field: string | string[], value?: number): Query;
|
||||
static include(query: object): Query; // TODO best parameter type
|
||||
|
||||
static exclude(field: string | string[], value?: number): Query;
|
||||
static exclude(query: object): Query; // TODO best parameter type
|
||||
|
||||
static remove(query?: object): Promise<object>;
|
||||
|
||||
Model: ModelClass;
|
||||
query: Array<[string, object]>;
|
||||
}
|
||||
|
||||
// "extends Query": not actually inheritance, but more easy to implement
|
||||
export class Model extends Query {
|
||||
|
||||
/**
|
||||
* @see Model#database
|
||||
* @see Database#connection()
|
||||
*/
|
||||
static getConnection(): Promise<Db>;
|
||||
|
||||
/**
|
||||
* @see Db#collection(string)
|
||||
*/
|
||||
static getCollection<TSchema = any>(): Promise<Collection<TSchema>>;
|
||||
|
||||
static use(plugins?: Plugin | Plugin[]): void;
|
||||
|
||||
static modifyReducer(reducerModifier: ReducerModifier): void;
|
||||
|
||||
static query(method: string, query: [string, any][]): Promise<object[]>;
|
||||
|
||||
/**
|
||||
* @see mongodb.Collection#listIndexes()
|
||||
* @return {Promise<any[]>} {@link CommandCursor#toArray()}
|
||||
*/
|
||||
static listIndexes(options?: { batchSize?: number, readPreference?: ReadPreference | string }): Promise<any[]>;
|
||||
|
||||
/**
|
||||
* @see mongodb.Collection#createIndex()
|
||||
*/
|
||||
static createIndex(fieldOrSpec: string | any, options?: IndexOptions): Promise<string>;
|
||||
|
||||
/**
|
||||
* @see mongodb.Collection#dropIndex()
|
||||
*/
|
||||
static dropIndex(indexName: string, options?: CommonOptions): Promise<object>;
|
||||
|
||||
static embeds(key: string, model: ModelClass): void;
|
||||
|
||||
constructor(fields?: object);
|
||||
|
||||
collection(): string;
|
||||
|
||||
getConnection(): Promise<Db>;
|
||||
|
||||
getCollection<TSchema = any>(): Promise<Collection<TSchema>>;
|
||||
|
||||
set(key: string, value: any): void;
|
||||
set(value: object): void;
|
||||
|
||||
get(key?: string): any;
|
||||
|
||||
unset(keys?: string | string[]): void;
|
||||
|
||||
toJSON(): object;
|
||||
|
||||
isSaved(): boolean;
|
||||
|
||||
save(): Promise<CreatedAction | UpdatedAction>;
|
||||
|
||||
increment(key: string, value?: number): Promise<RefreshedAction>;
|
||||
increment(keys: { [key: string]: number }): Promise<RefreshedAction>;
|
||||
|
||||
refresh(): Promise<RefreshedAction>;
|
||||
|
||||
remove(): Promise<RemovedAction>;
|
||||
}
|
||||
|
||||
export enum ActionTypes {
|
||||
GET = "@@mongorito/GET",
|
||||
SET = "@@mongorito/SET",
|
||||
UNSET = "@@mongorito/UNSET",
|
||||
REFRESH = "@@mongorito/REFRESH",
|
||||
REFRESHED = "@@mongorito/REFRESHED",
|
||||
SAVE = "@@mongorito/SAVE",
|
||||
CREATE = "@@mongorito/CREATE",
|
||||
CREATED = "@@mongorito/CREATED",
|
||||
UPDATE = "@@mongorito/UPDATE",
|
||||
UPDATED = "@@mongorito/UPDATED",
|
||||
REMOVE = "@@mongorito/REMOVE",
|
||||
REMOVED = "@@mongorito/REMOVED",
|
||||
INCREMENT = "@@mongorito/INCREMENT",
|
||||
CREATE_INDEX = "@@mongorito/CREATE_INDEX",
|
||||
DROP_INDEX = "@@mongorito/DROP_INDEX",
|
||||
LIST_INDEXES = "@@mongorito/LIST_INDEXES",
|
||||
QUERY = "@@mongorito/QUERY",
|
||||
CALL = "@@mongorito/CALL"
|
||||
}
|
||||
|
||||
export interface GetAction {
|
||||
type: string; // ActionTypes.GET
|
||||
key?: string;
|
||||
}
|
||||
|
||||
export interface SetAction {
|
||||
type: string; // ActionTypes.SET
|
||||
fields: object;
|
||||
}
|
||||
|
||||
export interface UnsetAction {
|
||||
type: string; // ActionTypes.UNSET
|
||||
keys: string | string[];
|
||||
}
|
||||
|
||||
export interface RefreshAction {
|
||||
type: string; // ActionTypes.REFRESH
|
||||
}
|
||||
|
||||
export interface RefreshedAction {
|
||||
type: string; // ActionTypes.REFRESHED
|
||||
fields: object;
|
||||
}
|
||||
|
||||
export interface SaveAction {
|
||||
type: string; // ActionTypes.SAVE
|
||||
fields: object;
|
||||
}
|
||||
|
||||
export interface CreateAction {
|
||||
type: string; // ActionTypes.CREATE
|
||||
id: object;
|
||||
}
|
||||
|
||||
export interface CreatedAction {
|
||||
type: string; // ActionTypes.CREATED
|
||||
id: object;
|
||||
}
|
||||
|
||||
export interface UpdateAction {
|
||||
type: string; // ActionTypes.UPDATE
|
||||
fields: object;
|
||||
}
|
||||
|
||||
export interface UpdatedAction {
|
||||
type: string; // ActionTypes.UPDATED
|
||||
fields: object;
|
||||
}
|
||||
|
||||
export interface RemoveAction {
|
||||
type: string; // ActionTypes.REMOVE
|
||||
}
|
||||
|
||||
export interface RemovedAction {
|
||||
type: string; // ActionTypes.REMOVED
|
||||
}
|
||||
|
||||
export interface IncrementAction {
|
||||
type: string; // ActionTypes.INCREMENT
|
||||
fields: object;
|
||||
}
|
||||
|
||||
export interface CreateIndexAction {
|
||||
type: string; // ActionTypes.CREATE_INDEX
|
||||
args: any[];
|
||||
}
|
||||
|
||||
export interface DropIndexAction {
|
||||
type: string; // ActionTypes.DROP_INDEX
|
||||
args: any[];
|
||||
}
|
||||
|
||||
export interface ListIndexesAction {
|
||||
type: string; // ActionTypes.LIST_INDEXES
|
||||
args: any[];
|
||||
}
|
||||
|
||||
export interface QueryAction {
|
||||
type: string; // ActionTypes.QUERY
|
||||
method: string;
|
||||
query: { method: string, args: any }[];
|
||||
}
|
||||
|
||||
export interface CallAction {
|
||||
type: string; // ActionTypes.CALL
|
||||
method: string;
|
||||
args: { method: string, args: any }[];
|
||||
}
|
||||
|
||||
export type Action =
|
||||
GetAction
|
||||
| SetAction
|
||||
| UnsetAction
|
||||
| RefreshAction
|
||||
| RefreshedAction
|
||||
| SaveAction
|
||||
| CreateAction
|
||||
| CreatedAction
|
||||
| UpdateAction
|
||||
| UpdatedAction
|
||||
| RemoveAction
|
||||
| RemovedAction
|
||||
| IncrementAction
|
||||
| CreateIndexAction
|
||||
| DropIndexAction
|
||||
| ListIndexesAction
|
||||
| QueryAction
|
||||
| CallAction
|
||||
|
||||
export type PluginNext = (action: Action) => void;
|
||||
|
||||
interface DefaultState {
|
||||
unset: string[],
|
||||
fields: object
|
||||
}
|
||||
|
||||
export type State = DefaultState & any;
|
||||
|
||||
export interface PluginStore {
|
||||
dispatch: (arg: any) => any,
|
||||
getState: () => State,
|
||||
modelClass: ModelClass,
|
||||
model?: Model
|
||||
}
|
||||
|
||||
export type Reducer<S = any> = (state: S, action: Action) => Reducer;
|
||||
|
||||
export interface DefaultReducer {
|
||||
unset: Reducer<string[]>;
|
||||
fields: Reducer<object>;
|
||||
}
|
||||
|
||||
export type ReducerState = DefaultReducer & Map<any, Reducer>;
|
||||
|
||||
export type ReducerModifier = (reducerState: ReducerState) => ReducerState;
|
||||
|
||||
export type Plugin = (modelClass: ModelClass) => (store: PluginStore) => (next: PluginNext) => (action: Action) => void;
|
||||
|
||||
export enum DatabaseState {
|
||||
STATE_CONNECTED = 0,
|
||||
STATE_CONNECTING = 1,
|
||||
STATE_DISCONNECTED = 2
|
||||
}
|
||||
|
||||
export class Database {
|
||||
options: MongoClientOptions;
|
||||
state: DatabaseState;
|
||||
models: ModelClass[];
|
||||
plugins: Plugin[];
|
||||
|
||||
constructor(urls?: string | string[], options?: MongoClientOptions);
|
||||
|
||||
connect(): Promise<Db>;
|
||||
|
||||
connection(): Promise<Db>;
|
||||
|
||||
disconnect(): Promise<void>;
|
||||
|
||||
register(models: ModelClass | ModelClass[]): void;
|
||||
|
||||
/**
|
||||
* @see Model#use()
|
||||
*/
|
||||
use(plugins?: Plugin | Plugin[]): void;
|
||||
}
|
||||
@@ -0,0 +1,553 @@
|
||||
/**
|
||||
* - Compilation type-check: using TS compiler "strict" options, and DefinitelyTyped linter (based on comments)
|
||||
* - Dynamic type-check: using `expect(...).toBeInstanceOf(...)` on a simple workflow (require Mongodb instance)
|
||||
*/
|
||||
|
||||
import {
|
||||
Collection,
|
||||
Db,
|
||||
DBRef as MongodbDBRef,
|
||||
Long as MongodbLong,
|
||||
MaxKey as MongodbMaxKey,
|
||||
MinKey as MongodbMinKey,
|
||||
MongoClientOptions,
|
||||
ObjectId as MongodbObjectId,
|
||||
Timestamp as MongodbTimestamp
|
||||
} from 'mongodb';
|
||||
import {
|
||||
Action,
|
||||
ActionTypes,
|
||||
CreatedAction,
|
||||
Database,
|
||||
DBRef,
|
||||
Long,
|
||||
MaxKey,
|
||||
MinKey,
|
||||
Model,
|
||||
ModelClass,
|
||||
ObjectId,
|
||||
Plugin,
|
||||
PluginNext,
|
||||
PluginStore,
|
||||
Query,
|
||||
ReducerModifier,
|
||||
ReducerState,
|
||||
RefreshedAction,
|
||||
RemovedAction,
|
||||
State,
|
||||
Timestamp,
|
||||
UpdatedAction
|
||||
} from 'mongorito';
|
||||
|
||||
const URL = `192.168.99.100:27017`;
|
||||
|
||||
// "jest" dependency
|
||||
declare function describe(description: string, callback: Function): void;
|
||||
declare function it(description: string, callback: Function | Promise<Function>): void;
|
||||
declare function expect(obj: any): any;
|
||||
|
||||
function expectToBeAModelClass(obj: ModelClass) {
|
||||
expect(obj).toBeInstanceOf(Function);
|
||||
expect(obj).toHaveProperty('constructor');
|
||||
expect(obj).toHaveProperty('database'); // static field set by "Database.register()" call
|
||||
}
|
||||
|
||||
function expectToBeAPluginStore(obj: PluginStore) {
|
||||
expect(obj.dispatch).toBeInstanceOf(Function);
|
||||
|
||||
expect(obj.getState).toBeInstanceOf(Function);
|
||||
let storeState: State = obj.getState();
|
||||
expectToBeAState(storeState);
|
||||
|
||||
expectToBeAModelClass(obj.modelClass);
|
||||
|
||||
expect(obj.model).toBeInstanceOf(Model);
|
||||
}
|
||||
|
||||
function expectToBeAState(obj: State) {
|
||||
expect(obj).toHaveProperty('unset');
|
||||
expect(Array.isArray(obj.unset)).toBe(true);
|
||||
expect(obj).toHaveProperty('fields');
|
||||
expect(obj.fields).toBeInstanceOf(Object);
|
||||
}
|
||||
|
||||
function expectToBeAQuery(obj: Query) {
|
||||
expectToBeAModelClass(obj.Model);
|
||||
expect(Array.isArray(obj.query)).toBe(true);
|
||||
}
|
||||
|
||||
function expectToBeAAction(obj: Action) {
|
||||
expect(obj).toBeInstanceOf(Object);
|
||||
expect(typeof obj.type).toBe("string");
|
||||
}
|
||||
|
||||
function expectToBeACreatedAction(obj: CreatedAction | UpdatedAction) {
|
||||
expect(obj).toBeInstanceOf(Object);
|
||||
expect(obj.type).toEqual(ActionTypes.CREATED);
|
||||
expect((obj as any).id).toBeInstanceOf(ObjectId);
|
||||
}
|
||||
|
||||
function expectToBeAUpdatedAction(obj: CreatedAction | UpdatedAction) {
|
||||
expect(obj).toBeInstanceOf(Object);
|
||||
expect(obj.type).toEqual(ActionTypes.UPDATED);
|
||||
expect((obj as any).fields).toBeInstanceOf(Object);
|
||||
}
|
||||
|
||||
function expectToBeARefreshedAction(obj: RefreshedAction) {
|
||||
expect(obj).toBeInstanceOf(Object);
|
||||
expect(obj.type).toEqual(ActionTypes.REFRESHED);
|
||||
expect(obj.fields).toBeInstanceOf(Object);
|
||||
}
|
||||
|
||||
function expectToBeARemovedAction(obj: RemovedAction) {
|
||||
expect(obj).toBeInstanceOf(Object);
|
||||
expect(obj.type).toEqual(ActionTypes.REMOVED);
|
||||
}
|
||||
|
||||
function expectToBeACollection(obj: Collection) {
|
||||
// expect(obj).toBeInstanceOf(Object);
|
||||
expect(typeof obj.collectionName).toBe("string");
|
||||
expect(typeof obj.namespace).toBe("string");
|
||||
// expect(obj).toHaveProperty("writeConcern");
|
||||
// expect(obj).toHaveProperty("readConcern");
|
||||
// expect(obj).toHaveProperty("hint");
|
||||
}
|
||||
|
||||
describe('mongorito', () => {
|
||||
const plugin1: Plugin = (modelClass: ModelClass) => (store: PluginStore) => (next: PluginNext) => (action: Action) => {
|
||||
// next(action);
|
||||
};
|
||||
const plugin2: Plugin = (modelClass: ModelClass) => (store: PluginStore) => (next: PluginNext) => (action: Action) => {
|
||||
// next(action);
|
||||
};
|
||||
const plugin3: Plugin = (modelClass: ModelClass) => (store: PluginStore) => (next: PluginNext) => (action: Action) => {
|
||||
// next(action);
|
||||
};
|
||||
|
||||
it('Plugin', async () => {
|
||||
let plugin: Plugin = (modelClass: ModelClass) => {
|
||||
expectToBeAModelClass(modelClass);
|
||||
return (store: PluginStore) => {
|
||||
expectToBeAPluginStore(store);
|
||||
return (next: PluginNext) => {
|
||||
expect(next).toBeInstanceOf(Function);
|
||||
return (action: Action) => {
|
||||
expectToBeAAction(action);
|
||||
// next(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let database = new Database(URL);
|
||||
await database.connect();
|
||||
|
||||
class PluggedModel extends Model {
|
||||
}
|
||||
|
||||
database.register(PluggedModel);
|
||||
PluggedModel.use(plugin);
|
||||
let obj: Model = new PluggedModel({attr: 42});
|
||||
await obj.save()
|
||||
});
|
||||
|
||||
it('Database', async () => {
|
||||
// ----- constructor -----
|
||||
|
||||
new Database(URL);
|
||||
let options: MongoClientOptions = {};
|
||||
let database = new Database(URL, options);
|
||||
|
||||
new Database(URL);
|
||||
|
||||
new Database(['other', URL]);
|
||||
|
||||
new Database(['other', URL], options);
|
||||
|
||||
// ----- connect -----
|
||||
|
||||
let connectResultPromise: Promise<Db> = database.connect();
|
||||
expect(connectResultPromise).toBeInstanceOf(Promise);
|
||||
let connectResult: Db = await connectResultPromise;
|
||||
expect(connectResult).toBeInstanceOf(Db);
|
||||
|
||||
// ----- connection -----
|
||||
|
||||
let connectionResultPromise: Promise<Db> = database.connection();
|
||||
expect(connectionResultPromise).toBeInstanceOf(Promise);
|
||||
let connectionResult: Db = await connectionResultPromise;
|
||||
expect(connectionResult).toBeInstanceOf(Db);
|
||||
|
||||
// ----- disconnect -----
|
||||
|
||||
let disconnectResultPromise: Promise<void> = database.disconnect();
|
||||
expect(disconnectResultPromise).toBeInstanceOf(Promise);
|
||||
let disconnectResult = await disconnectResultPromise;
|
||||
expect(disconnectResult).toBeUndefined();
|
||||
|
||||
// ----- register -----
|
||||
class RegisteredModel extends Model {
|
||||
constructor(fields?: Object) {
|
||||
super(fields);
|
||||
}
|
||||
}
|
||||
|
||||
class RegisteredModelA extends Model {
|
||||
constructor(fields?: Object) {
|
||||
super(fields);
|
||||
}
|
||||
}
|
||||
|
||||
class RegisteredModelB extends Model {
|
||||
constructor(fields?: Object) {
|
||||
super(fields);
|
||||
}
|
||||
}
|
||||
|
||||
expect(database.register(RegisteredModel)).toBeUndefined();
|
||||
expect(database.register([RegisteredModelA, RegisteredModelB])).toBeUndefined();
|
||||
|
||||
// ----- use -----
|
||||
|
||||
expect(database.use(plugin1)).toBeUndefined();
|
||||
expect(database.use([plugin2, plugin3])).toBeUndefined();
|
||||
});
|
||||
|
||||
describe('Model', () => {
|
||||
it('base static', async () => {
|
||||
let database = new Database(URL);
|
||||
await database.connect();
|
||||
|
||||
class Sample extends Model {
|
||||
constructor(fields?: Object) {
|
||||
super(fields);
|
||||
}
|
||||
}
|
||||
|
||||
database.register(Sample);
|
||||
|
||||
const fieldOrSpec: string = 'any';
|
||||
|
||||
// ----- getConnection -----
|
||||
|
||||
let getConnectionResultPromise: Promise<Db> = Sample.getConnection();
|
||||
expect(getConnectionResultPromise).toBeInstanceOf(Promise);
|
||||
let getConnectionResult: Db = await getConnectionResultPromise;
|
||||
expect(getConnectionResult).toBeInstanceOf(Db);
|
||||
|
||||
// ----- getCollection -----
|
||||
|
||||
let getCollectionResultPromise: Promise<Collection<any>> = Sample.getCollection();
|
||||
expect(getCollectionResultPromise).toBeInstanceOf(Promise);
|
||||
let getCollectionResult: Collection<any> = await getCollectionResultPromise;
|
||||
expectToBeACollection(getCollectionResult);
|
||||
|
||||
// ----- use -----
|
||||
|
||||
// expect(Sample.use(plugin1)).toBeUndefined();
|
||||
// expect(Sample.use([plugin2, plugin3])).toBeUndefined();
|
||||
|
||||
// ----- modifyReducer -----
|
||||
|
||||
let reducer: ReducerModifier = (reducerState: ReducerState) => {
|
||||
expect(reducerState.unset).toBeInstanceOf(Function);
|
||||
expect(reducerState.fields).toBeInstanceOf(Function);
|
||||
return reducerState
|
||||
};
|
||||
expect(Sample.modifyReducer(reducer)).toBeUndefined();
|
||||
|
||||
// ----- query -----
|
||||
await new Sample({'attr': 'first'}).save();
|
||||
await new Sample({'attr': 'second'}).save();
|
||||
|
||||
let method: string = 'find';
|
||||
let query: [string, any][] = [
|
||||
['where', {'attr': {$exists: true}}],
|
||||
['limit', 5]
|
||||
];
|
||||
let queryResultPromise: Promise<Object[]> = Sample.query(method, query);
|
||||
expect(queryResultPromise).toBeInstanceOf(Promise);
|
||||
let queryResult: Object[] = await queryResultPromise;
|
||||
expect(Array.isArray(queryResult)).toBe(true);
|
||||
expect(queryResult.length).toBeGreaterThan(0);
|
||||
queryResult.forEach(x => expect(x).toBeInstanceOf(Object));
|
||||
|
||||
// ----- listIndexes -----
|
||||
|
||||
let listIndexesResultPromise: Promise<any[]> = Sample.listIndexes();
|
||||
expect(listIndexesResultPromise).toBeInstanceOf(Promise);
|
||||
let listIndexesResult: any[] = await listIndexesResultPromise;
|
||||
expect(Array.isArray(listIndexesResult)).toBe(true);
|
||||
listIndexesResult.forEach(x => expect(x).not.toBeUndefined());
|
||||
|
||||
// ----- createIndex -----
|
||||
|
||||
let createIndexResultPromise: Promise<string> = Sample.createIndex(fieldOrSpec);
|
||||
expect(createIndexResultPromise).toBeInstanceOf(Promise);
|
||||
let createIndexResult: string = await createIndexResultPromise;
|
||||
expect(typeof createIndexResult).toBe('string');
|
||||
|
||||
// ----- dropIndex -----
|
||||
|
||||
let dropIndexResultPromise: Promise<object> = Sample.dropIndex(createIndexResult);
|
||||
expect(dropIndexResultPromise).toBeInstanceOf(Promise);
|
||||
let dropIndexResult: object = await dropIndexResultPromise;
|
||||
expect(dropIndexResult).not.toBeUndefined();
|
||||
|
||||
// ----- embeds -----
|
||||
class EmbeddedModel extends Model {
|
||||
}
|
||||
|
||||
let key: string = 'any';
|
||||
let model: ModelClass = EmbeddedModel;
|
||||
expect(Sample.embeds(key, model)).toBeUndefined()
|
||||
});
|
||||
|
||||
it('base', async () => {
|
||||
let database = new Database(URL);
|
||||
await database.connect();
|
||||
|
||||
class Sample extends Model {
|
||||
constructor(fields?: Object) {
|
||||
super(fields);
|
||||
}
|
||||
}
|
||||
|
||||
database.register(Sample);
|
||||
|
||||
// ----- constructor -----
|
||||
|
||||
let fields: Object = {views: 5, comments: 2};
|
||||
let model = new Sample(fields);
|
||||
|
||||
new Sample();
|
||||
|
||||
// ----- collection -----
|
||||
|
||||
let collectionResult: string = model.collection();
|
||||
expect(typeof collectionResult).toBe('string');
|
||||
|
||||
// ----- getConnection -----
|
||||
|
||||
let getConnectionResultPromise: Promise<Db> = model.getConnection();
|
||||
expect(getConnectionResultPromise).toBeInstanceOf(Promise);
|
||||
let getConnectionResult: Db = await getConnectionResultPromise;
|
||||
expect(getConnectionResult).toBeInstanceOf(Db);
|
||||
|
||||
// ----- getCollection -----
|
||||
|
||||
let getCollectionResultPromise: Promise<Collection<any>> = model.getCollection();
|
||||
expect(getCollectionResultPromise).toBeInstanceOf(Promise);
|
||||
let getCollectionResult: Collection<any> = await getCollectionResultPromise;
|
||||
expectToBeACollection(getCollectionResult);
|
||||
|
||||
// ----- set -----
|
||||
let key: string = 'attr';
|
||||
let key2: string = 'attr2';
|
||||
let key3: string = 'attr3';
|
||||
|
||||
expect(model.set(key, 'val')).toBeUndefined();
|
||||
expect(model.set({key2: 'val1', key3: 'val2'})).toBeUndefined();
|
||||
|
||||
// ----- get -----
|
||||
|
||||
let getResult1: any = model.get(key);
|
||||
expect(getResult1).not.toBeUndefined();
|
||||
|
||||
let getResult2: any = model.get();
|
||||
expect(getResult2).not.toBeUndefined();
|
||||
|
||||
// ----- unset -----
|
||||
|
||||
expect(model.unset(key)).toBeUndefined();
|
||||
|
||||
let keys: string[] = [key2, key3];
|
||||
expect(model.unset(keys)).toBeUndefined();
|
||||
|
||||
// ----- toJSON -----
|
||||
|
||||
let toJSONResult: object = model.toJSON();
|
||||
expect(toJSONResult).toBeInstanceOf(Object);
|
||||
|
||||
// ----- isSaved -----
|
||||
|
||||
let isSavedResult: boolean = model.isSaved();
|
||||
expect(typeof isSavedResult).toBe('boolean');
|
||||
|
||||
// ----- save -----
|
||||
|
||||
let save1ResultPromise: Promise<CreatedAction | UpdatedAction> = model.save();
|
||||
expect(save1ResultPromise).toBeInstanceOf(Promise);
|
||||
let save1Result: CreatedAction | UpdatedAction = await save1ResultPromise;
|
||||
expectToBeACreatedAction(save1Result);
|
||||
|
||||
let save2ResultPromise: Promise<CreatedAction | UpdatedAction> = model.save();
|
||||
expect(save2ResultPromise).toBeInstanceOf(Promise);
|
||||
let save2Result: CreatedAction | UpdatedAction = await save2ResultPromise;
|
||||
expectToBeAUpdatedAction(save2Result);
|
||||
|
||||
// ----- increment -----
|
||||
|
||||
let incrementResultPromise1: Promise<any> = model.increment('views');
|
||||
expect(incrementResultPromise1).toBeInstanceOf(Promise);
|
||||
let incrementResult1: any = await incrementResultPromise1;
|
||||
expectToBeARefreshedAction(incrementResult1);
|
||||
|
||||
let incrementResultPromise2: Promise<any> = model.increment('views', 2);
|
||||
expect(incrementResultPromise2).toBeInstanceOf(Promise);
|
||||
let incrementResult2: any = await incrementResultPromise2;
|
||||
expectToBeARefreshedAction(incrementResult2);
|
||||
|
||||
let incrementResultPromise3: Promise<any> = model.increment({views: 10, comments: 3});
|
||||
expect(incrementResultPromise3).toBeInstanceOf(Promise);
|
||||
let incrementResult3: any = await incrementResultPromise3;
|
||||
expectToBeARefreshedAction(incrementResult3);
|
||||
|
||||
// ----- refresh -----
|
||||
|
||||
let refreshResultPromise: Promise<RefreshedAction> = model.refresh();
|
||||
expect(refreshResultPromise).toBeInstanceOf(Promise);
|
||||
let refreshResult: RefreshedAction = await refreshResultPromise;
|
||||
expectToBeARefreshedAction(refreshResult);
|
||||
|
||||
// ----- remove -----
|
||||
|
||||
let removeResultPromise: Promise<RemovedAction> = model.remove();
|
||||
expect(removeResultPromise).toBeInstanceOf(Promise);
|
||||
let removeResult: RemovedAction = await removeResultPromise;
|
||||
expectToBeARemovedAction(removeResult);
|
||||
});
|
||||
|
||||
it('Query', async () => {
|
||||
let database = new Database(URL);
|
||||
await database.connect();
|
||||
|
||||
class Sample extends Model {
|
||||
constructor(fields?: Object) {
|
||||
super(fields);
|
||||
}
|
||||
}
|
||||
|
||||
database.register(Sample);
|
||||
|
||||
let cas = new Sample();
|
||||
await cas.save();
|
||||
let ID = cas.get('_id');
|
||||
|
||||
let query: object = {_id: ID};
|
||||
|
||||
// ----- find -----
|
||||
|
||||
let findResultPromise1: Promise<Sample[]> = Sample.find();
|
||||
expect(findResultPromise1).toBeInstanceOf(Promise);
|
||||
let findResult1: Sample[] = await findResultPromise1;
|
||||
expect(Array.isArray(findResult1)).toBe(true);
|
||||
expect(findResult1[0]).toBeInstanceOf(Sample);
|
||||
|
||||
let findResultPromise2: Promise<Sample[]> = Sample.find(query);
|
||||
expect(findResultPromise2).toBeInstanceOf(Promise);
|
||||
let findResult2: Sample[] = await findResultPromise2;
|
||||
expect(Array.isArray(findResult2)).toBe(true);
|
||||
expect(findResult2[0]).toBeInstanceOf(Sample);
|
||||
|
||||
// ----- findOne -----
|
||||
|
||||
let findOneResultPromise1: Promise<Sample | null> = Sample.findOne();
|
||||
expect(findOneResultPromise1).toBeInstanceOf(Promise);
|
||||
let findOneResult1: Sample | null = await findOneResultPromise1;
|
||||
expect(findOneResult1).toBeInstanceOf(Sample);
|
||||
|
||||
let findOneResultPromise2: Promise<Sample | null> = Sample.findOne(query);
|
||||
expect(findOneResultPromise2).toBeInstanceOf(Promise);
|
||||
let findOneResult2: Sample | null = await findOneResultPromise2;
|
||||
expect(findOneResult2).toBeInstanceOf(Sample);
|
||||
|
||||
// ----- findById -----
|
||||
|
||||
let findByIdResultPromise: Promise<Sample | null> = Sample.findById(ID);
|
||||
expect(findByIdResultPromise).toBeInstanceOf(Promise);
|
||||
let findByIdResult: Sample | null = await findByIdResultPromise;
|
||||
expect(findByIdResult).toBeInstanceOf(Sample);
|
||||
|
||||
await Sample.findById(ID.toString());
|
||||
|
||||
// ----- count -----
|
||||
|
||||
let countResultPromise: Promise<number> = Sample.count();
|
||||
expect(countResultPromise).toBeInstanceOf(Promise);
|
||||
let countResult: number = await countResultPromise;
|
||||
expect(typeof countResult).toBe('number');
|
||||
|
||||
await Sample.count(query);
|
||||
|
||||
// ----- sort -----
|
||||
|
||||
let sortResult1: Query = Sample.sort('field-test');
|
||||
expectToBeAQuery(sortResult1);
|
||||
|
||||
let sortResult2: Query = Sample.sort({field: 1, test: -1});
|
||||
expectToBeAQuery(sortResult2);
|
||||
|
||||
// ----- include -----
|
||||
|
||||
let includeResult1: Query = Sample.include('field-test');
|
||||
expectToBeAQuery(includeResult1);
|
||||
|
||||
let includeResult2: Query = Sample.include({field: 1, test: -1});
|
||||
expectToBeAQuery(includeResult2);
|
||||
|
||||
// ----- exclude -----
|
||||
|
||||
let excludeResult1: Query = Sample.exclude('field-test');
|
||||
expectToBeAQuery(excludeResult1);
|
||||
|
||||
let excludeResult2: Query = Sample.exclude({field: 1, test: -1});
|
||||
expectToBeAQuery(excludeResult2);
|
||||
|
||||
// ----- remove -----
|
||||
|
||||
let removeResultPromise: Promise<object> = Sample.remove();
|
||||
expect(removeResultPromise).toBeInstanceOf(Promise);
|
||||
let removeResult: object = await removeResultPromise;
|
||||
expect(removeResult).toBeInstanceOf(Object);
|
||||
|
||||
await Sample.remove(query);
|
||||
});
|
||||
});
|
||||
|
||||
it('Timestamp', async () => {
|
||||
let obj: Timestamp = new Timestamp(1, 9);
|
||||
expect(obj).toBeInstanceOf(Timestamp);
|
||||
expect(obj).toBeInstanceOf(MongodbTimestamp);
|
||||
});
|
||||
|
||||
it('ObjectId', async () => {
|
||||
let obj: ObjectId = new ObjectId();
|
||||
expect(obj).toBeInstanceOf(ObjectId);
|
||||
expect(obj).toBeInstanceOf(MongodbObjectId);
|
||||
});
|
||||
|
||||
it('MinKey', async () => {
|
||||
let obj: MinKey = new MinKey();
|
||||
expect(obj).toBeInstanceOf(MinKey);
|
||||
expect(obj).toBeInstanceOf(MongodbMinKey);
|
||||
});
|
||||
|
||||
it('MaxKey', async () => {
|
||||
let obj: MaxKey = new MaxKey();
|
||||
expect(obj).toBeInstanceOf(MaxKey);
|
||||
expect(obj).toBeInstanceOf(MongodbMaxKey);
|
||||
});
|
||||
|
||||
it('DBRef', async () => {
|
||||
let obj: DBRef = new DBRef('namespace', new ObjectId());
|
||||
expect(obj).toBeInstanceOf(DBRef);
|
||||
expect(obj).toBeInstanceOf(MongodbDBRef);
|
||||
});
|
||||
|
||||
it('Long', async () => {
|
||||
let obj: Long = new Long(1, 9);
|
||||
expect(obj).toBeInstanceOf(Long);
|
||||
expect(obj).toBeInstanceOf(MongodbLong);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"mongorito-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json",
|
||||
"rules": {
|
||||
"adjacent-overload-signatures": false,
|
||||
"array-type": false,
|
||||
"arrow-return-shorthand": false,
|
||||
"ban-types": false,
|
||||
"callable-types": false,
|
||||
"comment-format": false,
|
||||
"dt-header": false,
|
||||
"eofline": false,
|
||||
"export-just-namespace": false,
|
||||
"import-spacing": false,
|
||||
"interface-name": false,
|
||||
"interface-over-type-literal": false,
|
||||
"jsdoc-format": false,
|
||||
"max-line-length": false,
|
||||
"member-access": false,
|
||||
"new-parens": false,
|
||||
"no-any-union": false,
|
||||
"no-boolean-literal-compare": false,
|
||||
"no-conditional-assignment": false,
|
||||
"no-consecutive-blank-lines": false,
|
||||
"no-construct": false,
|
||||
"no-declare-current-package": false,
|
||||
"no-duplicate-imports": false,
|
||||
"no-duplicate-variable": false,
|
||||
"no-empty-interface": false,
|
||||
"no-for-in-array": false,
|
||||
"no-inferrable-types": false,
|
||||
"no-internal-module": false,
|
||||
"no-irregular-whitespace": false,
|
||||
"no-mergeable-namespace": false,
|
||||
"no-misused-new": false,
|
||||
"no-namespace": false,
|
||||
"no-object-literal-type-assertion": false,
|
||||
"no-padding": false,
|
||||
"no-redundant-jsdoc": false,
|
||||
"no-redundant-jsdoc-2": false,
|
||||
"no-redundant-undefined": false,
|
||||
"no-reference-import": false,
|
||||
"no-relative-import-in-test": false,
|
||||
"no-self-import": false,
|
||||
"no-single-declare-module": false,
|
||||
"no-string-throw": false,
|
||||
"no-unnecessary-callback-wrapper": false,
|
||||
"no-unnecessary-class": false,
|
||||
"no-unnecessary-generics": false,
|
||||
"no-unnecessary-qualifier": false,
|
||||
"no-unnecessary-type-assertion": false,
|
||||
"no-useless-files": false,
|
||||
"no-var-keyword": false,
|
||||
"no-var-requires": false,
|
||||
"no-void-expression": false,
|
||||
"no-trailing-whitespace": false,
|
||||
"object-literal-key-quotes": false,
|
||||
"object-literal-shorthand": false,
|
||||
"one-line": false,
|
||||
"one-variable-per-declaration": false,
|
||||
"only-arrow-functions": false,
|
||||
"prefer-conditional-expression": false,
|
||||
"prefer-const": false,
|
||||
"prefer-declare-function": false,
|
||||
"prefer-for-of": false,
|
||||
"prefer-method-signature": false,
|
||||
"prefer-template": false,
|
||||
"radix": false,
|
||||
"semicolon": false,
|
||||
"space-before-function-paren": false,
|
||||
"space-within-parens": false,
|
||||
"strict-export-declare-modifiers": false,
|
||||
"trim-file": false,
|
||||
"triple-equals": false,
|
||||
"typedef-whitespace": false,
|
||||
"unified-signatures": false,
|
||||
"void-return": false,
|
||||
"whitespace": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user