shadertoy_sqlite 2.0.3
shadertoy_sqlite: ^2.0.3 copied to clipboard

A implementation of the shadertoy storage API using Sqlite as the storage backend

shadertoy_sqlite #

A shadertoy storage implementation on sqlite using the moor package

Pub Package Coverage Status Package Documentation GitHub License

Overview #

This package implements the storage APIs defined in the shadertoy package on sqlite through the moor package

Getting Started #

Add shadertoy_sqlite to your pubspec.yaml replacing x.x.x with the latest version available:

dependencies:
    shadertoy_sqlite: ^x.x.x

Run the following command to install dependencies:

dart pub get

Finally, to start developing import the library:

import 'package:shadertoy_sqlite/shadertoy_sqlite.dart';

Usage #

To create a new store the ShadertoyStore implementation needs to be instanciated. The example bellow creates a new moor store with an in memory implementation (mostly used for test purposes)

final store = newShadertoySqliteStore(VmDatabase.memory(logStatements: logStatements));

A more real example would entail de creation of a database backed by a file like so:

final store = newShadertoyMoorStore(VmDatabase(File('shadertoy.db')))

This allows the execution of persistent operations, for example storing the definition of a shader with:

final shader = Shader(...);
final ssr = await store.saveShader(shader);
if (ssr.ok) {
    print('Shader stored');
} else {
    print('Error: ${response.error.message}')
}

A more complete example bellow:

import 'package:moor/ffi.dart';
import 'package:moor/moor.dart';
import 'package:shadertoy/shadertoy_api.dart';
import 'package:shadertoy_sqlite/shadertoy_sqlite.dart';

// In-memory executor
QueryExecutor memoryExecutor({bool logStatements = false}) {
  return VmDatabase.memory(logStatements: logStatements);
}

// File executor
QueryExecutor diskExecutor({bool logStatements = false}) {
  return VmDatabase(File('shadertoy.db'), logStatements: logStatements);
}

void main(List<String> arguments) async {
  // Creates a new store with an in-memory executor
  final store = newShadertoySqliteStore(memoryExecutor());

  // Creates user 1
  final userId1 = 'UzZ0Z1';
  final user1 =
      User(id: userId1, about: 'About user 1', memberSince: DateTime.now());
  await store.saveUser(user1);

  // Retrieves user 1
  await store.findUserById(userId1);

  // Creates user 2
  final userId2 = 'UzZ0Z2';
  final user2 =
      User(id: userId2, about: 'About user 2', memberSince: DateTime.now());
  await store.saveUser(user2);

  // Retrieves user 2
  await store.findUserById(userId2);

  // Creates a shader with two render passes
  final shaderId1 = 'SzZ0Zz';
  final shader1 = Shader(
      version: '0.1',
      info: Info(
          id: shaderId1,
          date: DateTime.fromMillisecondsSinceEpoch(1360495251),
          views: 131083,
          name: 'Example',
          userId: userId1,
          description: 'A shader example',
          likes: 570,
          privacy: ShaderPrivacy.publicApi,
          flags: 32,
          tags: [
            'procedural',
            '3d',
            'raymarching',
            'distancefield',
            'terrain',
            'motionblur',
            'vr'
          ],
          hasLiked: false),
      renderPasses: [
        RenderPass(
            name: 'Image',
            type: RenderPassType.image,
            description: '',
            code: 'code 0',
            inputs: [
              Input(
                  id: '257',
                  src: '/media/previz/buffer00.png',
                  type: InputType.texture,
                  channel: 0,
                  sampler: Sampler(
                      filter: FilterType.linear,
                      wrap: WrapType.clamp,
                      vflip: true,
                      srgb: true,
                      internal: 'byte'),
                  published: 1)
            ],
            outputs: [
              Output(id: '37', channel: 0)
            ]),
        RenderPass(
            name: 'Buffer A',
            type: RenderPassType.buffer,
            description: '',
            code: 'code 1',
            inputs: [
              Input(
                  id: '17',
                  src: '/media/a/zs098rere0323u85534ukj4.png',
                  type: InputType.texture,
                  channel: 0,
                  sampler: Sampler(
                      filter: FilterType.mipmap,
                      wrap: WrapType.repeat,
                      vflip: false,
                      srgb: false,
                      internal: 'byte'),
                  published: 1)
            ],
            outputs: [
              Output(id: '257', channel: 0)
            ])
      ]);

  // Saves the shader
  await store.saveShader(shader1);

  // Retrieves the saved shader
  await store.findShaderById(shaderId1);

  // Creates the first comment
  final comment1 = Comment(
      id: 'CaA0A1',
      shaderId: shaderId1,
      userId: userId2,
      picture: '/img/profile.jpg',
      date: DateTime.now(),
      text: 'Great shader!');

  // Creates the second comment
  final comment2 = Comment(
      id: 'CaA0A2',
      shaderId: shaderId1,
      userId: userId1,
      picture: '/img/profile.jpg',
      date: DateTime.now(),
      text: 'Thanks');

  // Save shader comments
  await store.saveShaderComments([comment1, comment2]);

  // Retrieves the shader comments
  await store.findCommentsByShaderId(shaderId1);

  // Creates a playlist
  final playlistId1 = 'week';
  final playlist1 = Playlist(
      id: playlistId1,
      userId: 'shadertoy',
      name: 'Shaders of the Week',
      description: 'Playlist with every single shader of the week ever.');

  // Save the playlist
  await store.savePlaylist(playlist1);

  // Retrieves the playlist
  await store.findPlaylistById(playlistId1);

  // Stores the shader on the playlist
  await store.savePlaylistShaders(playlistId1, [shaderId1]);

  // Retrieves the playlist shader ids
  await store.findShaderIdsByPlaylistId(playlistId1);
}

