This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var request = require('request'); | |
var addrs = [ | |
'http://127.0.0.1:3000', | |
'http://127.0.0.1:3001' | |
]; | |
var index = 0; | |
http.createServer(function(req, res) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MongoDB 2.6, Linux Mint 17.1, node driver HEAD | |
/* Output looks like: | |
``` | |
nextObject returned | |
Calling nextObject again... | |
Calling execGetMore | |
``` | |
The `console.log('second nextObject returned');` line never gets executed and the program exits because of no listeners |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Archetype = require('archetype'); | |
const assert = require('assert'); | |
const RequestType = new Archetype({ | |
service: { | |
$type: 'string', | |
$required: true | |
}, | |
// `location` is implicitly `$required` because | |
// `type` and `coordinates` are `$required` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Archetype = require('archetype'); | |
const express = require('express'); | |
const superagent = require('superagent'); | |
const app = express(); | |
const Params = new Archetype({ | |
names: { $type: ['string'], $default: [] } | |
}).compile('Params'); | |
app.get('/', function(req, res) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Archetype = require('archetype'); | |
// `Str` is a utility type for strings unrelated | |
// to archetype: https://www.npmjs.com/package/string | |
// It provides several convenient string utilities, | |
// including `.capitalize()`. | |
const Str = require('string'); | |
const NameType = new Archetype({ | |
// Archetype custom types are trivial: any function, | |
// including any class, is a viable `$type`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Archetype = require('archetype'); | |
// Simplified customer type for the sake of example. This customer | |
// type has 2 top-level properties: a string `email` and an object | |
// `name` that has nested string properties `first` and `last`. | |
const CustomerType = new Archetype({ | |
name: { | |
first: { $type: 'string' }, | |
last: { $type: 'string' } | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Archetype = require('archetype'); | |
const CustomerType = new Archetype({ | |
roles: { | |
$type: ['string'], | |
$enum: ['CUSTOMER', 'ADMIN'], | |
$required: true, | |
$default: ['CUSTOMER'] | |
}, | |
email: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Archetype = require('archetype'); | |
const assert = require('assert'); | |
// The 2nd param to the `$validate` function is the schema path, | |
// so `{ $type: 'number', ... }`. Can use that to configure | |
// validators from your schema path, like writing your own | |
// min/max validator | |
const minMax = (v, path) => { | |
const { $min, $max } = path; | |
assert.ok($min == null || v >= $min, `${v} < ${$min}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Archetype = require('archetype'); | |
const UserType = new Archetype({ | |
name: { $type: 'string' }, | |
age: { $type: 'number' } | |
}).compile('UserType'); | |
// Prints 'age: Could not cast "not a number" to number' | |
throwError().catch(error => console.error(error.message)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Archetype = require('archetype'); | |
const UserType = new Archetype({ | |
name: { $type: 'string', $required: true }, | |
age: { $type: 'number', $required: true } | |
}).compile('UserType'); | |
const RequestType = new Archetype({ | |
createdAt: { $type: Date }, | |
user: { |
OlderNewer