Database Migration

How add database migration,(add new field to existing collection in data base need to take effect every user new and old).
for example - after the game launch in production.
As the part of updation of game i need to add more fields to existing character collection storage - like new “character” to the game.
it need to take effect in all users in databse, new and old too…
how will i do this, please help, i stuck in this for 3 months, Please help

we already created the database objects, also doing data management and crates opening and giving reward from these collections of data.

@novabyte @sesposito @ftkg

@Albertcleetus to clarify, you’d like to add new storage objects to all existing accounts?

yes , that’s the thing. please help @sesposito

@sesposito did you need any code screenshot or our system setup?

@Albertcleetus the best way to do this would be player-by-player instead of all the users.
Use an after hook on your authentication method call that checks if the storage objects exist and if not, creates them for the user.

you telling every time login a user, check that user had this collection or object in collection, if it’s not, create for it.

That’s correct. This means you only need to perform the work of migrating data for active players and is our recommended approach.

Can you share any Database migration script example here. please @sesposito

Using TS as an example, you’d do something like this:

function InitModule(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) {
    initializer.registerAfterAuthenticateEmail(addNewStorageObjectsFn);'JavaScript logic loaded.');

function addNewStorageObjectsFn(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, data: nkruntime.Session, request: nkruntime.AuthenticateEmailRequest): void  {
    let user = nk.usersGetUsername([request.username]);
    if (user.length != 1) {
        throw Error('User not found');

    try {
        let userId = user[0].userId;

        let objects = nk.storageRead([{
            collection: 'foo',
            key: 'bar',
            userId: userId,

        if (objects.length === 0) {
            // Object not found, create it

            let newObject = {
                food: ['apple', 'pear'],

                collection: 'foo',
                key: 'baz',
                userId: userId,
                value: newObject,
    } catch(error) {
        logger.error('Error: %v', error);

For further examples pelase check out our project template.

@sesposito Thank you so much , I going to integrate and let you know

Yes ,its worked my database migration work Completed , its perfect @sesposito