diff --git a/src/index.test.ts b/src/index.test.ts index b97782c..6bac0cc 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -713,4 +713,58 @@ describe("omanyd", () => { expect(readThing).toBeNull(); }); }); + + describe("multiple table defintions", () => { + it("shoudl error when defining two stores for the same table", async () => { + interface Thing { + id: string; + value: string; + } + Omanyd.define({ + name: "multipleTablesError", + hashKey: "id", + schema: { + id: Omanyd.types.id(), + value: Joi.string().required(), + }, + }); + expect(() => { + Omanyd.define({ + name: "multipleTablesError", + hashKey: "id", + schema: { + id: Omanyd.types.id(), + value: Joi.string().required(), + }, + }); + }).toThrow(/clashing table name: "multipleTablesError"/); + }); + + it("should allow for a multiple table name to be defined multiple times ", async () => { + interface Thing { + id: string; + value: string; + } + Omanyd.define({ + name: "multipleTablesSuccess", + hashKey: "id", + schema: { + id: Omanyd.types.id(), + value: Joi.string().required(), + }, + }); + expect(() => { + Omanyd.define({ + name: "multipleTablesSuccess", + hashKey: "id", + schema: { + id: Omanyd.types.id(), + value: Joi.string().required(), + extras: Joi.array().items(Joi.string()), + }, + allowNameClash: true, + }); + }).not.toThrow(); + }); + }); }); diff --git a/src/index.ts b/src/index.ts index 44676d2..51b6a6a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import Table from "./table"; export { Schema } from "./types"; -let TABLES: Table[] = []; +let STORES: { [name: string]: Table } = {}; export const types = { id() { @@ -22,7 +22,14 @@ export const types = { export function define(options: Options) { const t = new Table(options); const validator = Joi.object(options.schema); - TABLES.push(t); + + const allowNameClash = options.allowNameClash ?? false; + if (STORES[t.name] && !allowNameClash) { + throw new Error( + `Trying to define store with clashing table name: "${t.name}"` + ); + } + STORES[t.name] = t; return { async create(obj: Omit | T): Promise { @@ -83,7 +90,7 @@ export function define(options: Options) { export async function createTables() { return Promise.all( - TABLES.map(async (t) => { + Object.values(STORES).map(async (t) => { const itExists = await t.tableExists(); if (!itExists) { await t.createTable(); @@ -94,7 +101,7 @@ export async function createTables() { async function deleteManagedTables() { await Promise.all( - TABLES.map(async (t) => { + Object.values(STORES).map(async (t) => { const itExists = await t.tableExists(); if (itExists) { await t.deleteTable(); @@ -105,7 +112,7 @@ async function deleteManagedTables() { export async function deleteTables() { await deleteManagedTables(); - TABLES = []; + STORES = {}; } export async function clearTables() { diff --git a/src/types.ts b/src/types.ts index 75652aa..e5bda12 100644 --- a/src/types.ts +++ b/src/types.ts @@ -24,6 +24,7 @@ export interface Options { type: "global"; hashKey: string; }[]; + allowNameClash?: boolean; } export interface Schema {