0.13.0 • Published 2 days ago

@sidewinder/mongo v0.13.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 days ago

Overview

This package provides a thin type safe layer over the official mongodb driver package for NodeJS. It enables document models to be created with Sidewinder Types with each document strictly data checked via JSON schema prior to writing to MongoDB. In addition, this package provides automatic ObjectId and binary data encode and decode to and from MongoDB. This allows applications to treat Mongo identifiers as validated hex strings and Mongo Binary objects as Uint8Array.

License MIT

Contents

Example

TypeScript Example Link

The Sidewinder MongoDatabase provides a strict subset of the Mongo Db API. It accepts a Database schema as it's first argument and Db instance for its second. Callers can access a Type Safe Collection API that automatically validates documents based on the Sidewinder Types defined in the Database schema. For indexing MongoDB and specifying other configuration options, use the Db object.

import { Type, MongoDatabase } from '@sidewinder/mongo'
import { MongoClient } from 'mongodb'

// ---------------------------------------------------------
// MongoClient
// ---------------------------------------------------------

const client = new MongoClient('mongodb://localhost:27017/db')
await client.connect()

// ---------------------------------------------------------
// Database Schematic
// ---------------------------------------------------------

const User = Type.Object({
  _id: Type.ObjectId(),
  name: Type.String(),
  email: Type.String({ format: 'email' }),
  created: Type.Integer(),
  updated: Type.Integer(),
})

const Record = Type.Object({
  _id: Type.ObjectId(),
  _user_id: Type.ObjectId(),
  created: Type.Integer(),
  updated: Type.Integer(),
  value: Type.Number(),
})

const Schema = Type.Database({
  users: User,
  records: Record,
})

// ---------------------------------------------------------
// Database
// ---------------------------------------------------------

const database = new MongoDatabase(Schema, client.db())

// ---------------------------------------------------------
// Insert
// ---------------------------------------------------------

const user_id = database.id()

await database.collection('users').insertOne({
  _id: user_id,
  name: 'dave',
  email: 'dave@domain.com',
  created: Date.now(),
  updated: Date.now(),
})

await database.collection('records').insertMany([
  {
    _id: database.id(),
    _user_id: user_id,
    created: Date.now(),
    updated: Date.now(),
    value: 0,
  },
  {
    _id: database.id(),
    _user_id: user_id,
    created: Date.now(),
    updated: Date.now(),
    value: 1,
  },
])

// ---------------------------------------------------------
// Update
// ---------------------------------------------------------

await database.collection('users').updateOne(
  { _id: user_id },
  {
    email: 'dave@other-domain.com',
    updated: Date.now(),
  },
)

// ---------------------------------------------------------
// Find
// ---------------------------------------------------------

const user = await database.collection('users').findOne({ _id: user_id })

// ---------------------------------------------------------
// Iterate
// ---------------------------------------------------------

for await (const user of database.collection('users').find({}).skip(0).take(10)) {
  // ...
}

// ---------------------------------------------------------
// Delete
// ---------------------------------------------------------

await database.collection('users').deleteOne({ _id: user_id })

ObjectId

Sidewinder Mongo does not use the MongoDB ObjectId to read and write _id fields to Mongo. Rather it detects the format of string values and automatically encodes to and from ObjectId. This allows ObjectId values to be transmitted across a network as strings without additional encoding to and from this type.

const User = Type.Object({
  _id:  Type.ObjectId() // Is a string regex string validated for 24 character hex strings values
  name: Type.String()
})

const Schema = Type.Database({
  users: User
})

const database = new MongoDatabase(Schema, Db)

database.collection('users').insertOne({
  _id: database.id(),  // Generates a new 24 character hex string
  name: 'dave'
})

Uint8Array

Sidewinder Mongo supports automatic encode and decode of JavaScript Uint8Array buffers only. This can be used to read and write binary property values into Mongo.

const ImageSegment = Type.Object({
  _id: Type.ObjectId()    // Is a string regex string validated for 24 character hex strings values
  data: Type.Uint8Array()
})
const Schema = Type.Database({
  imageSegments: ImageSegment
})

const database = new MongoDatabase(Schema, Db)

database.collection('imageSegments').insertOne({
  _id:  database.id(),        // Generates a new 24 character hex string
  data: new Uint8Array(16384) // 16K image segment
})
0.13.0

2 days ago

0.12.10

10 months ago

0.12.11

10 months ago

0.12.12

10 months ago

0.12.14

10 months ago

0.12.15

10 months ago

0.12.8

11 months ago

0.12.9

11 months ago

0.12.7

11 months ago

0.12.6

11 months ago

0.12.5

1 year ago

0.11.0

2 years ago

0.10.14

2 years ago

0.11.1

2 years ago

0.10.15

2 years ago

0.11.2

2 years ago

0.11.3

2 years ago

0.11.4

2 years ago

0.10.10

2 years ago

0.11.5

2 years ago

0.10.11

2 years ago

0.11.6

1 year ago

0.10.12

2 years ago

0.10.13

2 years ago

0.10.9

2 years ago

0.10.1

2 years ago

0.12.0

1 year ago

0.10.2

2 years ago

0.12.1

1 year ago

0.10.3

2 years ago

0.12.2

1 year ago

0.10.4

2 years ago

0.12.3

1 year ago

0.10.5

2 years ago

0.12.4

1 year ago

0.10.6

2 years ago

0.10.7

2 years ago

0.10.8

2 years ago

0.10.0

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.8.85

2 years ago

0.8.84

2 years ago

0.8.81

2 years ago

0.8.80

2 years ago

0.8.83

2 years ago

0.8.82

2 years ago

0.8.78

2 years ago

0.8.79

2 years ago

0.9.0

2 years ago

0.8.77

2 years ago

0.8.74

2 years ago

0.8.73

2 years ago

0.8.76

2 years ago

0.8.75

2 years ago

0.8.70

2 years ago

0.8.72

2 years ago

0.8.71

2 years ago

0.8.67

2 years ago

0.8.66

2 years ago

0.8.69

2 years ago

0.8.68

2 years ago

0.8.63

2 years ago

0.8.62

2 years ago

0.8.65

2 years ago

0.8.64

2 years ago

0.8.61

2 years ago

0.8.60

2 years ago

0.8.59

2 years ago

0.8.56

2 years ago

0.8.58

2 years ago

0.8.57

2 years ago

0.8.45

2 years ago

0.8.44

2 years ago

0.8.47

2 years ago

0.8.46

2 years ago

0.8.49

2 years ago

0.8.48

2 years ago

0.8.55

2 years ago

0.8.52

2 years ago

0.8.51

2 years ago

0.8.54

2 years ago

0.8.53

2 years ago

0.8.50

2 years ago

0.8.43

2 years ago

0.8.42

2 years ago

0.8.41

2 years ago

0.8.40

2 years ago

0.8.39

2 years ago

0.8.38

2 years ago

0.8.37

2 years ago

0.8.36

2 years ago

0.8.35

2 years ago

0.8.34

2 years ago

0.8.33

2 years ago

0.8.32

2 years ago

0.8.31

2 years ago

0.8.30

2 years ago

0.8.29

2 years ago