2023-10-02 17:51:03 +03:00
|
|
|
import nql from '@tryghost/nql';
|
2023-11-20 14:00:51 +03:00
|
|
|
import assert from 'assert/strict';
|
|
|
|
import {Knex} from 'knex';
|
|
|
|
import {BookshelfRepository, ModelClass, ModelInstance} from '../src/index';
|
2023-09-01 12:52:16 +03:00
|
|
|
|
|
|
|
type SimpleEntity = {
|
|
|
|
id: string;
|
|
|
|
deleted: boolean;
|
|
|
|
name: string;
|
|
|
|
age: number;
|
|
|
|
birthday: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SimpleBookshelfRepository extends BookshelfRepository<string, SimpleEntity> {
|
|
|
|
protected modelToEntity(model: ModelInstance<string>): SimpleEntity {
|
|
|
|
return {
|
|
|
|
id: model.id,
|
|
|
|
deleted: false,
|
|
|
|
name: model.get('name') as string,
|
|
|
|
age: model.get('age') as number,
|
|
|
|
birthday: model.get('birthday') as string
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected toPrimitive(entity: SimpleEntity): object {
|
|
|
|
return {
|
|
|
|
id: entity.id,
|
|
|
|
name: entity.name,
|
|
|
|
age: entity.age,
|
|
|
|
birthday: entity.birthday
|
|
|
|
};
|
|
|
|
}
|
2023-09-08 13:32:06 +03:00
|
|
|
|
2023-09-14 15:29:38 +03:00
|
|
|
protected getFieldToColumnMap(): Record<keyof SimpleEntity, string> {
|
|
|
|
return {
|
|
|
|
id: 'id',
|
|
|
|
deleted: 'deleted',
|
|
|
|
name: 'name',
|
|
|
|
age: 'age',
|
|
|
|
birthday: 'birthday'
|
|
|
|
};
|
2023-09-08 13:32:06 +03:00
|
|
|
}
|
2023-09-01 12:52:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class Model implements ModelClass<string> {
|
|
|
|
items: ModelInstance<string>[] = [];
|
|
|
|
|
2023-09-11 14:04:34 +03:00
|
|
|
orderRaw?: string;
|
|
|
|
limit?: number;
|
|
|
|
offset?: number;
|
2023-09-15 15:38:43 +03:00
|
|
|
returnCount = false;
|
2023-09-11 14:04:34 +03:00
|
|
|
|
2023-09-01 12:52:16 +03:00
|
|
|
constructor() {
|
|
|
|
this.items = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(data: {id: string;}): Promise<void> {
|
|
|
|
this.items = this.items.filter(item => item.id !== data.id);
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
findOne(data: {id: string;}, options?: {require?: boolean | undefined;} | undefined): Promise<ModelInstance<string> | null> {
|
|
|
|
const item = this.items.find(i => i.id === data.id);
|
|
|
|
if (!item && options?.require) {
|
|
|
|
throw new Error('Not found');
|
|
|
|
}
|
|
|
|
return Promise.resolve(item ?? null);
|
|
|
|
}
|
2023-09-11 14:04:34 +03:00
|
|
|
|
|
|
|
fetchAll(): Promise<ModelInstance<string>[]> {
|
2023-09-01 12:52:16 +03:00
|
|
|
const sorted = this.items.slice().sort((a, b) => {
|
2023-09-11 14:04:34 +03:00
|
|
|
for (const order of this.orderRaw?.split(',') ?? []) {
|
2023-09-08 13:32:06 +03:00
|
|
|
const [field, direction] = order.split(' ');
|
|
|
|
|
|
|
|
const aValue = a.get(field as string) as number;
|
|
|
|
const bValue = b.get(field as string) as number;
|
2023-09-01 12:52:16 +03:00
|
|
|
if (aValue < bValue) {
|
2023-09-08 13:32:06 +03:00
|
|
|
return direction === 'asc' ? -1 : 1;
|
2023-09-01 12:52:16 +03:00
|
|
|
} else if (aValue > bValue) {
|
2023-09-08 13:32:06 +03:00
|
|
|
return direction === 'asc' ? 1 : -1;
|
2023-09-01 12:52:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
2023-09-11 14:04:34 +03:00
|
|
|
return Promise.resolve(sorted.slice(this.offset ?? 0, (this.offset ?? 0) + (this.limit ?? sorted.length)));
|
2023-09-01 12:52:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
add(data: object): Promise<ModelInstance<string>> {
|
|
|
|
const item = {
|
|
|
|
id: (data as any).id,
|
2023-10-02 17:51:03 +03:00
|
|
|
...data,
|
2023-09-01 12:52:16 +03:00
|
|
|
get(field: string): unknown {
|
|
|
|
return (data as any)[field];
|
|
|
|
},
|
|
|
|
set(d: object|string, value?: unknown): void {
|
|
|
|
if (typeof d === 'string') {
|
|
|
|
(data as any)[d] = value;
|
|
|
|
} else {
|
|
|
|
Object.assign(data, d);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
save(properties: object): Promise<ModelInstance<string>> {
|
|
|
|
Object.assign(data, properties);
|
|
|
|
return Promise.resolve(item);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.items.push(item);
|
|
|
|
return Promise.resolve(item);
|
|
|
|
}
|
2023-09-08 13:32:06 +03:00
|
|
|
|
2023-10-02 17:51:03 +03:00
|
|
|
getFilteredCollection({filter, mongoTransformer}: {filter?: string, mongoTransformer?: unknown}) {
|
|
|
|
// Filter all items by filter and mongoTransformer
|
|
|
|
if (!filter) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
const n = nql(filter, {
|
|
|
|
transformer: mongoTransformer
|
|
|
|
});
|
|
|
|
|
|
|
|
const duplicate = new Model();
|
|
|
|
duplicate.items = this.items.filter(item => n.queryJSON(item));
|
|
|
|
return duplicate;
|
2023-09-08 13:32:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
count() {
|
|
|
|
return Promise.resolve(this.items.length);
|
|
|
|
}
|
2023-09-11 14:04:34 +03:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2023-09-15 15:38:43 +03:00
|
|
|
query(f?: (q: Knex.QueryBuilder) => void): Knex.QueryBuilder {
|
|
|
|
const builder = {
|
2023-09-11 14:04:34 +03:00
|
|
|
limit: (limit: number) => {
|
|
|
|
this.limit = limit;
|
2023-09-15 15:38:43 +03:00
|
|
|
return builder;
|
2023-09-11 14:04:34 +03:00
|
|
|
},
|
|
|
|
offset: (offset: number) => {
|
|
|
|
this.offset = offset;
|
2023-09-15 15:38:43 +03:00
|
|
|
return builder;
|
2023-09-11 14:04:34 +03:00
|
|
|
},
|
|
|
|
orderByRaw: (order: string) => {
|
|
|
|
this.orderRaw = order;
|
2023-09-15 15:38:43 +03:00
|
|
|
return builder;
|
|
|
|
},
|
|
|
|
select: () => {
|
|
|
|
return builder;
|
|
|
|
},
|
|
|
|
count: () => {
|
|
|
|
return builder;
|
|
|
|
},
|
|
|
|
groupBy: (field: string) => {
|
|
|
|
return Promise.resolve([
|
|
|
|
{
|
|
|
|
[field]: 5,
|
|
|
|
count: 5
|
|
|
|
}
|
|
|
|
]);
|
2023-09-11 14:04:34 +03:00
|
|
|
}
|
2023-09-15 15:38:43 +03:00
|
|
|
} as any as Knex.QueryBuilder;
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
f(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder;
|
2023-09-11 14:04:34 +03:00
|
|
|
}
|
2023-09-01 12:52:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('BookshelfRepository', function () {
|
|
|
|
it('Can save, retrieve, update and delete entities', async function () {
|
|
|
|
const repository = new SimpleBookshelfRepository(new Model());
|
|
|
|
|
|
|
|
checkRetrieving: {
|
|
|
|
const entity = {
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
};
|
|
|
|
|
|
|
|
await repository.save(entity);
|
|
|
|
const result = await repository.getById('1');
|
|
|
|
|
|
|
|
assert(result);
|
|
|
|
assert(result.name === 'John');
|
|
|
|
assert(result.age === 30);
|
|
|
|
assert(result.id === '1');
|
|
|
|
|
|
|
|
break checkRetrieving;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkUpdating: {
|
|
|
|
const entity = {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
};
|
|
|
|
|
|
|
|
await repository.save(entity);
|
|
|
|
|
|
|
|
entity.name = 'Kym';
|
|
|
|
|
|
|
|
await repository.save(entity);
|
|
|
|
|
|
|
|
const result = await repository.getById('2');
|
|
|
|
|
|
|
|
assert(result);
|
|
|
|
assert.equal(result.name, 'Kym');
|
|
|
|
assert.equal(result.age, 24);
|
|
|
|
assert.equal(result.id, '2');
|
|
|
|
|
|
|
|
break checkUpdating;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDeleting: {
|
|
|
|
const entity = {
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Egg',
|
|
|
|
age: 180,
|
|
|
|
birthday: new Date('2010-01-01').toISOString()
|
|
|
|
};
|
|
|
|
|
|
|
|
await repository.save(entity);
|
|
|
|
|
|
|
|
assert(await repository.getById('3'));
|
|
|
|
|
|
|
|
entity.deleted = true;
|
|
|
|
|
|
|
|
await repository.save(entity);
|
|
|
|
|
|
|
|
assert(!await repository.getById('3'));
|
|
|
|
|
|
|
|
break checkDeleting;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can save and retrieve all entities', async function () {
|
|
|
|
const repository = new SimpleBookshelfRepository(new Model());
|
|
|
|
const entities = [{
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kym',
|
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kevin',
|
|
|
|
age: 5,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}];
|
|
|
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
await repository.save(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await repository.getAll({
|
|
|
|
order: [{
|
|
|
|
field: 'age',
|
|
|
|
direction: 'desc'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(result);
|
|
|
|
assert(result.length === 3);
|
|
|
|
assert(result[0].age === 30);
|
|
|
|
assert(result[1].age === 24);
|
|
|
|
assert(result[2].age === 5);
|
|
|
|
});
|
2023-09-08 13:32:06 +03:00
|
|
|
|
|
|
|
it('Can retrieve page', async function () {
|
|
|
|
const repository = new SimpleBookshelfRepository(new Model());
|
|
|
|
const entities = [{
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kym',
|
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kevin',
|
|
|
|
age: 5,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}];
|
|
|
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
await repository.save(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await repository.getPage({
|
|
|
|
order: [{
|
|
|
|
field: 'age',
|
|
|
|
direction: 'desc'
|
|
|
|
}],
|
|
|
|
limit: 5,
|
|
|
|
page: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(result);
|
|
|
|
assert(result.length === 3);
|
|
|
|
assert(result[0].age === 30);
|
|
|
|
assert(result[1].age === 24);
|
|
|
|
assert(result[2].age === 5);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can retrieve page without order', async function () {
|
|
|
|
const repository = new SimpleBookshelfRepository(new Model());
|
|
|
|
const entities = [{
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kym',
|
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kevin',
|
|
|
|
age: 5,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}];
|
|
|
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
await repository.save(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await repository.getPage({
|
|
|
|
order: [],
|
|
|
|
limit: 5,
|
|
|
|
page: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(result);
|
|
|
|
assert(result.length === 3);
|
|
|
|
});
|
|
|
|
|
2023-10-02 17:51:03 +03:00
|
|
|
it('Cannot retrieve zero page number', async function () {
|
|
|
|
const repository = new SimpleBookshelfRepository(new Model());
|
|
|
|
const entities = [{
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kym',
|
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kevin',
|
|
|
|
age: 5,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}];
|
|
|
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
await repository.save(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = repository.getPage({
|
|
|
|
order: [],
|
|
|
|
limit: 5,
|
|
|
|
page: 0
|
|
|
|
});
|
|
|
|
|
|
|
|
await assert.rejects(result, /page/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Cannot retrieve zero limit', async function () {
|
|
|
|
const repository = new SimpleBookshelfRepository(new Model());
|
|
|
|
const entities = [{
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kym',
|
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kevin',
|
|
|
|
age: 5,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}];
|
|
|
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
await repository.save(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = repository.getPage({
|
|
|
|
order: [],
|
|
|
|
limit: 0,
|
|
|
|
page: 5
|
|
|
|
});
|
|
|
|
|
|
|
|
await assert.rejects(result, /limit/);
|
|
|
|
});
|
|
|
|
|
2023-09-08 13:32:06 +03:00
|
|
|
it('Can retrieve count', async function () {
|
|
|
|
const repository = new SimpleBookshelfRepository(new Model());
|
|
|
|
const entities = [{
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kym',
|
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kevin',
|
|
|
|
age: 5,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}];
|
|
|
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
await repository.save(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await repository.getCount({});
|
|
|
|
assert(result === 3);
|
|
|
|
});
|
2023-09-15 15:38:43 +03:00
|
|
|
|
|
|
|
it('Can retrieve grouped count', async function () {
|
|
|
|
const repository = new SimpleBookshelfRepository(new Model());
|
|
|
|
const entities = [{
|
|
|
|
id: '1',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kym',
|
|
|
|
age: 24,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '2',
|
|
|
|
deleted: false,
|
|
|
|
name: 'John',
|
|
|
|
age: 30,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}, {
|
|
|
|
id: '3',
|
|
|
|
deleted: false,
|
|
|
|
name: 'Kevin',
|
|
|
|
age: 5,
|
|
|
|
birthday: new Date('2000-01-01').toISOString()
|
|
|
|
}];
|
|
|
|
|
|
|
|
for (const entity of entities) {
|
|
|
|
await repository.save(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await repository.getGroupedCount({groupBy: 'age'});
|
|
|
|
assert(result.length === 1);
|
|
|
|
assert(result[0].age === 5);
|
|
|
|
assert(result[0].count === 5);
|
|
|
|
});
|
2023-09-01 12:52:16 +03:00
|
|
|
});
|