nedb: Lint (#15173)

This commit is contained in:
Andy
2017-03-14 12:12:30 -07:00
committed by Mohamed Hegazy
parent 28924690db
commit 21a11ab7d9
3 changed files with 250 additions and 280 deletions

156
nedb/index.d.ts vendored
View File

@@ -1,34 +1,31 @@
// Type definitions for NeDB
// Type definitions for NeDB 1.8
// Project: https://github.com/louischatriot/nedb
// Definitions by: Stefan Steinhart <https://github.com/reppners>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export = NeDBDataStore;
export = Nedb;
export as namespace Nedb;
declare namespace NeDBDataStore { }
declare class NeDBDataStore {
constructor();
constructor(path:string);
constructor(options:NeDB.DataStoreOptions);
declare class Nedb {
constructor(pathOrOptions?: string | Nedb.DataStoreOptions);
persistence:NeDB.Persistence;
persistence: Nedb.Persistence;
/**
* Load the database from the datafile, and trigger the execution of buffered commands if any
*/
loadDatabase(cb?:(err:Error)=>void):void;
loadDatabase(cb?: (err: Error) => void): void;
/**
* Get an array of all the data in the database
*/
getAllData():Array<any>;
getAllData(): Array<any>;
/**
* Reset all currently defined indexes
*/
resetIndexes(newData:any):void;
resetIndexes(newData: any): void;
/**
* Ensure an index is kept for this field. Same parameters as lib/indexes
@@ -39,34 +36,32 @@ declare class NeDBDataStore {
* @param {Boolean} options.sparse
* @param {Function} cb Optional callback, signature: err
*/
ensureIndex(options:NeDB.EnsureIndexOptions, cb?:(err:Error)=>void):void;
ensureIndex(options: Nedb.EnsureIndexOptions, cb?: (err: Error) => void): void;
/**
* Remove an index
* @param {String} fieldName
* @param {Function} cb Optional callback, signature: err
*/
removeIndex(fieldName:string, cb?:(err:Error)=>void):void;
removeIndex(fieldName: string, cb?: (err: Error) => void): void;
/**
* Add one or several document(s) to all indexes
*/
addToIndexes<T>(doc:T):void;
addToIndexes<T>(doc:Array<T>):void;
addToIndexes<T>(doc: T | T[]): void;
/**
* Remove one or several document(s) from all indexes
*/
removeFromIndexes<T>(doc:T):void;
removeFromIndexes<T>(doc:Array<T>):void;
removeFromIndexes<T>(doc: T | T[]): void;
/**
* Update one or several documents in all indexes
* To update multiple documents, oldDoc must be an array of { oldDoc, newDoc } pairs
* If one update violates a constraint, all changes are rolled back
*/
updateIndexes<T>(oldDoc:T, newDoc:T):void;
updateIndexes<T>(updates:Array<{oldDoc:T; newDoc:T;}>):void;
updateIndexes<T>(oldDoc: T, newDoc: T): void;
updateIndexes<T>(updates: Array<{oldDoc: T; newDoc: T}>): void;
/**
* Return the list of candidates for a given query
@@ -77,20 +72,20 @@ declare class NeDBDataStore {
*
* TODO: needs to be moved to the Cursor module
*/
getCandidates(query:any):void;
getCandidates(query: any): void;
/**
* Insert a new document
* @param {Function} cb Optional callback, signature: err, insertedDoc
*/
insert<T>(newDoc:T, cb?:(err:Error, document:T)=>void):void;
insert<T>(newDoc: T, cb?: (err: Error, document: T) => void): void;
/**
* Count all documents matching the query
* @param {any} query MongoDB-style query
*/
count(query:any, callback:(err:Error, n:number)=>void):void;
count(query:any):NeDB.CursorCount;
count(query: any, callback: (err: Error, n: number) => void): void;
count(query: any): Nedb.CursorCount;
/**
* Find all documents matching the query
@@ -98,61 +93,61 @@ declare class NeDBDataStore {
* @param {any} query MongoDB-style query
* @param {any} projection MongoDB-style projection
*/
find<T>(query:any, projection:T, callback:(err:Error, documents:Array<T>)=>void):void;
find<T>(query:any, projection:T):NeDB.Cursor<T>;
find<T>(query: any, projection: T, callback: (err: Error, documents: Array<T>) => void): void;
find<T>(query: any, projection?: T): Nedb.Cursor<T>;
/**
* Find all documents matching the query
* If no callback is passed, we return the cursor so that user can limit, skip and finally exec
* * @param {any} query MongoDB-style query
*/
find<T>(query:any, callback:(err:Error, documents:Array<T>)=>void):void;
find<T>(query:any):NeDB.Cursor<T>;
find<T>(query: any, callback: (err: Error, documents: Array<T>) => void): void;
/**
* Find one document matching the query
* @param {any} query MongoDB-style query
* @param {any} projection MongoDB-style projection
*/
findOne<T>(query:any, projection:T, callback:(err:Error, document:T)=>void):void;
findOne<T>(query: any, projection: T, callback: (err: Error, document: T) => void): void;
/**
* Find one document matching the query
* @param {any} query MongoDB-style query
*/
findOne<T>(query:any, callback:(err:Error, document:T)=>void):void;
findOne<T>(query: any, callback: (err: Error, document: T) => void): void;
/**
* Update all docs matching query v1.7.4 and prior signature.
* Update all docs matching query v1.7.4 and prior signature.
* For now, very naive implementation (recalculating the whole database)
* @param {any} query
* @param {any} updateQuery
* @param {Object} options Optional options
* options.multi If true, can update multiple documents (defaults to false)
* options.upsert If true, document is inserted if the query doesn't match anything
* @param {Function} cb Optional callback, signature: err,
* numReplaced,
* upsert (set to true if the update was in fact an upsert)
* @param {Function} cb Optional callback, signature: err,
* numReplaced,
* upsert (set to true if the update was in fact an upsert)
*
* @api private Use Datastore.update which has the same signature
*/
update(query:any, updateQuery:any, options?:NeDB.UpdateOptions, cb?:(err:Error, numberOfUpdated:number, upsert:boolean)=>void):void;
/**
* Update all docs matching query v1.8 signature.
* For now, very naive implementation (recalculating the whole database)
* @param {any} query
* @param {any} updateQuery
* @param {Object} options Optional options
* options.multi If true, can update multiple documents (defaults to false)
* options.upsert If true, document is inserted if the query doesn't match anything
* @param {Function} cb Optional callback, signature: err,
* numAffected,
* affectedDocuments (when returnUpdatedDocs is set to true), obj or array
* upsert (set to true if the update was in fact an upsert)
*
* @api private Use Datastore.update which has the same signature
*/
update<T>(query:any, updateQuery:any, options?:NeDB.UpdateOptions, cb?:(err:Error, numberOfUpdated:number, affectedDocuments:any, upsert:boolean)=>void):void;
update(query: any, updateQuery: any, options?: Nedb.UpdateOptions, cb?: (err: Error, numberOfUpdated: number, upsert: boolean) => void): void;
/**
* Update all docs matching query v1.8 signature.
* For now, very naive implementation (recalculating the whole database)
* @param {any} query
* @param {any} updateQuery
* @param {Object} options Optional options
* options.multi If true, can update multiple documents (defaults to false)
* options.upsert If true, document is inserted if the query doesn't match anything
* @param {Function} cb Optional callback, signature: err,
* numAffected,
* affectedDocuments (when returnUpdatedDocs is set to true), obj or array
* upsert (set to true if the update was in fact an upsert)
*
* @api private Use Datastore.update which has the same signature
*/
update<T>(query: any, updateQuery: any, options?: Nedb.UpdateOptions, cb?: (err: Error, numberOfUpdated: number, affectedDocuments: any, upsert: boolean) => void): void;
/**
* Remove all docs matching the query
@@ -164,32 +159,45 @@ declare class NeDBDataStore {
*
* @api private Use Datastore.remove which has the same signature
*/
remove(query:any, options:NeDB.RemoveOptions, cb?:(err:Error, n:number)=>void):void;
remove(query:any, cb?:(err:Error, n:number)=>void):void;
remove(query: any, options: Nedb.RemoveOptions, cb?: (err: Error, n: number) => void): void;
remove(query: any, cb?: (err: Error, n: number) => void): void;
}
declare namespace NeDB {
declare namespace Nedb {
interface Cursor<T> {
sort(query:any):Cursor<T>;
skip(n:number):Cursor<T>;
limit(n:number):Cursor<T>;
projection(query:any):Cursor<T>;
exec(callback:(err:Error, documents:Array<T>)=>void):void;
sort(query: any): Cursor<T>;
skip(n: number): Cursor<T>;
limit(n: number): Cursor<T>;
projection(query: any): Cursor<T>;
exec(callback: (err: Error, documents: Array<T>) => void): void;
}
interface CursorCount {
exec(callback:(err:Error, count:number)=>void):void;
exec(callback: (err: Error, count: number) => void): void;
}
interface DataStoreOptions {
filename?:string // Optional, datastore will be in-memory only if not provided
inMemoryOnly?:boolean // Optional, default to false
nodeWebkitAppName?:boolean // Optional, specify the name of your NW app if you want options.filename to be relative to the directory where
autoload?:boolean // Optional, defaults to false
onload?:(error:Error)=>any // Optional, if autoload is used this will be called after the load database with the error object as parameter. If you don't pass it the error will be thrown
afterSerialization?:(line:string)=>string; // (optional): hook you can use to transform data after it was serialized and before it is written to disk. Can be used for example to encrypt data before writing database to disk. This function takes a string as parameter (one line of an NeDB data file) and outputs the transformed string, which must absolutely not contain a \n character (or data will be lost)
beforeDeserialization?:(line:string)=>string; // (optional): reverse of afterSerialization. Make sure to include both and not just one or you risk data loss. For the same reason, make sure both functions are inverses of one another. Some failsafe mechanisms are in place to prevent data loss if you misuse the serialization hooks: NeDB checks that never one is declared without the other, and checks that they are reverse of one another by testing on random strings of various lengths. In addition, if too much data is detected as corrupt, NeDB will refuse to start as it could mean you're not using the deserialization hook corresponding to the serialization hook used before (see below)
corruptAlertThreshold?:number; // (optional): between 0 and 1, defaults to 10%. NeDB will refuse to start if more than this percentage of the datafile is corrupt. 0 means you don't tolerate any corruption, 1 means you don't care
filename?: string // Optional, datastore will be in-memory only if not provided
inMemoryOnly?: boolean // Optional, default to false
nodeWebkitAppName?: boolean // Optional, specify the name of your NW app if you want options.filename to be relative to the directory where
autoload?: boolean // Optional, defaults to false
// Optional, if autoload is used this will be called after the load database with the error object as parameter. If you don't pass it the error will be thrown
onload?: (error: Error) => any
// (optional): hook you can use to transform data after it was serialized and before it is written to disk.
// Can be used for example to encrypt data before writing database to disk.
// This function takes a string as parameter (one line of an NeDB data file) and outputs the transformed string, which must absolutely not contain a \n character (or data will be lost)
afterSerialization?: (line: string) => string;
// (optional): reverse of afterSerialization.
// Make sure to include both and not just one or you risk data loss.
// For the same reason, make sure both functions are inverses of one another.
// Some failsafe mechanisms are in place to prevent data loss if you misuse the serialization hooks:
// NeDB checks that never one is declared without the other, and checks that they are reverse of one another by testing on random strings of various lengths.
// In addition, if too much data is detected as corrupt,
// NeDB will refuse to start as it could mean you're not using the deserialization hook corresponding to the serialization hook used before (see below)
beforeDeserialization?: (line: string) => string;
// (optional): between 0 and 1, defaults to 10%. NeDB will refuse to start if more than this percentage of the datafile is corrupt.
// 0 means you don't tolerate any corruption, 1 means you don't care
corruptAlertThreshold?: number;
}
/**
@@ -206,18 +214,18 @@ declare namespace NeDB {
* options only one option for now: multi which allows the removal of multiple documents if set to true. Default is false
*/
interface RemoveOptions {
multi?:boolean
multi?: boolean
}
interface EnsureIndexOptions {
fieldName:string;
unique?:boolean;
sparse?:boolean;
fieldName: string;
unique?: boolean;
sparse?: boolean;
}
interface Persistence {
compactDatafile():void;
setAutocompactionInterval(interval:number):void;
stopAutocompaction():void;
compactDatafile(): void;
setAutocompactionInterval(interval: number): void;
stopAutocompaction(): void;
}
}

View File

@@ -2,193 +2,152 @@
* Created by stefansteinhart on 31.01.15.
*/
/// <reference types="q" />
/// <reference types="node" />
import * as es6styleimport from 'nedb';
import Q = require('q');
import nedb = require('nedb');
class BaseCollection<T> {
private dataStore: nedb;
private dataStore:nedb;
constructor(dataStore:nedb) {
constructor(dataStore: nedb) {
this.dataStore = dataStore;
}
public insert(document:T):Q.Promise<T> {
var deferred = Q.defer<T>();
this.dataStore.insert<T>(document, function (err:Error, newDoc:T) { // Callback is optional
// newDoc is the newly inserted document, including its _id
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(newDoc);
}
insert(document: T): Promise<T> {
return new Promise((resolve, reject) => {
this.dataStore.insert<T>(document, (err: Error, newDoc: T) => { // Callback is optional
// newDoc is the newly inserted document, including its _id
if (err) {
reject(err);
} else {
resolve(newDoc);
}
});
});
return deferred.promise;
}
public count():Q.Promise<number> {
var deferred = Q.defer<number>();
this.dataStore.count({}, function (err:Error, count:number) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(count);
}
count(): Promise<number> {
return new Promise((resolve, reject) => {
this.dataStore.count({}, (err: Error, count: number) => {
if (err) {
reject(err);
} else {
resolve(count);
}
});
});
return deferred.promise;
}
public countBy(criteria:any):Q.Promise<number> {
var deferred = Q.defer<number>();
this.dataStore.count(criteria, function (err:Error, count:number) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(count);
}
countBy(criteria: any): Promise<number> {
return new Promise((resolve, reject) => {
this.dataStore.count(criteria, (err: Error, count: number) => {
if (err) {
reject(err);
} else {
resolve(count);
}
});
});
return deferred.promise;
}
public findByID(id:string):Q.Promise<T> {
var deferred = Q.defer<T>();
this.dataStore.findOne<T>({_id: id}, function (err:Error, doc:T) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(doc);
}
findByID(id: string): Promise<T> {
return new Promise((resolve, reject) => {
this.dataStore.findOne<T>({_id: id}, (err: Error, doc: T) => {
if (err) {
reject(err);
} else {
resolve(doc);
}
});
});
return deferred.promise;
}
public findOne(criteria:any):Q.Promise<T> {
var deferred = Q.defer<T>();
this.dataStore.findOne<T>(criteria, function (err:Error, doc:T) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(doc);
}
findOne(criteria: any): Promise<T> {
return new Promise((resolve, reject) => {
this.dataStore.findOne<T>(criteria, (err: Error, doc: T) => {
if (err) {
reject(err);
} else {
resolve(doc);
}
});
});
return deferred.promise;
}
public find(criteria:any):Q.Promise<Array<T>> {
var deferred = Q.defer<Array<T>>();
this.dataStore.find(criteria, function (err:Error, docs:Array<T>) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(docs);
}
find(criteria: any): Promise<T[]> {
return new Promise((resolve, reject) => {
this.dataStore.find(criteria, (err: Error, docs: T[]) => {
if (err) {
reject(err);
}
else {
resolve(docs);
}
});
});
return deferred.promise;
}
public all():Q.Promise<Array<T>> {
var deferred = Q.defer<Array<T>>();
this.dataStore.find({}, function (err:Error, docs:Array<T>) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(docs);
}
all(): Promise<T[]> {
return new Promise((resolve, reject) => {
this.dataStore.find({}, (err: Error, docs: T[]) => {
if (err) {
reject(err);
} else {
resolve(docs);
}
});
});
return deferred.promise;
}
public upsert(query:any, updateQuery:any):Q.Promise<T> {
var deferred = Q.defer<T>();
this.dataStore.update(query, updateQuery, {upsert: true}, function (err:Error, numberOfUpdated:number, upsert: boolean) {
if (err) {
deferred.reject(err);
}
else {
//deferred.resolve(newDoc);
}
upsert(query: any, updateQuery: any): Promise<T[]> {
const a: Promise<T[]> = new Promise((resolve, reject) => {
this.dataStore.update(query, updateQuery, {upsert: true}, (err: Error, numberOfUpdated: number, upsert: boolean) => {
if (err) {
reject(err);
} else {
//resolve(newDoc);
}
});
});
this.dataStore.update(query, updateQuery, {upsert: true}, function (err:Error, numberOfUpdated:number, affectedDocs:any, upsert: boolean) {
if (err) {
deferred.reject(err);
}
else {
//deferred.resolve(newDoc);
}
const b: Promise<T[]> = new Promise((resolve, reject) => {
this.dataStore.update(query, updateQuery, {upsert: true}, (err: Error, numberOfUpdated: number, affectedDocs: any, upsert: boolean) => {
if (err) {
reject(err);
} else {
//resolve(newDoc);
}
});
});
return deferred.promise;
return a || b;
}
public update(query:Object, updateQuery:Object, options?:any):Q.Promise<number> {
var deferred = Q.defer<number>();
this.dataStore.update(query, updateQuery, options, function (err:Error, numberOfUpdated:number) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(numberOfUpdated);
}
update(query: {}, updateQuery: {}, options?: any): Promise<number> {
return new Promise((resolve, reject) => {
this.dataStore.update(query, updateQuery, options, (err: Error, numberOfUpdated: number) => {
if (err) {
reject(err);
} else {
resolve(numberOfUpdated);
}
});
});
return deferred.promise;
}
public remove(criteria:any):Q.Promise<number> {
var deferred = Q.defer<number>();
this.dataStore.remove(criteria, function (err:Error, numberOfDeletedEntrys:number) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(numberOfDeletedEntrys);
}
remove(criteria: any): Promise<number> {
return new Promise((resolve, reject) => {
this.dataStore.remove(criteria, (err: Error, numberOfDeletedEntrys: number) => {
if (err) {
reject(err);
}
else {
resolve(numberOfDeletedEntrys);
}
});
});
return deferred.promise;
}
}
@@ -199,7 +158,7 @@ var db = new Datastore();
// Type 2: Persistent datastore with manual loading
db = new Datastore({filename: 'path/to/datafile'});
db.loadDatabase(function (err) { // Callback is optional
db.loadDatabase((err) => { // Callback is optional
// Now commands will be executed
});
@@ -217,7 +176,7 @@ db = new Datastore({filename: path.join(require('nw.gui').App.dataPath, 'somethi
// Of course you can create multiple datastores if you need several
// collections. In this case it's usually a good idea to use autoload for all collections.
var dbContainer:any = {};
var dbContainer: any = {};
dbContainer.users = new Datastore('path/to/users.db');
dbContainer.robots = new Datastore('path/to/robots.db');
@@ -225,7 +184,7 @@ dbContainer.robots = new Datastore('path/to/robots.db');
dbContainer.users.loadDatabase();
dbContainer.robots.loadDatabase();
var doc:any = {
var doc: any = {
hello: 'world'
, n: 5
, today: new Date()
@@ -236,151 +195,151 @@ var doc:any = {
, infos: {name: 'nedb'}
};
db.insert(doc, function (err:Error, newDoc:any) { // Callback is optional
db.insert(doc, (err: Error, newDoc: any) => { // Callback is optional
// newDoc is the newly inserted document, including its _id
// newDoc has no key called notToBeSaved since its value was undefined
});
db.insert([{a: 5}, {a: 42}], function (err:Error, newdocs:Array<any>) {
db.insert([{a: 5}, {a: 42}], (err: Error, newdocs: any[]) => {
// Two documents were inserted in the database
// newDocs is an array with these documents, augmented with their _id
});
// If there is a unique constraint on field 'a', this will fail
db.insert([{a: 5}, {a: 42}, {a: 5}], function (err:Error) {
db.insert([{a: 5}, {a: 42}, {a: 5}], (err: Error) => {
// err is a 'uniqueViolated' error
// The database was not modified
});
// Finding all planets in the solar system
db.find({system: 'solar'}, function (err:Error, docs:Array<any>) {
db.find({system: 'solar'}, (err: Error, docs: any[]) => {
// docs is an array containing documents Mars, Earth, Jupiter
// If no document is found, docs is equal to []
});
// Finding all planets whose name contain the substring 'ar' using a regular expression
db.find({planet: /ar/}, function (err:Error, docs:Array<any>) {
db.find({planet: /ar/}, (err: Error, docs: any[]) => {
// docs contains Mars and Earth
});
// Finding all inhabited planets in the solar system
db.find({system: 'solar', inhabited: true}, function (err:Error, docs:Array<any>) {
db.find({system: 'solar', inhabited: true}, (err: Error, docs: any[]) => {
// docs is an array containing document Earth only
});
// Use the dot-notation to match fields in subdocuments
db.find({"humans.genders": 2}, function (err:Error, docs:Array<any>) {
db.find({"humans.genders": 2}, (err: Error, docs: any[]) => {
// docs contains Earth
});
// Use the dot-notation to navigate arrays of subdocuments
db.find({"completeData.planets.name": "Mars"}, function (err:Error, docs:Array<any>) {
db.find({"completeData.planets.name": "Mars"}, (err: Error, docs: any[]) => {
// docs contains document 5
});
db.find({"completeData.planets.name": "Jupiter"}, function (err:Error, docs:Array<any>) {
db.find({"completeData.planets.name": "Jupiter"}, (err: Error, docs: any[]) => {
// docs is empty
});
db.find({"completeData.planets.0.name": "Earth"}, function (err:Error, docs:Array<any>) {
db.find({"completeData.planets.0.name": "Earth"}, (err: Error, docs: any[]) => {
// docs contains document 5
// If we had tested against "Mars" docs would be empty because we are matching against a specific array element
});
// You can also deep-compare objects. Don't confuse this with dot-notation!
db.find({humans: {genders: 2}}, function (err:Error, docs:Array<any>) {
db.find({humans: {genders: 2}}, (err: Error, docs: any[]) => {
// docs is empty, because { genders: 2 } is not equal to { genders: 2, eyes: true }
});
// Find all documents in the collection
db.find({}, function (err:Error, docs:Array<any>) {
db.find({}, (err: Error, docs: any[]) => {
});
// The same rules apply when you want to only find one document
db.findOne({_id: 'id1'}, function (err:Error, doc:any) {
db.findOne({_id: 'id1'}, (err: Error, doc: any) => {
// doc is the document Mars
// If no document is found, doc is null
});
// $lt, $lte, $gt and $gte work on numbers and strings
db.find({"humans.genders": {$gt: 5}}, function (err:Error, docs:Array<any>) {
db.find({"humans.genders": {$gt: 5}}, (err: Error, docs: any[]) => {
// docs contains Omicron Persei 8, whose humans have more than 5 genders (7).
});
// When used with strings, lexicographical order is used
db.find({planet: {$gt: 'Mercury'}}, function (err:Error, docs:Array<any>) {
db.find({planet: {$gt: 'Mercury'}}, (err: Error, docs: any[]) => {
// docs contains Omicron Persei 8
})
// Using $in. $nin is used in the same way
db.find({planet: {$in: ['Earth', 'Jupiter']}}, function (err:Error, docs:Array<any>) {
db.find({planet: {$in: ['Earth', 'Jupiter']}}, (err: Error, docs: any[]) => {
// docs contains Earth and Jupiter
});
// Using $exists
db.find({satellites: {$exists: true}}, function (err:Error, docs:Array<any>) {
db.find({satellites: {$exists: true}}, (err: Error, docs: any[]) => {
// docs contains only Mars
});
// Using $regex with another operator
db.find({planet: {$regex: /ar/, $nin: ['Jupiter', 'Earth']}}, function (err:Error, docs:Array<any>) {
db.find({planet: {$regex: /ar/, $nin: ['Jupiter', 'Earth']}}, (err: Error, docs: any[]) => {
// docs only contains Mars because Earth was excluded from the match by $nin
});
// Using an array-specific comparison function
// Note: you can't use nested comparison functions, e.g. { $size: { $lt: 5 } } will throw an error
db.find({satellites: {$size: 2}}, function (err:Error, docs:Array<any>) {
db.find({satellites: {$size: 2}}, (err: Error, docs: any[]) => {
// docs contains Mars
});
db.find({satellites: {$size: 1}}, function (err:Error, docs:Array<any>) {
db.find({satellites: {$size: 1}}, (err: Error, docs: any[]) => {
// docs is empty
});
// If a document's field is an array, matching it means matching any element of the array
db.find({satellites: 'Phobos'}, function (err:Error, docs:Array<any>) {
db.find({satellites: 'Phobos'}, (err: Error, docs: any[]) => {
// docs contains Mars. Result would have been the same if query had been { satellites: 'Deimos' }
});
// This also works for queries that use comparison operators
db.find({satellites: {$lt: 'Amos'}}, function (err:Error, docs:Array<any>) {
db.find({satellites: {$lt: 'Amos'}}, (err: Error, docs: any[]) => {
// docs is empty since Phobos and Deimos are after Amos in lexicographical order
});
// This also works with the $in and $nin operator
db.find({satellites: {$in: ['Moon', 'Deimos']}}, function (err:Error, docs:Array<any>) {
db.find({satellites: {$in: ['Moon', 'Deimos']}}, (err: Error, docs: any[]) => {
// docs contains Mars (the Earth document is not complete!)
});
db.find({$or: [{planet: 'Earth'}, {planet: 'Mars'}]}, function (err:Error, docs:Array<any>) {
db.find({$or: [{planet: 'Earth'}, {planet: 'Mars'}]}, (err: Error, docs: any[]) => {
// docs contains Earth and Mars
});
db.find({$not: {planet: 'Earth'}}, function (err:Error, docs:Array<any>) {
db.find({$not: {planet: 'Earth'}}, (err: Error, docs: any[]) => {
// docs contains Mars, Jupiter, Omicron Persei 8
});
db.find({
$where: function () {
$where() {
return parseInt(Object.keys(this)[0]) > 6;
}
}, function (err:Error, docs:Array<any>) {
}, (err: Error, docs: any[]) => {
// docs with more than 6 properties
});
// You can mix normal queries, comparison queries and logical operators
db.find({$or: [{planet: 'Earth'}, {planet: 'Mars'}], inhabited: true}, function (err:Error, docs:Array<any>) {
db.find({$or: [{planet: 'Earth'}, {planet: 'Mars'}], inhabited: true}, (err: Error, docs: any[]) => {
// docs contains Earth
});
// No query used means all results are returned (before the Cursor modifiers)
db.find({}).sort({planet: 1}).skip(1).limit(2).exec(function (err:Error, docs:Array<any>) {
db.find({}).sort({planet: 1}).skip(1).limit(2).exec((err: Error, docs: any[]) => {
// docs is [doc3, doc1]
});
// You can sort in reverse order like this
db.find({system: 'solar'}).sort({planet: -1}).exec(function (err:Error, docs:Array<any>) {
db.find({system: 'solar'}).sort({planet: -1}).exec((err: Error, docs: any[]) => {
// docs is [doc1, doc3, doc2]
});
@@ -390,38 +349,38 @@ db.find({}).sort({firstField: 1, secondField: -1});
// Same database as above
// Keeping only the given fields
db.find({planet: 'Mars'}, {planet: 1, system: 1}, function (err:Error, docs:Array<any>) {
db.find({planet: 'Mars'}, {planet: 1, system: 1}, (err: Error, docs: any[]) => {
// docs is [{ planet: 'Mars', system: 'solar', _id: 'id1' }]
});
// Keeping only the given fields but removing _id
db.find({planet: 'Mars'}, {planet: 1, system: 1, _id: 0}, function (err:Error, docs:Array<any>) {
db.find({planet: 'Mars'}, {planet: 1, system: 1, _id: 0}, (err: Error, docs: any[]) => {
// docs is [{ planet: 'Mars', system: 'solar' }]
});
// Omitting only the given fields and removing _id
db.find({planet: 'Mars'}, {planet: 0, system: 0, _id: 0}, function (err:Error, docs:Array<any>) {
db.find({planet: 'Mars'}, {planet: 0, system: 0, _id: 0}, (err: Error, docs: any[]) => {
// docs is [{ inhabited: false, satellites: ['Phobos', 'Deimos'] }]
});
// Failure: using both modes at the same time
db.find({planet: 'Mars'}, {planet: 0, system: 1}, function (err:Error, docs:Array<any>) {
db.find({planet: 'Mars'}, {planet: 0, system: 1}, (err: Error, docs: any[]) => {
// err is the error message, docs is undefined
});
// You can also use it in a Cursor way but this syntax is not compatible with MongoDB
// If upstream compatibility is important don't use this method
db.find({planet: 'Mars'}).projection({planet: 1, system: 1}).exec(function (err:Error, docs:Array<any>) {
db.find({planet: 'Mars'}).projection({planet: 1, system: 1}).exec((err: Error, docs: any[]) => {
// docs is [{ planet: 'Mars', system: 'solar', _id: 'id1' }]
});
// Count all planets in the solar system
db.count({system: 'solar'}, function (err:Error, count:number) {
db.count({system: 'solar'}, (err: Error, count: number) => {
// count equals to 3
});
// Count all documents in the datastore
db.count({}, function (err:Error, count:number) {
db.count({}, (err: Error, count: number) => {
// count equals to 4
});
@@ -432,7 +391,7 @@ db.count({}, function (err:Error, count:number) {
// { _id: 'id4', planet: 'Omicron Persia 8', system: 'futurama', inhabited: true }
// Replace a document by another
db.update({planet: 'Jupiter'}, {planet: 'Pluton'}, {}, function (err:Error, numReplaced:number) {
db.update({planet: 'Jupiter'}, {planet: 'Pluton'}, {}, (err: Error, numReplaced: number) => {
// numReplaced = 1
// The doc #3 has been replaced by { _id: 'id3', planet: 'Pluton' }
// Note that the _id is kept unchanged, and the document has been replaced
@@ -440,19 +399,19 @@ db.update({planet: 'Jupiter'}, {planet: 'Pluton'}, {}, function (err:Error, numR
});
// Set an existing field's value
db.update({system: 'solar'}, {$set: {system: 'solar system'}}, {multi: true}, function (err:Error, numReplaced:number) {
db.update({system: 'solar'}, {$set: {system: 'solar system'}}, {multi: true}, (err: Error, numReplaced: number) => {
// numReplaced = 3
// Field 'system' on Mars, Earth, Jupiter now has value 'solar system'
});
// Setting the value of a non-existing field in a subdocument by using the dot-notation
db.update({planet: 'Mars'}, {$set: {"data.satellites": 2, "data.red": true}}, {}, function () {
db.update({planet: 'Mars'}, {$set: {"data.satellites": 2, "data.red": true}}, {}, () => {
// Mars document now is { _id: 'id1', system: 'solar', inhabited: false
// , data: { satellites: 2, red: true }
// }
// Not that to set fields in subdocuments, you HAVE to use dot-notation
// Using object-notation will just replace the top-level field
db.update({planet: 'Mars'}, {$set: {data: {satellites: 3}}}, {}, function () {
db.update({planet: 'Mars'}, {$set: {data: {satellites: 3}}}, {}, () => {
// Mars document now is { _id: 'id1', system: 'solar', inhabited: false
// , data: { satellites: 3 }
// }
@@ -461,7 +420,7 @@ db.update({planet: 'Mars'}, {$set: {"data.satellites": 2, "data.red": true}}, {}
});
// Deleting a field
db.update({planet: 'Mars'}, {$unset: {planet: true}}, {}, function () {
db.update({planet: 'Mars'}, {$unset: {planet: true}}, {}, () => {
// Now the document for Mars doesn't contain the planet field
// You can unset nested fields with the dot notation of course
});
@@ -470,14 +429,14 @@ db.update({planet: 'Mars'}, {$unset: {planet: true}}, {}, function () {
db.update({planet: 'Pluton'}, {
planet: 'Pluton',
inhabited: false
}, {upsert: true}, function (err:Error, numReplaced:number, upsert:boolean) {
}, {upsert: true}, (err: Error, numReplaced: number, upsert: boolean) => {
// numReplaced = 1, upsert = { _id: 'id5', planet: 'Pluton', inhabited: false }
// A new document { _id: 'id5', planet: 'Pluton', inhabited: false } has been added to the collection
});
// If you upsert with a modifier, the upserted doc is the query modified by the modifier
// This is simpler than it sounds :)
db.update({planet: 'Pluton'}, {$inc: {distance: 38}}, {upsert: true}, function () {
db.update({planet: 'Pluton'}, {$inc: {distance: 38}}, {upsert: true}, () => {
// A new document { _id: 'id5', planet: 'Pluton', distance: 38 } has been added to the collection
});
@@ -485,12 +444,12 @@ db.update({planet: 'Pluton'}, {$inc: {distance: 38}}, {upsert: true}, function (
// let's see how we can modify the array field atomically
// $push inserts new elements at the end of the array
db.update({_id: 'id6'}, {$push: {fruits: 'banana'}}, {}, function () {
db.update({_id: 'id6'}, {$push: {fruits: 'banana'}}, {}, () => {
// Now the fruits array is ['apple', 'orange', 'pear', 'banana']
});
// $pop removes an element from the end (if used with 1) or the front (if used with -1) of the array
db.update({_id: 'id6'}, {$pop: {fruits: 1}}, {}, function () {
db.update({_id: 'id6'}, {$pop: {fruits: 1}}, {}, () => {
// Now the fruits array is ['apple', 'orange']
// With { $pop: { fruits: -1 } }, it would have been ['orange', 'pear']
});
@@ -498,23 +457,23 @@ db.update({_id: 'id6'}, {$pop: {fruits: 1}}, {}, function () {
// $addToSet adds an element to an array only if it isn't already in it
// Equality is deep-checked (i.e. $addToSet will not insert an object in an array already containing the same object)
// Note that it doesn't check whether the array contained duplicates before or not
db.update({_id: 'id6'}, {$addToSet: {fruits: 'apple'}}, {}, function () {
db.update({_id: 'id6'}, {$addToSet: {fruits: 'apple'}}, {}, () => {
// The fruits array didn't change
// If we had used a fruit not in the array, e.g. 'banana', it would have been added to the array
});
// $pull removes all values matching a value or even any NeDB query from the array
db.update({_id: 'id6'}, {$pull: {fruits: 'apple'}}, {}, function () {
db.update({_id: 'id6'}, {$pull: {fruits: 'apple'}}, {}, () => {
// Now the fruits array is ['orange', 'pear']
});
db.update({_id: 'id6'}, {$pull: {fruits: {$in: ['apple', 'pear']}}}, {}, function () {
db.update({_id: 'id6'}, {$pull: {fruits: {$in: ['apple', 'pear']}}}, {}, () => {
// Now the fruits array is ['orange']
});
// $each can be used to $push or $addToSet multiple values at once
// This example works the same way with $addToSet
db.update({_id: 'id6'}, {$push: {fruits: {$each: ['banana', 'orange']}}}, {}, function () {
db.update({_id: 'id6'}, {$push: {fruits: {$each: ['banana', 'orange']}}}, {}, () => {
// Now the fruits array is ['apple', 'orange', 'pear', 'banana', 'orange']
});
@@ -526,33 +485,33 @@ db.update({_id: 'id6'}, {$push: {fruits: {$each: ['banana', 'orange']}}}, {}, fu
// Remove one document from the collection
// options set to {} since the default for multi is false
db.remove({_id: 'id2'}, {}, function (err:Error, numRemoved:number) {
db.remove({_id: 'id2'}, {}, (err: Error, numRemoved: number) => {
// numRemoved = 1
});
// Remove multiple documents
db.remove({system: 'solar'}, {multi: true}, function (err:Error, numRemoved:number) {
db.remove({system: 'solar'}, {multi: true}, (err: Error, numRemoved: number) => {
// numRemoved = 3
// All planets from the solar system were removed
});
db.ensureIndex({fieldName: 'somefield'}, function (err:Error) {
db.ensureIndex({fieldName: 'somefield'}, (err: Error) => {
// If there was an error, err is not null
});
// Using a unique constraint with the index
db.ensureIndex({fieldName: 'somefield', unique: true}, function (err:Error) {
db.ensureIndex({fieldName: 'somefield', unique: true}, (err: Error) => {
});
// Using a sparse unique index
db.ensureIndex({fieldName: 'somefield', unique: true, sparse: true}, function (err:Error) {
db.ensureIndex({fieldName: 'somefield', unique: true, sparse: true}, (err: Error) => {
});
// Format of the error message when the unique constraint is not met
db.insert({somefield: 'nedb'}, function (err:Error) {
db.insert({somefield: 'nedb'}, (err: Error) => {
// err is null
db.insert({somefield: 'nedb'}, function (err:Error) {
db.insert({somefield: 'nedb'}, (err: Error) => {
// err is { errorType: 'uniqueViolated'
// , key: 'name'
// , message: 'Unique constraint violated for key name' }
@@ -560,5 +519,5 @@ db.insert({somefield: 'nedb'}, function (err:Error) {
});
// Remove index on field somefield
db.removeIndex('somefield', function (err:Error) {
db.removeIndex('somefield', (err: Error) => {
});

3
nedb/tslint.json Normal file
View File

@@ -0,0 +1,3 @@
{
"extends": "../tslint.json"
}