APIs #

Storage API #

OperationDescription
findUserByIdFinds a Shadertoy user by id
findAllUserIdsReturns all the user ids stored
findAllUsersReturns all the users stored
saveUserSaves a user
saveUsersSaves a list of users
deleteUserByIdDeletes a user by id
findShaderByIdFinds a shader by id
findAllShaderIdsFetches all the shader ids
findShaderIdsQueries shader ids by term, tags and sort them by name, likes, views, newness and by hotness (proportional to popularity and inversely proportional to lifetime). All the query results are paginated through the from and num parameters
findShadersByIdSetFinds shaders by a set of ids
findShadersQueries shaders by term, tags and sort them by name, likes, views, newness and by hotness (proportional to popularity and inversely proportional to lifetime). All the query results are paginated through the from and num parameters
findAllShadersReturns all the shaders stored
saveShaderSaves a shader
saveShadersSaves a list of shaders
deleteShaderByIdDeletes a shader by id
findCommentByIdFetches a comment by id
findAllCommentIdsFetches all the comment ids
findCommentsByShaderIdFetches the comments of a shader id
findAllCommentsReturns all the comments stored
saveShaderCommentsSaves the comments of a shader
deleteCommentByIdDeletes a comment by id
findShadersByUserIdQueries shaders by user id, tags and allows sorting by name, likes, views, newness and hotness (proportional to popularity and inversely proportional to lifetime). All the query results are paginated and the pages fetched with the from and num parameters
findShaderIdsByUserIdQueries shader ids by user id, tags and allows sorting by name, likes, views, newness and hotness (proportional to popularity and inversely proportional to lifetime). All the query results are paginated and the pages fetched with the from and num parameters
findAllShaderIdsByUserIdFetches all the shader ids by user id
findPlaylistByIdFetches a playlist by id
findAllPlaylistIdsFetches all the playlist ids
findAllPlaylistsReturns all the playlists stored
findShadersByPlaylistIdFetches the shaders of a playlist id. All the query results are paginated through the from and num parameters
findShaderIdsByPlaylistIdFetches the shader ids of a playlist id. All the query results are paginated through the from and num parameters
findAllShaderIdsByPlaylistIdFetches all the shader ids of playlist id
savePlaylistSaves a playlist
savePlaylistShadersSaves the playlist shader ids
deletePlaylistByIdDeletes a playlist by id

Model #

Shadertoy Model

Contributing #

This is a unofficial Shadertoy storage API. It is developed by best effort, in the motto of "Scratch your own itch!", meaning APIs that are meaningful for the author use cases.

If you would like to contribute with other parts of the API, feel free to make a Github pull request as I'm always looking for contributions for:

  • Tests
  • Documentation
  • New APIs

See CONTRIBUTING.md for ways to get started.

Features and Bugs #

Please file feature requests and bugs at the issue tracker.

License #

This project is licensed under the MIT License - see the LICENSE file for details

0
likes
120
pub points
30%
popularity

Publisher

ivoleitao.dev

A implementation of the shadertoy storage API using Sqlite as the storage backend

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

enum_to_string, moor, shadertoy

More

Packages that depend on shadertoy_sqlite