Crud sample question

Hi I’m trying to make my first little beginner actor but am not succeeding. I have no real goal other than to test some basic functions and get a feel for it. Not even sure the difference between an array and a list but was just trying to make a few list/arrays of type colors and just sort’ve add, merge, clear, etc. So idk if anyone knows how to add and query maybe to get me started that would be cool. I’ve been trying to look at example apps for guidance but some are more complicated than my level and others are just different and of course I’m trying to experiment and learn. Anyways any direction would be helpful and greatly appreciated!

// import Array "mo:base/Array";
import List "mo:base/List";
// type Iter<T> = Iter.Iter<T>;
// type Result<T,E> = Result.Result<T,E>;
// type UserId = Principal;

type Color = {
  #red;
  #orange;
  #yellow;
  #green;
  #blue;
  #purple;
};

// type Colors = [Color];

actor {
  stable var pallete : [Color] = [];
  flexible var tempPallete : [Color] = [#red];
  // flexible var tempPallete : Colors = Array.init<Colors>(10, #red);

  public query func getPalleteByIndex(n : Nat ) : async Color {
    List.get<Color>(tempPallete : [Color], n);
  };

  // public query func getPallete() : async Colors {
  //   pallete
  // };

  // public query func getTempPallete() : async Colors {
  //   tempPallete
  // };

  // public func addToTemp(color: Color) : async Colors {
  //   Array.append<Colors>(tempPallete, [color]) : Colors;
  //   tempPallete
  // };

  // public func mergeTemp() : async Colors {
  //   Array.append<Colors>(pallete, tempPallete) : Colors;
  //   tempPallete := [];
  //   pallete
  // };

  // public func clearPallete() : async Colors {
  //   pallete := [];
  //   pallete
  // };
}
1 Like

Hi jar,
I think Enzo’s example crud app could help you out here if you’d like to explore that, a link to the repo is on this thread:

Some of the tutorials also showcase basic CRUD functionality, eg:

Regarding list vs array -> Motoko’s arrays seem to be based on a classical array data structure, whereas its lists (including its associative list) seem to be using linked lists.

5 Likes

@jar, @matthewhammer also has a CRUD example available here, in case helpful.

2 Likes

Here’s one attempt at implementing the sketch above:
https://github.com/matthewhammer/motoko-crud/pull/13

Comments welcome and appreciated!

1 Like

Hey Matthew, question for you… should I still use a TrieMap for sequential reads with filtering & sorting? Basically, I want some SQL-like thing:

SELECT a, b, c FROM table WHERE a < 5 ORDER BY (b DESC, c ASC) LIMIT 100
1 Like

Hi Norton!

Honestly, because the libraries for Motoko are all so new, we don’t yet have satisfying data structure for relational data with the usual operations all there (products/joins, filters, sorting). Exploring that missing functionality is a big part of my motivation for writing the motoko-crud package, though it only supports non-relational databases at present.

For relational data, I would use nested Tries (the 2D and 3D versions, or deeper if necessary. The TrieMap module provides a wrapper that may get in the way for now.)

The Trie has nice support for joins, though I do not think your example query is doing any (right?).

All of the base collections support filtering and mapping, but not many support sorting (yet). RBTree is already sorted, but in this case (you only ask for LIMIT 100), you probably want to do something more intelligent, where you do not sort beyond the first 100 answers. That kind of functionality could be expressed with a lazy merge sort (lazily merging/sorting binary trees into lazy streams). I don’t have example code preprepared, but I can prepare some in the coming days. It’s part of what I want to do with motoko-crud anyway.

4 Likes

Heap also supports top-k. I wonder if we can write a generic collection library, similar to LINQ, and eventually make it incremental.

4 Likes

I wonder if we can write a generic collection library, similar to LINQ, and eventually make it incremental.

I know we can. : )

4 Likes

^^ This needs more likes, everyone!

6 Likes