-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a0f63e
commit da3a1ef
Showing
11 changed files
with
591 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import S3Engine from '../../src/engine/S3Engine.js'; | ||
|
||
export default S3Engine; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,7 +141,7 @@ export default class Engine { | |
} | ||
|
||
static toString() { | ||
return 'Engine'; | ||
return this.name; | ||
} | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {GetObjectCommand, PutObjectCommand} from '@aws-sdk/client-s3'; | ||
|
||
import Engine from './Engine.js'; | ||
|
||
export default class S3Engine extends Engine { | ||
static async getById(id) { | ||
const objectPath = [this._configuration.prefix, `${id}.json`].join('/'); | ||
|
||
try { | ||
const data = await this._configuration.client.send(new GetObjectCommand({ | ||
Bucket: this._configuration.bucket, | ||
Key: objectPath, | ||
})); | ||
return JSON.parse(await data.Body.transformToString()); | ||
} catch (_error) { | ||
return undefined; | ||
} | ||
} | ||
|
||
static async findByValue(model, parameters) { | ||
const index = await this.getIndex(model.name); | ||
return Object.values(index) | ||
.filter((model) => | ||
Object.entries(parameters) | ||
.some(([name, value]) => model[name] === value), | ||
); | ||
} | ||
|
||
static async putModel(model) { | ||
const Key = [this._configuration.prefix, `${model.id}.json`].join('/'); | ||
|
||
await this._configuration.client.send(new PutObjectCommand({ | ||
Key, | ||
Body: JSON.stringify(model.toData()), | ||
Bucket: this._configuration.bucket, | ||
ContentType: 'application/json', | ||
})); | ||
} | ||
|
||
static async getIndex(location) { | ||
try { | ||
const data = await this._configuration.client.send(new GetObjectCommand({ | ||
Key: [this._configuration.prefix].concat([location]).concat(['_index.json']).filter(e => !!e).join('/'), | ||
Bucket: this._configuration.bucket, | ||
})); | ||
|
||
return JSON.parse(await data.Body.transformToString()); | ||
} catch (_error) { | ||
return {}; | ||
} | ||
} | ||
|
||
static async putIndex(index) { | ||
const processIndex = async (location, models) => { | ||
const modelIndex = Object.fromEntries(models.map(m => [m.id, m.toIndexData()])); | ||
const Key = [this._configuration.prefix].concat([location]).concat(['_index.json']).filter(e => !!e).join('/'); | ||
|
||
const currentIndex = await this.getIndex(location); | ||
|
||
await this._configuration.client.send(new PutObjectCommand({ | ||
Key, | ||
Bucket: this._configuration.bucket, | ||
ContentType: 'application/json', | ||
Body: JSON.stringify({ | ||
...currentIndex, | ||
...modelIndex, | ||
}), | ||
})); | ||
}; | ||
|
||
for (const [location, models] of Object.entries(index)) { | ||
await processIndex(location, models); | ||
} | ||
|
||
await processIndex(null, Object.values(index).flat()); | ||
} | ||
} |
Oops, something went wrong.