forked from Superalgos/Superalgos
-
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.
added localstorage to schema
- Loading branch information
Showing
5 changed files
with
148 additions
and
19 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,55 @@ | ||
/** | ||
* @typedef {{ | ||
* initialize: initialize, | ||
* finalize: finalize, | ||
* saveItem: saveItem, | ||
* saveAll: saveAll, | ||
* deleteItem: deleteItem, | ||
* deleteAll: deleteAll, | ||
* findItem: findItem, | ||
* findMany: findMany, | ||
* }} PersistenceModel | ||
*/ | ||
|
||
/** | ||
* Returns a function the creates a new persistence store matching the storeType | ||
* (file, mongodb) with the name being either | ||
* the file path for files or database name for a database | ||
* @returns {{ | ||
* newPersistenceStore: (storeType: string, name: string) => Promise<PersistenceModel> | ||
* }} | ||
*/ | ||
exports.newLocalStorageGlobalsPersistence = function newLocalStorageGlobalsPersistence() { | ||
const thisObject = { | ||
newPersistenceStore: newPersistenceStore | ||
} | ||
|
||
let databaseRepositories = undefined | ||
|
||
return thisObject | ||
|
||
/** | ||
* Returns a new implementation of the chosen data store, | ||
* if the storeType does not match any of the current | ||
* implementations then an error is thrown. | ||
* | ||
* @param {string} storeType | ||
* @param {string} name | ||
*/ | ||
async function newPersistenceStore(storeType, name) { | ||
switch (storeType) { | ||
case 'file': | ||
return require('../Modules/FileStore').newNetworkGlobalsStoresFileStore(name) | ||
case 'database': | ||
return await getDatabaseRepositories().getRepository(name) | ||
default: throw new Error('The store type ' + storeType + ' is not implemented, why not create it yourself.') | ||
} | ||
} | ||
|
||
function getDatabaseRepositories() { | ||
if(databaseRepositories === undefined) { | ||
databaseRepositories = SA.projects.internalStorage.modules.databaseRepositories() | ||
} | ||
return databaseRepositories | ||
} | ||
} |
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,53 @@ | ||
/** | ||
* | ||
* @returns {{ | ||
* getRepository: (name: string) => import('../Globals/Persistence').PersistenceModel | ||
* }} | ||
*/ | ||
exports.newDatabaseRepositories = function newDatabaseRepositories() { | ||
const thisObject = { | ||
getRepository: getRepository | ||
} | ||
|
||
/** | ||
* This property should only be accessed using the getDbContext function | ||
* as it will operate as a singleton for all the repositories and only be | ||
* initialized when first required | ||
* | ||
* @type {import('../Internal/DbContext').DbContext} | ||
*/ | ||
let privateDbContext = undefined | ||
|
||
return thisObject | ||
|
||
|
||
async function getRepository(name) { | ||
switch (name) { | ||
case 'UserBalances': | ||
const repo = require('../Repositories/UserBalanceRepository').newUserBalanceRepository(await getDbContext()) | ||
await repo.initialize() | ||
return repo | ||
default: | ||
throw new Error('The database repository ' + name + ' is not implemented, why not create it yourself.') | ||
|
||
} | ||
} | ||
|
||
/** | ||
* | ||
* @returns {Promise<import('../Internal/DbContext').DbContext>} | ||
*/ | ||
async function getDbContext() { | ||
if (privateDbContext === undefined) { | ||
privateDbContext = require('../Internal/DbContext').newDbContext() | ||
return await privateDbContext.intialize({ | ||
databse: global.env.DATABASE.name, | ||
host: global.env.DATABASE.host, | ||
password: global.env.DATABASE.password, | ||
port: global.env.DATABASE.port, | ||
user: global.env.DATABASE.username, | ||
}).then(() => privateDbContext) | ||
} | ||
return privateDbContext | ||
} | ||
} |
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