xpress-mongo
Advanced tools
Comparing version 0.0.45 to 0.0.46
@@ -6,2 +6,3 @@ "use strict"; | ||
const mongodb_1 = require("mongodb"); | ||
const uuid = require("uuid"); | ||
const isString = (v) => typeof v === 'string'; | ||
@@ -48,2 +49,27 @@ const isBoolean = (v) => typeof v === 'boolean'; | ||
/** | ||
* Uuid | ||
* @param version - version of uuid | ||
* @param options - options of uuid version 3 or 5 | ||
*/ | ||
Uuid: (version = 4, options) => { | ||
if (![1, 3, 4, 5].includes(version)) { | ||
throw Error("Uuid version argument expects 1, 3, 4 or 5!"); | ||
} | ||
if ([3, 5].includes(version) && !options) { | ||
throw Error(`Uuid version (${version}) requires {name, namespace} options!`); | ||
} | ||
return new XMongoDataType('Uuid').validator((value) => uuid.validate(value)).default(() => { | ||
switch (version) { | ||
case 1: | ||
return uuid.v1(); | ||
case 3: | ||
return uuid.v3(options.name, options.namespace); | ||
case 4: | ||
return uuid.v4(); | ||
case 5: | ||
return uuid.v5(options.name, options.namespace); | ||
} | ||
}); | ||
}, | ||
/** | ||
* Array | ||
@@ -50,0 +76,0 @@ * @param def |
{ | ||
"name": "xpress-mongo", | ||
"version": "0.0.45", | ||
"version": "0.0.46", | ||
"description": "Light Weight ODM for mongoDb", | ||
@@ -11,3 +11,4 @@ "main": "js/index.js", | ||
"scripts": { | ||
"test": "nodemon tests/test.js" | ||
"test": "nodemon tests/test.js", | ||
"watch": "tsc --watch" | ||
}, | ||
@@ -31,3 +32,4 @@ "repository": { | ||
"mongodb": "^3.6.2", | ||
"object-collection": "^1.0.29" | ||
"object-collection": "^1.0.29", | ||
"uuid": "^8.3.1" | ||
}, | ||
@@ -37,2 +39,3 @@ "devDependencies": { | ||
"@types/node": "^14.11.2", | ||
"@types/uuid": "^8.3.0", | ||
"chance": "^1.1.7", | ||
@@ -39,0 +42,0 @@ "typescript": "^4.0.3" |
import XMongoDataType = require("./XMongoDataType"); | ||
import {ObjectID} from "mongodb"; | ||
import {StringToAnyObject} from "./CustomTypes"; | ||
import uuid = require("uuid"); | ||
@@ -22,13 +23,28 @@ | ||
type XMongoSchemaBuilder = { | ||
ObjectId: () => XMongoDataType, | ||
Array: { (def?: () => Array<any>): XMongoDataType }, | ||
Object: { (def?: () => StringToAnyObject): XMongoDataType }, | ||
String: { (def?: string): XMongoDataType }, | ||
Boolean: { (def?: boolean): XMongoDataType }, | ||
Date: { (def?: () => Date): XMongoDataType }, | ||
Number: { (def?: 0): XMongoDataType }, | ||
Types: { (types: XMongoDataType[]): XMongoDataType }, | ||
type InputBuffer = ArrayLike<number>; | ||
type UuidOptions = { | ||
name: string | InputBuffer, | ||
namespace: string | InputBuffer | ||
}; | ||
interface XMongoSchemaBuilder { | ||
ObjectId(): XMongoDataType | ||
Uuid(version: number, options?: UuidOptions): XMongoDataType | ||
Array(def?: () => Array<any>): XMongoDataType | ||
Object(def?: () => StringToAnyObject): XMongoDataType | ||
String(def?: string): XMongoDataType | ||
Boolean(def?: boolean): XMongoDataType | ||
Date(def?: () => Date): XMongoDataType | ||
Number(def?: 0): XMongoDataType | ||
Types(types: XMongoDataType[]): XMongoDataType | ||
} | ||
const is: XMongoSchemaBuilder = { | ||
@@ -60,2 +76,30 @@ /** | ||
/** | ||
* Uuid | ||
* @param version - version of uuid | ||
* @param options - options of uuid version 3 or 5 | ||
*/ | ||
Uuid: (version: (1 | 3 | 4 | 5 | number) = 4, options?: UuidOptions): XMongoDataType => { | ||
if (![1, 3, 4, 5].includes(version)) { | ||
throw Error("Uuid version argument expects 1, 3, 4 or 5!") | ||
} | ||
if ([3, 5].includes(version) && !options) { | ||
throw Error(`Uuid version (${version}) requires {name, namespace} options!`) | ||
} | ||
return new XMongoDataType('Uuid').validator((value) => uuid.validate(value)).default(() => { | ||
switch (version) { | ||
case 1: | ||
return uuid.v1(); | ||
case 3: | ||
return uuid.v3((options as UuidOptions).name, (options as UuidOptions).namespace); | ||
case 4: | ||
return uuid.v4() | ||
case 5: | ||
return uuid.v5((options as UuidOptions).name, (options as UuidOptions).namespace) | ||
} | ||
}) | ||
}, | ||
/** | ||
* Array | ||
@@ -62,0 +106,0 @@ * @param def |
@@ -32,2 +32,3 @@ const {is, ModelDataType} = require('../'); | ||
const GuestSchema = { | ||
code: is.Uuid().isOptional(), | ||
type: is.String().required(), | ||
@@ -45,2 +46,3 @@ first_name: is.String().required(), | ||
class Users extends Database.model("users") { | ||
@@ -47,0 +49,0 @@ |
@@ -7,7 +7,7 @@ const connection = require('./connection'); | ||
const user = await Users.findById('5e5acba088ebeef8a715ca43', { | ||
/*const user = await Users.findById('5e5acba088ebeef8a715ca43', { | ||
projection: {_id: 0} | ||
}); | ||
console.log(user); | ||
console.log(user);*/ | ||
@@ -17,3 +17,3 @@ /** | ||
*/ | ||
/*const guest = new Users().set({ | ||
const guest = new Users().set({ | ||
type: 'guest', | ||
@@ -27,3 +27,3 @@ first_name: 'Hello', | ||
// console.log(guest.data); | ||
console.log(guest.validate());*/ | ||
console.log(guest.validate()); | ||
@@ -30,0 +30,0 @@ |
import XMongoDataType = require("./XMongoDataType"); | ||
import { StringToAnyObject } from "./CustomTypes"; | ||
declare type XMongoSchemaBuilder = { | ||
ObjectId: () => XMongoDataType; | ||
Array: { | ||
(def?: () => Array<any>): XMongoDataType; | ||
}; | ||
Object: { | ||
(def?: () => StringToAnyObject): XMongoDataType; | ||
}; | ||
String: { | ||
(def?: string): XMongoDataType; | ||
}; | ||
Boolean: { | ||
(def?: boolean): XMongoDataType; | ||
}; | ||
Date: { | ||
(def?: () => Date): XMongoDataType; | ||
}; | ||
Number: { | ||
(def?: 0): XMongoDataType; | ||
}; | ||
Types: { | ||
(types: XMongoDataType[]): XMongoDataType; | ||
}; | ||
declare type InputBuffer = ArrayLike<number>; | ||
declare type UuidOptions = { | ||
name: string | InputBuffer; | ||
namespace: string | InputBuffer; | ||
}; | ||
interface XMongoSchemaBuilder { | ||
ObjectId(): XMongoDataType; | ||
Uuid(version: number, options?: UuidOptions): XMongoDataType; | ||
Array(def?: () => Array<any>): XMongoDataType; | ||
Object(def?: () => StringToAnyObject): XMongoDataType; | ||
String(def?: string): XMongoDataType; | ||
Boolean(def?: boolean): XMongoDataType; | ||
Date(def?: () => Date): XMongoDataType; | ||
Number(def?: 0): XMongoDataType; | ||
Types(types: XMongoDataType[]): XMongoDataType; | ||
} | ||
declare const is: XMongoSchemaBuilder; | ||
export { is, XMongoSchemaBuilder }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
130073
3905
5
5
+ Addeduuid@^8.3.1
+ Addeduuid@8.3.2(transitive)