# Crud sample question

**URL:** <https://forum.dfinity.org/t/crud-sample-question/1022>\
**Category:** Language Support\
**Created:** [August 8, 2020, 9:52pm UTC](https://forum.dfinity.org/t/crud-sample-question/1022 "2020-08-08T21:52:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jar/32/462_2.png) [@jar](https://forum.dfinity.org/u/jar)\
**Post date:** [August 8, 2020, 9:52pm UTC](https://forum.dfinity.org/t/crud-sample-question/1022/1 "2020-08-08T21:52:46Z")

</div>

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!

```auto
// 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
  // };
}

```

---

<div class="post-metadata">

**Author:** ![Ori](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Ori](https://forum.dfinity.org/u/Ori)\
**Post date:** [August 9, 2020, 9:44am UTC](https://forum.dfinity.org/t/crud-sample-question/1022/2 "2020-08-09T09:44:45Z")

</div>

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:

> [@Dfinity/superheroes](https://forum.dfinity.org/t/dfinity-superheroes/845):
>
> [[demo] ](https://www.youtube.com/watch?v=ioCW-9C8feE)

---

<div class="post-metadata">

**Author:** ![morrolan](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@morrolan](https://forum.dfinity.org/u/morrolan)\
**Post date:** [August 9, 2020, 11:04am UTC](https://forum.dfinity.org/t/crud-sample-question/1022/3 "2020-08-09T11:04:39Z")

</div>

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

- [Todo management with arrays](https://sdk.dfinity.org/docs/developers-guide/tutorials/multiple-actors.html)
- [Address management with associative lists](https://sdk.dfinity.org/docs/developers-guide/tutorials/phonebook.html)

Regarding list vs array -\> Motoko’s arrays seem to be based on a classical [array data structure](https://en.wikipedia.org/wiki/Array_data_structure), whereas its lists (including its associative list) seem to be using [linked lists](https://en.wikipedia.org/wiki/Linked_list).

---

<div class="post-metadata">

**Author:** ![alexa.smith](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alexa.smith/32/21_2.png) [@alexa.smith](https://forum.dfinity.org/u/alexa.smith)\
**Post date:** [August 10, 2020, 4:22pm UTC](https://forum.dfinity.org/t/crud-sample-question/1022/4 "2020-08-10T16:22:59Z")

</div>

@jar, @matthewhammer also has a CRUD example available [here](https://github.com/matthewhammer/motoko-crud), in case helpful.

---

<div class="post-metadata">

**Author:** ![matthewhammer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/matthewhammer/32/323_2.png) [@matthewhammer](https://forum.dfinity.org/u/matthewhammer)\
**Post date:** [August 10, 2020, 11:46pm UTC](https://forum.dfinity.org/t/crud-sample-question/1022/6 "2020-08-10T23:46:35Z")

</div>

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

Comments welcome and appreciated!

---

<div class="post-metadata">

**Author:** ![wang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/wang/32/446_2.png) [@wang](https://forum.dfinity.org/u/wang)\
**Post date:** [August 11, 2020, 2:11am UTC](https://forum.dfinity.org/t/crud-sample-question/1022/7 "2020-08-11T02:11:08Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![matthewhammer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/matthewhammer/32/323_2.png) [@matthewhammer](https://forum.dfinity.org/u/matthewhammer)\
**Post date:** [August 11, 2020, 3:49pm UTC](https://forum.dfinity.org/t/crud-sample-question/1022/8 "2020-08-11T15:49:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [August 12, 2020, 12:50am UTC](https://forum.dfinity.org/t/crud-sample-question/1022/9 "2020-08-12T00:50:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matthewhammer](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/matthewhammer/32/323_2.png) [@matthewhammer](https://forum.dfinity.org/u/matthewhammer)\
**Post date:** [August 12, 2020, 11:03pm UTC](https://forum.dfinity.org/t/crud-sample-question/1022/10 "2020-08-12T23:03:15Z")

</div>

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

I know we can. : )

---

<div class="post-metadata">

**Author:** ![Ori](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Ori](https://forum.dfinity.org/u/Ori)\
**Post date:** [August 13, 2020, 6:04am UTC](https://forum.dfinity.org/t/crud-sample-question/1022/11 "2020-08-13T06:04:06Z")

</div>

^^ This needs more likes, everyone!
