Skip to content

Commit

Permalink
feat: export data iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
hperrin committed Jun 18, 2024
1 parent 2689576 commit b86aa19
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 127 deletions.
84 changes: 60 additions & 24 deletions packages/driver-mysql/src/MySQLDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,18 +606,39 @@ export default class MySQLDriver extends NymphDriver {
return true;
}

protected async exportEntities(writeLine: (line: string) => void) {
writeLine('#nex2');
writeLine('# Nymph Entity Exchange v2');
writeLine('# http://nymph.io');
writeLine('#');
writeLine('# Generation Time: ' + new Date().toLocaleString());
writeLine('');

writeLine('#');
writeLine('# UIDs');
writeLine('#');
writeLine('');
public async *exportDataIterator(): AsyncGenerator<
{ type: 'comment' | 'uid' | 'entity'; content: string },
void,
false | undefined
> {
if (
yield {
type: 'comment',
content: `#nex2
# Nymph Entity Exchange v2
# http://nymph.io
#
# Generation Time: ${new Date().toLocaleString()}
`,
}
) {
return;
}

if (
yield {
type: 'comment',
content: `
#
# UIDs
#
`,
}
) {
return;
}

// Export UIDs.
let uids = await this.queryIter(
Expand All @@ -626,14 +647,25 @@ export default class MySQLDriver extends NymphDriver {
)} ORDER BY \`name\`;`,
);
for (const uid of uids) {
writeLine(`<${uid.name}>[${uid.cur_uid}]`);
if (yield { type: 'uid', content: `<${uid.name}>[${uid.cur_uid}]\n` }) {
return;
}
}

writeLine('');
writeLine('#');
writeLine('# Entities');
writeLine('#');
writeLine('');
if (
yield {
type: 'comment',
content: `
#
# Entities
#
`,
}
) {
return;
}

// Get the etypes.
const tables = await this.queryIter('SHOW TABLES;');
Expand Down Expand Up @@ -666,9 +698,10 @@ export default class MySQLDriver extends NymphDriver {
const tags = datum.value.tags.slice(1, -1).split(' ').join(',');
const cdate = datum.value.cdate;
const mdate = datum.value.mdate;
writeLine(`{${guid}}<${etype}>[${tags}]`);
writeLine(`\tcdate=${JSON.stringify(cdate)}`);
writeLine(`\tmdate=${JSON.stringify(mdate)}`);
let currentEntityExport: string[] = [];
currentEntityExport.push(`{${guid}}<${etype}>[${tags}]`);
currentEntityExport.push(`\tcdate=${JSON.stringify(cdate)}`);
currentEntityExport.push(`\tmdate=${JSON.stringify(mdate)}`);
if (datum.value.dname != null) {
// This do will keep going and adding the data until the
// next entity is reached. $row will end on the next entity.
Expand All @@ -679,17 +712,20 @@ export default class MySQLDriver extends NymphDriver {
: datum.value.dvalue === 'S'
? JSON.stringify(datum.value.string)
: datum.value.dvalue;
writeLine(`\t${datum.value.dname}=${value}`);
currentEntityExport.push(`\t${datum.value.dname}=${value}`);
datum = dataIterator.next();
} while (!datum.done && datum.value.guid === guid);
} else {
// Make sure that datum is incremented :)
datum = dataIterator.next();
}
currentEntityExport.push('');

if (yield { type: 'entity', content: currentEntityExport.join('\n') }) {
return;
}
}
}

return;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/driver-mysql/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"extends": "@tsconfig/recommended/tsconfig.json",

