Skip to content

Instantly share code, notes, and snippets.

View vkarpov15's full-sized avatar

Valeri Karpov vkarpov15

View GitHub Profile
@vkarpov15
vkarpov15 / gist:c58b1cabb5250e4c999c
Created August 25, 2014 21:55
15 line (but not durable) HTTP load balancer with NodeJS
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) {
@vkarpov15
vkarpov15 / hanging_nextobject
Created January 13, 2015 21:03
Hanging nextObject() call
// 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
@vkarpov15
vkarpov15 / archetype-20171013-embedded-object.js
Created October 13, 2017 17:24
Embedded Objects vs Embedded Types in Archetype
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`
@vkarpov15
vkarpov15 / example.js
Last active October 26, 2017 22:55
Consistent Arrays from Query Params with Express and Archetype
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) {
@vkarpov15
vkarpov15 / archetype-20171028-custom-types.js
Last active October 28, 2017 08:16
Custom Types with Archetype
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`.
@vkarpov15
vkarpov15 / archetype-20171020-pick.js
Created October 20, 2017 23:59
Embedding a Subset of One Archetype's Properties in another Archetype
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' }
},
@vkarpov15
vkarpov15 / archetype-20171103-conditional-required.js
Created November 3, 2017 23:46
Conditionally Required Properties with Archetype
const Archetype = require('archetype');
const CustomerType = new Archetype({
roles: {
$type: ['string'],
$enum: ['CUSTOMER', 'ADMIN'],
$required: true,
$default: ['CUSTOMER']
},
email: {
@vkarpov15
vkarpov15 / archetype-20171110-validate-config.js
Created November 11, 2017 01:05
Configuring Custom Validators for Individual Paths in Archetype
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}`);
@vkarpov15
vkarpov15 / archetype-20171117-async-await.js
Created November 18, 2017 01:32
Async/Await with Archetype
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));
@vkarpov15
vkarpov15 / archetype-20171124-embedding.js
Created November 24, 2017 19:45
Embedding Only Certain Fields in an Embedded Archetype
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: {