From daf48e4c35e0990dfaf56d063b90ba83e55e02f0 Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Thu, 6 Jun 2024 17:34:46 +0200 Subject: [PATCH] Don't extend Database --- src/BaseServer.ts | 104 ++++++++++++++++++++++--------- src/Collection.spec.ts | 48 +++++++------- src/Collection.ts | 25 ++++---- src/Database.spec.ts | 2 +- src/Database.ts | 6 +- src/Single.spec.ts | 36 +++++------ src/Single.ts | 23 +++---- src/adapters/SinonServer.spec.ts | 62 +++++++++--------- 8 files changed, 179 insertions(+), 127 deletions(-) diff --git a/src/BaseServer.ts b/src/BaseServer.ts index f925627..c6ef837 100644 --- a/src/BaseServer.ts +++ b/src/BaseServer.ts @@ -1,19 +1,28 @@ +import type { Collection } from './Collection.js'; import { Database, type DatabaseOptions } from './Database.js'; -import type { QueryFunction } from './types.js'; +import type { Single } from './Single.js'; +import type { CollectionItem, QueryFunction } from './types.js'; -export class BaseServer extends Database { +export class BaseServer { baseUrl = ''; defaultQuery: QueryFunction = () => ({}); middlewares: Array> = []; + database: Database; constructor({ baseUrl = '', defaultQuery = () => ({}), + database, ...options }: BaseServerOptions = {}) { - super(options); this.baseUrl = baseUrl; this.defaultQuery = defaultQuery; + + if (database) { + this.database = database; + } else { + this.database = new Database(options); + } } /** @@ -23,13 +32,8 @@ export class BaseServer extends Database { this.defaultQuery = query; } - getContext( - context: Pick< - FakeRestContext, - 'url' | 'method' | 'params' | 'requestBody' - >, - ): FakeRestContext { - for (const name of this.getSingleNames()) { + getContext(context: NormalizedRequest): FakeRestContext { + for (const name of this.database.getSingleNames()) { const matches = context.url?.match( new RegExp(`^${this.baseUrl}\\/(${name})(\\/?.*)?$`), ); @@ -61,11 +65,7 @@ export class BaseServer extends Database { return context; } - getNormalizedRequest( - request: RequestType, - ): Promise< - Pick - > { + getNormalizedRequest(request: RequestType): Promise { throw new Error('Not implemented'); } @@ -111,7 +111,7 @@ export class BaseServer extends Database { handleRequest(request: RequestType, ctx: FakeRestContext): BaseResponse { // Handle Single Objects - for (const name of this.getSingleNames()) { + for (const name of this.database.getSingleNames()) { const matches = ctx.url?.match( new RegExp(`^${this.baseUrl}\\/(${name})(\\/?.*)?$`), ); @@ -121,7 +121,7 @@ export class BaseServer extends Database { try { return { status: 200, - body: this.getOnly(name), + body: this.database.getOnly(name), headers: { 'Content-Type': 'application/json', }, @@ -143,7 +143,7 @@ export class BaseServer extends Database { } return { status: 200, - body: this.updateOnly(name, ctx.requestBody), + body: this.database.updateOnly(name, ctx.requestBody), headers: { 'Content-Type': 'application/json', }, @@ -165,7 +165,7 @@ export class BaseServer extends Database { } return { status: 200, - body: this.updateOnly(name, ctx.requestBody), + body: this.database.updateOnly(name, ctx.requestBody), headers: { 'Content-Type': 'application/json', }, @@ -190,15 +190,15 @@ export class BaseServer extends Database { const params = Object.assign({}, this.defaultQuery(name), ctx.params); if (!matches[2]) { if (ctx.method === 'GET') { - if (!this.getCollection(name)) { + if (!this.database.getCollection(name)) { return { status: 404, headers: {} }; } - const count = this.getCount( + const count = this.database.getCount( name, params.filter ? { filter: params.filter } : {}, ); if (count > 0) { - const items = this.getAll(name, params); + const items = this.database.getAll(name, params); const first = params.range ? params.range[0] : 0; const last = params.range && params.range.length === 2 @@ -235,9 +235,11 @@ export class BaseServer extends Database { }; } - const newResource = this.addOne(name, ctx.requestBody); + const newResource = this.database.addOne(name, ctx.requestBody); const newResourceURI = `${this.baseUrl}/${name}/${ - newResource[this.getCollection(name).identifierName] + newResource[ + this.database.getCollection(name).identifierName + ] }`; return { @@ -250,7 +252,7 @@ export class BaseServer extends Database { }; } } else { - if (!this.getCollection(name)) { + if (!this.database.getCollection(name)) { return { status: 404, headers: {} }; } const id = Number.parseInt(matches[3]); @@ -258,7 +260,7 @@ export class BaseServer extends Database { try { return { status: 200, - body: this.getOne(name, id, params), + body: this.database.getOne(name, id, params), headers: { 'Content-Type': 'application/json', }, @@ -280,7 +282,11 @@ export class BaseServer extends Database { } return { status: 200, - body: this.updateOne(name, id, ctx.requestBody), + body: this.database.updateOne( + name, + id, + ctx.requestBody, + ), headers: { 'Content-Type': 'application/json', }, @@ -302,7 +308,11 @@ export class BaseServer extends Database { } return { status: 200, - body: this.updateOne(name, id, ctx.requestBody), + body: this.database.updateOne( + name, + id, + ctx.requestBody, + ), headers: { 'Content-Type': 'application/json', }, @@ -318,7 +328,7 @@ export class BaseServer extends Database { try { return { status: 200, - body: this.removeOne(name, id), + body: this.database.removeOne(name, id), headers: { 'Content-Type': 'application/json', }, @@ -340,6 +350,36 @@ export class BaseServer extends Database { addMiddleware(middleware: Middleware) { this.middlewares.push(middleware); } + + addCollection( + name: string, + collection: Collection, + ) { + this.database.addCollection(name, collection); + } + + getCollection(name: string) { + return this.database.getCollection(name); + } + + getCollectionNames() { + return this.database.getCollectionNames(); + } + + addSingle( + name: string, + single: Single, + ) { + this.database.addSingle(name, single); + } + + getSingle(name: string) { + return this.database.getSingle(name); + } + + getSingleNames() { + return this.database.getSingleNames(); + } } export type Middleware = ( @@ -352,6 +392,7 @@ export type Middleware = ( ) => Promise | BaseResponse | null; export type BaseServerOptions = DatabaseOptions & { + database?: Database; baseUrl?: string; batchUrl?: string | null; defaultQuery?: QueryFunction; @@ -371,3 +412,8 @@ export type FakeRestContext = { requestBody: Record | undefined; params: { [key: string]: any }; }; + +export type NormalizedRequest = Pick< + FakeRestContext, + 'url' | 'method' | 'params' | 'requestBody' +>; diff --git a/src/Collection.spec.ts b/src/Collection.spec.ts index b17687a..4887680 100644 --- a/src/Collection.spec.ts +++ b/src/Collection.spec.ts @@ -1,5 +1,5 @@ import { Collection } from './Collection.js'; -import { Server } from './adapters/SinonServer.js'; +import { Database } from './Database.js'; import type { CollectionItem } from './types.js'; describe('Collection', () => { @@ -627,8 +627,8 @@ describe('Collection', () => { const foos = new Collection({ items: [{ name: 'John', bar_id: 123 }], }); - const server = new Server(); - server.addCollection('foos', foos); + const database = new Database(); + database.addCollection('foos', foos); expect(() => { foos.getAll({ embed: ['bar'] }); }).toThrow( @@ -641,9 +641,9 @@ describe('Collection', () => { items: [{ name: 'John', bar_id: 123 }], }); const bars = new Collection({ items: [] }); - const server = new Server(); - server.addCollection('foos', foos); - server.addCollection('bars', bars); + const database = new Database(); + database.addCollection('foos', foos); + database.addCollection('bars', bars); const expected = [{ id: 0, name: 'John', bar_id: 123 }]; expect(foos.getAll({ embed: ['bar'] })).toEqual(expected); }); @@ -662,9 +662,9 @@ describe('Collection', () => { { id: 456, bar: 'bazz' }, ], }); - const server = new Server(); - server.addCollection('foos', foos); - server.addCollection('bars', bars); + const database = new Database(); + database.addCollection('foos', foos); + database.addCollection('bars', bars); const expected = [ { id: 0, @@ -686,8 +686,8 @@ describe('Collection', () => { const foos = new Collection({ items: [{ name: 'John', bar_id: 123 }], }); - const server = new Server(); - server.addCollection('foos', foos); + const database = new Database(); + database.addCollection('foos', foos); expect(() => { foos.getAll({ embed: ['bars'] }); }).toThrow( @@ -702,9 +702,9 @@ describe('Collection', () => { const bars = new Collection({ items: [{ id: 1, bar: 'nobody wants me' }], }); - const server = new Server(); - server.addCollection('foos', foos); - server.addCollection('bars', bars); + const database = new Database(); + database.addCollection('foos', foos); + database.addCollection('bars', bars); const expected = [{ id: 1, bar: 'nobody wants me', foos: [] }]; expect(bars.getAll({ embed: ['foos'] })).toEqual(expected); }); @@ -724,9 +724,9 @@ describe('Collection', () => { { id: 456, bar: 'bazz' }, ], }); - const server = new Server(); - server.addCollection('foos', foos); - server.addCollection('bars', bars); + const database = new Database(); + database.addCollection('foos', foos); + database.addCollection('bars', bars); const expected = [ { id: 1, bar: 'nobody wants me', foos: [] }, { @@ -761,9 +761,9 @@ describe('Collection', () => { { id: 456, bar: 'bazz', foos: [2, 3] }, ], }); - const server = new Server(); - server.addCollection('foos', foos); - server.addCollection('bars', bars); + const database = new Database(); + database.addCollection('foos', foos); + database.addCollection('bars', bars); const expected = [ { id: 1, bar: 'nobody wants me', foos: [] }, { id: 123, bar: 'baz', foos: [{ id: 1, name: 'John' }] }, @@ -809,10 +809,10 @@ describe('Collection', () => { { id: 2, name: 'Russia' }, ], }); - const server = new Server(); - server.addCollection('books', books); - server.addCollection('authors', authors); - server.addCollection('countrys', countries); // nevermind the plural + const database = new Database(); + database.addCollection('books', books); + database.addCollection('authors', authors); + database.addCollection('countrys', countries); // nevermind the plural const expected = [ { id: 1, diff --git a/src/Collection.ts b/src/Collection.ts index 203466e..f03112c 100644 --- a/src/Collection.ts +++ b/src/Collection.ts @@ -14,7 +14,7 @@ import type { export class Collection { sequence = 0; items: T[] = []; - server: Database | null = null; + database: Database | null = null; name: string | null = null; identifierName = 'id'; getNewId: () => number | string; @@ -40,10 +40,10 @@ export class Collection { /** * A Collection may need to access other collections (e.g. for embedding references) - * This is done through a reference to the parent server. + * This is done through a reference to the parent database. */ - setServer(server: Database) { - this.server = server; + setDatabase(database: Database) { + this.database = database; } setName(name: string) { @@ -66,10 +66,10 @@ export class Collection { const singularResourceName = this.name.slice(0, -1); const referenceName = `${singularResourceName}_id`; return (item: T) => { - if (this.server == null) { - throw new Error("Can't embed references without a server"); + if (this.database == null) { + throw new Error("Can't embed references without a database"); } - const otherCollection = this.server.collections[resourceName]; + const otherCollection = this.database.collections[resourceName]; if (!otherCollection) throw new Error( `Can't embed a non-existing collection ${resourceName}`, @@ -108,10 +108,11 @@ export class Collection { const pluralResourceName = `${resourceName}s`; const referenceName = `${resourceName}_id`; return (item: T) => { - if (this.server == null) { - throw new Error("Can't embed references without a server"); + if (this.database == null) { + throw new Error("Can't embed references without a database"); } - const otherCollection = this.server.collections[pluralResourceName]; + const otherCollection = + this.database.collections[pluralResourceName]; if (!otherCollection) throw new Error( `Can't embed a non-existing collection ${resourceName}`, @@ -163,7 +164,7 @@ export class Collection { items = rangeItems(items, query.range); } items = items.map((item) => Object.assign({}, item)); // clone item to avoid returning the original - if (query.embed && this.server) { + if (query.embed && this.database) { items = items.map(this._itemEmbedder(query.embed)); // embed reference } } @@ -184,7 +185,7 @@ export class Collection { } let item = this.items[index]; item = Object.assign({}, item); // clone item to avoid returning the original - if (query?.embed && this.server) { + if (query?.embed && this.database) { item = this._itemEmbedder(query.embed)(item); // embed reference } return item; diff --git a/src/Database.spec.ts b/src/Database.spec.ts index ae500e0..da112cd 100644 --- a/src/Database.spec.ts +++ b/src/Database.spec.ts @@ -2,7 +2,7 @@ import { Database } from './Database.js'; import { Single } from './Single.js'; import { Collection } from './Collection.js'; -describe('AbstractBaseServer', () => { +describe('Database', () => { describe('init', () => { it('should populate several collections', () => { const server = new Database(); diff --git a/src/Database.ts b/src/Database.ts index a1cd19e..1f8f66a 100644 --- a/src/Database.ts +++ b/src/Database.ts @@ -47,7 +47,7 @@ export class Database { collection: Collection, ) { this.collections[name] = collection; - collection.setServer(this); + collection.setDatabase(this); collection.setName(name); } @@ -64,7 +64,7 @@ export class Database { single: Single, ) { this.singles[name] = single; - single.setServer(this); + single.setDatabase(this); single.setName(name); } @@ -102,7 +102,7 @@ export class Database { name, new Collection({ items: [], - identifierName: 'id', + identifierName: this.identifierName, getNewId: this.getNewId, }), ); diff --git a/src/Single.spec.ts b/src/Single.spec.ts index f9107b5..625bcca 100644 --- a/src/Single.spec.ts +++ b/src/Single.spec.ts @@ -1,6 +1,6 @@ import { Single } from './Single.js'; import { Collection } from './Collection.js'; -import { Server } from './adapters/SinonServer.js'; +import { Database } from './Database.js'; describe('Single', () => { describe('constructor', () => { @@ -19,8 +19,8 @@ describe('Single', () => { describe('embed query', () => { it('should throw an error when trying to embed a non-existing collection', () => { const foo = new Single({ name: 'foo', bar_id: 123 }); - const server = new Server(); - server.addSingle('foo', foo); + const database = new Database(); + database.addSingle('foo', foo); expect(() => { foo.getOnly({ embed: ['bar'] }); }).toThrow( @@ -31,9 +31,9 @@ describe('Single', () => { it('should return the original object for missing embed one', () => { const foo = new Single({ name: 'foo', bar_id: 123 }); const bars = new Collection({ items: [] }); - const server = new Server(); - server.addSingle('foo', foo); - server.addCollection('bars', bars); + const database = new Database(); + database.addSingle('foo', foo); + database.addCollection('bars', bars); const expected = { name: 'foo', bar_id: 123 }; expect(foo.getOnly({ embed: ['bar'] })).toEqual(expected); }); @@ -47,9 +47,9 @@ describe('Single', () => { { id: 456, bar: 'bazz' }, ], }); - const server = new Server(); - server.addSingle('foo', foo); - server.addCollection('bars', bars); + const database = new Database(); + database.addSingle('foo', foo); + database.addCollection('bars', bars); const expected = { name: 'foo', bar_id: 123, @@ -60,8 +60,8 @@ describe('Single', () => { it('should throw an error when trying to embed many a non-existing collection', () => { const foo = new Single({ name: 'foo', bar_id: 123 }); - const server = new Server(); - server.addSingle('foo', foo); + const database = new Database(); + database.addSingle('foo', foo); expect(() => { foo.getOnly({ embed: ['bars'] }); }).toThrow( @@ -78,9 +78,9 @@ describe('Single', () => { { id: 3, bar: 'boz' }, ], }); - const server = new Server(); - server.addSingle('foo', foo); - server.addCollection('bars', bars); + const database = new Database(); + database.addSingle('foo', foo); + database.addCollection('bars', bars); const expected = { name: 'foo', bars: [ @@ -111,10 +111,10 @@ describe('Single', () => { { id: 6, name: 'baz3' }, ], }); - const server = new Server(); - server.addSingle('foo', foo); - server.addCollection('bars', bars); - server.addCollection('bazs', bazs); + const database = new Database(); + database.addSingle('foo', foo); + database.addCollection('bars', bars); + database.addCollection('bazs', bazs); const expected = { name: 'foo', bars: [ diff --git a/src/Single.ts b/src/Single.ts index 2d5a87b..cc66a6f 100644 --- a/src/Single.ts +++ b/src/Single.ts @@ -3,7 +3,7 @@ import type { CollectionItem, Embed, Query } from './types.js'; export class Single { obj: T | null = null; - server: Database | null = null; + database: Database | null = null; name: string | null = null; constructor(obj: T) { @@ -17,10 +17,10 @@ export class Single { /** * A Single may need to access other collections (e.g. for embedded - * references) This is done through a reference to the parent server. + * references) This is done through a reference to the parent database. */ - setServer(server: Database) { - this.server = server; + setDatabase(database: Database) { + this.database = database; } setName(name: string) { @@ -32,10 +32,10 @@ export class Single { // it is by definition a singleton _oneToManyEmbedder(resourceName: string) { return (item: T) => { - if (this.server == null) { - throw new Error("Can't embed references without a server"); + if (this.database == null) { + throw new Error("Can't embed references without a database"); } - const otherCollection = this.server.collections[resourceName]; + const otherCollection = this.database.collections[resourceName]; if (!otherCollection) throw new Error( `Can't embed a non-existing collection ${resourceName}`, @@ -57,10 +57,11 @@ export class Single { const pluralResourceName = `${resourceName}s`; const referenceName = `${resourceName}_id`; return (item: T) => { - if (this.server == null) { - throw new Error("Can't embed references without a server"); + if (this.database == null) { + throw new Error("Can't embed references without a database"); } - const otherCollection = this.server.collections[pluralResourceName]; + const otherCollection = + this.database.collections[pluralResourceName]; if (!otherCollection) throw new Error( `Can't embed a non-existing collection ${resourceName}`, @@ -93,7 +94,7 @@ export class Single { getOnly(query?: Query) { let item = this.obj; - if (query?.embed && this.server) { + if (query?.embed && this.database) { item = Object.assign({}, item); // Clone item = this._itemEmbedder(query.embed)(item); } diff --git a/src/adapters/SinonServer.spec.ts b/src/adapters/SinonServer.spec.ts index d433c7e..d7de34a 100644 --- a/src/adapters/SinonServer.spec.ts +++ b/src/adapters/SinonServer.spec.ts @@ -40,7 +40,7 @@ describe('SinonServer', () => { context.params.range = [start, end]; return next(request, context); }); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [ @@ -85,7 +85,7 @@ describe('SinonServer', () => { }; return response; }); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [ @@ -111,7 +111,7 @@ describe('SinonServer', () => { requestUrl = request.url; return next(request, context); }); - server.addCollection('foo', new Collection()); + server.database.addCollection('foo', new Collection()); const request = getFakeXMLHTTPRequest('GET', '/foo'); if (request == null) throw new Error('request is null'); @@ -132,7 +132,7 @@ describe('SinonServer', () => { it('should respond to GET /foo by sending all items in collection foo', async () => { const server = new SinonServer(); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [ @@ -159,7 +159,7 @@ describe('SinonServer', () => { it('should respond to GET /foo?queryString by sending all items in collection foo satisfying query', async () => { const server = new SinonServer(); - server.addCollection( + server.database.addCollection( 'foos', new Collection({ items: [ @@ -169,7 +169,7 @@ describe('SinonServer', () => { ], }), ); - server.addCollection( + server.database.addCollection( 'bars', new Collection({ items: [{ id: 0, name: 'a', foo_id: 1 }] }), ); @@ -194,7 +194,7 @@ describe('SinonServer', () => { it('should respond to GET /foo?queryString with pagination by sending the correct content-range header', async () => { const server = new SinonServer(); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}], @@ -233,7 +233,7 @@ describe('SinonServer', () => { it('should respond to GET /foo on an empty collection with a []', async () => { const server = new SinonServer(); - server.addCollection('foo', new Collection()); + server.database.addCollection('foo', new Collection()); const request = getFakeXMLHTTPRequest('GET', '/foo'); if (request == null) throw new Error('request is null'); await server.handle(request); @@ -247,7 +247,7 @@ describe('SinonServer', () => { it('should respond to POST /foo by adding an item to collection foo', async () => { const server = new SinonServer(); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [ @@ -270,7 +270,7 @@ describe('SinonServer', () => { 'application/json', ); expect(request.getResponseHeader('Location')).toEqual('/foo/3'); - expect(server.getAll('foo')).toEqual([ + expect(server.database.getAll('foo')).toEqual([ { id: 1, name: 'foo' }, { id: 2, name: 'bar' }, { id: 3, name: 'baz' }, @@ -293,12 +293,14 @@ describe('SinonServer', () => { 'application/json', ); expect(request.getResponseHeader('Location')).toEqual('/foo/0'); - expect(server.getAll('foo')).toEqual([{ id: 0, name: 'baz' }]); + expect(server.database.getAll('foo')).toEqual([ + { id: 0, name: 'baz' }, + ]); }); it('should respond to GET /foo/:id by sending element of identifier id in collection foo', async () => { const server = new SinonServer(); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [ @@ -320,7 +322,7 @@ describe('SinonServer', () => { it('should respond to GET /foo/:id on a non-existing id with a 404', async () => { const server = new SinonServer(); - server.addCollection('foo', new Collection()); + server.database.addCollection('foo', new Collection()); const request = getFakeXMLHTTPRequest('GET', '/foo/3'); if (request == null) throw new Error('request is null'); await server.handle(request); @@ -329,7 +331,7 @@ describe('SinonServer', () => { it('should respond to PUT /foo/:id by updating element of identifier id in collection foo', async () => { const server = new SinonServer(); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [ @@ -351,7 +353,7 @@ describe('SinonServer', () => { expect(request.getResponseHeader('Content-Type')).toEqual( 'application/json', ); - expect(server.getAll('foo')).toEqual([ + expect(server.database.getAll('foo')).toEqual([ { id: 1, name: 'foo' }, { id: 2, name: 'baz' }, ]); @@ -359,7 +361,7 @@ describe('SinonServer', () => { it('should respond to PUT /foo/:id on a non-existing id with a 404', async () => { const server = new SinonServer(); - server.addCollection('foo', new Collection({ items: [] })); + server.database.addCollection('foo', new Collection({ items: [] })); const request = getFakeXMLHTTPRequest( 'PUT', '/foo/3', @@ -372,7 +374,7 @@ describe('SinonServer', () => { it('should respond to PATCH /foo/:id by updating element of identifier id in collection foo', async () => { const server = new SinonServer(); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [ @@ -394,7 +396,7 @@ describe('SinonServer', () => { expect(request.getResponseHeader('Content-Type')).toEqual( 'application/json', ); - expect(server.getAll('foo')).toEqual([ + expect(server.database.getAll('foo')).toEqual([ { id: 1, name: 'foo' }, { id: 2, name: 'baz' }, ]); @@ -402,7 +404,7 @@ describe('SinonServer', () => { it('should respond to PATCH /foo/:id on a non-existing id with a 404', async () => { const server = new SinonServer(); - server.addCollection('foo', new Collection({ items: [] })); + server.database.addCollection('foo', new Collection({ items: [] })); const request = getFakeXMLHTTPRequest( 'PATCH', '/foo/3', @@ -415,7 +417,7 @@ describe('SinonServer', () => { it('should respond to DELETE /foo/:id by removing element of identifier id in collection foo', async () => { const server = new SinonServer(); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [ @@ -433,12 +435,14 @@ describe('SinonServer', () => { expect(request.getResponseHeader('Content-Type')).toEqual( 'application/json', ); - expect(server.getAll('foo')).toEqual([{ id: 1, name: 'foo' }]); + expect(server.database.getAll('foo')).toEqual([ + { id: 1, name: 'foo' }, + ]); }); it('should respond to DELETE /foo/:id on a non-existing id with a 404', async () => { const server = new SinonServer(); - server.addCollection('foo', new Collection({ items: [] })); + server.database.addCollection('foo', new Collection({ items: [] })); const request = getFakeXMLHTTPRequest('DELETE', '/foo/3'); if (request == null) throw new Error('request is null'); await server.handle(request); @@ -447,7 +451,7 @@ describe('SinonServer', () => { it('should respond to GET /foo/ with single item', async () => { const server = new SinonServer(); - server.addSingle('foo', new Single({ name: 'foo' })); + server.database.addSingle('foo', new Single({ name: 'foo' })); const request = getFakeXMLHTTPRequest('GET', '/foo'); if (request == null) throw new Error('request is null'); @@ -462,7 +466,7 @@ describe('SinonServer', () => { it('should respond to PUT /foo/ by updating the singleton record', async () => { const server = new SinonServer(); - server.addSingle('foo', new Single({ name: 'foo' })); + server.database.addSingle('foo', new Single({ name: 'foo' })); const request = getFakeXMLHTTPRequest( 'PUT', @@ -477,12 +481,12 @@ describe('SinonServer', () => { expect(request.getResponseHeader('Content-Type')).toEqual( 'application/json', ); - expect(server.getOnly('foo')).toEqual({ name: 'baz' }); + expect(server.database.getOnly('foo')).toEqual({ name: 'baz' }); }); it('should respond to PATCH /foo/ by updating the singleton record', async () => { const server = new SinonServer(); - server.addSingle('foo', new Single({ name: 'foo' })); + server.database.addSingle('foo', new Single({ name: 'foo' })); const request = getFakeXMLHTTPRequest( 'PATCH', @@ -497,14 +501,14 @@ describe('SinonServer', () => { expect(request.getResponseHeader('Content-Type')).toEqual( 'application/json', ); - expect(server.getOnly('foo')).toEqual({ name: 'baz' }); + expect(server.database.getOnly('foo')).toEqual({ name: 'baz' }); }); }); describe('setDefaultQuery', () => { it('should set the default query string', async () => { const server = new SinonServer(); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}], @@ -527,7 +531,7 @@ describe('SinonServer', () => { it('should not override any provided query string', async () => { const server = new SinonServer(); - server.addCollection( + server.database.addCollection( 'foo', new Collection({ items: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}],