A simplistic redis clone, whereby it stores key-value pairs and is accessible programmatically, or via the provided CLI.
Sets a particular key to the specified value in the datastore.
> SET x 10
> GET x
10
Retrieves the specified key's value.
> SET x 10
> GET x
10
Deletes the specified key.
> SET x 10
> GET x
10
> UNSET x
> GET x
NULL
Retrieves the number of keys with the specified value.
> SET x 10
> GET x
10
> SET y 10
> NUMEQUALTO 10
2
Starts a new transaction. Transactions can be nested.
> SET x 10
> GET x
10
> BEGIN
> SET x 20
> GET x
20
> ROLLBACK
> GET x
10
Commits the changes made to the datastore.
> SET x 10
> GET x
10
> BEGIN
> SET x 20
> GET x
20
> COMMIT
> GET x
20
Undoes any changes made to the datastore during the current transaction.
> SET x 10
> GET x
10
> BEGIN
> SET x 20
> GET x
20
> ROLLBACK
> GET x
10
Terminates the CLI and triggers it's end
event.
> SET 10
> END
(program terminates)
In addition to using the CLI, you can also access the datastore programmatically.
None.
const store = new Datastore();
Sets the key to specified value. Will trigger the set
event with key, old value, and new value.
key
(string): The key to set.value
(value): The value to associate with the key.
Nothing.
store.set('x', '10');
Retrieves the key's associated value.
key
(string): The key to lookup.
(string): The value associated with the key in the store.
store.set('x', '10');
const result = store.get('x');
console.log(result);
// 10
Deletes the key in the store. Will emit the unset
event with key, and key's old value.
key
(string): The key to delete.
Nothing.
store.set('x', '10');
let result = store.get('x');
console.log(result);
// 10
store.unset('x');
let result = store.get('x');
console.log(result);
// NULL
Returns a count of the number of keys matching the value specified.
value
(string): The value to test against.
(int): The number of keys that match value
;
store.set('x', '10');
store.set('y', '10');
let result = store.numEqualTo('10');
console.log(result);
// 2
- key (string): The key that was set.
- oldValue (string): The value of key prior to being set.
- newValue (string): The new value of key.
- key (string): The key that was unset.
- oldValue (string): The value of key prior to being unset.
datastore
(Datastore): The datastore to apply transactions to.
const store = new Datastore();
const manager = new TransactionManager(store);
Starts a new transaction.
None.
Nothing.
manager.begin();
Starts a new transaction.
None.
Nothing.
store.set('x', '10');
manager.begin();
store.set('x', '20');
manager.commit();
const result = store.get('x');
console.log(result);
// 20
Starts a new transaction.
None.
Nothing.
store.set('x', '10');
manager.begin();
store.set('x', '20');
manager.rollback();
const result = store.get('x');
console.log(result);
// 10
The Cli constructor will either use the datastore provided, or create its own. It will create its own TransactionManager instance.
datastore
(Datastore): The datastore to attach the CLI to.
const store = new Datastore();
const cli = new Cli(store);
Emits the end
event.
None.
Nothing.
cli.end();
Emitted when a call to the end()
method is made.
You will need the following things properly installed on your computer.
git clone <repository-url>
this repository- change into the new directory
npm install
npm run-script build
npm start
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
MIT © Asa Rudick