Skip to content

Commit 692c222

Browse files
authored
Merge pull request #779 from appwrite/feat-clientside-id-gen
Generate Appwrite ID's on the Client-Side
2 parents c9c07e8 + 1549570 commit 692c222

File tree

17 files changed

+348
-120
lines changed

17 files changed

+348
-120
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ jobs:
6767
run: composer install
6868

6969
- name: Lint
70-
run: composer lint
70+
run: composer lint

composer.lock

Lines changed: 90 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
package {{ sdk.namespace | caseDot }}
22

3+
import java.time.Instant
4+
import kotlin.math.floor
5+
import kotlin.random.Random
6+
37
class ID {
48
companion object {
9+
// Generate an hex ID based on timestamp
10+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
11+
private fun hexTimestamp(): String {
12+
val now = Instant.now()
13+
val sec = now.epochSecond
14+
val usec = (System.nanoTime() / 1000) % 1000
15+
16+
val hexTimestamp = "%08x%05x".format(sec, usec)
17+
18+
return hexTimestamp
19+
}
20+
521
fun custom(id: String): String
622
= id
7-
fun unique(): String
8-
= "unique()"
23+
24+
// Generate a unique ID with padding to have a longer ID
25+
fun unique(padding: Int = 7): String {
26+
val baseId = hexTimestamp()
27+
val randomPadding = (1..padding)
28+
.map { Random.nextInt(0, 16).toString(16) }
29+
.joinToString("")
30+
31+
return baseId + randomPadding
32+
}
933
}
10-
}
34+
}

templates/dart/lib/id.dart.twig

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,34 @@ part of {{ language.params.packageName }};
44
class ID {
55
ID._();
66

7-
/// Have Appwrite generate a unique ID for you.
8-
static String unique() {
9-
return 'unique()';
7+
// Generate an hex ID based on timestamp
8+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
9+
static String _hexTimestamp() {
10+
final now = DateTime.now();
11+
final sec = (now.millisecondsSinceEpoch / 1000).floor();
12+
final usec = now.microsecondsSinceEpoch - (sec * 1000000);
13+
return sec.toRadixString(16) +
14+
usec.toRadixString(16).padLeft(5, '0');
15+
}
16+
17+
// Generate a unique ID with padding to have a longer ID
18+
static String unique({int padding = 7}) {
19+
String id = _hexTimestamp();
20+
21+
if (padding > 0) {
22+
StringBuffer sb = StringBuffer();
23+
for (var i = 0; i < padding; i++) {
24+
sb.write(Random().nextInt(16).toRadixString(16));
25+
}
26+
27+
id += sb.toString();
28+
}
29+
30+
return id;
1031
}
1132

1233
/// Uses [id] as the ID for the resource.
1334
static String custom(String id) {
1435
return id;
1536
}
16-
}
37+
}

templates/dart/lib/package.dart.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
library {{ language.params.packageName }};
77

88
import 'dart:async';
9+
import 'dart:math';
910
import 'dart:typed_data';
1011
import 'dart:convert';
1112

templates/dart/test/id_test.dart.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import 'package:flutter_test/flutter_test.dart';
77

88
void main() {
99
group('unique()', () {
10-
test('returns unique()', () {
11-
expect(ID.unique(), 'unique()');
10+
test('returns unique ID', () {
11+
expect((ID.unique()).length, 20);
1212
});
1313
});
1414

templates/deno/src/id.ts.twig

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
export class ID {
2+
// Generate an hex ID based on timestamp
3+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
4+
static #hexTimestamp(): string {
5+
const now = new Date();
6+
const sec = Math.floor(now.getTime() / 1000);
7+
const msec = now.getMilliseconds();
8+
9+
// Convert to hexadecimal
10+
const hexTimestamp = sec.toString(16) + msec.toString(16).padStart(5, '0');
11+
return hexTimestamp;
12+
}
13+
214
public static custom(id: string): string {
315
return id
416
}
5-
6-
public static unique(): string {
7-
return 'unique()'
17+
18+
// Generate a unique ID with padding to have a longer ID
19+
public static unique(padding: number = 7): string {
20+
const baseId = ID.#hexTimestamp();
21+
let randomPadding = '';
22+
23+
for (let i = 0; i < padding; i++) {
24+
const randomHexDigit = Math.floor(Math.random() * 16).toString(16);
25+
randomPadding += randomHexDigit;
26+
}
27+
28+
return baseId + randomPadding;
829
}
9-
}
30+
}
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1+
using System;
2+
13
namespace {{ spec.title | caseUcfirst }}
24
{
35
public static class ID
46
{
5-
public static string Unique()
7+
// Generate an hex ID based on timestamp
8+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
9+
private static string HexTimestamp()
610
{
7-
return "unique()";
11+
var now = DateTime.UtcNow;
12+
var epoch = (now - new DateTime(1970, 1, 1));
13+
var sec = (long)epoch.TotalSeconds;
14+
var usec = (long)((epoch.TotalMilliseconds * 1000) % 1000);
15+
16+
// Convert to hexadecimal
17+
var hexTimestamp = sec.ToString("x") + usec.ToString("x").PadLeft(5, '0');
18+
return hexTimestamp;
19+
}
20+
21+
// Generate a unique ID with padding to have a longer ID
22+
public static string Unique(int padding = 7)
23+
{
24+
var random = new Random();
25+
var baseId = HexTimestamp();
26+
var randomPadding = "";
27+
28+
for (int i = 0; i < padding; i++)
29+
{
30+
var randomHexDigit = random.Next(0, 16).ToString("x");
31+
randomPadding += randomHexDigit;
32+
}
33+
34+
return baseId + randomPadding;
835
}
936

1037
public static string Custom(string id)
1138
{
1239
return id;
1340
}
1441
}
15-
}
42+
}

templates/flutter/lib/package.dart.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
library {{ language.params.packageName }};
77

88
import 'dart:async';
9+
import 'dart:math';
910
import 'dart:typed_data';
1011
import 'dart:convert';
1112

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
package {{ sdk.namespace | caseDot }}
22

3+
import java.time.Instant
4+
import kotlin.math.floor
5+
import kotlin.random.Random
6+
37
class ID {
48
companion object {
9+
// Generate an hex ID based on timestamp
10+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
11+
private fun hexTimestamp(): String {
12+
val now = Instant.now()
13+
val sec = now.epochSecond
14+
val usec = (System.nanoTime() / 1000) % 1000
15+
16+
val hexTimestamp = "%08x%05x".format(sec, usec)
17+
18+
return hexTimestamp
19+
}
20+
521
fun custom(id: String): String
622
= id
7-
fun unique(): String
8-
= "unique()"
23+
// Generate a unique ID with padding to have a longer ID
24+
fun unique(padding: Int = 7): String {
25+
val baseId = ID.hexTimestamp()
26+
val randomPadding = (1..padding)
27+
.map { Random.nextInt(0, 16).toString(16) }
28+
.joinToString("")
29+
30+
return baseId + randomPadding
31+
}
932
}
10-
}
33+
}

0 commit comments

Comments
 (0)