|
| 1 | +import {join} from 'node:path'; |
| 2 | +import {Sequelize, DataTypes} from 'sequelize'; |
| 3 | + |
| 4 | +export default class AssetCache { |
| 5 | + // db: any; |
| 6 | + sequelize: any; |
| 7 | + Asset: any; |
| 8 | + assets: any; |
| 9 | + fileCache: any; |
| 10 | + |
| 11 | + constructor({fileCache}: {fileCache: any}) { |
| 12 | + this.fileCache = fileCache; |
| 13 | + |
| 14 | + const theCachePath = join(this.fileCache.tmpDir, 'assets-cache'); |
| 15 | + |
| 16 | + // Create the cache DB if it doesn't exist |
| 17 | + // if (!this.fileCache.hasFile(join('assets-cache', 'assets.db'), 'tmp')) { |
| 18 | + // this.fileCache.writeTmpFileSync('', join('assets-cache', 'assets.db'), false); |
| 19 | + // } else { |
| 20 | + // // console.log(`File '${theCachePath + '/assets.db'}' already exists.`); |
| 21 | + // } |
| 22 | + // if (!this.fileCache.hasFile(join('assets', 'assets.db'), 'tmp')) { |
| 23 | + // this.fileCache.writeTmpFileSync('', join('assets', 'assets.db'), false); |
| 24 | + // } else { |
| 25 | + // // console.log(`File '${theCachePath + '/assets.db'}' already exists.`); |
| 26 | + // } |
| 27 | + |
| 28 | + // const sequelize = new Sequelize('sqlite::memory:'); |
| 29 | + // const sequelize = new Sequelize(theCachePath + '/assets.db'); |
| 30 | + const sequelize = new Sequelize({ |
| 31 | + dialect: 'sqlite', |
| 32 | + storage: theCachePath + '/assets.db', |
| 33 | + logging: false |
| 34 | + }); |
| 35 | + const Asset = sequelize.define('Asset', { |
| 36 | + src: DataTypes.STRING, |
| 37 | + status: DataTypes.NUMBER, |
| 38 | + localPath: DataTypes.STRING |
| 39 | + }); |
| 40 | + |
| 41 | + this.sequelize = sequelize; |
| 42 | + this.Asset = Asset; |
| 43 | + // this.db = db; |
| 44 | + } |
| 45 | + |
| 46 | + async init() { |
| 47 | + await this.sequelize.sync(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Add an item to the asset cache list |
| 52 | + * TODO: findOrCreate might make sense here |
| 53 | + */ |
| 54 | + async add(src: string) { |
| 55 | + // Check if it exists first, and return that if so |
| 56 | + const existingAsset = await this.Asset.findOne({where: {src: src}}); |
| 57 | + |
| 58 | + if (existingAsset) { |
| 59 | + return existingAsset; |
| 60 | + } |
| 61 | + |
| 62 | + // If not, insert it and return the new item |
| 63 | + const newAsset = await this.Asset.create({ |
| 64 | + src: src |
| 65 | + }); |
| 66 | + |
| 67 | + return newAsset; |
| 68 | + } |
| 69 | + |
| 70 | + async update(id: string, key: any, value: any) { |
| 71 | + // this.db.update((item: any) => item.uuid === uuid, (item: any) => ({...item, [key]: value})); |
| 72 | + |
| 73 | + return this.Asset.update({ |
| 74 | + [key]: value |
| 75 | + }, |
| 76 | + { |
| 77 | + where: { |
| 78 | + id: id |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get all the items from the asset cache list |
| 85 | + */ |
| 86 | + async getAll() { |
| 87 | + // TODO: Add pagination |
| 88 | + // return this.db.get(); |
| 89 | + return this.Asset.findAll(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Find an individual item form the asset cache list |
| 94 | + */ |
| 95 | + // async find(clauses: any) { |
| 96 | + // return this.db.get((item: any) => clauses(item)); |
| 97 | + // } |
| 98 | + |
| 99 | + /** |
| 100 | + * A common method to find an item by its src |
| 101 | + */ |
| 102 | + async findBySrc(src: string) { |
| 103 | + // return this.db.getOne((item: any) => item.src === src); // Returns the record with id 1 |
| 104 | + const existingAsset = await this.Asset.findOne({where: {src: src}}); |
| 105 | + return existingAsset; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Delete an item from the asset cache list |
| 110 | + */ |
| 111 | + // async deleteBySrc(src: string) { |
| 112 | + // this.db.delete((item: any) => item.src === src); |
| 113 | + // } |
| 114 | + |
| 115 | + /** |
| 116 | + * Used in tests, not intended for general use |
| 117 | + */ |
| 118 | + async _reset() { |
| 119 | + /* c8 ignore next 3 */ |
| 120 | + if (process.env.NODE_ENV !== 'test') { |
| 121 | + throw new Error('This method is only for use in tests'); |
| 122 | + } |
| 123 | + |
| 124 | + await this.Asset.drop(); |
| 125 | + } |
| 126 | +} |
0 commit comments