You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@naturalcycles/db-lib

Package Overview
Dependencies
Maintainers
0
Versions
307
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@naturalcycles/db-lib - npm Package Compare versions

Comparing version

to
10.0.1

2

dist/testing/daoTest.d.ts
import { CommonDB } from '../common.db';
import { CommonDBImplementationQuirks } from './dbTest';
export declare function runCommonDaoTest(db: CommonDB, quirks?: CommonDBImplementationQuirks): void;
export declare function runCommonDaoTest(db: CommonDB, quirks?: CommonDBImplementationQuirks): Promise<void>;

@@ -7,8 +7,8 @@ "use strict";

const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
const vitest_1 = require("vitest");
const __1 = require("..");
const common_dao_1 = require("../commondao/common.dao");
const dbTest_1 = require("./dbTest");
const test_model_1 = require("./test.model");
function runCommonDaoTest(db, quirks = {}) {
async function runCommonDaoTest(db, quirks = {}) {
// this is because vitest cannot be "required" from cjs
const { test, expect } = await import('vitest');
const { support } = db;

@@ -28,5 +28,5 @@ const dao = new common_dao_1.CommonDao({

...i,
updated: vitest_1.expect.any(Number),
updated: expect.any(Number),
}));
(0, vitest_1.test)('ping', async () => {
test('ping', async () => {
await dao.ping();

@@ -36,3 +36,3 @@ });

if (support.createTable) {
(0, vitest_1.test)('createTable, dropIfExists=true', async () => {
test('createTable, dropIfExists=true', async () => {
await dao.createTable(test_model_1.testItemBMJsonSchema, { dropIfExists: true });

@@ -43,3 +43,3 @@ });

// DELETE ALL initially
(0, vitest_1.test)('deleteByIds test items', async () => {
test('deleteByIds test items', async () => {
const rows = await dao.query().select(['id']).runQuery();

@@ -49,23 +49,23 @@ await db.deleteByQuery(__1.DBQuery.create(test_model_1.TEST_TABLE).filter('id', 'in', rows.map(r => r.id)));

// QUERY empty
(0, vitest_1.test)('runQuery(all), runQueryCount should return empty', async () => {
(0, vitest_1.expect)(await dao.query().runQuery()).toEqual([]);
(0, vitest_1.expect)(await dao.query().runQueryCount()).toBe(0);
test('runQuery(all), runQueryCount should return empty', async () => {
expect(await dao.query().runQuery()).toEqual([]);
expect(await dao.query().runQueryCount()).toBe(0);
});
}
// GET empty
(0, vitest_1.test)('getByIds(item1.id) should return empty', async () => {
test('getByIds(item1.id) should return empty', async () => {
const [item1Loaded] = await dao.getByIds([item1.id]);
(0, vitest_1.expect)(item1Loaded).toBeUndefined();
(0, vitest_1.expect)(await dao.getById(item1.id)).toBeNull();
expect(item1Loaded).toBeUndefined();
expect(await dao.getById(item1.id)).toBeNull();
});
(0, vitest_1.test)('getByIds([]) should return []', async () => {
(0, vitest_1.expect)(await dao.getByIds([])).toEqual([]);
test('getByIds([]) should return []', async () => {
expect(await dao.getByIds([])).toEqual([]);
});
(0, vitest_1.test)('getByIds(...) should return empty', async () => {
(0, vitest_1.expect)(await dao.getByIds(['abc', 'abcd'])).toEqual([]);
test('getByIds(...) should return empty', async () => {
expect(await dao.getByIds(['abc', 'abcd'])).toEqual([]);
});
// TimeMachine
if (support.timeMachine) {
(0, vitest_1.test)('getByIds(...) 10 minutes ago should return []', async () => {
(0, vitest_1.expect)(await dao.getByIds([item1.id, 'abc'], {
test('getByIds(...) 10 minutes ago should return []', async () => {
expect(await dao.getByIds([item1.id, 'abc'], {
readAt: js_lib_1.localTime.now().minus(10, 'minute').unix,

@@ -77,3 +77,3 @@ })).toEqual([]);

if (support.nullValues) {
(0, vitest_1.test)('should allow to save and load null values', async () => {
test('should allow to save and load null values', async () => {
const item3 = {

@@ -86,9 +86,9 @@ ...(0, test_model_1.createTestItemBM)(3),

const item3Loaded = await dao.requireById(item3.id);
(0, dbTest_1.expectMatch)([item3], [item3Loaded], quirks);
(0, vitest_1.expect)(item3Loaded.k2).toBeNull();
(0, vitest_1.expect)(Object.keys(item3)).toContain('k2');
(0, vitest_1.expect)(item3.k2).toBeNull();
expectMatch([item3], [item3Loaded], quirks);
expect(item3Loaded.k2).toBeNull();
expect(Object.keys(item3)).toContain('k2');
expect(item3.k2).toBeNull();
});
}
(0, vitest_1.test)('undefined values should not be saved/loaded', async () => {
test('undefined values should not be saved/loaded', async () => {
const item3 = {

@@ -104,18 +104,18 @@ ...(0, test_model_1.createTestItemBM)(3),

const item3Loaded = await dao.requireById(item3.id);
(0, dbTest_1.expectMatch)([expected], [item3Loaded], quirks);
(0, vitest_1.expect)(item3Loaded.k2).toBeUndefined();
(0, vitest_1.expect)(Object.keys(item3Loaded)).not.toContain('k2');
(0, vitest_1.expect)(Object.keys(item3)).toContain('k2');
(0, vitest_1.expect)(item3.k2).toBeUndefined();
expectMatch([expected], [item3Loaded], quirks);
expect(item3Loaded.k2).toBeUndefined();
expect(Object.keys(item3Loaded)).not.toContain('k2');
expect(Object.keys(item3)).toContain('k2');
expect(item3.k2).toBeUndefined();
});
(0, vitest_1.test)('saveBatch test items', async () => {
test('saveBatch test items', async () => {
const itemsSaved = await dao.saveBatch(items);
(0, vitest_1.expect)(itemsSaved[0]).toBe(items[0]); // expect "same object" returned
expect(itemsSaved[0]).toBe(items[0]); // expect "same object" returned
// no unnecessary mutation
const { updated: _, ...clone } = itemsClone[0];
(0, vitest_1.expect)(items[0]).toMatchObject(clone);
(0, dbTest_1.expectMatch)(expectedItems, itemsSaved, quirks);
expect(items[0]).toMatchObject(clone);
expectMatch(expectedItems, itemsSaved, quirks);
});
if (support.increment) {
(0, vitest_1.test)('increment', async () => {
test('increment', async () => {
await dao.incrementBatch('k3', { id1: 1, id2: 2 });

@@ -139,3 +139,3 @@ let rows = await dao.query().runQuery();

});
(0, dbTest_1.expectMatch)(expected, rows, quirks);
expectMatch(expected, rows, quirks);
// reset the changes

@@ -147,20 +147,20 @@ await dao.increment('k3', 'id1', -1);

// GET not empty
(0, vitest_1.test)('getByIds all items', async () => {
test('getByIds all items', async () => {
const rows = await dao.getByIds(items.map(i => i.id).concat('abcd'));
(0, dbTest_1.expectMatch)(expectedItems, (0, js_lib_1._sortBy)(rows, r => r.id), quirks);
expectMatch(expectedItems, (0, js_lib_1._sortBy)(rows, r => r.id), quirks);
});
// QUERY
if (support.queries) {
(0, vitest_1.test)('runQuery(all) should return all items', async () => {
test('runQuery(all) should return all items', async () => {
let rows = await dao.query().runQuery();
rows = (0, js_lib_1._sortBy)(rows, r => r.id);
(0, dbTest_1.expectMatch)(expectedItems, rows, quirks);
expectMatch(expectedItems, rows, quirks);
});
if (support.dbQueryFilter) {
(0, vitest_1.test)('query even=true', async () => {
test('query even=true', async () => {
let rows = await dao.query().filter('even', '==', true).runQuery();
rows = (0, js_lib_1._sortBy)(rows, r => r.id);
(0, dbTest_1.expectMatch)(expectedItems.filter(i => i.even), rows, quirks);
expectMatch(expectedItems.filter(i => i.even), rows, quirks);
});
(0, vitest_1.test)('query nested property', async () => {
test('query nested property', async () => {
let rows = await dao

@@ -171,20 +171,20 @@ .query()

rows = (0, js_lib_1._sortBy)(rows, r => r.id);
(0, dbTest_1.expectMatch)(expectedItems.filter(i => i.nested?.foo === 1), rows, quirks);
expectMatch(expectedItems.filter(i => i.nested?.foo === 1), rows, quirks);
});
}
if (support.dbQueryOrder) {
(0, vitest_1.test)('query order by k1 desc', async () => {
test('query order by k1 desc', async () => {
const rows = await dao.query().order('k1', true).runQuery();
(0, dbTest_1.expectMatch)([...expectedItems].reverse(), rows, quirks);
expectMatch([...expectedItems].reverse(), rows, quirks);
});
}
if (support.dbQuerySelectFields) {
(0, vitest_1.test)('projection query with only ids', async () => {
test('projection query with only ids', async () => {
let rows = await dao.query().select(['id']).runQuery();
rows = (0, js_lib_1._sortBy)(rows, r => r.id);
(0, dbTest_1.expectMatch)(expectedItems.map(item => (0, js_lib_1._pick)(item, ['id'])), rows, quirks);
expectMatch(expectedItems.map(item => (0, js_lib_1._pick)(item, ['id'])), rows, quirks);
});
}
(0, vitest_1.test)('runQueryCount should return 3', async () => {
(0, vitest_1.expect)(await dao.query().runQueryCount()).toBe(3);
test('runQueryCount should return 3', async () => {
expect(await dao.query().runQueryCount()).toBe(3);
});

@@ -194,25 +194,25 @@ }

if (support.streaming) {
(0, vitest_1.test)('streamQueryForEach all', async () => {
test('streamQueryForEach all', async () => {
let rows = [];
await dao.query().streamQueryForEach(bm => void rows.push(bm));
rows = (0, js_lib_1._sortBy)(rows, r => r.id);
(0, dbTest_1.expectMatch)(expectedItems, rows, quirks);
expectMatch(expectedItems, rows, quirks);
});
(0, vitest_1.test)('streamQuery all', async () => {
test('streamQuery all', async () => {
let rows = await dao.query().streamQuery().toArray();
rows = (0, js_lib_1._sortBy)(rows, r => r.id);
(0, dbTest_1.expectMatch)(expectedItems, rows, quirks);
expectMatch(expectedItems, rows, quirks);
});
(0, vitest_1.test)('streamQueryIdsForEach all', async () => {
test('streamQueryIdsForEach all', async () => {
let ids = [];
await dao.query().streamQueryIdsForEach(id => void ids.push(id));
ids = ids.sort();
(0, dbTest_1.expectMatch)(expectedItems.map(i => i.id), ids, quirks);
expectMatch(expectedItems.map(i => i.id), ids, quirks);
});
(0, vitest_1.test)('streamQueryIds all', async () => {
test('streamQueryIds all', async () => {
let ids = await dao.query().streamQueryIds().toArray();
ids = ids.sort();
(0, dbTest_1.expectMatch)(expectedItems.map(i => i.id), ids, quirks);
expectMatch(expectedItems.map(i => i.id), ids, quirks);
});
(0, vitest_1.test)('streamSaveTransform', async () => {
test('streamSaveTransform', async () => {
const items2 = (0, test_model_1.createTestItemsBM)(2).map(i => ({ ...i, id: i.id + '_str' }));

@@ -222,3 +222,3 @@ const ids = items2.map(i => i.id);

const items2Loaded = await dao.getByIds(ids);
(0, dbTest_1.expectMatch)(items2, items2Loaded, quirks);
expectMatch(items2, items2Loaded, quirks);
// cleanup

@@ -230,8 +230,8 @@ await dao.query().filterIn('id', ids).deleteByQuery();

if (support.queries) {
(0, vitest_1.test)('deleteByQuery even=false', async () => {
test('deleteByQuery even=false', async () => {
const deleted = await dao.query().filter('even', '==', false).deleteByQuery();
(0, vitest_1.expect)(deleted).toBe(items.filter(item => !item.even).length);
(0, vitest_1.expect)(await dao.query().runQueryCount()).toBe(1);
expect(deleted).toBe(items.filter(item => !item.even).length);
expect(await dao.query().runQueryCount()).toBe(1);
});
(0, vitest_1.test)('cleanup', async () => {
test('cleanup', async () => {
// CLEAN UP

@@ -242,3 +242,3 @@ await dao.query().deleteByQuery();

if (support.transactions) {
(0, vitest_1.test)('transaction happy path', async () => {
test('transaction happy path', async () => {
// cleanup

@@ -253,6 +253,6 @@ await dao.query().deleteByQuery();

const loaded = await dao.query().runQuery();
(0, vitest_1.expect)(loaded.length).toBe(1);
(0, vitest_1.expect)(loaded[0].id).toBeDefined();
(0, vitest_1.expect)(loaded[0].created).toBeGreaterThanOrEqual(now);
(0, vitest_1.expect)(loaded[0].updated).toBe(loaded[0].created);
expect(loaded.length).toBe(1);
expect(loaded[0].id).toBeDefined();
expect(loaded[0].created).toBeGreaterThanOrEqual(now);
expect(loaded[0].updated).toBe(loaded[0].created);
await dao.runInTransaction(async (tx) => {

@@ -272,6 +272,6 @@ await tx.deleteById(dao, loaded[0].id);

const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
(0, dbTest_1.expectMatch)(expected, rows, quirks);
expectMatch(expected, rows, quirks);
});
(0, vitest_1.test)('transaction rollback', async () => {
await (0, vitest_1.expect)(dao.runInTransaction(async (tx) => {
test('transaction rollback', async () => {
await expect(dao.runInTransaction(async (tx) => {
await tx.deleteById(dao, items[2].id);

@@ -282,6 +282,6 @@ await tx.save(dao, { ...items[0], k1: 5 }); // it should fail here

const expected = [items[0], { ...items[2], k1: 'k1_mod' }];
(0, dbTest_1.expectMatch)(expected, rows, quirks);
expectMatch(expected, rows, quirks);
});
if (support.queries) {
(0, vitest_1.test)('transaction cleanup', async () => {
test('transaction cleanup', async () => {
await dao.query().deleteByQuery();

@@ -291,2 +291,17 @@ });

}
function expectMatch(expected, actual, quirks) {
// const expectedSorted = sortObjectDeep(expected)
// const actualSorted = sortObjectDeep(actual)
if (quirks.allowBooleansAsUndefined) {
expected = expected.map(r => {
return typeof r !== 'object' ? r : (0, js_lib_1._filterObject)(r, (_k, v) => v !== false);
});
}
if (quirks.allowExtraPropertiesInResponse) {
expect(actual).toMatchObject(expected);
}
else {
expect(actual).toEqual(expected);
}
}
}

@@ -15,3 +15,2 @@ import { CommonDB } from '../common.db';

}
export declare function runCommonDBTest(db: CommonDB, quirks?: CommonDBImplementationQuirks): void;
export declare function expectMatch(expected: any[], actual: any[], quirks: CommonDBImplementationQuirks): void;
export declare function runCommonDBTest(db: CommonDB, quirks?: CommonDBImplementationQuirks): Promise<void>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runCommonDBTest = runCommonDBTest;
exports.expectMatch = expectMatch;
const js_lib_1 = require("@naturalcycles/js-lib");
const vitest_1 = require("vitest");
const common_db_1 = require("../common.db");
const dbQuery_1 = require("../query/dbQuery");
const test_model_1 = require("./test.model");
function runCommonDBTest(db, quirks = {}) {
async function runCommonDBTest(db, quirks = {}) {
// this is because vitest cannot be "required" from cjs
const { test, expect } = await import('vitest');
const { support } = db;

@@ -16,3 +16,3 @@ const items = (0, test_model_1.createTestItemsDBM)(3);

const queryAll = () => dbQuery_1.DBQuery.create(test_model_1.TEST_TABLE);
(0, vitest_1.test)('ping', async () => {
test('ping', async () => {
await db.ping();

@@ -22,3 +22,3 @@ });

if (support.createTable) {
(0, vitest_1.test)('createTable, dropIfExists=true', async () => {
test('createTable, dropIfExists=true', async () => {
await db.createTable(test_model_1.TEST_TABLE, test_model_1.testItemBMJsonSchema, { dropIfExists: true });

@@ -29,3 +29,3 @@ });

// DELETE ALL initially
(0, vitest_1.test)('deleteByIds test items', async () => {
test('deleteByIds test items', async () => {
const { rows } = await db.runQuery(queryAll().select(['id']));

@@ -35,23 +35,23 @@ await db.deleteByQuery(queryAll().filterIn('id', rows.map(i => i.id)));

// QUERY empty
(0, vitest_1.test)('runQuery(all), runQueryCount should return empty', async () => {
(0, vitest_1.expect)((await db.runQuery(queryAll())).rows).toEqual([]);
(0, vitest_1.expect)(await db.runQueryCount(queryAll())).toBe(0);
test('runQuery(all), runQueryCount should return empty', async () => {
expect((await db.runQuery(queryAll())).rows).toEqual([]);
expect(await db.runQueryCount(queryAll())).toBe(0);
});
}
// GET empty
(0, vitest_1.test)('getByIds(item1.id) should return empty', async () => {
test('getByIds(item1.id) should return empty', async () => {
const [item1Loaded] = await db.getByIds(test_model_1.TEST_TABLE, [item1.id]);
// console.log(a)
(0, vitest_1.expect)(item1Loaded).toBeUndefined();
expect(item1Loaded).toBeUndefined();
});
(0, vitest_1.test)('getByIds([]) should return []', async () => {
(0, vitest_1.expect)(await db.getByIds(test_model_1.TEST_TABLE, [])).toEqual([]);
test('getByIds([]) should return []', async () => {
expect(await db.getByIds(test_model_1.TEST_TABLE, [])).toEqual([]);
});
(0, vitest_1.test)('getByIds(...) should return empty', async () => {
(0, vitest_1.expect)(await db.getByIds(test_model_1.TEST_TABLE, ['abc', 'abcd'])).toEqual([]);
test('getByIds(...) should return empty', async () => {
expect(await db.getByIds(test_model_1.TEST_TABLE, ['abc', 'abcd'])).toEqual([]);
});
// TimeMachine
if (support.timeMachine) {
(0, vitest_1.test)('getByIds(...) 10 minutes ago should return []', async () => {
(0, vitest_1.expect)(await db.getByIds(test_model_1.TEST_TABLE, [item1.id, 'abc'], {
test('getByIds(...) 10 minutes ago should return []', async () => {
expect(await db.getByIds(test_model_1.TEST_TABLE, [item1.id, 'abc'], {
readAt: js_lib_1.localTime.now().minus(10, 'minute').unix,

@@ -63,3 +63,3 @@ })).toEqual([]);

if (support.nullValues) {
(0, vitest_1.test)('should allow to save and load null values', async () => {
test('should allow to save and load null values', async () => {
const item3 = {

@@ -73,7 +73,7 @@ ...(0, test_model_1.createTestItemDBM)(3),

expectMatch([item3], [item3Loaded], quirks);
(0, vitest_1.expect)(item3Loaded.k2).toBeNull();
expect(item3Loaded.k2).toBeNull();
});
}
if (db.dbType === common_db_1.CommonDBType.document) {
(0, vitest_1.test)('undefined values should not be saved/loaded', async () => {
test('undefined values should not be saved/loaded', async () => {
const item3 = {

@@ -89,24 +89,24 @@ ...(0, test_model_1.createTestItemDBM)(3),

expectMatch([expected], [item3Loaded], quirks);
(0, vitest_1.expect)(item3Loaded.k2).toBeUndefined();
(0, vitest_1.expect)(Object.keys(item3Loaded)).not.toContain('k2');
expect(item3Loaded.k2).toBeUndefined();
expect(Object.keys(item3Loaded)).not.toContain('k2');
});
}
if (support.updateSaveMethod) {
(0, vitest_1.test)('saveBatch UPDATE method should throw', async () => {
await (0, vitest_1.expect)(db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'update' })).rejects.toThrow();
test('saveBatch UPDATE method should throw', async () => {
await expect(db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'update' })).rejects.toThrow();
});
}
(0, vitest_1.test)('saveBatch test items', async () => {
test('saveBatch test items', async () => {
await db.saveBatch(test_model_1.TEST_TABLE, items);
});
(0, vitest_1.test)('saveBatch should throw on null id', async () => {
await (0, vitest_1.expect)(db.saveBatch(test_model_1.TEST_TABLE, [{ ...item1, id: null }])).rejects.toThrow();
test('saveBatch should throw on null id', async () => {
await expect(db.saveBatch(test_model_1.TEST_TABLE, [{ ...item1, id: null }])).rejects.toThrow();
});
if (support.insertSaveMethod) {
(0, vitest_1.test)('saveBatch INSERT method should throw', async () => {
await (0, vitest_1.expect)(db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'insert' })).rejects.toThrow();
test('saveBatch INSERT method should throw', async () => {
await expect(db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'insert' })).rejects.toThrow();
});
}
if (support.updateSaveMethod) {
(0, vitest_1.test)('saveBatch UPDATE method should pass', async () => {
test('saveBatch UPDATE method should pass', async () => {
await db.saveBatch(test_model_1.TEST_TABLE, items, { saveMethod: 'update' });

@@ -116,3 +116,3 @@ });

// GET not empty
(0, vitest_1.test)('getByIds all items', async () => {
test('getByIds all items', async () => {
const rows = await db.getByIds(test_model_1.TEST_TABLE, items.map(i => i.id).concat('abcd'));

@@ -123,3 +123,3 @@ expectMatch(items, (0, js_lib_1._sortBy)(rows, r => r.id), quirks);

if (support.queries) {
(0, vitest_1.test)('runQuery(all) should return all items', async () => {
test('runQuery(all) should return all items', async () => {
let { rows } = await db.runQuery(queryAll());

@@ -130,3 +130,3 @@ rows = (0, js_lib_1._sortBy)(rows, r => r.id); // because query doesn't specify order here

if (support.dbQueryFilter) {
(0, vitest_1.test)('query even=true', async () => {
test('query even=true', async () => {
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).filter('even', '==', true);

@@ -140,3 +140,3 @@ let { rows } = await db.runQuery(q);

if (support.dbQueryOrder) {
(0, vitest_1.test)('query order by k1 desc', async () => {
test('query order by k1 desc', async () => {
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).order('k1', true);

@@ -148,3 +148,3 @@ const { rows } = await db.runQuery(q);

if (support.dbQuerySelectFields) {
(0, vitest_1.test)('projection query with only ids', async () => {
test('projection query with only ids', async () => {
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).select(['id']);

@@ -155,3 +155,3 @@ let { rows } = await db.runQuery(q);

});
(0, vitest_1.test)('projection query without ids', async () => {
test('projection query without ids', async () => {
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).select(['k1']);

@@ -162,3 +162,3 @@ let { rows } = await db.runQuery(q);

});
(0, vitest_1.test)('projection query empty fields (edge case)', async () => {
test('projection query empty fields (edge case)', async () => {
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).select([]);

@@ -169,4 +169,4 @@ const { rows } = await db.runQuery(q);

}
(0, vitest_1.test)('runQueryCount should return 3', async () => {
(0, vitest_1.expect)(await db.runQueryCount(new dbQuery_1.DBQuery(test_model_1.TEST_TABLE))).toBe(3);
test('runQueryCount should return 3', async () => {
expect(await db.runQueryCount(new dbQuery_1.DBQuery(test_model_1.TEST_TABLE))).toBe(3);
});

@@ -176,3 +176,3 @@ }

if (support.streaming) {
(0, vitest_1.test)('streamQuery all', async () => {
test('streamQuery all', async () => {
let rows = await db.streamQuery(queryAll()).toArray();

@@ -184,3 +184,3 @@ rows = (0, js_lib_1._sortBy)(rows, r => r.id); // cause order is not specified in DBQuery

// getTables
(0, vitest_1.test)('getTables, getTableSchema (if supported)', async () => {
test('getTables, getTableSchema (if supported)', async () => {
const tables = await db.getTables();

@@ -192,3 +192,3 @@ // console.log({ tables })

// console.log(schema)
(0, vitest_1.expect)(schema.$id).toBe(`${table}.schema.json`);
expect(schema.$id).toBe(`${table}.schema.json`);
});

@@ -199,7 +199,7 @@ }

if (support.queries && support.dbQueryFilter) {
(0, vitest_1.test)('deleteByQuery even=false', async () => {
test('deleteByQuery even=false', async () => {
const q = new dbQuery_1.DBQuery(test_model_1.TEST_TABLE).filter('even', '==', false);
const deleted = await db.deleteByQuery(q);
(0, vitest_1.expect)(deleted).toBe(items.filter(item => !item.even).length);
(0, vitest_1.expect)(await db.runQueryCount(queryAll())).toBe(1);
expect(deleted).toBe(items.filter(item => !item.even).length);
expect(await db.runQueryCount(queryAll())).toBe(1);
});

@@ -209,3 +209,3 @@ }

if (support.bufferValues) {
(0, vitest_1.test)('buffer values', async () => {
test('buffer values', async () => {
const s = 'helloWorld 1';

@@ -228,8 +228,8 @@ const b1 = Buffer.from(s);

// })
(0, vitest_1.expect)(b1Loaded).toEqual(b1);
(0, vitest_1.expect)(b1Loaded.toString()).toBe(s);
expect(b1Loaded).toEqual(b1);
expect(b1Loaded.toString()).toBe(s);
});
}
if (support.transactions) {
(0, vitest_1.test)('transaction happy path', async () => {
test('transaction happy path', async () => {
// cleanup

@@ -250,3 +250,3 @@ await db.deleteByQuery(queryAll());

});
(0, vitest_1.test)('transaction rollback', async () => {
test('transaction rollback', async () => {
let err;

@@ -263,3 +263,3 @@ try {

}
(0, vitest_1.expect)(err).toBeDefined();
expect(err).toBeDefined();
const { rows } = await db.runQuery(queryAll());

@@ -271,3 +271,3 @@ const expected = [items[0], { ...items[2], k1: 'k1_mod' }];

if (support.patchByQuery) {
(0, vitest_1.test)('patchByQuery', async () => {
test('patchByQuery', async () => {
// cleanup, reset initial data

@@ -292,3 +292,3 @@ await db.deleteByQuery(queryAll());

if (support.increment) {
(0, vitest_1.test)('incrementBatch', async () => {
test('incrementBatch', async () => {
// cleanup, reset initial data

@@ -312,3 +312,3 @@ await db.deleteByQuery(queryAll());

if (support.queries) {
(0, vitest_1.test)('cleanup', async () => {
test('cleanup', async () => {
// CLEAN UP

@@ -318,17 +318,17 @@ await db.deleteByQuery(queryAll());

}
}
function expectMatch(expected, actual, quirks) {
// const expectedSorted = sortObjectDeep(expected)
// const actualSorted = sortObjectDeep(actual)
if (quirks.allowBooleansAsUndefined) {
expected = expected.map(r => {
return typeof r !== 'object' ? r : (0, js_lib_1._filterObject)(r, (_k, v) => v !== false);
});
function expectMatch(expected, actual, quirks) {
// const expectedSorted = sortObjectDeep(expected)
// const actualSorted = sortObjectDeep(actual)
if (quirks.allowBooleansAsUndefined) {
expected = expected.map(r => {
return typeof r !== 'object' ? r : (0, js_lib_1._filterObject)(r, (_k, v) => v !== false);
});
}
if (quirks.allowExtraPropertiesInResponse) {
expect(actual).toMatchObject(expected);
}
else {
expect(actual).toEqual(expected);
}
}
if (quirks.allowExtraPropertiesInResponse) {
(0, vitest_1.expect)(actual).toMatchObject(expected);
}
else {
(0, vitest_1.expect)(actual).toEqual(expected);
}
}
import { CommonKeyValueDB } from '../kv/commonKeyValueDB';
export declare function runCommonKeyValueDaoTest(db: CommonKeyValueDB): void;
export declare function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<void>;

@@ -5,3 +5,2 @@ "use strict";

const js_lib_1 = require("@naturalcycles/js-lib");
const vitest_1 = require("vitest");
const commonKeyValueDao_1 = require("../kv/commonKeyValueDao");

@@ -12,3 +11,4 @@ const test_model_1 = require("./test.model");

const testEntries = testItems.map(e => [e.id, Buffer.from(`${e.id}value`)]);
function runCommonKeyValueDaoTest(db) {
async function runCommonKeyValueDaoTest(db) {
const { afterAll, beforeAll, expect, test } = await import('vitest');
const dao = new commonKeyValueDao_1.CommonKeyValueDao({

@@ -20,3 +20,3 @@ db,

const { support } = db;
(0, vitest_1.beforeAll)(async () => {
beforeAll(async () => {
// Tests in this suite are not isolated,

@@ -29,20 +29,20 @@ // and failing tests can leave the DB in an unexpected state for other tests,

});
(0, vitest_1.afterAll)(async () => {
afterAll(async () => {
const ids = await dao.streamIds().toArray();
await dao.deleteByIds(ids);
});
(0, vitest_1.test)('ping', async () => {
test('ping', async () => {
await dao.ping();
});
(0, vitest_1.test)('createTable', async () => {
test('createTable', async () => {
await dao.createTable({ dropIfExists: true });
});
(0, vitest_1.test)('deleteByIds non existing', async () => {
test('deleteByIds non existing', async () => {
await dao.deleteByIds(testIds);
});
(0, vitest_1.test)('getByIds should return empty', async () => {
test('getByIds should return empty', async () => {
const results = await dao.getByIds(testIds);
(0, vitest_1.expect)(results).toEqual([]);
expect(results).toEqual([]);
});
(0, vitest_1.test)('saveBatch, then getByIds', async () => {
test('saveBatch, then getByIds', async () => {
await dao.saveBatch(testEntries);

@@ -52,12 +52,12 @@ const entries = await dao.getByIds(testIds);

(0, js_lib_1._sortBy)(entries, e => e[0], true);
(0, vitest_1.expect)(entries).toEqual(testEntries); // Jest doesn't allow to compare Buffers directly
expect(entries).toEqual(testEntries); // Jest doesn't allow to compare Buffers directly
// expect(entries.map(e => e[0])).toEqual(testEntries.map(e => e[0]))
// expect(entries.map(e => e[1].toString())).toEqual(testEntries.map(e => e[1].toString()))
});
(0, vitest_1.test)('streamIds', async () => {
test('streamIds', async () => {
const ids = await dao.streamIds().toArray();
ids.sort();
(0, vitest_1.expect)(ids).toEqual(testIds);
expect(ids).toEqual(testIds);
});
(0, vitest_1.test)('streamIds limited', async () => {
test('streamIds limited', async () => {
const idsLimited = await dao.streamIds(2).toArray();

@@ -67,30 +67,30 @@ // Order is non-deterministic, so, cannot compare values

// expect(idsLimited).toEqual(testIds.slice(0, 2))
(0, vitest_1.expect)(idsLimited.length).toBe(2);
expect(idsLimited.length).toBe(2);
});
(0, vitest_1.test)('streamValues', async () => {
test('streamValues', async () => {
const values = await dao.streamValues().toArray();
values.sort();
(0, vitest_1.expect)(values).toEqual(testEntries.map(e => e[1]));
expect(values).toEqual(testEntries.map(e => e[1]));
});
(0, vitest_1.test)('streamValues limited', async () => {
test('streamValues limited', async () => {
const valuesLimited = await dao.streamValues(2).toArray();
// valuesLimited.sort()
// expect(valuesLimited).toEqual(testEntries.map(e => e[1]).slice(0, 2))
(0, vitest_1.expect)(valuesLimited.length).toBe(2);
expect(valuesLimited.length).toBe(2);
});
(0, vitest_1.test)('streamEntries', async () => {
test('streamEntries', async () => {
const entries = await dao.streamEntries().toArray();
entries.sort();
(0, vitest_1.expect)(entries).toEqual(testEntries);
expect(entries).toEqual(testEntries);
});
(0, vitest_1.test)('streamEntries limited', async () => {
test('streamEntries limited', async () => {
const entriesLimited = await dao.streamEntries(2).toArray();
// entriesLimited.sort()
// expect(entriesLimited).toEqual(testEntries.slice(0, 2))
(0, vitest_1.expect)(entriesLimited.length).toBe(2);
expect(entriesLimited.length).toBe(2);
});
(0, vitest_1.test)('deleteByIds should clear', async () => {
test('deleteByIds should clear', async () => {
await dao.deleteByIds(testIds);
const results = await dao.getByIds(testIds);
(0, vitest_1.expect)(results).toEqual([]);
expect(results).toEqual([]);
});

@@ -100,15 +100,15 @@ if (support.increment) {

const id2 = 'nonExistingField2';
(0, vitest_1.test)('increment on a non-existing field should set the value to 1', async () => {
test('increment on a non-existing field should set the value to 1', async () => {
const result = await dao.increment(id);
(0, vitest_1.expect)(result).toBe(1);
expect(result).toBe(1);
});
(0, vitest_1.test)('increment on a existing field should increase the value by one', async () => {
test('increment on a existing field should increase the value by one', async () => {
const result = await dao.increment(id);
(0, vitest_1.expect)(result).toBe(2);
expect(result).toBe(2);
});
(0, vitest_1.test)('increment should increase the value by the specified amount', async () => {
test('increment should increase the value by the specified amount', async () => {
const result = await dao.increment(id, 2);
(0, vitest_1.expect)(result).toBe(4);
expect(result).toBe(4);
});
(0, vitest_1.test)('increment 2 ids at the same time', async () => {
test('increment 2 ids at the same time', async () => {
const result = await dao.incrementBatch([

@@ -118,3 +118,3 @@ [id, 1],

]);
(0, vitest_1.expect)(Object.fromEntries(result)).toEqual({
expect(Object.fromEntries(result)).toEqual({
[id]: 5,

@@ -121,0 +121,0 @@ [id2]: 2,

import { CommonKeyValueDB } from '../kv/commonKeyValueDB';
export declare function runCommonKeyValueDBTest(db: CommonKeyValueDB): void;
export declare function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<void>;

@@ -5,3 +5,2 @@ "use strict";

const js_lib_1 = require("@naturalcycles/js-lib");
const vitest_1 = require("vitest");
const test_model_1 = require("./test.model");

@@ -13,4 +12,5 @@ const testIds = (0, js_lib_1._range)(1, 4).map(n => `id${n}`);

]);
function runCommonKeyValueDBTest(db) {
(0, vitest_1.beforeAll)(async () => {
async function runCommonKeyValueDBTest(db) {
const { afterAll, beforeAll, expect, test } = await import('vitest');
beforeAll(async () => {
// Tests in this suite are not isolated,

@@ -23,3 +23,3 @@ // and failing tests can leave the DB in an unexpected state for other tests,

});
(0, vitest_1.afterAll)(async () => {
afterAll(async () => {
const ids = await db.streamIds(test_model_1.TEST_TABLE).toArray();

@@ -29,35 +29,35 @@ await db.deleteByIds(test_model_1.TEST_TABLE, ids);

const { support } = db;
(0, vitest_1.test)('ping', async () => {
test('ping', async () => {
await db.ping();
});
(0, vitest_1.test)('createTable', async () => {
test('createTable', async () => {
await db.createTable(test_model_1.TEST_TABLE, { dropIfExists: true });
});
(0, vitest_1.test)('deleteByIds non existing', async () => {
test('deleteByIds non existing', async () => {
await db.deleteByIds(test_model_1.TEST_TABLE, testIds);
});
(0, vitest_1.test)('getByIds should return empty', async () => {
test('getByIds should return empty', async () => {
const results = await db.getByIds(test_model_1.TEST_TABLE, testIds);
(0, vitest_1.expect)(results).toEqual([]);
expect(results).toEqual([]);
});
(0, vitest_1.test)('count should be 0', async () => {
(0, vitest_1.expect)(await db.count(test_model_1.TEST_TABLE)).toBe(0);
test('count should be 0', async () => {
expect(await db.count(test_model_1.TEST_TABLE)).toBe(0);
});
(0, vitest_1.test)('saveBatch, then getByIds', async () => {
test('saveBatch, then getByIds', async () => {
await db.saveBatch(test_model_1.TEST_TABLE, testEntries);
const entries = await db.getByIds(test_model_1.TEST_TABLE, testIds);
(0, js_lib_1._sortBy)(entries, e => e[0], true);
(0, vitest_1.expect)(entries).toEqual(testEntries);
expect(entries).toEqual(testEntries);
});
if (support.count) {
(0, vitest_1.test)('count should be 3', async () => {
(0, vitest_1.expect)(await db.count(test_model_1.TEST_TABLE)).toBe(3);
test('count should be 3', async () => {
expect(await db.count(test_model_1.TEST_TABLE)).toBe(3);
});
}
(0, vitest_1.test)('streamIds', async () => {
test('streamIds', async () => {
const ids = await db.streamIds(test_model_1.TEST_TABLE).toArray();
ids.sort();
(0, vitest_1.expect)(ids).toEqual(testIds);
expect(ids).toEqual(testIds);
});
(0, vitest_1.test)('streamIds limited', async () => {
test('streamIds limited', async () => {
const idsLimited = await db.streamIds(test_model_1.TEST_TABLE, 2).toArray();

@@ -67,30 +67,30 @@ // Order is non-deterministic, so, cannot compare values

// expect(idsLimited).toEqual(testIds.slice(0, 2))
(0, vitest_1.expect)(idsLimited.length).toBe(2);
expect(idsLimited.length).toBe(2);
});
(0, vitest_1.test)('streamValues', async () => {
test('streamValues', async () => {
const values = await db.streamValues(test_model_1.TEST_TABLE).toArray();
values.sort();
(0, vitest_1.expect)(values).toEqual(testEntries.map(e => e[1]));
expect(values).toEqual(testEntries.map(e => e[1]));
});
(0, vitest_1.test)('streamValues limited', async () => {
test('streamValues limited', async () => {
const valuesLimited = await db.streamValues(test_model_1.TEST_TABLE, 2).toArray();
// valuesLimited.sort()
// expect(valuesLimited).toEqual(testEntries.map(e => e[1]).slice(0, 2))
(0, vitest_1.expect)(valuesLimited.length).toBe(2);
expect(valuesLimited.length).toBe(2);
});
(0, vitest_1.test)('streamEntries', async () => {
test('streamEntries', async () => {
const entries = await db.streamEntries(test_model_1.TEST_TABLE).toArray();
entries.sort();
(0, vitest_1.expect)(entries).toEqual(testEntries);
expect(entries).toEqual(testEntries);
});
(0, vitest_1.test)('streamEntries limited', async () => {
test('streamEntries limited', async () => {
const entriesLimited = await db.streamEntries(test_model_1.TEST_TABLE, 2).toArray();
// entriesLimited.sort()
// expect(entriesLimited).toEqual(testEntries.slice(0, 2))
(0, vitest_1.expect)(entriesLimited.length).toBe(2);
expect(entriesLimited.length).toBe(2);
});
(0, vitest_1.test)('deleteByIds should clear', async () => {
test('deleteByIds should clear', async () => {
await db.deleteByIds(test_model_1.TEST_TABLE, testIds);
const results = await db.getByIds(test_model_1.TEST_TABLE, testIds);
(0, vitest_1.expect)(results).toEqual([]);
expect(results).toEqual([]);
});

@@ -100,15 +100,15 @@ if (support.increment) {

const id2 = 'nonExistingField2';
(0, vitest_1.test)('increment on a non-existing field should set the value to 1', async () => {
test('increment on a non-existing field should set the value to 1', async () => {
const result = await db.incrementBatch(test_model_1.TEST_TABLE, [[id, 1]]);
(0, vitest_1.expect)(result).toEqual([[id, 1]]);
expect(result).toEqual([[id, 1]]);
});
(0, vitest_1.test)('increment on a existing field should increase the value by one', async () => {
test('increment on a existing field should increase the value by one', async () => {
const result = await db.incrementBatch(test_model_1.TEST_TABLE, [[id, 1]]);
(0, vitest_1.expect)(result).toEqual([[id, 2]]);
expect(result).toEqual([[id, 2]]);
});
(0, vitest_1.test)('increment should increase the value by the specified amount', async () => {
test('increment should increase the value by the specified amount', async () => {
const result = await db.incrementBatch(test_model_1.TEST_TABLE, [[id, 2]]);
(0, vitest_1.expect)(result).toEqual([[id, 4]]);
expect(result).toEqual([[id, 4]]);
});
(0, vitest_1.test)('increment 2 ids at the same time', async () => {
test('increment 2 ids at the same time', async () => {
const result = await db.incrementBatch(test_model_1.TEST_TABLE, [

@@ -118,3 +118,3 @@ [id, 1],

]);
(0, vitest_1.expect)(Object.fromEntries(result)).toEqual({
expect(Object.fromEntries(result)).toEqual({
[id]: 5,

@@ -121,0 +121,0 @@ [id2]: 2,

@@ -49,3 +49,3 @@ {

},
"version": "10.0.0",
"version": "10.0.1",
"description": "Lowest Common Denominator API to supported Databases",

@@ -52,0 +52,0 @@ "keywords": [

import { Readable } from 'node:stream'
import { _deepCopy, _omit, _pick, _sortBy, localTime } from '@naturalcycles/js-lib'
import { _deepCopy, _filterObject, _omit, _pick, _sortBy, localTime } from '@naturalcycles/js-lib'
import { _pipeline } from '@naturalcycles/nodejs-lib'
import { expect, test } from 'vitest'
import { CommonDaoLogLevel, DBQuery } from '..'

@@ -9,3 +8,3 @@ import { CommonDB } from '../common.db'

import { TestItemBM } from '.'
import { CommonDBImplementationQuirks, expectMatch } from './dbTest'
import { CommonDBImplementationQuirks } from './dbTest'
import {

@@ -19,3 +18,9 @@ createTestItemBM,

export function runCommonDaoTest(db: CommonDB, quirks: CommonDBImplementationQuirks = {}): void {
export async function runCommonDaoTest(
db: CommonDB,
quirks: CommonDBImplementationQuirks = {},
): Promise<void> {
// this is because vitest cannot be "required" from cjs
const { test, expect } = await import('vitest')
const { support } = db

@@ -364,2 +369,19 @@ const dao = new CommonDao({

}
function expectMatch(expected: any[], actual: any[], quirks: CommonDBImplementationQuirks): void {
// const expectedSorted = sortObjectDeep(expected)
// const actualSorted = sortObjectDeep(actual)
if (quirks.allowBooleansAsUndefined) {
expected = expected.map(r => {
return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
})
}
if (quirks.allowExtraPropertiesInResponse) {
expect(actual).toMatchObject(expected)
} else {
expect(actual).toEqual(expected)
}
}
}
import { _deepFreeze, _filterObject, _pick, _sortBy, localTime, pMap } from '@naturalcycles/js-lib'
import { expect, test } from 'vitest'
import { CommonDB, CommonDBType } from '../common.db'

@@ -28,3 +27,9 @@ import { DBQuery } from '../query/dbQuery'

export function runCommonDBTest(db: CommonDB, quirks: CommonDBImplementationQuirks = {}): void {
export async function runCommonDBTest(
db: CommonDB,
quirks: CommonDBImplementationQuirks = {},
): Promise<void> {
// this is because vitest cannot be "required" from cjs
const { test, expect } = await import('vitest')
const { support } = db

@@ -384,23 +389,19 @@ const items = createTestItemsDBM(3)

}
}
export function expectMatch(
expected: any[],
actual: any[],
quirks: CommonDBImplementationQuirks,
): void {
// const expectedSorted = sortObjectDeep(expected)
// const actualSorted = sortObjectDeep(actual)
function expectMatch(expected: any[], actual: any[], quirks: CommonDBImplementationQuirks): void {
// const expectedSorted = sortObjectDeep(expected)
// const actualSorted = sortObjectDeep(actual)
if (quirks.allowBooleansAsUndefined) {
expected = expected.map(r => {
return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
})
}
if (quirks.allowBooleansAsUndefined) {
expected = expected.map(r => {
return typeof r !== 'object' ? r : _filterObject(r, (_k, v) => v !== false)
})
}
if (quirks.allowExtraPropertiesInResponse) {
expect(actual).toMatchObject(expected)
} else {
expect(actual).toEqual(expected)
if (quirks.allowExtraPropertiesInResponse) {
expect(actual).toMatchObject(expected)
} else {
expect(actual).toEqual(expected)
}
}
}
import { _sortBy } from '@naturalcycles/js-lib'
import { afterAll, beforeAll, expect, test } from 'vitest'
import { CommonKeyValueDao } from '../kv/commonKeyValueDao'

@@ -11,3 +10,5 @@ import { CommonKeyValueDB, KeyValueDBTuple } from '../kv/commonKeyValueDB'

export function runCommonKeyValueDaoTest(db: CommonKeyValueDB): void {
export async function runCommonKeyValueDaoTest(db: CommonKeyValueDB): Promise<void> {
const { afterAll, beforeAll, expect, test } = await import('vitest')
const dao = new CommonKeyValueDao({

@@ -14,0 +15,0 @@ db,

import { _range, _sortBy, KeyValueTuple } from '@naturalcycles/js-lib'
import { afterAll, beforeAll, expect, test } from 'vitest'
import { CommonKeyValueDB } from '../kv/commonKeyValueDB'

@@ -13,3 +12,5 @@ import { TEST_TABLE } from './test.model'

export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
export async function runCommonKeyValueDBTest(db: CommonKeyValueDB): Promise<void> {
const { afterAll, beforeAll, expect, test } = await import('vitest')
beforeAll(async () => {

@@ -16,0 +17,0 @@ // Tests in this suite are not isolated,