├── .gitignore ├── README.md ├── package.json ├── index.js └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Usage 2 | ``` 3 | import bsf from 'bson-schema-faker' 4 | 5 | fakeDoc = bsf(schema) 6 | ``` 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bson-schema-faker", 3 | "version": "1.0.1", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/poetic/bson-schema-faker.git" 12 | }, 13 | "author": "", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/poetic/bson-schema-faker/issues" 17 | }, 18 | "homepage": "https://github.com/poetic/bson-schema-faker#readme", 19 | "dependencies": { 20 | "bson": "^0.5.5" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | "object", 3 | "array", 4 | "string", 5 | "bool", 6 | "date", 7 | "timestamp", 8 | "int", 9 | "long", 10 | "double", 11 | "objectId", 12 | */ 13 | 14 | var bson = require('bson') 15 | 16 | function bsonSchemaFaker (schema) { 17 | switch (schema.type) { 18 | case 'object': 19 | var fakeObject = {} 20 | 21 | for (var key in schema.properties) { 22 | fakeObject[key] = bsonSchemaFaker(schema.properties[key]) 23 | } 24 | 25 | return fakeObject 26 | case 'array': 27 | return [bsonSchemaFaker(schema.items)] 28 | case 'string': 29 | return 'a' 30 | case 'bool': 31 | return true 32 | case 'date': 33 | return new Date(0) 34 | case 'timestamp': 35 | return bson.Timestamp.fromNumber(0) 36 | case 'int': 37 | return new bson.Int32(0) 38 | case 'long': 39 | return bson.Long.fromNumber(0) 40 | case 'double': 41 | return new bson.Double(0) 42 | case 'objectId': 43 | return bson.ObjectID('57fbc79335224114d6c0e98b') 44 | default: 45 | throw new Error(schema.type + ' is not recognized') 46 | } 47 | } 48 | 49 | module.exports = bsonSchemaFaker 50 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /* 2 | // "undefined", 3 | // "null", 4 | // "binData", 5 | // "regex", 6 | // "dbPointer", 7 | // "javascript", 8 | // "symbol", 9 | // "javascriptWithScope", 10 | "object", 11 | "array", 12 | "string", 13 | "bool", 14 | "date", 15 | "timestamp", 16 | "int", 17 | "long", 18 | "double", 19 | "objectId", 20 | // "minKey", 21 | // "maxKey", 22 | */ 23 | 24 | var assert = require('assert') 25 | var bsj = require('./index') 26 | var bson = require('bson') 27 | 28 | var testCases = [ 29 | { 30 | schema: { 31 | type: 'object', 32 | properties: { 33 | user: { 34 | type: 'object', 35 | properties: { 36 | string: { 37 | type: 'string' 38 | }, 39 | long: { 40 | type: 'long', 41 | }, 42 | int: { 43 | type: 'int', 44 | }, 45 | bool: { 46 | type: 'bool', 47 | } 48 | }, 49 | }, 50 | cars: { 51 | type: 'array', 52 | items: { 53 | type: 'object', 54 | properties: { 55 | string: { 56 | type: 'string' 57 | } 58 | } 59 | } 60 | } 61 | }, 62 | }, 63 | fakeDoc: { 64 | user: { 65 | string: 'a', 66 | long: bson.Long.fromInt(0), 67 | int: new bson.Int32(0), 68 | bool: true, 69 | }, 70 | cars: [ 71 | { 72 | string: 'a' 73 | } 74 | ] 75 | } 76 | } 77 | ] 78 | 79 | testCases.forEach(function(testCase) { 80 | assert.equal( 81 | JSON.stringify(bsj(testCase.schema), 2, null), 82 | JSON.stringify(testCase.fakeDoc, 2, null) 83 | ) 84 | }) 85 | --------------------------------------------------------------------------------