Skip to content

Commit

Permalink
🚧⚙️ Work on Prisma DB
Browse files Browse the repository at this point in the history
  • Loading branch information
MulverineX committed Sep 10, 2023
1 parent 570e100 commit 7e88088
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 66 deletions.
4 changes: 2 additions & 2 deletions packages/client-flutter/lib/app/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_js/extensions/fetch.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:path/path.dart';
Expand Down Expand Up @@ -122,7 +123,6 @@ class AppCore extends InheritedWidget {

final cascade = Cascade().add(shelf_router.Router()..post('/', updateRes));

// TODO: Set this to false before every release
if (true) await shelf_io.serve(logRequests().addHandler(cascade.handler), InternetAddress.anyIPv4, 4578);
if (kDebugMode) await shelf_io.serve(logRequests().addHandler(cascade.handler), InternetAddress.anyIPv4, 4578);
}
}
1 change: 1 addition & 0 deletions packages/core/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
# Keep environment variables out of version control
.env
pack/database
4 changes: 4 additions & 0 deletions packages/core/pack/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default [
...require('./polyfill/index'),
require('./database/index'),
]
3 changes: 3 additions & 0 deletions packages/core/pack/polyfill/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default [
require('./timeout')
]
51 changes: 51 additions & 0 deletions packages/core/pack/polyfill/timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const webpack = require('webpack');

var __polyfillTimeout_timers = {};
function __polyfillTimeout_generateNewId() {
var r = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
while (__polyfillTimeout_timers.hasOwnProperty(r)) { // check weather the id already exists
r = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
}
return r;
}


function __polyfillTimeout_check() {
if (Object.keys(__polyfillTimeout_timers).length <= 0) return;

var t = Date.now();
for (var timerId in __polyfillTimeout_timers) {
if (__polyfillTimeout_timers.hasOwnProperty(timerId) && t > __polyfillTimeout_timers[timerId].time) {
__polyfillTimeout_timers[timerId].callback();
clearTimeout(timerId);
}
}
requestIdleCallback(__polyfillTimeout_check);
}


function _setTimeout(callback, delay) {
if (typeof(callback) != 'function') throw new Error("callback should be a function");
if (typeof(delay) != 'number' || delay < 0) throw new Error("delay should be a positive number");
var newId = __polyfillTimeout_generateNewId();
__polyfillTimeout_timers[newId] = {
callback: callback,
time: Date.now() + delay
};
if (Object.keys(__polyfillTimeout_timers).length == 1) {
requestIdleCallback(__polyfillTimeout_check);
}
return newId;
}

function _clearTimeout(id) {
if (__polyfillTimeout_timers.hasOwnProperty(id)) delete __polyfillTimeout_timers[id];
}

export default timeout = new webpack.DefinePlugin({
__polyfillTimeout_timers,
__polyfillTimeout_generateNewId,
__polyfillTimeout_check,
setTimeout: _setTimeout,
clearTimeout: _clearTimeout,
})
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@prisma/client": "5.2.0",
"@types/uuid": "^8.3.4",
"core-js": "^3.32.2",
"locale-codes": "^1.3.1",
Expand Down
Binary file added packages/core/prisma/dev.db
Binary file not shown.
Binary file added packages/core/prisma/dev.db-journal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- CreateTable
CREATE TABLE "Track" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL,
"duration" BIGINT NOT NULL,
"track_number" INTEGER NOT NULL,
CONSTRAINT "Track_id_fkey" FOREIGN KEY ("id") REFERENCES "Collection" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Artist" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"profile_image" TEXT NOT NULL,
"name" TEXT NOT NULL,
"biography" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "User" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"profile_image" TEXT NOT NULL,
"name" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "Collection" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL,
"art" TEXT,
"year" INTEGER,
"duration" BIGINT NOT NULL,
CONSTRAINT "Collection_id_fkey" FOREIGN KEY ("id") REFERENCES "Artist" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Collection_id_fkey" FOREIGN KEY ("id") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "_ArtistToTrack" (
"A" INTEGER NOT NULL,
"B" INTEGER NOT NULL,
CONSTRAINT "_ArtistToTrack_A_fkey" FOREIGN KEY ("A") REFERENCES "Artist" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "_ArtistToTrack_B_fkey" FOREIGN KEY ("B") REFERENCES "Track" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "_ArtistToTrack_AB_unique" ON "_ArtistToTrack"("A", "B");

