Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lostintime/io-ts-bson
io-ts codecs for bson types
https://github.com/lostintime/io-ts-bson
Last synced: 23 days ago
JSON representation
io-ts codecs for bson types
- Host: GitHub
- URL: https://github.com/lostintime/io-ts-bson
- Owner: lostintime
- License: mit
- Created: 2022-11-04T11:15:43.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2022-11-07T18:32:13.000Z (about 2 years ago)
- Last Synced: 2024-10-09T18:19:58.900Z (about 1 month ago)
- Language: TypeScript
- Size: 174 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `io-ts` codecs for `bson` types
This package provides [io-ts](https://github.com/gcanti/io-ts) codecs for [bson](https://github.com/mongodb/js-bson) data types.
## Installation
```sh
npm install --save io-ts-bson
```## Usage
Database entities may be defined using the `io-ts-bson` types and then decoded/encoded.
```ts
import * as bson from 'bson';
import * as t from 'io-ts';
import * as bt from 'io-ts-bson';export const UserProfile = t.type(
{
_id: bt.ObjectId,
name: t.string,
createdAt: bt.date,
},
'UserProfile',
);
export type UserProfile = t.TypeOf;export const loadUserProfile = async (id: string) => {
const user = await db.collection('users').findOne({
_id: bson.ObjectId.createFromHexString(id),
});return UserProfile.decode(user);
};
```