diff --git a/src/db/dbdataemoji.ts b/src/db/dbdataemoji.ts new file mode 100644 index 0000000000000000000000000000000000000000..e047757364992946c90fc760a9ed95275b454100 --- /dev/null +++ b/src/db/dbdataemoji.ts @@ -0,0 +1,69 @@ +import { DiscordStore } from "../store"; +import { IDbData } from "./dbdatainterface"; +import * as log from "npmlog"; + +export class DbGuildEmoji implements IDbData { + + public EmojiId: string; + public GuildId: string; + public Name: string; + public MxcUrl: string; + public CreatedAt: number; + public UpdatedAt: number; + public Result: boolean; + + public RunQuery(store: DiscordStore, params: any): Promise<null> { + log.silly("DiscordStore", "_get_schema_version"); + return store.db.getAsync(` + SELECT * + FROM guild_emoji + WHERE emoji_id = $id`, { + $id: params.emoji_id, + }).then((row) => { + this.Result = row != undefined; + if(this.Result) { + this.EmojiId = row.emoji_id; + this.GuildId = row.guild_id; + this.Name = row.name; + this.MxcUrl = row.mxc_url; + this.CreatedAt = row.created_at; + this.UpdatedAt = row.updated_at; + } + }); + } + + public Insert(store: DiscordStore): Promise<null> { + this.CreatedAt = new Date().getTime(); + this.UpdatedAt = this.CreatedAt; + return store.db.runAsync(` + INSERT INTO guild_emoji + (emoji_id,guild_id,name,mxc_url,created_at,updated_at) + VALUES ($emoji_id,$guild_id,$name,$mxc_url,$created_at,$updated_at);`, { + $emoji_id: this.EmojiId, + $guild_id: this.GuildId, + $name: this.Name, + $mxc_url: this.MxcUrl, + $created_at: this.CreatedAt, + $updated_at: this.UpdatedAt, + }); + } + + public Update(store: DiscordStore) { + // Ensure this has incremented by 1 for Insert+Update operations. + this.UpdatedAt = new Date().getTime()+1; + return store.db.runAsync(` + UPDATE guild_emoji + SET name = $name, + mxc_url = $mxc_url, + updated_at = $updated_at + WHERE + emoji_id = $emoji_id + AND guild_id = $guild_id`, { + $emoji_id: this.EmojiId, + $guild_id: this.GuildId, + $name: this.Name, + $mxc_url: this.MxcUrl, + $updated_at: this.UpdatedAt, + }); + } +} diff --git a/src/db/dbdatainterface.ts b/src/db/dbdatainterface.ts index 7687e18fa1e9a8d1ac7d24476d2aa082ea3f53e4..97861acb51b4c3e3b737c1c17c46e55fef395abd 100644 --- a/src/db/dbdatainterface.ts +++ b/src/db/dbdatainterface.ts @@ -2,7 +2,7 @@ import { DiscordStore } from "../store"; export interface IDbData { Result: boolean; - RunQuery(store: DiscordStore, params: any); - Insert(store: DiscordStore); - Update(store: DiscordStore); + RunQuery(store: DiscordStore, params: any): Promise<null>; + Insert(store: DiscordStore): Promise<null>; + Update(store: DiscordStore): Promise<null>; } diff --git a/src/store.ts b/src/store.ts index c4c695a66ae82ac359609fcf81e98813797d737e..648928dda2a7453c9cddaad1112ccfe813397c52 100644 --- a/src/store.ts +++ b/src/store.ts @@ -227,21 +227,22 @@ export class DiscordStore { }); } - public Get<T extends IDbData>(dbType: {new(): T; }, params: any): T { + public Get<T extends IDbData>(dbType: {new(): T; }, params: any): Promise<T> { const dType = new dbType(); log.silly("DiscordStore", `get <${dType.constructor.name}>`); - dType.RunQuery(this, params); - return dType; + return dType.RunQuery(this, params).then(() => { + return dType; + }); } - public Insert<T extends IDbData>(data: T) { + public Insert<T extends IDbData>(data: T): Promise<null> { log.silly("DiscordStore", `insert <${data.constructor.name}>`); - data.Insert(this); + return data.Insert(this); } - public Update<T extends IDbData>(data: T) { + public Update<T extends IDbData>(data: T): Promise<null> { log.silly("DiscordStore", `insert <${data.constructor.name}>`); - data.Update(this); + return data.Update(this); } private getSchemaVersion ( ): Promise<number> { diff --git a/test/test_store.ts b/test/test_store.ts index f3e82c092a10ec941a254c893b4a30b146640c13..24b1689fbfc3e020ba74dd60b481f33d73ba15f2 100644 --- a/test/test_store.ts +++ b/test/test_store.ts @@ -3,6 +3,7 @@ import * as ChaiAsPromised from "chai-as-promised"; // import * as Proxyquire from "proxyquire"; import { DiscordStore } from "../src/store"; import * as log from "npmlog"; +import { DbGuildEmoji } from "../src/db/dbdataemoji"; Chai.use(ChaiAsPromised); const expect = Chai.expect; @@ -33,12 +34,55 @@ describe("DiscordStore", () => { })).to.eventually.be.fulfilled; }); }); - describe("add_user_token", () => { - it("should not throw when adding an entry", () => { + describe("Get|Insert|Update<DbGuildEmoji>", () => { + it("should insert successfully", () => { const store = new DiscordStore(":memory:"); return expect(store.init().then(() => { - return store.add_user_token("userid", "token", "discordid"); + const emoji = new DbGuildEmoji(); + emoji.EmojiId = "123"; + emoji.GuildId = "456"; + emoji.Name = "TestEmoji"; + emoji.MxcUrl = "TestUrl"; + return store.Insert(emoji); })).to.eventually.be.fulfilled; }); + it("should get successfully", async function() { + const store = new DiscordStore(":memory:"); + await store.init(); + const insert_emoji = new DbGuildEmoji(); + insert_emoji.EmojiId = "123"; + insert_emoji.GuildId = "456"; + insert_emoji.Name = "TestEmoji"; + insert_emoji.MxcUrl = "TestUrl"; + await store.Insert(insert_emoji); + const get_emoji = await store.Get(DbGuildEmoji, {emoji_id: "123"}); + Chai.assert.equal(get_emoji.Name, "TestEmoji"); + Chai.assert.equal(get_emoji.MxcUrl, "TestUrl"); + }); + it("should not return nonexistant emoji", async function() { + const store = new DiscordStore(":memory:"); + await store.init(); + const get_emoji = await store.Get(DbGuildEmoji, {emoji_id: "123"}); + Chai.assert.isFalse(get_emoji.Result); + }); + it("should update successfully", async function() { + const store = new DiscordStore(":memory:"); + await store.init(); + const insert_emoji = new DbGuildEmoji(); + insert_emoji.EmojiId = "123"; + insert_emoji.GuildId = "456"; + insert_emoji.Name = "TestEmoji"; + insert_emoji.MxcUrl = "TestUrl"; + await store.Insert(insert_emoji); + insert_emoji.EmojiId = "123"; + insert_emoji.GuildId = "456"; + insert_emoji.Name = "TestEmoji2"; + insert_emoji.MxcUrl = "NewURL"; + await store.Update(insert_emoji); + const get_emoji = await store.Get(DbGuildEmoji, {emoji_id: "123"}); + Chai.assert.equal(get_emoji.Name, "TestEmoji2"); + Chai.assert.equal(get_emoji.MxcUrl, "NewURL"); + Chai.assert.notEqual(get_emoji.CreatedAt,get_emoji.UpdatedAt); + }); }); });