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

wasp

v1.0.0

Published

The modern TypeScript local database

Downloads

57

Readme

Wasp

The modern TypeScript local database.

Quick Start

You need to have Node.js installed.

Next step is install the wasp library:

npm install wasp --save

First of all we need to know how the data will be saved and validated. For it, we need to create our own Schema instance.

interface Post {
    title: string;
    content: string;
}

const postSchema = new Schema<Post>({
    title: Schema.string(),
    content: Schema.string(),
});

Now we have to create the Model where the data will be saved.

const posts = new Model('posts', postSchema);

The Model class is where the magic happens, where our documents will be saved, created, updated, deleted and found. Each document has all properties declared in the Schema used in the model.

Let's create some post:

const newPost = post.create({
    title: 'Wasp',
    content: 'Why wasp is so good?',
    id: '123',
});

console.log(newPost.title); // Prints: "Wasp"

Each document has an unique ID (identifier), so you can pass your own id when creating a new document with <Model>.create function.

We need to find our documents that we created before, for it we use the findById function.

const posts = new Model('posts', postSchema);

// Return { "title": "Wasp", "content": "Why wasp is so good?", "id": "123" }
posts.findById('123');

Sometimes we need to delete some unused document or a document that we don't want, so we use the <Model>.findByIdAndDelete function.

const posts = new Model('posts', postSchema);

// Return the data and delete the document in the schema
posts.findByIdAndDelete('123');

Congrats

You finnaly know how to create/read/delete data with wasp database!

Managing the data by yourself

If you want to manage all data by yourself, use JSONDriver instead of createModel

import { JSONDriver } from 'wasp';

const driver = new JSONDriver(PATH);

// Updates the data, the `update` function doesn't care with the function return value
driver.update((data) => {
    data['Me'] = { username: 'Unknown' };
});

/**
 * {
 *    "Me": {
 *       "username": "Unknown"
 *    }
 * }
 */
driver.read();

Schema

Schemas are the best way to validate data with the Wasp library.

  • Definition: new Schema(shape)

Types

  • Any (Any value)
  • BigInt
  • Boolean
  • Date
  • Number (not a bigint)
  • String

Types Example

// String
Schema.string();

// BigInt
Schema.bigint();

// Boolean
Schema.boolean();

// Date
Schema.date();

// Number
Schema.number();

// Any
Schema.any();

Example

interface User {
    username: string;
    age: number;
    birthday?: Date;
}

const userSchema = new Schema<User>({
    username: Schema.string(),
    age: Schema.number().integer(),
    birthday: Schema.date().optional(),
});
const users = createModel('users', userSchema);

// Returns an instance of `Document<User>`
users.create({ username: 'Unknown', age: 5 });

Model

Models are the base for creating, reading, updating and deleting a document and other stuffs.

There are two ways to create a model, using Model class, or using createModel function.

Examples

Creating a model

import { Model, createModel } from 'wasp';

const users = new Model('users', userSchema);

const users = createModel('users', userSchema);

Using a model

const postSchema = new Schema({
    title: Schema.string(),
    content: Schema.string(),
    createdAt: Schema.date(),
});
const posts = createModel('posts', postSchema);

const { data } = posts.create({
    title: 'Wasp Database',
    content: 'The new modern TypeScript local database!',
    createdAt: new Date(),
});

// Prints: "Wasp Database"
console.log(data.title);