UNPKG

@shopify/shopify-app-session-storage-mongodb

Version:
1 lines 6.34 kB
{"version":3,"file":"mongodb.mjs","sources":["../../../src/mongodb.ts"],"sourcesContent":["import * as mongodb from 'mongodb';\nimport {Session} from '@shopify/shopify-api';\nimport {SessionStorage} from '@shopify/shopify-app-session-storage';\n\nexport interface MongoDBSessionStorageOptions {\n sessionCollectionName: string;\n}\nconst defaultMongoDBSessionStorageOptions: MongoDBSessionStorageOptions = {\n sessionCollectionName: 'shopify_sessions',\n};\n\nexport class MongoDBSessionStorage implements SessionStorage {\n static withCredentials(\n host: string,\n dbName: string,\n username: string,\n password: string,\n opts: Partial<MongoDBSessionStorageOptions>,\n ) {\n return new MongoDBSessionStorage(\n new URL(\n `mongodb://${encodeURIComponent(username)}:${encodeURIComponent(\n password,\n )}@${host}/`,\n ),\n dbName,\n opts,\n );\n }\n\n public readonly ready: Promise<void>;\n private options: MongoDBSessionStorageOptions;\n // `mongodb` has no types for `MongoClient`???!\n private client: any;\n\n constructor(\n private dbUrl: URL,\n private dbName: string,\n opts: Partial<MongoDBSessionStorageOptions> = {},\n ) {\n if (typeof this.dbUrl === 'string') {\n this.dbUrl = new URL(this.dbUrl);\n }\n this.options = {...defaultMongoDBSessionStorageOptions, ...opts};\n this.ready = this.init();\n }\n\n public async storeSession(session: Session): Promise<boolean> {\n await this.ready;\n\n await this.collection.findOneAndReplace(\n {id: session.id},\n session.toObject(),\n {\n upsert: true,\n },\n );\n return true;\n }\n\n public async loadSession(id: string): Promise<Session | undefined> {\n await this.ready;\n\n const result = await this.collection.findOne({id});\n\n return result ? new Session(result) : undefined;\n }\n\n public async deleteSession(id: string): Promise<boolean> {\n await this.ready;\n await this.collection.deleteOne({id});\n return true;\n }\n\n public async deleteSessions(ids: string[]): Promise<boolean> {\n await this.ready;\n await this.collection.deleteMany({id: {$in: ids}});\n return true;\n }\n\n public async findSessionsByShop(shop: string): Promise<Session[]> {\n await this.ready;\n\n const rawResults = await this.collection.find({shop}).toArray();\n if (!rawResults || rawResults?.length === 0) return [];\n\n return rawResults.map((rawResult: any) => new Session(rawResult));\n }\n\n public async disconnect(): Promise<void> {\n await this.client.close();\n }\n\n private get collection() {\n return this.client\n .db(this.dbName)\n .collection(this.options.sessionCollectionName);\n }\n\n private async init() {\n this.client = new (mongodb as any).MongoClient(this.dbUrl.toString());\n await this.client.connect();\n await this.client.db().command({ping: 1});\n await this.createCollection();\n }\n\n private async hasSessionCollection(): Promise<boolean> {\n const collections = await this.client.db().collections();\n return collections\n .map((collection: any) => collection.collectionName)\n .includes(this.options.sessionCollectionName);\n }\n\n private async createCollection() {\n const hasSessionCollection = await this.hasSessionCollection();\n if (!hasSessionCollection) {\n await this.client.db().collection(this.options.sessionCollectionName);\n }\n }\n}\n"],"names":[],"mappings":";;;AAOA,MAAM,mCAAmC,GAAiC;AACxE,IAAA,qBAAqB,EAAE,kBAAkB;CAC1C;MAEY,qBAAqB,CAAA;AAyBtB,IAAA,KAAA;AACA,IAAA,MAAA;IAzBV,OAAO,eAAe,CACpB,IAAY,EACZ,MAAc,EACd,QAAgB,EAChB,QAAgB,EAChB,IAA2C,EAAA;QAE3C,OAAO,IAAI,qBAAqB,CAC9B,IAAI,GAAG,CACL,CAAA,UAAA,EAAa,kBAAkB,CAAC,QAAQ,CAAC,CAAA,CAAA,EAAI,kBAAkB,CAC7D,QAAQ,CACT,CAAA,CAAA,EAAI,IAAI,CAAA,CAAA,CAAG,CACb,EACD,MAAM,EACN,IAAI,CACL;IACH;AAEgB,IAAA,KAAK;AACb,IAAA,OAAO;;AAEP,IAAA,MAAM;AAEd,IAAA,WAAA,CACU,KAAU,EACV,MAAc,EACtB,OAA8C,EAAE,EAAA;QAFxC,IAAA,CAAA,KAAK,GAAL,KAAK;QACL,IAAA,CAAA,MAAM,GAAN,MAAM;AAGd,QAAA,IAAI,OAAO,IAAI,CAAC,KAAK,KAAK,QAAQ,EAAE;YAClC,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,K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