npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

replit-database

v1.0.1

Published

A package to interact with your repl's database using an interface similar to mongoose.

Downloads

6

Readme

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, numberand 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
});