Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mirek/node-pg-safe-numbers
Safe number parsing for pg and Sequelize.
https://github.com/mirek/node-pg-safe-numbers
Last synced: about 1 month ago
JSON representation
Safe number parsing for pg and Sequelize.
- Host: GitHub
- URL: https://github.com/mirek/node-pg-safe-numbers
- Owner: mirek
- Created: 2015-10-29T02:58:42.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-08-10T01:19:30.000Z (over 1 year ago)
- Last Synced: 2024-10-03T02:14:18.308Z (3 months ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 3
- Watchers: 4
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
## Summary [![Build Status](https://travis-ci.org/mirek/node-pg-safe-numbers.png?branch=master)](https://travis-ci.org/mirek/node-pg-safe-numbers)
Safe number parsers for pg/sequelize. When unsafe parsing is detected, decision to handle this case is delegated to the
caller. Default action is to throw `TypeError` exception. You can use your handlers to return parsed value, original
string value, null etc.## Example
Setup your configuration:
// setup-pg-safe-numbers.js
import util from 'util';
import { pgSetTypeParsers } from 'pg-safe-numbers';// Setup parsers for unsafe numbers.
pgSetTypeParsers({// Handle unsafe integers, ie. >= Math.pow(2, 53)
unsafeInt(parsed, text) {
console.error(`Unsafe int ${util.inspect(text)}) parse to ${util.inspect(parsed)}.\n${new Error().stack}`);
return parsed;
},// Handle unsafe floats.
unsafeFloat(parsed, text) {
console.error(`Unsafe float ${util.inspect(text)}) parse to ${util.inspect(parsed)}.\n${new Error().stack}`);
return parsed;
}});
// Or simply to use default TypeError throw:
// pgSetTypeParsers();And:
// my.js
require('./setup-pg-safe-numbers'); // NOTE: Doesn't really need to be before importing sequelize.
import Sequelize from 'sequelize';
...If you're not on ES6 follow https://babeljs.io/docs/usage/polyfill and you can use this library from your ES5, grampa :)
## License
MIT