Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding types #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ typings/
# next.js build output
.next

lib/

Comment on lines +63 to +64
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depending on how it's deployed this might have to be removed

data/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
index.test.js
tsconfig.json
src/
4 changes: 3 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ComfyDB = require( "./index" );
//@ts-check
const ComfyDB = require( "./lib/index" );

async function testComfy() {
try {
Expand Down Expand Up @@ -101,6 +102,7 @@ ComfyMongo.on( "error", ( err ) => {
ComfyMongo.on( "ready", async () => {
console.log( "[ComfyDB] Ready..." );
await testComfy();
// @ts-expect-error missing definitions for `comfy-mongo`
ComfyMongo.shutdown();
});
ComfyMongo.on( "exit", ( code ) => {
Expand Down
275 changes: 0 additions & 275 deletions index.js

This file was deleted.

28 changes: 18 additions & 10 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ComfyDB = require( "./index" );
//@ts-check
const ComfyDB = require( "./lib/index" );

test( "connects to MongoDB", async () => {
await ComfyDB.Connect();
Expand All @@ -11,7 +12,7 @@ test( "gets undefined for non-existent values", async () => {

test( "saves and gets values", async () => {
await ComfyDB.Store( "test", { name: "Instafluff" } );
expect( ( await ComfyDB.Get( "test" ) ).name ).toBe( "Instafluff" );
expect( ( await ComfyDB.Get( "test" ) )?.name ).toBe( "Instafluff" );
});

test( "deletes value", async () => {
Expand All @@ -21,17 +22,17 @@ test( "deletes value", async () => {

test( "sets _id", async () => {
await ComfyDB.Store( "identifier", { name: "Instafluff" } );
expect( ( await ComfyDB.Get( "identifier" ) )._id ).toBeDefined();
expect( ( await ComfyDB.Get( "identifier" ) )?._id ).toBeDefined();
});

test( "adds createdAt", async () => {
await ComfyDB.Store( "created", { name: "Instafluff" } );
expect( ( await ComfyDB.Get( "created" ) ).createdAt ).toBeDefined();
expect( ( await ComfyDB.Get( "created" ) )?.createdAt ).toBeDefined();
});

test( "adds updatedAt", async () => {
await ComfyDB.Store( "updated", { name: "Instafluff" } );
expect( ( await ComfyDB.Get( "updated" ) ).updatedAt ).toBeDefined();
expect( ( await ComfyDB.Get( "updated" ) )?.updatedAt ).toBeDefined();
});

test( "search returns all results", async () => {
Expand All @@ -40,7 +41,7 @@ test( "search returns all results", async () => {
}
let results = await ComfyDB.Search();
for( let i = 0; i < 5; i++ ) {
expect( ( await ComfyDB.Get( `test${i}` ) ).value ).toBe( `test${i}` );
expect( ( await ComfyDB.Get( `test${i}` ) )?.value ).toBe( `test${i}` );
}
});

Expand Down Expand Up @@ -110,15 +111,22 @@ test( "count is accurate", async () => {
expect( result ).toBe( 10 );
});

/**
* @typedef TestValue
* @property {string} key
* @property {number} value
*/

Comment on lines +114 to +119
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is the best option, but its the one i found for jsdoc, it comes along with:

let results = /** @type {TestValue[]} */ ( await ComfyDB.Search( { where: { key: { contains: "increment" } } } ) );

test( "increment adds amount", async () => {
for( let i = 0; i < 10; i++ ) {
await ComfyDB.Store( `skipincrement${i}`, { value: 5 } );
}
for( let i = 0; i < 5; i++ ) {
await ComfyDB.Store( `increment${i}`, { value: 5 } );
}
await ComfyDB.Increment( "value", { by: 5, where: { key: { startsWith: "increment" } } } );
let results = await ComfyDB.Search( { where: { key: { contains: "increment" } } } );
await ComfyDB.Increment( "value", { where: { key: { startsWith: "increment" } }, by: 5, } );

let results = /** @type {TestValue[]} */ ( await ComfyDB.Search( { where: { key: { contains: "increment" } } } ) );
expect( results.length ).toBe( 15 );
for( let i = 0; i < 15; i++ ) {
if( results[ i ].key.startsWith( "skip" ) ) {
Expand All @@ -138,7 +146,7 @@ test( "decrement subtracts amount", async () => {
await ComfyDB.Store( `decrement${i}`, { value: 5 } );
}
await ComfyDB.Decrement( "value", { by: 5, where: { key: { startsWith: "decrement" } } } );
let results = await ComfyDB.Search( { where: { key: { contains: "decrement" } } } );
let results = /** @type {TestValue[]} */ ( await ComfyDB.Search({ where: { key: { contains: "decrement" } } } ));
expect( results.length ).toBe( 15 );
for( let i = 0; i < 15; i++ ) {
if( results[ i ].key.startsWith( "skip" ) ) {
Expand All @@ -163,7 +171,7 @@ beforeAll( () => {
});
ComfyMongo.on( "ready", async () => {
console.log( "[ComfyDB] Ready..." );
resolve();
resolve(void 0);
});
ComfyMongo.on( "exit", ( code ) => {
console.log( "[ComfyDB] Exit:", code );
Expand Down
Loading