-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- *Before creating an issue please make sure you are using the latest version of mongoose --> | ||
|
||
**Do you want to request a *feature* or report a *bug*?** | ||
|
||
**What is the current behavior?** | ||
|
||
**If the current behavior is a bug, please provide the steps to reproduce.** | ||
|
||
**What is the expected behavior?** | ||
|
||
**What are the versions of Node.js, mongoose-lean-getters, and Mongoose are you are using? Note that "latest" is not a version.** | ||
|
||
<!-- You can print `mongoose.version` to get your current version of Mongoose: https://mongoosejs.com/docs/api.html#mongoose_Mongoose-version --> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. The two fields below are mandatory. | ||
If you're making a change to documentation, do **not** modify a `.html` file directly. Instead find the corresponding `.pug` file or test case in the `test/docs` directory. --> | ||
|
||
**Summary** | ||
|
||
<!-- Explain the **motivation** for making this change. What problem does the pull request solve? --> | ||
|
||
**Examples** | ||
|
||
<!-- If this code fixes a bug or adds a new feature, provide an example demonstrating the change, unless you added a test. --> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: Test | ||
on: | ||
pull_request: | ||
push: | ||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
test: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
node: [14, 16, 18] | ||
os: [ubuntu-20.04] | ||
include: | ||
- os: ubuntu-20.04 | ||
mongo-os: ubuntu2004 | ||
mongo: 5.0.2 | ||
name: Node ${{ matrix.node }} MongoDB ${{ matrix.mongo }} | ||
steps: | ||
- uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # v3 | ||
|
||
- name: Setup node | ||
uses: actions/setup-node@5b52f097d36d4b0b2f94ed6de710023fbb8b2236 # v3.1.0 | ||
with: | ||
node-version: ${{ matrix.node }} | ||
|
||
- run: npm install | ||
|
||
- name: Setup | ||
run: | | ||
wget -q https://downloads.mongodb.org/linux/mongodb-linux-x86_64-${{ matrix.mongo-os }}-${{ matrix.mongo }}.tgz | ||
tar xf mongodb-linux-x86_64-${{ matrix.mongo-os }}-${{ matrix.mongo }}.tgz | ||
mkdir -p ./data/db/27017 ./data/db/27000 | ||
printf "\n--timeout 8000" >> ./test/mocha.opts | ||
./mongodb-linux-x86_64-${{ matrix.mongo-os }}-${{ matrix.mongo }}/bin/mongod --setParameter ttlMonitorSleepSecs=1 --fork --dbpath ./data/db/27017 --syslog --port 27017 | ||
sleep 2 | ||
mongod --version | ||
echo `pwd`/mongodb-linux-x86_64-${{ matrix.mongo-os }}-${{ matrix.mongo }}/bin >> $GITHUB_PATH | ||
- run: npm test |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,25 @@ | ||
# mongoose-bson-timestamps | ||
Implementation of BSON timestamp type for Mongoose | ||
|
||
## Usage | ||
|
||
First, `npm install @mongoosejs/bson-timestamp` and import this package. | ||
|
||
```javascript | ||
// Using Node.js `require()` | ||
const Timestamp = require('@mongoosejs/bson-timestamp'); | ||
|
||
// Using ES6 imports | ||
import Timestamp from '@mongoosejs/bson-timestamp'; | ||
``` | ||
|
||
You can then use `Timestamp` as a schema type in your schema definitions. | ||
|
||
```javascript | ||
const Timestamp = require('@mongoosejs/bson-timestamp'); | ||
const mongoose = require('mongoose'); | ||
|
||
const schema = new mongoose.Schema({ | ||
myTimestamp: { type: Timestamp } | ||
}); | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
const { SchemaType, Schema, Types } = require('mongoose'); | ||
const bson = require('bson'); | ||
|
||
// Define the custom Timestamp type | ||
class Timestamp extends SchemaType { | ||
constructor(key, options) { | ||
super(key, options, 'Timestamp'); | ||
} | ||
|
||
// Cast the value to a BSON Timestamp | ||
cast(val) { | ||
if (val instanceof bson.Timestamp) { | ||
return val; | ||
} | ||
|
||
if (typeof val === 'object' && val != null && val.hasOwnProperty('t') && val.hasOwnProperty('i')) { | ||
return new bson.Timestamp(val); | ||
} | ||
if (typeof val === 'bigint') { | ||
return new bson.Timestamp(val); | ||
} | ||
|
||
throw new Error('Value is not a valid BSON Timestamp or compatible object'); | ||
} | ||
} | ||
|
||
Schema.Types.Timestamp = Timestamp; | ||
Types.Timestamp = Timestamp; | ||
|
||
module.exports = Timestamp; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@mongoosejs/bson-timestamp", | ||
"version": "0.1.0", | ||
"description": "Implementation of BSON timestamp type for Mongoose", | ||
"main": "index.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "mocha ./test/*.test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/mongoosejs/mongoose-bson-timestamps.git" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"mocha": "10.2.0", | ||
"mongoose": "8.x" | ||
}, | ||
"peerDependencies": { | ||
"mongoose": "8.x", | ||
"bson": "6.x" | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
|
||
const Timestamp = require('../'); | ||
const assert = require('assert'); | ||
const bson = require('bson'); | ||
const mongoose = require('mongoose'); | ||
|
||
describe('Double', function() { | ||
let Model; | ||
|
||
before(function() { | ||
mongoose.connect('mongodb://127.0.0.1:27017/timestamps_test'); | ||
const schema = new mongoose.Schema({ ts: Timestamp }); | ||
Model = mongoose.model('Test', schema); | ||
}); | ||
|
||
after(function() { | ||
return mongoose.disconnect(); | ||
}); | ||
|
||
describe('casts', function() { | ||
it('bson timestamp instances', async function() { | ||
const ts = new bson.Timestamp(1444n); | ||
const doc = new Model({ ts }); | ||
assert.strictEqual(doc.ts, ts); | ||
|
||
await doc.validate(); | ||
}); | ||
|
||
it('bigint', async function() { | ||
const ts = 8472n; | ||
const doc = new Model({ ts }); | ||
assert.strictEqual(doc.ts.toString(), '8472'); | ||
|
||
await doc.validate(); | ||
}); | ||
|
||
it('object', async function() { | ||
const ts = { t: 1, i: 2 }; | ||
const doc = new Model({ ts }); | ||
assert.strictEqual(doc.ts.toString(), '4294967298'); | ||
|
||
await doc.validate(); | ||
}); | ||
|
||
it('null', async function() { | ||
const doc = new Model({ ts: null }); | ||
assert.strictEqual(doc.ts, null); | ||
|
||
await doc.validate(); | ||
}); | ||
|
||
it('handles cast error', async function() { | ||
const doc = new Model({ ts: 'taco tuesday' }); | ||
assert.strictEqual(doc.ts, undefined); | ||
|
||
const err = await doc.validate().then(() => null, err => err); | ||
assert.ok(err); | ||
assert.ok(err.errors['ts']); | ||
assert.equal(err.errors['ts'].constructor.name, 'CastError'); | ||
}); | ||
}); | ||
|
||
it('saves correctly', async function() { | ||
const doc = new Model({ ts: 8472n }); | ||
await doc.save(); | ||
|
||
const { ts } = await Model.findById(doc._id).lean().orFail(); | ||
assert.strictEqual(ts.constructor.name, 'Timestamp'); | ||
assert.strictEqual(ts.toString(), '8472'); | ||
}); | ||
}); |