"compilerOptions": {
"lib": ["ES2021"],
"target": "ES2021",
"lib": ["ES2023"],
"module": "node16",
"target": "ES2022",
"noImplicitAny": true,
"removeComments": false,
"sourceMap": true,
Expand Down
84 changes: 60 additions & 24 deletions packages/driver-postgresql/src/PostgreSQLDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,18 +767,39 @@ export default class PostgreSQLDriver extends NymphDriver {
return true;
}

protected async exportEntities(writeLine: (line: string) => void) {
writeLine('#nex2');
writeLine('# Nymph Entity Exchange v2');
writeLine('# http://nymph.io');
writeLine('#');
writeLine('# Generation Time: ' + new Date().toLocaleString());
writeLine('');

writeLine('#');
writeLine('# UIDs');
writeLine('#');
writeLine('');
public async *exportDataIterator(): AsyncGenerator<
{ type: 'comment' | 'uid' | 'entity'; content: string },
void,
false | undefined
> {
if (
yield {
type: 'comment',
content: `#nex2
# Nymph Entity Exchange v2
# http://nymph.io
#
# Generation Time: ${new Date().toLocaleString()}
`,
}
) {
return;
}

if (
yield {
type: 'comment',
content: `
#
# UIDs
#
`,
}
) {
return;
}

// Export UIDs.
let uids = await this.queryIter(
Expand All @@ -787,14 +808,25 @@ export default class PostgreSQLDriver extends NymphDriver {
)} ORDER BY "name";`,
);
for (const uid of uids) {
writeLine(`<${uid.name}>[${uid.cur_uid}]`);
if (yield { type: 'uid', content: `<${uid.name}>[${uid.cur_uid}]\n` }) {
return;
}
}

writeLine('');
writeLine('#');
writeLine('# Entities');
writeLine('#');
writeLine('');
if (
yield {
type: 'comment',
content: `
#
# Entities
#
`,
}
) {
return;
}

// Get the etypes.
const tables = await this.queryIter(
Expand Down Expand Up @@ -829,9 +861,10 @@ export default class PostgreSQLDriver extends NymphDriver {
const tags = datum.value.tags.join(',');
const cdate = datum.value.cdate;
const mdate = datum.value.mdate;
writeLine(`{${guid}}<${etype}>[${tags}]`);
writeLine(`\tcdate=${JSON.stringify(cdate)}`);
writeLine(`\tmdate=${JSON.stringify(mdate)}`);
let currentEntityExport: string[] = [];
currentEntityExport.push(`{${guid}}<${etype}>[${tags}]`);
currentEntityExport.push(`\tcdate=${JSON.stringify(cdate)}`);
currentEntityExport.push(`\tmdate=${JSON.stringify(mdate)}`);
if (datum.value.dname != null) {
// This do will keep going and adding the data until the
// next entity is reached. $row will end on the next entity.
Expand All @@ -842,17 +875,20 @@ export default class PostgreSQLDriver extends NymphDriver {
: datum.value.dvalue === 'S'
? JSON.stringify(datum.value.string)
: datum.value.dvalue;
writeLine(`\t${datum.value.dname}=${value}`);
currentEntityExport.push(`\t${datum.value.dname}=${value}`);
datum = dataIterator.next();
} while (!datum.done && datum.value.guid === guid);
} else {
// Make sure that datum is incremented :)
datum = dataIterator.next();
}
currentEntityExport.push('');

if (yield { type: 'entity', content: currentEntityExport.join('\n') }) {
return;
}
}
}

return;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/driver-postgresql/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"extends": "@tsconfig/recommended/tsconfig.json",

"compilerOptions": {
"lib": ["ES2021"],
"target": "ES2021",
"lib": ["ES2023"],
"module": "node16",
"target": "ES2022",
"noImplicitAny": true,
"removeComments": false,
"sourceMap": true,
Expand Down
82 changes: 59 additions & 23 deletions packages/driver-sqlite3/src/SQLite3Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,18 +602,39 @@ export default class SQLite3Driver extends NymphDriver {
return true;
}

protected async exportEntities(writeLine: (line: string) => void) {
writeLine('#nex2');
writeLine('# Nymph Entity Exchange v2');
writeLine('# http://nymph.io');
writeLine('#');
writeLine('# Generation Time: ' + new Date().toLocaleString());
writeLine('');
public async *exportDataIterator(): AsyncGenerator<
{ type: 'comment' | 'uid' | 'entity'; content: string },
void,
false | undefined
> {
if (
yield {
type: 'comment',
content: `#nex2
# Nymph Entity Exchange v2
# http://nymph.io
#
# Generation Time: ${new Date().toLocaleString()}
`,
}
) {
return;
}

if (
yield {
type: 'comment',
content: `
writeLine('#');
writeLine('# UIDs');
writeLine('#');
writeLine('');
#
# UIDs
#
`,
}
) {
return;
}

// Export UIDs.
let uids: IterableIterator<any> = this.queryIter(
Expand All @@ -622,14 +643,25 @@ export default class SQLite3Driver extends NymphDriver {
)} ORDER BY "name";`,
);
for (const uid of uids) {
writeLine(`<${uid.name}>[${uid.cur_uid}]`);
if (yield { type: 'uid', content: `<${uid.name}>[${uid.cur_uid}]\n` }) {
return;
}
}

writeLine('');
writeLine('#');
writeLine('# Entities');
writeLine('#');
writeLine('');
if (
yield {
type: 'comment',
content: `
#
# Entities
#
`,
}
) {
return;
}

// Get the etypes.
const tables: IterableIterator<any> = this.queryIter(
Expand Down Expand Up @@ -659,9 +691,10 @@ export default class SQLite3Driver extends NymphDriver {
const tags = datum.value.tags.slice(1, -1);
const cdate = datum.value.cdate;
const mdate = datum.value.mdate;
writeLine(`{${guid}}<${etype}>[${tags}]`);
writeLine(`\tcdate=${JSON.stringify(cdate)}`);
writeLine(`\tmdate=${JSON.stringify(mdate)}`);
let currentEntityExport: string[] = [];
currentEntityExport.push(`{${guid}}<${etype}>[${tags}]`);
currentEntityExport.push(`\tcdate=${JSON.stringify(cdate)}`);
currentEntityExport.push(`\tmdate=${JSON.stringify(mdate)}`);
if (datum.value.dname != null) {
// This do will keep going and adding the data until the
// next entity is reached. datum will end on the next entity.
Expand All @@ -672,17 +705,20 @@ export default class SQLite3Driver extends NymphDriver {
: datum.value.dvalue === 'S'
? JSON.stringify(datum.value.string)
: datum.value.dvalue;
writeLine(`\t${datum.value.dname}=${value}`);
currentEntityExport.push(`\t${datum.value.dname}=${value}`);
datum = dataIterator.next();
} while (!datum.done && datum.value.guid === guid);
} else {
// Make sure that datum is incremented :)
datum = dataIterator.next();
}
currentEntityExport.push('');

if (yield { type: 'entity', content: currentEntityExport.join('\n') }) {
return;
}
}
}

return;
}

/**
Expand Down
Loading

0 comments on commit b86aa19

Please sign in to comment.