Skip to content

Commit

Permalink
Allow clashing table names to allow users to extend libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
tgandrews committed Oct 30, 2020
1 parent 71525e3 commit 6a279fc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
54 changes: 54 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Thing>({
name: "multipleTablesError",
hashKey: "id",
schema: {
id: Omanyd.types.id(),
value: Joi.string().required(),
},
});
expect(() => {
Omanyd.define<Thing>({
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<Thing>({
name: "multipleTablesSuccess",
hashKey: "id",
schema: {
id: Omanyd.types.id(),
value: Joi.string().required(),
},
});
expect(() => {
Omanyd.define<Thing>({
name: "multipleTablesSuccess",
hashKey: "id",
schema: {
id: Omanyd.types.id(),
value: Joi.string().required(),
extras: Joi.array().items(Joi.string()),
},
allowNameClash: true,
});
}).not.toThrow();
});
});
});
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Table from "./table";

export { Schema } from "./types";

let TABLES: Table[] = [];
let STORES: { [name: string]: Table } = {};

export const types = {
id() {
Expand All @@ -22,7 +22,14 @@ export const types = {
export function define<T>(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, "id"> | T): Promise<T> {
Expand Down Expand Up @@ -83,7 +90,7 @@ export function define<T>(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();
Expand All @@ -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();
Expand All @@ -105,7 +112,7 @@ async function deleteManagedTables() {

export async function deleteTables() {
await deleteManagedTables();
TABLES = [];
STORES = {};
}

export async function clearTables() {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Options {
type: "global";
hashKey: string;
}[];
allowNameClash?: boolean;
}

export interface Schema {
Expand Down

0 comments on commit 6a279fc

Please sign in to comment.