replit-database
replit-database is a database client for interacting with a replit database. The official package is here, but this one implements a lot of new features, such as schemas, update functionality and some search functions in the database values. This package is more complex than the official one, but thanks to it you will be able to do much more. The way it works is similar to mongoose, even if it's not as fast as mongoose.
You can find an example of an app using this package here.
Get started
Install replit-database:
npm i replit-database
Use it in a project:
import { Client, Schema } from 'replit-database'
// or
const { Client, Schema } = require('replit-database');
const UserSchema = new Schema({
username: {
type: 'string',
required: true
},
bio: 'string'
});
const db = new Client();
const User = db.addSchema('user', UserSchema);
const user = new User({
username: 'nathanTi',
bio: 'This is my bio'
});
await user.save();
In this example, you've imported two classes of the replit-database
module. The first, called Client
will be used to create the database instance. The other one, Schema
, is used to create a data scheme.
Then, you create a schema for the users. To create a schema, you create an instance of he Schema
class, and you pass some options to it. You have to specify the data type for all the keys of a user. There are four data types: string
, boolean
, number
and object
(an array is considered as an object).
Then, you create a database instance, and you add the schema to the database. It returns a class.
After that, you create an instance of the User
class, and you save it.
Docs
Client
class Client(String url)
Returns an instance of the Client
class.
const db = new Client();
addSchema(String name, Schema schema)
const db = new Client();
db.addSchema('user', UserSchema);
list(String prefix?)
const db = new Client();
const keys = await db.list();
clear()
const db = new Client();
await db.clear();
Schema
class Schema(Object options)
const UserSchema = new Schema({
username: {
type: 'string',
default: 'Anonymous',
required: true
},
createdAt: 'string',
});
Document
class Document(Object data)
This code creates the class Model
, and its instance model
that is a document.
const db = new Client();
const ModelSchema = new Schema({
key1: {
type: 'string',
default: 'value1',
required: true
},
key2: 'number',
key3: 'object',
key4: {
type: 'boolean',
default: false
}
});
// we create the model
const Model = db.addSchema(ModelSchema);
// we create a document
const model = new Model({
key1: 'test',
key2: 1,
key3: [],
key4: true
});
save()
Save the document
await model.save();
delete()
Delete the document
await model.delete();
getId()
Get the doc's id.
console.log(model.getId());
getValue()
Get the doc's value
console.log(model.getValue());
getKey()
Get the doc's key
console.log(model.getKey());
Interact with a model
const db = new Client();
const ModelSchema = new Schema({
key1: {
type: 'string',
default: 'value1',
required: true
},
key2: 'number',
key3: 'object',
key4: {
type: 'boolean',
default: false
}
});
// we create the model
const Model = db.addSchema(ModelSchema);
findById(String id)
Find a Model's document with its id. Returns a document.
const doc = await Model.findById(/* id */)
listKeys()
Returns an array of keys of the Model's documents
const keys = await Model.listKeys();
listIds()
Returns an array of ids of the Model's documents
const keys = await Model.listIds();
listIds()
Returns an array of ids of the Model's documents
const keys = await Model.listIds();
list(Object query)
It will search the documents with a value that match the query (the where
key).
In this query, you can specify some fields, that the value must have. For example, if you set in the query a value called name
, and its value is John Doe
, it will retrieve a document which has John Doe
as value for the name key.
// query
{
where: {
name: 'John Doe'
}
}
// document's value that match
{
name: 'John Doe'
}
On the other hand, you can specify many possible options. In your query, instead of setting a String as value of the name key, you can set an array, with different options, and it will match if a key's value has one of the options as value for the name key.
// query
{
where: {
name: ['John', 'Jane']
}
}
// a document's value that match
{
name: 'Jane'
}
But if the a value has a key called name, with an array as value, the query will match only if this array contains at least all the items in the query's name array.
// query
{
where: {
names: ['John', 'Doe']
}
}
// a document's value that match
{
names: ['John', 'Jane', 'Doe']
}
An example of query:
const keys = await Model.list({
where: {
key1: 'test',
key2: [5, 10]
},
limit: 10,
offset: 5
});
You can add some more parameters, such as a limit (a number of results you want to have) or an offset (the query will skip the first documents)
deleteById(String id)
Delete a model's document with a specified id
await Model.deleteById(/* id */);
deleteMany(Object query)
Delete some model's documents that matches a query
await Model.deleteMany({
where: {/* ... */}
});
deleteAll()
Delete all the Model's documents
await Model.deleteAll();
find(Object query)
Same as Model.list
Same as
const docs = await Model.find(/* ... */);
findOne(Object query)
Same as find, but returns only the first document that matches
const doc = await Model.findOne(/* ... */);
count()
Same as find, but returns only the first document that matches
const doc = await Model.findOne(/* ... */);
findOneAndUpdate(Object query, Object data)
Same as find, but returns only the first document that matches
const doc = await Model.findOneAndUpdate({
where: {/* query */}
}, {
key1: 'another test',
key4: false
});