Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): Datastore package p/jeronimoalbi/datastore #3698

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

jeronimoalbi
Copy link
Member

@jeronimoalbi jeronimoalbi commented Feb 6, 2025

The purpose of this package is to support a data store where multiple collection based storages could be defined.

Each named storage defines a collection of records where each record can optionally and by default have any number of user defined fields, which can be constrained to a pre defined set of fields by using a schema definition.

The implementation is a layer on top of p/moul/collection.

Example of datastore package usage:

package main

import (
	ds "gno.land/p/jeronimoalbi/datastore"
)

func main() {
	var db ds.Datastore

	// Define a unique case insensitive index for user emails
	emailIdx := ds.NewIndex("email", func(r ds.Record) string {
		return r.MustGet("email").(string)
	}).Unique().CaseInsensitive()

	// Create a new storage for user records
	storage := db.CreateStorage("users", ds.WithIndex(emailIdx))

	// Add a user with a single "email" field
	u := storage.NewRecord()
	u.Set("email", "foo@bar.com")

	// Save to assing user ID and update index(es)
	u.Save()
	println("Records =", storage.Size())

	// Find the user by email
	print("Search by Email ... ")
	record := storage.WithIndex("email").GetFirst("foo@bar.com")
	if record != nil {
		println("ID =", record.ID())
	}

	// Find user by ID
	print("Search by ID ... ")
	record = storage.GetFirst(u.Key())
	if record != nil {
		email := record.MustGet("email").(string)
		println("Email =", email)
	}

	// Delete the user from the storage and update index(es)
	storage.Delete(u.ID())
	println("Records =", storage.Size())
}

// Output:
// Records = 1
// Search by Email ... ID = 1
// Search by ID ... Email = "foo@bar.com"
// Records = 0

This adds typing support for record collection indexing functions.
It also enforces sparse indexes which are required to make sure they
work when record fields are removed or renamed.
Datastore allows the creation of multiple collection based storages.
@jeronimoalbi jeronimoalbi self-assigned this Feb 6, 2025
@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Feb 6, 2025
@Gno2D2 Gno2D2 requested a review from a team February 6, 2025 16:01
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Feb 6, 2025

🛠 PR Checks Summary

🔴 Pending initial approval by a review team member, or review from tech-staff

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
Read More

🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers.

✅ Automated Checks (for Contributors):

🔴 Pending initial approval by a review team member, or review from tech-staff

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Pending initial approval by a review team member, or review from tech-staff

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 Not (🔴 Pull request author is a member of the team: tech-staff)

Then

🔴 Requirement not satisfied
└── 🔴 If
    ├── 🔴 Condition
    │   └── 🔴 Or
    │       ├── 🔴 At least 1 user(s) of the organization reviewed the pull request (with state "APPROVED")
    │       ├── 🔴 At least 1 user(s) of the team tech-staff reviewed pull request
    │       └── 🔴 This pull request is a draft
    └── 🔴 Else
        └── 🔴 And
            ├── 🟢 This label is applied to pull request: review/triage-pending
            └── 🔴 On no pull request

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission

Copy link

codecov bot commented Feb 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@jeronimoalbi jeronimoalbi marked this pull request as ready for review February 7, 2025 19:00
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧾 package/realm Tag used for new Realms or Packages. review/triage-pending PRs opened by external contributors that are waiting for the 1st review
Projects
Status: Triage
Development

Successfully merging this pull request may close these issues.

2 participants