-- CreateIndex
CREATE INDEX "_ArtistToTrack_B_index" ON "_ArtistToTrack"("B");
3 changes: 3 additions & 0 deletions packages/core/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
31 changes: 22 additions & 9 deletions packages/core/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,37 @@ datasource db {
}

model Track {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
title String
duration BigInt
artists Artist[] @relation(fields: [name], references: [id])
album Album @relation(fields: [title, art], references: [id])
artists Artist[] @relation
album Collection @relation(fields: [id], references: [id])
track_number Int
}

model Artist {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
profile_image String
name String
tracks Track[] @relation(fields: [title, duration, artists, album], references: [id])
biography String
tracks Track[]
albums Collection[] @relation
}

model Album {
id Int @id @default(autoincrement())
model User {
id Int @id @default(autoincrement())
profile_image String
name String
playlists Collection[] @relation
}

model Collection {
id Int @id @default(autoincrement())
title String
art String
artist Artist @relation(fields: [name], references: [id])
art String?
artist Artist? @relation(fields: [id], references: [id])
user User? @relation(fields: [id], references: [id])
year Int?
duration BigInt
tracks Track[]
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'react-native-polyfill';
import 'core-js/actual/url';

import { AccountManager, AccountClass } from './account/index';
import { ItemManager } from './media/index';
import Player from './player/index';
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RawData } from "../plugin/index";
import { PrismaClient } from '@prisma/client'

interface ConnectionData {
address: string
Expand All @@ -7,7 +8,9 @@ interface ConnectionData {
declare const connection_data: ConnectionData

export class Database {
address: URL;
public internal = new PrismaClient({ log: [{ level: 'query', emit: 'event' }] });

protected server_address?: URL;

/**
* Requests
Expand All @@ -19,7 +22,11 @@ export class Database {
}

constructor () {
this.address = new URL(connection_data.address);
this.internal.$on('query', (query) => {
sendMessage('query', JSON.stringify(query));
})

if (connection_data) this.server_address = new URL(connection_data.address);
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"target": "ES2020"
"target": "ES2020",
"module": "NodeNext"
}
}
52 changes: 2 additions & 50 deletions packages/core/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,5 @@
const path = require('path');
const webpack = require('webpack');

var timers = {};
function generateNewId() {
var r = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
while (timers.hasOwnProperty(r)) { // check weather the id already exists
r = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
}
return r;
}


function check() {
if (Object.keys(timers).length <= 0) return;

var t = Date.now();
for (var timerId in timers) {
if (timers.hasOwnProperty(timerId) && t > timers[timerId].time) {
timers[timerId].callback();
clearTimeout(timerId);
}
}
requestIdleCallback(check);
}


function _setTimeout(callback, delay) {
if (typeof(callback) != 'function') throw new Error("callback should be a function");
if (typeof(delay) != 'number' || delay < 0) throw new Error("delay should be a positive number");
var newId = generateNewId();
timers[newId] = {
callback: callback,
time: Date.now() + delay
};
if (Object.keys(timers).length == 1) {
requestIdleCallback(check);
}
return newId;
}

function _clearTimeout(id) {
if (timers.hasOwnProperty(id)) delete timers[id];
}
const plugins = require('./pack/plugins');

module.exports = {
entry: './src/index.ts',
Expand All @@ -61,11 +19,5 @@ module.exports = {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new webpack.DefinePlugin({
setTimeout: _setTimeout,
clearTimeout: _clearTimeout,
})
//...
]
plugins
};
23 changes: 21 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions scripts/types/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export async function webpackDB(pkg_dir: string) {
const migrations: { date: number, sql: string }[] = [];

const output =
`const webpack = require('webpack');
export default new webpack.DefinePlugin({
database_migrations: {
index: [ ${migrations.map(m => m.date).join(', ')} ],
entries: {
${migrations.map(m => ` ${m.date}: \`${m.sql}\`,`).join('\n')}
}
}
})`
}

0 comments on commit 7e88088

Please sign in to comment.