TypeORM + SQLite based storage plug-in
In your ARK Core directory:
cd plugins
git clone https://github.com/nos/core-plugin-storage.git
Add the plug-in to your network's plugins.js after the postgres module:
...
"@arkecosystem/core-database-postgres": {
...
},
"@nosplatform/storage": {},
...
Delete the entities (not index.ts and tsconfig.json) from the entities dir as these are native to the nOS blockchain.
Create TypeORM entities into the plugin's entities directory.
Example User.ts entity:
import{Entity,PrimaryGeneratedColumn,Column,BaseEntity}from"typeorm";
@Entity()exportclassUserextendsBaseEntity{
@PrimaryGeneratedColumn("int")id: number;
@Column("varchar")firstName: string;
@Column("varchar")lastName: string;
@Column("int")age: number;}Include the User entity in the plugin's src/storage.ts in createConnection({...}):
awaitcreateConnection({type: "sqlite",database: dbPath,// Import entities to connectionentities: [User],synchronize: true,});yarn build in the plug-in dir.
Import q and your entity into any other ARK Core module or plug-in and use it.
Example:
import{q,User}from"@nosplatform/storage";q(async()=>{constuser=newUser();user.firstName="Timber";user.lastName="Saw";user.age=25;awaituser.save();constallUsers=awaitUser.find();constfirstUser=awaitUser.findOne(1);consttimber=awaitUser.findOne({firstName: "Timber",lastName: "Saw"});awaittimber.remove();});Wrapping your TypeORM database tasks in the q module executes the task in a queue. This way, database write tasks can be executed virtually simultaneously without blocking the blockchain's consensus and synchronization processes.
If you don't wrap your database tasks in q(), you might run into issues with concurrent sqlite database writes.
If you discover a security vulnerability within this package, please send an e-mail to contact@nos.io. All security vulnerabilities will be promptly addressed.